swift_mt_message/messages/
mt941.rs

1use crate::fields::*;
2use serde::{Deserialize, Serialize};
3use swift_mt_message_macros::{SwiftMessage, serde_swift_fields};
4
5/// # MT941: Balance Report
6///
7/// This message is used by financial institutions to report account balance
8/// information to their customers or correspondent banks. It provides a summary
9/// of account balances at specific value dates without detailed transaction
10/// information, making it ideal for balance monitoring and cash management.
11///
12/// ## Key Features
13/// - **Balance reporting**: Summary of account balances at specific dates
14/// - **Multi-date balances**: Forward value dates for liquidity planning
15/// - **Cash management**: Real-time balance monitoring capabilities
16/// - **Correspondent banking**: Inter-bank balance reporting
17/// - **Treasury operations**: Daily balance reconciliation
18/// - **Liquidity management**: Available funds tracking
19///
20/// ## Field Structure
21/// All fields follow the enhanced macro system with proper validation rules.
22/// The message supports repetitive balance lines for multiple value dates.
23///
24/// ## Business Rules
25/// - All balance fields must use the same currency
26/// - Forward balances must have value dates in the future
27/// - Available balances reflect actual spendable funds
28#[serde_swift_fields]
29#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftMessage)]
30#[validation_rules(MT941_VALIDATION_RULES)]
31pub struct MT941 {
32    /// **Transaction Reference Number** - Field 20
33    ///
34    /// Unique reference for this balance report.
35    /// Used for tracking and referencing this specific report.
36    #[field("20", mandatory)]
37    pub field_20: GenericReferenceField,
38
39    /// **Related Reference** - Field 21 (Optional)
40    ///
41    /// Links to MT920 request if applicable.
42    /// Provides connection to balance request that triggered this report.
43    #[field("21", optional)]
44    pub field_21: Option<GenericReferenceField>,
45
46    /// **Account Identification** - Field 25
47    ///
48    /// IBAN or account identifier.
49    /// Identifies the account for which balances are reported.
50    #[field("25", mandatory)]
51    pub field_25: GenericTextField,
52
53    /// **Statement Number** - Field 28D
54    ///
55    /// Statement sequence number for tracking.
56    /// Enables proper sequencing of balance reports.
57    #[field("28D", mandatory)]
58    pub field_28d: Field28D,
59
60    /// **Opening Balance** - Field 60F
61    ///
62    /// Booked opening balance for the reporting period.
63    /// Reference point for balance changes during the period.
64    #[field("60F", mandatory)]
65    pub field_60f: GenericBalanceField,
66
67    /// **Balance Lines** (Repetitive)
68    ///
69    /// Forward balances at different value dates.
70    /// Each line represents balance projection for specific dates.
71    #[field("BALANCE_LINES", repetitive)]
72    pub balance_lines: Vec<MT941BalanceLine>,
73
74    /// **Closing Balance** - Field 62F
75    ///
76    /// Booked closing balance at end of reporting period.
77    /// Final balance after all transactions for the period.
78    #[field("62F", mandatory)]
79    pub field_62f: GenericBalanceField,
80
81    /// **Closing Available Balance** - Field 64 (Optional)
82    ///
83    /// Available funds at close of business.
84    /// Shows actual spendable balance after reserves and holds.
85    #[field("64", optional)]
86    pub field_64: Option<GenericBalanceField>,
87}
88
89/// # MT941 Balance Line
90///
91/// Represents a forward balance at a specific value date.
92/// Enhanced with SwiftMessage derive for automatic parsing and validation.
93#[serde_swift_fields]
94#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, SwiftMessage)]
95#[validation_rules(MT941_BALANCE_LINE_VALIDATION_RULES)]
96pub struct MT941BalanceLine {
97    /// **Forward Available Balance** - Field 65
98    ///
99    /// Available balance at specific future value date.
100    /// Shows projected available funds considering pending transactions.
101    #[field("65", mandatory)]
102    pub field_65: GenericBalanceField,
103}
104
105/// Enhanced validation rules for MT941
106const MT941_VALIDATION_RULES: &str = r#"{
107  "rules": [
108    {
109      "id": "CURRENCY_CONSISTENCY",
110      "description": "All balance fields must use the same currency",
111      "condition": {
112        "and": [
113          {"==": [
114            {"var": "field_60f.currency"},
115            {"var": "field_62f.currency"}
116          ]}
117        ]
118      }
119    },
120    {
121      "id": "REF_FORMAT",
122      "description": "Transaction reference must not have invalid slash patterns",
123      "condition": {
124        "and": [
125          {"!": {"startsWith": [{"var": "field_20.value"}, "/"]}},
126          {"!": {"endsWith": [{"var": "field_20.value"}, "/"]}},
127          {"!": {"includes": [{"var": "field_20.value"}, "//"]}}
128        ]
129      }
130    },
131    {
132      "id": "REQUIRED_FIELDS",
133      "description": "All mandatory fields must be present and non-empty",
134      "condition": {
135        "and": [
136          {"!=": [{"var": "field_20.value"}, ""]},
137          {"!=": [{"var": "field_25.value"}, ""]},
138          {"var": "field_28d.is_valid"},
139          {"var": "field_60f.is_valid"},
140          {"var": "field_62f.is_valid"}
141        ]
142      }
143    }
144  ]
145}"#;
146
147/// Validation rules specific to MT941 balance lines
148const MT941_BALANCE_LINE_VALIDATION_RULES: &str = r#"{
149  "rules": [
150    {
151      "id": "BALANCE_LINE_VALID",
152      "description": "Balance line must be valid",
153      "condition": {
154        "var": "field_65.is_valid"
155      }
156    }
157  ]
158}"#;