swift_mt_message/messages/
mt205.rs

1use crate::fields::*;
2use serde::{Deserialize, Serialize};
3use swift_mt_message_macros::{SwiftMessage, serde_swift_fields};
4
5/// MT205: General Financial Institution Transfer
6///
7/// ## Purpose
8/// Used for financial institution transfers where the ordering institution is always identified.
9/// This message type is specifically designed for transfers initiated by a financial institution
10/// for its own account or on behalf of its customers, with mandatory ordering institution details.
11///
12/// ## Scope
13/// This message is:
14/// - Sent between financial institutions for institutional transfers
15/// - Used when the ordering institution must be explicitly identified (unlike MT202)
16/// - Applicable for both direct transfers and cover payments
17/// - Compatible with correspondent banking arrangements
18/// - Subject to enhanced validation rules for cross-border payments
19/// - Includes contingency processing capabilities for qualifying transfers
20///
21/// ## Key Features
22/// - **Mandatory Ordering Institution**: Field 52 is always required (key difference from MT202)
23/// - **Dual Sequence Architecture**:
24///   - Sequence A: Institution-to-institution transfer details
25///   - Sequence B: Underlying customer payment details (for cover payments)
26/// - **Enhanced Validation**: Sophisticated rules for institution identification and routing
27/// - **Cover Payment Support**: Full support for cover payment scenarios with customer details
28/// - **Settlement Flexibility**: Compatible with various settlement mechanisms
29/// - **Cross-Currency Capability**: Support for foreign exchange operations
30///
31/// ## Common Use Cases
32/// - Central bank operations requiring explicit ordering institution identification
33/// - Correspondent banking transfers with mandatory institution details
34/// - Cross-border institutional payments
35/// - Treasury operations between financial institutions
36/// - Cover payments for underlying customer transfers
37/// - Settlement of securities transactions
38/// - Liquidity management between institutions
39///
40/// ## Message Structure
41/// ### Sequence A (Institution Transfer Details)
42/// - **Field 20**: Transaction Reference (mandatory) - Unique transaction identifier
43/// - **Field 21**: Related Reference (mandatory) - Reference to related transaction/message
44/// - **Field 13C**: Time Indication (optional, repetitive) - Processing time constraints
45/// - **Field 32A**: Value Date/Currency/Amount (mandatory) - Settlement amount and timing
46/// - **Field 52**: Ordering Institution (mandatory) - Institution initiating the transfer
47/// - **Field 53**: Sender's Correspondent (optional) - Sender's correspondent bank
48/// - **Field 56**: Intermediary Institution (optional) - Intermediary in payment chain
49/// - **Field 57**: Account With Institution (optional) - Crediting institution
50/// - **Field 58**: Beneficiary Institution (mandatory) - Final beneficiary institution
51/// - **Field 72**: Sender to Receiver Information (optional) - Additional instructions
52///
53/// ### Sequence B (Cover Payment Details - Optional)
54/// - **Field 50**: Ordering Customer (optional) - Underlying ordering customer
55/// - **Field 52**: Ordering Institution (optional) - Additional ordering institution details
56/// - **Field 56**: Intermediary Institution (optional) - Cover payment intermediary
57/// - **Field 57**: Account With Institution (optional) - Cover payment account details
58/// - **Field 59**: Beneficiary Customer (optional) - Underlying beneficiary customer
59/// - **Field 70**: Remittance Information (optional) - Payment purpose and details
60/// - **Field 72**: Sender to Receiver Info (optional) - Cover-specific instructions
61/// - **Field 33B**: Currency/Instructed Amount (optional) - Original currency/amount
62///
63/// ## Network Validation Rules
64/// - **Field 52 Mandatory**: Ordering institution validation (field 52) - key difference from MT202
65/// - **Cover Payment Structure**: Validation of Sequence B customer fields presence
66/// - **Cross-Currency Validation**: Currency consistency between fields 32A and 33B
67/// - **Correspondent Chain**: Banking chain validation for intermediary institutions
68/// - **Settlement Method**: Determination based on correspondent relationships
69/// - **Time Indication**: Compliance checking for CLS/TARGET timing constraints
70/// - **REJT/RETN Indicators**: Structured validation of reject/return codes in field 72
71///
72/// ## SRG2025 Status
73/// - **Structural Changes**: None - MT205 structure remains unchanged
74/// - **Enhanced Validation**: Additional network rules for institutional transfers
75/// - **Contingency Processing**: Enhanced processing rules for qualifying transfers
76/// - **Cross-Border Compliance**: Strengthened validation for international payments
77///
78/// ## Integration Considerations
79/// - **Banking Systems**: Compatible with real-time gross settlement (RTGS) systems
80/// - **Central Bank Operations**: Direct integration with central bank settlement mechanisms
81/// - **API Integration**: RESTful API support for modern banking infrastructure
82/// - **Processing Requirements**: Support for time-critical payment processing
83///
84/// ## Relationship to Other Messages
85/// - **Triggers**: Can be triggered by MT202COV or customer payment instructions
86/// - **Responses**: May generate MT202, MT210 (notice to receive), or MT292 (reject)
87/// - **Related**: Works with MT950/MT940 for account reporting and confirmation
88/// - **Alternatives**: MT202 for transfers without mandatory ordering institution
89/// - **Cover Payments**: Supports underlying MT103 customer credit transfers
90
91#[serde_swift_fields]
92#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftMessage)]
93#[validation_rules(MT205_VALIDATION_RULES)]
94pub struct MT205 {
95    // Sequence A: Mandatory Fields
96    #[field("20")]
97    pub field_20: Field20, // Transaction Reference Number
98
99    #[field("21")]
100    pub field_21: Field21NoOption, // Related Reference
101
102    #[field("13C")]
103    pub field_13c: Option<Vec<Field13C>>, // Time Indication (repetitive)
104
105    #[field("32A")]
106    pub field_32a: Field32A, // Value Date/Currency/Amount
107
108    #[field("52")]
109    pub field_52: Field52OrderingInstitution, // Ordering Institution (MANDATORY in MT205)
110
111    #[field("53")]
112    pub field_53: Option<Field53SenderCorrespondent>, // Sender's Correspondent
113
114    #[field("56")]
115    pub field_56: Option<Field56Intermediary>, // Intermediary Institution
116
117    #[field("57")]
118    pub field_57: Option<Field57AccountWithInstitution>, // Account With Institution
119
120    #[field("58")]
121    pub field_58: Field58, // Beneficiary Institution
122
123    #[field("72")]
124    pub field_72: Option<Field72>, // Sender to Receiver Information
125
126    // Sequence B: COV Cover Message Fields (Optional)
127    #[field("50#b")]
128    pub field_50_seq_b: Option<Field50OrderingCustomerAFK>, // Ordering Customer
129
130    #[field("52#b")]
131    pub field_52_seq_b: Option<Field52OrderingInstitution>, // Ordering Institution (Seq B)
132
133    #[field("56#b")]
134    pub field_56_seq_b: Option<Field56Intermediary>, // Intermediary Institution (Seq B)
135
136    #[field("57#b")]
137    pub field_57_seq_b: Option<Field57AccountWithInstitution>, // Account With Institution (Seq B)
138
139    #[field("59#b")]
140    pub field_59_seq_b: Option<Field59>, // Beneficiary Customer
141
142    #[field("70#b")]
143    pub field_70_seq_b: Option<Field70>, // Remittance Information
144
145    #[field("72#b")]
146    pub field_72_seq_b: Option<Field72>, // Sender to Receiver Info (Seq B)
147
148    #[field("33B#b")]
149    pub field_33b_seq_b: Option<Field33B>, // Currency/Instructed Amount
150}
151
152impl MT205 {
153    /// Check if this MT205 message contains reject codes
154    ///
155    /// Reject messages are identified by checking:
156    /// 1. Field 20 (Transaction Reference) for "REJT" prefix or content
157    /// 2. Field 72 (Sender to Receiver Information) containing `/REJT/` codes
158    /// 3. Additional structured reject information in field 72
159    pub fn has_reject_codes(&self) -> bool {
160        // Check field 20 (transaction reference)
161        if self.field_20.reference.to_uppercase().contains("REJT") {
162            return true;
163        }
164
165        // Check field 72 for structured reject codes
166        if let Some(field_72) = &self.field_72 {
167            let content = field_72.information.join(" ").to_uppercase();
168            if content.contains("/REJT/") || content.contains("REJT") {
169                return true;
170            }
171        }
172
173        false
174    }
175
176    /// Check if this MT205 message contains return codes
177    ///
178    /// Return messages are identified by checking:
179    /// 1. Field 20 (Transaction Reference) for "RETN" prefix or content
180    /// 2. Field 72 (Sender to Receiver Information) containing `/RETN/` codes
181    /// 3. Additional structured return information in field 72
182    pub fn has_return_codes(&self) -> bool {
183        // Check field 20 (transaction reference)
184        if self.field_20.reference.to_uppercase().contains("RETN") {
185            return true;
186        }
187
188        // Check field 72 for structured return codes
189        if let Some(field_72) = &self.field_72 {
190            let content = field_72.information.join(" ").to_uppercase();
191            if content.contains("/RETN/") || content.contains("RETN") {
192                return true;
193            }
194        }
195
196        false
197    }
198
199    /// Check if this MT205 message is a Cover (COV) message
200    ///
201    /// COV messages are distinguished by:
202    /// - Presence of Sequence B customer fields (50a, 59a)
203    /// - Additional underlying customer credit transfer details
204    ///
205    /// Based on the MT205 specification: "Cover Detection: Based on presence of Sequence B customer fields (50a, 59a)"
206    pub fn is_cover_message(&self) -> bool {
207        // The key distinguishing feature of COV is the presence of Sequence B customer fields
208        // According to spec: field 50a (Ordering Customer) or field 59a (Beneficiary Customer)
209        self.field_50_seq_b.is_some() || self.field_59_seq_b.is_some()
210    }
211}
212
213/// Validation rules for MT205 - General Financial Institution Transfer
214const MT205_VALIDATION_RULES: &str = r#"{
215	"rules": [
216		{
217			"id": "C1",
218			"description": "If field 56a is present, then field 57a must also be present",
219			"condition": {
220				"if": [
221					{"exists": ["fields", "56"]},
222					{"exists": ["fields", "57"]},
223					true
224				]
225			}
226		},
227		{
228			"id": "C2",
229			"description": "If field 56a is present in Sequence B, then field 57a must also be present",
230			"condition": {
231				"if": [
232					{"exists": ["fields", "56#b"]},
233					{"exists": ["fields", "57#b"]},
234					true
235				]
236			}
237		},
238		{
239			"id": "COV_FIELDS",
240			"description": "MT205 COV must include both field 50a (Ordering Customer) and 59a (Beneficiary)",
241			"condition": {
242				"if": [
243					{"or": [
244						{"exists": ["fields", "50#b"]},
245						{"exists": ["fields", "59#b"]}
246					]},
247					{"and": [
248						{"exists": ["fields", "50#b"]},
249						{"exists": ["fields", "59#b"]}
250					]},
251					true
252				]
253			}
254		}
255	]
256}"#;