File: test_purchase_invoice.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 (1198 lines) | stat: -rw-r--r-- 57,854 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
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import timedelta

from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.exceptions import UserError
from odoo.tests import Form, tagged
from odoo import Command, fields


class TestPurchaseToInvoiceCommon(AccountTestInvoicingCommon):

    @classmethod
    def setUpClass(cls):
        super(TestPurchaseToInvoiceCommon, cls).setUpClass()
        cls.other_currency = cls.setup_other_currency('EUR')
        uom_unit = cls.env.ref('uom.product_uom_unit')
        uom_hour = cls.env.ref('uom.product_uom_hour')
        cls.product_order = cls.env['product.product'].create({
            'name': "Zed+ Antivirus",
            'standard_price': 235.0,
            'list_price': 280.0,
            'type': 'consu',
            'uom_id': uom_unit.id,
            'uom_po_id': uom_unit.id,
            'purchase_method': 'purchase',
            'default_code': 'PROD_ORDER',
            'taxes_id': False,
        })
        cls.product_order_other_price = cls.env['product.product'].create({
            'name': "Zed+ Antivirus",
            'standard_price': 240.0,
            'list_price': 290.0,
            'type': 'consu',
            'uom_id': uom_unit.id,
            'uom_po_id': uom_unit.id,
            'purchase_method': 'purchase',
            'default_code': 'PROD_ORDER',
            'taxes_id': False,
        })
        cls.product_order_var_name = cls.env['product.product'].create({
            'name': "Zed+ Antivirus Var Name",
            'standard_price': 235.0,
            'list_price': 280.0,
            'type': 'consu',
            'uom_id': uom_unit.id,
            'uom_po_id': uom_unit.id,
            'purchase_method': 'purchase',
            'default_code': 'PROD_ORDER_VAR_NAME',
            'taxes_id': False,
        })
        cls.service_deliver = cls.env['product.product'].create({
            'name': "Cost-plus Contract",
            'standard_price': 200.0,
            'list_price': 180.0,
            'type': 'service',
            'uom_id': uom_unit.id,
            'uom_po_id': uom_unit.id,
            'purchase_method': 'receive',
            'default_code': 'SERV_DEL',
            'taxes_id': False,
        })
        cls.service_order = cls.env['product.product'].create({
            'name': "Prepaid Consulting",
            'standard_price': 40.0,
            'list_price': 90.0,
            'type': 'service',
            'uom_id': uom_hour.id,
            'uom_po_id': uom_hour.id,
            'purchase_method': 'purchase',
            'default_code': 'PRE-PAID',
            'taxes_id': False,
        })
        cls.product_deliver = cls.env['product.product'].create({
            'name': "Switch, 24 ports",
            'standard_price': 55.0,
            'list_price': 70.0,
            'type': 'consu',
            'uom_id': uom_unit.id,
            'uom_po_id': uom_unit.id,
            'purchase_method': 'receive',
            'default_code': 'PROD_DEL',
            'taxes_id': False,
        })

    @classmethod
    def init_purchase(cls, partner=None, confirm=False, products=None, taxes=None, company=False):
        date_planned = fields.Datetime.now() - timedelta(days=1)
        po_form = Form(cls.env['purchase.order'] \
                    .with_company(company or cls.env.company) \
                    .with_context(tracking_disable=True))
        po_form.partner_id = partner or cls.partner_a
        po_form.partner_ref = 'my_match_reference'

        for product in (products or []):
            with po_form.order_line.new() as line_form:
                line_form.product_id = product
                line_form.price_unit = product.list_price
                line_form.product_qty = 1
                line_form.product_uom = product.uom_id
                line_form.date_planned = date_planned
                if taxes:
                    line_form.tax_ids.clear()
                    for tax in taxes:
                        line_form.tax_ids.add(tax)

        rslt = po_form.save()

        if confirm:
            rslt.button_confirm()

        return rslt


