| 1 |
from freezegun import freeze_time |
| 2 |
from requests import PreparedRequest, Response, Session |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
from odoo import Command |
| 6 |
from odoo.exceptions import UserError |
| 7 |
from odoo.tests import tagged |
| 8 |
|
| 9 |
from odoo.addons.account.tests.test_account_move_send import TestAccountMoveSendCommon |
| 10 |
from odoo.addons.l10n_account_edi_ubl_cii_tests.tests.common import TestUBLCommon |
| 11 |
|
| 12 |
|
| 13 |
@tagged('post_install_l10n', 'post_install', '-at_install') |
| 14 |
class TestUBLDKOIOUBL21(TestUBLCommon, TestAccountMoveSendCommon): |
| 15 |
|
| 16 |
@classmethod |
| 17 |
@TestUBLCommon.setup_country('dk') |
| 18 |
def setUpClass(cls): |
| 19 |
super().setUpClass() |
| 20 |
cls.company_data['company'].write({ |
| 21 |
'city': 'Aalborg', |
| 22 |
'zip': '9430', |
| 23 |
'vat': 'DK12345674', |
| 24 |
'phone': '+45 32 12 34 56', |
| 25 |
'street': 'Paradisæblevej, 10', |
| 26 |
}) |
| 27 |
cls.env['res.partner.bank'].create({ |
| 28 |
'acc_type': 'iban', |
| 29 |
'partner_id': cls.company_data['company'].partner_id.id, |
| 30 |
'acc_number': 'DK5000400440116243', |
| 31 |
}) |
| 32 |
|
| 33 |
cls.company_data['company'].partner_id.update({ |
| 34 |
'peppol_endpoint': False, |
| 35 |
}) |
| 36 |
|
| 37 |
cls.partner_a.write({ |
| 38 |
'name': 'SUPER DANISH PARTNER', |
| 39 |
'city': 'Aalborg', |
| 40 |
'zip': '9430', |
| 41 |
'vat': 'DK12345674', |
| 42 |
'phone': '+45 32 12 35 56', |
| 43 |
'street': 'Paradisæblevej, 11', |
| 44 |
'country_id': cls.env.ref('base.dk').id, |
| 45 |
'invoice_edi_format': 'oioubl_21', |
| 46 |
}) |
| 47 |
cls.partner_b.write({ |
| 48 |
'name': 'SUPER BELGIAN PARTNER', |
| 49 |
'street': 'Rue du Paradis, 10', |
| 50 |
'zip': '6870', |
| 51 |
'city': 'Eghezee', |
| 52 |
'country_id': cls.env.ref('base.be').id, |
| 53 |
'phone': '061928374', |
| 54 |
'vat': 'BE0897223670', |
| 55 |
'invoice_edi_format': 'oioubl_21', |
| 56 |
'nemhandel_identifier_type': '0088', |
| 57 |
'nemhandel_identifier_value': '5798009811512', |
| 58 |
|
| 59 |
}) |
| 60 |
cls.partner_c = cls.env["res.partner"].create({ |
| 61 |
'name': 'SUPER FRENCH PARTNER', |
| 62 |
'street': 'Rue Fabricy, 16', |
| 63 |
'zip': '59000', |
| 64 |
'city': 'Lille', |
| 65 |
'country_id': cls.env.ref('base.fr').id, |
| 66 |
'phone': '+33 1 23 45 67 89', |
| 67 |
'vat': 'FR23334175221', |
| 68 |
'company_registry': '123 568 941 00056', |
| 69 |
'invoice_edi_format': 'oioubl_21', |
| 70 |
'nemhandel_identifier_type': '0088', |
| 71 |
'nemhandel_identifier_value': '5798009811639', |
| 72 |
}) |
| 73 |
cls.dk_local_sale_tax_1 = cls.env["account.chart.template"].ref('tax_s1y') |
| 74 |
cls.dk_local_sale_tax_2 = cls.env["account.chart.template"].ref('tax_s1') |
| 75 |
cls.dk_foreign_sale_tax_1 = cls.env["account.chart.template"].ref('tax_s0') |
| 76 |
cls.dk_foreign_sale_tax_2 = cls.env["account.chart.template"].ref('tax_s7') |
| 77 |
cls.dk_local_purchase_tax_goods = cls.env["account.chart.template"].ref('tax_k1') |
| 78 |
|
| 79 |
def create_post_and_send_invoice(self, partner=None, move_type='out_invoice', send=True): |
| 80 |
if not partner: |
| 81 |
partner = self.partner_a |
| 82 |
|
| 83 |
if partner == self.partner_a: |
| 84 |
# local dk taxes |
| 85 |
tax_1, tax_2 = self.dk_local_sale_tax_1, self.dk_local_sale_tax_2 |
| 86 |
else: |
| 87 |
# dk taxes for foreigners |
| 88 |
tax_1, tax_2 = self.dk_foreign_sale_tax_1, self.dk_foreign_sale_tax_2 |
| 89 |
|
| 90 |
invoice = self.env["account.move"].create({ |
| 91 |
'move_type': move_type, |
| 92 |
'partner_id': partner.id, |
| 93 |
'partner_bank_id': self.env.company.partner_id.bank_ids[:1].id, |
| 94 |
'invoice_payment_term_id': self.pay_terms_b.id, |
| 95 |
'invoice_date': '2017-01-01', |
| 96 |
'date': '2017-01-01', |
| 97 |
'narration': 'test narration', |
| 98 |
'ref': 'ref_move', |
| 99 |
'invoice_line_ids': [ |
| 100 |
Command.create({ |
| 101 |
'product_id': self.product_a.id, |
| 102 |
'quantity': 1.0, |
| 103 |
'price_unit': 500.0, |
| 104 |
'tax_ids': [Command.set(tax_1.ids)], |
| 105 |
}), |
| 106 |
Command.create({ |
| 107 |
'product_id': self.product_b.id, |
| 108 |
'quantity': 1.0, |
| 109 |
'price_unit': 1000.0, |
| 110 |
'tax_ids': [Command.set(tax_2.ids)], |
| 111 |
}), |
| 112 |
], |
| 113 |
}) |
| 114 |
invoice.action_post() |
| 115 |
if send: |
| 116 |
self._send_patched(invoice) |
| 117 |
return invoice |
| 118 |
|
| 119 |
@classmethod |
| 120 |
def _send_patched(cls, invoice): |
| 121 |
with patch('odoo.addons.l10n_dk_nemhandel.models.res_partner.ResPartner._get_nemhandel_verification_state', return_value='not_valid'): |
| 122 |
wizard = cls.env['account.move.send.wizard'] \ |
| 123 |
.with_context(active_model=invoice._name, active_ids=invoice.ids) \ |
| 124 |
.create({}) |
| 125 |
wizard.action_send_and_print() |
| 126 |
|
| 127 |
@classmethod |
| 128 |
def _request_handler(cls, s: Session, r: PreparedRequest, /, **kw): |
| 129 |
response = Response() |
| 130 |
response.status_code = 200 |
| 131 |
if r.url.endswith('iso6523-actorid-upis%3A%3A0184%3A12345674'): |
| 132 |
response._content = b"""<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<smp:ServiceGroup xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:id="http://busdox.org/transport/identifiers/1.0/" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:smp="http://busdox.org/serviceMetadata/publishing/1.0/"><id:ParticipantIdentifier scheme="iso6523-actorid-upis">0208:0477472701</id:ParticipantIdentifier>' |
| 133 |
'<smp:ServiceMetadataReferenceCollection><smp:ServiceMetadataReference href="http://smp.nemhandel.dk/iso6523-actorid-upis%3A%3A0184%3A12345674/services/busdox-docid-qns%3A%3Aurn%3Aoasis%3Anames%3Aspecification%3Aubl%3Aschema%3Axsd%3AInvoice-2%3A%3AInvoice%23%23urn%3Acen.eu%3Aen16931%3A2017%23compliant%23urn%3Afdc%3Apeppol.eu%3A2017%3Apoacc%3Abilling%3A3.0%3A%3A2.1"/>' |
| 134 |
'</smp:ServiceMetadataReferenceCollection></smp:ServiceGroup>""" |
| 135 |
return response |
| 136 |
if r.url.endswith('iso6523-actorid-upis%3A%3A0208%3A5798009811639'): |
| 137 |
response._content = b"""<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<smp:ServiceGroup xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:id="http://busdox.org/transport/identifiers/1.0/" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:smp="http://busdox.org/serviceMetadata/publishing/1.0/"><id:ParticipantIdentifier scheme="iso6523-actorid-upis">0088:5798009811639</id:ParticipantIdentifier> |
| 138 |
'<smp:ServiceMetadataReferenceCollection><smp:ServiceMetadataReference href="http://smp.nemhandel.dk/iso6523-actorid-upis%3A%3A0208%3A5798009811639/services/busdox-docid-qns%3A%3Aurn%3Aoasis%3Anames%3Aspecification%3Aubl%3Aschema%3Axsd%3AInvoice-2%3A%3AInvoice%23%23urn%3Acen.eu%3Aen16931%3A2017%23compliant%23urn%3Afdc%3Apeppol.eu%3A2017%3Apoacc%3Abilling%3A3.0%3A%3A2.1"/>' |
| 139 |
'</smp:ServiceMetadataReferenceCollection></smp:ServiceGroup>""" |
| 140 |
return response |
| 141 |
if r.url.endswith('iso6523-actorid-upis%3A%3A0208%3A5798009811512'): |
| 142 |
response._content = b"""<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<smp:ServiceGroup xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:id="http://busdox.org/transport/identifiers/1.0/" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:smp="http://busdox.org/serviceMetadata/publishing/1.0/"><id:ParticipantIdentifier scheme="iso6523-actorid-upis">0088:5798009811512</id:ParticipantIdentifier> |
| 143 |
'<smp:ServiceMetadataReferenceCollection><smp:ServiceMetadataReference href="http://smp.nemhandel.dk/iso6523-actorid-upis%3A%3A0088%3A5798009811512/services/busdox-docid-qns%3A%3Aurn%3Aoasis%3Anames%3Aspecification%3Aubl%3Aschema%3Axsd%3AInvoice-2%3A%3AInvoice%23%23urn%3Acen.eu%3Aen16931%3A2017%23compliant%23urn%3Afdc%3Apeppol.eu%3A2017%3Apoacc%3Abilling%3A3.0%3A%3A2.1"/>' |
| 144 |
'</smp:ServiceMetadataReferenceCollection></smp:ServiceGroup>""" |
| 145 |
return response |
| 146 |
|
| 147 |
return super()._request_handler(s, r, **kw) |
| 148 |
|
| 149 |
######### |
| 150 |
# EXPORT |
| 151 |
######### |
| 152 |
|
| 153 |
@freeze_time('2017-01-01') |
| 154 |
def test_export_invoice_partner_dk(self): |
| 155 |
invoice = self.create_post_and_send_invoice() |
| 156 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 157 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_partner_dk.xml") |
| 158 |
|
| 159 |
@freeze_time('2017-01-01') |
| 160 |
def test_export_invoice_foreign_partner_be(self): |
| 161 |
# Set peppol endpoint to have schemeID of 'GLN' |
| 162 |
self.company_data['company'].partner_id.peppol_endpoint = '0239843188' |
| 163 |
invoice = self.create_post_and_send_invoice(partner=self.partner_b) |
| 164 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 165 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_foreign_partner_be.xml") |
| 166 |
|
| 167 |
@freeze_time('2017-01-01') |
| 168 |
def test_export_invoice_foreign_partner_fr(self): |
| 169 |
invoice = self.create_post_and_send_invoice(partner=self.partner_c) |
| 170 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 171 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_foreign_partner_fr.xml") |
| 172 |
|
| 173 |
@freeze_time('2017-01-01') |
| 174 |
def test_export_credit_note_partner_dk(self): |
| 175 |
""" Check the export of a credit note that has been partially paid """ |
| 176 |
refund = self.create_post_and_send_invoice(move_type='out_refund', send=False) |
| 177 |
self.env['account.payment.register'].with_context(active_model='account.move', active_ids=refund.ids).create({ |
| 178 |
'amount': 1000, |
| 179 |
})._create_payments() |
| 180 |
|
| 181 |
self._send_patched(refund) |
| 182 |
self.assertTrue(refund.ubl_cii_xml_id) |
| 183 |
self._assert_invoice_attachment(refund.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_refund_partner_dk.xml") |
| 184 |
|
| 185 |
@freeze_time('2017-01-01') |
| 186 |
def test_export_credit_note_partner_fr(self): |
| 187 |
refund = self.create_post_and_send_invoice(partner=self.partner_c, move_type='out_refund') |
| 188 |
self.assertTrue(refund.ubl_cii_xml_id) |
| 189 |
self._assert_invoice_attachment(refund.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_refund_foreign_partner_fr.xml") |
| 190 |
|
| 191 |
@freeze_time('2017-01-01') |
| 192 |
def test_oioubl_export_should_still_be_valid_when_currency_has_more_precision_digit(self): |
| 193 |
self.company_data['company'].currency_id.rounding = 0.001 |
| 194 |
invoice = self.create_post_and_send_invoice() |
| 195 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 196 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_partner_dk.xml") |
| 197 |
|
| 198 |
@freeze_time('2017-01-01') |
| 199 |
def test_oioubl_export_import_with_discount(self): |
| 200 |
""" Tests that the discount on a line is well exported, then taken into account when imported """ |
| 201 |
line_vals = { |
| 202 |
'product_id': self.product_a.id, |
| 203 |
'quantity': 10.0, |
| 204 |
'price_unit': 500.0, |
| 205 |
'discount': 10, |
| 206 |
'tax_ids': [self.dk_local_sale_tax_1.id], |
| 207 |
} |
| 208 |
invoice = self.env["account.move"].create({ |
| 209 |
'move_type': 'out_invoice', |
| 210 |
'partner_id': self.partner_a.id, |
| 211 |
'partner_bank_id': self.env.company.partner_id.bank_ids[:1].id, |
| 212 |
'invoice_date': '2017-01-01', |
| 213 |
'date': '2017-01-01', |
| 214 |
'narration': 'test narration', |
| 215 |
'ref': 'ref_move', |
| 216 |
'invoice_line_ids': [Command.create(line_vals)], |
| 217 |
}) |
| 218 |
invoice.action_post() |
| 219 |
self._send_patched(invoice) |
| 220 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 221 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_discount.xml") |
| 222 |
new_invoice = invoice.journal_id._create_document_from_attachment(invoice.ubl_cii_xml_id.ids) |
| 223 |
self.assertRecordValues(new_invoice.invoice_line_ids, [line_vals]) |
| 224 |
|
| 225 |
@freeze_time('2017-01-01') |
| 226 |
def test_oioubl_export_should_raise_an_error_when_partner_building_number_is_missing(self): |
| 227 |
self.partner_a.street = 'Paradisæblevej' # remove the street number from the address |
| 228 |
with self.assertRaisesRegex(UserError, "The following partner's street number is missing"): |
| 229 |
self.create_post_and_send_invoice() |
| 230 |
|
| 231 |
@freeze_time('2017-01-01') |
| 232 |
def test_oioubl_export_should_raise_an_error_when_company_building_number_is_missing(self): |
| 233 |
self.env.company.partner_id.street = 'Paradisæblevej' |
| 234 |
with self.assertRaisesRegex(UserError, "The following partner's street number is missing"): |
| 235 |
self.create_post_and_send_invoice() |
| 236 |
|
| 237 |
@freeze_time('2017-01-01') |
| 238 |
def test_export_partner_fr_without_siret_should_raise_an_error(self): |
| 239 |
self.partner_c.company_registry = False |
| 240 |
self.partner_c.invoice_edi_format = 'oioubl_21' |
| 241 |
with self.assertRaisesRegex(UserError, "The company registry is required for french partner:"): |
| 242 |
self.create_post_and_send_invoice(partner=self.partner_c) |
| 243 |
|
| 244 |
@freeze_time('2017-01-01') |
| 245 |
def test_oioubl_export_partner_without_vat_number(self): |
| 246 |
""" This test verifies that we can't export an OIOUBL file for a partner |
| 247 |
who doesn't have a tax ID. It verifies that we receive a UserError |
| 248 |
telling to the user that this field is missing. |
| 249 |
""" |
| 250 |
self.partner_b.vat = None |
| 251 |
self.partner_b.invoice_edi_format = 'oioubl_21' # default format recomputes when vat is changed |
| 252 |
with self.assertRaises(UserError) as exception: |
| 253 |
self.create_post_and_send_invoice(partner=self.partner_b) |
| 254 |
self.assertIn(f"The field '{self.partner_b._fields['vat'].string}' is required", exception.exception.args[0]) |