Debian Codegraph @

addons/purchase_edi_ubl_bis3/tests/test_account_move_import.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 from odoo import fields, Command
3
4 from odoo.addons.account.tests.common import AccountTestInvoicingCommon
5 from odoo.tests import tagged
6 from odoo.tools import file_open
7
8
9 @tagged('post_install', '-at_install')
10 class TestAccountMoveImport(AccountTestInvoicingCommon):
11
12 @classmethod
13 def setUpClass(cls):
14 super().setUpClass()
15
16 cls.partner_open_wood = cls.env['res.partner'].create({
17 'name': 'openWood',
18 'country_id': cls.env.ref('base.be').id,
19 })
20
21 cls.product = cls.env['product.product'].create({
22 'name': 'Test Product',
23 'standard_price': 40.0,
24 })
25
26 cls.purchase_order = cls.env['purchase.order'].create({
27 'partner_id': cls.partner_open_wood.id,
28 'date_order': fields.Date.today(),
29 'name': 'test_purchase_order',
30 'order_line': [Command.create({
31 'product_id': cls.product.id,
32 'name': cls.product.name,
33 'product_qty': 1.0,
34 })],
35 })
36 cls.purchase_order.button_confirm()
37
38 def _create_bill_from_xml(self, xml_file_path):
39 file_path = f"{self.test_module}/tests/test_files/{xml_file_path}"
40 with file_open(file_path, 'rb') as file:
41 xml_attachment = self.env['ir.attachment'].create({
42 'mimetype': 'application/xml',
43 'name': 'test_bill.xml',
44 'raw': file.read(),
45 })
46 return self._import_attachment(xml_attachment)
47
48 def _import_attachment(self, attachment, journal=None):
49 journal = journal or self.company_data["default_journal_purchase"]
50 return self.env['account.journal'] \
51 .with_context(default_journal_id=journal.id) \
52 ._create_document_from_attachment(attachment.id)
53
54 def test_import_purchase_order_reference_from_provided_field(self):
55 """
56 This test will try to match a purchase order when the purchase reference is in the provided field
57 """
58 bill = self._create_bill_from_xml("ubl_bis3_PO.xml")
59 self.assertEqual(bill.invoice_origin, self.purchase_order.name)
60 # Checks if all lines referencing a PO reference the PO we created in setup.
61 # The 'or [False]' makes sure that there's at least one line referencing a PO.
62 self.assertTrue(all([line.purchase_order_id == self.purchase_order for line in bill.line_ids if line.purchase_order_id] or [False]))
63
64 def test_import_purchase_order_reference_from_lines_description(self):
65 """
66 This test will try to match a purchase order when the purchase reference is not
67 in the provided field but in the lines descriptions
68 """
69 bill = self._create_bill_from_xml("ubl_bis3_PO_description.xml")
70 self.assertEqual(bill.invoice_origin, self.purchase_order.name)
71 # Checks if all lines referencing a PO reference the PO we created in setup.
72 # The 'or [False]' makes sure that there's at least one line referencing a PO.
73 self.assertTrue(all([line.purchase_order_id == self.purchase_order for line in bill.line_ids if line.purchase_order_id] or [False]))
74
75 def test_multiple_purchase_order_references(self):
76 """
77 This test checks that we find the purchase_order_id when giving multiple possible
78 references in the 'invoice_origin' field
79 """
80 # Test with reference
81 bill = self.env['account.move'].create({
82 'move_type': 'in_invoice',
83 'invoice_origin': 'TEST multiple references test_purchase_order and other refs',
84 'partner_id': self.partner_a.id,
85 'invoice_line_ids': [Command.create({
86 'quantity': 1,
87 'price_unit': 40,
88 })]
89 })
90 bill._link_bill_origin_to_purchase_orders()
91 self.assertEqual(bill.invoice_origin, 'test_purchase_order')
92 self.assertTrue(all(line.purchase_order_id == self.purchase_order for line in bill.line_ids if line.purchase_order_id))
93
94 # Test without ref
95 bill_2 = self.env['account.move'].create({
96 'move_type': 'in_invoice',
97 'invoice_origin': 'TEST multiple references and other refs',
98 'partner_id': self.partner_a.id,
99 'invoice_line_ids': [Command.create({
100 'quantity': 1,
101 'price_unit': 40,
102 })]
103 })
104 bill_2._link_bill_origin_to_purchase_orders()
105 self.assertFalse(bill_2.invoice_origin)
106 self.assertTrue(all(not line.purchase_order_id for line in bill_2.line_ids))