@tagged('post_install', '-at_install')
class TestPurchaseToInvoice(TestPurchaseToInvoiceCommon):

    def test_vendor_bill_delivered(self):
        """Test if a order of product invoiced by delivered quantity can be
        correctly invoiced."""
        purchase_order = self.env['purchase.order'].with_context(tracking_disable=True).create({
            'partner_id': self.partner_a.id,
        })
        PurchaseOrderLine = self.env['purchase.order.line'].with_context(tracking_disable=True)
        pol_prod_deliver = PurchaseOrderLine.create({
            'name': self.product_deliver.name,
            'product_id': self.product_deliver.id,
            'product_qty': 10.0,
            'product_uom': self.product_deliver.uom_id.id,
            'price_unit': self.product_deliver.list_price,
            'order_id': purchase_order.id,
            'taxes_id': False,
        })
        pol_serv_deliver = PurchaseOrderLine.create({
            'name': self.service_deliver.name,
            'product_id': self.service_deliver.id,
            'product_qty': 10.0,
            'product_uom': self.service_deliver.uom_id.id,
            'price_unit': self.service_deliver.list_price,
            'order_id': purchase_order.id,
            'taxes_id': False,
        })
        purchase_order.button_confirm()

        self.assertEqual(purchase_order.invoice_status, "no")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 0.0)
            self.assertEqual(line.qty_invoiced, 0.0)

        purchase_order.order_line.qty_received = 5
        self.assertEqual(purchase_order.invoice_status, "to invoice")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 5)
            self.assertEqual(line.qty_invoiced, 0.0)

        purchase_order.action_create_invoice()
        self.assertEqual(purchase_order.invoice_status, "invoiced")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 0.0)
            self.assertEqual(line.qty_invoiced, 5)

    def test_vendor_bill_ordered(self):
        """Test if a order of product invoiced by ordered quantity can be
        correctly invoiced."""
        purchase_order = self.env['purchase.order'].with_context(tracking_disable=True).create({
            'partner_id': self.partner_a.id,
        })
        PurchaseOrderLine = self.env['purchase.order.line'].with_context(tracking_disable=True)
        pol_prod_order = PurchaseOrderLine.create({
            'name': self.product_order.name,
            'product_id': self.product_order.id,
            'product_qty': 10.0,
            'product_uom': self.product_order.uom_id.id,
            'price_unit': self.product_order.list_price,
            'order_id': purchase_order.id,
            'taxes_id': False,
        })
        pol_serv_order = PurchaseOrderLine.create({
            'name': self.service_order.name,
            'product_id': self.service_order.id,
            'product_qty': 10.0,
            'product_uom': self.service_order.uom_id.id,
            'price_unit': self.service_order.list_price,
            'order_id': purchase_order.id,
            'taxes_id': False,
        })
        purchase_order.button_confirm()

        self.assertEqual(purchase_order.invoice_status, "to invoice")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 10)
            self.assertEqual(line.qty_invoiced, 0.0)

        purchase_order.order_line.qty_received = 5
        self.assertEqual(purchase_order.invoice_status, "to invoice")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 10)
            self.assertEqual(line.qty_invoiced, 0.0)

        purchase_order.action_create_invoice()
        self.assertEqual(purchase_order.invoice_status, "invoiced")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 0.0)
            self.assertEqual(line.qty_invoiced, 10)

    def test_vendor_bill_delivered_return(self):
        """Test when return product, a order of product invoiced by delivered
        quantity can be correctly invoiced."""
        purchase_order = self.env['purchase.order'].with_context(tracking_disable=True).create({
            'partner_id': self.partner_a.id,
        })
        PurchaseOrderLine = self.env['purchase.order.line'].with_context(tracking_disable=True)
        pol_prod_deliver = PurchaseOrderLine.create({
            'name': self.product_deliver.name,
            'product_id': self.product_deliver.id,
            'product_qty': 10.0,
            'product_uom': self.product_deliver.uom_id.id,
            'price_unit': self.product_deliver.list_price,
            'order_id': purchase_order.id,
            'taxes_id': False,
        })
        pol_serv_deliver = PurchaseOrderLine.create({
            'name': self.service_deliver.name,
            'product_id': self.service_deliver.id,
            'product_qty': 10.0,
            'product_uom': self.service_deliver.uom_id.id,
            'price_unit': self.service_deliver.list_price,
            'order_id': purchase_order.id,
            'taxes_id': False,
        })
        purchase_order.button_confirm()

        purchase_order.order_line.qty_received = 10
        purchase_order.action_create_invoice()
        self.assertEqual(purchase_order.invoice_status, "invoiced")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 0.0)
            self.assertEqual(line.qty_invoiced, 10)

        purchase_order.order_line.qty_received = 5
        self.assertEqual(purchase_order.invoice_status, "to invoice")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, -5)
            self.assertEqual(line.qty_invoiced, 10)
        purchase_order.action_create_invoice()
        self.assertEqual(purchase_order.invoice_status, "invoiced")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 0.0)
            self.assertEqual(line.qty_invoiced, 5)

    def test_vendor_bill_ordered_return(self):
        """Test when return product, a order of product invoiced by ordered
        quantity can be correctly invoiced."""
        purchase_order = self.env['purchase.order'].with_context(tracking_disable=True).create({
            'partner_id': self.partner_a.id,
        })
        PurchaseOrderLine = self.env['purchase.order.line'].with_context(tracking_disable=True)
        pol_prod_order = PurchaseOrderLine.create({
            'name': self.product_order.name,
            'product_id': self.product_order.id,
            'product_qty': 10.0,
            'product_uom': self.product_order.uom_id.id,
            'price_unit': self.product_order.list_price,
            'order_id': purchase_order.id,
            'taxes_id': False,
        })
        pol_serv_order = PurchaseOrderLine.create({
            'name': self.service_order.name,
            'product_id': self.service_order.id,
            'product_qty': 10.0,
            'product_uom': self.service_order.uom_id.id,
            'price_unit': self.service_order.list_price,
            'order_id': purchase_order.id,
            'taxes_id': False,
        })
        purchase_order.button_confirm()

        purchase_order.order_line.qty_received = 10
        purchase_order.action_create_invoice()
        self.assertEqual(purchase_order.invoice_status, "invoiced")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 0.0)
            self.assertEqual(line.qty_invoiced, 10)

        purchase_order.order_line.qty_received = 5
        self.assertEqual(purchase_order.invoice_status, "invoiced")
        for line in purchase_order.order_line:
            self.assertEqual(line.qty_to_invoice, 0.0)
            self.assertEqual(line.qty_invoiced, 10)

    def test_vendor_severals_bills_and_multicurrency(self):
        """
        This test ensures that, when adding several PO to a bill, if they are expressed with different
        currency, the amount of each AML is converted to the bill's currency
        """
        PurchaseOrderLine = self.env['purchase.order.line']
        PurchaseBillUnion = self.env['purchase.bill.union']
        ResCurrencyRate = self.env['res.currency.rate']
        usd = self.env.ref('base.USD')
        eur = self.env.ref('base.EUR')
        purchase_orders = []

        ResCurrencyRate.create({'currency_id': usd.id, 'rate': 1})
        ResCurrencyRate.create({'currency_id': eur.id, 'rate': 2})

        for currency in [usd, eur]:
            po = self.env['purchase.order'].with_context(tracking_disable=True).create({
                'partner_id': self.partner_a.id,
                'currency_id': currency.id,
            })
            pol_prod_order = PurchaseOrderLine.create({
                'name': self.product_order.name,
                'product_id': self.product_order.id,
                'product_qty': 1,
                'product_uom': self.product_order.uom_id.id,
                'price_unit': 1000,
                'order_id': po.id,
                'taxes_id': False,
            })
            po.button_confirm()
            pol_prod_order.write({'qty_received': 1})
            purchase_orders.append(po)

        move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
        move_form.purchase_vendor_bill_id = PurchaseBillUnion.browse(-purchase_orders[0].id)
        move_form.purchase_vendor_bill_id = PurchaseBillUnion.browse(-purchase_orders[1].id)
        move = move_form.save()

        self.assertInvoiceValues(move, [
            {
                'display_type': 'product',
                'amount_currency': 500,
                'balance': 500,
            }, {
                'display_type': 'product',
                'amount_currency': 1000,
                'balance': 1000,
            }, {
                'display_type': 'payment_term',
                'amount_currency': -1500,
                'balance': -1500,
            },
        ], {
            'amount_total': 1500,
            'currency_id': usd.id,
        })

    def test_product_price_decimal_accuracy(self):
        self.env.ref('product.decimal_price').digits = 3
        self.env.company.currency_id.rounding = 0.01

        po = self.env['purchase.order'].with_context(tracking_disable=True).create({
            'partner_id': self.partner_a.id,
            'order_line': [(0, 0, {
                'name': self.product_a.name,
                'product_id': self.product_a.id,
                'product_qty': 12,
                'product_uom': self.product_a.uom_id.id,
                'price_unit': 0.001,
                'taxes_id': False,
            })]
        })
        po.button_confirm()
        po.order_line.qty_received = 12

        move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
        move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-po.id)
        move = move_form.save()

        self.assertEqual(move.amount_total, 0.01)

    def test_vendor_bill_analytic_account_model_change(self):
        """ Tests whether, when an analytic account rule is set, and user changes manually the analytic account on
        the po, it is the same that is mentioned in the bill.
        """
        # Required for `analytic.group_analytic_accounting` to be visible in the view
        self.env.user.groups_id += self.env.ref('analytic.group_analytic_accounting')
        analytic_plan = self.env['account.analytic.plan'].create({'name': 'Plan Test'})
        analytic_account_default = self.env['account.analytic.account'].create({'name': 'default', 'plan_id': analytic_plan.id})
        analytic_account_manual = self.env['account.analytic.account'].create({'name': 'manual', 'plan_id': analytic_plan.id})

        self.env['account.analytic.distribution.model'].create({
            'analytic_distribution': {analytic_account_default.id: 100},
            'product_id': self.product_order.id,
        })
        analytic_distribution_manual = {str(analytic_account_manual.id): 100}

        po_form = Form(self.env['purchase.order'].with_context(tracking_disable=True))
        po_form.partner_id = self.partner_a
        with po_form.order_line.new() as po_line_form:
            po_line_form.name = self.product_order.name
            po_line_form.product_id = self.product_order
            po_line_form.product_qty = 1.0
            po_line_form.price_unit = 10
            po_line_form.analytic_distribution = analytic_distribution_manual

        purchase_order = po_form.save()
        purchase_order.button_confirm()
        purchase_order.action_create_invoice()

        aml = self.env['account.move.line'].search([('purchase_line_id', '=', purchase_order.order_line.id)])
        self.assertRecordValues(aml, [{'analytic_distribution': analytic_distribution_manual}])

    def test_purchase_order_analytic_account_product_change(self):
        self.env.user.groups_id += self.env.ref('account.group_account_readonly')
        self.env.user.groups_id += self.env.ref('analytic.group_analytic_accounting')

        analytic_plan = self.env['account.analytic.plan'].create({'name': 'Plan Test'})
        analytic_account_super = self.env['account.analytic.account'].create({'name': 'Super Account', 'plan_id': analytic_plan.id})
        analytic_account_great = self.env['account.analytic.account'].create({'name': 'Great Account', 'plan_id': analytic_plan.id})

        super_product = self.env['product.product'].create({'name': 'Super Product'})
        great_product = self.env['product.product'].create({'name': 'Great Product'})
        self.env['account.analytic.distribution.model'].create([
            {
                'analytic_distribution': {analytic_account_super.id: 100},
                'product_id': super_product.id,
            },
            {
                'analytic_distribution': {analytic_account_great.id: 100},
                'product_id': great_product.id,
            },
        ])
        po_form = Form(self.env['purchase.order'].with_context(tracking_disable=True))
        partner = self.env['res.partner'].create({'name': 'Test Partner'})
        po_form.partner_id = partner
        with po_form.order_line.new() as po_line_form:
            po_line_form.name = super_product.name
            po_line_form.product_id = super_product
        purchase_order = po_form.save()
        purchase_order_line = purchase_order.order_line

        self.assertEqual(purchase_order_line.analytic_distribution, {str(analytic_account_super.id): 100}, "The analytic account should be set to 'Super Account'")
        purchase_order_line.write({'product_id': great_product.id})
        self.assertEqual(purchase_order_line.analytic_distribution, {str(analytic_account_great.id): 100}, "The analytic account should be set to 'Great Account'")

        po_no_analytic_distribution = self.env['purchase.order'].create({
            'partner_id': partner.id,
        })
        pol_no_analytic_distribution = self.env['purchase.order.line'].create({
            'name': super_product.name,
            'product_id': super_product.id,
            'order_id': po_no_analytic_distribution.id,
            'analytic_distribution': False,
        })
        po_no_analytic_distribution.button_confirm()
        self.assertFalse(pol_no_analytic_distribution.analytic_distribution, "The compute should not overwrite what the user has set.")

    def test_purchase_order_to_invoice_analytic_rule_with_account_prefix(self):
        """
        Test whether, when an analytic plan is set within the scope (applicability) of purchase
        and with an account prefix set in the distribution model,
        the default analytic account is correctly set during the conversion from po to invoice
        """
        self.env.user.groups_id += self.env.ref('analytic.group_analytic_accounting')
        analytic_plan_default = self.env['account.analytic.plan'].create({
            'name': 'default',
            'applicability_ids': [Command.create({
                'business_domain': 'bill',
                'applicability': 'optional',
            })]
        })
        analytic_account_default = self.env['account.analytic.account'].create({'name': 'default', 'plan_id': analytic_plan_default.id})

        analytic_distribution_model = self.env['account.analytic.distribution.model'].create({
            'account_prefix': '600',
            'analytic_distribution': {analytic_account_default.id: 100},
            'product_id': self.product_a.id,
        })

        po = self.env['purchase.order'].create({'partner_id': self.partner_a.id})
        self.env['purchase.order.line'].create({
            'order_id': po.id,
            'name': 'test',
            'product_id': self.product_a.id
        })
        self.assertFalse(po.order_line.analytic_distribution, "There should be no analytic set.")
        po.button_confirm()
        po.order_line.qty_received = 1
        po.action_create_invoice()
        self.assertRecordValues(po.invoice_ids.invoice_line_ids,
                                [{'analytic_distribution': analytic_distribution_model.analytic_distribution}])

    def test_sequence_invoice_lines_from_multiple_purchases(self):
        """Test if the invoice lines are sequenced by purchase order when creating an invoice
           from multiple selected po's"""
        purchase_orders = self.env['purchase.order']

        for _ in range(3):
            pol_vals = [
                (0, 0, {
                    'name': self.product_order.name,
                    'product_id': self.product_order.id,
                    'product_qty': 10.0,
                    'product_uom': self.product_order.uom_id.id,
                    'price_unit': self.product_order.list_price,
                    'taxes_id': False,
                    'sequence': sequence_number,
                }) for sequence_number in range(10, 13)]
            purchase_order = self.env['purchase.order'].with_context(tracking_disable=True).create({
                'partner_id': self.partner_a.id,
                'order_line': pol_vals,
            })
            purchase_order.button_confirm()
            purchase_orders |= purchase_order

        action = purchase_orders.action_create_invoice()
        invoice = self.env['account.move'].browse(action['res_id'])

        expected_purchase = [
            purchase_orders[0], purchase_orders[0], purchase_orders[0],
            purchase_orders[1], purchase_orders[1], purchase_orders[1],
            purchase_orders[2], purchase_orders[2], purchase_orders[2],
        ]
        for line in invoice.invoice_line_ids.sorted('sequence'):
            self.assertEqual(line.purchase_order_id, expected_purchase.pop(0))

    def test_sequence_autocomplete_invoice(self):
        """Test if the invoice lines are sequenced by purchase order when using the autocomplete
           feature on a bill to add lines from po's"""
        purchase_orders = self.env['purchase.order']

        for _ in range(3):
            pol_vals = [
                (0, 0, {
                    'name': self.product_order.name,
                    'product_id': self.product_order.id,
                    'product_qty': 10.0,
                    'product_uom': self.product_order.uom_id.id,
                    'price_unit': self.product_order.list_price,
                    'taxes_id': False,
                    'sequence': sequence_number,
                }) for sequence_number in range(10, 13)]
            purchase_order = self.env['purchase.order'].with_context(tracking_disable=True).create({
                'partner_id': self.partner_a.id,
                'order_line': pol_vals,
            })
            purchase_order.button_confirm()
            purchase_orders |= purchase_order

        move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
        PurchaseBillUnion = self.env['purchase.bill.union']
        move_form.purchase_vendor_bill_id = PurchaseBillUnion.browse(-purchase_orders[0].id)
        move_form.purchase_vendor_bill_id = PurchaseBillUnion.browse(-purchase_orders[1].id)
        move_form.purchase_vendor_bill_id = PurchaseBillUnion.browse(-purchase_orders[2].id)
        invoice = move_form.save()

        expected_purchase = [
            purchase_orders[0], purchase_orders[0], purchase_orders[0],
            purchase_orders[1], purchase_orders[1], purchase_orders[1],
            purchase_orders[2], purchase_orders[2], purchase_orders[2],
        ]
        for line in invoice.invoice_line_ids.sorted('sequence'):
            self.assertEqual(line.purchase_order_id, expected_purchase.pop(0))

    def test_partial_billing_interaction_with_invoicing_switch_threshold(self):
        """ Let's say you create a partial bill 'B' for a given PO. Now if you change the
            'Invoicing Switch Threshold' such that the bill date of 'B' is before the new threshold,
            the PO should still take bill 'B' into account.
        """
        if not self.env['ir.module.module'].search([('name', '=', 'account_accountant'), ('state', '=', 'installed')]):
            self.skipTest("This test requires the installation of the account_account module")

        purchase_order = self.env['purchase.order'].with_context(tracking_disable=True).create({
            'partner_id': self.partner_a.id,
            'order_line': [
                Command.create({
                    'name': self.product_deliver.name,
                    'product_id': self.product_deliver.id,
                    'product_qty': 20.0,
                    'product_uom': self.product_deliver.uom_id.id,
                    'price_unit': self.product_deliver.list_price,
                    'taxes_id': False,
                }),
            ],
        })
        line = purchase_order.order_line[0]

        purchase_order.button_confirm()
        line.qty_received = 10
        purchase_order.action_create_invoice()

        invoice = purchase_order.invoice_ids
        invoice.invoice_date = invoice.date
        invoice.action_post()

        self.assertEqual(line.qty_invoiced, 10)

        self.env['res.config.settings'].create({
            'invoicing_switch_threshold': fields.Date.add(invoice.invoice_date, days=30),
        }).execute()

        invoice.invalidate_model(fnames=['payment_state'])

        self.assertEqual(line.qty_invoiced, 10)
        line.qty_received = 15
        self.assertEqual(line.qty_invoiced, 10)

    def test_on_change_quantity_price_unit(self):
        """ When a user changes the quantity of a product in a purchase order it
        should only update the unit price if PO line has no invoice line. """

        supplierinfo_vals = {
            'partner_id': self.partner_a.id,
            'price': 10.0,
            'min_qty': 1,
            "product_id": self.product_order.id,
            "product_tmpl_id": self.product_order.product_tmpl_id.id,
        }

        supplierinfo = self.env["product.supplierinfo"].create(supplierinfo_vals)
        po_form = Form(self.env['purchase.order'])
        po_form.partner_id = self.partner_a
        with po_form.order_line.new() as po_line_form:
            po_line_form.product_id = self.product_order
            po_line_form.product_qty = 1
        po = po_form.save()
        po_line = po.order_line[0]

        self.assertEqual(10.0, po_line.price_unit, "Unit price should be set to 10.0 for 1 quantity")

        # Ensure price unit is updated when changing quantity on a un-confirmed PO
        supplierinfo.write({'min_qty': 2, 'price': 20.0})
        po_line.write({'product_qty': 2})
        self.assertEqual(20.0, po_line.price_unit, "Unit price should be set to 20.0 for 2 quantity")

        po.button_confirm()

        # Ensure price unit is updated when changing quantity on a confirmed PO
        supplierinfo.write({'min_qty': 3, 'price': 30.0})
        po_line.write({'product_qty': 3})
        self.assertEqual(30.0, po_line.price_unit, "Unit price should be set to 30.0 for 3 quantity")

        po.action_create_invoice()

        # Ensure price unit is NOT updated when changing quantity on PO confirmed and line linked to an invoice line
        supplierinfo.write({'min_qty': 4, 'price': 40.0})
        po_line.write({'product_qty': 4})
        self.assertEqual(30.0, po_line.price_unit, "Unit price should be set to 30.0 for 3 quantity")

        with po_form.order_line.new() as po_line_form:
            po_line_form.product_id = self.product_order
            po_line_form.product_qty = 1
        po = po_form.save()
        po_line = po.order_line[1]

        self.assertEqual(235.0, po_line.price_unit, "Unit price should be reset to 235.0 since the supplier supplies minimum of 4 quantities")

        # Ensure price unit is updated when changing quantity on PO confirmed and line NOT linked to an invoice line
        po_line.write({'product_qty': 4})
        self.assertEqual(40.0, po_line.price_unit, "Unit price should be set to 40.0 for 4 quantity")

    def test_supplier_discounted_price(self):
        """ Check the lower price (discount included) is used.
        """
        uom_dozen = self.env.ref('uom.product_uom_dozen')
        supplierinfo_common_vals = {
            'partner_id': self.partner_a.id,
            'product_id': self.product_order.id,
            'product_tmpl_id': self.product_order.product_tmpl_id.id,
        }
        supplierinfo = self.env['product.supplierinfo'].create([
            {
                **supplierinfo_common_vals,
                'price': 100.0,
                'discount': 0,  # Real price by unit: 100.00
            }, {
                **supplierinfo_common_vals,
                'price': 120.0,
                'discount': 20,  # Real price by unit: 96.00
            }, {
                **supplierinfo_common_vals,
                'min_qty': 10,
                'price': 140.0,
                'discount': 50,  # Real price by unit: 70.00
            },
        ])
        self.assertRecordValues(supplierinfo, [
            {'price_discounted': 100},
            {'price_discounted': 96},
            {'price_discounted': 70},
        ])

        po_form = Form(self.env['purchase.order'])
        po_form.partner_id = self.partner_a
        with po_form.order_line.new() as po_line_form:
            po_line_form.product_id = self.product_order
            po_line_form.product_qty = 1
        po = po_form.save()
        po_line = po.order_line[0]
        self.assertEqual(120.0, po_line.price_unit)
        self.assertEqual(20, po_line.discount)
        self.assertEqual(96.0, po_line.price_unit_discounted)
        self.assertEqual(96.0, po_line.price_subtotal)

        # Increase the PO line quantity: it should take another price if min. qty. is reached.
        po_form = Form(po)
        with po_form.order_line.edit(0) as po_line_form:
            po_line_form.product_uom = uom_dozen
            po_line_form.product_qty = 3
        po = po_form.save()
        po_line = po.order_line[0]

        self.assertEqual(1680.0, po_line.price_unit, "140.0 * 12 = 1680.0")
        self.assertEqual(50, po_line.discount)
        self.assertEqual(840.0, po_line.price_unit_discounted, "1680.0 * 0.5 = 840.0")
        self.assertEqual(2520.0, po_line.price_subtotal, "840.0 * 3 = 2520.0")

    def test_invoice_line_name_has_product_name(self):
        """ Testing that when invoicing a sales order, the invoice line name ALWAYS contains the product name. """
        # Create a purchase order with different descriptions
        po = self.env['purchase.order'].with_context(tracking_disable=True).create({
            'partner_id': self.partner_a.id,
        })
        PurchaseOrderLine = self.env['purchase.order.line'].with_context(tracking_disable=True)
        pol_prod_no_redundancy = PurchaseOrderLine.create({
            'name': "just a description",
            'product_id': self.product_deliver.id,
            'product_qty': 1,
            'product_uom': self.product_deliver.uom_id.id,
            'price_unit': self.product_deliver.list_price,
            'order_id': po.id,
            'taxes_id': False,
        })
        pol_prod_same = PurchaseOrderLine.create({
            'name': self.product_deliver.display_name,
            'product_id': self.product_deliver.id,
            'product_qty': 1,
            'product_uom': self.product_deliver.uom_id.id,
            'price_unit': self.product_deliver.list_price,
            'order_id': po.id,
            'taxes_id': False,
        })
        pol_prod_product_in_name = PurchaseOrderLine.create({
            'name': f"{self.product_deliver.display_name} with more description",
            'product_id': self.product_deliver.id,
            'product_qty': 1,
            'product_uom': self.product_deliver.uom_id.id,
            'price_unit': self.product_deliver.list_price,
            'order_id': po.id,
            'taxes_id': False,
        })
        pol_prod_name_in_product = PurchaseOrderLine.create({
            'name': "Switch",
            'product_id': self.product_deliver.id,
            'product_qty': 1,
            'product_uom': self.product_deliver.uom_id.id,
            'price_unit': self.product_deliver.list_price,
            'order_id': po.id,
            'taxes_id': False,
        })

        # Invoice the purchase order
        po.button_confirm()
        po.order_line.qty_received = 4
        po.action_create_invoice()
        inv = po.invoice_ids

        # Check the invoice line names
        self.assertEqual(inv.invoice_line_ids[0].name, f"{pol_prod_no_redundancy.product_id.display_name} {pol_prod_no_redundancy.name}", "When the description doesn't contain the product name, it should be added to the invoice line name")
        self.assertEqual(inv.invoice_line_ids[1].name, f"{pol_prod_same.name}", "When the description is the product name, the invoice line name should only be the description")
        self.assertEqual(inv.invoice_line_ids[2].name, f"{pol_prod_product_in_name.name}", "When description contains the product name, the invoice line name should only be the description")
        self.assertEqual(inv.invoice_line_ids[3].name, f"{pol_prod_name_in_product.product_id.display_name} {pol_prod_name_in_product.name}", "When the product name contains the description, the invoice line name should be the product name and the description")


