swift-mt-message 3.1.5

A fast, type-safe Rust implementation of SWIFT MT message parsing with comprehensive field support, derive macros, and validation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
use crate::errors::SwiftValidationError;
use crate::fields::*;
use crate::parser::utils::*;
use serde::{Deserialize, Serialize};

/// Sequence B - Transaction details
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct MT107Transaction {
    /// Transaction reference (Field 21)
    #[serde(rename = "21")]
    pub field_21: Field21NoOption,

    /// Instruction code (Field 23E)
    #[serde(rename = "23E", skip_serializing_if = "Option::is_none")]
    pub field_23e: Option<Field23E>,

    /// Mandate reference (Field 21C)
    #[serde(rename = "21C", skip_serializing_if = "Option::is_none")]
    pub field_21c: Option<Field21C>,

    /// Direct debit reference (Field 21D)
    #[serde(rename = "21D", skip_serializing_if = "Option::is_none")]
    pub field_21d: Option<Field21D>,

    /// Registration reference (Field 21E)
    #[serde(rename = "21E", skip_serializing_if = "Option::is_none")]
    pub field_21e: Option<Field21E>,

    /// Transaction amount (Field 32B)
    #[serde(rename = "32B")]
    pub field_32b: Field32B,

    /// Instructing party (Field 50C/L)
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub instructing_party_tx: Option<Field50InstructingParty>,

    /// Creditor (Field 50A/K)
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub creditor_tx: Option<Field50Creditor>,

    /// Creditor's bank (Field 52)
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub field_52: Option<Field52CreditorBank>,

    /// Debtor's bank (Field 57)
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub field_57: Option<Field57DebtorBank>,

    /// Debtor (Field 59)
    #[serde(flatten)]
    pub field_59: Field59,

    /// Remittance information (Field 70)
    #[serde(rename = "70", skip_serializing_if = "Option::is_none")]
    pub field_70: Option<Field70>,

    /// Transaction type code (Field 26T)
    #[serde(rename = "26T", skip_serializing_if = "Option::is_none")]
    pub field_26t: Option<Field26T>,

    /// Regulatory reporting (Field 77B)
    #[serde(rename = "77B", skip_serializing_if = "Option::is_none")]
    pub field_77b: Option<Field77B>,

    /// Original ordered amount (Field 33B)
    #[serde(rename = "33B", skip_serializing_if = "Option::is_none")]
    pub field_33b: Option<Field33B>,

    /// Details of charges (Field 71A)
    #[serde(rename = "71A", skip_serializing_if = "Option::is_none")]
    pub field_71a: Option<Field71A>,

    /// Sender's charges (Field 71F)
    #[serde(rename = "71F", skip_serializing_if = "Option::is_none")]
    pub field_71f: Option<Field71F>,

    /// Receiver's charges (Field 71G)
    #[serde(rename = "71G", skip_serializing_if = "Option::is_none")]
    pub field_71g: Option<Field71G>,

    /// Exchange rate (Field 36)
    #[serde(rename = "36", skip_serializing_if = "Option::is_none")]
    pub field_36: Option<Field36>,
}

/// **MT107: General Direct Debit Message**
///
/// General direct debit instruction with flexible structure and settlement details.
/// Similar to MT104 but with additional flexibility for complex scenarios.
///
/// **Usage:** General direct debits, flexible collections
/// **Category:** Category 1 (Customer Payments)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct MT107 {
    /// Sender's reference (Field 20)
    #[serde(rename = "20")]
    pub field_20: Field20,

    /// Instruction code (Field 23E)
    #[serde(rename = "23E", skip_serializing_if = "Option::is_none")]
    pub field_23e: Option<Field23E>,

    /// Registration reference (Field 21E)
    #[serde(rename = "21E", skip_serializing_if = "Option::is_none")]
    pub field_21e: Option<Field21E>,

    /// Requested execution date (Field 30)
    #[serde(rename = "30")]
    pub field_30: Field30,

    /// Sending institution (Field 51A)
    #[serde(rename = "51A", skip_serializing_if = "Option::is_none")]
    pub field_51a: Option<Field51A>,

    /// Instructing party (Field 50C/L)
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub instructing_party: Option<Field50InstructingParty>,

    /// Creditor (Field 50A/K)
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub creditor: Option<Field50Creditor>,

    /// Creditor's bank (Field 52)
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub field_52: Option<Field52CreditorBank>,

    /// Transaction type code (Field 26T)
    #[serde(rename = "26T", skip_serializing_if = "Option::is_none")]
    pub field_26t: Option<Field26T>,

    /// Regulatory reporting (Field 77B)
    #[serde(rename = "77B", skip_serializing_if = "Option::is_none")]
    pub field_77b: Option<Field77B>,

    /// Details of charges (Field 71A)
    #[serde(rename = "71A", skip_serializing_if = "Option::is_none")]
    pub field_71a: Option<Field71A>,

    /// Sender to receiver information (Field 72)
    #[serde(rename = "72", skip_serializing_if = "Option::is_none")]
    pub field_72: Option<Field72>,

    /// Transaction details (Sequence B)
    #[serde(rename = "#")]
    pub transactions: Vec<MT107Transaction>,

    /// Settlement amount (Field 32B, Sequence C)
    #[serde(rename = "32B")]
    pub field_32b: Field32B,

    /// Sum of amounts (Field 19)
    #[serde(rename = "19", skip_serializing_if = "Option::is_none")]
    pub field_19: Option<Field19>,

    /// Sum of sender's charges (Field 71F)
    #[serde(rename = "71F", skip_serializing_if = "Option::is_none")]
    pub field_71f: Option<Field71F>,

    /// Sum of receiver's charges (Field 71G)
    #[serde(rename = "71G", skip_serializing_if = "Option::is_none")]
    pub field_71g: Option<Field71G>,

    /// Sender's correspondent (Field 53)
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub field_53: Option<Field53SenderCorrespondent>,
}

