| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
import base64 |
| 3 |
|
| 4 |
from datetime import datetime |
| 5 |
from freezegun import freeze_time |
| 6 |
from lxml import etree |
| 7 |
from pytz import timezone |
| 8 |
from odoo import Command |
| 9 |
|
| 10 |
from odoo.exceptions import ValidationError, UserError |
| 11 |
from odoo.tests import tagged |
| 12 |
from odoo.tools import misc |
| 13 |
from odoo.addons.l10n_sa_edi.tests.common import TestSaEdiCommon |
| 14 |
|
| 15 |
|
| 16 |
@tagged('post_install_l10n', '-at_install', 'post_install') |
| 17 |
class TestEdiZatca(TestSaEdiCommon): |
| 18 |
# """Test ZATCA EDI compliance for Saudi Arabia.""" |
| 19 |
|
| 20 |
def _test_document_generation(self, test_file_path, expected_xpath, freeze_time_at, additional_xpath='', document_type=False, move=False, move_data=False): |
| 21 |
""" |
| 22 |
Common helper to test document generation against expected XML. |
| 23 |
""" |
| 24 |
with freeze_time(freeze_time_at): |
| 25 |
# Load expected XML |
| 26 |
expected_xml = misc.file_open(test_file_path, 'rb').read() |
| 27 |
expected_tree = self.get_xml_tree_from_string(expected_xml) |
| 28 |
expected_tree = self.with_applied_xpath(expected_tree, expected_xpath) |
| 29 |
|
| 30 |
creation_handlers = { |
| 31 |
"invoice": self._create_test_invoice, |
| 32 |
"credit_note": self._create_credit_note, |
| 33 |
"debit_note": self._create_debit_note, |
| 34 |
} |
| 35 |
|
| 36 |
if additional_xpath: |
| 37 |
expected_tree = self.with_applied_xpath(expected_tree, additional_xpath) |
| 38 |
|
| 39 |
if move: |
| 40 |
final_move = move |
| 41 |
elif move_data and document_type in creation_handlers: |
| 42 |
final_move = creation_handlers[document_type](**move_data) |
| 43 |
else: |
| 44 |
raise ValidationError("Either move or document_type + move_data need to be given") |
| 45 |
|
| 46 |
# Generate ZATCA XML |
| 47 |
if final_move.state != 'posted': |
| 48 |
final_move.action_post() |
| 49 |
|
| 50 |
final_move._l10n_sa_generate_unsigned_data() |
| 51 |
generated_file = self.env['account.edi.format']._l10n_sa_generate_zatca_template(final_move) |
| 52 |
current_tree = self.get_xml_tree_from_string(generated_file) |
| 53 |
current_tree = self.with_applied_xpath(current_tree, self.remove_ubl_extensions_xpath) |
| 54 |
|
| 55 |
# Assert |
| 56 |
self.assertXmlTreeEqual(current_tree, expected_tree) |
| 57 |
|
| 58 |
def testCreditNoteSimplified(self): |
| 59 |
"""Test simplified credit note generation.""" |
| 60 |
move_data = { |
| 61 |
'name': 'INV/2023/00034', |
| 62 |
'invoice_date': '2023-03-10', |
| 63 |
'invoice_date_due': '2023-03-10', |
| 64 |
'partner_id': self.partner_sa_simplified, |
| 65 |
'invoice_line_ids': [{ |
| 66 |
'product_id': self.product_burger.id, |
| 67 |
'price_unit': self.product_burger.standard_price, |
| 68 |
'quantity': 3, |
| 69 |
'tax_ids': self.tax_15.ids, |
| 70 |
}] |
| 71 |
} |
| 72 |
|
| 73 |
self._test_document_generation( |
| 74 |
document_type='credit_note', |
| 75 |
test_file_path='l10n_sa_edi/tests/compliance/simplified/credit.xml', |
| 76 |
expected_xpath=self.credit_note_applied_xpath, |
| 77 |
move_data=move_data, |
| 78 |
freeze_time_at=datetime(2023, 3, 10, 14, 59, 38, tzinfo=timezone('Etc/GMT-3')) |
| 79 |
) |
| 80 |
|
| 81 |
def testCreditNoteStandard(self): |
| 82 |
"""Test standard credit note generation.""" |
| 83 |
move_data = { |
| 84 |
'name': 'INV/2022/00014', |
| 85 |
'invoice_date': '2022-09-05', |
| 86 |
'invoice_date_due': '2022-09-22', |
| 87 |
'partner_id': self.partner_sa, |
| 88 |
'invoice_line_ids': [{ |
| 89 |
'product_id': self.product_a.id, |
| 90 |
'price_unit': self.product_a.standard_price, |
| 91 |
'tax_ids': self.tax_15.ids, |
| 92 |
}] |
| 93 |
} |
| 94 |
|
| 95 |
additional_xpath = ''' |
| 96 |
<xpath expr="(//*[local-name()='AdditionalDocumentReference']/*[local-name()='UUID'])[1]" position="replace"> |
| 97 |
<cbc:UUID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:UUID> |
| 98 |
</xpath> |
| 99 |
''' |
| 100 |
|
| 101 |
self._test_document_generation( |
| 102 |
document_type='credit_note', |
| 103 |
test_file_path='l10n_sa_edi/tests/compliance/standard/credit.xml', |
| 104 |
expected_xpath=self.credit_note_applied_xpath, |
| 105 |
move_data=move_data, |
| 106 |
freeze_time_at=datetime(2022, 9, 5, 9, 39, 15, tzinfo=timezone('Etc/GMT-3')), |
| 107 |
additional_xpath=additional_xpath |
| 108 |
) |
| 109 |
|
| 110 |
def testDebitNoteSimplified(self): |
| 111 |
"""Test simplified debit note generation.""" |
| 112 |
move_data = { |
| 113 |
'name': 'INV/2023/00034', |
| 114 |
'invoice_date': '2023-03-10', |
| 115 |
'invoice_date_due': '2023-03-10', |
| 116 |
'partner_id': self.partner_sa_simplified, |
| 117 |
'invoice_line_ids': [{ |
| 118 |
'product_id': self.product_burger.id, |
| 119 |
'price_unit': self.product_burger.standard_price, |
| 120 |
'quantity': 2, |
| 121 |
'tax_ids': self.tax_15.ids, |
| 122 |
}] |
| 123 |
} |
| 124 |
|
| 125 |
self._test_document_generation( |
| 126 |
document_type='debit_note', |
| 127 |
test_file_path='l10n_sa_edi/tests/compliance/simplified/debit.xml', |
| 128 |
expected_xpath=self.debit_note_applied_xpath, |
| 129 |
move_data=move_data, |
| 130 |
freeze_time_at=datetime(2023, 3, 10, 15, 1, 46, tzinfo=timezone('Etc/GMT-3')) |
| 131 |
) |
| 132 |
|
| 133 |
def testDebitNoteStandard(self): |
| 134 |
"""Test standard debit note generation.""" |
| 135 |
move_data = { |
| 136 |
'name': 'INV/2022/00001', |
| 137 |
'invoice_date': '2022-09-05', |
| 138 |
'invoice_date_due': '2022-09-22', |
| 139 |
'partner_id': self.partner_sa, |
| 140 |
'invoice_line_ids': [{ |
| 141 |
'product_id': self.product_b.id, |
| 142 |
'price_unit': self.product_b.standard_price, |
| 143 |
'tax_ids': self.tax_15.ids, |
| 144 |
}] |
| 145 |
} |
| 146 |
|
| 147 |
additional_xpath = ''' |
| 148 |
<xpath expr="(//*[local-name()='AdditionalDocumentReference']/*[local-name()='UUID'])[1]" position="replace"> |
| 149 |
<cbc:UUID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:UUID> |
| 150 |
</xpath> |
| 151 |
''' |
| 152 |
|
| 153 |
self._test_document_generation( |
| 154 |
document_type='debit_note', |
| 155 |
test_file_path='l10n_sa_edi/tests/compliance/standard/debit.xml', |
| 156 |
expected_xpath=self.debit_note_applied_xpath, |
| 157 |
move_data=move_data, |
| 158 |
freeze_time_at=datetime(2022, 9, 5, 9, 45, 27, tzinfo=timezone('Etc/GMT-3')), |
| 159 |
additional_xpath=additional_xpath |
| 160 |
) |
| 161 |
|
| 162 |
def testInvoiceSimplified(self): |
| 163 |
"""Test simplified invoice generation.""" |
| 164 |
move_data = { |
| 165 |
'name': 'INV/2023/00034', |
| 166 |
'invoice_date': '2023-03-10', |
| 167 |
'invoice_date_due': '2023-03-10', |
| 168 |
'partner_id': self.partner_sa_simplified, |
| 169 |
'invoice_line_ids': [{ |
| 170 |
'product_id': self.product_burger.id, |
| 171 |
'price_unit': self.product_burger.standard_price, |
| 172 |
'quantity': 3, |
| 173 |
'tax_ids': self.tax_15.ids, |
| 174 |
}] |
| 175 |
} |
| 176 |
|
| 177 |
self._test_document_generation( |
| 178 |
document_type='invoice', |
| 179 |
test_file_path='l10n_sa_edi/tests/compliance/simplified/invoice.xml', |
| 180 |
expected_xpath=self.invoice_applied_xpath, |
| 181 |
move_data=move_data, |
| 182 |
freeze_time_at=datetime(2023, 3, 10, 14, 56, 55, tzinfo=timezone('Etc/GMT-3')) |
| 183 |
) |
| 184 |
|
| 185 |
def testInvoiceStandard(self): |
| 186 |
"""Test standard invoice generation.""" |
| 187 |
move_data = { |
| 188 |
'name': 'INV/2022/00014', |
| 189 |
'invoice_date': '2022-09-05', |
| 190 |
'invoice_date_due': '2022-09-22', |
| 191 |
'partner_id': self.partner_sa, |
| 192 |
'invoice_line_ids': [{ |
| 193 |
'product_id': self.product_a.id, |
| 194 |
'price_unit': self.product_a.standard_price, |
| 195 |
'tax_ids': self.tax_15.ids, |
| 196 |
}] |
| 197 |
} |
| 198 |
|
| 199 |
self._test_document_generation( |
| 200 |
document_type='invoice', |
| 201 |
test_file_path='l10n_sa_edi/tests/compliance/standard/invoice.xml', |
| 202 |
expected_xpath=self.invoice_applied_xpath, |
| 203 |
move_data=move_data, |
| 204 |
freeze_time_at=datetime(2022, 9, 5, 8, 20, 2, tzinfo=timezone('Etc/GMT-3')) |
| 205 |
) |
| 206 |
|
| 207 |
def testInvoiceWithDownpayment(self): |
| 208 |
"""Test invoice generation with downpayment scenarios.""" |
| 209 |
if 'sale' not in self.env["ir.module.module"]._installed(): |
| 210 |
self.skipTest("Sale module is not installed") |
| 211 |
self.env.user.group_ids += self.env.ref('sales_team.group_sale_salesman') |
| 212 |
|
| 213 |
freeze = datetime(2022, 9, 5, 8, 20, 2, tzinfo=timezone('Etc/GMT-3')) |
| 214 |
|
| 215 |
# Helper to test generated files |
| 216 |
saudi_pricelist = self.env['product.pricelist'].create({ |
| 217 |
'name': 'SAR', |
| 218 |
'currency_id': self.env.ref('base.SAR').id |
| 219 |
}) |
| 220 |
with freeze_time(freeze): |
| 221 |
sale_order = self.env['sale.order'].sudo().create({ |
| 222 |
'partner_id': self.partner_sa.id, |
| 223 |
'pricelist_id': saudi_pricelist.id, |
| 224 |
'order_line': [ |
| 225 |
Command.create({ |
| 226 |
'product_id': self.product_a.id, |
| 227 |
'price_unit': 1000, |
| 228 |
'product_uom_qty': 1, |
| 229 |
'tax_ids': [Command.set(self.tax_15.ids)], |
| 230 |
}) |
| 231 |
] |
| 232 |
}).sudo(False) |
| 233 |
sale_order.action_confirm() |
| 234 |
|
| 235 |
# Context for wizards |
| 236 |
context = { |
| 237 |
'active_model': 'sale.order', |
| 238 |
'active_ids': [sale_order.id], |
| 239 |
'active_id': sale_order.id, |
| 240 |
'default_journal_id': self.customer_invoice_journal.id, |
| 241 |
} |
| 242 |
|
| 243 |
# Create downpayment invoice |
| 244 |
downpayment_wizard = self.env['sale.advance.payment.inv'].with_context(context).sudo().create({ |
| 245 |
'advance_payment_method': 'fixed', |
| 246 |
'fixed_amount': 115, |
| 247 |
}) |
| 248 |
downpayment = downpayment_wizard._create_invoices(sale_order) |
| 249 |
downpayment.invoice_date_due = '2022-09-22' |
| 250 |
|
| 251 |
# Create final invoice |
| 252 |
final_wizard = self.env['sale.advance.payment.inv'].with_context(context).sudo().create({}) |
| 253 |
final = final_wizard._create_invoices(sale_order) |
| 254 |
final.invoice_line_ids.filtered('is_downpayment').name = 'Down Payment' |
| 255 |
final.invoice_date_due = '2022-09-22' |
| 256 |
|
| 257 |
# Test invoices |
| 258 |
additional_xpath = f''' |
| 259 |
<xpath expr="(//*[local-name()='PaymentMeans']/*[local-name()='InstructionID'])" position="after"> |
| 260 |
<cbc:InstructionNote xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">{sale_order.name}</cbc:InstructionNote> |
| 261 |
</xpath> |
| 262 |
''' |
| 263 |
for move, test_file in [ |
| 264 |
(downpayment, "downpayment_invoice"), |
| 265 |
(final, "final_invoice") |
| 266 |
]: |
| 267 |
with self.subTest(move=move, test_file=test_file): |
| 268 |
self._test_document_generation( |
| 269 |
test_file_path=f'l10n_sa_edi/tests/test_files/{test_file}.xml', |
| 270 |
expected_xpath=self.invoice_applied_xpath, |
| 271 |
additional_xpath=additional_xpath, |
| 272 |
freeze_time_at=freeze, |
| 273 |
move=move, |
| 274 |
) |
| 275 |
|
| 276 |
# Test credit notes |
| 277 |
for move, test_file in [ |
| 278 |
(downpayment, "downpayment_credit_note"), |
| 279 |
(final, "final_credit_note") |
| 280 |
]: |
| 281 |
with self.subTest(move=move, test_file=test_file): |
| 282 |
# Create refund |
| 283 |
wiz_context = { |
| 284 |
'active_model': 'account.move', |
| 285 |
'active_ids': [move.id], |
| 286 |
'default_journal_id': move.journal_id.id, |
| 287 |
} |
| 288 |
refund_wizard = self.env['account.move.reversal'].with_context(wiz_context).create({ |
| 289 |
'l10n_sa_reason': 'BR-KSA-17-reason-5', |
| 290 |
'date': '2022-09-05', |
| 291 |
}) |
| 292 |
refund_invoice = self.env['account.move'].browse(refund_wizard.reverse_moves()['res_id']) |
| 293 |
refund_invoice.invoice_date_due = '2022-09-22' |
| 294 |
self._test_document_generation( |
| 295 |
test_file_path=f'l10n_sa_edi/tests/test_files/{test_file}.xml', |
| 296 |
expected_xpath=self.credit_note_applied_xpath, |
| 297 |
freeze_time_at=freeze, |
| 298 |
move=refund_invoice, |
| 299 |
) |
| 300 |
|
| 301 |
def testInvoiceWithRetention(self): |
| 302 |
"""Test standard invoice generation.""" |
| 303 |
|
| 304 |
retention_tax = self.env['account.tax'].create({ |
| 305 |
'l10n_sa_is_retention': True, |
| 306 |
'name': 'Retention Tax', |
| 307 |
'amount_type': 'percent', |
| 308 |
'amount': -10.0, |
| 309 |
}) |
| 310 |
|
| 311 |
move_data = { |
| 312 |
'name': 'INV/2022/00014', |
| 313 |
'invoice_date': '2022-09-05', |
| 314 |
'invoice_date_due': '2022-09-22', |
| 315 |
'partner_id': self.partner_sa, |
| 316 |
'invoice_line_ids': [{ |
| 317 |
'product_id': self.product_a.id, |
| 318 |
'price_unit': self.product_a.standard_price, |
| 319 |
'tax_ids': self.tax_15.ids + retention_tax.ids, |
| 320 |
}] |
| 321 |
} |
| 322 |
|
| 323 |
self._test_document_generation( |
| 324 |
document_type='invoice', |
| 325 |
test_file_path='l10n_sa_edi/tests/compliance/standard/invoice.xml', |
| 326 |
expected_xpath=self.invoice_applied_xpath, |
| 327 |
move_data=move_data, |
| 328 |
freeze_time_at=datetime(2022, 9, 5, 8, 20, 2, tzinfo=timezone('Etc/GMT-3')) |
| 329 |
) |
| 330 |
|
| 331 |
def testCompanyOnSimplifiedInvoiceQR(self): |
| 332 |
move_data = { |
| 333 |
'name': 'INV/2025/00012', |
| 334 |
'invoice_date': '2025-07-05', |
| 335 |
'invoice_date_due': '2025-07-12', |
| 336 |
'company_id': self.sa_branch, |
| 337 |
'partner_id': self.partner_sa_simplified, |
| 338 |
'invoice_line_ids': [{ |
| 339 |
'product_id': self.product_a.id, |
| 340 |
'price_unit': self.product_a.standard_price, |
| 341 |
'tax_ids': self.tax_15.ids, |
| 342 |
}], |
| 343 |
} |
| 344 |
|
| 345 |
# Fetch company name from xml |
| 346 |
invoice = self._create_test_invoice(**move_data) |
| 347 |
invoice.action_post() |
| 348 |
xml_content = self.env['account.edi.format']._l10n_sa_generate_zatca_template(invoice) |
| 349 |
xml_root = etree.fromstring(xml_content) |
| 350 |
xml_company_name = xml_root.xpath( |
| 351 |
"//cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name", |
| 352 |
namespaces=self.env['account.edi.xml.ubl_21.zatca']._l10n_sa_get_namespaces() |
| 353 |
)[0].text.strip() |
| 354 |
|
| 355 |
# Fetch company name from QR code |
| 356 |
# Format: Tag (1 Byte) - Length (1 Byte) - Value |
| 357 |
invoice._l10n_sa_generate_unsigned_data() |
| 358 |
decoded_qr = base64.b64decode(invoice.l10n_sa_qr_code_str) |
| 359 |
length = decoded_qr[1] |
| 360 |
qr_company_name = decoded_qr[2:2 + length].decode() |
| 361 |
|
| 362 |
self.assertEqual(xml_company_name, qr_company_name, "Seller name on the xml does not match the seller name on the QR code") |
| 363 |
|
| 364 |
def test_company_missing_country_on_standard_invoice(self): |
| 365 |
"""Test standard invoice generation when the company does not have a country set.""" |
| 366 |
# setup new company to prevent errors in other tests |
| 367 |
vals = self._get_company_vals({"name": "SA Company (Minus Country)"}) |
| 368 |
new_company = self._create_company(**vals) |
| 369 |
|
| 370 |
new_company_customer_invoice_journal = self.env['account.journal'].search([ |
| 371 |
('company_id', '=', new_company.id), |
| 372 |
('type', '=', 'sale'), |
| 373 |
], limit=1) |
| 374 |
new_company_customer_invoice_journal._l10n_sa_load_edi_demo_data() |
| 375 |
|
| 376 |
new_company.country_id = False |
| 377 |
|
| 378 |
# missing tax should always cause a user error, even if the country is blank |
| 379 |
move_data = { |
| 380 |
'name': 'INV/2022/00014', |
| 381 |
'invoice_date': '2022-09-05', |
| 382 |
'invoice_date_due': '2022-09-22', |
| 383 |
'company_id': new_company, |
| 384 |
'partner_id': self.partner_sa, |
| 385 |
'invoice_line_ids': [{ |
| 386 |
'product_id': self.product_a.id, |
| 387 |
'price_unit': self.product_a.standard_price, |
| 388 |
'tax_ids': False, |
| 389 |
}], |
| 390 |
} |
| 391 |
|
| 392 |
invoice = self._create_test_invoice(**move_data) |
| 393 |
with self.assertRaises(UserError): |
| 394 |
invoice.action_post() |
| 395 |
|
| 396 |
def test_zatca_xml_price_amount_precision(self): |
| 397 |
""" |
| 398 |
Test that PriceAmount has 10 decimal precision to satisfy ZATCA validation BR-KSA-EN16931-11 |
| 399 |
""" |
| 400 |
|
| 401 |
self.tax_15.write({ |
| 402 |
'price_include_override': 'tax_included', |
| 403 |
}) |
| 404 |
move_data = { |
| 405 |
'name': 'INV/2025/00013', |
| 406 |
'invoice_date': '2025-01-15', |
| 407 |
'invoice_date_due': '2025-01-15', |
| 408 |
'partner_id': self.partner_sa, |
| 409 |
'invoice_line_ids': [{ |
| 410 |
'product_id': self.product_a.id, |
| 411 |
'price_unit': 200.0, |
| 412 |
'quantity': 7, |
| 413 |
'tax_ids': self.tax_15.ids, |
| 414 |
}] |
| 415 |
} |
| 416 |
invoice = self._create_test_invoice(**move_data) |
| 417 |
invoice.action_post() |
| 418 |
|
| 419 |
# Generate XML |
| 420 |
xml_content = self.env['account.edi.format']._l10n_sa_generate_zatca_template(invoice) |
| 421 |
xml_root = etree.fromstring(xml_content) |
| 422 |
|
| 423 |
# Get PriceAmount from XML |
| 424 |
price_amount_nodes = xml_root.xpath( |
| 425 |
"//cac:InvoiceLine/cac:Price/cbc:PriceAmount", |
| 426 |
namespaces=self.env['account.edi.xml.ubl_21.zatca']._l10n_sa_get_namespaces() |
| 427 |
) |
| 428 |
self.assertTrue(price_amount_nodes, "PriceAmount node not found in XML") |
| 429 |
price_amount_str = price_amount_nodes[0].text |
| 430 |
self.assertEqual(price_amount_str, '173.9128571429') |
| 431 |
|
| 432 |
def test_zatca_xml_line_rounding_amount_consistency(self): |
| 433 |
"""Test that LineExtensionAmount + TaxAmount = RoundingAmount for each invoice line.""" |
| 434 |
self.tax_15.price_include_override = 'tax_included' |
| 435 |
invoice = self._create_test_invoice( |
| 436 |
name='INV/2022/00001', |
| 437 |
invoice_date='2022-09-05', |
| 438 |
invoice_date_due='2022-09-22', |
| 439 |
partner_id=self.partner_sa, |
| 440 |
invoice_line_ids=[ |
| 441 |
{ |
| 442 |
'product_id': self.product_a.id, |
| 443 |
'price_unit': 18.0, |
| 444 |
'tax_ids': self.tax_15.ids, |
| 445 |
}, |
| 446 |
{ |
| 447 |
'product_id': self.product_b.id, |
| 448 |
'price_unit': 14.0, |
| 449 |
'tax_ids': self.tax_15.ids, |
| 450 |
} |
| 451 |
] |
| 452 |
) |
| 453 |
invoice.action_post() |
| 454 |
xml_content = self.env['account.edi.format']._l10n_sa_generate_zatca_template(invoice) |
| 455 |
xml_root = etree.fromstring(xml_content) |
| 456 |
namespaces = self.env['account.edi.xml.ubl_21.zatca']._l10n_sa_get_namespaces() |
| 457 |
|
| 458 |
for line in xml_root.xpath('//cac:InvoiceLine', namespaces=namespaces): |
| 459 |
line_ext = float(line.xpath('cbc:LineExtensionAmount/text()', namespaces=namespaces)[0]) |
| 460 |
tax_amt = float(line.xpath('cac:TaxTotal/cbc:TaxAmount/text()', namespaces=namespaces)[0]) |
| 461 |
rounding_amt = float(line.xpath('cac:TaxTotal/cbc:RoundingAmount/text()', namespaces=namespaces)[0]) |
| 462 |
self.assertEqual(line_ext + tax_amt, rounding_amt, |
| 463 |
msg=f"LineExtensionAmount ({line_ext}) + TaxAmount ({tax_amt}) != RoundingAmount ({rounding_amt})") |
| 464 |
|
| 465 |
def test_csr_generation_compliant_company(self): |
| 466 |
"""Test that CSR generation succeeds for a compliant company with valid field lengths.""" |
| 467 |
compliant_company = self.env['res.company'].create({ |
| 468 |
'name': 'Valid Company Name', |
| 469 |
'vat': '300000000000003', |
| 470 |
'street': 'Short Street Name', |
| 471 |
'city': 'Riyadh', |
| 472 |
'zip': '12345', |
| 473 |
'country_id': self.saudi_arabia.id, |
| 474 |
'state_id': self.riyadh.id, |
| 475 |
'l10n_sa_api_mode': 'sandbox', |
| 476 |
'currency_id': self.env.ref('base.SAR').id, |
| 477 |
}) |
| 478 |
compliant_company.partner_id.industry_id = self.env['res.partner.industry'].create({ |
| 479 |
'name': 'Technology', |
| 480 |
}) |
| 481 |
compliant_company.l10n_sa_private_key_id = self.env['certificate.key'].sudo()._generate_ec_private_key( |
| 482 |
compliant_company, name='Test private key' |
| 483 |
) |
| 484 |
compliant_journal = self.env['account.journal'].create({ |
| 485 |
'name': 'Sales', |
| 486 |
'code': 'SAL', |
| 487 |
'type': 'sale', |
| 488 |
'company_id': compliant_company.id, |
| 489 |
}) |
| 490 |
|
| 491 |
try: |
| 492 |
csr_string = self.env['certificate.certificate'].sudo()._l10n_sa_get_csr_str(compliant_journal) |
| 493 |
self.assertTrue(csr_string, "a Valid CSR should not be empty") |
| 494 |
except UserError as e: |
| 495 |
self.fail(f"Compliant company should not raise error: {e}") |
| 496 |
|
| 497 |
def test_csr_generation_non_compliant_company(self): |
| 498 |
"""Test that CSR generation fails for non-compliant company with all invalid fields listed.""" |
| 499 |
long_name = "A" * 70 |
| 500 |
long_street = "B" * 70 |
| 501 |
long_city = "C" * 70 |
| 502 |
long_state_name = "D" * 70 |
| 503 |
long_industry_name = "E" * 70 |
| 504 |
long_journal_name = "F" * 70 |
| 505 |
|
| 506 |
long_state = self.env['res.country.state'].create({ |
| 507 |
'name': long_state_name, |
| 508 |
'code': 'LST', |
| 509 |
'country_id': self.saudi_arabia.id, |
| 510 |
}) |
| 511 |
long_industry = self.env['res.partner.industry'].create({ |
| 512 |
'name': long_industry_name, |
| 513 |
}) |
| 514 |
|
| 515 |
non_compliant_company = self.env['res.company'].create({ |
| 516 |
'name': long_name, |
| 517 |
'vat': '333333333333333', |
| 518 |
'street': long_street, |
| 519 |
'city': long_city, |
| 520 |
'zip': '12345', |
| 521 |
'country_id': self.saudi_arabia.id, |
| 522 |
'state_id': long_state.id, |
| 523 |
'l10n_sa_api_mode': 'sandbox', |
| 524 |
'currency_id': self.env.ref('base.SAR').id, |
| 525 |
}) |
| 526 |
non_compliant_company.partner_id.industry_id = long_industry |
| 527 |
non_compliant_company.l10n_sa_private_key_id = self.env['certificate.key'].sudo()._generate_ec_private_key( |
| 528 |
non_compliant_company, name='Test private key' |
| 529 |
) |
| 530 |
non_compliant_journal = self.env['account.journal'].create({ |
| 531 |
'name': long_journal_name, |
| 532 |
'code': 'NC', |
| 533 |
'type': 'sale', |
| 534 |
'company_id': non_compliant_company.id, |
| 535 |
}) |
| 536 |
|
| 537 |
with self.assertRaises(UserError) as context: |
| 538 |
self.env['certificate.certificate'].sudo()._l10n_sa_get_csr_str(non_compliant_journal) |
| 539 |
|
| 540 |
error_message = str(context.exception) |
| 541 |
expected_error_fields = [ |
| 542 |
"Company Name", |
| 543 |
"Common Name", |
| 544 |
"Street", |
| 545 |
"Locality Name", |
| 546 |
"State/Province Name", |
| 547 |
"Partner Industry Name", |
| 548 |
] |
| 549 |
|
| 550 |
for field_name in expected_error_fields: |
| 551 |
self.assertIn(field_name, error_message, f"Error message should contain '{field_name}'") |