[FIX] sale: prevent the zero division error

When converting a quotation to an invoice with a fixed down payment
amount and the total amount of the quotation is zero, a float division by
zero error occurs. This is because the total amount is zero, and it tries
to divide the fixed amount by the total amount.

This commit solves the above issue by dividing a fixed amount
by one when the total amount is zero and added the test case.

sentry-4079147910

closes odoo/odoo#119957

X-original-commit: 41d3bf23770ef1716e9d34e7930adad58a17c7de
Signed-off-by: Ansari Mahamadasif (maan) <maan@odoo.com>
This commit is contained in:
Mahamadasif Ansari
2023-04-27 16:10:39 +02:00
parent 1979e2671f
commit cd4fc2e591
2 changed files with 33 additions and 1 deletions
+32
View File
@@ -197,6 +197,38 @@ class TestSaleToInvoice(TestSaleCommon):
downpayment_line = sale_order.order_line.filtered(lambda l: l.is_downpayment and not l.display_type)
self.assertEqual(downpayment_line[0].price_unit, 50, 'The down payment unit price should not change on SO')
def test_downpayment_fixed_amount_with_zero_total_amount(self):
# Create the SO with one line and amount total is zero
sale_order = self.env['sale.order'].with_context(tracking_disable=True).create({
'partner_id': self.partner_a.id,
'partner_invoice_id': self.partner_a.id,
'partner_shipping_id': self.partner_a.id,
'pricelist_id': self.company_data['default_pricelist'].id,
'order_line': [Command.create({
'product_id': self.company_data['product_order_no'].id,
'product_uom_qty': 5,
'price_unit': 0,
'tax_id': False,
}), ]
})
sale_order.action_confirm()
sale_order.order_line.write({'qty_delivered': 5.0})
context = {
'active_model': 'sale.order',
'active_ids': [sale_order.id],
'active_id': sale_order.id,
'default_journal_id': self.company_data['default_journal_sale'].id,
}
# Let's do an invoice for a down payment of 50
downpayment = self.env['sale.advance.payment.inv'].with_context(context).create({
'advance_payment_method': 'fixed',
'fixed_amount': 50,
'deposit_account_id': self.company_data['default_account_revenue'].id
})
# Create invoice
downpayment.create_invoices()
self.assertEqual(downpayment.amount, 0.0, 'The down payment amount should be 0.0')
def test_downpayment_percentage_tax_icl(self):
""" Test invoice with a percentage downpayment and an included tax
Check the total amount of invoice is correct and equal to a respective sale order's total amount
@@ -282,7 +282,7 @@ class SaleAdvancePaymentInv(models.TransientModel):
if self.advance_payment_method == 'percentage':
percentage = self.amount / 100
else:
percentage = self.fixed_amount / order.amount_total
percentage = self.fixed_amount / order.amount_total if order.amount_total else 1
order_lines = order.order_line.filtered(lambda l: not l.display_type)
base_downpayment_lines_values = self._prepare_base_downpayment_line_values(order)