| 1 |
from freezegun import freeze_time |
| 2 |
|
| 3 |
from odoo import Command, fields |
| 4 |
from odoo.addons.l10n_account_edi_ubl_cii_tests.tests.common import TestUBLCommon |
| 5 |
from odoo.addons.account.tests.test_account_move_send import TestAccountMoveSendCommon |
| 6 |
from odoo.exceptions import UserError |
| 7 |
from odoo.tests import tagged |
| 8 |
from odoo.tools import file_open |
| 9 |
|
| 10 |
|
| 11 |
@tagged('post_install_l10n', 'post_install', '-at_install') |
| 12 |
class TestUBLDK(TestUBLCommon, TestAccountMoveSendCommon): |
| 13 |
|
| 14 |
@classmethod |
| 15 |
@TestUBLCommon.setup_country('dk') |
| 16 |
def setUpClass(cls): |
| 17 |
super().setUpClass() |
| 18 |
cls.company_data['company'].write({ |
| 19 |
'city': 'Aalborg', |
| 20 |
'zip': '9430', |
| 21 |
'vat': 'DK12345674', |
| 22 |
'phone': '+45 32 12 34 56', |
| 23 |
'street': 'Paradisæblevej, 10', |
| 24 |
}) |
| 25 |
cls.env['res.partner.bank'].create({ |
| 26 |
'acc_type': 'iban', |
| 27 |
'partner_id': cls.company_data['company'].partner_id.id, |
| 28 |
'acc_number': 'DK5000400440116243', |
| 29 |
}) |
| 30 |
|
| 31 |
cls.company_data['company'].partner_id.update({ |
| 32 |
'peppol_endpoint': False, |
| 33 |
}) |
| 34 |
|
| 35 |
cls.partner_a.write({ |
| 36 |
'name': 'SUPER DANISH PARTNER', |
| 37 |
'city': 'Aalborg', |
| 38 |
'zip': '9430', |
| 39 |
'vat': 'DK12345674', |
| 40 |
'phone': '+45 32 12 35 56', |
| 41 |
'street': 'Paradisæblevej, 11', |
| 42 |
'country_id': cls.env.ref('base.dk').id, |
| 43 |
'invoice_edi_format': 'oioubl_201', |
| 44 |
'peppol_endpoint': False, |
| 45 |
}) |
| 46 |
cls.partner_b.write({ |
| 47 |
'name': 'SUPER BELGIAN PARTNER', |
| 48 |
'street': 'Rue du Paradis, 10', |
| 49 |
'zip': '6870', |
| 50 |
'city': 'Eghezee', |
| 51 |
'country_id': cls.env.ref('base.be').id, |
| 52 |
'phone': '061928374', |
| 53 |
'vat': 'BE0897223670', |
| 54 |
'invoice_edi_format': 'oioubl_201', |
| 55 |
'peppol_endpoint': False, |
| 56 |
}) |
| 57 |
cls.partner_c = cls.env["res.partner"].create({ |
| 58 |
'name': 'SUPER FRENCH PARTNER', |
| 59 |
'street': 'Rue Fabricy, 16', |
| 60 |
'zip': '59000', |
| 61 |
'city': 'Lille', |
| 62 |
'country_id': cls.env.ref('base.fr').id, |
| 63 |
'phone': '+33 1 23 45 67 89', |
| 64 |
'vat': 'FR23334175221', |
| 65 |
'company_registry': '123 568 941 00056', |
| 66 |
'invoice_edi_format': 'oioubl_201', |
| 67 |
'peppol_endpoint': False, |
| 68 |
}) |
| 69 |
cls.dk_local_sale_tax_1 = cls.env["account.chart.template"].ref('tax_s1y') |
| 70 |
cls.dk_local_sale_tax_2 = cls.env["account.chart.template"].ref('tax_s1') |
| 71 |
cls.dk_foreign_sale_tax_1 = cls.env["account.chart.template"].ref('tax_s0') |
| 72 |
cls.dk_foreign_sale_tax_2 = cls.env["account.chart.template"].ref('tax_s7') |
| 73 |
cls.dk_local_purchase_tax_goods = cls.env["account.chart.template"].ref('tax_k1') |
| 74 |
|
| 75 |
def create_post_and_send_invoice(self, partner=None, move_type='out_invoice'): |
| 76 |
if not partner: |
| 77 |
partner = self.partner_a |
| 78 |
|
| 79 |
if partner == self.partner_a: |
| 80 |
# local dk taxes |
| 81 |
tax_1, tax_2 = self.dk_local_sale_tax_1, self.dk_local_sale_tax_2 |
| 82 |
else: |
| 83 |
# dk taxes for foreigners |
| 84 |
tax_1, tax_2 = self.dk_foreign_sale_tax_1, self.dk_foreign_sale_tax_2 |
| 85 |
|
| 86 |
invoice = self.env["account.move"].create({ |
| 87 |
'move_type': move_type, |
| 88 |
'partner_id': partner.id, |
| 89 |
'partner_bank_id': self.env.company.partner_id.bank_ids[:1].id, |
| 90 |
'invoice_payment_term_id': self.pay_terms_b.id, |
| 91 |
'invoice_date': '2017-01-01', |
| 92 |
'date': '2017-01-01', |
| 93 |
'narration': 'test narration', |
| 94 |
'ref': 'ref_move', |
| 95 |
'invoice_line_ids': [ |
| 96 |
Command.create({ |
| 97 |
'product_id': self.product_a.id, |
| 98 |
'quantity': 1.0, |
| 99 |
'price_unit': 500.0, |
| 100 |
'tax_ids': [Command.set(tax_1.ids)], |
| 101 |
}), |
| 102 |
Command.create({ |
| 103 |
'product_id': self.product_b.id, |
| 104 |
'quantity': 1.0, |
| 105 |
'price_unit': 1000.0, |
| 106 |
'tax_ids': [Command.set(tax_2.ids)], |
| 107 |
}), |
| 108 |
], |
| 109 |
}) |
| 110 |
invoice.action_post() |
| 111 |
wizard = self.env['account.move.send.wizard'] \ |
| 112 |
.with_context(active_model=invoice._name, active_ids=invoice.ids) \ |
| 113 |
.create({}) |
| 114 |
wizard.action_send_and_print() |
| 115 |
return invoice |
| 116 |
|
| 117 |
######### |
| 118 |
# EXPORT |
| 119 |
######### |
| 120 |
|
| 121 |
@freeze_time('2017-01-01') |
| 122 |
def test_export_invoice_two_line_partner_dk(self): |
| 123 |
invoice = self.create_post_and_send_invoice() |
| 124 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 125 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_partner_dk.xml") |
| 126 |
|
| 127 |
@freeze_time('2017-01-01') |
| 128 |
def test_export_invoice_two_line_foreign_partner_be(self): |
| 129 |
# Set peppol endpoint to have schemeID of 'GLN' |
| 130 |
self.company_data['company'].partner_id.peppol_endpoint = '0239843188' |
| 131 |
invoice = self.create_post_and_send_invoice(partner=self.partner_b) |
| 132 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 133 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_foreign_partner_be.xml") |
| 134 |
|
| 135 |
@freeze_time('2017-01-01') |
| 136 |
def test_export_invoice_two_line_foreign_partner_fr(self): |
| 137 |
invoice = self.create_post_and_send_invoice(partner=self.partner_c) |
| 138 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 139 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_foreign_partner_fr.xml") |
| 140 |
|
| 141 |
@freeze_time('2017-01-01') |
| 142 |
def test_export_credit_note_two_line_partner_dk(self): |
| 143 |
refund = self.create_post_and_send_invoice(move_type='out_refund') |
| 144 |
self.assertTrue(refund.ubl_cii_xml_id) |
| 145 |
self._assert_invoice_attachment(refund.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_refund_partner_dk.xml") |
| 146 |
|
| 147 |
@freeze_time('2017-01-01') |
| 148 |
def test_export_credit_note_two_line_partner_fr(self): |
| 149 |
refund = self.create_post_and_send_invoice(partner=self.partner_c, move_type='out_refund') |
| 150 |
self.assertTrue(refund.ubl_cii_xml_id) |
| 151 |
self._assert_invoice_attachment(refund.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_refund_foreign_partner_fr.xml") |
| 152 |
|
| 153 |
@freeze_time('2017-01-01') |
| 154 |
def test_oioubl_export_should_still_be_valid_when_currency_has_more_precision_digit(self): |
| 155 |
self.company_data['company'].currency_id.rounding = 0.001 |
| 156 |
invoice = self.create_post_and_send_invoice() |
| 157 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 158 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_partner_dk.xml") |
| 159 |
|
| 160 |
@freeze_time('2017-01-01') |
| 161 |
def test_oioubl_export_should_raise_an_error_when_partner_building_number_is_missing(self): |
| 162 |
self.partner_a.street = 'Paradisæblevej' # remove the street number from the address |
| 163 |
with self.assertRaisesRegex(UserError, "The following partner's street number is missing"): |
| 164 |
self.create_post_and_send_invoice() |
| 165 |
|
| 166 |
@freeze_time('2017-01-01') |
| 167 |
def test_oioubl_export_should_raise_an_error_when_company_building_number_is_missing(self): |
| 168 |
self.env.company.partner_id.street = 'Paradisæblevej' |
| 169 |
with self.assertRaisesRegex(UserError, "The following partner's street number is missing"): |
| 170 |
self.create_post_and_send_invoice() |
| 171 |
|
| 172 |
@freeze_time('2017-01-01') |
| 173 |
def test_export_invoice_company_and_partner_without_country_code_prefix_in_vat(self): |
| 174 |
self.company_data['company'].vat = '12345674' |
| 175 |
self.company_data['company'].partner_id.peppol_endpoint = False |
| 176 |
self.partner_a.write({ |
| 177 |
'vat': 'DK12345674', |
| 178 |
'peppol_endpoint': False, |
| 179 |
'invoice_edi_format': 'oioubl_201', |
| 180 |
}) |
| 181 |
invoice = self.create_post_and_send_invoice() |
| 182 |
self.assertTrue(invoice.ubl_cii_xml_id) |
| 183 |
self._assert_invoice_attachment(invoice.ubl_cii_xml_id, xpaths=None, expected_file_path="from_odoo/oioubl_out_invoice_partner_dk.xml") |
| 184 |
|
| 185 |
@freeze_time('2017-01-01') |
| 186 |
def test_export_partner_fr_without_siret_should_raise_an_error(self): |
| 187 |
self.partner_c.company_registry = False |
| 188 |
self.partner_c.invoice_edi_format = 'oioubl_201' |
| 189 |
with self.assertRaisesRegex(UserError, "The company registry is required for french partner:"): |
| 190 |
self.create_post_and_send_invoice(partner=self.partner_c) |
| 191 |
|
| 192 |
@freeze_time('2017-01-01') |
| 193 |
def test_oioubl_export_partner_without_vat_number(self): |
| 194 |
""" This test verifies that we can't export an OIOUBL file for a partner |
| 195 |
who doesn't have a tax ID. It verifies that we receive a UserError |
| 196 |
telling to the user that this field is missing. |
| 197 |
""" |
| 198 |
self.partner_b.vat = None |
| 199 |
self.partner_b.invoice_edi_format = 'oioubl_201' # default format recomputes when vat is changed |
| 200 |
with self.assertRaises(UserError) as exception: |
| 201 |
self.create_post_and_send_invoice(partner=self.partner_b) |
| 202 |
self.assertIn(f"The field '{self.partner_b._fields['vat'].string}' is required", exception.exception.args[0]) |
| 203 |
|
| 204 |
######### |
| 205 |
# IMPORT |
| 206 |
######### |
| 207 |
|
| 208 |
def import_bill_xml_file_in_purchase_journal(self, file_path): |
| 209 |
file_path = f"{self.test_module}/tests/test_files/{file_path}" |
| 210 |
with file_open(file_path, 'rb') as file: |
| 211 |
xml_attachment = self.env['ir.attachment'].create({ |
| 212 |
'mimetype': 'application/xml', |
| 213 |
'name': 'test_invoice.xml', |
| 214 |
'raw': file.read(), |
| 215 |
}) |
| 216 |
purchase_journal = self.company_data["default_journal_purchase"] |
| 217 |
invoice = purchase_journal._create_document_from_attachment(xml_attachment.id) |
| 218 |
return invoice |
| 219 |
|
| 220 |
@freeze_time('2017-01-01') |
| 221 |
def test_oioubl_import_exemple_file_1(self): |
| 222 |
file_name = 'external/ADVORD_01_01_00_Invoice_v2p1.xml' |
| 223 |
bill = self.import_bill_xml_file_in_purchase_journal(file_name) |
| 224 |
self.assertRecordValues(bill, ({ |
| 225 |
'ref': 'A00095678', |
| 226 |
'invoice_date': fields.Date.from_string('2006-04-10'), |
| 227 |
'amount_total': 6_250.00, |
| 228 |
},)) |
| 229 |
self.assertRecordValues(bill.invoice_line_ids, ({ |
| 230 |
'name': 'Fine toy', |
| 231 |
'quantity': 1, |
| 232 |
'price_unit': 5_000.00, |
| 233 |
'price_subtotal': 5_000.00, |
| 234 |
'price_total': 6_250.00, |
| 235 |
'tax_ids': self.dk_local_purchase_tax_goods.ids, |
| 236 |
},)) |
| 237 |
|
| 238 |
@freeze_time('2017-01-01') |
| 239 |
def test_oioubl_import_exemple_file_2(self): |
| 240 |
file_name = 'external/ADVORD_02_02_00_Invoice_v2p1.xml' |
| 241 |
bill = self.import_bill_xml_file_in_purchase_journal(file_name) |
| 242 |
self.assertRecordValues(bill, ({ |
| 243 |
'ref': 'A00095680', |
| 244 |
'invoice_date': fields.Date.from_string('2006-04-10'), |
| 245 |
'amount_total': 5_000.00, |
| 246 |
},)) |
| 247 |
self.assertRecordValues(bill.invoice_line_ids, ({ |
| 248 |
'name': 'Superble', |
| 249 |
'quantity': 800, |
| 250 |
'price_unit': 5.00, |
| 251 |
'price_subtotal': 4_000.00, |
| 252 |
'price_total': 5_000.00, |
| 253 |
'tax_ids': self.dk_local_purchase_tax_goods.ids, |
| 254 |
},)) |
| 255 |
|
| 256 |
@freeze_time('2017-01-01') |
| 257 |
def test_oioubl_import_exemple_file_3(self): |
| 258 |
file_name = 'external/ADVORD_03_03_00_Invoice_v2p1.xml' |
| 259 |
bill = self.import_bill_xml_file_in_purchase_journal(file_name) |
| 260 |
self.assertRecordValues(bill, ({ |
| 261 |
'ref': 'A00095678', |
| 262 |
'invoice_date': fields.Date.from_string('2006-04-10'), |
| 263 |
'amount_total': 6_250.00, |
| 264 |
},)) |
| 265 |
self.assertRecordValues(bill.invoice_line_ids, ({ |
| 266 |
'name': 'Konsulentrapport', |
| 267 |
'quantity': 1, |
| 268 |
'price_unit': 5_000.00, |
| 269 |
'price_subtotal': 5_000.00, |
| 270 |
'price_total': 6_250.00, |
| 271 |
'tax_ids': self.dk_local_purchase_tax_goods.ids, |
| 272 |
},)) |
| 273 |
|
| 274 |
@freeze_time('2017-01-01') |
| 275 |
def test_oioubl_import_exemple_file_4(self): |
| 276 |
file_name = 'external/BASPRO_01_01_00_Invoice_v2p1.xml' |
| 277 |
bill = self.import_bill_xml_file_in_purchase_journal(file_name) |
| 278 |
self.assertRecordValues(bill, ({ |
| 279 |
'ref': 'A00095678', |
| 280 |
'invoice_date': fields.Date.from_string('2005-11-20'), |
| 281 |
'amount_total': 6_312.50, |
| 282 |
},)) |
| 283 |
self.assertRecordValues(bill.invoice_line_ids, ({ |
| 284 |
'name': 'Hejsetavle', |
| 285 |
'quantity': 1, |
| 286 |
'price_unit': 5_000.00, |
| 287 |
'price_subtotal': 5_000.00, |
| 288 |
'price_total': 6_250.00, |
| 289 |
'tax_ids': self.dk_local_purchase_tax_goods.ids, |
| 290 |
}, { |
| 291 |
'name': 'Beslag', |
| 292 |
'quantity': 2, |
| 293 |
'price_unit': 25.00, |
| 294 |
'price_subtotal': 50.00, |
| 295 |
'price_total': 62.50, |
| 296 |
'tax_ids': self.dk_local_purchase_tax_goods.ids, |
| 297 |
})) |