impl MT107 {
    /// Parse message from Block 4 content
    pub fn parse_from_block4(block4: &str) -> Result<Self, crate::errors::ParseError> {
        let mut parser = crate::parser::MessageParser::new(block4, "107");

        // Parse Sequence A - General Information
        let field_20 = parser.parse_field::<Field20>("20")?;
        let field_23e = parser.parse_optional_field::<Field23E>("23E")?;
        let field_21e = parser.parse_optional_field::<Field21E>("21E")?;
        let field_30 = parser.parse_field::<Field30>("30")?;
        let field_51a = parser.parse_optional_field::<Field51A>("51A")?;

        // Try to parse field 50 - could be instructing party (C/L) or creditor (A/K)
        // We'll need to detect the variant and determine which type
        let (instructing_party, creditor) = Self::parse_field_50(&mut parser)?;

        let field_52 = parser.parse_optional_variant_field::<Field52CreditorBank>("52")?;
        let field_26t = parser.parse_optional_field::<Field26T>("26T")?;
        let field_77b = parser.parse_optional_field::<Field77B>("77B")?;
        let field_71a = parser.parse_optional_field::<Field71A>("71A")?;
        let field_72 = parser.parse_optional_field::<Field72>("72")?;

        // Parse Sequence B - Transaction Details (repeating)
        let mut transactions = Vec::new();
        parser = parser.with_duplicates(true);

        while parser.detect_field("21") {
            let txn_field_21 = parser.parse_field::<Field21NoOption>("21")?;
            let txn_field_23e = parser.parse_optional_field::<Field23E>("23E")?;
            let txn_field_21c = parser.parse_optional_field::<Field21C>("21C")?;
            let txn_field_21d = parser.parse_optional_field::<Field21D>("21D")?;
            let txn_field_21e = parser.parse_optional_field::<Field21E>("21E")?;
            let txn_field_32b = parser.parse_field::<Field32B>("32B")?;

            let (instructing_party_tx, creditor_tx) = Self::parse_field_50(&mut parser)?;

            let txn_field_52 = parser.parse_optional_variant_field::<Field52CreditorBank>("52")?;
            let txn_field_57 = parser.parse_optional_variant_field::<Field57DebtorBank>("57")?;
            let txn_field_59 = parser.parse_variant_field::<Field59>("59")?;
            let txn_field_70 = parser.parse_optional_field::<Field70>("70")?;
            let txn_field_26t = parser.parse_optional_field::<Field26T>("26T")?;
            let txn_field_77b = parser.parse_optional_field::<Field77B>("77B")?;
            let txn_field_33b = parser.parse_optional_field::<Field33B>("33B")?;
            let txn_field_71a = parser.parse_optional_field::<Field71A>("71A")?;
            let txn_field_71f = parser.parse_optional_field::<Field71F>("71F")?;
            let txn_field_71g = parser.parse_optional_field::<Field71G>("71G")?;
            let txn_field_36 = parser.parse_optional_field::<Field36>("36")?;

            transactions.push(MT107Transaction {
                field_21: txn_field_21,
                field_23e: txn_field_23e,
                field_21c: txn_field_21c,
                field_21d: txn_field_21d,
                field_21e: txn_field_21e,
                field_32b: txn_field_32b,
                instructing_party_tx,
                creditor_tx,
                field_52: txn_field_52,
                field_57: txn_field_57,
                field_59: txn_field_59,
                field_70: txn_field_70,
                field_26t: txn_field_26t,
                field_77b: txn_field_77b,
                field_33b: txn_field_33b,
                field_71a: txn_field_71a,
                field_71f: txn_field_71f,
                field_71g: txn_field_71g,
                field_36: txn_field_36,
            });
        }

        // Parse Sequence C - Settlement Details
        // Note: duplicates remain enabled to allow parsing field 32B again
        let settlement_field_32b = parser.parse_field::<Field32B>("32B")?;
        let settlement_field_19 = parser.parse_optional_field::<Field19>("19")?;
        let settlement_field_71f = parser.parse_optional_field::<Field71F>("71F")?;
        let settlement_field_71g = parser.parse_optional_field::<Field71G>("71G")?;
        let settlement_field_53 =
            parser.parse_optional_variant_field::<Field53SenderCorrespondent>("53")?;

        // Verify all content is consumed
        if !parser.is_complete() {
            return Err(crate::errors::ParseError::InvalidFormat {
                message: format!(
                    "Unparsed content remaining in message: {}",
                    parser.remaining()
                ),
            });
        }

        Ok(Self {
            field_20,
            field_23e,
            field_21e,
            field_30,
            field_51a,
            instructing_party,
            creditor,
            field_52,
            field_26t,
            field_77b,
            field_71a,
            field_72,
            transactions,
            field_32b: settlement_field_32b,
            field_19: settlement_field_19,
            field_71f: settlement_field_71f,
            field_71g: settlement_field_71g,
            field_53: settlement_field_53,
        })
    }

