|
| 1 | +# Copyright 2026 Nextev Srl |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from lxml import etree |
| 5 | + |
| 6 | +from odoo.tests import tagged |
| 7 | + |
| 8 | +from odoo.addons.l10n_it_edi.tests.common import TestItEdi |
| 9 | + |
| 10 | + |
| 11 | +@tagged("post_install", "-at_install") |
| 12 | +class TestEdiDeliveryNote(TestItEdi): |
| 13 | + @classmethod |
| 14 | + def setUpClass(cls): |
| 15 | + super().setUpClass() |
| 16 | + |
| 17 | + # Product |
| 18 | + cls.product = ( |
| 19 | + cls.env["product.product"] |
| 20 | + .with_company(cls.company) |
| 21 | + .create( |
| 22 | + { |
| 23 | + "name": "Test Product", |
| 24 | + "type": "consu", |
| 25 | + "list_price": 100.0, |
| 26 | + "invoice_policy": "delivery", |
| 27 | + } |
| 28 | + ) |
| 29 | + ) |
| 30 | + |
| 31 | + def test_deferred_invoice_with_delivery_note(self): |
| 32 | + """ |
| 33 | + Test that a deferred invoice (TD24) with delivery notes |
| 34 | + includes DatiDDT in the FatturaPA XML. |
| 35 | + """ |
| 36 | + from datetime import timedelta |
| 37 | + |
| 38 | + # Create sale order |
| 39 | + sale_order = ( |
| 40 | + self.env["sale.order"] |
| 41 | + .with_company(self.company) |
| 42 | + .create( |
| 43 | + { |
| 44 | + "partner_id": self.italian_partner_a.id, |
| 45 | + "order_line": [ |
| 46 | + ( |
| 47 | + 0, |
| 48 | + 0, |
| 49 | + { |
| 50 | + "product_id": self.product.id, |
| 51 | + "product_uom_qty": 5, |
| 52 | + "price_unit": 100.0, |
| 53 | + "tax_id": [(6, 0, self.default_tax.ids)], |
| 54 | + }, |
| 55 | + ) |
| 56 | + ], |
| 57 | + } |
| 58 | + ) |
| 59 | + ) |
| 60 | + sale_order.action_confirm() |
| 61 | + |
| 62 | + # Deliver products |
| 63 | + picking = sale_order.picking_ids |
| 64 | + picking.move_ids.quantity = 5 |
| 65 | + picking.button_validate() |
| 66 | + |
| 67 | + # Create delivery note from picking |
| 68 | + wizard = ( |
| 69 | + self.env["stock.delivery.note.create.wizard"] |
| 70 | + .with_context( |
| 71 | + active_model="stock.picking", |
| 72 | + active_ids=picking.ids, |
| 73 | + ) |
| 74 | + .create( |
| 75 | + { |
| 76 | + "partner_shipping_id": self.italian_partner_a.id, |
| 77 | + } |
| 78 | + ) |
| 79 | + ) |
| 80 | + wizard.confirm() |
| 81 | + |
| 82 | + delivery_note = self.env["stock.delivery.note"].search( |
| 83 | + [("picking_ids", "in", picking.ids)] |
| 84 | + ) |
| 85 | + self.assertTrue(delivery_note, "Delivery note should be created") |
| 86 | + |
| 87 | + # Validate delivery note |
| 88 | + delivery_note.action_confirm() |
| 89 | + |
| 90 | + # Create invoice from delivery note (deferred) |
| 91 | + invoice_wizard = ( |
| 92 | + self.env["stock.delivery.note.invoice.wizard"] |
| 93 | + .with_context( |
| 94 | + active_model="stock.delivery.note", |
| 95 | + active_ids=delivery_note.ids, |
| 96 | + ) |
| 97 | + .create({}) |
| 98 | + ) |
| 99 | + invoice_wizard.create_invoices() |
| 100 | + |
| 101 | + # Get the created invoice |
| 102 | + invoice = self.env["account.move"].search( |
| 103 | + [("partner_id", "=", self.italian_partner_a.id)], |
| 104 | + order="id desc", |
| 105 | + limit=1, |
| 106 | + ) |
| 107 | + self.assertTrue(invoice, "Invoice should be created") |
| 108 | + |
| 109 | + # Check that delivery_note_ids is populated |
| 110 | + self.assertEqual( |
| 111 | + len(invoice.delivery_note_ids), |
| 112 | + 1, |
| 113 | + "Invoice should be linked to 1 delivery note", |
| 114 | + ) |
| 115 | + |
| 116 | + # Set invoice date to a different day from delivery to make it deferred (TD24) |
| 117 | + # This simulates the real scenario: delivery today, invoice later |
| 118 | + invoice.invoice_date = picking.date_done.date() + timedelta(days=1) |
| 119 | + |
| 120 | + # Post invoice |
| 121 | + invoice.action_post() |
| 122 | + |
| 123 | + # Generate XML |
| 124 | + xml_content = invoice._l10n_it_edi_render_xml() |
| 125 | + self.assertTrue(xml_content, "XML should be generated") |
| 126 | + |
| 127 | + # Parse XML using local-name() to avoid namespace issues |
| 128 | + root = etree.fromstring(xml_content) |
| 129 | + |
| 130 | + # Check that DatiDDT exists |
| 131 | + dati_ddt = root.xpath("//*[local-name()='DatiDDT']") |
| 132 | + self.assertTrue(dati_ddt, "DatiDDT should be present in XML") |
| 133 | + |
| 134 | + # Check NumeroDDT |
| 135 | + numero_ddt = root.xpath( |
| 136 | + "//*[local-name()='DatiDDT']/*[local-name()='NumeroDDT']" |
| 137 | + ) |
| 138 | + self.assertTrue( |
| 139 | + len(numero_ddt) >= 1, "Should have at least one NumeroDDT element" |
| 140 | + ) |
| 141 | + # Verify one of them matches our delivery note |
| 142 | + numero_ddt_texts = [n.text for n in numero_ddt] |
| 143 | + self.assertIn( |
| 144 | + delivery_note.name, |
| 145 | + numero_ddt_texts, |
| 146 | + f"NumeroDDT should contain delivery note name {delivery_note.name}", |
| 147 | + ) |
| 148 | + |
| 149 | + # Check DataDDT |
| 150 | + data_ddt = root.xpath("//*[local-name()='DatiDDT']/*[local-name()='DataDDT']") |
| 151 | + self.assertTrue(len(data_ddt) >= 1, "Should have at least one DataDDT element") |
| 152 | + |
| 153 | + # Check document type is TD24 for deferred invoice (different day delivery) |
| 154 | + tipo_documento = root.xpath( |
| 155 | + "//*[local-name()='DatiGeneraliDocumento']/*[local-name()='TipoDocumento']" |
| 156 | + ) |
| 157 | + self.assertEqual( |
| 158 | + tipo_documento[0].text, |
| 159 | + "TD24", |
| 160 | + "Should be TD24 (deferred) invoice with DDT on different date", |
| 161 | + ) |
0 commit comments