From e61125d3a8d9a160d40aab40ea529369eeeef93d Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Sun, 15 Dec 2024 14:20:59 +0100 Subject: [PATCH] feat(Sales Invoice): guess document tax rate from line item --- .../european_e_invoice/custom/sales_invoice.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/eu_einvoice/european_e_invoice/custom/sales_invoice.py b/eu_einvoice/european_e_invoice/custom/sales_invoice.py index 827c751..4e01041 100644 --- a/eu_einvoice/european_e_invoice/custom/sales_invoice.py +++ b/eu_einvoice/european_e_invoice/custom/sales_invoice.py @@ -104,6 +104,7 @@ def __init__( self.seller_contact = seller_contact self.buyer_contact = buyer_contact self.doc = None + self.item_tax_rates = set() def get_einvoice(self) -> Document | None: """Return the einvoice document as a Python object.""" @@ -360,9 +361,9 @@ def _add_line_item(self, item: SalesInvoiceItem): # [BR-AE-05] In an Invoice line (BG-25) where the Invoiced item VAT category code (BT-151) is "Reverse charge" the Invoiced item VAT rate (BT-152) shall be 0 (zero). li.settlement.trade_tax.rate_applicable_percent = 0 else: - li.settlement.trade_tax.rate_applicable_percent = get_item_rate( - item.item_tax_template, self.invoice.taxes - ) + item_tax_rate = get_item_rate(item.item_tax_template, self.invoice.taxes) + self.item_tax_rates.add(item_tax_rate) + li.settlement.trade_tax.rate_applicable_percent = item_tax_rate if li.settlement.trade_tax.rate_applicable_percent._value == 0: li.settlement.trade_tax.exemption_reason_code = vat_exemption_reason_codes.get( @@ -403,7 +404,12 @@ def _add_taxes_and_charges(self): trade_tax.rate_applicable_percent = tax_rate if len(self.invoice.taxes) == 1: + # We only have one tax, so we can use the net total as basis amount trade_tax.basis_amount = self.invoice.net_total + if len(self.item_tax_rates) == 1 and tax_rate == 0: + # We only have one tax rate on the line items, but it was not specified on the tax row + # so we use the tax rate from the line items. + trade_tax.rate_applicable_percent = self.item_tax_rates.pop() elif hasattr(tax, "net_amount"): trade_tax.basis_amount = tax.net_amount elif hasattr(tax, "custom_net_amount"):