| 1 |
# coding: utf-8 |
| 2 |
import json |
| 3 |
from base64 import b64decode |
| 4 |
|
| 5 |
from odoo import Command |
| 6 |
from odoo.tests import tagged |
| 7 |
from odoo.addons.account_edi.tests.common import AccountEdiTestCommon |
| 8 |
|
| 9 |
|
| 10 |
@tagged('post_install_l10n', '-at_install', 'post_install') |
| 11 |
class TestSaEdiCommon(AccountEdiTestCommon): |
| 12 |
""" |
| 13 |
Base test class for Saudi Arabia EDI functionality. |
| 14 |
|
| 15 |
Sets up test data for ZATCA (Saudi tax authority) compliance testing including: |
| 16 |
- Company with Saudi-specific fields |
| 17 |
- Partners (company and individual) |
| 18 |
- Products and taxes |
| 19 |
- XPath templates for XML comparison |
| 20 |
""" |
| 21 |
|
| 22 |
@classmethod |
| 23 |
@AccountEdiTestCommon.setup_edi_format('l10n_sa_edi.edi_sa_zatca') |
| 24 |
@AccountEdiTestCommon.setup_chart_template('sa') |
| 25 |
@AccountEdiTestCommon.setup_country('sa') |
| 26 |
def setUpClass(cls): |
| 27 |
super().setUpClass() |
| 28 |
|
| 29 |
# Setup frequently used references |
| 30 |
cls.company = cls.company_data['company'] |
| 31 |
cls.saudi_arabia = cls.env.ref('base.sa') |
| 32 |
cls.riyadh = cls._get_or_create_state('Riyadh', 'RUH', cls.saudi_arabia) |
| 33 |
|
| 34 |
# Setup test data |
| 35 |
cls._setup_company() |
| 36 |
cls._setup_branches() |
| 37 |
cls._setup_partners() |
| 38 |
cls._setup_products() |
| 39 |
cls._setup_taxes() |
| 40 |
cls._setup_journal() |
| 41 |
cls._setup_xpath_templates() |
| 42 |
|
| 43 |
@classmethod |
| 44 |
def _get_company_vals(cls, defaults=None): |
| 45 |
return { |
| 46 |
'name': 'SA Company Test', |
| 47 |
'email': '[email protected]', |
| 48 |
'phone': '+966 51 234 5678', |
| 49 |
'vat': '311111111111113', |
| 50 |
# Address fields |
| 51 |
'street': 'Al Amir Mohammed Bin Abdul Aziz Street', |
| 52 |
'street2': 'Testomania', |
| 53 |
'city': 'المدينة المنورة', |
| 54 |
'zip': '42317', |
| 55 |
'country_id': cls.saudi_arabia.id, |
| 56 |
'state_id': cls.riyadh.id, |
| 57 |
# Saudi-specific fields |
| 58 |
'l10n_sa_edi_building_number': '1234', |
| 59 |
'l10n_sa_edi_plot_identification': '1234', |
| 60 |
'l10n_sa_edi_additional_identification_number': '2525252525252', |
| 61 |
'l10n_sa_edi_additional_identification_scheme': 'CRN', # Commercial Registration Number |
| 62 |
**(defaults or {}) |
| 63 |
} |
| 64 |
|
| 65 |
@classmethod |
| 66 |
def _setup_company(cls): |
| 67 |
"""Configure the test company with Saudi Arabia specific settings.""" |
| 68 |
cls.company.write(cls._get_company_vals()) |
| 69 |
|
| 70 |
@classmethod |
| 71 |
def _setup_branches(cls): |
| 72 |
vals = cls._get_company_vals({"name": "SA Branch", "parent_id": cls.company.id}) |
| 73 |
cls.sa_branch = cls._create_company(**vals) |
| 74 |
|
| 75 |
@classmethod |
| 76 |
def _setup_partners(cls): |
| 77 |
"""Create test partners for different invoice types.""" |
| 78 |
# Standard invoice partner (company) |
| 79 |
cls.partner_sa = cls._create_saudi_company_partner() |
| 80 |
|
| 81 |
# Simplified invoice partner (individual) |
| 82 |
cls.partner_sa_simplified = cls._create_saudi_individual_partner() |
| 83 |
|
| 84 |
@classmethod |
| 85 |
def _create_saudi_company_partner(cls): |
| 86 |
"""Create a Saudi company partner with full ZATCA requirements.""" |
| 87 |
return cls.env['res.partner'].create({ |
| 88 |
'name': 'Saud Ahmed', |
| 89 |
'ref': 'Saudi Aramco', |
| 90 |
'company_type': 'company', |
| 91 |
'lang': 'en_US', |
| 92 |
# Contact info |
| 93 |
'email': '[email protected]', |
| 94 |
'phone': '+966556666666', |
| 95 |
# Tax info |
| 96 |
'vat': '311111111111113', |
| 97 |
'l10n_sa_edi_additional_identification_scheme': 'CRN', |
| 98 |
'l10n_sa_edi_additional_identification_number': '353535353535353', |
| 99 |
# Address |
| 100 |
'street': '4557 King Salman St', |
| 101 |
'street2': 'Neighbor!', |
| 102 |
'city': 'Riyadh', |
| 103 |
'zip': '94538', |
| 104 |
'state_id': cls.riyadh.id, |
| 105 |
'country_id': cls.saudi_arabia.id, |
| 106 |
# Saudi-specific address fields |
| 107 |
'l10n_sa_edi_building_number': '12300', |
| 108 |
'l10n_sa_edi_plot_identification': '2323', |
| 109 |
}) |
| 110 |
|
| 111 |
@classmethod |
| 112 |
def _create_saudi_individual_partner(cls): |
| 113 |
"""Create a Saudi individual partner for simplified invoices.""" |
| 114 |
return cls.env['res.partner'].create({ |
| 115 |
'name': 'Mohammed Ali', |
| 116 |
'ref': 'Mohammed Ali', |
| 117 |
'company_type': 'person', |
| 118 |
'lang': 'en_US', |
| 119 |
'country_id': cls.saudi_arabia.id, |
| 120 |
'state_id': cls.riyadh.id, |
| 121 |
# Simplified invoices use different ID schemes |
| 122 |
'l10n_sa_edi_additional_identification_scheme': 'MOM', # Momra License |
| 123 |
'l10n_sa_edi_additional_identification_number': '3123123213131', |
| 124 |
}) |
| 125 |
|
| 126 |
@classmethod |
| 127 |
def _setup_products(cls): |
| 128 |
"""Create test products.""" |
| 129 |
cls.product_a = cls._create_product(name='Product A', standard_price=320.0, default_code='P0001') |
| 130 |
cls.product_b = cls._create_product(name='Product B', standard_price=15.8, default_code='P0002') |
| 131 |
cls.product_burger = cls._create_product(name='Burger', standard_price=265.0) |
| 132 |
|
| 133 |
@classmethod |
| 134 |
def _setup_taxes(cls): |
| 135 |
"""Setup tax references.""" |
| 136 |
# Standard 15% VAT in Saudi Arabia |
| 137 |
cls.tax_15 = cls.env['account.tax'].search([ |
| 138 |
('company_id', '=', cls.company.id), |
| 139 |
('amount', '=', 15.0) |
| 140 |
], limit=1) |
| 141 |
|
| 142 |
@classmethod |
| 143 |
def _setup_journal(cls): |
| 144 |
"""Setup and configure the sales journal.""" |
| 145 |
cls.customer_invoice_journal = cls.env['account.journal'].search([ |
| 146 |
('company_id', '=', cls.company.id), |
| 147 |
('type', '=', 'sale') |
| 148 |
], limit=1) |
| 149 |
|
| 150 |
# Load ZATCA demo data (certificates, etc.) |
| 151 |
cls.customer_invoice_journal._l10n_sa_load_edi_demo_data() |
| 152 |
PCSID_Data = json.loads(cls.customer_invoice_journal.l10n_sa_production_csid_json) |
| 153 |
pcsid_certificate = cls.env['certificate.certificate'].create({ |
| 154 |
'name': 'PCSID Certificate', |
| 155 |
'content': b64decode(PCSID_Data['binarySecurityToken']), |
| 156 |
}) |
| 157 |
cls.customer_invoice_journal.l10n_sa_production_csid_certificate_id = pcsid_certificate |
| 158 |
|
| 159 |
@classmethod |
| 160 |
def _setup_xpath_templates(cls): |
| 161 |
""" |
| 162 |
Setup XPath templates for XML testing. |
| 163 |
|
| 164 |
These remove or replace dynamic elements (IDs, UUIDs) that change |
| 165 |
between test runs to allow XML comparison. |
| 166 |
""" |
| 167 |
cls.remove_ubl_extensions_xpath = '''<xpath expr="//*[local-name()='UBLExtensions']" position="replace"/>''' |
| 168 |
|
| 169 |
# Common replacements for all document types |
| 170 |
common_replacements = ''' |
| 171 |
<xpath expr="(//*[local-name()='Invoice']/*[local-name()='ID'])[1]" position="replace"> |
| 172 |
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:ID> |
| 173 |
</xpath> |
| 174 |
<xpath expr="(//*[local-name()='Invoice']/*[local-name()='UUID'])[1]" position="replace"> |
| 175 |
<cbc:UUID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:UUID> |
| 176 |
</xpath> |
| 177 |
<xpath expr="(//*[local-name()='Contact']/*[local-name()='ID'])[1]" position="replace"> |
| 178 |
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:ID> |
| 179 |
</xpath> |
| 180 |
<xpath expr="(//*[local-name()='Contact']/*[local-name()='ID'])[2]" position="replace"> |
| 181 |
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:ID> |
| 182 |
</xpath> |
| 183 |
''' |
| 184 |
|
| 185 |
# Invoice-specific replacements |
| 186 |
cls.invoice_applied_xpath = common_replacements + ''' |
| 187 |
<xpath expr="//*[local-name()='PaymentMeans']/*[local-name()='InstructionID']" position="replace"> |
| 188 |
<cbc:InstructionID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:InstructionID> |
| 189 |
</xpath> |
| 190 |
<xpath expr="(//*[local-name()='PaymentMeans']/*[local-name()='PaymentID'])" position="replace"> |
| 191 |
<cbc:PaymentID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:PaymentID> |
| 192 |
</xpath> |
| 193 |
<xpath expr="//*[local-name()='InvoiceLine']/*[local-name()='ID']" position="replace"> |
| 194 |
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:ID> |
| 195 |
</xpath> |
| 196 |
''' |
| 197 |
|
| 198 |
# Credit note specific replacements |
| 199 |
cls.credit_note_applied_xpath = common_replacements + ''' |
| 200 |
<xpath expr="(//*[local-name()='InvoiceDocumentReference']/*[local-name()='ID'])[1]" position="replace"> |
| 201 |
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:ID> |
| 202 |
</xpath> |
| 203 |
<xpath expr="(//*[local-name()='PaymentMeans']/*[local-name()='InstructionNote'])" position="replace"> |
| 204 |
<cbc:InstructionNote xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:InstructionNote> |
| 205 |
</xpath> |
| 206 |
<xpath expr="(//*[local-name()='PaymentMeans']/*[local-name()='PaymentID'])" position="replace"> |
| 207 |
<cbc:PaymentID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:PaymentID> |
| 208 |
</xpath> |
| 209 |
<xpath expr="//*[local-name()='InvoiceLine']/*[local-name()='ID']" position="replace"> |
| 210 |
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:ID> |
| 211 |
</xpath> |
| 212 |
''' |
| 213 |
|
| 214 |
# Debit note specific replacements |
| 215 |
cls.debit_note_applied_xpath = common_replacements + ''' |
| 216 |
<xpath expr="(//*[local-name()='InvoiceDocumentReference']/*[local-name()='ID'])[1]" position="replace"> |
| 217 |
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:ID> |
| 218 |
</xpath> |
| 219 |
<xpath expr="//*[local-name()='InvoiceLine']/*[local-name()='ID']" position="replace"> |
| 220 |
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:ID> |
| 221 |
</xpath> |
| 222 |
<xpath expr="//*[local-name()='PaymentMeans']/*[local-name()='InstructionID']" position="replace"> |
| 223 |
<cbc:InstructionID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:InstructionID> |
| 224 |
</xpath> |
| 225 |
<xpath expr="(//*[local-name()='PaymentMeans']/*[local-name()='PaymentID'])" position="replace"> |
| 226 |
<cbc:PaymentID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:PaymentID> |
| 227 |
</xpath> |
| 228 |
<xpath expr="(//*[local-name()='PaymentMeans']/*[local-name()='InstructionNote'])" position="replace"> |
| 229 |
<cbc:InstructionNote xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">___ignore___</cbc:InstructionNote> |
| 230 |
</xpath> |
| 231 |
''' |
| 232 |
|
| 233 |
@classmethod |
| 234 |
def _get_or_create_state(cls, name, code, country): |
| 235 |
"""Ensure a state exists for the given country.""" |
| 236 |
state = cls.env['res.country.state'].search([ |
| 237 |
('code', '=', code), |
| 238 |
('country_id', '=', country.id) |
| 239 |
], limit=1) |
| 240 |
|
| 241 |
if not state: |
| 242 |
state = cls.env['res.country.state'].create({ |
| 243 |
'name': name, |
| 244 |
'code': code, |
| 245 |
'country_id': country.id |
| 246 |
}) |
| 247 |
|
| 248 |
return state |
| 249 |
|
| 250 |
# Helper methods for creating documents |
| 251 |
def _create_test_invoice( |
| 252 |
self, |
| 253 |
name="", |
| 254 |
move_type="out_invoice", |
| 255 |
company_id=None, |
| 256 |
partner_id=None, |
| 257 |
invoice_date='2025-01-01', |
| 258 |
invoice_date_due='2025-01-01', |
| 259 |
currency_id=None, |
| 260 |
invoice_line_ids=[]): |
| 261 |
""" |
| 262 |
Create a draft invoice with the given parameters. |
| 263 |
""" |
| 264 |
def _create_invoice_line(line): |
| 265 |
vals = { |
| 266 |
'price_unit': line.get('price_unit', 0.0), |
| 267 |
'quantity': line.get('quantity', 1), |
| 268 |
'tax_ids': line.get('tax_ids', []), |
| 269 |
} |
| 270 |
if product_id := line.get('product_id'): |
| 271 |
vals['product_id'] = product_id |
| 272 |
if name := line.get('name'): |
| 273 |
vals['name'] = name |
| 274 |
return Command.create(vals) |
| 275 |
vals = { |
| 276 |
'name': name, |
| 277 |
'move_type': move_type, |
| 278 |
'company_id': (company_id or self.company).id, |
| 279 |
'partner_id': partner_id.id, |
| 280 |
'invoice_date': invoice_date, |
| 281 |
'invoice_date_due': invoice_date_due, |
| 282 |
'currency_id': (currency_id or self.company.currency_id).id, |
| 283 |
'invoice_line_ids': [ |
| 284 |
_create_invoice_line(line) for line in invoice_line_ids |
| 285 |
], |
| 286 |
} |
| 287 |
return self.env['account.move'].create(vals) |
| 288 |
|
| 289 |
def _create_debit_note( |
| 290 |
self, |
| 291 |
name="", |
| 292 |
move_type="out_invoice", |
| 293 |
company_id=None, |
| 294 |
partner_id=None, |
| 295 |
invoice_date='2025-01-01', |
| 296 |
invoice_date_due='2025-01-01', |
| 297 |
currency_id=None, |
| 298 |
invoice_line_ids=[], |
| 299 |
reason="BR-KSA-17-reason-5"): |
| 300 |
""" |
| 301 |
Create a draft debit note from the given invoice values. |
| 302 |
""" |
| 303 |
# Create and post the original invoice |
| 304 |
invoice = self._create_test_invoice( |
| 305 |
name=name, |
| 306 |
move_type=move_type, |
| 307 |
company_id=company_id, |
| 308 |
partner_id=partner_id, |
| 309 |
invoice_date=invoice_date, |
| 310 |
invoice_date_due=invoice_date_due, |
| 311 |
currency_id=currency_id, |
| 312 |
invoice_line_ids=invoice_line_ids) |
| 313 |
invoice.action_post() |
| 314 |
|
| 315 |
# Create debit note via wizard |
| 316 |
debit_note_wizard = self.env['account.debit.note'].with_context({ |
| 317 |
'active_ids': [invoice.id], |
| 318 |
'active_model': 'account.move', |
| 319 |
'default_copy_lines': True |
| 320 |
}).create({ |
| 321 |
'l10n_sa_reason': reason, |
| 322 |
}) |
| 323 |
res = debit_note_wizard.create_debit() |
| 324 |
|
| 325 |
return self.env['account.move'].browse(res.get('res_id', [])) |
| 326 |
|
| 327 |
def _create_credit_note( |
| 328 |
self, |
| 329 |
name="", |
| 330 |
move_type="out_invoice", |
| 331 |
company_id=None, |
| 332 |
partner_id=None, |
| 333 |
invoice_date='2025-01-01', |
| 334 |
invoice_date_due='2025-01-01', |
| 335 |
currency_id=None, |
| 336 |
invoice_line_ids=[], |
| 337 |
reason='BR-KSA-17-reason-5'): |
| 338 |
""" |
| 339 |
Create a draft credit note from the given invoice values. |
| 340 |
""" |
| 341 |
# Create and post the original invoice |
| 342 |
invoice = self._create_test_invoice( |
| 343 |
name=name, |
| 344 |
move_type=move_type, |
| 345 |
company_id=company_id, |
| 346 |
partner_id=partner_id, |
| 347 |
invoice_date=invoice_date, |
| 348 |
invoice_date_due=invoice_date_due, |
| 349 |
currency_id=currency_id, |
| 350 |
invoice_line_ids=invoice_line_ids) |
| 351 |
invoice.action_post() |
| 352 |
|
| 353 |
# Create credit note via reversal wizard |
| 354 |
move_reversal = self.env['account.move.reversal'].with_context({ |
| 355 |
'active_model': 'account.move', |
| 356 |
'active_ids': invoice.ids |
| 357 |
}).create({ |
| 358 |
'l10n_sa_reason': reason, |
| 359 |
'journal_id': invoice.journal_id.id, |
| 360 |
}) |
| 361 |
reversal = move_reversal.reverse_moves() |
| 362 |
|
| 363 |
return self.env['account.move'].browse(reversal['res_id']) |