    /// Helper to parse field 50 which can be either instructing party (C/L) or creditor (A/K)
    fn parse_field_50(
        parser: &mut crate::parser::MessageParser,
    ) -> Result<(Option<Field50InstructingParty>, Option<Field50Creditor>), crate::errors::ParseError>
    {
        // Detect which variant of field 50 is present
        let remaining = parser.remaining();
        let trimmed = remaining.trim_start_matches(|c: char| c.is_whitespace());

        // Check for instructing party variants (C, L)
        if trimmed.starts_with(":50C:") {
            let instructing_party =
                parser.parse_optional_variant_field::<Field50InstructingParty>("50")?;
            return Ok((instructing_party, None));
        }
        if trimmed.starts_with(":50L:") {
            let instructing_party =
                parser.parse_optional_variant_field::<Field50InstructingParty>("50")?;
            return Ok((instructing_party, None));
        }

        // Check for creditor variants (A, K)
        if trimmed.starts_with(":50A:") || trimmed.starts_with(":50K:") {
            let creditor = parser.parse_optional_variant_field::<Field50Creditor>("50")?;
            return Ok((None, creditor));
        }

        // No field 50 present
        Ok((None, None))
    }

    // ========================================================================
    // NETWORK VALIDATION RULES (SR 2025 MT107)
    // ========================================================================

