[FIX] stock-account: UserError on div by zero

With:
- Anglo-saxon activated,
- Costing Method: FIFO,
- Inventory Valuation: Automated.

Create a PO for product AAA, receive the product and archive it,
create and try to POST the vendor bill.
> This lead to a ZeroDivisionError: float division by zero

The anglo-saxon entries cannot be generated as the product
is not valued anymore.

This commit is displaying a clearer error message for end users.

more info from SLE on 2250451
opw-2307562, 2317338, 2320080, 2311843

Fine tuning of https://github.com/odoo/odoo/commit/8ce4db8ca04cd8e5266fc186b35deef76506da46

closes odoo/odoo#57016

X-original-commit: 83239af1d396cf7f5aca37e33445405238fcfc48
Signed-off-by: Simon Goffin (sig) <sig@openerp.com>
Co-authored-by: simongoffin <sig@odoo.com>
This commit is contained in:
Abridbus
2020-09-03 10:00:46 +00:00
committed by Goffin Simon
co-authored by simongoffin
parent 47b0c0d2d4
commit 88ccd2dfbd
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
from odoo.tools.float_utils import float_compare
from odoo import api, fields, models, _
from odoo.tools.float_utils import float_compare, float_is_zero
from odoo.exceptions import UserError
class AccountMove(models.Model):
@@ -82,6 +83,9 @@ class AccountMove(models.Model):
move.company_id, valuation_date, round=False,
)
valuation_total_qty += layers_qty
if float_is_zero(valuation_total_qty, precision_rounding=line.product_uom_id.rounding or line.product_id.uom_id.rounding):
raise UserError(_('Odoo is not able to generate the anglo saxon entries. The total valuation of %s is zero.') % line.product_id.display_name)
valuation_price_unit = valuation_price_unit_total / valuation_total_qty
valuation_price_unit = line.product_id.uom_id._compute_price(valuation_price_unit, line.product_uom_id)