swift_mt_message/messages/mt204.rs
1use crate::fields::*;
2use serde::{Deserialize, Serialize};
3use swift_mt_message_macros::{SwiftMessage, serde_swift_fields};
4
5/// MT204: Financial Markets Direct Debit Message
6///
7/// ## Purpose
8/// Used for direct debit transactions in financial markets, particularly for settlement of trades
9/// and market-related obligations. This message enables efficient collection of funds from multiple
10/// debtors in financial market transactions, supporting both single and bulk debit operations.
11///
12/// ## Scope
13/// This message is:
14/// - Used between financial institutions for market-related direct debits
15/// - Designed for settlement of securities trades and derivatives transactions
16/// - Applicable for margin calls and collateral management
17/// - Used for collecting clearing house obligations
18/// - Compatible with central counterparty (CCP) settlement systems
19/// - Limited to a maximum of 10 transactions per message
20///
21/// ## Key Features
22/// - **Two-Sequence Structure**: Common reimbursement details and individual transaction details
23/// - **Bulk Processing**: Processes up to 10 direct debit transactions in one message
24/// - **Sum Validation**: Built-in validation that sum of transactions equals total amount
25/// - **Currency Consistency**: All transactions must use the same currency
26/// - **Efficient Settlement**: Optimized for financial markets settlement processes
27/// - **Reimbursement Focus**: Streamlined for financial institution reimbursements
28///
29/// ## Common Use Cases
30/// - Securities trade settlement collections
31/// - Derivatives margin call collections
32/// - Clearing house member collections
33/// - Central securities depository (CSD) fee collections
34/// - Exchange membership fee collections
35/// - Market maker obligation settlements
36/// - Cross-border financial market settlements
37///
38/// ## Message Structure
39/// ### Sequence A (Common Elements - Reimbursement Details - Mandatory, Single)
40/// - **Field 20**: Transaction Reference Number (mandatory) - Unique message identifier
41/// - **Field 19**: Sum of Amounts (mandatory) - Total amount to be collected
42/// - **Field 30**: Value Date (mandatory) - Settlement date for all transactions
43/// - **Field 57**: Account With Institution (optional) - Institution holding the account
44/// - **Field 58**: Beneficiary Institution (optional) - Final beneficiary institution
45/// - **Field 72**: Sender to Receiver Information (optional) - Additional instructions
46///
47/// ### Sequence B (Transaction Details - Mandatory, Repetitive, Max 10)
48/// - **Field 20**: Transaction Reference Number (mandatory) - Individual transaction reference
49/// - **Field 21**: Related Reference (optional) - Reference to related transaction/trade
50/// - **Field 32B**: Transaction Amount (mandatory) - Individual debit amount
51/// - **Field 53**: Debit Institution (mandatory) - Institution to be debited
52/// - **Field 72**: Sender to Receiver Information (optional) - Transaction-specific instructions
53///
54/// ## Network Validation Rules
55/// - **C1**: Field 19 amount must equal sum of all Field 32B amounts
56/// - **C2**: Currency in all Field 32B occurrences must be identical
57/// - **C3**: Maximum 10 occurrences of Sequence B allowed (T10 error if exceeded)
58///
59/// ## Integration Considerations
60/// - **Trading Systems**: Direct integration with trading and settlement platforms
61/// - **CCP Systems**: Compatible with central counterparty clearing systems
62/// - **Risk Management**: Integration with margin and collateral management systems
63/// - **Regulatory Reporting**: Supports transaction reporting requirements
64///
65/// ## Relationship to Other Messages
66/// - **Triggers**: Often triggered by trade confirmations or margin calculations
67/// - **Related**: Works with MT202 for cover payments and MT210 for pre-notifications
68/// - **Confirmations**: May generate MT900 (debit) confirmations
69/// - **Status**: May receive MT296 for cancellation responses
70/// - **Reporting**: Reflected in MT940/MT950 account statements
71#[serde_swift_fields]
72#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftMessage)]
73#[validation_rules(MT204_VALIDATION_RULES)]
74pub struct MT204 {
75 // Sequence A: Common Elements - Reimbursement Details
76 #[field("20")]
77 pub field_20: Field20,
78
79 #[field("19")]
80 pub field_19: Field19,
81
82 #[field("30")]
83 pub field_30: Field30,
84
85 #[field("57")]
86 pub field_57_seq_a: Option<Field57DebtInstitution>,
87
88 #[field("58")]
89 pub field_58: Option<Field58>,
90
91 #[field("72")]
92 pub field_72_seq_a: Option<Field72>,
93
94 // Sequence B: Transaction Details (repetitive)
95 #[field("#")]
96 pub transactions: Vec<MT204Transaction>,
97}
98
99/// Individual transaction details for MT204 Sequence B
100#[serde_swift_fields]
101#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftMessage)]
102pub struct MT204Transaction {
103 #[field("20")]
104 pub field_20: Field20,
105
106 #[field("21")]
107 pub field_21: Option<Field21NoOption>,
108
109 #[field("32B")]
110 pub field_32b: Field32B,
111
112 #[field("53")]
113 pub field_53: Field53SenderCorrespondent,
114
115 #[field("72")]
116 pub field_72: Option<Field72>,
117}
118
119// Validation rules for MT204 using JSONLogic
120pub const MT204_VALIDATION_RULES: &str = r##"{
121 "rules": [
122 {
123 "id": "C1",
124 "description": "The amount in field 19 must equal the sum of the amounts in all occurrences of field 32B",
125 "condition": {
126 "==": [
127 {"var": "fields.19.amount"},
128 {"reduce": [
129 {"var": "fields.#"},
130 {"+": [{"var": "accumulator"}, {"var": "current.32B.amount"}]},
131 0
132 ]}
133 ]
134 }
135 },
136 {
137 "id": "C2",
138 "description": "The currency code in the amount field 32B must be the same for all occurrences of this field in the message",
139 "condition": {
140 "all": [
141 {"var": "fields.#"},
142 {
143 "==": [
144 {"var": "32B.currency"},
145 {"val": [[-3], "fields", "#", 0, "32B", "currency"]}
146 ]
147 }
148 ]
149 }
150 },
151 {
152 "id": "C3",
153 "description": "The repetitive sequence must not appear more than ten times",
154 "condition": {
155 "if": [
156 {"exists": ["fields", "#"]},
157 {"<=": [{"length": {"var": "fields.#"}}, 10]},
158 true
159 ]
160 }
161 }
162 ]
163}"##;