    /// Field 23E valid instruction codes for MT107
    const MT107_VALID_23E_CODES: &'static [&'static str] = &[
        "AUTH", // Pre-authorised direct debit
        "NAUT", // Non pre-authorised
        "OTHR", // Other
        "RTND", // Returned
    ];

    // ========================================================================
    // HELPER METHODS
    // ========================================================================

    /// Check if field 23E is present in Sequence A
    fn has_23e_in_seq_a(&self) -> bool {
        self.field_23e.is_some()
    }

    /// Check if field 23E is present in all Sequence B transactions
    fn has_23e_in_all_seq_b(&self) -> bool {
        !self.transactions.is_empty() && self.transactions.iter().all(|tx| tx.field_23e.is_some())
    }

    /// Check if field 23E is present in any Sequence B transaction
    fn has_23e_in_any_seq_b(&self) -> bool {
        self.transactions.iter().any(|tx| tx.field_23e.is_some())
    }

    /// Check if creditor (A/K) is present in Sequence A
    fn has_creditor_in_seq_a(&self) -> bool {
        self.creditor.is_some()
    }

    /// Check if creditor (A/K) is present in all Sequence B transactions
    fn has_creditor_in_all_seq_b(&self) -> bool {
        !self.transactions.is_empty() && self.transactions.iter().all(|tx| tx.creditor_tx.is_some())
    }

    /// Check if creditor (A/K) is present in any Sequence B transaction
    fn has_creditor_in_any_seq_b(&self) -> bool {
        self.transactions.iter().any(|tx| tx.creditor_tx.is_some())
    }

    /// Check if instructing party (C/L) is present in Sequence A
    fn has_instructing_party_in_seq_a(&self) -> bool {
        self.instructing_party.is_some()
    }

    /// Check if instructing party (C/L) is present in any Sequence B transaction
    fn has_instructing_party_in_any_seq_b(&self) -> bool {
        self.transactions
            .iter()
            .any(|tx| tx.instructing_party_tx.is_some())
    }

    /// Check if field 21E is present in Sequence A
    fn has_21e_in_seq_a(&self) -> bool {
        self.field_21e.is_some()
    }

    /// Check if field 21E is present in any Sequence B transaction
    fn has_21e_in_any_seq_b(&self) -> bool {
        self.transactions.iter().any(|tx| tx.field_21e.is_some())
    }

    /// Check if field 26T is present in Sequence A
    fn has_26t_in_seq_a(&self) -> bool {
        self.field_26t.is_some()
    }

    /// Check if field 26T is present in any Sequence B transaction
    fn has_26t_in_any_seq_b(&self) -> bool {
        self.transactions.iter().any(|tx| tx.field_26t.is_some())
    }

    /// Check if field 77B is present in Sequence A
    fn has_77b_in_seq_a(&self) -> bool {
        self.field_77b.is_some()
    }

    /// Check if field 77B is present in any Sequence B transaction
    fn has_77b_in_any_seq_b(&self) -> bool {
        self.transactions.iter().any(|tx| tx.field_77b.is_some())
    }

    /// Check if field 71A is present in Sequence A
    fn has_71a_in_seq_a(&self) -> bool {
        self.field_71a.is_some()
    }

    /// Check if field 71A is present in any Sequence B transaction
    fn has_71a_in_any_seq_b(&self) -> bool {
        self.transactions.iter().any(|tx| tx.field_71a.is_some())
    }

    /// Check if field 52a (creditor's bank) is present in Sequence A
    fn has_52a_in_seq_a(&self) -> bool {
        self.field_52.is_some()
    }

    /// Check if field 52a (creditor's bank) is present in any Sequence B transaction
    fn has_52a_in_any_seq_b(&self) -> bool {
        self.transactions.iter().any(|tx| tx.field_52.is_some())
    }

    /// Check if field 71F is present in any Sequence B transaction
    fn has_71f_in_seq_b(&self) -> bool {
        self.transactions.iter().any(|tx| tx.field_71f.is_some())
    }

    /// Check if field 71F is present in Sequence C
    fn has_71f_in_seq_c(&self) -> bool {
        self.field_71f.is_some()
    }

    /// Check if field 71G is present in any Sequence B transaction
    fn has_71g_in_seq_b(&self) -> bool {
        self.transactions.iter().any(|tx| tx.field_71g.is_some())
    }

    /// Check if field 71G is present in Sequence C
    fn has_71g_in_seq_c(&self) -> bool {
        self.field_71g.is_some()
    }

    // ========================================================================
    // VALIDATION RULES (C1-C9)
    // ========================================================================

    /// C1: Field 23E and Field 50a (option A or K) Mutual Exclusivity (Error code: D86)
    /// Field 23E and field 50a (option A or K) must be present either in sequence A
    /// or in each occurrence of sequence B, but not in both
    fn validate_c1_23e_and_creditor_placement(&self) -> Vec<SwiftValidationError> {
        let mut errors = Vec::new();

        // Validate Field 23E placement
        let has_23e_a = self.has_23e_in_seq_a();
        let has_23e_all_b = self.has_23e_in_all_seq_b();
        let has_23e_any_b = self.has_23e_in_any_seq_b();

        if has_23e_a && has_23e_any_b {
            errors.push(SwiftValidationError::content_error(
                "D86",
                "23E",
                "",
                "Field 23E must not be present in both Sequence A and Sequence B",
                "Field 23E must be present either in Sequence A or in each occurrence of Sequence B, but not in both",
            ));
        } else if !has_23e_a && !has_23e_all_b {
            if has_23e_any_b {
                errors.push(SwiftValidationError::content_error(
                    "D86",
                    "23E",
                    "",
                    "Field 23E must be present in every Sequence B transaction when not in Sequence A",
                    "When field 23E is not in Sequence A, it must be present in each occurrence of Sequence B",
                ));
            } else {
                errors.push(SwiftValidationError::content_error(
                    "D86",
                    "23E",
                    "",
                    "Field 23E must be present in either Sequence A or in every Sequence B transaction",
                    "Field 23E must be present either in Sequence A or in each occurrence of Sequence B",
                ));
            }
        }

        // Validate Field 50a (A/K - Creditor) placement
        let has_creditor_a = self.has_creditor_in_seq_a();
        let has_creditor_all_b = self.has_creditor_in_all_seq_b();
        let has_creditor_any_b = self.has_creditor_in_any_seq_b();

        if has_creditor_a && has_creditor_any_b {
            errors.push(SwiftValidationError::content_error(
                "D86",
                "50a",
                "",
                "Field 50a (Creditor A/K) must not be present in both Sequence A and Sequence B",
                "Field 50a (option A or K) must be present either in Sequence A or in each occurrence of Sequence B, but not in both",
            ));
        } else if !has_creditor_a && !has_creditor_all_b {
            if has_creditor_any_b {
                errors.push(SwiftValidationError::content_error(
                    "D86",
                    "50a",
                    "",
                    "Field 50a (Creditor A/K) must be present in every Sequence B transaction when not in Sequence A",
                    "When field 50a (option A or K) is not in Sequence A, it must be present in each occurrence of Sequence B",
                ));
            } else {
                errors.push(SwiftValidationError::content_error(
                    "D86",
                    "50a",
                    "",
                    "Field 50a (Creditor A/K) must be present in either Sequence A or in every Sequence B transaction",
                    "Field 50a (option A or K) must be present either in Sequence A or in each occurrence of Sequence B",
                ));
            }
        }

        errors
    }

    /// C2: Fields in Sequence A vs Sequence B Mutual Exclusivity (Error code: D73)
    /// When present in sequence A, fields 21E, 26T, 77B, 71A, 52a and 50a (option C or L)
    /// must not be present in any occurrence of sequence B, and vice versa
    fn validate_c2_seq_a_b_mutual_exclusivity(&self) -> Vec<SwiftValidationError> {
        let mut errors = Vec::new();

        // Field 21E
        if self.has_21e_in_seq_a() && self.has_21e_in_any_seq_b() {
            errors.push(SwiftValidationError::content_error(
                "D73",
                "21E",
                "",
                "Field 21E must not be present in both Sequence A and Sequence B",
                "When present in Sequence A, field 21E must not be present in any occurrence of Sequence B",
            ));
        }

        // Field 26T
        if self.has_26t_in_seq_a() && self.has_26t_in_any_seq_b() {
            errors.push(SwiftValidationError::content_error(
                "D73",
                "26T",
                "",
                "Field 26T must not be present in both Sequence A and Sequence B",
                "When present in Sequence A, field 26T must not be present in any occurrence of Sequence B",
            ));
        }

        // Field 77B
        if self.has_77b_in_seq_a() && self.has_77b_in_any_seq_b() {
            errors.push(SwiftValidationError::content_error(
                "D73",
                "77B",
                "",
                "Field 77B must not be present in both Sequence A and Sequence B",
                "When present in Sequence A, field 77B must not be present in any occurrence of Sequence B",
            ));
        }

        // Field 71A
        if self.has_71a_in_seq_a() && self.has_71a_in_any_seq_b() {
            errors.push(SwiftValidationError::content_error(
                "D73",
                "71A",
                "",
                "Field 71A must not be present in both Sequence A and Sequence B",
                "When present in Sequence A, field 71A must not be present in any occurrence of Sequence B",
            ));
        }

        // Field 52a
        if self.has_52a_in_seq_a() && self.has_52a_in_any_seq_b() {
            errors.push(SwiftValidationError::content_error(
                "D73",
                "52a",
                "",
                "Field 52a must not be present in both Sequence A and Sequence B",
                "When present in Sequence A, field 52a must not be present in any occurrence of Sequence B",
            ));
        }

        // Field 50a (C/L - Instructing Party)
        if self.has_instructing_party_in_seq_a() && self.has_instructing_party_in_any_seq_b() {
            errors.push(SwiftValidationError::content_error(
                "D73",
                "50a",
                "",
                "Field 50a (Instructing Party C/L) must not be present in both Sequence A and Sequence B",
                "When present in Sequence A, field 50a (option C or L) must not be present in any occurrence of Sequence B",
            ));
        }

        errors
    }

    /// C3: Registration Reference and Creditor Dependency (Error code: D77)
    /// If field 21E is present, then field 50a (option A or K) must also be present
    /// in the same sequence
    fn validate_c3_registration_creditor_dependency(&self) -> Vec<SwiftValidationError> {
        let mut errors = Vec::new();

        // Check Sequence A
        if self.field_21e.is_some() && self.creditor.is_none() {
            errors.push(SwiftValidationError::content_error(
                "D77",
                "50a",
                "",
                "Sequence A: Field 50a (Creditor A/K) is mandatory when field 21E is present",
                "If field 21E is present in Sequence A, then field 50a (option A or K) must also be present in Sequence A",
            ));
        }

        // Check each transaction in Sequence B
        for (idx, transaction) in self.transactions.iter().enumerate() {
            if transaction.field_21e.is_some() && transaction.creditor_tx.is_none() {
                errors.push(SwiftValidationError::content_error(
                    "D77",
                    "50a",
                    "",
                    &format!(
                        "Transaction {}: Field 50a (Creditor A/K) is mandatory when field 21E is present",
                        idx + 1
                    ),
                    "If field 21E is present in Sequence B, then field 50a (option A or K) must also be present in the same occurrence",
                ));
            }
        }

        errors
    }

    /// C4: Field 23E RTND and Field 72 Dependency (Error code: C82)
    /// In sequence A, if field 23E contains RTND then field 72 must be present,
    /// otherwise field 72 is not allowed
    fn validate_c4_rtnd_field_72_dependency(&self) -> Option<SwiftValidationError> {
        if let Some(ref field_23e) = self.field_23e {
            let is_rtnd = field_23e.instruction_code == "RTND";

            if is_rtnd && self.field_72.is_none() {
                return Some(SwiftValidationError::content_error(
                    "C82",
                    "72",
                    "",
                    "Field 72 is mandatory when field 23E contains code RTND",
                    "In Sequence A, if field 23E is present and contains RTND then field 72 must be present",
                ));
            }

            if !is_rtnd && self.field_72.is_some() {
                return Some(SwiftValidationError::content_error(
                    "C82",
                    "72",
                    "",
                    "Field 72 is not allowed when field 23E does not contain code RTND",
                    "Field 72 is only allowed when field 23E contains code RTND",
                ));
            }
        } else {
            // Field 23E not present in Sequence A
            if self.field_72.is_some() {
                return Some(SwiftValidationError::content_error(
                    "C82",
                    "72",
                    "",
                    "Field 72 is not allowed when field 23E is not present in Sequence A",
                    "Field 72 is only allowed when field 23E is present in Sequence A with code RTND",
                ));
            }
        }

        None
    }

    /// C5: Charges Fields in Sequence B and Sequence C (Error code: D79)
    /// If fields 71F and 71G are present in Sequence B, they must also be present in Sequence C,
    /// and vice versa
    fn validate_c5_charges_fields_consistency(&self) -> Vec<SwiftValidationError> {
        let mut errors = Vec::new();

        // Field 71F
        let has_71f_b = self.has_71f_in_seq_b();
        let has_71f_c = self.has_71f_in_seq_c();

        if has_71f_b && !has_71f_c {
            errors.push(SwiftValidationError::content_error(
                "D79",
                "71F",
                "",
                "Field 71F is mandatory in Sequence C when present in Sequence B",
                "If field 71F is present in one or more occurrence of Sequence B, then it must also be present in Sequence C",
            ));
        }

        if has_71f_c && !has_71f_b {
            errors.push(SwiftValidationError::content_error(
                "D79",
                "71F",
                "",
                "Field 71F is not allowed in Sequence C when not present in Sequence B",
                "If field 71F is present in Sequence C, it must also be present in at least one occurrence of Sequence B",
            ));
        }

        // Field 71G
        let has_71g_b = self.has_71g_in_seq_b();
        let has_71g_c = self.has_71g_in_seq_c();

        if has_71g_b && !has_71g_c {
            errors.push(SwiftValidationError::content_error(
                "D79",
                "71G",
                "",
                "Field 71G is mandatory in Sequence C when present in Sequence B",
                "If field 71G is present in one or more occurrence of Sequence B, then it must also be present in Sequence C",
            ));
        }

        if has_71g_c && !has_71g_b {
            errors.push(SwiftValidationError::content_error(
                "D79",
                "71G",
                "",
                "Field 71G is not allowed in Sequence C when not present in Sequence B",
                "If field 71G is present in Sequence C, it must also be present in at least one occurrence of Sequence B",
            ));
        }

        errors
    }

    /// C6: Field 33B and 32B Comparison (Error code: D21)
    /// If field 33B is present, the currency code or amount, or both, must be different
    /// between fields 33B and 32B
    fn validate_c6_field_33b_32b_comparison(&self) -> Vec<SwiftValidationError> {
        let mut errors = Vec::new();

        for (idx, transaction) in self.transactions.iter().enumerate() {
            if let Some(ref field_33b) = transaction.field_33b {
                let currency_32b = &transaction.field_32b.currency;
                let amount_32b = transaction.field_32b.amount;
                let currency_33b = &field_33b.currency;
                let amount_33b = field_33b.amount;

                // Both currency and amount must not be the same
                if currency_32b == currency_33b && (amount_32b - amount_33b).abs() < 0.01 {
                    errors.push(SwiftValidationError::content_error(
                        "D21",
                        "33B",
                        &format!("{}{}", currency_33b, amount_33b),
                        &format!(
                            "Transaction {}: Field 33B must have different currency code or amount from field 32B. Both are {}{:.2}",
                            idx + 1, currency_32b, amount_32b
                        ),
                        "If field 33B is present, the currency code or the amount, or both, must be different between fields 33B and 32B",
                    ));
                }
            }
        }

        errors
    }

    /// C7: Field 33B, 32B and Exchange Rate (Error code: D75)
    /// If field 33B is present and currency codes are different, field 36 must be present.
    /// Otherwise, field 36 must not be present
    fn validate_c7_exchange_rate_dependency(&self) -> Vec<SwiftValidationError> {
        let mut errors = Vec::new();

        for (idx, transaction) in self.transactions.iter().enumerate() {
            if let Some(ref field_33b) = transaction.field_33b {
                let currency_32b = &transaction.field_32b.currency;
                let currency_33b = &field_33b.currency;

                if currency_32b != currency_33b {
                    // Different currencies - field 36 is mandatory
                    if transaction.field_36.is_none() {
                        errors.push(SwiftValidationError::content_error(
                            "D75",
                            "36",
                            "",
                            &format!(
                                "Transaction {}: Field 36 (Exchange Rate) is mandatory when field 33B has different currency ({}) from field 32B ({})",
                                idx + 1, currency_33b, currency_32b
                            ),
                            "If field 33B is present and currency codes in fields 32B and 33B are different, field 36 must be present",
                        ));
                    }
                } else {
                    // Same currencies - field 36 must not be present
                    if transaction.field_36.is_some() {
                        errors.push(SwiftValidationError::content_error(
                            "D75",
                            "36",
                            "",
                            &format!(
                                "Transaction {}: Field 36 (Exchange Rate) is not allowed when field 33B has same currency as field 32B ({})",
                                idx + 1, currency_32b
                            ),
                            "If field 33B is present and currency codes in fields 32B and 33B are the same, field 36 must not be present",
                        ));
                    }
                }
            } else {
                // Field 33B not present - field 36 must not be present
                if transaction.field_36.is_some() {
                    errors.push(SwiftValidationError::content_error(
                        "D75",
                        "36",
                        "",
                        &format!(
                            "Transaction {}: Field 36 (Exchange Rate) is not allowed when field 33B is not present",
                            idx + 1
                        ),
                        "Field 36 is only allowed when field 33B is present",
                    ));
                }
            }
        }

        errors
    }

    /// C8: Sum of Amounts and Settlement Amount (Error code: D80, C01)
    /// The sum of amounts in field 32B of Sequence B must be in field 32B of Sequence C
    /// (when no charges) or in field 19 of Sequence C
    fn validate_c8_sum_of_amounts(&self) -> Vec<SwiftValidationError> {
        let mut errors = Vec::new();

        if self.transactions.is_empty() {
            return errors;
        }

        // Calculate sum of all transaction amounts
        let sum_of_amounts: f64 = self.transactions.iter().map(|tx| tx.field_32b.amount).sum();

        // Check if charges are present
        let has_charges = self.has_71f_in_seq_b() || self.has_71g_in_seq_b();

        if has_charges {
            // Field 19 should be present and equal to sum
            if let Some(ref field_19) = self.field_19 {
                let field_19_amount = field_19.amount;
                if (field_19_amount - sum_of_amounts).abs() >= 0.01 {
                    errors.push(SwiftValidationError::content_error(
                        "C01",
                        "19",
                        &field_19_amount.to_string(),
                        &format!(
                            "Field 19 ({:.2}) must equal the sum of amounts in field 32B of Sequence B ({:.2})",
                            field_19_amount, sum_of_amounts
                        ),
                        "Field 19 must equal the sum of the amounts in all occurrences of field 32B in Sequence B",
                    ));
                }
            } else {
                errors.push(SwiftValidationError::content_error(
                    "D80",
                    "19",
                    "",
                    "Field 19 is mandatory when charges are present in Sequence B",
                    "When charges are included (field 71F or 71G in Sequence B), the sum of amounts must be in field 19 of Sequence C",
                ));
            }
        } else {
            // No charges - field 32B of Sequence C should equal sum, field 19 must not be present
            let settlement_amount = self.field_32b.amount;
            if (settlement_amount - sum_of_amounts).abs() >= 0.01 {
                errors.push(SwiftValidationError::content_error(
                    "D80",
                    "32B",
                    &settlement_amount.to_string(),
                    &format!(
                        "Sequence C field 32B amount ({:.2}) must equal the sum of amounts in Sequence B field 32B ({:.2}) when no charges are included",
                        settlement_amount, sum_of_amounts
                    ),
                    "When no charges are included, the sum of amounts in field 32B of Sequence B must be in field 32B of Sequence C",
                ));
            }

            if self.field_19.is_some() {
                errors.push(SwiftValidationError::content_error(
                    "D80",
                    "19",
                    "",
                    "Field 19 must not be present when no charges are included in Sequence B",
                    "Field 19 must not be present when the sum goes directly to field 32B of Sequence C",
                ));
            }
        }

        errors
    }

    /// C9: Currency Consistency Across Message (Error code: C02)
    /// Currency codes in fields 32B and 71G must be the same across all sequences.
    /// Currency codes in field 71F must be the same across all sequences.
    fn validate_c9_currency_consistency(&self) -> Vec<SwiftValidationError> {
        let mut errors = Vec::new();

        if self.transactions.is_empty() {
            return errors;
        }

        // Get reference currencies from Sequence C
        let settlement_currency = &self.field_32b.currency;
        let ref_71f_currency = self.field_71f.as_ref().map(|f| &f.currency);
        let ref_71g_currency = self.field_71g.as_ref().map(|f| &f.currency);

        // Check 32B currency consistency in Sequence B
        for (idx, transaction) in self.transactions.iter().enumerate() {
            if &transaction.field_32b.currency != settlement_currency {
                errors.push(SwiftValidationError::content_error(
                    "C02",
                    "32B",
                    &transaction.field_32b.currency,
                    &format!(
                        "Transaction {}: Currency code in field 32B ({}) must be the same as in Sequence C ({})",
                        idx + 1, transaction.field_32b.currency, settlement_currency
                    ),
                    "The currency code in field 32B must be the same for all occurrences in Sequences B and C",
                ));
            }

            // Check 71F currency consistency
            if let Some(ref tx_71f) = transaction.field_71f
                && let Some(ref_currency) = ref_71f_currency
                && &tx_71f.currency != ref_currency
            {
                errors.push(SwiftValidationError::content_error(
                        "C02",
                        "71F",
                        &tx_71f.currency,
                        &format!(
                            "Transaction {}: Currency code in field 71F ({}) must be the same as in Sequence C ({})",
                            idx + 1, tx_71f.currency, ref_currency
                        ),
                        "The currency code in field 71F must be the same for all occurrences in Sequences B and C",
                    ));
            }

            // Check 71G currency consistency
            if let Some(ref tx_71g) = transaction.field_71g {
                if &tx_71g.currency != settlement_currency {
                    errors.push(SwiftValidationError::content_error(
                        "C02",
                        "71G",
                        &tx_71g.currency,
                        &format!(
                            "Transaction {}: Currency code in field 71G ({}) must be the same as in Sequence C ({})",
                            idx + 1, tx_71g.currency, settlement_currency
                        ),
                        "The currency code in field 71G must be the same for all occurrences in Sequences B and C",
                    ));
                }

                if let Some(ref_currency) = ref_71g_currency
                    && &tx_71g.currency != ref_currency
                {
                    errors.push(SwiftValidationError::content_error(
                            "C02",
                            "71G",
                            &tx_71g.currency,
                            &format!(
                                "Transaction {}: Currency code in field 71G ({}) must be the same as in Sequence C ({})",
                                idx + 1, tx_71g.currency, ref_currency
                            ),
                            "The currency code in field 71G must be the same for all occurrences in Sequences B and C",
                        ));
                }
            }
        }

        errors
    }

    /// Validate Field 23E instruction codes (Error codes: T47, D81)
    /// Validates instruction code values and additional information restrictions
    fn validate_field_23e(&self) -> Vec<SwiftValidationError> {
        let mut errors = Vec::new();

        // Check Sequence A field 23E
        if let Some(ref field_23e) = self.field_23e {
            let code = &field_23e.instruction_code;

            // T47: Validate instruction code is in allowed list
            if !Self::MT107_VALID_23E_CODES.contains(&code.as_str()) {
                errors.push(SwiftValidationError::format_error(
                    "T47",
                    "23E",
                    code,
                    &format!("One of: {}", Self::MT107_VALID_23E_CODES.join(", ")),
                    &format!(
                        "Sequence A: Instruction code '{}' is not valid for MT107. Valid codes: {}",
                        code,
                        Self::MT107_VALID_23E_CODES.join(", ")
                    ),
                ));
            }

            // D81: Additional information only allowed for code OTHR
            if field_23e.additional_info.is_some() && code != "OTHR" {
                errors.push(SwiftValidationError::content_error(
                    "D81",
                    "23E",
                    code,
                    &format!(
                        "Sequence A: Additional information is only allowed for code OTHR. Code '{}' does not allow additional information",
                        code
                    ),
                    "Additional information in field 23E is only allowed for code OTHR",
                ));
            }
        }

        // Check Sequence B field 23E
        for (idx, transaction) in self.transactions.iter().enumerate() {
            if let Some(ref field_23e) = transaction.field_23e {
                let code = &field_23e.instruction_code;

                // T47: Validate instruction code is in allowed list
                if !Self::MT107_VALID_23E_CODES.contains(&code.as_str()) {
                    errors.push(SwiftValidationError::format_error(
                        "T47",
                        "23E",
                        code,
                        &format!("One of: {}", Self::MT107_VALID_23E_CODES.join(", ")),
                        &format!(
                            "Transaction {}: Instruction code '{}' is not valid for MT107. Valid codes: {}",
                            idx + 1,
                            code,
                            Self::MT107_VALID_23E_CODES.join(", ")
                        ),
                    ));
                }

                // D81: Additional information only allowed for code OTHR
                if field_23e.additional_info.is_some() && code != "OTHR" {
                    errors.push(SwiftValidationError::content_error(
                        "D81",
                        "23E",
                        code,
                        &format!(
                            "Transaction {}: Additional information is only allowed for code OTHR. Code '{}' does not allow additional information",
                            idx + 1, code
                        ),
                        "Additional information in field 23E is only allowed for code OTHR",
                    ));
                }
            }
        }

        errors
    }

    /// Main validation method - validates all network rules
    /// Returns array of validation errors, respects stop_on_first_error flag
    pub fn validate_network_rules(&self, stop_on_first_error: bool) -> Vec<SwiftValidationError> {
        let mut all_errors = Vec::new();

        // C1: Field 23E and Creditor Placement
        let c1_errors = self.validate_c1_23e_and_creditor_placement();
        all_errors.extend(c1_errors);
        if stop_on_first_error && !all_errors.is_empty() {
            return all_errors;
        }

        // C2: Sequence A/B Mutual Exclusivity
        let c2_errors = self.validate_c2_seq_a_b_mutual_exclusivity();
        all_errors.extend(c2_errors);
        if stop_on_first_error && !all_errors.is_empty() {
            return all_errors;
        }

        // C3: Registration Reference and Creditor Dependency
        let c3_errors = self.validate_c3_registration_creditor_dependency();
        all_errors.extend(c3_errors);
        if stop_on_first_error && !all_errors.is_empty() {
            return all_errors;
        }

        // C4: Field 23E RTND and Field 72 Dependency
        if let Some(error) = self.validate_c4_rtnd_field_72_dependency() {
            all_errors.push(error);
            if stop_on_first_error {
                return all_errors;
            }
        }

        // C5: Charges Fields Consistency
        let c5_errors = self.validate_c5_charges_fields_consistency();
        all_errors.extend(c5_errors);
        if stop_on_first_error && !all_errors.is_empty() {
            return all_errors;
        }

        // C6: Field 33B and 32B Comparison
        let c6_errors = self.validate_c6_field_33b_32b_comparison();
        all_errors.extend(c6_errors);
        if stop_on_first_error && !all_errors.is_empty() {
            return all_errors;
        }

        // C7: Exchange Rate Dependency
        let c7_errors = self.validate_c7_exchange_rate_dependency();
        all_errors.extend(c7_errors);
        if stop_on_first_error && !all_errors.is_empty() {
            return all_errors;
        }

        // C8: Sum of Amounts
        let c8_errors = self.validate_c8_sum_of_amounts();
        all_errors.extend(c8_errors);
        if stop_on_first_error && !all_errors.is_empty() {
            return all_errors;
        }

        // C9: Currency Consistency
        let c9_errors = self.validate_c9_currency_consistency();
        all_errors.extend(c9_errors);
        if stop_on_first_error && !all_errors.is_empty() {
            return all_errors;
        }

        // Field 23E Validation
        let f23e_errors = self.validate_field_23e();
        all_errors.extend(f23e_errors);

        all_errors
    }

    /// Parse from SWIFT MT text format
    pub fn parse(input: &str) -> Result<Self, crate::errors::ParseError> {
        // If input starts with block headers, extract Block 4
        let block4 = if input.starts_with("{") {
            crate::parser::SwiftParser::extract_block(input, 4)?.ok_or_else(|| {
                crate::errors::ParseError::InvalidFormat {
                    message: "Block 4 not found".to_string(),
                }
            })?
        } else {
            // Assume input is already block 4 content
            input.to_string()
        };

        Self::parse_from_block4(&block4)
    }
}

