From a74346a30aeff7e62fd4f47bcf78379742fdcf6a Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Thu, 12 May 2022 14:48:12 +0200 Subject: [PATCH] [14.0][IMP] make template data overridable --- l10n_it_fatturapa_out/wizard/efattura.py | 68 ++------------- .../wizard/wizard_export_fatturapa.py | 87 +++++++++++++++++++ 2 files changed, 93 insertions(+), 62 deletions(-) diff --git a/l10n_it_fatturapa_out/wizard/efattura.py b/l10n_it_fatturapa_out/wizard/efattura.py index 8cb2a07a7ae0..1ed2fd5cd63b 100644 --- a/l10n_it_fatturapa_out/wizard/efattura.py +++ b/l10n_it_fatturapa_out/wizard/efattura.py @@ -195,67 +195,9 @@ def in_eu(partner): return False def get_all_taxes(record): - """Generate summary data for taxes. - Odoo does that for us, but only for nonzero taxes. - SdI expects a summary for every tax mentioned in the invoice, - even those with price_total == 0. - """ - - def _key(tax_id): - return tax_id.id - - out_computed = {} - # existing tax lines - tax_ids = record.line_ids.filtered(lambda line: line.tax_line_id) - for tax_id in tax_ids: - tax_line_id = tax_id.tax_line_id - aliquota = format_numbers(tax_line_id.amount) - key = _key(tax_line_id) - out_computed[key] = { - "AliquotaIVA": aliquota, - "Natura": tax_line_id.kind_id.code, - # 'Arrotondamento':'', - "ImponibileImporto": tax_id.tax_base_amount, - "Imposta": tax_id.price_total, - "EsigibilitaIVA": tax_line_id.payability, - } - if tax_line_id.law_reference: - out_computed[key]["RiferimentoNormativo"] = encode_for_export( - tax_line_id.law_reference, 100 - ) - - out = {} - # check for missing tax lines - for line in record.invoice_line_ids: - if line.display_type in ("line_section", "line_note"): - # notes and sections - # we ignore line.tax_ids altogether, - # (it is popolated with a default tax usually) - # and use another tax in the template - continue - for tax_id in line.tax_ids: - aliquota = format_numbers(tax_id.amount) - key = _key(tax_id) - if key in out_computed: - continue - if key not in out: - out[key] = { - "AliquotaIVA": aliquota, - "Natura": tax_id.kind_id.code, - # 'Arrotondamento':'', - "ImponibileImporto": line.price_subtotal, - "Imposta": 0.0, - "EsigibilitaIVA": tax_id.payability, - } - if tax_id.law_reference: - out[key]["RiferimentoNormativo"] = encode_for_export( - tax_id.law_reference, 100 - ) - else: - out[key]["ImponibileImporto"] += line.price_subtotal - out[key]["Imposta"] += 0.0 - out.update(out_computed) - return out + # wrapper to a method in wizard (for better overriding) + wiz = self.env["wizard.export.fatturapa"] + return wiz.getAllTaxes(record) def get_importo(line): str_number = str(line.discount) @@ -315,7 +257,9 @@ def fpa_to_eur(amount, invoice): }, "fpa_to_eur": fpa_to_eur, } - return template_values + + wiz = self.env["wizard.export.fatturapa"] + return wiz.getTemplateValues(template_values) def to_xml(self, env): """Create the xml file content. diff --git a/l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py b/l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py index a2b56ed95395..6ed97c7ec21c 100644 --- a/l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py +++ b/l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py @@ -13,8 +13,11 @@ from odoo import api, fields, models from odoo.exceptions import UserError +from odoo.tools import float_repr from odoo.tools.translate import _ +from odoo.addons.l10n_it_account.tools.account_tools import encode_for_export + from .efattura import EFatturaOut _logger = logging.getLogger(__name__) @@ -26,6 +29,18 @@ def id_generator( return "".join(random.choice(chars) for dummy in range(size)) +def format_numbers(number): + # format number to str with between 2 and 8 decimals (event if it's .00) + number_splited = str(number).split(".") + if len(number_splited) == 1: + return "%.02f" % number + + cents = number_splited[1] + if len(cents) > 8: + return "%.08f" % number + return float_repr(number, max(2, len(cents))) + + class WizardExportFatturapa(models.TransientModel): _name = "wizard.export.fatturapa" _description = "Export E-invoice" @@ -87,6 +102,78 @@ def getImportoTotale(self, invoice): # in the total amount of the invoice) return invoice.amount_total + @api.model + def getAllTaxes(self, invoice): + """Generate summary data for taxes. + Odoo does that for us, but only for nonzero taxes. + SdI expects a summary for every tax mentioned in the invoice, + even those with price_total == 0. + """ + + def _key(tax_id): + return tax_id.id + + out_computed = {} + # existing tax lines + tax_ids = invoice.line_ids.filtered(lambda line: line.tax_line_id) + for tax_id in tax_ids: + tax_line_id = tax_id.tax_line_id + aliquota = format_numbers(tax_line_id.amount) + key = _key(tax_line_id) + out_computed[key] = { + "AliquotaIVA": aliquota, + "Natura": tax_line_id.kind_id.code, + # 'Arrotondamento':'', + "ImponibileImporto": tax_id.tax_base_amount, + "Imposta": tax_id.price_total, + "EsigibilitaIVA": tax_line_id.payability, + } + if tax_line_id.law_reference: + out_computed[key]["RiferimentoNormativo"] = encode_for_export( + tax_line_id.law_reference, 100 + ) + + out = {} + # check for missing tax lines + for line in invoice.invoice_line_ids: + if line.display_type in ("line_section", "line_note"): + # notes and sections + # we ignore line.tax_ids altogether, + # (it is popolated with a default tax usually) + # and use another tax in the template + continue + for tax_id in line.tax_ids: + aliquota = format_numbers(tax_id.amount) + key = _key(tax_id) + if key in out_computed: + continue + if key not in out: + out[key] = { + "AliquotaIVA": aliquota, + "Natura": tax_id.kind_id.code, + # 'Arrotondamento':'', + "ImponibileImporto": line.price_subtotal, + "Imposta": 0.0, + "EsigibilitaIVA": tax_id.payability, + } + if tax_id.law_reference: + out[key]["RiferimentoNormativo"] = encode_for_export( + tax_id.law_reference, 100 + ) + else: + out[key]["ImponibileImporto"] += line.price_subtotal + out[key]["Imposta"] += 0.0 + out.update(out_computed) + return out + + @api.model + def getTemplateValues(self, template_values): + """ + Entry point for other modules to override values + (and helper functions) passed to template + """ + return template_values + def group_invoices_by_partner(self): def split_list(my_list, size): it = iter(my_list)