| 1 |
# UBL structure for CIUS HR |
| 2 |
# Updated to work with but doesn't entirely follow the full structure of the UBL rework |
| 3 |
# implemented in commit a3c6e5abe0d964f0768de68d526905ae3dccac8a |
| 4 |
|
| 5 |
from odoo import fields, models |
| 6 |
from odoo.tools import html2plaintext |
| 7 |
from lxml import etree |
| 8 |
|
| 9 |
|
| 10 |
class AccountEdiXmlUBLHR(models.AbstractModel): |
| 11 |
_inherit = 'account.edi.xml.ubl_bis3' |
| 12 |
_name = 'account.edi.xml.ubl_hr' |
| 13 |
_description = "CIUS HR" |
| 14 |
|
| 15 |
def _export_invoice_filename(self, invoice): |
| 16 |
return f"{invoice.name.replace('/', '_')}_ubl_hr.xml" |
| 17 |
|
| 18 |
# ------------------------------------------------------------------------- |
| 19 |
# EXPORT: New (dict_to_xml) helpers |
| 20 |
# ------------------------------------------------------------------------- |
| 21 |
|
| 22 |
def _get_document_template(self, vals): |
| 23 |
ext_template = { |
| 24 |
'ext:UBLExtension': { |
| 25 |
'ext:ExtensionContent': { |
| 26 |
'hrextac:HRFISK20Data': { |
| 27 |
'hrextac:HRObracunPDVPoNaplati': {}, |
| 28 |
'hrextac:HRTaxTotal': { |
| 29 |
'cbc:TaxAmount': {}, |
| 30 |
'hrextac:HRTaxSubtotal': { |
| 31 |
'cbc:TaxableAmount': {}, |
| 32 |
'cbc:TaxAmount': {}, |
| 33 |
'hrextac:HRTaxCategory': { |
| 34 |
'cbc:ID': {}, |
| 35 |
'cbc:Name': {}, |
| 36 |
'cbc:Percent': {}, |
| 37 |
'cbc:TaxExemptionReasonCode': {}, |
| 38 |
'cbc:TaxExemptionReason': {}, |
| 39 |
'hrextac:HRTaxScheme': { |
| 40 |
'cbc:ID': {}, |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
}, |
| 45 |
'hrextac:HRLegalMonetaryTotal': { |
| 46 |
'cbc:TaxExclusiveAmount': {}, |
| 47 |
'hrextac:OutOfScopeOfVATAmount': {}, |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
template = super()._get_document_template(vals) |
| 54 |
# Overriding the node as it appears to be localization-specific |
| 55 |
template['ext:UBLExtensions'] = ext_template |
| 56 |
return template |
| 57 |
|
| 58 |
def _get_document_nsmap(self, vals): |
| 59 |
nsmap = super()._get_document_nsmap(vals) |
| 60 |
nsmap.update({ |
| 61 |
'hrextac': "urn:mfin.gov.hr:schema:xsd:HRExtensionAggregateComponents-1", |
| 62 |
}) |
| 63 |
return nsmap |
| 64 |
|
| 65 |
def _export_invoice_constraints_new(self, invoice, vals): |
| 66 |
constraints = super()._export_invoice_constraints_new(invoice, vals) |
| 67 |
constraints.update( |
| 68 |
self._invoice_constraints_eracun_new(invoice, vals) |
| 69 |
) |
| 70 |
return constraints |
| 71 |
|
| 72 |
def _invoice_constraints_eracun_new(self, invoice, vals): |
| 73 |
# Corresponds to Croatian eRacun format constrains |
| 74 |
constraints = {} |
| 75 |
if vals['document_type'] in ['invoice', 'credit_note']: |
| 76 |
if any(c.isspace() for c in vals['document_node']['cac:PaymentMeans']['cac:PayeeFinancialAccount']['cbc:ID'].get('_text')): |
| 77 |
constraints.update({'ubl_hr_br_1': self.env._("HR-BR-1: The account number must not contain whitespace characters.")}) |
| 78 |
if invoice.amount_residual > 0 and not invoice.invoice_date_due: |
| 79 |
constraints.update({'ubl_hr_br_4': self.env._("HR-BT-4: In the case of a positive amount due for payment (BT-115), the payment due date (BT-9) must be specified.")}) |
| 80 |
constraints.update({ |
| 81 |
'ubl_hr_br_7_seller_email_required': ( |
| 82 |
self.env._("The Seller's e-mail must be provided.") |
| 83 |
) if not vals['document_node']['cac:AccountingSupplierParty']['cac:Party']['cac:Contact']['cbc:ElectronicMail'].get('_text') else None, |
| 84 |
'ubl_hr_br_10_buyer_email_required': ( |
| 85 |
self.env._("The Buyer's e-mail must be provided.") |
| 86 |
) if not vals['document_node']['cac:AccountingCustomerParty']['cac:Party']['cac:Contact']['cbc:ElectronicMail'].get('_text') else None, |
| 87 |
'ubl_hr_br_s_buyer_vat_required': ( |
| 88 |
self.env._("The invoice must contain the Customer's VAT identification number (BT-48).") |
| 89 |
) if any(not item['cbc:CompanyID'].get('_text') for item in vals['document_node']['cac:AccountingCustomerParty']['cac:Party']['cac:PartyTaxScheme']) else None, |
| 90 |
'ubl_hr_br_37_operator_label_required': ( |
| 91 |
self.env._("The invoice must contain the Operator Label (HR-BT-4).") |
| 92 |
) if not vals['document_node']['cac:AccountingSupplierParty']['cac:SellerContact']['cbc:Name'].get('_text') else None, |
| 93 |
'ubl_hr_br_9_operator_oib_required': ( |
| 94 |
self.env._("The invoice must contain the Operator OIB (HR-BT-5).") |
| 95 |
) if not vals['document_node']['cac:AccountingSupplierParty']['cac:SellerContact']['cbc:ID'].get('_text') else None, |
| 96 |
}) |
| 97 |
return constraints |
| 98 |
|
| 99 |
def _get_invoice_node(self, vals): |
| 100 |
document_node = super()._get_invoice_node(vals) |
| 101 |
# HRFISC20Data extension support |
| 102 |
self._add_hr_extension_node(document_node) |
| 103 |
return document_node |
| 104 |
|
| 105 |
def _add_invoice_header_nodes(self, document_node, vals): |
| 106 |
super()._add_invoice_header_nodes(document_node, vals) |
| 107 |
invoice = vals['invoice'] |
| 108 |
document_node.update({ |
| 109 |
# For Croatia, ID should be the Croatian-format fiscalization number |
| 110 |
'cbc:ID': { |
| 111 |
'_text': invoice.l10n_hr_fiscalization_number |
| 112 |
}, |
| 113 |
# HR-BT-1: Copy indicator - is the invoice the original or already sent |
| 114 |
# This doesn't appear to be currently supported in Odoo, and is set to 'false' in TR localization using a similar format |
| 115 |
'cbc:CopyIndicator': { |
| 116 |
'_text': 'false' |
| 117 |
}, |
| 118 |
# HR-BT-2: The invoice must have an invoice issuance time. |
| 119 |
# (in addition to BT-2: Date of issue) |
| 120 |
'cbc:IssueDate': { |
| 121 |
'_text': fields.Datetime.to_string(invoice.l10n_hr_invoice_sending_time).split()[0] |
| 122 |
}, |
| 123 |
'cbc:IssueTime': { |
| 124 |
'_text': fields.Datetime.to_string(invoice.l10n_hr_invoice_sending_time).split()[1] |
| 125 |
}, |
| 126 |
# HR-BR-34: The process label MUST be specified. Values P1-P12 or P99:Customer ID from Table 4 Business Process Types are used. |
| 127 |
'cbc:ProfileID': { |
| 128 |
'_text': f"P99:{invoice.l10n_hr_customer_defined_process_name}" if invoice.l10n_hr_process_type == 'P99' else invoice.l10n_hr_process_type |
| 129 |
}, |
| 130 |
# HR-BR-5: The specification identifier must have the value |
| 131 |
# 'urn:cen.eu:en16931:2017#compliant#urn:mfin.gov.hr:cius-2025:1.0#conformant#urn:mfin.gov.hr:ext-2025:1.0' |
| 132 |
'cbc:CustomizationID': { |
| 133 |
'_text': 'urn:cen.eu:en16931:2017#compliant#urn:mfin.gov.hr:cius-2025:1.0#conformant#urn:mfin.gov.hr:ext-2025:1.0' |
| 134 |
}, |
| 135 |
}) |
| 136 |
# HR-BT-4: In the case of a positive amount due for payment (BT-115), the payment due date (BT-9) must be specified. |
| 137 |
if invoice.amount_residual > 0 and not document_node['cbc:DueDate'] and vals['document_type'] == 'invoice': |
| 138 |
if invoice.invoice_date_due: |
| 139 |
document_node.update({ |
| 140 |
'cbc:DueDate': { |
| 141 |
'_text': invoice.invoice_date_due |
| 142 |
} |
| 143 |
}) |
| 144 |
# HR-BT-3: Note on previous invoice |
| 145 |
# HR-BR-6: Each previous invoice reference (BG-3) must have the date of issue of the previous invoice (BT-26). |
| 146 |
if 'refund' in invoice.move_type and invoice.reversed_entry_id: |
| 147 |
document_node['cac:BillingReference'] = { |
| 148 |
'cac:InvoiceDocumentReference': { |
| 149 |
'cbc:ID': {'_text': invoice.ref}, |
| 150 |
}, |
| 151 |
'cbc:IssueDate': { |
| 152 |
'_text': invoice.reversed_entry_id.invoice_date |
| 153 |
} |
| 154 |
} |
| 155 |
# Document Type Codes and Process Type Logic |
| 156 |
if invoice.l10n_hr_process_type in ('P4', 'P6'): |
| 157 |
if invoice.move_type == 'out_invoice': |
| 158 |
document_node['cbc:InvoiceTypeCode'].update({ |
| 159 |
'_text': '386' |
| 160 |
}) |
| 161 |
elif invoice.move_type == 'out_refund': |
| 162 |
document_node['cbc:CreditNoteTypeCode'].update({ |
| 163 |
'_text': '386' |
| 164 |
}) |
| 165 |
elif invoice.l10n_hr_process_type == 'P9': |
| 166 |
document_node['cbc:CreditNoteTypeCode'].update({ |
| 167 |
'_text': '381' |
| 168 |
}) |
| 169 |
|
| 170 |
def _add_hr_extension_node(self, document_node): |
| 171 |
tax_subtotals = document_node['cac:TaxTotal'][0]['cac:TaxSubtotal'] |
| 172 |
hr_tax_subtotals = [] |
| 173 |
cash_basis_line = False |
| 174 |
for i in range(len(tax_subtotals)): |
| 175 |
# Removing the HR-specific node from the normal subtotal where we calculate it |
| 176 |
hr_tax_name = tax_subtotals[i]['cac:TaxCategory'][0].pop('cbc:Name') |
| 177 |
cash_basis_flag = tax_subtotals[i]['cac:TaxCategory'][0].pop('hrextac:HRObracunPDVPoNaplati') # Ensure pop() always runs |
| 178 |
cash_basis_line = cash_basis_line or cash_basis_flag |
| 179 |
new_item = { |
| 180 |
'cbc:TaxableAmount': tax_subtotals[i]['cbc:TaxableAmount'], |
| 181 |
'cbc:TaxAmount': tax_subtotals[i]['cbc:TaxAmount'], |
| 182 |
'hrextac:HRTaxCategory': { |
| 183 |
'cbc:ID': tax_subtotals[i]['cac:TaxCategory'][0]['cbc:ID'], |
| 184 |
'cbc:Name': hr_tax_name, |
| 185 |
'cbc:Percent': tax_subtotals[i]['cac:TaxCategory'][0]['cbc:Percent'], |
| 186 |
'cbc:TaxExemptionReasonCode': tax_subtotals[i]['cac:TaxCategory'][0]['cbc:TaxExemptionReasonCode'], |
| 187 |
'cbc:TaxExemptionReason': tax_subtotals[i]['cac:TaxCategory'][0]['cbc:TaxExemptionReason'], |
| 188 |
'hrextac:HRTaxScheme': tax_subtotals[i]['cac:TaxCategory'][0]['cac:TaxScheme'] if hr_tax_name['_text'] != "HR:POVNAK" else {'_text': "OTH"}, |
| 189 |
} |
| 190 |
} |
| 191 |
hr_tax_subtotals.append(new_item) |
| 192 |
out_of_scope_node = { |
| 193 |
'currencyID': document_node['cac:LegalMonetaryTotal']['cbc:TaxExclusiveAmount'].get('currencyID'), |
| 194 |
'_text': '0.00' # Currently unsupported, a HR-specific workaround can potentially be made |
| 195 |
} |
| 196 |
document_node.update({ |
| 197 |
'ext:UBLExtensions': { |
| 198 |
'ext:UBLExtension': { |
| 199 |
'ext:ExtensionContent': { |
| 200 |
'hrextac:HRFISK20Data': { |
| 201 |
'hrextac:HRObracunPDVPoNaplati': cash_basis_line, |
| 202 |
'hrextac:HRTaxTotal': { |
| 203 |
'cbc:TaxAmount': document_node['cac:TaxTotal'][0]['cbc:TaxAmount'], |
| 204 |
'hrextac:HRTaxSubtotal': hr_tax_subtotals, |
| 205 |
}, |
| 206 |
'hrextac:HRLegalMonetaryTotal': { |
| 207 |
'cbc:TaxExclusiveAmount': document_node['cac:LegalMonetaryTotal']['cbc:TaxExclusiveAmount'], |
| 208 |
'hrextac:OutOfScopeOfVATAmount': out_of_scope_node, |
| 209 |
} |
| 210 |
} |
| 211 |
}, |
| 212 |
} |
| 213 |
} |
| 214 |
}) |
| 215 |
|
| 216 |
def _add_invoice_line_item_nodes(self, line_node, vals): |
| 217 |
super()._add_invoice_line_item_nodes(line_node, vals) |
| 218 |
line = vals['base_line']['record'] |
| 219 |
# HR-BR-25: Each item MUST have an item classification identifier from the Classification of Products |
| 220 |
# by Activities scheme: KPD (CPA) - listID "CG", except in the case of advance payment invoices. |
| 221 |
line_node['cac:Item'].update({ |
| 222 |
'cac:CommodityClassification': { |
| 223 |
'cbc:ItemClassificationCode': { |
| 224 |
'listID': 'CG', |
| 225 |
'_text': line.l10n_hr_kpd_category_id.name |
| 226 |
} |
| 227 |
} |
| 228 |
}) |
| 229 |
|
| 230 |
def _get_party_node(self, vals): |
| 231 |
party_node = super()._get_party_node(vals) |
| 232 |
commercial_partner = vals['partner'].commercial_partner_id |
| 233 |
if commercial_partner.l10n_hr_personal_oib: |
| 234 |
if commercial_partner.l10n_hr_business_unit_code: |
| 235 |
party_node['cac:PartyIdentification']['cbc:ID'].update({ |
| 236 |
'_text': '9934:' + commercial_partner.l10n_hr_personal_oib + '::HR99:' + commercial_partner.l10n_hr_business_unit_code |
| 237 |
}) |
| 238 |
else: |
| 239 |
party_node['cac:PartyIdentification']['cbc:ID'].update({ |
| 240 |
'_text': '9934:' + commercial_partner.l10n_hr_personal_oib |
| 241 |
}) |
| 242 |
elif commercial_partner.company_registry: |
| 243 |
party_node['cac:PartyIdentification']['cbc:ID'].update({ |
| 244 |
'_text': '0088:' + commercial_partner.company_registry |
| 245 |
}) |
| 246 |
return party_node |
| 247 |
|
| 248 |
def _add_invoice_accounting_supplier_party_nodes(self, document_node, vals): |
| 249 |
super()._add_invoice_accounting_supplier_party_nodes(document_node, vals) |
| 250 |
invoice = vals['invoice'] |
| 251 |
# HR-BR-37: Invoice must contain HR-BT-4: Operator code in accordance with the Fiscalization Act. |
| 252 |
# HR-BR-9: Invoice must contain HR-BT-5: Operator OIB in accordance with the Fiscalization Act. |
| 253 |
document_node['cac:AccountingSupplierParty'].update({ |
| 254 |
'cac:SellerContact': { |
| 255 |
'cbc:ID': { |
| 256 |
'_text': invoice.l10n_hr_operator_oib |
| 257 |
}, |
| 258 |
'cbc:Name': { |
| 259 |
'_text': invoice.l10n_hr_operator_name |
| 260 |
} |
| 261 |
} |
| 262 |
}) |
| 263 |
|
| 264 |
def _ubl_default_tax_category_grouping_key(self, base_line, tax_data, vals, currency): |
| 265 |
# EXTENDS account.edi.xml.ubl_bis3 |
| 266 |
grouping_key = super()._ubl_default_tax_category_grouping_key(base_line, tax_data, vals, currency) |
| 267 |
if not grouping_key or not tax_data: |
| 268 |
return |
| 269 |
|
| 270 |
tax = tax_data['tax'] |
| 271 |
|
| 272 |
# HR-BR-11: Each document-level expense (BG-21) that is not subject to VAT or is exempt from VAT must have |
| 273 |
# a document-level expense VAT category code (HR-BT-6) from table HR-TB-2 HR VAT category codes |
| 274 |
# Instead of determining what the elements should be from the invoice details, here we directly use |
| 275 |
# the data of the VAT expence category defined on the tax by the user |
| 276 |
if ( |
| 277 |
tax.l10n_hr_tax_category_id |
| 278 |
and tax.amount_type == 'percent' |
| 279 |
and not tax.amount |
| 280 |
): |
| 281 |
grouping_key.update({ |
| 282 |
'tax_category_code': tax.l10n_hr_tax_category_id.code_untdid, |
| 283 |
'tax_exemption_reason': tax.l10n_hr_tax_category_id.description, |
| 284 |
}) |
| 285 |
|
| 286 |
if tax.tax_exigibility == 'on_payment': |
| 287 |
invoice_legal_notes_str = html2plaintext(tax.invoice_legal_notes or '') or "Obračun po naplaćenoj naknadi" |
| 288 |
else: |
| 289 |
invoice_legal_notes_str = None |
| 290 |
|
| 291 |
grouping_key.update({ |
| 292 |
'hr_category_name': tax.l10n_hr_tax_category_id.name, |
| 293 |
'invoice_legal_notes_str': invoice_legal_notes_str, |
| 294 |
}) |
| 295 |
return grouping_key |
| 296 |
|
| 297 |
def _ubl_get_tax_category_node(self, vals, tax_category): |
| 298 |
# EXTENDS account.edi.xml.ubl_bis3 |
| 299 |
node = super()._ubl_get_tax_category_node(vals, tax_category) |
| 300 |
node['cbc:Name']['_text'] = tax_category['hr_category_name'] |
| 301 |
node['hrextac:HRObracunPDVPoNaplati'] = {'_text': tax_category['invoice_legal_notes_str']} |
| 302 |
return node |
| 303 |
|
| 304 |
def _ubl_get_line_item_node_classified_tax_category_node(self, vals, tax_category): |
| 305 |
# EXTENDS account.edi.xml.ubl_bis3 |
| 306 |
node = super()._ubl_get_line_item_node_classified_tax_category_node(vals, tax_category) |
| 307 |
node['cbc:Name']['_text'] = tax_category['hr_category_name'] |
| 308 |
node['cbc:TaxExemptionReasonCode']['_text'] = tax_category.get('tax_exemption_reason_code') |
| 309 |
node['cbc:TaxExemptionReason']['_text'] = tax_category.get('tax_exemption_reason') |
| 310 |
return node |
| 311 |
|
| 312 |
def _import_fill_invoice(self, invoice, tree, qty_factor): |
| 313 |
logs = super()._import_fill_invoice(invoice, tree, qty_factor) |
| 314 |
profile_id = tree.findtext('./{*}ProfileID') |
| 315 |
invoice_values = { |
| 316 |
'l10n_hr_process_type': profile_id[:3] if profile_id[:3] == 'P99' else profile_id, |
| 317 |
'l10n_hr_customer_defined_process_name': profile_id[4:] if profile_id[:3] == 'P99' else False, |
| 318 |
} |
| 319 |
invoice.write(invoice_values) |
| 320 |
invoice.l10n_hr_edi_addendum_id.write({'fiscalization_number': tree.findtext('./{*}ID')}) |
| 321 |
return logs |
| 322 |
|
| 323 |
def _import_invoice_lines(self, invoice, tree, xpath, qty_factor): |
| 324 |
# Override to work with Croatian tax exigibility flag |
| 325 |
tax_exigibility = 'on_payment' if tree.find('.//{*}HRObracunPDVPoNaplati') is not None else 'on_invoice' |
| 326 |
logs = [] |
| 327 |
lines_values = [] |
| 328 |
for line_tree in tree.iterfind(xpath): |
| 329 |
line_values = self.with_company(invoice.company_id)._retrieve_invoice_line_vals(line_tree, invoice.move_type, qty_factor) |
| 330 |
line_values['tax_ids'], tax_logs = self._retrieve_taxes( |
| 331 |
invoice, line_values, invoice.journal_id.type, tax_exigibility, |
| 332 |
) |
| 333 |
logs += tax_logs |
| 334 |
if not line_values['product_uom_id']: |
| 335 |
line_values.pop('product_uom_id') # if no uom, pop it so it's inferred from the product_id |
| 336 |
lines_values.append(line_values) |
| 337 |
lines_values += self._retrieve_line_charges(invoice, line_values, line_values['tax_ids']) |
| 338 |
return lines_values, logs |
| 339 |
|
| 340 |
def _retrieve_line_vals(self, tree, document_type=False, qty_factor=1): |
| 341 |
line_values = super()._retrieve_line_vals(tree, document_type, qty_factor) |
| 342 |
kpd_category_code = tree.findtext('./{*}Item/{*}CommodityClassification/{*}ItemClassificationCode') |
| 343 |
if kpd_category_code: |
| 344 |
line_kpd_category = self.env['l10n_hr.kpd.category'].search([('name', '=', kpd_category_code)], limit=1) |
| 345 |
if line_kpd_category: |
| 346 |
line_values.update({ |
| 347 |
'l10n_hr_kpd_category_id': line_kpd_category.id, |
| 348 |
}) |
| 349 |
return line_values |
| 350 |
|
| 351 |
def _retrieve_rejection_reference(self, attachment): |
| 352 |
string_to_find = b'Rejected</cbc:StatusReasonCode>' |
| 353 |
if string_to_find in attachment['raw']: |
| 354 |
tree = etree.fromstring(attachment['raw']) |
| 355 |
reason_node = tree.findtext('.//{*}Response/{*}Status/{*}StatusReason') |
| 356 |
if "Electronic ID:" in reason_node: |
| 357 |
original_document_id = reason_node[reason_node.find("Electronic ID:") + 15:reason_node.find("Electronic ID:") + 22] |
| 358 |
return (original_document_id, reason_node) |
| 359 |
return 'not_found' |
| 360 |
return False |