@tagged('post_install', '-at_install')
class TestInvoicePurchaseMatch(TestPurchaseToInvoiceCommon):

    def test_total_match_via_partner(self):
        po = self.init_purchase(confirm=True, partner=self.partner_a, products=[self.product_order])
        invoice = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order])

        invoice._find_and_set_purchase_orders(
            [], invoice.partner_id.id, invoice.amount_total)

        self.assertTrue(invoice.id in po.invoice_ids.ids)
        self.assertEqual(invoice.amount_total, po.amount_total)

    def test_total_match_via_po_reference(self):
        po = self.init_purchase(confirm=True, products=[self.product_order])
        invoice = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order])

        invoice._find_and_set_purchase_orders(
            ['my_match_reference'], invoice.partner_id.id, invoice.amount_total)

        self.assertTrue(invoice.id in po.invoice_ids.ids)
        self.assertEqual(invoice.amount_total, po.amount_total)

    def test_subset_total_match_from_ocr(self):
        po = self.init_purchase(confirm=True, products=[self.product_order, self.service_order])
        invoice = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order])

        invoice._find_and_set_purchase_orders(
            ['my_match_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=True)
        additional_unmatch_po_line = po.order_line.filtered(lambda l: l.product_id == self.service_order)

        self.assertTrue(invoice.id in po.invoice_ids.ids)
        self.assertTrue(additional_unmatch_po_line.id in invoice.line_ids.purchase_line_id.ids)
        self.assertTrue(invoice.line_ids.filtered(lambda l: l.purchase_line_id == additional_unmatch_po_line).quantity == 0)

    def test_subset_match_from_edi_full(self):
        """An invoice totally matches a purchase order line by line
        """
        po = self.init_purchase(confirm=True, products=[self.product_order, self.service_order])
        invoice = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order, self.service_order])

        invoice._find_and_set_purchase_orders(
            ['my_match_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=False)

        self.assertTrue(invoice.id in po.invoice_ids.ids)
        invoice_lines = invoice.line_ids.filtered(lambda l: l.price_unit)
        self.assertEqual(len(invoice_lines), 2)
        for line in invoice_lines:
            self.assertTrue(line.purchase_line_id in po.order_line)
        self.assertEqual(invoice.amount_total, po.amount_total)

    def test_subset_match_from_edi_partial_po(self):
        """A line of the invoice totally matches a 1-line purchase order
        """
        po = self.init_purchase(confirm=True, products=[self.product_order])
        invoice = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order, self.service_order])

        invoice._find_and_set_purchase_orders(
            ['my_match_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=False)

        self.assertTrue(invoice.id in po.invoice_ids.ids)
        invoice_lines = invoice.line_ids.filtered(lambda l: l.price_unit)
        self.assertEqual(len(invoice_lines), 2)
        for line in po.order_line:
            self.assertTrue(line in invoice_lines.purchase_line_id)

    def test_subset_match_from_edi_partial_inv(self):
        """An invoice totally matches some purchase order line
        """
        po = self.init_purchase(confirm=True, products=[self.product_order, self.service_order])
        invoice = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order])

        invoice._find_and_set_purchase_orders(
            ['my_match_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=False)

        self.assertTrue(invoice.id in po.invoice_ids.ids)
        invoice_lines = invoice.line_ids.filtered(lambda l: l.price_unit)
        self.assertEqual(len(invoice_lines), 1)
        for line in invoice_lines:
            self.assertTrue(line.purchase_line_id in po.order_line)

    def test_subset_match_from_edi_same_unit_price(self):
        """An invoice matches some purchase order line by unit price
        """
        po = self.init_purchase(confirm=True, products=[self.product_order_var_name, self.product_order])
        invoice = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order])

        invoice._find_and_set_purchase_orders(
            ['my_match_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=False)

        self.assertTrue(invoice.id in po.invoice_ids.ids)
        invoice_lines = invoice.line_ids.filtered(lambda l: l.price_unit)
        self.assertEqual(len(invoice_lines), 1)
        for line in invoice_lines:
            self.assertTrue(line.purchase_line_id in po.order_line)

    def test_subset_match_from_edi_and_diff_unit_price(self):
        """An invoice matches some purchase order line but not another one because of a unit price difference
        """
        po = self.init_purchase(confirm=True, products=[self.product_order_var_name, self.product_order])
        invoice = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order, self.product_order_other_price])

        invoice._find_and_set_purchase_orders(
            ['my_match_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=False)

        self.assertTrue(invoice.id in po.invoice_ids.ids)
        invoice_lines = invoice.line_ids.filtered(lambda l: l.price_unit)
        self.assertEqual(len(invoice_lines), 2)
        for line in invoice_lines:
            if (line.product_id == self.product_order):
                self.assertTrue(line.purchase_line_id in po.order_line)
            else:
                self.assertFalse(line.purchase_line_id in po.order_line)


    def test_po_match_from_ocr(self):
        po = self.init_purchase(confirm=True, products=[self.product_order, self.service_order])
        invoice = self.init_invoice('in_invoice', products=[self.product_a])

        invoice._find_and_set_purchase_orders(
            ['my_match_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=True)

        self.assertTrue(invoice.id in po.invoice_ids.ids)

    def test_no_match_same_reference(self):
        po = self.init_purchase(confirm=True, products=[self.product_order, self.service_order])
        invoice = self.init_invoice('in_invoice', products=[self.product_a])

        invoice._find_and_set_purchase_orders(
            ['my_match_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=False)

        self.assertTrue(invoice.id not in po.invoice_ids.ids)

    def test_no_match(self):
        po = self.init_purchase(confirm=True, products=[self.product_order, self.service_order])
        invoice = self.init_invoice('in_invoice', products=[self.product_a])

        invoice._find_and_set_purchase_orders(
            ['other_reference'], invoice.partner_id.id, invoice.amount_total, from_ocr=False)

        self.assertTrue(invoice.id not in po.invoice_ids.ids)

    def test_manual_matching(self):
        po = self.init_purchase(confirm=True, products=[self.product_order])
        bill = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order])

        self.env['account.move.line'].flush_model()  # necessary to get the bill lines
        match_lines = self.env['purchase.bill.line.match'].search([('partner_id', '=', self.partner_a.id)])

        expected_ids = po.order_line.ids + [-lid for lid in bill.invoice_line_ids.ids]
        self.assertListEqual(match_lines.ids, expected_ids)

        match_lines.action_match_lines()
        self.assertEqual(bill.invoice_line_ids.purchase_line_id, po.order_line)
        self.assertEqual(po.order_line.qty_invoiced, bill.invoice_line_ids.quantity)

    def test_manual_matching_restrict_no_pol(self):
        """ raises when there's no POL found """
        with self.assertRaisesRegex(UserError, "must select at least one Purchase Order line"):
            match_lines = self.env['purchase.bill.line.match'].search([('partner_id', '=', self.partner_a.id)])
            match_lines.action_match_lines()

    def test_manual_matching_restrict_multi_bill(self):
        """ raises when multiple bill selected """
        with self.assertRaisesRegex(UserError, "can't select lines from multiple Vendor Bill"):
            self.init_purchase(confirm=True, products=[self.product_order])
            self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order])
            self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order])
            match_lines = self.env['purchase.bill.line.match'].search([('partner_id', '=', self.partner_a.id)])
            match_lines.action_match_lines()

    def test_manual_matching_create_bill(self):
        """ Selecting POL without AML will create bill with the selected POL as the lines """
        prev_moves = self.env['account.move'].search([])
        self.init_purchase(confirm=True, products=[self.product_order, self.product_order_var_name])
        self.env['purchase.order.line'].flush_model()

        match_lines = self.env['purchase.bill.line.match'].search([('partner_id', '=', self.partner_a.id)])
        match_lines.action_match_lines()

        new_move = self.env['account.move'].search([]) - prev_moves
        self.assertEqual(new_move.partner_id, self.partner_a)
        self.assertRecordValues(new_move.invoice_line_ids, [
            {'product_id': self.product_order.id},
            {'product_id': self.product_order_var_name.id},
        ])

    def test_manual_matching_multi_po(self):
        """ All POL are matched/added into the bill, and all unmatched AML are discarded """
        po_1 = self.init_purchase(confirm=True, products=[self.product_order, self.product_order_var_name])
        po_2 = self.init_purchase(confirm=True, products=[self.service_deliver])
        po_3 = self.init_purchase(confirm=True, products=[self.service_order])
        bill = self.init_invoice(move_type='in_invoice', partner=self.partner_a,
                                 products=[self.product_order, self.product_order_other_price])

        self.env['account.move.line'].flush_model()
        match_lines = self.env['purchase.bill.line.match'].search([('partner_id', '=', self.partner_a.id)])
        match_lines.action_match_lines()

        self.env.flush_all()
        self.assertEqual(bill.invoice_line_ids.purchase_line_id, (po_1 + po_2 + po_3).order_line)
        self.assertRecordValues(bill.invoice_line_ids, [
            {'product_id': self.product_order.id},
            {'product_id': self.service_order.id},
            {'product_id': self.product_order_var_name.id},
            {'product_id': self.service_deliver.id},
        ])

    def test_add_bill_to_po(self):
        bill = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order_var_name, self.service_deliver], post=True)
        self.env['account.move.line'].flush_model()

        match_lines = self.env['purchase.bill.line.match'].search([('partner_id', '=', self.partner_a.id)])

        # Use wizard to create a new PO
        action = match_lines.action_add_to_po()
        wizard = self.env['bill.to.po.wizard'].with_context({**action['context'], 'active_ids': match_lines.ids}).create({})
        self.assertEqual(wizard.partner_id, self.partner_a)
        self.assertFalse(wizard.purchase_order_id)

        action = wizard.action_add_to_po()
        po = self.env['purchase.order'].browse(action['res_id'])
        self.assertEqual(po.partner_id, self.partner_a)
        self.assertTrue(po.order_line.taxes_id)
        self.assertEqual(po.order_line.taxes_id, bill.invoice_line_ids.tax_ids)
        self.assertEqual(po.order_line.product_id, bill.invoice_line_ids.product_id)
        self.assertEqual(po.order_line.product_id, self.product_order_var_name + self.service_deliver)

        bill_2 = self.init_invoice('in_invoice', partner=self.partner_a, products=[self.product_order_other_price], post=False)
        bill_2.invoice_line_ids.write({
            'discount': 2.0,
            'product_uom_id': self.env.ref('uom.product_uom_dozen').id,
        })
        bill_2.action_post()
        self.env['account.move.line'].flush_model()

        match_lines = self.env['purchase.bill.line.match'].search([('partner_id', '=', self.partner_a.id)])

        # Use wizard to add lines to existing PO
        match_lines.action_add_to_po()
        wizard = self.env['bill.to.po.wizard'].with_context({
            'default_purchase_order_id': po.id,
            'active_ids': match_lines.ids
        }).create({})

        action = wizard.action_add_to_po()
        self.assertEqual(action['res_id'], po.id)
        self.assertEqual(len(po.order_line), 3)
        self.assertEqual(po.order_line[-1:].product_id, self.product_order_other_price)
        self.assertListEqual(po.order_line.mapped('price_total'), (bill + bill_2).invoice_line_ids.mapped('price_total'))

    def test_onchange_partner_currency(self):
        """
        Test that the currency of the Bill is correctly set when the partner is changed
        as well as the currency of the Bill lines
        """

        vendor_eur = self.env['res.partner'].create({
            'name': 'Vendor EUR',
            'property_purchase_currency_id': self.env.ref('base.EUR').id,
        })
        vendor_us = self.env['res.partner'].create({
            'name': 'Vendor USD',
            'property_purchase_currency_id': self.env.ref('base.USD').id,
        })
        vendor_no_currency = self.env['res.partner'].create({
            'name': 'Vendor No Currency',
        })

        move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
        move_form.partner_id = vendor_eur
        with move_form.invoice_line_ids.new() as line_form:
            line_form.product_id = self.product_order
            line_form.quantity = 1
        bill = move_form.save()

        self.assertEqual(bill.currency_id, self.env.ref('base.EUR'), "The currency of the Bill should be the same as the currency of the partner")
        self.assertEqual(bill.invoice_line_ids.currency_id, self.env.ref('base.EUR'), "The currency of the Bill lines should be the same as the currency of the partner")

        move_form.partner_id = vendor_us
        bill = move_form.save()

        self.assertEqual(bill.currency_id, self.env.ref('base.USD'), "The currency of the Bill should be the same as the currency of the partner")
        self.assertEqual(bill.invoice_line_ids.currency_id, self.env.ref('base.USD'), "The currency of the Bill lines should be the same as the currency of the partner")

        move_form.partner_id = vendor_no_currency
        bill = move_form.save()

        self.assertEqual(bill.currency_id, self.env.company.currency_id, "The currency of the Bill should be the same as the currency of the company")
        self.assertEqual(bill.invoice_line_ids.currency_id, self.env.company.currency_id, "The currency of the Bill lines should be the same as the currency of the company")

    def test_onchange_partner_no_currency(self):
        """
        Test that the currency of the Bill is correctly set when the partner is changed
        as well as the currency of the Bill lines even if the partner has no property_purchase_currency_id set
        or when and the `default_currency_id` is defined in the context
        """

        vendor_a = self.env['res.partner'].create({
            'name': 'Vendor A with No Currency',
        })
        vendor_b = self.env['res.partner'].create({
            'name': 'Vendor B with No Currency',
        })

        ctx = {'default_move_type': 'in_invoice'}
        move_form = Form(self.env['account.move'].with_context(ctx))
        move_form.partner_id = vendor_a
        move_form.currency_id = self.env.ref('base.EUR')
        with move_form.invoice_line_ids.new() as line_form:
            line_form.product_id = self.product_order
            line_form.quantity = 1
        bill = move_form.save()

        self.assertEqual(bill.currency_id, self.env.ref('base.EUR'), "The currency of the Bill should be the one set on the Bill")
        self.assertEqual(bill.invoice_line_ids.currency_id, self.env.ref('base.EUR'), "The currency of the Bill lines should be the same as the currency of the Bill")

        move_form.partner_id = vendor_b
        bill = move_form.save()

        self.assertEqual(bill.currency_id, self.env.ref('base.EUR'), "The currency of the Bill should be the one set on the Bill")
        self.assertEqual(bill.invoice_line_ids.currency_id, self.env.ref('base.EUR'), "The currency of the Bill lines should be the same as the currency of the Bill")

        ctx['default_currency_id'] = self.other_currency.id
        move_form_currency_in_context = Form(self.env['account.move'].with_context(ctx))
        move_form_currency_in_context.currency_id = self.env.ref('base.EUR')
        with move_form_currency_in_context.invoice_line_ids.new() as line_form:
            line_form.product_id = self.product_order
            line_form.quantity = 1
        move_form_currency_in_context.save()

        move_form_currency_in_context.partner_id = vendor_a
        bill = move_form_currency_in_context.save()

        self.assertEqual(bill.currency_id, self.other_currency, "The currency of the Bill should be the one of the context")
        self.assertEqual(bill.invoice_line_ids.currency_id, self.other_currency, "The currency of the Bill lines should be the same as the currency of the Bill")

    def test_payment_reference_autocomplete_invoice(self):
        """
        Test that the payment_reference field is not replaced when selected a purchase order
        We test the flow for 8 use cases:
        - Purchase order with partner ref:
            - Bill with ref:
                - Bill with payment_reference -> should not be replaced
                - Bill without payment_reference -> should be the po.partner_ref
            - Bill without ref:
                - Bill with payment_reference -> should not be replaced
                - Bill without payment_reference -> should be the po.partner_ref
        - Purchase order without partner ref:
            - Bill with ref
                - Bill with payment_reference -> should not be replaced
                - Bill without payment_reference -> should be the bill ref
            - Bill with ref
                - Bill with payment_reference -> should not be replaced
                - Bill without payment_reference -> should be empty
        """
        purchase_order_w_ref, purchase_order_wo_ref = self.env['purchase.order'].with_context(tracking_disable=True).create([
            {
                'partner_id': self.partner_a.id,
                'partner_ref': partner_ref,
                'order_line': [
                    Command.create({
                        'product_id': self.product_order.id,
                        'product_qty': 1.0,
                        'price_unit': self.product_order.list_price,
                        'taxes_id': False,
                    }),
                ]
            } for partner_ref in ('PO-001', False)
        ])
        (purchase_order_w_ref + purchase_order_wo_ref).button_confirm()

        expected_values_dict = {
            purchase_order_w_ref: {
                'w_bill_ref': {'w_payment_reference': '222', 'wo_payment_reference': purchase_order_w_ref.partner_ref},
                'wo_bill_ref': {'w_payment_reference': '222', 'wo_payment_reference': purchase_order_w_ref.partner_ref},
            },
            purchase_order_wo_ref: {
                'w_bill_ref': {'w_payment_reference': '222', 'wo_payment_reference': '111'},
                'wo_bill_ref': {'w_payment_reference': '222', 'wo_payment_reference': ''},
            }
        }

        for purchase_order, purchase_expected_values in expected_values_dict.items():
            for w_bill_ref, expected_values in purchase_expected_values.items():
                for w_payment_reference, expected_value in expected_values.items():
                    with self.subTest(po_partner_ref=purchase_order.partner_ref, w_bill_ref=w_bill_ref, w_payment_reference=w_payment_reference, expected_value=expected_value):
                        move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
                        move_form.ref = '111' if w_bill_ref == 'w_bill_ref' else ''
                        move_form.payment_reference = '222' if w_payment_reference == 'w_payment_reference' else ''
                        move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-purchase_order.id).exists()
                        payment_reference = move_form._values['payment_reference']
                        self.assertEqual(payment_reference, expected_value, "The payment reference should be %s" % expected_value)

    def test_invoice_user_id_on_bill(self):
        """
        Test that the invoice_user_id field is False when creating a vendor bill from a PO
        or when using Auto-Complete feature of a vendor bill.
        """
        group_purchase_user = self.env.ref('purchase.group_purchase_user')
        group_employee = self.env.ref('base.group_user')
        group_partner_manager = self.env.ref('base.group_partner_manager')
        purchase_user = self.env['res.users'].with_context(no_reset_password=True).create({
            'name': 'Purchase user',
            'login': 'purchaseUser',
            'email': 'pu@odoo.com',
            'groups_id': [Command.set([group_purchase_user.id, group_employee.id, group_partner_manager.id])],
        })
        po1 = self.env['purchase.order'].with_context(tracking_disable=True).create({
            'partner_id': self.partner_a.id,
            'user_id': purchase_user.id,
            'order_line': [
                Command.create({
                    'product_id': self.product_order.id,
                    'product_qty': 1.0,
                    'price_unit': self.product_order.list_price,
                    'taxes_id': False,
                }),
            ]
        })
        po2 = po1.copy()
        po1.button_confirm()
        po2.button_confirm()
        # creating bill from PO
        po1.order_line.qty_received = 1
        po1.action_create_invoice()
        invoice1 = po1.invoice_ids
        self.assertFalse(invoice1.invoice_user_id)
        # creating bill with Auto_complete feature
        move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
        move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-po2.id)
        invoice2 = move_form.save()
        self.assertFalse(invoice2.invoice_user_id)