impl crate::traits::SwiftMessageBody for MT107 {
    fn message_type() -> &'static str {
        "107"
    }

    fn parse_from_block4(block4: &str) -> Result<Self, crate::errors::ParseError> {
        // Call the existing public method implementation
        MT107::parse_from_block4(block4)
    }

    fn to_mt_string(&self) -> String {
        let mut result = String::new();

        // Sequence A - General Information
        append_field(&mut result, &self.field_20);
        append_optional_field(&mut result, &self.field_23e);
        append_optional_field(&mut result, &self.field_21e);
        append_field(&mut result, &self.field_30);
        append_optional_field(&mut result, &self.field_51a);
        append_optional_field(&mut result, &self.instructing_party);
        append_optional_field(&mut result, &self.creditor);
        append_optional_field(&mut result, &self.field_52);
        append_optional_field(&mut result, &self.field_26t);
        append_optional_field(&mut result, &self.field_77b);
        append_optional_field(&mut result, &self.field_71a);
        append_optional_field(&mut result, &self.field_72);

        // Sequence B - Transaction Details
        for txn in &self.transactions {
            append_field(&mut result, &txn.field_21);
            append_optional_field(&mut result, &txn.field_23e);
            append_optional_field(&mut result, &txn.field_21c);
            append_optional_field(&mut result, &txn.field_21d);
            append_optional_field(&mut result, &txn.field_21e);
            append_field(&mut result, &txn.field_32b);
            append_optional_field(&mut result, &txn.instructing_party_tx);
            append_optional_field(&mut result, &txn.creditor_tx);
            append_optional_field(&mut result, &txn.field_52);
            append_optional_field(&mut result, &txn.field_57);
            append_field(&mut result, &txn.field_59);
            append_optional_field(&mut result, &txn.field_70);
            append_optional_field(&mut result, &txn.field_26t);
            append_optional_field(&mut result, &txn.field_77b);
            append_optional_field(&mut result, &txn.field_33b);
            append_optional_field(&mut result, &txn.field_71a);
            append_optional_field(&mut result, &txn.field_71f);
            append_optional_field(&mut result, &txn.field_71g);
            append_optional_field(&mut result, &txn.field_36);
        }

        // Sequence C - Settlement Details
        append_field(&mut result, &self.field_32b);
        append_optional_field(&mut result, &self.field_19);
        append_optional_field(&mut result, &self.field_71f);
        append_optional_field(&mut result, &self.field_71g);
        append_optional_field(&mut result, &self.field_53);

        finalize_mt_string(result, false)
    }

    fn validate_network_rules(&self, stop_on_first_error: bool) -> Vec<SwiftValidationError> {
        // Call the existing public method implementation
        MT107::validate_network_rules(self, stop_on_first_error)
    }
}