Debian Codegraph @

addons/l10n_anz_ubl_pint/models/account_edi_xml_pint_anz.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 from odoo import models, _
3 from odoo.tools import float_is_zero
4
5 ANZ_TAX_CATEGORIES = {'S', 'E', 'Z', 'G', 'O'}
6
7
8 class AccountEdiXmlPint_Anz(models.AbstractModel):
9 _name = 'account.edi.xml.pint_anz'
10 _inherit = ["account.edi.xml.ubl_bis3"]
11 _description = "Australia & New Zealand implementation of Peppol International (PINT) model for Billing"
12 """
13 Pint is a standard for International Billing from Peppol. It is based on Peppol BIS Billing 3.
14 It serves as a base for per-country specialization, while keeping a standard core for data being used
15 across countries. This is not meant to be used directly, but rather to be extended by country-specific modules.
16
17 The ANZ PINT format is the Australia & New Zealand implementation of PINT.
18
19 * PINT Official documentation: https://docs.peppol.eu/poac/pint/pint/
20 * PINT ANZ Official documentation: https://docs.peppol.eu/poac/aunz/pint-aunz/
21 """
22
23 def _export_invoice_filename(self, invoice):
24 # EXTENDS account_edi_ubl_cii
25 return f"{invoice.name.replace('/', '_')}_pint_anz.xml"
26
27 def _get_customization_id(self, process_type='billing'):
28 if process_type == 'billing':
29 return 'urn:peppol:pint:billing-1@aunz-1'
30
31 # -------------------------------------------------------------------------
32 # EXPORT: Templates
33 # -------------------------------------------------------------------------
34
35 def _ubl_default_tax_category_grouping_key(self, base_line, tax_data, vals, currency):
36 # EXTENDS account.edi.xml.ubl_bis3
37 grouping_key = super()._ubl_default_tax_category_grouping_key(base_line, tax_data, vals, currency)
38 if not grouping_key:
39 return
40
41 grouping_key['scheme_id'] = 'GST'
42
43 # A business not registered for GST cannot issue tax invoices.
44 # In this case, the tax category code should be O (Outside scope of tax).
45 # See https://docs.peppol.eu/poac/aunz/pint-aunz/bis/#_tax_category_code
46 supplier = vals['supplier']
47 if not supplier.ref_company_ids[:1].l10n_au_is_gst_registered:
48 grouping_key['tax_category_code'] = 'O'
49
50 # As of PINT A-NZ v1.1.0: [aligned-ibrp-o-05-aunz] tax categories of type "Not Subject to tax" (i.e. tax_category_code == 'O') must NOT have
51 # tax rate ('cbc:Percent').
52 if (
53 grouping_key['tax_category_code'] == 'O'
54 and not grouping_key['percent']
55 ):
56 grouping_key['percent'] = None
57
58 return grouping_key
59
60 def _add_invoice_header_nodes(self, document_node, vals):
61 # EXTENDS account.edi.xml.ubl_bis3
62 super()._add_invoice_header_nodes(document_node, vals)
63
64 # see https://docs.peppol.eu/poac/aunz/pint-aunz/bis/#_identifying_the_a_nz_billing_specialisation
65 document_node['cbc:ProfileID'] = {'_text': 'urn:peppol:bis:billing'}
66
67 def _get_party_node(self, vals):
68 # EXTENDS account.edi.xml.ubl_bis3
69 party_node = super()._get_party_node(vals)
70 commercial_partner = vals['partner'].commercial_partner_id
71
72 party_node['cac:PartyTaxScheme'][0]['cac:TaxScheme']['cbc:ID']['_text'] = 'GST'
73
74 # In both cases the scheme must be set to a value that comes from the eas.
75 if commercial_partner.country_code in ('AU', 'NZ'):
76 party_node['cac:PartyLegalEntity']['cbc:CompanyID']['schemeID'] = commercial_partner.peppol_eas
77
78 return party_node
79
80 # -------------------------------------------------------------------------
81 # EXPORT: Constraints
82 # -------------------------------------------------------------------------
83
84 def _export_invoice_constraints(self, invoice, vals):
85 # EXTENDS account_edi_ubl_cii
86 constraints = super()._export_invoice_constraints(invoice, vals)
87
88 # Tax category must be filled on the line, with a value from SG categories.
89 tax_total_node = vals['document_node']['cac:TaxTotal'][0]
90 for tax_subtotal_node in tax_total_node['cac:TaxSubtotal']:
91 if any(tax_category_node['cbc:ID']['_text'] not in ANZ_TAX_CATEGORIES for tax_category_node in tax_subtotal_node['cac:TaxCategory']):
92 constraints['anz_vat_category_required'] = _("You must set a tax category on each taxes of the invoice.\nValid categories are: S, E, Z, G, O")
93
94 # Tax category of type "Not subject to tax" must have tax amount 0
95 count_outside_of_scope_breakdown = 0
96 for tax_subtotal in tax_total_node['cac:TaxSubtotal']:
97 if any(tax_category_node['cbc:ID']['_text'] != 'O' for tax_category_node in tax_subtotal['cac:TaxCategory']):
98 continue
99 count_outside_of_scope_breakdown += 1
100
101 if float_is_zero(tax_subtotal['cbc:TaxAmount']['_text'], precision_digits=2):
102 continue
103
104 if vals['supplier'].ref_company_ids[:1].l10n_au_is_gst_registered:
105 constraints['anz_tax_breakdown_amount'] = \
106 self.env._("A tax category of type 'Not subject to tax' must have tax"
107 " amount set to 0")
108 else:
109 # If a company is not GST registered, this module remaps all tax categories
110 # to 'O' (Other/Out of Scope). This causes an issue when a line contained a
111 # non-zero tax, as it would create a tax subtotal with (Code: 'O', Amount:
112 # non-zero), which violates PINT rules.
113 # Since the code silently remaps tax classification code, the original error
114 # message might be misleading for users. This constraint contains better
115 # explaination of error and intructions on how to prevent it.
116 constraints['anz_non_gst_supplier_tax_scope'] = \
117 self.env._("Suppliers not registered for GST cannot use taxes that are"
118 " not zero-rated. Please ensure the tax category is set to"
119 " 'O' (Services outside scope of tax) with a tax amount of 0.")
120
121 # There should be at most one tax breakdown of type "Not subject to tax".
122 if count_outside_of_scope_breakdown > 1 and vals['supplier'].ref_company_ids[:1].l10n_au_is_gst_registered:
123 constraints['anz_duplicate_tax_breakdown'] = \
124 self.env._("A tax breakdown of type 'Not subject to tax' should appear at most"
125 " once in the tax breakdown")
126
127 # ALIGNED-IBR-001-AUNZ and ALIGNED-IBR-002-AUNZ
128 for partner_type in ('supplier', 'customer'):
129 partner = vals[partner_type]
130 if partner.country_code == 'AU' and partner.peppol_eas != '0151':
131 constraints[f'au_{partner_type}_eas_0151'] = _("The Peppol EAS must be set to ABN (0151) if the partner country is Australia.")
132 elif partner.country_code == 'NZ' and partner.peppol_eas != '0088':
133 constraints[f'nz_{partner_type}_eas_0088'] = _("The Peppol EAS must be set to EAN (0088) if the partner country is New Zealand.")
134
135 return constraints