diff --git a/stock_picking_auto_create_lot/models/stock_move_line.py b/stock_picking_auto_create_lot/models/stock_move_line.py index 2635505f..6addbcc2 100644 --- a/stock_picking_auto_create_lot/models/stock_move_line.py +++ b/stock_picking_auto_create_lot/models/stock_move_line.py @@ -2,17 +2,20 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import models from odoo.fields import first +from odoo.tools import OrderedSet +from odoo.tools.float_utils import float_compare class StockMoveLine(models.Model): _inherit = "stock.move.line" - def _get_value_production_lot(self): - res = super()._get_value_production_lot() - if "name" in res and not res["name"]: - del res["name"] - return res + def _prepare_auto_lot_values(self): + """ + Prepare multi valued lots per line to use multi creation. + """ + self.ensure_one() + return {"product_id": self.product_id.id, "company_id": self.company_id.id} def set_lot_auto(self): """ @@ -24,8 +27,7 @@ def set_lot_auto(self): stock_lot_obj = self.env["stock.lot"] lots_by_product = dict() for line in self: - # Prepare multi valued lots per line to use multi creation. - values.append(line._get_value_production_lot()) + values.append(line._prepare_auto_lot_values()) lots = stock_lot_obj.create(values) for lot in lots: if lot.product_id.id not in lots_by_product: @@ -37,3 +39,25 @@ def set_lot_auto(self): line.lot_id = lot if lot.product_id.tracking == "serial": lots_by_product[line.product_id.id] -= lot + + def _action_done(self): + ml_ids_to_delete = OrderedSet() + for ml in self: + qty_done_float_compared = float_compare( + ml.qty_done, 0, precision_rounding=ml.product_uom_id.rounding + ) + if qty_done_float_compared != 0.00 or ml.is_inventory: + continue + if ( + ml.product_id.tracking == "serial" + and ml.product_id.auto_create_lot + and ml.move_id.picking_type_id.auto_create_lot + ): + ml_ids_to_delete.add(ml.id) + mls_to_delete = self.env["stock.move.line"].browse(ml_ids_to_delete) + if mls_to_delete: + for ml in mls_to_delete: + lot = self.env["stock.lot"].search([("id", "=", ml.lot_id.id)]) + if lot.product_qty == 0.00: + lot.sudo().unlink() + return super(StockMoveLine, self)._action_done()