| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from datetime import datetime |
| 3 |
|
| 4 |
from freezegun import freeze_time |
| 5 |
from lxml import etree |
| 6 |
from odoo.addons.account.tests.common import AccountTestInvoicingCommon |
| 7 |
|
| 8 |
from odoo.exceptions import UserError |
| 9 |
from odoo.fields import Command |
| 10 |
from odoo.tests import Form, tagged |
| 11 |
from odoo.tools import file_open |
| 12 |
|
| 13 |
NS_MAP = { |
| 14 |
'cac': 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2', |
| 15 |
'cbc': 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2', |
| 16 |
'ext': 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2', |
| 17 |
'sig': 'urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2', |
| 18 |
'sac': 'urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2', |
| 19 |
'sbc': 'urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2', |
| 20 |
'ds': 'http://www.w3.org/2000/09/xmldsig#', |
| 21 |
'xades': 'http://uri.etsi.org/01903/v1.3.2#', |
| 22 |
'inv': 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2', |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
@tagged('post_install_l10n', 'post_install', '-at_install') |
| 27 |
class L10nMyEDITestFileGeneration(AccountTestInvoicingCommon): |
| 28 |
|
| 29 |
@classmethod |
| 30 |
@AccountTestInvoicingCommon.setup_country('my') |
| 31 |
def setUpClass(cls): |
| 32 |
super().setUpClass() |
| 33 |
|
| 34 |
cls.other_currency = cls.setup_other_currency('EUR') |
| 35 |
|
| 36 |
# TIN number is required |
| 37 |
cls.company_data['company'].write({ |
| 38 |
'vat': 'C2584563200', |
| 39 |
'l10n_my_edi_mode': 'test', |
| 40 |
'l10n_my_edi_industrial_classification': cls.env['l10n_my_edi.industry_classification'].search([('code', '=', '01111')]).id, |
| 41 |
'l10n_my_identification_type': 'BRN', |
| 42 |
'l10n_my_identification_number': '202001234567', |
| 43 |
'state_id': cls.env.ref('base.state_my_jhr').id, |
| 44 |
'street': 'that one street, 5', |
| 45 |
'city': 'Main city', |
| 46 |
'phone': '+60123456789', |
| 47 |
}) |
| 48 |
cls.partner_a.write({ |
| 49 |
'vat': 'C2584563201', |
| 50 |
'l10n_my_identification_type': 'BRN', |
| 51 |
'l10n_my_identification_number': '202001234568', |
| 52 |
'country_id': cls.env.ref('base.my').id, |
| 53 |
'state_id': cls.env.ref('base.state_my_jhr').id, |
| 54 |
'street': 'that other street, 3', |
| 55 |
'city': 'Main city', |
| 56 |
'phone': '+60123456786', |
| 57 |
'l10n_my_edi_industrial_classification': cls.env['l10n_my_edi.industry_classification'].search([('code', '=', '01111')]).id, |
| 58 |
'ref': "MY-REF", |
| 59 |
}) |
| 60 |
cls.partner_b.write({ |
| 61 |
'vat': 'EI00000000020', |
| 62 |
'l10n_my_identification_type': 'BRN', |
| 63 |
'l10n_my_identification_number': 'NA', |
| 64 |
'country_id': cls.env.ref('base.us').id, |
| 65 |
'state_id': cls.env.ref('base.state_us_1'), |
| 66 |
'street': 'that other street, 3', |
| 67 |
'city': 'Main city', |
| 68 |
'phone': '+60123456785', |
| 69 |
'l10n_my_edi_industrial_classification': cls.env.ref('l10n_my_edi.class_00000', raise_if_not_found=False).id, |
| 70 |
}) |
| 71 |
cls.product_a.l10n_my_edi_classification_code = "001" |
| 72 |
|
| 73 |
cls.purchase_tax = cls.env['account.tax'].create({ |
| 74 |
'name': 'tax_10', |
| 75 |
'amount_type': 'percent', |
| 76 |
'amount': 10, |
| 77 |
'type_tax_use': 'purchase', |
| 78 |
'country_id': cls.env.ref('base.my').id, |
| 79 |
}) |
| 80 |
|
| 81 |
cls.fakenow = datetime(2024, 7, 15, 10, 00, 00) |
| 82 |
cls.startClassPatcher(freeze_time(cls.fakenow)) |
| 83 |
|
| 84 |
def test_01_can_generate_file(self): |
| 85 |
""" |
| 86 |
Simply test that with a valid configuration, we can generate the file. |
| 87 |
""" |
| 88 |
invoice = self.init_invoice('out_invoice', taxes=self.company_data['default_tax_sale'], products=self.product_a, post=True) |
| 89 |
myinvois_document = invoice._create_myinvois_document() |
| 90 |
|
| 91 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 92 |
self.assertFalse(errors) |
| 93 |
|
| 94 |
# The file is working! Now we assert that the specificities needed for this EDI (what you can find in this module) are found in the file. |
| 95 |
root = etree.fromstring(file) |
| 96 |
|
| 97 |
# 1. Invoice level |
| 98 |
|
| 99 |
# Validate the invoice type. In this case, an invoice with version 1.1 at the time of writing. |
| 100 |
self._assert_node_values( |
| 101 |
root, |
| 102 |
'cbc:InvoiceTypeCode', |
| 103 |
'01', |
| 104 |
attributes={'listVersionID': '1.1'}, |
| 105 |
) |
| 106 |
|
| 107 |
# Validate that the issue time is present, and correct |
| 108 |
self._assert_node_values( |
| 109 |
root, |
| 110 |
'cbc:IssueTime', |
| 111 |
'10:00:00Z', |
| 112 |
) |
| 113 |
|
| 114 |
# 2. Supplier party |
| 115 |
supplier_root = root.xpath('cac:AccountingSupplierParty/cac:Party', namespaces=NS_MAP)[0] |
| 116 |
|
| 117 |
# Industry classification |
| 118 |
self._assert_node_values( |
| 119 |
supplier_root, |
| 120 |
'cbc:IndustryClassificationCode', |
| 121 |
self.company_data['company'].l10n_my_edi_industrial_classification.code, |
| 122 |
attributes={'name': self.company_data['company'].l10n_my_edi_industrial_classification.name}, |
| 123 |
) |
| 124 |
# Party Identifications - TIN and BRN (or other type of id) are required. SST & TTX are tested separately. |
| 125 |
self._assert_node_values( |
| 126 |
supplier_root, |
| 127 |
'cac:PartyIdentification/cbc:ID[@schemeID="TIN"]', |
| 128 |
self.company_data['company'].vat, |
| 129 |
) |
| 130 |
self._assert_node_values( |
| 131 |
supplier_root, |
| 132 |
'cac:PartyIdentification/cbc:ID[@schemeID="BRN"]', |
| 133 |
self.company_data['company'].l10n_my_identification_number, |
| 134 |
) |
| 135 |
|
| 136 |
# Address format |
| 137 |
self._assert_node_values( |
| 138 |
supplier_root, |
| 139 |
'cac:PostalAddress/cac:AddressLine/cbc:Line', |
| 140 |
self.company_data['company'].street, |
| 141 |
) |
| 142 |
|
| 143 |
# 3. Customer level |
| 144 |
customer_root = root.xpath('cac:AccountingCustomerParty/cac:Party', namespaces=NS_MAP)[0] |
| 145 |
|
| 146 |
# Party Identifications - TIN and BRN (or other type of id) are required. SST & TTX are tested separately. |
| 147 |
self._assert_node_values( |
| 148 |
customer_root, |
| 149 |
'cac:PartyIdentification/cbc:ID[@schemeID="TIN"]', |
| 150 |
self.partner_a.commercial_partner_id.vat, |
| 151 |
) |
| 152 |
self._assert_node_values( |
| 153 |
customer_root, |
| 154 |
'cac:PartyIdentification/cbc:ID[@schemeID="BRN"]', |
| 155 |
self.partner_a.commercial_partner_id.l10n_my_identification_number, |
| 156 |
) |
| 157 |
|
| 158 |
# Address format |
| 159 |
self._assert_node_values( |
| 160 |
customer_root, |
| 161 |
'cac:PostalAddress/cac:AddressLine/cbc:Line', |
| 162 |
self.partner_a.street, |
| 163 |
) |
| 164 |
|
| 165 |
with file_open('l10n_my_edi/tests/expected_xmls/invoice.xml', 'rb') as f: |
| 166 |
expected_xml = etree.fromstring(f.read()) |
| 167 |
self.assertXmlTreeEqual(root, expected_xml) |
| 168 |
|
| 169 |
def test_02_multicurrency(self): |
| 170 |
""" |
| 171 |
Simply ensure that in a multi currency environment, the rate is found in the file and is the expected one. |
| 172 |
""" |
| 173 |
invoice = self.init_invoice( |
| 174 |
'out_invoice', currency=self.other_currency, taxes=self.company_data['default_tax_sale'], products=self.product_a, post=True |
| 175 |
) |
| 176 |
myinvois_document = invoice._create_myinvois_document() |
| 177 |
|
| 178 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 179 |
self.assertFalse(errors) |
| 180 |
|
| 181 |
root = etree.fromstring(file) |
| 182 |
# We should have a tax exchange rate set. |
| 183 |
# The rate is the rate from foreign currency to MYR |
| 184 |
self._assert_node_values( |
| 185 |
root, |
| 186 |
'cac:TaxExchangeRate/cbc:CalculationRate', |
| 187 |
'0.5', |
| 188 |
) |
| 189 |
# Target currency is always MYR |
| 190 |
self._assert_node_values( |
| 191 |
root, |
| 192 |
'cac:TaxExchangeRate/cbc:TargetCurrencyCode', |
| 193 |
'MYR', |
| 194 |
) |
| 195 |
self._assert_node_values( |
| 196 |
root, |
| 197 |
'cac:TaxExchangeRate/cbc:SourceCurrencyCode', |
| 198 |
'EUR', |
| 199 |
) |
| 200 |
# Check that the TaxAmount node has the correct currency too |
| 201 |
self._assert_node_values( |
| 202 |
root, |
| 203 |
'cac:TaxTotal/cbc:TaxAmount', |
| 204 |
text='200.00', |
| 205 |
attributes={'currencyID': 'EUR'}, |
| 206 |
) |
| 207 |
|
| 208 |
with file_open('l10n_my_edi/tests/expected_xmls/invoice_multicurrency.xml', 'rb') as f: |
| 209 |
expected_xml = etree.fromstring(f.read()) |
| 210 |
self.assertXmlTreeEqual(root, expected_xml) |
| 211 |
|
| 212 |
def test_03_optional_fields(self): |
| 213 |
""" |
| 214 |
Set a few optional fields, and ensure that they appear as expecting in the file. |
| 215 |
""" |
| 216 |
invoice = self.init_invoice( |
| 217 |
'out_invoice', currency=self.other_currency, taxes=self.company_data['default_tax_sale'], products=self.product_a |
| 218 |
) |
| 219 |
invoice.write({ |
| 220 |
'invoice_incoterm_id': self.env.ref('account.incoterm_CFR').id, |
| 221 |
'l10n_my_edi_custom_form_reference': 'E12345678912', |
| 222 |
}) |
| 223 |
|
| 224 |
self.company_data['company'].write({ |
| 225 |
'sst_registration_number': 'A01-2345-67891012', |
| 226 |
'ttx_registration_number': '123-4567-89012345', |
| 227 |
}) |
| 228 |
self.partner_a.commercial_partner_id.sst_registration_number = 'A01-2345-67891013' |
| 229 |
|
| 230 |
invoice.action_post() |
| 231 |
myinvois_document = invoice._create_myinvois_document() |
| 232 |
|
| 233 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 234 |
self.assertFalse(errors) |
| 235 |
|
| 236 |
root = etree.fromstring(file) |
| 237 |
|
| 238 |
# We test a few values that are optional, yet mandatory in some cases (we leave it up to the user) |
| 239 |
# AdditionalDocumentReference => incoterm and customs |
| 240 |
self._assert_node_values( |
| 241 |
root, |
| 242 |
'cac:AdditionalDocumentReference[not(descendant::*[local-name() = "DocumentType"])]/cbc:ID', |
| 243 |
invoice.invoice_incoterm_id.code, |
| 244 |
) |
| 245 |
self._assert_node_values( |
| 246 |
root, |
| 247 |
'cac:AdditionalDocumentReference[descendant::*[local-name() = "DocumentType"]]/cbc:DocumentType', |
| 248 |
'K2', |
| 249 |
) |
| 250 |
self._assert_node_values( |
| 251 |
root, |
| 252 |
'cac:AdditionalDocumentReference[descendant::*[local-name() = "DocumentType"]]/cbc:ID', |
| 253 |
invoice.l10n_my_edi_custom_form_reference, |
| 254 |
) |
| 255 |
# SST and TTX numbers (tax registrations). SST can be used in both cases, TTX only for supplier. |
| 256 |
# supplier |
| 257 |
self._assert_node_values( |
| 258 |
root, |
| 259 |
'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID[@schemeID="SST"]', |
| 260 |
self.company_data['company'].sst_registration_number, |
| 261 |
) |
| 262 |
self._assert_node_values( |
| 263 |
root, |
| 264 |
'cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID[@schemeID="TTX"]', |
| 265 |
self.company_data['company'].ttx_registration_number, |
| 266 |
) |
| 267 |
# customer |
| 268 |
self._assert_node_values( |
| 269 |
root, |
| 270 |
'cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID[@schemeID="SST"]', |
| 271 |
self.partner_a.commercial_partner_id.sst_registration_number, |
| 272 |
) |
| 273 |
|
| 274 |
with file_open('l10n_my_edi/tests/expected_xmls/invoice_optional_fields.xml', 'rb') as f: |
| 275 |
expected_xml = etree.fromstring(f.read()) |
| 276 |
self.assertXmlTreeEqual(root, expected_xml) |
| 277 |
|
| 278 |
def test_04_credit_note(self): |
| 279 |
""" |
| 280 |
Ensure that the type is correctly set for another move type, as well as that the original |
| 281 |
uuid is present in an adjustment invoice. |
| 282 |
""" |
| 283 |
invoice = self.init_invoice( |
| 284 |
'out_invoice', currency=self.other_currency, taxes=self.company_data['default_tax_sale'], products=self.product_a, post=True, |
| 285 |
) |
| 286 |
invoice_document = invoice._create_myinvois_document() |
| 287 |
# Simulate that the document was sent |
| 288 |
invoice_document.write({ |
| 289 |
'myinvois_state': 'valid', |
| 290 |
'myinvois_external_uuid': '12345678912345678912345678', |
| 291 |
}) |
| 292 |
|
| 293 |
action = invoice.action_reverse() |
| 294 |
reversal_wizard = self.env[action['res_model']].with_context( |
| 295 |
active_ids=invoice.ids, |
| 296 |
active_model='account.move', |
| 297 |
default_journal_id=invoice.journal_id.id, |
| 298 |
).create({}) |
| 299 |
action = reversal_wizard.reverse_moves() |
| 300 |
credit_note = self.env['account.move'].browse(action['res_id']) |
| 301 |
credit_note.action_post() |
| 302 |
myinvois_document = credit_note._create_myinvois_document() |
| 303 |
|
| 304 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 305 |
self.assertFalse(errors) |
| 306 |
|
| 307 |
root = etree.fromstring(file) |
| 308 |
# Check the invoice type to endure that it is marked as credit note. |
| 309 |
self._assert_node_values( |
| 310 |
root, |
| 311 |
'cbc:InvoiceTypeCode', |
| 312 |
'02', |
| 313 |
attributes={'listVersionID': '1.1'}, |
| 314 |
) |
| 315 |
# Ensure that the UUID of the origin invoice is present |
| 316 |
self._assert_node_values( |
| 317 |
root, |
| 318 |
'cac:BillingReference/cac:InvoiceDocumentReference/cbc:ID', |
| 319 |
invoice_document.name, |
| 320 |
) |
| 321 |
self._assert_node_values( |
| 322 |
root, |
| 323 |
'cac:BillingReference/cac:InvoiceDocumentReference/cbc:UUID', |
| 324 |
invoice_document.myinvois_external_uuid, |
| 325 |
) |
| 326 |
|
| 327 |
with file_open('l10n_my_edi/tests/expected_xmls/credit_note.xml', 'rb') as f: |
| 328 |
expected_xml = etree.fromstring(f.read()) |
| 329 |
self.assertXmlTreeEqual(root, expected_xml) |
| 330 |
|
| 331 |
def test_05_invoice_with_so(self): |
| 332 |
""" |
| 333 |
Ensure that an invoice linked to an SO will not contain this information in the xml. |
| 334 |
""" |
| 335 |
if self.env.ref('base.module_sale').state != 'installed': |
| 336 |
self.skipTest("This test requires the sale module to be installed.") |
| 337 |
|
| 338 |
sale_order = self.env['sale.order'].sudo().create({ |
| 339 |
'partner_id': self.partner_a.id, |
| 340 |
'order_line': [(0, 0, { |
| 341 |
'product_id': self.product_a.id, |
| 342 |
'product_uom_qty': 1, |
| 343 |
'price_unit': 100, |
| 344 |
'currency_id': self.other_currency.id, |
| 345 |
'tax_ids': [Command.set(self.company_data['default_tax_sale'].ids)], |
| 346 |
})], |
| 347 |
}).sudo(False) |
| 348 |
sale_order.action_confirm() |
| 349 |
|
| 350 |
payment = self.env['sale.advance.payment.inv'].with_context({ |
| 351 |
'active_model': 'sale.order', |
| 352 |
'active_ids': [sale_order.id], |
| 353 |
'active_id': sale_order.id, |
| 354 |
'default_journal_id': self.company_data['default_journal_sale'].id, |
| 355 |
}).sudo().create({ |
| 356 |
'advance_payment_method': 'delivered' |
| 357 |
}).sudo(False) |
| 358 |
payment.create_invoices() |
| 359 |
invoice = sale_order.invoice_ids[0] |
| 360 |
invoice.action_post() |
| 361 |
myinvois_document = invoice._create_myinvois_document() |
| 362 |
|
| 363 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 364 |
self.assertFalse(errors) |
| 365 |
|
| 366 |
root = etree.fromstring(file) |
| 367 |
|
| 368 |
# Check the invoice type to endure that it is marked as credit note. |
| 369 |
node = root.xpath('cac:OrderReference', namespaces=NS_MAP) |
| 370 |
self.assertEqual(node, []) |
| 371 |
|
| 372 |
with file_open('l10n_my_edi/tests/expected_xmls/invoice_with_so.xml', 'rb') as f: |
| 373 |
expected_xml = etree.fromstring(f.read()) |
| 374 |
self.assertXmlTreeEqual(root, expected_xml) |
| 375 |
|
| 376 |
def test_06_foreigner(self): |
| 377 |
""" |
| 378 |
Check that the file is correct with a foreign customer. |
| 379 |
""" |
| 380 |
invoice = self.init_invoice( |
| 381 |
'out_invoice', partner=self.partner_b, products=self.product_a, post=True, |
| 382 |
) |
| 383 |
myinvois_document = invoice._create_myinvois_document() |
| 384 |
|
| 385 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 386 |
self.assertFalse(errors) |
| 387 |
|
| 388 |
root = etree.fromstring(file) |
| 389 |
customer_root = root.xpath('cac:AccountingCustomerParty/cac:Party', namespaces=NS_MAP)[0] |
| 390 |
|
| 391 |
# Party Identifications - TIN and BRN should be set. |
| 392 |
self._assert_node_values( |
| 393 |
customer_root, |
| 394 |
'cac:PartyIdentification/cbc:ID[@schemeID="TIN"]', |
| 395 |
self.partner_b.commercial_partner_id.vat, |
| 396 |
) |
| 397 |
self._assert_node_values( |
| 398 |
customer_root, |
| 399 |
'cac:PartyIdentification/cbc:ID[@schemeID="BRN"]', |
| 400 |
self.partner_b.commercial_partner_id.l10n_my_identification_number, |
| 401 |
) |
| 402 |
|
| 403 |
with file_open('l10n_my_edi/tests/expected_xmls/invoice_foreigner.xml', 'rb') as f: |
| 404 |
expected_xml = etree.fromstring(f.read()) |
| 405 |
self.assertXmlTreeEqual(root, expected_xml) |
| 406 |
|
| 407 |
def test_07_tax_exempt(self): |
| 408 |
""" |
| 409 |
Check that the file is correct with an exempt tax. |
| 410 |
""" |
| 411 |
exempt_tax = self.env['account.tax'].create({ |
| 412 |
'name': 'Tax Exempt', |
| 413 |
'type_tax_use': 'sale', |
| 414 |
'amount_type': 'percent', |
| 415 |
'amount': 0, |
| 416 |
'l10n_my_tax_type': 'E', |
| 417 |
}) |
| 418 |
invoice = self.init_invoice( |
| 419 |
'out_invoice', partner=self.partner_b, products=self.product_a, taxes=exempt_tax, post=True, |
| 420 |
) |
| 421 |
invoice.l10n_my_edi_exemption_reason = "Exempt Customer" |
| 422 |
myinvois_document = invoice._create_myinvois_document() |
| 423 |
|
| 424 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 425 |
self.assertFalse(errors) |
| 426 |
|
| 427 |
root = etree.fromstring(file) |
| 428 |
# The tax exemption info should be on the line. |
| 429 |
item_root = root.xpath('cac:InvoiceLine/cac:Item', namespaces=NS_MAP)[0] |
| 430 |
self._assert_node_values( |
| 431 |
item_root, |
| 432 |
'cac:ClassifiedTaxCategory/cbc:Name', |
| 433 |
invoice.l10n_my_edi_exemption_reason, |
| 434 |
) |
| 435 |
self._assert_node_values( |
| 436 |
item_root, |
| 437 |
'cac:ClassifiedTaxCategory/cbc:TaxExemptionReason', |
| 438 |
invoice.l10n_my_edi_exemption_reason, |
| 439 |
) |
| 440 |
# And also on the tax total |
| 441 |
tax_subtotal_root = root.xpath('cac:TaxTotal/cac:TaxSubtotal', namespaces=NS_MAP)[0] |
| 442 |
self._assert_node_values( |
| 443 |
tax_subtotal_root, |
| 444 |
'cac:TaxCategory/cbc:Name', |
| 445 |
invoice.l10n_my_edi_exemption_reason, |
| 446 |
) |
| 447 |
self._assert_node_values( |
| 448 |
tax_subtotal_root, |
| 449 |
'cac:TaxCategory/cbc:TaxExemptionReason', |
| 450 |
invoice.l10n_my_edi_exemption_reason, |
| 451 |
) |
| 452 |
|
| 453 |
with file_open('l10n_my_edi/tests/expected_xmls/invoice_tax_exempt.xml', 'rb') as f: |
| 454 |
expected_xml = etree.fromstring(f.read()) |
| 455 |
self.assertXmlTreeEqual(root, expected_xml) |
| 456 |
|
| 457 |
def test_08_self_billing(self): |
| 458 |
bill = self.init_invoice( |
| 459 |
'in_invoice', partner=self.partner_b, products=self.product_a, taxes=self.purchase_tax, post=True |
| 460 |
) |
| 461 |
myinvois_document = bill._create_myinvois_document() |
| 462 |
|
| 463 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 464 |
self.assertFalse(errors) |
| 465 |
|
| 466 |
root = etree.fromstring(file) |
| 467 |
# We assert that the supplier is the partner of the invoice, with all information present. |
| 468 |
supplier_root = root.xpath('cac:AccountingSupplierParty/cac:Party', namespaces=NS_MAP)[0] |
| 469 |
data_to_check = [ |
| 470 |
('cac:PartyIdentification/cbc:ID[@schemeID="TIN"]', 'EI00000000030'), # The partner_b malaysian TIN is set to the customer one, and it will be transformed to supplier during submission |
| 471 |
('cac:PartyIdentification/cbc:ID[@schemeID="BRN"]', self.partner_b.commercial_partner_id.l10n_my_identification_number), |
| 472 |
('cbc:IndustryClassificationCode', self.partner_b.commercial_partner_id.l10n_my_edi_industrial_classification.code), # It should use the code on the partner. |
| 473 |
('cac:PartyName/cbc:Name', self.partner_b.name), |
| 474 |
] |
| 475 |
for path, expected_value in data_to_check: |
| 476 |
self._assert_node_values(supplier_root, path, expected_value) |
| 477 |
# And that the customer is the company. |
| 478 |
customer_root = root.xpath('cac:AccountingCustomerParty/cac:Party', namespaces=NS_MAP)[0] |
| 479 |
data_to_check = [ |
| 480 |
('cac:PartyIdentification/cbc:ID[@schemeID="TIN"]', self.company_data['company'].vat), # We didn't set the new field as the company is malaysian, the vat should be in use. |
| 481 |
('cac:PartyIdentification/cbc:ID[@schemeID="BRN"]', self.company_data['company'].l10n_my_identification_number), |
| 482 |
('cac:PartyName/cbc:Name', self.company_data['company'].name), |
| 483 |
] |
| 484 |
for path, expected_value in data_to_check: |
| 485 |
self._assert_node_values(customer_root, path, expected_value) |
| 486 |
|
| 487 |
with file_open('l10n_my_edi/tests/expected_xmls/invoice_self_billing.xml', 'rb') as f: |
| 488 |
expected_xml = etree.fromstring(f.read()) |
| 489 |
self.assertXmlTreeEqual(root, expected_xml) |
| 490 |
|
| 491 |
def test_09_refund_note(self): |
| 492 |
""" A refund note is issued when an invoice has received a credit note, and that credit note was paid to the customer. """ |
| 493 |
# Create the original invoice, and receive the payment. |
| 494 |
invoice = self.init_invoice( |
| 495 |
'out_invoice', partner=self.partner_b, products=self.product_a, post=True, |
| 496 |
) |
| 497 |
invoice_document = invoice._create_myinvois_document() |
| 498 |
# Simulate that the document was sent |
| 499 |
invoice_document.write({ |
| 500 |
'myinvois_state': 'valid', |
| 501 |
'myinvois_external_uuid': '12345678912345678912345678', |
| 502 |
}) |
| 503 |
self.env['account.payment.register'].with_context(active_model='account.move', active_ids=invoice.ids).create({ |
| 504 |
'payment_date': '2019-01-02', |
| 505 |
})._create_payments() |
| 506 |
# Create the credit note, and pay it back. |
| 507 |
action = invoice.action_reverse() |
| 508 |
reversal_wizard = self.env[action['res_model']].with_context( |
| 509 |
active_ids=invoice.ids, |
| 510 |
active_model='account.move', |
| 511 |
default_journal_id=invoice.journal_id.id, |
| 512 |
).create({}) |
| 513 |
action = reversal_wizard.reverse_moves() |
| 514 |
credit_note = self.env['account.move'].browse(action['res_id']) |
| 515 |
credit_note.action_post() |
| 516 |
self.env['account.payment.register'].with_context(active_model='account.move', active_ids=credit_note.ids).create({ |
| 517 |
'payment_date': '2019-01-02', |
| 518 |
})._create_payments() |
| 519 |
# Generate the file and assert the type, should be "refund" (04) |
| 520 |
myinvois_document = credit_note._create_myinvois_document() |
| 521 |
|
| 522 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 523 |
self.assertFalse(errors) |
| 524 |
|
| 525 |
root = etree.fromstring(file) |
| 526 |
self._assert_node_values( |
| 527 |
root, |
| 528 |
'cbc:InvoiceTypeCode', |
| 529 |
'04', |
| 530 |
attributes={'listVersionID': '1.1'}, |
| 531 |
) |
| 532 |
|
| 533 |
with file_open('l10n_my_edi/tests/expected_xmls/refund_note_company_currency.xml', 'rb') as f: |
| 534 |
expected_xml = etree.fromstring(f.read()) |
| 535 |
self.assertXmlTreeEqual(root, expected_xml) |
| 536 |
|
| 537 |
def test_10_credit_note(self): |
| 538 |
""" A credit note is issued when an invoice has received a credit note, and that credit note was not paid to the customer. """ |
| 539 |
# Create the original invoice, don't receive a payment. |
| 540 |
invoice = self.init_invoice( |
| 541 |
'out_invoice', partner=self.partner_b, products=self.product_a, post=True, |
| 542 |
) |
| 543 |
invoice_document = invoice._create_myinvois_document() |
| 544 |
# Simulate that the document was sent |
| 545 |
invoice_document.write({ |
| 546 |
'myinvois_state': 'valid', |
| 547 |
'myinvois_external_uuid': '12345678912345678912345678', |
| 548 |
}) |
| 549 |
# Create the credit note to reduce the amount due of the invoice |
| 550 |
action = invoice.action_reverse() |
| 551 |
reversal_wizard = self.env[action['res_model']].with_context( |
| 552 |
active_ids=invoice.ids, |
| 553 |
active_model='account.move', |
| 554 |
default_journal_id=invoice.journal_id.id, |
| 555 |
).create({}) |
| 556 |
action = reversal_wizard.reverse_moves() |
| 557 |
credit_note = self.env['account.move'].browse(action['res_id']) |
| 558 |
with Form(credit_note) as credit_note_form: |
| 559 |
with credit_note_form.invoice_line_ids.edit(0) as line: |
| 560 |
line.price_unit = 500 |
| 561 |
credit_note.action_post() |
| 562 |
# Generate the file and assert the type, should be "credit note" (03) |
| 563 |
myinvois_document = credit_note._create_myinvois_document() |
| 564 |
|
| 565 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 566 |
self.assertFalse(errors) |
| 567 |
|
| 568 |
root = etree.fromstring(file) |
| 569 |
self._assert_node_values( |
| 570 |
root, |
| 571 |
'cbc:InvoiceTypeCode', |
| 572 |
'02', |
| 573 |
attributes={'listVersionID': '1.1'}, |
| 574 |
) |
| 575 |
|
| 576 |
with file_open('l10n_my_edi/tests/expected_xmls/credit_note_company_currency.xml', 'rb') as f: |
| 577 |
expected_xml = etree.fromstring(f.read()) |
| 578 |
self.assertXmlTreeEqual(root, expected_xml) |
| 579 |
|
| 580 |
def test_11_bill_imports_form(self): |
| 581 |
""" |
| 582 |
Ensure that when a bill contains a customs number; it is treated as an importation and not exportation. |
| 583 |
""" |
| 584 |
exempt_tax = self.env['account.tax'].create({ |
| 585 |
'name': 'Tax Exempt', |
| 586 |
'type_tax_use': 'sale', |
| 587 |
'amount_type': 'percent', |
| 588 |
'amount': 0, |
| 589 |
'l10n_my_tax_type': 'E', |
| 590 |
}) |
| 591 |
bill = self.init_invoice( |
| 592 |
'in_invoice', products=self.product_a, taxes=exempt_tax, post=True, |
| 593 |
) |
| 594 |
bill.write({ |
| 595 |
'l10n_my_edi_exemption_reason': 'Exempt Customer', |
| 596 |
'l10n_my_edi_custom_form_reference': 'E12345678912', |
| 597 |
}) |
| 598 |
myinvois_document = bill._create_myinvois_document() |
| 599 |
|
| 600 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 601 |
self.assertFalse(errors) |
| 602 |
|
| 603 |
root = etree.fromstring(file) |
| 604 |
|
| 605 |
self._assert_node_values( |
| 606 |
root, |
| 607 |
'cac:AdditionalDocumentReference[descendant::*[local-name() = "DocumentType"]]/cbc:DocumentType', |
| 608 |
'CustomsImportForm', |
| 609 |
) |
| 610 |
|
| 611 |
with file_open('l10n_my_edi/tests/expected_xmls/bill_import.xml', 'rb') as f: |
| 612 |
expected_xml = etree.fromstring(f.read()) |
| 613 |
self.assertXmlTreeEqual(root, expected_xml) |
| 614 |
|
| 615 |
def test_12_partner_ref_not_in_party_id(self): |
| 616 |
""" |
| 617 |
Ensure that when an invoice contains a customs number; it is treated as an importation and not exportation. |
| 618 |
""" |
| 619 |
invoice = self.init_invoice( |
| 620 |
'out_invoice', taxes=self.company_data['default_tax_sale'], products=self.product_a, post=True |
| 621 |
) |
| 622 |
myinvois_document = invoice._create_myinvois_document() |
| 623 |
|
| 624 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 625 |
self.assertFalse(errors) |
| 626 |
|
| 627 |
root = etree.fromstring(file) |
| 628 |
|
| 629 |
# There should not be any ID without attribute |
| 630 |
customer_root = root.xpath('cac:AccountingCustomerParty/cac:Party', namespaces=NS_MAP)[0] |
| 631 |
node = customer_root.xpath('cac:PartyIdentification/cbc:ID[count(@*)=0]', namespaces=NS_MAP) |
| 632 |
self.assertEqual(node, []) |
| 633 |
|
| 634 |
with file_open('l10n_my_edi/tests/expected_xmls/invoice_import.xml', 'rb') as f: |
| 635 |
expected_xml = etree.fromstring(f.read()) |
| 636 |
self.assertXmlTreeEqual(root, expected_xml) |
| 637 |
|
| 638 |
def test_13_prepaid_amount_present(self): |
| 639 |
""" |
| 640 |
Ensure the prepaid amount is present in the UBL XML under <cac:PrepaidPayment> |
| 641 |
""" |
| 642 |
invoice = self.init_invoice('out_invoice', currency=self.other_currency, taxes=self.company_data['default_tax_sale'], products=self.product_a, post=True) |
| 643 |
myinvois_document = invoice._create_myinvois_document() |
| 644 |
|
| 645 |
self.env['account.payment.register'].with_context(active_model='account.move', active_ids=invoice.ids).create({ |
| 646 |
'amount': 2200.00, 'payment_date': '2024-07-15' |
| 647 |
})._create_payments() |
| 648 |
|
| 649 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 650 |
self.assertFalse(errors) |
| 651 |
root = etree.fromstring(file) |
| 652 |
self._assert_node_values( |
| 653 |
root, |
| 654 |
'cac:PrepaidPayment/cbc:PaidAmount', |
| 655 |
'2200.00', |
| 656 |
attributes={'currencyID': self.other_currency.name} |
| 657 |
) |
| 658 |
|
| 659 |
def test_14_prioritize_invoice_line_classification_code(self): |
| 660 |
""" |
| 661 |
Check if the classification code of invoice line is prioritized over the classification code of product |
| 662 |
when the two codes differ. |
| 663 |
""" |
| 664 |
invoice = self.init_invoice( |
| 665 |
'out_invoice', taxes=self.company_data['default_tax_sale'], products=self.product_a, |
| 666 |
) |
| 667 |
invoice.line_ids[0].write({ |
| 668 |
'l10n_my_edi_classification_code': '002', |
| 669 |
}) |
| 670 |
invoice.action_post() |
| 671 |
myinvois_document = invoice._create_myinvois_document() |
| 672 |
|
| 673 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 674 |
self.assertFalse(errors) |
| 675 |
|
| 676 |
root = etree.fromstring(file) |
| 677 |
class_root = root.xpath('cac:InvoiceLine/cac:Item/cac:CommodityClassification', namespaces=NS_MAP)[0] |
| 678 |
|
| 679 |
self._assert_node_values( |
| 680 |
class_root, |
| 681 |
'cbc:ItemClassificationCode[@listID="CLASS"]', |
| 682 |
invoice.line_ids[0].l10n_my_edi_classification_code, |
| 683 |
) |
| 684 |
|
| 685 |
def test_15_none_tax(self): |
| 686 |
invoice = self.init_invoice( |
| 687 |
'out_invoice', |
| 688 |
partner=self.partner_b, |
| 689 |
products=self.product_a, |
| 690 |
post=False, |
| 691 |
) |
| 692 |
invoice.invoice_line_ids.write({'tax_ids': [Command.clear()]}) # remove existing taxes |
| 693 |
invoice.action_post() |
| 694 |
myinvois_document = invoice._create_myinvois_document() |
| 695 |
with self.assertRaises(UserError): |
| 696 |
myinvois_document.action_generate_xml_file() |
| 697 |
|
| 698 |
def test_16_original_document_id(self): |
| 699 |
""" |
| 700 |
Ensure the original document id is present in the reversed document. |
| 701 |
""" |
| 702 |
bill = self.init_invoice('in_invoice', products=self.product_a, taxes=self.company_data['default_tax_purchase'], post=True) |
| 703 |
bill.ref = 'BILL-123' |
| 704 |
myinvois_document = bill._create_myinvois_document() |
| 705 |
bill_file, errors = myinvois_document._myinvois_generate_xml_file() |
| 706 |
self.assertFalse(errors) |
| 707 |
|
| 708 |
myinvois_document.write({ |
| 709 |
'myinvois_file_id': self.env['ir.attachment'].create({'name': 'test myinvois', 'raw': bill_file}).id, |
| 710 |
'myinvois_state': 'valid', |
| 711 |
}) |
| 712 |
bill.l10n_my_edi_document_ids |= myinvois_document |
| 713 |
|
| 714 |
action = bill.action_reverse() |
| 715 |
reversal_wizard = self.env[action['res_model']].with_context( |
| 716 |
active_ids=bill.ids, |
| 717 |
active_model='account.move', |
| 718 |
default_journal_id=bill.journal_id.id, |
| 719 |
).create({}) |
| 720 |
action = reversal_wizard.reverse_moves() |
| 721 |
credit_note = self.env['account.move'].browse(action['res_id']) |
| 722 |
credit_note.action_post() |
| 723 |
|
| 724 |
myinvois_document = credit_note._create_myinvois_document() |
| 725 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 726 |
self.assertFalse(errors) |
| 727 |
|
| 728 |
root = etree.fromstring(file) |
| 729 |
self._assert_node_values( |
| 730 |
root, |
| 731 |
'cac:BillingReference/cac:InvoiceDocumentReference/cbc:ID', |
| 732 |
'BILL-123', |
| 733 |
) |
| 734 |
|
| 735 |
def test_17_original_document_id_from_xml(self): |
| 736 |
""" |
| 737 |
Ensure the original document id is present in the reversed document even if bill reference has changed after |
| 738 |
sending. |
| 739 |
""" |
| 740 |
bill = self.init_invoice('in_invoice', products=self.product_a, taxes=self.company_data['default_tax_purchase'], post=True) |
| 741 |
|
| 742 |
# Set reference before generating e-invoice |
| 743 |
bill.ref = 'Initial Reference' |
| 744 |
|
| 745 |
# Generate e-invoice and mock values |
| 746 |
myinvois_document = bill._create_myinvois_document() |
| 747 |
bill_file, errors = myinvois_document._myinvois_generate_xml_file() |
| 748 |
self.assertFalse(errors) |
| 749 |
|
| 750 |
myinvois_document.write({ |
| 751 |
'myinvois_file_id': self.env['ir.attachment'].create({'name': 'test myinvois', 'raw': bill_file}).id, |
| 752 |
'myinvois_state': 'valid', |
| 753 |
}) |
| 754 |
bill.l10n_my_edi_document_ids |= myinvois_document |
| 755 |
bill.ref = 'Some other reference' |
| 756 |
|
| 757 |
# Generate credit note |
| 758 |
action = bill.action_reverse() |
| 759 |
reversal_wizard = self.env[action['res_model']].with_context( |
| 760 |
active_ids=bill.ids, |
| 761 |
active_model='account.move', |
| 762 |
default_journal_id=bill.journal_id.id, |
| 763 |
).create({}) |
| 764 |
action = reversal_wizard.reverse_moves() |
| 765 |
credit_note = self.env['account.move'].browse(action['res_id']) |
| 766 |
credit_note.action_post() |
| 767 |
|
| 768 |
myinvois_document = credit_note._create_myinvois_document() |
| 769 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 770 |
self.assertFalse(errors) |
| 771 |
|
| 772 |
root = etree.fromstring(file) |
| 773 |
self._assert_node_values( |
| 774 |
root, |
| 775 |
'cac:BillingReference/cac:InvoiceDocumentReference/cbc:ID', |
| 776 |
'Initial Reference', |
| 777 |
) |
| 778 |
|
| 779 |
def test_18_downpayment(self): |
| 780 |
"""Test that a downpayment line will have their classification code correctly set to 022 (other)""" |
| 781 |
self.ensure_installed('sale') |
| 782 |
so = self.env["sale.order"].sudo().create( |
| 783 |
{ |
| 784 |
"partner_id": self.partner_b.id, |
| 785 |
"order_line": [Command.create({"product_id": self.product_a.id})], |
| 786 |
} |
| 787 |
) |
| 788 |
so.action_confirm() |
| 789 |
|
| 790 |
context = { |
| 791 |
"active_model": "sale.order", |
| 792 |
"active_ids": so.ids, |
| 793 |
"active_id": so.id, |
| 794 |
} |
| 795 |
downpayment_wizard = ( |
| 796 |
self.env["sale.advance.payment.inv"] |
| 797 |
.sudo() |
| 798 |
.with_context(context) |
| 799 |
.create( |
| 800 |
{ |
| 801 |
"advance_payment_method": "percentage", |
| 802 |
"amount": 50, |
| 803 |
} |
| 804 |
) |
| 805 |
) |
| 806 |
invoice = downpayment_wizard._create_invoices(sale_orders=so) |
| 807 |
invoice.action_post() |
| 808 |
myinvois_document = invoice._create_myinvois_document() |
| 809 |
|
| 810 |
file, errors = myinvois_document._myinvois_generate_xml_file() |
| 811 |
self.assertFalse(errors) |
| 812 |
root = etree.fromstring(file) |
| 813 |
|
| 814 |
self._assert_node_values( |
| 815 |
root, |
| 816 |
"cac:InvoiceLine/cac:Item/cac:CommodityClassification/cbc:ItemClassificationCode", |
| 817 |
"022", |
| 818 |
attributes={"listID": "CLASS"}, |
| 819 |
) |
| 820 |
|
| 821 |
def _assert_node_values(self, root, node_path, text, attributes=None): |
| 822 |
node = root.xpath(node_path, namespaces=NS_MAP) |
| 823 |
|
| 824 |
assert node, f'The requested node has not been found: {node_path}' |
| 825 |
|
| 826 |
# Ensure that we don't have duplicated nodes. As of writing, all tested nodes are expected to exist only once in the result. |
| 827 |
node = root.xpath(node_path, namespaces=NS_MAP) |
| 828 |
self.assertEqual(len(node), 1, f"The node {node[0].tag} has been found {len(node)} time in the file") |
| 829 |
|
| 830 |
self.assertEqual( |
| 831 |
node[0].text, |
| 832 |
text, |
| 833 |
) |
| 834 |
if attributes: |
| 835 |
for attribute, value in attributes.items(): |
| 836 |
self.assertEqual( |
| 837 |
node[0].attrib[attribute], |
| 838 |
value, |
| 839 |
) |