From 04cfc5a47c8e8e4795faa0aa92aa4dd6f54799d4 Mon Sep 17 00:00:00 2001 From: Raimon Esteve Date: Thu, 20 Jul 2023 09:21:19 +0200 Subject: [PATCH] Replace euro digits to Double Up to Eight 160937 --- invoice.py | 21 +++++++++++++++++++++ template_facturae_3.2.2.xml | 34 +++++++++++++++++----------------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/invoice.py b/invoice.py index c991727..bd9ff57 100644 --- a/invoice.py +++ b/invoice.py @@ -9,6 +9,7 @@ import random import xmlsig import hashlib import datetime +import math from decimal import Decimal from jinja2 import Environment, FileSystemLoader from lxml import etree @@ -297,6 +298,7 @@ class Invoice(metaclass=PoolMeta): """Return the content to render in factura-e XML file""" pool = Pool() Currency = pool.get('currency.currency') + Invoice = pool.get('account.invoice') Date = pool.get('ir.date') Rate = pool.get('currency.currency.rate') @@ -436,6 +438,7 @@ class Invoice(metaclass=PoolMeta): 'invoice': self, 'Decimal': Decimal, 'Currency': Currency, + 'Invoice': Invoice, 'euro': euro, 'exchange_rate': exchange_rate, 'exchange_rate_date': exchange_rate_date, @@ -718,6 +721,24 @@ class Invoice(metaclass=PoolMeta): return signed_file_content + @classmethod + def double_up_to_eight(cls, value): + # return max 8 digits in DoubleUpToEightDecimalType + _TOTAL_DIGITS = 8 + def digits_decimal(value): + num_str = str(value) + if '.' in num_str: + num_digits = num_str[::-1].find('.') + else: + num_digits = 0 + + return num_digits + + if digits_decimal(value) > _TOTAL_DIGITS: + precision = 10 ** -_TOTAL_DIGITS + return math.floor(float(value) / precision) * precision + return value + class InvoiceLine(metaclass=PoolMeta): __name__ = 'account.invoice.line' diff --git a/template_facturae_3.2.2.xml b/template_facturae_3.2.2.xml index c969731..2570764 100644 --- a/template_facturae_3.2.2.xml +++ b/template_facturae_3.2.2.xml @@ -161,7 +161,7 @@ {{ invoice.currency.code.upper() }} {% if invoice.currency != euro %} - {{ Currency.compute(invoice.currency, exchange_rate, euro) }} + {{ Invoice.double_up_to_eight(exchange_rate) }} {{ exchange_rate_date }} {% endif %} @@ -173,15 +173,15 @@ {% for invoice_tax in invoice.taxes_outputs %} {{ invoice_tax.tax.report_type }} - {{ Currency.compute(invoice.currency, invoice_tax.tax.rate * 100, euro) }} + {{ Invoice.double_up_to_eight(invoice_tax.tax.rate * 100) }} - {{ Currency.compute(invoice.currency, invoice_tax.base, euro) }} + {{ Invoice.double_up_to_eight(invoice_tax.base) }} {% if invoice.currency != euro %} - {{ Currency.compute(invoice.currency, invoice_tax.base, euro) }} + {{ Invoice.double_up_to_eight(invoice_tax.base) }} {% endif %} - {{ Currency.compute(invoice.currency, invoice_tax.amount, euro) }} + {{ Invoice.double_up_to_eight(invoice_tax.amount) }} {% if invoice.currency != euro %} {{ Currency.compute(invoice.currency, invoice_tax.amount, euro) }} {% endif %} @@ -208,7 +208,7 @@ {% for invoice_tax in invoice.taxes_withheld %} {{ invoice_tax.tax.report_type }} - {{ Currency.compute(invoice.currency, invoice_tax.tax.rate * 100, euro) }} + {{ Invoice.double_up_to_eight(invoice_tax.tax.rate * 100) }} {{ Currency.compute(invoice.currency, invoice_tax.base, euro) }} {% if invoice.currency != euro %} @@ -226,13 +226,13 @@ {% endif %} - {{ Currency.compute(invoice.currency, invoice.untaxed_amount, euro) }} + {{ Invoice.double_up_to_eight(invoice.untaxed_amount) }} {# TODO: GeneralDiscounts and TotalGeneralDiscounts (account_invoice_discount_global) not supported #} {# TODO: GeneralSurcharges and TotalGeneralSurcharges not supported #} - {{ Currency.compute(invoice.currency, invoice.untaxed_amount, euro) }} - {{ Currency.compute(invoice.currency, invoice.taxes_outputs | sum(attribute='amount', start=Decimal(0)), euro) }} - {{ Currency.compute(invoice.currency, invoice.taxes_withheld | sum(attribute='amount', start=Decimal(0)), euro) }} - {{ Currency.compute(invoice.currency, invoice.total_amount, euro) }} + {{ Invoice.double_up_to_eight(invoice.untaxed_amount) }} + {{ Invoice.double_up_to_eight(invoice.taxes_outputs | sum(attribute='amount', start=Decimal(0))) }} + {{ Invoice.double_up_to_eight(invoice.taxes_withheld | sum(attribute='amount', start=Decimal(0))) }} + {{ Invoice.double_up_to_eight(invoice.total_amount) }} {# TODO: optional, not supported - Subsidies - PaymentsOnAccount, TotalPaymentsOnAccount @@ -240,8 +240,8 @@ - TotalFinancialExpenses (account_payment_type_cost?) - AmountsWithheld #} - {{ Currency.compute(invoice.currency, invoice.total_amount, euro) }} - {{ Currency.compute(invoice.currency, invoice.total_amount, euro) }} + {{ Invoice.double_up_to_eight(invoice.total_amount) }} + {{ Invoice.double_up_to_eight(invoice.total_amount) }} {% for line in invoice.lines if line.type == 'line' %} @@ -258,19 +258,19 @@ {{ line.facturae_item_description }} {{ line.quantity }} {{ UOM_CODE2TYPE.get(line.unit.symbol, '05') if line.unit else '05' }} - {{ Currency.compute(invoice.currency, line.unit_price, euro) }} - {{ Currency.compute(invoice.currency, line.amount, euro) }} + {{ Invoice.double_up_to_eight(line.unit_price) }} + {{ Invoice.double_up_to_eight(line.amount) }} {# TODO: optional, not supported - DiscountsAndRebates (account_invoice_discount) - Charges #} - {{ Currency.compute(invoice.currency, line.amount, euro) }} + {{ Invoice.double_up_to_eight(line.amount) }} {% if line.taxes_withheld %} {% for line_tax in invoice.taxes_withheld %} {{ line_tax.tax.report_type }} - {{ Currency.compute(invoice.currency, line_tax.tax.rate * 100, euro) }} + {{ Invoice.double_up_to_eight(line_tax.tax.rate * 100) }} {{ Currency.compute(invoice.currency, line.amount, euro) }} {% if invoice.currency != euro %}