| 1 |
import re |
| 2 |
from odoo import models, fields, _ |
| 3 |
from odoo.exceptions import UserError |
| 4 |
|
| 5 |
|
| 6 |
class ResCompany(models.Model): |
| 7 |
_inherit = "res.company" |
| 8 |
|
| 9 |
l10n_sa_private_key_id = fields.Many2one(string="ZATCA Private key", comodel_name='certificate.key', copy=False, domain=[('public', '=', False)], |
| 10 |
help="The private key used to generate the CSR and obtain certificates",) |
| 11 |
|
| 12 |
l10n_sa_api_mode = fields.Selection( |
| 13 |
[('sandbox', 'Sandbox'), ('preprod', 'Simulation (Pre-Production)'), ('prod', 'Production')], |
| 14 |
help="Specifies which API the system should use", required=True, |
| 15 |
default='sandbox', copy=False) |
| 16 |
|
| 17 |
l10n_sa_edi_building_number = fields.Char(compute='_compute_address', |
| 18 |
inverse='_l10n_sa_edi_inverse_building_number') |
| 19 |
l10n_sa_edi_plot_identification = fields.Char(compute='_compute_address', |
| 20 |
inverse='_l10n_sa_edi_inverse_plot_identification') |
| 21 |
|
| 22 |
l10n_sa_edi_additional_identification_scheme = fields.Selection( |
| 23 |
related='partner_id.l10n_sa_edi_additional_identification_scheme', readonly=False) |
| 24 |
l10n_sa_edi_additional_identification_number = fields.Char( |
| 25 |
related='partner_id.l10n_sa_edi_additional_identification_number', readonly=False) |
| 26 |
|
| 27 |
l10n_sa_edi_is_production = fields.Boolean(string="Is Production", copy=False) |
| 28 |
|
| 29 |
def write(self, vals): |
| 30 |
for company in self: |
| 31 |
if 'l10n_sa_api_mode' in vals: |
| 32 |
if company.l10n_sa_api_mode == 'prod' and vals['l10n_sa_api_mode'] != 'prod': |
| 33 |
|
| 34 |
if company.l10n_sa_edi_is_production: |
| 35 |
raise UserError(_("ZATCA API Mode cannot be changed after an invoice has been successfully submitted under the Production Mode.")) |
| 36 |
journals = self.env['account.journal'].search(self.env['account.journal']._check_company_domain(company)) |
| 37 |
journals._l10n_sa_reset_certificates() |
| 38 |
journals.l10n_sa_latest_submission_hash = False |
| 39 |
api_mode = dict(self._fields['l10n_sa_api_mode'].selection).get(vals['l10n_sa_api_mode']) |
| 40 |
for journal in journals.filtered(lambda j: j.type == 'sale'): |
| 41 |
journal.message_post(body=_("ZATCA API Mode changed to %s", api_mode)) |
| 42 |
return super().write(vals) |
| 43 |
|
| 44 |
def _get_company_address_field_names(self): |
| 45 |
""" Override to add ZATCA specific address fields """ |
| 46 |
return super()._get_company_address_field_names() + \ |
| 47 |
['l10n_sa_edi_building_number', 'l10n_sa_edi_plot_identification'] |
| 48 |
|
| 49 |
def _l10n_sa_edi_inverse_building_number(self): |
| 50 |
for company in self: |
| 51 |
company.partner_id.l10n_sa_edi_building_number = company.l10n_sa_edi_building_number |
| 52 |
|
| 53 |
def _l10n_sa_edi_inverse_plot_identification(self): |
| 54 |
for company in self: |
| 55 |
company.partner_id.l10n_sa_edi_plot_identification = company.l10n_sa_edi_plot_identification |
| 56 |
|
| 57 |
def _l10n_sa_get_csr_invoice_type(self): |
| 58 |
""" |
| 59 |
Return the Invoice Type flag used in the CSR. 4-digit numerical input using 0 & 1 mapped to “TSCZ” where: |
| 60 |
- 0: False/Not supported, 1: True/Supported |
| 61 |
- T: Tax Invoice (Standard), S: Simplified Invoice, C & Z will be used in the future and should |
| 62 |
always be 0 |
| 63 |
For example: 1100 would mean the Solution will be generating Standard and Simplified invoices. |
| 64 |
We can assume Odoo-powered EGS solutions will always generate both Standard & Simplified invoices |
| 65 |
:return: |
| 66 |
""" |
| 67 |
return '1100' |
| 68 |
|
| 69 |
def _l10n_sa_check_organization_unit(self): |
| 70 |
""" |
| 71 |
Check company Organization Unit according to ZATCA specifications |
| 72 |
Standards: |
| 73 |
BR-KSA-39 |
| 74 |
BR-KSA-40 |
| 75 |
See https://zatca.gov.sa/ar/RulesRegulations/Taxes/Documents/20210528_ZATCA_Electronic_Invoice_XML_Implementation_Standard_vShared.pdf |
| 76 |
""" |
| 77 |
self.ensure_one() |
| 78 |
if not self.vat: |
| 79 |
return False |
| 80 |
return len(self.vat) == 15 and bool(re.match(r'^3\d{13}3$', self.vat)) |