Skip to content

Commit

Permalink
[14.0][IMP] l10n_it_fatturapa_out: make template data overridable
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMule71 committed Jul 15, 2022
1 parent d71d34c commit ecda656
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 83 deletions.
15 changes: 6 additions & 9 deletions l10n_it_fatturapa_out/data/invoice_it_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -648,16 +648,13 @@ e 'line' per riga di fattura (a seconda del livello in cui sono chiamati)
<!-- <Data t-esc=""/>-->
<!-- <TotalePercorso t-esc=""/>-->
<!-- </DatiVeicoli>-->
<t
t-set="payments"
t-value="record.line_ids.filtered(lambda line: line.account_id.user_type_id.type in ('receivable', 'payable'))"
/>
<t t-set="payments" t-value="get_payments(record)" />
<DatiPagamento t-if="record.invoice_payment_term_id and payments">
<!-- <CondizioniPagamento><t t-if="len(payments) == 1">TP02</t><t t-else="">TP01</t></CondizioniPagamento>-->
<CondizioniPagamento
t-esc="record.invoice_payment_term_id.fatturapa_pt_id.code"
/>
<t t-foreach="payments" t-as="move_line">
<t t-foreach="payments" t-as="payment">
<DettaglioPagamento>
<t
t-set="company_bank_account"
Expand All @@ -670,16 +667,16 @@ e 'line' per riga di fattura (a seconda del livello in cui sono chiamati)
<!-- <DataRiferimentoTerminiPagamento t-esc=""/>-->
<!-- <GiorniTerminiPagamento t-esc=""/>-->
<DataScadenzaPagamento
t-if="move_line.date_maturity"
t-esc="format_date(move_line.date_maturity)"
t-if="payment.date_maturity"
t-esc="format_date(payment.date_maturity)"
/>
<ImportoPagamento
t-if="record.company_id.xml_divisa_value == 'keep_orig'"
t-esc="format_numbers_two(move_line.amount_currency or move_line.debit)"
t-esc="format_numbers_two(payment.amount_currency or payment.debit)"
/>
<ImportoPagamento
t-if="not record.company_id.xml_divisa_value == 'keep_orig'"
t-esc="format_numbers_two(fpa_to_eur(move_line.amount_currency or move_line.debit, record))"
t-esc="format_numbers_two(fpa_to_eur(payment.amount_currency or payment.debit, record))"
/>

<!-- <CodUfficioPostale t-esc=""/>-->
Expand Down
97 changes: 24 additions & 73 deletions l10n_it_fatturapa_out/wizard/efattura.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ def __call__(self, *args, **kwargs):
DEFAULT_INVOICE_ITALIAN_DATE_FORMAT = "%Y-%m-%d"


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 EFatturaOut:

_validator = FPAValidator()
Expand All @@ -76,17 +88,6 @@ def format_monetary(number, currency):
# (e.g. 90.85000000000001).
return float_repr(number, min(2, currency.decimal_places))

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)))

def format_numbers_two(number):
# format number to str with 2 (event if it's .00)
return "%.02f" % number
Expand Down Expand Up @@ -195,67 +196,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)
Expand All @@ -269,6 +212,11 @@ def get_importo_totale(invoice):
wiz = self.env["wizard.export.fatturapa"]
return wiz.getImportoTotale(invoice)

def get_payments(invoice):
# wrapper to a method in wizard (for better overriding)
wiz = self.env["wizard.export.fatturapa"]
return wiz.getPayments(invoice)

def fpa_to_eur(amount, invoice):
currency = invoice.currency_id
euro = self.env.ref("base.EUR")
Expand Down Expand Up @@ -310,12 +258,15 @@ def fpa_to_eur(amount, invoice):
"wizard": self.wizard,
"get_importo": get_importo,
"get_importo_totale": get_importo_totale,
"get_payments": get_payments,
"all_taxes": {
invoice.id: get_all_taxes(invoice) for invoice in self.invoices
},
"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.
Expand Down
101 changes: 100 additions & 1 deletion l10n_it_fatturapa_out/wizard/wizard_export_fatturapa.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
from odoo.exceptions import UserError
from odoo.tools.translate import _

from .efattura import EFatturaOut
from odoo.addons.l10n_it_account.tools.account_tools import encode_for_export

from .efattura import EFatturaOut, format_numbers

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -74,6 +76,31 @@ def getPartnerId(self, invoice_ids):

return partner

@api.model
def getPayments(self, invoice):
"""Entry point for other modules to override computation of
DettaglioPagamento
We use a specialized class to allow other modules change
values w/o altering the original lines"""

class _Payment:
__slots__ = "date_maturity", "amount_currency", "debit"

def __init__(self, date_maturity, amount_currency, debit):
self.date_maturity = date_maturity
self.amount_currency = amount_currency
self.debit = debit

payments = []
for line in invoice.line_ids.filtered(
lambda line: line.account_id.user_type_id.type in ("receivable", "payable")
):
payments.append(
_Payment(line.date_maturity, line.amount_currency, line.debit)
)
return payments

@api.model
def getImportoTotale(self, invoice):
"""Entry point for other modules to override computation of
Expand All @@ -87,6 +114,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)
Expand Down

0 comments on commit ecda656

Please sign in to comment.