swift_mt_message/messages/mt940.rs
1use crate::fields::*;
2use serde::{Deserialize, Serialize};
3use swift_mt_message_macros::{SwiftMessage, serde_swift_fields};
4
5/// MT940: Customer Statement Message
6///
7/// ## Purpose
8/// Used to transmit detailed account statement information from an account servicing institution
9/// to the account holder. This message provides a complete transaction-by-transaction record
10/// of account activity with opening and closing balances for a specific period.
11///
12/// ## Scope
13/// This message is:
14/// - Sent by account servicing institutions to account holders
15/// - Used for regular account statement transmission (daily, weekly, monthly)
16/// - Applied to various account types including current, savings, and foreign currency accounts
17/// - Essential for account reconciliation and cash management
18/// - Part of automated statement delivery and cash management systems
19///
20/// ## Key Features
21/// - **Complete Transaction Detail**: Full transaction-by-transaction statement
22/// - **Balance Information**: Opening and closing balances with currency consistency
23/// - **Statement Line Details**: Individual transaction entries with references and descriptions
24/// - **Value Dating**: Precise value dates for each transaction
25/// - **Available Balance**: Optional available balance information for credit management
26/// - **Information Lines**: Additional transaction details and narrative information
27///
28/// ## Common Use Cases
29/// - Daily account statement delivery
30/// - End-of-month statement transmission
31/// - Real-time account activity reporting
32/// - Cash management system integration
33/// - Account reconciliation support
34/// - Regulatory reporting and compliance
35/// - Customer self-service portal integration
36/// - Treasury management system feeds
37///
38/// ## Message Structure
39/// ### Header Information
40/// - **20**: Transaction Reference (mandatory) - Unique statement reference
41/// - **21**: Related Reference (optional) - Reference to related statement or period
42/// - **25**: Account Identification (mandatory) - Account being reported
43/// - **28C**: Statement Number/Sequence (mandatory) - Statement numbering
44/// - **60**: Opening Balance (mandatory) - Starting balance for statement period
45///
46/// ### Transaction Details
47/// - **Statement Lines**: Repetitive sequence of individual transactions
48/// - **62**: Closing Balance (mandatory) - Ending balance for statement period
49/// - **64**: Available Balance (optional) - Available credit or debit balance
50/// - **65**: Forward Available Balance (optional, repetitive) - Future available balances
51/// - **86**: Information to Account Owner (optional) - Additional statement information
52///
53/// ### Statement Line Structure (MT940StatementLine)
54/// Each statement line contains:
55/// - **61**: Statement Line (optional) - Individual transaction details
56/// - **86**: Information to Account Owner (optional) - Additional transaction information
57///
58/// ## Field Details
59/// ### Field 60/62 - Balance Information
60/// - **Currency**: ISO 4217 3-character currency code
61/// - **Amount**: Balance amount with appropriate precision
62/// - **Date**: Balance date (YYMMDD format)
63/// - **Credit/Debit Indicator**: C (Credit) or D (Debit) balance
64///
65/// ### Field 61 - Statement Line
66/// - **Value Date**: Date when transaction becomes effective
67/// - **Entry Date**: Date when transaction was posted (optional)
68/// - **Credit/Debit Mark**: C (Credit) or D (Debit) entry
69/// - **Amount**: Transaction amount
70/// - **Transaction Type**: SWIFT transaction type identification
71/// - **Reference**: Transaction reference number
72/// - **Account Servicing Institution Reference**: Bank's internal reference
73///
74/// ## Network Validation Rules
75/// - **Currency Consistency**: Opening and closing balances must use the same currency
76/// - **Reference Format**: Transaction references must follow SWIFT formatting standards
77/// - **Required Fields**: All mandatory fields must be present and properly formatted
78/// - **Balance Logic**: Closing balance should reflect opening balance plus/minus transactions
79/// - **Date Validation**: All dates must be valid and in proper sequence
80/// - **Amount Validation**: All amounts must be properly formatted with currency precision
81///
82/// ## Processing Context
83/// ### Statement Generation
84/// 1. Account activity accumulated over statement period
85/// 2. Transactions sorted by value date and sequence
86/// 3. Opening balance carried forward from previous statement
87/// 4. MT940 generated with complete transaction detail
88/// 5. Closing balance calculated and validated
89///
90/// ### Cash Management Integration
91/// - Real-time balance updating
92/// - Transaction categorization and analysis
93/// - Cash flow forecasting input
94/// - Reconciliation automation
95/// - Exception reporting and investigation
96///
97/// ## SRG2025 Status
98/// - **No Structural Changes**: MT940 format remains unchanged in SRG2025
99/// - **Enhanced Validation**: Additional validation for statement accuracy and completeness
100/// - **Digital Integration**: Improved support for digital banking and API integration
101/// - **Real-time Capabilities**: Enhanced support for real-time statement delivery
102///
103/// ## Integration Considerations
104/// - **Banking Systems**: Core component of account management and customer communication
105/// - **Cash Management**: Primary input for automated cash management and forecasting
106/// - **ERP Integration**: Critical for enterprise financial management and reconciliation
107/// - **Regulatory Reporting**: Essential for compliance and audit trail requirements
108///
109/// ## Relationship to Other Messages
110/// - **Triggered by**: MT920 (Request Message) for on-demand statement delivery
111/// - **Complements**: MT900/MT910 confirmation messages for real-time transaction notification
112/// - **Supports**: Complete account management and cash management workflows
113/// - **Integrates with**: Customer communication and digital banking platforms
114
115#[serde_swift_fields]
116#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftMessage)]
117#[validation_rules(MT940_VALIDATION_RULES)]
118pub struct MT940 {
119 #[field("20")]
120 pub field_20: Field20,
121
122 #[field("21")]
123 pub field_21: Option<Field21NoOption>,
124
125 #[field("25")]
126 pub field_25: Field25AccountIdentification,
127
128 #[field("28C")]
129 pub field_28c: Field28C,
130
131 #[field("60")]
132 pub field_60: Field60,
133
134 #[field("#")]
135 pub statement_lines: Vec<MT940StatementLine>,
136
137 #[field("62")]
138 pub field_62: Field62,
139
140 #[field("64")]
141 pub field_64: Option<Field64>,
142
143 #[field("65")]
144 pub field_65: Option<Vec<Field65>>,
145
146 #[field("86")]
147 pub field_86: Option<Field86>,
148}
149
150#[serde_swift_fields]
151#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftMessage)]
152pub struct MT940StatementLine {
153 #[field("61")]
154 pub field_61: Option<Field61>,
155
156 #[field("86")]
157 pub field_86: Option<Field86>,
158}
159
160/// Validation rules for MT940 - Customer Statement Message
161const MT940_VALIDATION_RULES: &str = r#"{
162 "rules": [
163 {
164 "id": "C1",
165 "description": "The repetitive sequence starting with field 61 must appear at least once and no more than 500 times",
166 "condition": {
167 "and": [
168 {">=": [{"length": {"var": "fields.#"}}, 1]},
169 {"<=": [{"length": {"var": "fields.#"}}, 500]}
170 ]
171 }
172 },
173 {
174 "id": "C2",
175 "description": "If field 64 is present, field 60F must also be present, and field 62F must also be present",
176 "condition": {
177 "if": [
178 {"exists": ["fields", "64"]},
179 {
180 "and": [
181 {"exists": ["fields", "60"]},
182 {"exists": ["fields", "62"]}
183 ]
184 },
185 true
186 ]
187 }
188 }
189 ]
190}"#;