| 1 |
from odoo import _, models, tools |
| 2 |
from odoo.tools import html2plaintext |
| 3 |
from odoo.tools.float_utils import float_round |
| 4 |
|
| 5 |
from odoo.addons.account_edi_ubl_cii.models.account_edi_common import FloatFmt |
| 6 |
|
| 7 |
DANISH_NATIONAL_IT_AND_TELECOM_AGENCY_ID = '320' |
| 8 |
|
| 9 |
UBL_TO_OIOUBL_TAX_CATEGORY_ID_MAPPING = { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
'AE': 'ReverseCharge', |
| 14 |
'E': 'ZeroRated', |
| 15 |
'S': 'StandardRated', |
| 16 |
'Z': 'ZeroRated', |
| 17 |
'G': 'ZeroRated', |
| 18 |
'O': 'ZeroRated', |
| 19 |
'K': 'ReverseCharge', |
| 20 |
'L': 'ZeroRated', |
| 21 |
'M': 'ZeroRated', |
| 22 |
} |
| 23 |
|
| 24 |
SCHEME_ID_MAPPING = { |
| 25 |
'0088': 'GLN', |
| 26 |
'0184': 'DK:CVR', |
| 27 |
'9918': 'IBAN', |
| 28 |
'0198': 'DK:SE', |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
def format_vat_number(partner, vat): |
| 33 |
vat = (vat or '').replace(' ', '') |
| 34 |
if vat[:2].isnumeric(): |
| 35 |
vat = partner.country_code.upper() + vat |
| 36 |
return vat |
| 37 |
|
| 38 |
|
| 39 |
class AccountEdiXmlOIOUBL21(models.AbstractModel): |
| 40 |
_name = "account.edi.xml.oioubl_21" |
| 41 |
_inherit = 'account.edi.xml.ubl_21' |
| 42 |
_description = "OIOUBL 2.1" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def _export_invoice_filename(self, invoice): |
| 52 |
return f"{invoice.name.replace('/', '_')}_oioubl_21.xml" |
| 53 |
|
| 54 |
def _get_currency_decimal_places(self, currency_id): |
| 55 |
|
| 56 |
return 2 |
| 57 |
|
| 58 |
def _get_customization_id(self, process_type='billing'): |
| 59 |
if process_type == 'billing': |
| 60 |
return 'OIOUBL-2.1' |
| 61 |
|
| 62 |
def _export_invoice_constraints(self, invoice, vals): |
| 63 |
|
| 64 |
constraints = super()._export_invoice_constraints(invoice, vals) |
| 65 |
|
| 66 |
for partner_type in ('supplier', 'customer'): |
| 67 |
partner = vals[partner_type] |
| 68 |
building_number = tools.street_split(partner.street).get('street_number') |
| 69 |
if not building_number: |
| 70 |
constraints[f"oioubl21_{partner_type}_building_number_required"] = _("The following partner's street number is missing: %s", partner.display_name) |
| 71 |
if partner.country_code == "FR" and not partner.commercial_partner_id.company_registry: |
| 72 |
constraints["oioubl21_company_registry_required_for_french_partner"] = _("The company registry is required for french partner: %s", partner.display_name) |
| 73 |
constraints[f'oioubl21_{partner_type}_vat_required'] = self._check_required_fields(partner.commercial_partner_id, 'vat') |
| 74 |
constraints['oioubl21_supplier_company_registry_required'] = self._check_required_fields(vals['supplier'], 'company_registry') |
| 75 |
|
| 76 |
return constraints |
| 77 |
|
| 78 |
def _add_invoice_header_nodes(self, document_node, vals): |
| 79 |
|
| 80 |
super()._add_invoice_header_nodes(document_node, vals) |
| 81 |
document_node.update({ |
| 82 |
'cbc:CustomizationID': {'_text': self._get_customization_id()}, |
| 83 |
'cbc:ProfileID': { |
| 84 |
'_text': 'Procurement-BilSim-1.0', |
| 85 |
'schemeID': 'urn:oioubl:id:profileid-1.6', |
| 86 |
'schemeAgencyID': DANISH_NATIONAL_IT_AND_TELECOM_AGENCY_ID, |
| 87 |
|
| 88 |
}, |
| 89 |
'cbc:InvoiceTypeCode': { |
| 90 |
'_text': 380, |
| 91 |
'listID': 'urn:oioubl:codelist:invoicetypecode-1.2', |
| 92 |
'listAgencyID': DANISH_NATIONAL_IT_AND_TELECOM_AGENCY_ID, |
| 93 |
|
| 94 |
} if vals['document_type'] == 'invoice' else None, |
| 95 |
'cbc:CreditNoteTypeCode': { |
| 96 |
'_text': 381, |
| 97 |
'listID': 'urn:oioubl:codelist:invoicetypecode-1.2', |
| 98 |
'listAgencyID': DANISH_NATIONAL_IT_AND_TELECOM_AGENCY_ID, |
| 99 |
|
| 100 |
} if vals['document_type'] == 'credit_note' else None, |
| 101 |
}) |
| 102 |
|
| 103 |
def _get_document_type_code_node(self, invoice, invoice_data): |
| 104 |
|
| 105 |
|
| 106 |
return { |
| 107 |
'_text': '380' if invoice.move_type == 'out_invoice' else '381', |
| 108 |
'listAgencyID': '6', |
| 109 |
'listID': 'UN/ECE 1001', |
| 110 |
} |
| 111 |
|
| 112 |
def _get_address_node(self, vals): |
| 113 |
|
| 114 |
address_node = super()._get_address_node(vals) |
| 115 |
|
| 116 |
address = tools.street_split(vals['partner'].street) |
| 117 |
street_name = address.get('street_name') |
| 118 |
building_number = address.get('street_number') |
| 119 |
address_node.update({ |
| 120 |
'cbc:AddressFormatCode': { |
| 121 |
|
| 122 |
|
| 123 |
'_text': 'StructuredDK', |
| 124 |
'listAgencyID': DANISH_NATIONAL_IT_AND_TELECOM_AGENCY_ID, |
| 125 |
'listID': 'urn:oioubl:codelist:addressformatcode-1.1', |
| 126 |
}, |
| 127 |
'cbc:StreetName': {'_text': street_name}, |
| 128 |
'cbc:BuildingNumber': {'_text': building_number}, |
| 129 |
}) |
| 130 |
|
| 131 |
return address_node |
| 132 |
|
| 133 |
def _get_party_node(self, vals): |
| 134 |
|
| 135 |
party_node = super()._get_party_node(vals) |
| 136 |
partner = vals['partner'] |
| 137 |
vat = format_vat_number(partner, partner.vat) |
| 138 |
cvr = format_vat_number(partner, partner.company_registry) or vat |
| 139 |
party_node['cac:PartyLegalEntity'].update({ |
| 140 |
|
| 141 |
|
| 142 |
'cbc:CompanyID': { |
| 143 |
'_text': cvr, |
| 144 |
'schemeID': 'DK:CVR' if vat[:2] == 'DK' else 'ZZZ' |
| 145 |
}, |
| 146 |
}) |
| 147 |
if vat and vat != '/': |
| 148 |
party_node['cac:PartyTaxScheme'].update({ |
| 149 |
|
| 150 |
'cbc:CompanyID': { |
| 151 |
'_text': vat, |
| 152 |
'schemeID': 'DK:SE' if vat[:2] == 'DK' else 'ZZZ' |
| 153 |
}, |
| 154 |
'cac:TaxScheme': { |
| 155 |
'cbc:ID': { |
| 156 |
'_text': 63, |
| 157 |
'schemeID': 'urn:oioubl:id:taxschemeid-1.5', |
| 158 |
}, |
| 159 |
'cbc:Name': {'_text': 'Moms'}, |
| 160 |
}, |
| 161 |
}) |
| 162 |
if partner.nemhandel_identifier_type and partner.nemhandel_identifier_value: |
| 163 |
prefix = 'DK' if partner.nemhandel_identifier_type == '0184' else '' |
| 164 |
party_node['cbc:EndpointID'] = { |
| 165 |
'_text': f'{prefix}{partner.nemhandel_identifier_value}', |
| 166 |
'schemeID': SCHEME_ID_MAPPING[partner.nemhandel_identifier_type], |
| 167 |
} |
| 168 |
|
| 169 |
return party_node |
| 170 |
|
| 171 |
def _get_tax_subtotal_node(self, vals): |
| 172 |
tax_subtotal_node = super()._get_tax_subtotal_node(vals) |
| 173 |
|
| 174 |
tax_subtotal_node['cbc:Percent'] = None |
| 175 |
return tax_subtotal_node |
| 176 |
|
| 177 |
def _add_invoice_payment_means_nodes(self, document_node, vals): |
| 178 |
|
| 179 |
super()._add_invoice_payment_means_nodes(document_node, vals) |
| 180 |
payment_means_node = document_node['cac:PaymentMeans'] |
| 181 |
payment_means_node['cbc:PaymentMeansCode'] = { |
| 182 |
'_text': 1, |
| 183 |
'name': 'unknown', |
| 184 |
} |
| 185 |
|
| 186 |
invoice = vals['invoice'] |
| 187 |
supplier = vals['supplier'] |
| 188 |
if ( |
| 189 |
invoice.partner_bank_id in supplier.bank_ids |
| 190 |
and payment_means_node.get('cac:PayeeFinancialAccount') |
| 191 |
and payment_means_node['cac:PayeeFinancialAccount'].get('cac:FinancialInstitutionBranch') |
| 192 |
and payment_means_node['cac:PayeeFinancialAccount']['cac:FinancialInstitutionBranch'].get('cac:FinancialInstitution') |
| 193 |
): |
| 194 |
payment_means_node['cac:PayeeFinancialAccount']['cac:FinancialInstitutionBranch']['cac:FinancialInstitution']['cac:Address'] = None |
| 195 |
|
| 196 |
def _add_invoice_monetary_total_nodes(self, document_node, vals): |
| 197 |
|
| 198 |
super()._add_invoice_monetary_total_nodes(document_node, vals) |
| 199 |
monetary_total_tag = self._get_tags_for_document_type(vals)['monetary_total'] |
| 200 |
currency_suffix = vals['currency_suffix'] |
| 201 |
document_node[monetary_total_tag]['cbc:TaxExclusiveAmount'] = { |
| 202 |
|
| 203 |
'_text': self.format_float( |
| 204 |
vals[f'tax_inclusive_amount{currency_suffix}'] - vals[f'tax_exclusive_amount{currency_suffix}'], |
| 205 |
vals['currency_dp'], |
| 206 |
), |
| 207 |
'currencyID': vals['currency_name'], |
| 208 |
} |
| 209 |
|
| 210 |
if document_node[monetary_total_tag].get('cbc:PrepaidAmount') and document_node[monetary_total_tag]['cbc:PrepaidAmount'].get('_text') == FloatFmt(0, 2): |
| 211 |
document_node[monetary_total_tag]['cbc:PrepaidAmount'] = None |
| 212 |
|
| 213 |
def _get_tax_category_node(self, vals): |
| 214 |
|
| 215 |
tax_category_node = super()._get_tax_category_node(vals) |
| 216 |
|
| 217 |
tax_category_node['cbc:ID'].update({ |
| 218 |
'_text': UBL_TO_OIOUBL_TAX_CATEGORY_ID_MAPPING.get(tax_category_node['cbc:ID']['_text']), |
| 219 |
'schemeID': 'urn:oioubl:id:taxcategoryid-1.3', |
| 220 |
'schemeAgencyID': DANISH_NATIONAL_IT_AND_TELECOM_AGENCY_ID, |
| 221 |
}) |
| 222 |
tax_category_node['cac:TaxScheme'] = { |
| 223 |
'cbc:ID': { |
| 224 |
'_text': 63, |
| 225 |
'schemeID': 'urn:oioubl:id:taxschemeid-1.5', |
| 226 |
}, |
| 227 |
'cbc:Name': {'_text': 'Moms'}, |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
tax_category_node['cbc:Name'] = None |
| 235 |
return tax_category_node |
| 236 |
|
| 237 |
def _add_invoice_payment_terms_nodes(self, document_node, vals): |
| 238 |
|
| 239 |
invoice = vals['invoice'] |
| 240 |
if payment_term := invoice.invoice_payment_term_id: |
| 241 |
sign = 1 if invoice.is_inbound(include_receipts=True) else -1 |
| 242 |
document_node['cac:PaymentTerms'] = [ |
| 243 |
{ |
| 244 |
'cbc:ID': {'_text': line.id}, |
| 245 |
'cbc:Note': {'_text': html2plaintext(payment_term.note)}, |
| 246 |
'cbc:Amount': { |
| 247 |
'_text': self.format_float(sign * line.amount_currency, 2), |
| 248 |
'currencyID': line.currency_id.name, |
| 249 |
}, |
| 250 |
'cac:SettlementPeriod': { |
| 251 |
'cbc:StartDate': {'_text': invoice.invoice_date}, |
| 252 |
'cbc:EndDate': {'_text': line.date_maturity}, |
| 253 |
}, |
| 254 |
} |
| 255 |
for line in |
| 256 |
invoice.line_ids.filtered(lambda line: line.display_type == 'payment_term').sorted('date_maturity') |
| 257 |
] |
| 258 |
|
| 259 |
def _add_document_line_price_nodes(self, line_node, vals): |
| 260 |
|
| 261 |
currency_suffix = vals['currency_suffix'] |
| 262 |
base_line = vals['base_line'] |
| 263 |
product_price_dp = self.env['decimal.precision'].precision_get('Product Price') |
| 264 |
|
| 265 |
line_node['cac:Price'] = { |
| 266 |
|
| 267 |
|
| 268 |
'cbc:PriceAmount': { |
| 269 |
'_text': float_round( |
| 270 |
vals[f'gross_price_unit{currency_suffix}'] * (1 - base_line['discount'] / 100), |
| 271 |
precision_digits=product_price_dp, |
| 272 |
), |
| 273 |
'currencyID': vals['currency_name'], |
| 274 |
}, |
| 275 |
} |
| 276 |
|
| 277 |
def _retrieve_rebate_val(self, tree, xpath_dict, quantity): |
| 278 |
|
| 279 |
rebate = super()._retrieve_rebate_val(tree, xpath_dict, quantity) |
| 280 |
|
| 281 |
discount_amount, charges = self._retrieve_charge_allowance_vals(tree, xpath_dict, quantity) |
| 282 |
charge_amount = sum(d['amount'] for d in charges) |
| 283 |
|
| 284 |
return rebate + (discount_amount - charge_amount) / quantity |