odoo18/custom_addons/costing_mrp_bom/models/stock_quant.py

39 lines
1.6 KiB
Python

from odoo import fields, models, _, api
from odoo.tools import float_round, float_compare
from odoo.exceptions import ValidationError, UserError
class StockQuant(models.Model):
_inherit = "stock.quant"
@api.constrains("product_id", "quantity")
def check_negative_qty(self):
if self.env.context.get("skip_negative_qty_check"):
return
p = self.env["decimal.precision"].precision_get("Product Unit of Measure")
for quant in self:
if (
float_compare(quant.quantity, 0, precision_digits=p) == -1
and quant.product_id.type == "consu"
and quant.location_id.usage in ["internal", "transit"]
):
msg_add = ""
if quant.lot_id:
msg_add = _(" lot {}").format(quant.lot_id.name_get()[0][1])
raise ValidationError(
_(
"You cannot validate this stock operation because the "
"stock level of the product '{name}'{name_lot} would "
"become negative "
"({q_quantity}) on the stock location '{complete_name}' "
"and negative stock is "
"not allowed for this product and/or location."
).format(
name=quant.product_id.display_name,
name_lot=msg_add,
q_quantity=quant.quantity,
complete_name=quant.location_id.complete_name,
)
)