diff --git a/base_import_pdf_by_template_account/tests/test_base_import_pdf_by_template_account.py b/base_import_pdf_by_template_account/tests/test_base_import_pdf_by_template_account.py index 1771e81cb3..cc6033cbd4 100644 --- a/base_import_pdf_by_template_account/tests/test_base_import_pdf_by_template_account.py +++ b/base_import_pdf_by_template_account/tests/test_base_import_pdf_by_template_account.py @@ -12,21 +12,160 @@ class TestBaseImportPdfByTemplateAccount(BaseCommon): def setUpClass(cls): super().setUpClass() cls.env.user.groups_id += cls.env.ref("analytic.group_analytic_accounting") - generic_product = cls.env.ref( - "base_import_pdf_by_template_account.generic_product" + # Demo data is not consistent, let's set our own template here + # We define a different auto_detect_pattern in the existing demo template + # to prevent it from being used. + cls.env.ref("base_import_pdf_by_template_account.invoice_tecnativa").write( + {"auto_detect_pattern": "custom-pattern"} ) - cls.partner_tecnativa = cls.env.ref( - "base_import_pdf_by_template_account.partner_tecnativa" + generic_product = cls.env["product.product"].create( + { + "name": "Test product", + "default_code": "GENERIC", + } + ) + plan = cls.env["account.analytic.plan"].create({"name": "Test plan"}) + cls.analytic_account = cls.env["account.analytic.account"].create( + { + "name": "Test account", + "plan_id": plan.id, + } + ) + cls.partner_tecnativa = cls.env["res.partner"].create({"name": "Tecnativa"}) + cls.env["product.product"].create( + { + "name": "Test Rotulador", + "default_code": "ROTULADOR", + "seller_ids": [ + ( + 0, + 0, + { + "partner_id": cls.partner_tecnativa.id, + "product_code": "CONS_0001", + }, + ), + ], + } ) - cls.env.ref( - "base_import_pdf_by_template_account.invoice_tecnativa_line_product_id" - ).write({"default_value": f"product.product,{generic_product.id}"}) - cls.analytic_account = cls.env.ref( - "base_import_pdf_by_template_account.analytic_account_tecnativa" + # We change the values of existing products so that they are not found and + # make the test "simpler". + cls.env.ref("base_import_pdf_by_template_account.product_boligrafo").write( + {"seller_ids": [(5, 0, 0)]} + ) + cls.env.ref("base_import_pdf_by_template_account.product_leds").write( + {"seller_ids": [(5, 0, 0)]} + ) + cls.env.ref("base_import_pdf_by_template_account.product_plastificadora").write( + {"seller_ids": [(5, 0, 0)]} + ) + cls.env.ref("base_import_pdf_by_template_account.product_laminas").write( + {"seller_ids": [(5, 0, 0)]} + ) + cls.env.ref("base_import_pdf_by_template_account.product_trituradora").write( + {"seller_ids": [(5, 0, 0)]} + ) + # pylint: disable=W1401 + cls.env["base.import.pdf.template"].create( + { + "name": "Invoices Tecnativa", + "model_id": cls.env.ref("account.model_account_move").id, + "child_field_id": cls.env.ref( + "account.field_account_move__invoice_line_ids" + ).id, + "auto_detect_pattern": "(B 8 7 5 3 0 4 3 2)", + "header_items": "Producto,Cantidad,Precio,AnalyticDistribution", + "line_ids": [ + ( + 0, + 0, + { + "related_model": "header", + "field_id": cls.env.ref( + "account.field_account_move__partner_id" + ).id, + "value_type": "fixed", + "fixed_value": f"res.partner,{cls.partner_tecnativa.id}", + }, + ), + ( + 0, + 0, + { + "related_model": "header", + "field_id": cls.env.ref( + "account.field_account_move__move_type" + ).id, + "value_type": "fixed", + "fixed_value_selection": cls.env.ref( + "account.selection__account_invoice_report__move_type__in_invoice" + ).id, + }, + ), + ( + 0, + 0, + { + "related_model": "lines", + "field_id": cls.env.ref( + "account.field_account_move_line__product_id" + ).id, + "column": 0, + "pattern": "\[([A-Z\d]+[_|-][A-Z\d]+)\]", + "value_type": "variable", + "search_field_id": cls.env.ref( + "product.field_product_product__seller_ids" + ).id, + "search_subfield_id": cls.env.ref( + "product.field_product_supplierinfo__product_code" + ).id, + "default_value": f"product.product,{generic_product.id}", + }, + ), + ( + 0, + 0, + { + "related_model": "lines", + "field_id": cls.env.ref( + "account.field_account_move_line__quantity" + ).id, + "column": 1, + "pattern": "\[[A-Z\d]+[_|-][A-Z\d]+\] [a-zA-Záí]* ([0-9]{1,3})", # noqa: E501 + "value_type": "variable", + }, + ), + ( + 0, + 0, + { + "related_model": "lines", + "field_id": cls.env.ref( + "account.field_account_move_line__price_unit" + ).id, + "column": 2, + "pattern": "\[[A-Z\d]+[_|-][A-Z\d]+\] [a-zA-Záí]* [0-9]{1,3} ([0-9]{1,3}.[0-9]{2})", # noqa: E501 + "value_type": "variable", + "log_distinct_value": True, + }, + ), + ( + 0, + 0, + { + "related_model": "lines", + "field_id": cls.env.ref( + "account.field_account_move_line__analytic_distribution" + ).id, + "column": 3, + "value_type": "fixed", + "fixed_value_text": '{"%s": 100.0}' + % (cls.analytic_account.id), + }, + ), + ], + } ) - cls.env.ref( - "base_import_pdf_by_template_account.invoice_tecnativa_analytic_distribution" - ).write({"fixed_value_text": '{"%s": 100.0}' % (cls.analytic_account.id)}) cls.journal = cls.env["account.journal"].search( [("type", "=", "purchase"), ("company_id", "=", cls.env.company.id)], limit=1, @@ -63,11 +202,12 @@ def _test_account_invoice_tecnativa_data(self, record): self.assertEqual(sum(record.invoice_line_ids.mapped("quantity")), 665) default_codes = record.invoice_line_ids.mapped("product_id.default_code") self.assertIn("ROTULADOR", default_codes) - self.assertIn("BOLIGRAFO", default_codes) - self.assertIn("LEDS", default_codes) - self.assertIn("PLASTIFICADORA", default_codes) - self.assertIn("LAMINAS", default_codes) - self.assertIn("TRITURADORA", default_codes) + self.assertNotIn("BOLIGRAFO", default_codes) + self.assertNotIn("LEDS", default_codes) + self.assertNotIn("PLASTIFICADORA", default_codes) + self.assertNotIn("LAMINAS", default_codes) + self.assertNotIn("TRITURADORA", default_codes) + self.assertIn("GENERIC", default_codes) self.assertIn("100.25", record.message_ids[-1].body) self.assertEqual( record.invoice_line_ids[0].analytic_distribution,