File: test_account_bank_statement.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (1438 lines) | stat: -rw-r--r-- 66,154 bytes parent folder | download
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
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
# -*- coding: utf-8 -*-
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.tests import tagged
from odoo.exceptions import ValidationError, UserError
from odoo import fields, Command

import base64

@tagged('post_install', '-at_install')
class TestAccountBankStatementLine(AccountTestInvoicingCommon):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        cls.currency_1 = cls.company_data['currency']
        # We need a third currency as you could have a company's currency != journal's currency !=
        cls.currency_2 = cls.setup_other_currency('EUR')
        cls.currency_3 = cls.setup_other_currency('CAD', rates=[('2016-01-01', 6.0), ('2017-01-01', 4.0)])
        cls.currency_4 = cls.setup_other_currency('GBP', rates=[('2016-01-01', 12.0), ('2017-01-01', 8.0)])

        cls.company_data_2 = cls.setup_other_company()

        cls.bank_journal_1 = cls.company_data['default_journal_bank']
        cls.bank_journal_2 = cls.bank_journal_1.copy()
        cls.bank_journal_3 = cls.bank_journal_2.copy()

        cls.statement = cls.env['account.bank.statement'].create({
            'name': 'test_statement',
            'line_ids': [
                (0, 0, {
                    'date': '2019-01-01',
                    'payment_ref': 'line_1',
                    'partner_id': cls.partner_a.id,
                    'foreign_currency_id': cls.currency_2.id,
                    'journal_id': cls.bank_journal_1.id,
                    'amount': 1250.0,
                    'amount_currency': 2500.0,
                }),
            ],
        })
        cls.statement_line = cls.statement.line_ids

        cls.expected_st_line = {
            'date': fields.Date.from_string('2019-01-01'),
            'journal_id': cls.statement.journal_id.id,
            'payment_ref': 'line_1',
            'partner_id': cls.partner_a.id,
            'currency_id': cls.currency_1.id,
            'foreign_currency_id': cls.currency_2.id,
            'amount': 1250.0,
            'amount_currency': 2500.0,
            'is_reconciled': False,
        }

        cls.expected_bank_line = {
            'name': cls.statement_line.payment_ref,
            'partner_id': cls.statement_line.partner_id.id,
            'currency_id': cls.currency_1.id,
            'account_id': cls.statement.journal_id.default_account_id.id,
            'debit': 1250.0,
            'credit': 0.0,
            'amount_currency': 1250.0,
        }

        cls.expected_counterpart_line = {
            'name': cls.statement_line.payment_ref,
            'partner_id': cls.statement_line.partner_id.id,
            'currency_id': cls.currency_2.id,
            'account_id': cls.statement.journal_id.suspense_account_id.id,
            'debit': 0.0,
            'credit': 1250.0,
            'amount_currency': -2500.0,
        }

    def assertBankStatementLine(self, statement_line, expected_statement_line_vals, expected_move_line_vals):
        self.assertRecordValues(statement_line, [expected_statement_line_vals])
        self.assertRecordValues(statement_line.line_ids.sorted('balance'), expected_move_line_vals)

    def create_bank_transaction(self, amount, date, amount_currency=None, currency=None, statement=None,
                                partner=None, journal=None, sequence=0):
        values = {
            'payment_ref': str(amount),
            'amount': amount,
            'date': date,
            'partner_id': partner and partner.id,
            'sequence': sequence,
        }
        if amount_currency:
            values['amount_currency'] = amount_currency
            values['foreign_currency_id'] = currency.id
        if statement and journal and statement.journal_id != journal:
            raise (ValidationError("The statement and the journal are contradictory"))
        if statement:
            values['journal_id'] = statement.journal_id.id
            values['statement_id'] = statement.id
        if journal:
            values['journal_id'] = journal.id
        if not values.get('journal_id'):
            values['journal_id'] = (self.company_data_2['default_journal_bank']
                                    if self.env.company == self.company_data_2['company']
                                    else self.company_data['default_journal_bank']
                                    ).id
        return self.env['account.bank.statement.line'].create(values)

    # -------------------------------------------------------------------------
    # TESTS about the statement line model.
    # -------------------------------------------------------------------------

    def _test_statement_line_edition(
            self,
            journal,
            amount, amount_currency,
            journal_currency, foreign_currency,
            expected_liquidity_values, expected_counterpart_values):
        ''' Test the edition of a statement line from itself or from its linked journal entry.
        :param journal:                     The account.journal record that will be set on the statement line.
        :param amount:                      The amount in journal's currency.
        :param amount_currency:             The amount in the foreign currency.
        :param journal_currency:            The journal's currency as a res.currency record.
        :param foreign_currency:            The foreign currency as a res.currency record.
        :param expected_liquidity_values:   The expected account.move.line values for the liquidity line.
        :param expected_counterpart_values: The expected account.move.line values for the counterpart line.
        '''
        if journal_currency:
            journal.currency_id = journal_currency.id

        statement_line = self.env['account.bank.statement.line'].create({
            'date': '2019-01-01',
            'journal_id': journal.id,
            'payment_ref': 'line_1',
            'partner_id': self.partner_a.id,
            'foreign_currency_id': foreign_currency and foreign_currency.id,
            'amount': amount,
            'amount_currency': amount_currency,
        })

        # ==== Test the statement line amounts are correct ====
        # If there is a bug in the compute/inverse methods, the amount/amount_currency could be
        # incorrect directly after the creation of the statement line.

        self.assertRecordValues(statement_line, [{
            'amount': amount,
            'amount_currency': amount_currency,
        }])
        self.assertRecordValues(statement_line.move_id, [{
            'partner_id': self.partner_a.id,
            'currency_id': (statement_line.foreign_currency_id or statement_line.currency_id).id,
        }])

        # ==== Test the edition of statement line amounts ====
        # The statement line must remain consistent with its account.move.
        # To test the compute/inverse methods are correctly managing all currency setup,
        # we check the edition of amounts in both directions statement line <-> journal entry.

        # Check initial state of the statement line.
        liquidity_lines, suspense_lines, other_lines = statement_line._seek_for_lines()
        self.assertRecordValues(liquidity_lines, [expected_liquidity_values])
        self.assertRecordValues(suspense_lines, [expected_counterpart_values])

        # Check the account.move is still correct after editing the account.bank.statement.line.
        statement_line.write({
            'amount': statement_line.amount * 2,
            'amount_currency': statement_line.amount_currency * 2,
        })
        self.assertRecordValues(statement_line, [{
            'amount': amount * 2,
            'amount_currency': amount_currency * 2,
        }])
        self.assertRecordValues(liquidity_lines, [{
            **expected_liquidity_values,
            'debit': expected_liquidity_values.get('debit', 0.0) * 2,
            'credit': expected_liquidity_values.get('credit', 0.0) * 2,
            'amount_currency': expected_liquidity_values.get('amount_currency', 0.0) * 2,
        }])
        self.assertRecordValues(suspense_lines, [{
            'debit': expected_counterpart_values.get('debit', 0.0) * 2,
            'credit': expected_counterpart_values.get('credit', 0.0) * 2,
            'amount_currency': expected_counterpart_values.get('amount_currency', 0.0) * 2,
        }])

        # Check the account.bank.statement.line is still correct after editing the account.move.
        statement_line.move_id.with_context(skip_readonly_check=True).write({'line_ids': [
            (1, liquidity_lines.id, {
                'debit': expected_liquidity_values.get('debit', 0.0),
                'credit': expected_liquidity_values.get('credit', 0.0),
                'amount_currency': expected_liquidity_values.get('amount_currency', 0.0),
            }),
            (1, suspense_lines.id, {
                'debit': expected_counterpart_values.get('debit', 0.0),
                'credit': expected_counterpart_values.get('credit', 0.0),
                'amount_currency': expected_counterpart_values.get('amount_currency', 0.0),
            }),
        ]})
        self.assertRecordValues(statement_line, [{
            'amount': amount,
            'amount_currency': amount_currency,
        }])

    def _test_edition_customer_and_supplier_flows(
            self,
            amount, amount_currency,
            journal_currency, foreign_currency,
            expected_liquidity_values, expected_counterpart_values):
        ''' Test '_test_statement_line_edition' using the customer (positive amounts)
        & the supplier flow (negative amounts).
        :param amount:                      The amount in journal's currency.
        :param amount_currency:             The amount in the foreign currency.
        :param journal_currency:            The journal's currency as a res.currency record.
        :param foreign_currency:            The foreign currency as a res.currency record.
        :param expected_liquidity_values:   The expected account.move.line values for the liquidity line.
        :param expected_counterpart_values: The expected account.move.line values for the counterpart line.
        '''

        # Check the full process with positive amount (customer process).
        self._test_statement_line_edition(
            self.bank_journal_2,
            amount, amount_currency,
            journal_currency, foreign_currency,
            expected_liquidity_values,
            expected_counterpart_values,
        )

        # Check the full process with negative amount (supplier process).
        self._test_statement_line_edition(
            self.bank_journal_3,
            -amount, -amount_currency,
            journal_currency, foreign_currency,
            {
                **expected_liquidity_values,
                'debit': expected_liquidity_values.get('credit', 0.0),
                'credit': expected_liquidity_values.get('debit', 0.0),
                'amount_currency': -expected_liquidity_values.get('amount_currency', 0.0),
            },
            {
                **expected_counterpart_values,
                'debit': expected_counterpart_values.get('credit', 0.0),
                'credit': expected_counterpart_values.get('debit', 0.0),
                'amount_currency': -expected_counterpart_values.get('amount_currency', 0.0),
            },
        )

    def test_edition_journal_curr_2_statement_curr_3(self):
        self._test_edition_customer_and_supplier_flows(
            # pylint: disable=bad-whitespace
            80.0,               120.0,
            self.currency_2,    self.currency_3,
            {'debit': 40.0,     'credit': 0.0,      'amount_currency': 80.0,        'currency_id': self.currency_2.id},
            {'debit': 0.0,      'credit': 40.0,     'amount_currency': -120.0,      'currency_id': self.currency_3.id},
        )

    def test_edition_journal_curr_2_statement_curr_1(self):
        self._test_edition_customer_and_supplier_flows(
            # pylint: disable=bad-whitespace
            120.0,              80.0,
            self.currency_2,    self.currency_1,
            {'debit': 80.0,     'credit': 0.0,      'amount_currency': 120.0,       'currency_id': self.currency_2.id},
            {'debit': 0.0,      'credit': 80.0,     'amount_currency': -80.0,       'currency_id': self.currency_1.id},
        )

    def test_edition_journal_curr_1_statement_curr_2(self):
        self._test_edition_customer_and_supplier_flows(
            # pylint: disable=bad-whitespace
            80.0,               120.0,
            self.currency_1,    self.currency_2,
            {'debit': 80.0,     'credit': 0.0,      'amount_currency': 80.0,        'currency_id': self.currency_1.id},
            {'debit': 0.0,      'credit': 80.0,     'amount_currency': -120.0,      'currency_id': self.currency_2.id},
        )

    def test_edition_journal_curr_2_statement_false(self):
        self._test_edition_customer_and_supplier_flows(
            # pylint: disable=bad-whitespace
            80.0,               0.0,
            self.currency_2,    False,
            {'debit': 40.0,     'credit': 0.0,      'amount_currency': 80.0,        'currency_id': self.currency_2.id},
            {'debit': 0.0,      'credit': 40.0,     'amount_currency': -80.0,       'currency_id': self.currency_2.id},
        )

    def test_edition_journal_curr_1_statement_false(self):
        self._test_edition_customer_and_supplier_flows(
            # pylint: disable=bad-whitespace
            80.0,               0.0,
            self.currency_1,    False,
            {'debit': 80.0,     'credit': 0.0,      'amount_currency': 80.0,        'currency_id': self.currency_1.id},
            {'debit': 0.0,      'credit': 80.0,     'amount_currency': -80.0,       'currency_id': self.currency_1.id},
        )

    def test_zero_amount_journal_curr_1_statement_curr_2(self):
        self.bank_journal_2.currency_id = self.currency_1

        statement_line = self.env['account.bank.statement.line'].create({
            'journal_id': self.bank_journal_2.id,
            'date': '2019-01-01',
            'payment_ref': 'line_1',
            'partner_id': self.partner_a.id,
            'foreign_currency_id': self.currency_2.id,
            'amount': 0.0,
            'amount_currency': 10.0,
        })

        self.assertRecordValues(statement_line.move_id.line_ids, [
            # pylint: disable=bad-whitespace
            {'debit': 0.0,      'credit': 0.0,      'amount_currency': 0.0,         'currency_id': self.currency_1.id},
            {'debit': 0.0,      'credit': 0.0,      'amount_currency': -10.0,       'currency_id': self.currency_2.id},
        ])

    def test_zero_amount_journal_curr_2_statement_curr_1(self):
        self.bank_journal_2.currency_id = self.currency_2

        statement_line = self.env['account.bank.statement.line'].create({
            'journal_id': self.bank_journal_2.id,
            'date': '2019-01-01',
            'payment_ref': 'line_1',
            'partner_id': self.partner_a.id,
            'foreign_currency_id': self.currency_1.id,
            'amount': 0.0,
            'amount_currency': 10.0,
        })

        self.assertRecordValues(statement_line.move_id.line_ids, [
            # pylint: disable=bad-whitespace
            {'debit': 10.0,     'credit': 0.0,      'amount_currency': 0.0,         'currency_id': self.currency_2.id},
            {'debit': 0.0,      'credit': 10.0,     'amount_currency': -10.0,       'currency_id': self.currency_1.id},
        ])

    def test_zero_amount_journal_curr_2_statement_curr_3(self):
        self.bank_journal_2.currency_id = self.currency_2

        statement_line = self.env['account.bank.statement.line'].create({
            'journal_id': self.bank_journal_2.id,
            'date': '2019-01-01',
            'payment_ref': 'line_1',
            'partner_id': self.partner_a.id,
            'foreign_currency_id': self.currency_3.id,
            'amount': 0.0,
            'amount_currency': 10.0,
        })

        self.assertRecordValues(statement_line.move_id.line_ids, [
            # pylint: disable=bad-whitespace
            {'debit': 0.0,      'credit': 0.0,      'amount_currency': 0.0,         'currency_id': self.currency_2.id},
            {'debit': 0.0,      'credit': 0.0,      'amount_currency': -10.0,       'currency_id': self.currency_3.id},
        ])

    def test_constraints(self):
        def assertStatementLineConstraint(statement_line_vals):
            with self.assertRaises(Exception), self.cr.savepoint():
                self.env['account.bank.statement.line'].create(statement_line_vals)

        statement_line_vals = {
            'journal_id': self.bank_journal_2.id,
            'date': '2019-01-01',
            'payment_ref': 'line_1',
            'partner_id': self.partner_a.id,
            'foreign_currency_id': False,
            'amount': 10.0,
            'amount_currency': 0.0,
        }

        # ==== Test constraints at creation ====

        # Can't have a stand alone amount in foreign currency without foreign currency set.
        assertStatementLineConstraint({
            **statement_line_vals,
            'amount_currency': 10.0,
        })

        # Can't have a foreign currency set without amount in foreign currency.
        assertStatementLineConstraint({
            **statement_line_vals,
            'foreign_currency_id': self.currency_2.id,
        })

        # ==== Test constraints at edition ====

        st_line = self.env['account.bank.statement.line'].create(statement_line_vals)

        # You can't messed up the journal entry by adding another liquidity line.
        addition_lines_to_create = [
            {
                'debit': 1.0,
                'credit': 0,
                'account_id': self.bank_journal_2.default_account_id.id,
                'move_id': st_line.move_id.id,
            },
            {
                'debit': 0,
                'credit': 1.0,
                'account_id': self.company_data['default_account_revenue'].id,
                'move_id': st_line.move_id.id,
            },
        ]
        with self.assertRaises(UserError), self.cr.savepoint():
            st_line.move_id.write({
                'line_ids': [(0, 0, vals) for vals in addition_lines_to_create]
            })

        with self.assertRaises(UserError), self.cr.savepoint():
            st_line.line_ids.create(addition_lines_to_create)

    def test_statement_line_move_onchange_1(self):
        ''' Test the consistency between the account.bank.statement.line and the generated account.move.lines
        using the form view emulator.
        '''

        # Check the initial state of the statement line.
        self.assertBankStatementLine(self.statement_line, self.expected_st_line,
                                     [self.expected_counterpart_line, self.expected_bank_line])

        # Inverse the amount + change them.
        self.statement_line.write({
            'amount': -2000.0,
            'amount_currency': -4000.0,
            'foreign_currency_id': self.currency_3.id,
        })

        self.assertBankStatementLine(self.statement_line, {
            **self.expected_st_line,
            'amount': -2000.0,
            'amount_currency': -4000.0,
            'foreign_currency_id': self.currency_3.id,
        }, [
            {
                **self.expected_bank_line,
                'debit': 0.0,
                'credit': 2000.0,
                'amount_currency': -2000.0,
                'currency_id': self.currency_1.id,
            },
            {
                **self.expected_counterpart_line,
                'debit': 2000.0,
                'credit': 0.0,
                'amount_currency': 4000.0,
                'currency_id': self.currency_3.id,
            },
        ])

        # Check changing the label and the partner.
        self.statement_line.write({
            'payment_ref': 'line_1 (bis)',
            'partner_id': self.partner_b.id,
        })

        self.assertBankStatementLine(self.statement_line, {
            **self.expected_st_line,
            'payment_ref': self.statement_line.payment_ref,
            'partner_id': self.statement_line.partner_id.id,
            'amount': -2000.0,
            'amount_currency': -4000.0,
            'foreign_currency_id': self.currency_3.id,
        }, [
            {
                **self.expected_bank_line,
                'name': self.statement_line.payment_ref,
                'partner_id': self.statement_line.partner_id.id,
                'debit': 0.0,
                'credit': 2000.0,
                'amount_currency': -2000.0,
                'currency_id': self.currency_1.id,
            },
            {
                **self.expected_counterpart_line,
                'name': self.statement_line.payment_ref,
                'partner_id': self.statement_line.partner_id.id,
                'debit': 2000.0,
                'credit': 0.0,
                'amount_currency': 4000.0,
                'currency_id': self.currency_3.id,
            },
        ])

    def test_prepare_counterpart_amounts_using_st_line_rate(self):

        def assertAppliedRate(
            journal_currency, foreign_currency, aml_currency,
            amount, amount_currency, aml_amount_currency, aml_balance,
            expected_amount_currency, expected_balance,
        ):
            journal = self.bank_journal_1.copy()
            journal.currency_id = journal_currency

            statement_line = self.env['account.bank.statement.line'].create({
                'journal_id': journal.id,
                'date': '2019-01-01',
                'payment_ref': 'test_prepare_counterpart_amounts_using_st_line_rate',
                'foreign_currency_id': foreign_currency.id if foreign_currency != journal_currency else None,
                'amount': amount,
                'amount_currency': amount_currency if foreign_currency != journal_currency else 0.0,
            })

            res = statement_line._prepare_counterpart_amounts_using_st_line_rate(aml_currency, -aml_balance,
                                                                                 -aml_amount_currency)
            self.assertAlmostEqual(res['amount_currency'], expected_amount_currency)
            self.assertAlmostEqual(res['balance'], expected_balance)

        for params in (
            (self.currency_2, self.currency_3, self.currency_3, 80.0, 120.0, 120.0, 20.0, -120.0, -40.0),
            (self.currency_2, self.currency_1, self.currency_2, 120.0, 80.0, 120.0, 40.0, -80.0, -80.0),
            (self.currency_2, self.currency_3, self.currency_2, 80.0, 120.0, 80.0, 26.67, -120.0, -40.0),
            (self.currency_2, self.currency_3, self.currency_4, 80.0, 120.0, 480.0, 40.0, -120.0, -40.0),
            (self.currency_1, self.currency_2, self.currency_2, 80.0, 120.0, 120.0, 40.0, -120.0, -80.0),
            (self.currency_1, self.currency_2, self.currency_3, 80.0, 120.0, 480.0, 80.0, -120.0, -80.0),
            (self.currency_2, self.currency_2, self.currency_2, 80.0, 80.0, 80.0, 26.67, -80.0, -40.0),
            (self.currency_2, self.currency_2, self.currency_3, 80.0, 80.0, 240.0, 40.0, -80.0, -40.0),
            (self.currency_1, self.currency_1, self.currency_3, 80.0, 80.0, 480.0, 80.0, -80.0, -80.0),
            (self.currency_2, self.currency_1, self.currency_1, 120.0, 80.0, 80.0, 80.0, -80.0, -80.0),
            (self.currency_2, self.currency_3, self.currency_1, 80.0, 120.0, 40.0, 40.0, -120.0, -40.0),
            (self.currency_1, self.currency_2, self.currency_1, 80.0, 120.0, 80.0, 80.0, -120.0, -80.0),
            (self.currency_2, self.currency_2, self.currency_1, 80.0, 80.0, 40.0, 40.0, -80.0, -40.0),
            (self.currency_1, self.currency_1, self.currency_1, 80.0, 80.0, 80.0, 80.0, -80.0, -80.0),
        ):
            with self.subTest(params=params):
                assertAppliedRate(*params)

    def test_for_presence_single_suspense_line(self):
        statement_line = self.env['account.bank.statement.line'].create({
            'journal_id': self.bank_journal_3.id,
            'date': '2019-01-01',
            'payment_ref': 'line_1',
            'amount': 0.0,
        })

        with self.assertRaises(UserError):
            statement_line.line_ids = [Command.create({
                'account_id': statement_line.journal_id.suspense_account_id.id,
                'balance': 0.0,
            })]

    def test_zero_amount_statement_line(self):
        ''' Ensure the statement line is directly marked as reconciled when having an amount of zero. '''
        self.company_data['company'].account_journal_suspense_account_id.reconcile = False

        statement = self.env['account.bank.statement'].with_context(skip_check_amounts_currencies=True).create({
            'name': 'test_statement',
            'line_ids': [
                (0, 0, {
                    'date': '2019-01-01',
                    'payment_ref': "Happy new year",
                    'amount': 0.0,
                    'journal_id': self.bank_journal_2.id,
                }),
            ],
        })
        statement_line = statement.line_ids

        self.assertRecordValues(statement_line, [{'is_reconciled': True, 'amount_residual': 0.0}])

    def test_statement_valid_complete_1(self):
        self.env.user.company_id = self.company_data_2['company']

        # create a valid and complete statement as the first lines (no statement before)
        line1 = self.create_bank_transaction(1, '2020-01-10')
        line2 = self.create_bank_transaction(2, '2020-01-11')
        statement1 = self.env['account.bank.statement'].with_context(st_line_id=line1.id, active_ids=[line1.id, line2.id]).create({})
        self.assertRecordValues(statement1, [{
            'is_complete': True,
            'is_valid': True,
            'balance_start': 0,
            'balance_end': 3,
            'balance_end_real': 3,
        }])
        # remove the first line, so not complete but it is still valid because there is no statement before
        line1.statement_id = False
        self.assertRecordValues(statement1, [{
            'is_complete': False,
            'is_valid': True,
            'balance_start': 0,
            'balance_end': 2,
            'balance_end_real': 3,
        }])
        # create a new line in the statement to make it complete again. Starting value does not match the last line
        # but it is still valid because it is the first statement
        line3 = self.create_bank_transaction(1, '2020-01-12', statement=statement1)
        statement1.invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1, [{
            'is_complete': True,
            'is_valid': True,
        }])
        # add a statement to the first line, statement1 is still complete but not valid because balance start
        # does not match the previous statement
        statement2 = self.env['account.bank.statement'].create({
            'line_ids': [Command.set(line1.ids)],
            'balance_end_real': 1,
        })
        (statement1 + statement2).invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1 + statement2, [{
            'is_complete': True,
            'is_valid': False,
        }, {
            'is_complete': True,
            'is_valid': True,  # first statement
        }])
        # Fix the statement balance start, the balance_end_real is computed, making it complete
        statement1.balance_start = 1
        statement1.invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1, [{
            'is_complete': True,
            'is_valid': True,
        }])
        # change the prev statement so the end balance does not match the start balance of statement 1
        statement2.balance_end_real = 10
        statement2.flush_recordset(['balance_end_real'])
        statement1.invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1, [{
            'is_complete': True,
            'is_valid': False,
        }])
        # make the statement valid again, but keep it incomplete
        statement1.write({'balance_start': 10, 'balance_end_real': 3})
        statement1.invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1, [{
            'is_complete': False,
            'is_valid': True,
        }])
        # and complete again by adding a new transaction to it
        line4 = self.create_bank_transaction(-10, '2020-01-13', statement=statement1)
        (statement1 + statement2).invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1 + statement2, [{
            'is_complete': True,
            'is_valid': True,
            'date': fields.Date.from_string('2020-01-13'),
        }, {
            'is_complete': False,
            'is_valid': True,
            'date': fields.Date.from_string('2020-01-10'),
        }])
        # check point
        self.assertRecordValues(line1 + line2 + line3 + line4, [
            {'date': fields.Date.from_string('2020-01-10'), 'statement_id': statement2.id},
            {'date': fields.Date.from_string('2020-01-11'), 'statement_id': statement1.id},
            {'date': fields.Date.from_string('2020-01-12'), 'statement_id': statement1.id},
            {'date': fields.Date.from_string('2020-01-13'), 'statement_id': statement1.id},
        ])

        # changing statement 2 balance makes statement 1 invalid,
        # but making statement 1 the first statement should make it valid again
        statement2.balance_end_real = 100
        statement2.flush_recordset(['balance_end_real'])
        statement1.invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1, [{
            'is_valid': False,
        }])
        line1.statement_id = False
        line1.flush_model()
        statement2.flush_model()
        statement1.invalidate_model(['is_valid'])
        self.assertRecordValues(statement1, [{
            'is_valid': True,
        }])

        # having a gap in the statement shouldn't make it invalid
        line3.statement_id = False
        statement1.flush_recordset(['is_valid'])
        self.assertRecordValues(statement1, [{
            'is_valid': True,
        }])

        # Change the statement on one of the lines of statement 1
        statement3 = self.env['account.bank.statement'].create({
            'line_ids': [Command.set(line4.ids)],
            'balance_start': -5,
        })
        (statement1 + statement3).flush_recordset(['is_valid'])
        self.assertRecordValues(statement1 + statement3, [{
            'is_valid': True,
        }, {
            'is_valid': False,  # balance does not match with statement1
        }])

        # changing statement1 end_balance should change the validity of statement3
        statement1.balance_end_real = -5
        statement1.flush_recordset(['balance_end_real'])
        (statement1 + statement3).invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1 + statement3, [{
            'is_valid': True,
        }, {
            'is_valid': True,  # balance start matches previous end, despite the gap
        }])
        # check point
        self.assertRecordValues(line1 + line2 + line3 + line4, [
            {'date': fields.Date.from_string('2020-01-10'), 'statement_id': False},
            {'date': fields.Date.from_string('2020-01-11'), 'statement_id': statement1.id},
            {'date': fields.Date.from_string('2020-01-12'), 'statement_id': False},
            {'date': fields.Date.from_string('2020-01-13'), 'statement_id': statement3.id},
        ])
        self.assertRecordValues(statement1 + statement2 + statement3, [
            {'is_valid': True, 'balance_start': 10, 'balance_end_real': -5,
             'date': fields.Date.from_string('2020-01-11')},
            {'is_valid': True, 'balance_start': False, 'balance_end_real': 100, 'date': False},
            {'is_valid': True, 'balance_start': -5, 'balance_end_real': -15,
             'date': fields.Date.from_string('2020-01-13')},
        ])

        # adding a statement to the first line should make statement1 invalid
        line1.statement_id = statement2
        statement2.flush_model()
        (statement1 + statement2).invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1 + statement2, [{'is_valid': False}, {'is_valid': True}])

        # moving statement2 the line between statement1 and statement3 should make statement1 valid again
        # and statement3 invalid
        statement2.line_ids = line3
        statement2.flush_model()
        (statement1 + statement2 + statement3).invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1 + statement2 + statement3, [
            {'is_valid': True}, {'is_valid': False}, {'is_valid': False},
        ])

    def test_statement_line_ordering(self):
        self.env.user.company_id = self.company_data_2['company']

        # the line numbers are chosen based on the order of the lines in the list view
        line7 = self.create_bank_transaction(7, '2020-01-10', sequence=1)
        line8 = self.create_bank_transaction(8, '2020-01-10', sequence=2)
        line2 = self.create_bank_transaction(2, '2020-01-13')
        line6 = self.create_bank_transaction(6, '2020-01-11')
        line5 = self.create_bank_transaction(5, '2020-01-12', sequence=3)
        line4 = self.create_bank_transaction(4, '2020-01-12', sequence=2)
        line1 = self.create_bank_transaction(1, '2020-01-13')
        line3 = self.create_bank_transaction(3, '2020-01-12', sequence=1)

        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                {'amount': 1, 'running_balance': 36, 'statement_id': False},
                {'amount': 2, 'running_balance': 35, 'statement_id': False},
                {'amount': 3, 'running_balance': 33, 'statement_id': False},
                {'amount': 4, 'running_balance': 30, 'statement_id': False},
                {'amount': 5, 'running_balance': 26, 'statement_id': False},
                {'amount': 6, 'running_balance': 21, 'statement_id': False},
                {'amount': 7, 'running_balance': 15, 'statement_id': False},
                {'amount': 8, 'running_balance': 8, 'statement_id': False},
            ],
        )

        # Same but with a subset of lines to ensure the balance is not only computed based on selected records.
        self.env['account.bank.statement.line'].invalidate_model(fnames=['running_balance'])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([
                ('company_id', '=', self.env.company.id),
                ('amount', '>=', 3),
                ('amount', '<=', 6),
            ]),
            [
                {'amount': 3, 'running_balance': 33},
                {'amount': 4, 'running_balance': 30},
                {'amount': 5, 'running_balance': 26},
                {'amount': 6, 'running_balance': 21},
            ],
        )

        # Put line2 -> line4 inside a statement with a wrong balance_end_real.
        (line2 + line3 + line4).statement_id = statement1 = \
            self.env['account.bank.statement'].create({'balance_start': 20, 'balance_end_real': 29})

        self.assertRecordValues(statement1, [{
            'is_complete': True,
        }])

        statement1.invalidate_recordset(['is_valid'])
        statement1.balance_start = 26
        self.assertRecordValues(statement1, [{
            'is_complete': True,
            'balance_end_real': 35, # autocorrect
        }])

        # line3, line4 and line5 have the same date. Move line5 at the first place using the sequence.
        line5.sequence = -1
        statement1.invalidate_recordset(['is_valid'])
        self.env['account.bank.statement.line'].invalidate_model(fnames=['running_balance'])
        self.assertRecordValues(statement1, [{
            'is_complete': True,
        }])

        statement1.balance_start = 21
        statement1.invalidate_recordset(['is_valid'])
        self.assertRecordValues(statement1, [{
            'is_complete': True,
            'balance_end_real': 30,
        }])

        self.env['account.bank.statement.line'].invalidate_model(fnames=['running_balance'])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 36, 'statement_id': False},
                {'amount': 2, 'running_balance': 35, 'statement_id': statement1.id},
                {'amount': 5, 'running_balance': 33, 'statement_id': False},
                {'amount': 3, 'running_balance': 28, 'statement_id': statement1.id},
                {'amount': 4, 'running_balance': 25, 'statement_id': statement1.id},
                {'amount': 6, 'running_balance': 21, 'statement_id': False},
                {'amount': 7, 'running_balance': 15, 'statement_id': False},
                {'amount': 8, 'running_balance': 8, 'statement_id': False},
            ],
        )

        line8.amount = 18

        self.env['account.bank.statement.line'].invalidate_model(fnames=['running_balance'])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 36, 'statement_id': False},
                {'amount': 2, 'running_balance': 35, 'statement_id': statement1.id},
                {'amount': 5, 'running_balance': 33, 'statement_id': False},
                {'amount': 3, 'running_balance': 28, 'statement_id': statement1.id},
                {'amount': 4, 'running_balance': 25, 'statement_id': statement1.id},
                {'amount': 6, 'running_balance': 31, 'statement_id': False},
                {'amount': 7, 'running_balance': 25, 'statement_id': False},
                {'amount': 18, 'running_balance': 18, 'statement_id': False},
            ],
        )
        line5.amount = 15

        self.env['account.bank.statement.line'].invalidate_model(fnames=['running_balance'])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 46, 'statement_id': False},
                {'amount': 2, 'running_balance': 45, 'statement_id': statement1.id},
                {'amount': 15, 'running_balance': 43, 'statement_id': False},
                {'amount': 3, 'running_balance': 28, 'statement_id': statement1.id},
                {'amount': 4, 'running_balance': 25, 'statement_id': statement1.id},
                {'amount': 6, 'running_balance': 31, 'statement_id': False},
                {'amount': 7, 'running_balance': 25, 'statement_id': False},
                {'amount': 18, 'running_balance': 18, 'statement_id': False},
            ],
        )

        line7.unlink()

        self.env['account.bank.statement.line'].invalidate_model(fnames=['running_balance'])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 46, 'statement_id': False},
                {'amount': 2, 'running_balance': 45, 'statement_id': statement1.id},
                {'amount': 15, 'running_balance': 43, 'statement_id': False},
                {'amount': 3, 'running_balance': 28, 'statement_id': statement1.id},
                {'amount': 4, 'running_balance': 25, 'statement_id': statement1.id},
                {'amount': 6, 'running_balance': 24, 'statement_id': False},
                {'amount': 18, 'running_balance': 18, 'statement_id': False},
            ],
        )
        line1.move_id.button_cancel()
        line6.move_id.button_cancel()
        line6.move_id.button_draft()
        self.env['account.bank.statement.line'].invalidate_model(fnames=['running_balance'])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 45, 'statement_id': False},
                {'amount': 2, 'running_balance': 45, 'statement_id': statement1.id},
                {'amount': 15, 'running_balance': 43, 'statement_id': False},
                {'amount': 3, 'running_balance': 28, 'statement_id': statement1.id},
                {'amount': 4, 'running_balance': 25, 'statement_id': statement1.id},
                {'amount': 6, 'running_balance': 18, 'statement_id': False},
                {'amount': 18, 'running_balance': 18, 'statement_id': False},
            ],
        )

        # remove the anchor point
        statement1.line_ids = False
        self.env['account.bank.statement.line'].invalidate_model(fnames=['running_balance'])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 42, 'statement_id': False},
                {'amount': 2, 'running_balance': 42, 'statement_id': False},
                {'amount': 15, 'running_balance': 40, 'statement_id': False},
                {'amount': 3, 'running_balance': 25, 'statement_id': False},
                {'amount': 4, 'running_balance': 22, 'statement_id': False},
                {'amount': 6, 'running_balance': 18, 'statement_id': False},
                {'amount': 18, 'running_balance': 18, 'statement_id': False},
            ],
        )

    def test_statement_split(self):
        self.env.user.company_id = self.company_data_2['company']

        # the line numbers are chosen based on the order of the lines in the list view
        line7 = self.create_bank_transaction(7, '2020-01-10', sequence=1)
        line8 = self.create_bank_transaction(8, '2020-01-10', sequence=2)
        line2 = self.create_bank_transaction(2, '2020-01-13')
        line6 = self.create_bank_transaction(6, '2020-01-11')
        _line5 = self.create_bank_transaction(5, '2020-01-12', sequence=3)
        line4 = self.create_bank_transaction(4, '2020-01-12', sequence=2)
        line1 = self.create_bank_transaction(1, '2020-01-13')
        line3 = self.create_bank_transaction(3, '2020-01-12', sequence=1)

        # Split the last 2 lines by splitting on the line before last.
        statement1 = self.env['account.bank.statement'].with_context({'split_line_id': line7.id}).create({})
        self.assertRecordValues(statement1, [{
            'balance_start': 0.0,
            'balance_end': 15.0,
            'balance_end_real': 15.0,
            'is_complete': True,
            'is_valid': True,
        }])
        self.assertRecordValues(line7 + line8 + line6, [
            {'amount': 7, 'statement_id': statement1.id},
            {'amount': 8, 'statement_id': statement1.id},
            {'amount': 6, 'statement_id': False},
        ])

        # Split on a line adjutant to another statement
        statement2 = self.env['account.bank.statement'].with_context({'split_line_id': line6.id}).create({})
        self.assertRecordValues(statement2, [{
            'balance_start': 15.0,
            'balance_end': 21.0,
            'balance_end_real': 21.0,
            'is_complete': True,
            'is_valid': True,
        }])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 36, 'statement_id': False},
                {'amount': 2, 'running_balance': 35, 'statement_id': False},
                {'amount': 3, 'running_balance': 33, 'statement_id': False},
                {'amount': 4, 'running_balance': 30, 'statement_id': False},
                {'amount': 5, 'running_balance': 26, 'statement_id': False},
                {'amount': 6, 'running_balance': 21, 'statement_id': statement2.id},
                {'amount': 7, 'running_balance': 15, 'statement_id': statement1.id},
                {'amount': 8, 'running_balance': 8,  'statement_id': statement1.id},
            ],
        )

        # Split on a line with a gap to another statement
        statement1.unlink()
        statement3 = self.env['account.bank.statement'].with_context({'split_line_id': line3.id}).create({})
        self.assertRecordValues(statement3, [{
            'balance_start': 21.0,
            'balance_end': 33.0,
            'balance_end_real': 33.0,
            'is_complete': True,
            'is_valid': True,
        }])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 36, 'statement_id': False},
                {'amount': 2, 'running_balance': 35, 'statement_id': False},
                {'amount': 3, 'running_balance': 33, 'statement_id': statement3.id},
                {'amount': 4, 'running_balance': 30, 'statement_id': statement3.id},
                {'amount': 5, 'running_balance': 26, 'statement_id': statement3.id},
                {'amount': 6, 'running_balance': 21, 'statement_id': statement2.id},
                {'amount': 7, 'running_balance': 15, 'statement_id': False},
                {'amount': 8, 'running_balance': 8,  'statement_id': False},
            ],
        )
        # Split on a line with a single line statement
        statement4 = self.env['account.bank.statement'].with_context({'split_line_id': line6.id}).create({})
        self.assertRecordValues(statement3 + statement4, [
            {
                'balance_start': 21.0,
                'balance_end': 33.0,
                'balance_end_real': 33.0,
                'is_complete': True,
                'is_valid': True,
            },
            {
                'balance_start': 0.0,
                'balance_end': 21.0,
                'balance_end_real': 21.0,
                'is_complete': True,
                'is_valid': True,
            },
        ])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 36, 'statement_id': False},
                {'amount': 2, 'running_balance': 35, 'statement_id': False},
                {'amount': 3, 'running_balance': 33, 'statement_id': statement3.id},
                {'amount': 4, 'running_balance': 30, 'statement_id': statement3.id},
                {'amount': 5, 'running_balance': 26, 'statement_id': statement3.id},
                {'amount': 6, 'running_balance': 21, 'statement_id': statement4.id},
                {'amount': 7, 'running_balance': 15, 'statement_id': statement4.id},
                {'amount': 8, 'running_balance': 8,  'statement_id': statement4.id},
            ],
        )
        # check double split on a single line
        statement5 = self.env['account.bank.statement'].with_context({'split_line_id': line2.id}).create({})
        self.assertRecordValues(statement5, [{
            'balance_start': 33.0,
            'balance_end': 35.0,
            'balance_end_real': 35.0,
            'is_complete': True,
            'is_valid': True,
        }])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 36, 'statement_id': False},
                {'amount': 2, 'running_balance': 35, 'statement_id': statement5.id},
                {'amount': 3, 'running_balance': 33, 'statement_id': statement3.id},
                {'amount': 4, 'running_balance': 30, 'statement_id': statement3.id},
                {'amount': 5, 'running_balance': 26, 'statement_id': statement3.id},
                {'amount': 6, 'running_balance': 21, 'statement_id': statement4.id},
                {'amount': 7, 'running_balance': 15, 'statement_id': statement4.id},
                {'amount': 8, 'running_balance': 8,  'statement_id': statement4.id},
            ],
        )
        statement6 = self.env['account.bank.statement'].with_context({'split_line_id': line2.id}).create({'reference': '6'})
        self.assertRecordValues(statement6, [{
            'balance_start': 33.0,
            'balance_end': 35.0,
            'balance_end_real': 35.0,
            'is_complete': True,
            'is_valid': True,
        }])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 36, 'statement_id': False},
                {'amount': 2, 'running_balance': 35, 'statement_id': statement6.id},
                {'amount': 3, 'running_balance': 33, 'statement_id': statement3.id},
                {'amount': 4, 'running_balance': 30, 'statement_id': statement3.id},
                {'amount': 5, 'running_balance': 26, 'statement_id': statement3.id},
                {'amount': 6, 'running_balance': 21, 'statement_id': statement4.id},
                {'amount': 7, 'running_balance': 15, 'statement_id': statement4.id},
                {'amount': 8, 'running_balance': 8,  'statement_id': statement4.id},
            ],
        )

        # Split in the middle of a statement
        statement7 = self.env['account.bank.statement'].with_context({'split_line_id': line4.id}).create({})
        self.assertRecordValues(statement3 + statement7, [
            {
                'balance_start': 21.0,
                'balance_end': 24.0,
                'balance_end_real': 33.0,
                'is_complete': False,
                'is_valid': False,
            },
            {
                'balance_start': 21.0,
                'balance_end': 30.0,
                'balance_end_real': 30.0,
                'is_complete': True,
                'is_valid': True,
            },
        ])

        # Fix statement3
        statement3._compute_balance_start()
        self.assertRecordValues(statement3, [{
            'balance_start': 30.0,
            'balance_end': 33.0,
            'balance_end_real': 33.0,
            'is_complete': True,
            'is_valid': True,
        }])

        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'running_balance': 36, 'statement_id': False},
                {'amount': 2, 'running_balance': 35, 'statement_id': statement6.id},
                {'amount': 3, 'running_balance': 33, 'statement_id': statement3.id},
                {'amount': 4, 'running_balance': 30, 'statement_id': statement7.id},
                {'amount': 5, 'running_balance': 26, 'statement_id': statement7.id},
                {'amount': 6, 'running_balance': 21, 'statement_id': statement4.id},
                {'amount': 7, 'running_balance': 15, 'statement_id': statement4.id},
                {'amount': 8, 'running_balance': 8,  'statement_id': statement4.id},
            ],
        )

        # split at start of another statement
        statement8 = self.env['account.bank.statement'].with_context({'split_line_id': line6.id}).create({})
        self.assertRecordValues(statement7 + statement8, [
            {
                'balance_end_real': 30.0,
                'balance_end': 30.0,
                'balance_start': 21.0,
                'is_complete': True,
                'is_valid': True,
            },
            {
                'balance_end_real': 21.0,
                'balance_end': 21.0,
                'balance_start': 0.0,
                'is_complete': True,
                'is_valid': True,
            },
        ])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'statement_id': False},
                {'amount': 2, 'statement_id': statement6.id},
                {'amount': 3, 'statement_id': statement3.id},
                {'amount': 4, 'statement_id': statement7.id},
                {'amount': 5, 'statement_id': statement7.id},
                {'amount': 6, 'statement_id': statement8.id},
                {'amount': 7, 'statement_id': statement8.id},
                {'amount': 8, 'statement_id': statement8.id},
            ],
        )

        # split at end of another statement
        statement9 = self.env['account.bank.statement'].with_context({'split_line_id': line8.id}).create({})
        self.assertRecordValues(statement8 + statement9, [
            {
                'balance_end_real': 21.0,
                'balance_end': 13.0,
                'balance_start': 0.0,
                'is_complete': False,
                'is_valid': False,
            },
            {
                'balance_end_real': 8.0,
                'balance_end': 8.0,
                'balance_start': 0.0,
                'is_complete': True,
                'is_valid': True,
            },
        ])

        # Fix statement8
        statement8._compute_balance_start() # TODO: add_to_compute not working, why?
        self.assertRecordValues(statement8, [{
            'balance_end_real': 21.0,
            'balance_end': 21.0,
            'balance_start': 8.0,
            'is_complete': True,
            'is_valid': True,
        }])

        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'statement_id': False},
                {'amount': 2, 'statement_id': statement6.id},
                {'amount': 3, 'statement_id': statement3.id},
                {'amount': 4, 'statement_id': statement7.id},
                {'amount': 5, 'statement_id': statement7.id},
                {'amount': 6, 'statement_id': statement8.id},
                {'amount': 7, 'statement_id': statement8.id},
                {'amount': 8, 'statement_id': statement9.id},
            ],
        )

        # split at most recent line
        statement10 = self.env['account.bank.statement'].with_context({'split_line_id': line1.id}).create({})
        self.assertRecordValues(statement10, [{
            'balance_start': 35.0,
            'balance_end': 36.0,
            'balance_end_real': 36.0,
            'is_complete': True,
            'is_valid': True,
        }])
        self.assertRecordValues(
            self.env['account.bank.statement.line'].search([('company_id', '=', self.env.company.id)]),
            [
                # pylint: disable=C0326
                {'amount': 1, 'statement_id': statement10.id},
                {'amount': 2, 'statement_id': statement6.id},
                {'amount': 3, 'statement_id': statement3.id},
                {'amount': 4, 'statement_id': statement7.id},
                {'amount': 5, 'statement_id': statement7.id},
                {'amount': 6, 'statement_id': statement8.id},
                {'amount': 7, 'statement_id': statement8.id},
                {'amount': 8, 'statement_id': statement9.id},
            ],
        )

        all_statements = self.env['account.bank.statement'].search([
            ('line_ids', '!=', False),
            ('company_id', '=', self.env.company.id),
        ])
        all_statements.invalidate_recordset(['is_valid'])
        self.assertRecordValues(all_statements, [{'is_valid': True, 'is_complete': True}] * len(all_statements))

    def test_statement_with_canceled_lines(self):
        line1 = self.create_bank_transaction(1, '2020-01-10', journal=self.bank_journal_2)
        line2 = self.create_bank_transaction(2, '2020-01-11', journal=self.bank_journal_2)
        statement1 = self.env['account.bank.statement'].create({
            'line_ids': [Command.set((line1 + line2).ids)],
        })
        self.assertRecordValues(statement1, [{
            'is_complete': True,
            'is_valid': True,
            'date': fields.Date.from_string(line2.date),
            'balance_start': 0,
            'balance_end_real': 3,
        }])
        # test canceling a line
        line2.move_id.button_cancel()
        self.assertRecordValues(statement1, [{
            'is_complete': False,
            'balance_end': 1,
            'date': fields.Date.from_string(line1.date),
        }])
        # add a line with same amount as the canceled line makes statement1 complete again
        line3 = self.create_bank_transaction(2, '2020-01-12', journal=self.bank_journal_2, statement=statement1)
        self.assertRecordValues(statement1, [{
            'is_complete': True,
            'balance_end': 3,
            'date': fields.Date.from_string(line3.date),
        }])
        # test adding a draft line to a statement, nothing should be changed in statement
        line4 = self.create_bank_transaction(4, '2020-01-13', journal=self.bank_journal_2)
        line4.move_id.button_cancel()
        line4.move_id.button_draft()
        statement1.line_ids |= line4
        self.assertRecordValues(statement1, [{
            'is_complete': True,
            'balance_end': 3,
            'date': fields.Date.from_string(line3.date),
        }])
        # test split with canceled/draft lines
        statement2 = self.env['account.bank.statement'].with_context({'split_line_id': line2.id}).create({})
        self.assertRecordValues(statement1 + statement2, [{
            'is_complete': False,
            'balance_end': 2,
            'balance_start': 0,
            'line_ids': [line4.id, line3.id],
        }, {
            'is_complete': True,
            'balance_start': 0,
            'balance_end': 1,
            'line_ids': [line1.id, line2.id],
        }])

        # test cancel/draft all statement lines
        # line 4 is draft, and we cancel line 3 so the statement should be empty
        line3.move_id.button_cancel()
        self.assertRecordValues(statement1, [{
            'is_complete': False,
            'balance_start': 0,
            'balance_end': 0,
        }])

        # create a statement line with already canceled lines
        statement3 = self.env['account.bank.statement'].create({
            'line_ids': [Command.set((line3 + line4).ids)],
        })
        self.assertRecordValues(statement1 + statement3, [{
            'is_complete': False,
            'balance_start': 0,
            'balance_end': 0,
        }, {
            'is_complete': False, # no posted transactions
            'balance_start': 1,  # from statement2's balance_end_real
            'balance_end': 1, # no posted transactions
        }])

    def test_create_statement_line_with_inconsistent_currencies(self):
        statement_line = self.env['account.bank.statement.line'].create({
            'date': '2019-01-01',
            'journal_id': self.bank_journal_1.id,
            'payment_ref': "Happy new year",
            'amount': 200.0,
            'amount_currency': 200.0,
            'foreign_currency_id': self.env.company.currency_id.id,
        })

        self.assertRecordValues(statement_line, [{
            'currency_id': self.env.company.currency_id.id,
            'foreign_currency_id': False,
            'amount': 200.0,
            'amount_currency': 0.0,
        }])

    def test_statement_balance_warnings(self):
        ''' Ensure that new statements have the correct opening/closing balances or warnings '''
        lines = [
            self.create_bank_transaction(amount, date, journal=self.bank_journal_2)
            for amount, date in [
                (10.0, '2019-01-01'),
                (15.0, '2019-01-02'),
                (20.0, '2019-01-03'),
                (30.0, '2019-01-03'),
                (40.0, '2019-01-04'),
                (50.0, '2019-01-05'),
            ]
        ]

        # new statement from single line
        contexts = [{
            'active_ids': [line.id],
            'st_line_id': line.id,
        } for line in lines[:3]]

        self.assertRecordValues(self.env['account.bank.statement'].with_context(contexts[1]).new({}), [{
            'balance_start': 10.0,
            'balance_end_real': 25.0,
            'is_valid': True,
            'is_complete': True,
        }])

        st1 = self.env['account.bank.statement'].with_context(contexts[0]).create({'name': 'Statement 1'})
        self.assertRecordValues(st1, [{
            'balance_start': 0.0,
            'balance_end_real': 10.0,
            'is_valid': True,
            'is_complete': True,
        }])
        self.assertEqual(lines[0].statement_id, st1)

        self.assertRecordValues(self.env['account.bank.statement'].with_context(contexts[2]).new(), [{
            'balance_start': 25.0,
            'balance_end_real': 45.0,
            'is_valid': False,
            'is_complete': True,
        }])

        self.assertRecordValues(self.env['account.bank.statement'].with_context(contexts[1]).new(), [{
            'balance_start': 10.0,
            'balance_end_real': 25.0,
            'is_valid': True,
            'is_complete': True,
        }])

        # multi line edit, one line with statement
        context = {
            'active_ids': [line.id for line in lines[:3]],
            'st_line_id': lines[2].id,
        }
        self.assertRecordValues(self.env['account.bank.statement'].with_context(context).new({}), [{
            'balance_start': 0.0,
            'balance_end_real': 45.0,
            'is_valid': True,
            'is_complete': True,
            'line_ids': [lines[0].id, lines[1].id, lines[2].id],
        }])

        # multi line edit, skip lines
        context = {
            'active_ids': [line.id for line in lines[2:4]],
            'st_line_id': lines[3].id,
        }
        self.assertRecordValues(self.env['account.bank.statement'].with_context(context).new({}), [{
            'balance_start': 25.0,
            'balance_end_real': 75.0,
            'is_valid': False,
            'is_complete': True,
            'line_ids': [lines[2].id, lines[3].id],
        }])

        # multi line edit
        expected_st_vals = [{
            'balance_start': 10.0,
            'balance_end_real': 75.0,
            'is_valid': True,
            'is_complete': True,
            'line_ids': [lines[1].id, lines[2].id, lines[3].id],
        }]
        context = {
            'active_ids': [line.id for line in lines[1:4]],
            'st_line_id': lines[3].id,
        }
        self.assertRecordValues(self.env['account.bank.statement'].with_context(context).new({}), expected_st_vals)

        # split button
        self.assertRecordValues(self.env['account.bank.statement'].with_context({'split_line_id': lines[3].id}).new({}),
                                expected_st_vals)

        # raise error if lines skipped during multi-edit
        context = {
            'active_ids': [lines[1].id, lines[3].id],
            'st_line_id': lines[3].id,
        }
        with self.assertRaises(UserError):
            self.env['account.bank.statement'].with_context(context).create({})

        # create the second statement using split button
        st2 = self.env['account.bank.statement'].with_context({'split_line_id': lines[-1].id}).create({'name': 'Statement 2'})
        self.assertRecordValues(st2, [{
            'balance_start': 10.0,
            'balance_end_real': 165.0,
            'is_valid': True,
            'is_complete': True,
        }])

    def test_statement_attachments(self):
        ''' Ensure that attachments are properly linked to bank statements '''

        attachment_vals = {
            'datas': base64.b64encode(b'My attachment'),
            'name': 'doc.txt',
        }

        attachment = self.env['ir.attachment'].create(attachment_vals)

        statement = self.env['account.bank.statement'].create({
            'name': 'test_statement',
            'attachment_ids': [Command.set(attachment.ids)],
        })

        attachment = self.env['ir.attachment'].create(attachment_vals)

        statement.write({'attachment_ids': [Command.link(attachment.id)]})

        self.assertRecordValues(statement.attachment_ids, [
            {'res_id': statement.id, 'res_model': 'account.bank.statement'},
            {'res_id': statement.id, 'res_model': 'account.bank.statement'},
        ])

    def test_statement_reverse_keeps_partner(self):
        partner = self.env['res.partner'].create({
            'name': 'Test Partner',
        })

        statement_line = self.env['account.bank.statement.line'].create({
            'date': '2019-01-01',
            'payment_ref': 'line_1',
            'partner_id': partner.id,
            'journal_id': self.bank_journal_1.id,
            'amount': 1250.0,
        })
        move = statement_line.move_id

        move_reversal = self.env['account.move.reversal'].with_context(active_model="account.move", active_ids=move.ids).create({
            'date': fields.Date.from_string('2021-02-01'),
            'journal_id': self.bank_journal_1.id,
        })
        reversal = move_reversal.reverse_moves()
        reversed_move = self.env['account.move'].browse(reversal['res_id'])

        self.assertEqual(reversed_move.partner_id, partner)