[FIX] l10n_es_edi_tbai: fix discount computation traceback

Currently there is a traceback in case of a 100% discount on an invoice line.
This commit fixes the issue by computing the discount differently.

Previously it was tried to calculate the discount amount from the
discounted value and the discount factor.
This is (mathematically) not possible if the discounted value is 0.
After this commit we compute and use the undiscounted value in case
the discount is 100% to compute the discount amount.

The computation was adapted from '_prepare_edi_vals_to_export' from account.move.line

Reproduce
  1. Install l10n_es_edi_tbai
  2. Select the Spanish company
  3. Settings > Accounting: Ensure "Test Mode" is set in Spain Localization section
  4. Create a new invoice with Spanish customer
  5. Add a line with a 100% discount
  6. Confirm the invoice
  7. Process the invocie with TicketBAI
  8. Error / Traceback

opw-3572426

closes odoo/odoo#144571

X-original-commit: 74aa7136fea8bd0f962c577fba89bbd4f7618742
Signed-off-by: Cedric Snauwaert <csn@odoo.com>
Signed-off-by: Sven Führ (svfu) <svfu@odoo.com>
This commit is contained in:
Sven Fuehr
2023-12-01 19:25:32 +00:00
parent f3d565738f
commit bf9e47e479
@@ -320,7 +320,12 @@ class AccountEdiFormat(models.Model):
refund_sign = (1 if values['is_refund'] else -1)
invoice_lines = []
for line in invoice.invoice_line_ids.filtered(lambda line: line.display_type not in ('line_section', 'line_note')):
discount = line.balance / (1 - line.discount / 100) * line.discount / 100
if line.discount == 100.0:
gross_price_subtotal = line.price_unit * line.quantity
else:
gross_price_subtotal = line.price_subtotal / (1 - line.discount / 100.0)
discount = (gross_price_subtotal - line.price_subtotal) * refund_sign
if not any([t.l10n_es_type == 'sujeto_isp' for t in line.tax_ids]):
total = line.price_total * abs(line.balance / line.amount_currency if line.amount_currency != 0 else 1) * -refund_sign
else: