diff --git a/addons/stock/models/stock_inventory.py b/addons/stock/models/stock_inventory.py index acccd8a90a447..7b3166169bdec 100644 --- a/addons/stock/models/stock_inventory.py +++ b/addons/stock/models/stock_inventory.py @@ -384,12 +384,11 @@ def _generate_moves(self): moves = self.env['stock.move'] Quant = self.env['stock.quant'] for line in self: - neg_quants = self.env['stock.quant'] neg_quants = Quant.search([('qty', '<', 0.0), ('product_id', '=', line.product_id.id), ('location_id', '=', line.location_id.id), ('package_id', '=', line.package_id.id), ('lot_id', '=', line.prod_lot_id.id), ('owner_id', '=', line.partner_id.id)]) if float_utils.float_compare(line.theoretical_qty, line.product_qty, precision_rounding=line.product_id.uom_id.rounding) == 0 and not neg_quants: - continue + continue neg_quant_qty = - sum([x.qty for x in neg_quants]) if float_utils.float_compare(line.theoretical_qty, 0, precision_rounding=line.product_id.uom_id.rounding) < 0: @@ -397,6 +396,9 @@ def _generate_moves(self): diff = line.theoretical_qty - line.product_qty if diff > 0: neg_quant_qty -= diff + if neg_quant_qty < 0: + neg_quant_qty = 0 + vals = { 'name': _('INV:') + (line.inventory_id.name or ''), 'product_id': line.product_id.id, @@ -407,31 +409,31 @@ def _generate_moves(self): 'state': 'confirmed', 'restrict_lot_id': line.prod_lot_id.id, 'restrict_partner_id': line.partner_id.id} - move_neg = False - move_pos = False + move_add = False + move_rem = False if diff < 0 or neg_quant_qty: # found more than expected vals['location_id'] = line.product_id.property_stock_inventory.id vals['location_dest_id'] = line.location_id.id vals['product_uom_qty'] = abs(diff) + neg_quant_qty - move_neg = moves.create(vals) + move_add = moves.create(vals) if diff > 0 or neg_quant_qty: vals['location_id'] = line.location_id.id vals['location_dest_id'] = line.product_id.property_stock_inventory.id vals['product_uom_qty'] = diff + neg_quant_qty - move_pos = moves.create(vals) + move_rem = moves.create(vals) - if move_pos: + if move_rem: domain = [('qty', '>', 0.0), ('package_id', '=', line.package_id.id), ('lot_id', '=', line.prod_lot_id.id), ('location_id', '=', line.location_id.id)] preferred_domain_list = [[('reservation_id', '=', False)], [('reservation_id.inventory_id', '!=', line.inventory_id.id)]] - quants = Quant.quants_get_preferred_domain(move_pos.product_qty, move_pos, domain=domain, preferred_domain_list=preferred_domain_list) - Quant.quants_reserve(quants, move_pos) - if move_neg and line.package_id: - move_neg.action_done() - move_neg.quant_ids.write({'package_id': line.package_id.id}) - quants = Quant.search([('qty', '<', 0.0), ('product_id', '=', move_neg.product_id.id), - ('location_id', '=', move_neg.location_dest_id.id), ('package_id', '!=', False)], limit=1) + quants = Quant.quants_get_preferred_domain(move_rem.product_qty, move_rem, domain=domain, preferred_domain_list=preferred_domain_list) + Quant.quants_reserve(quants, move_rem) + if move_add and line.package_id: + move_add.action_done() + move_add.quant_ids.write({'package_id': line.package_id.id}) + quants = Quant.search([('qty', '<', 0.0), ('product_id', '=', move_add.product_id.id), + ('location_id', '=', move_add.location_dest_id.id), ('package_id', '!=', False)], limit=1) if quants: - for quant in move_neg.quant_ids: - if quant.location_id.id == move_neg.location_dest_id.id: #To avoid we take a quant that was reconcile already - quant._quant_reconcile_negative(move_neg) + for quant in move_add.quant_ids: + if quant.location_id.id == move_add.location_dest_id.id: #To avoid we take a quant that was reconcile already + quant._quant_reconcile_negative(move_add) return moves diff --git a/addons/stock_account/views/product_views.xml b/addons/stock_account/views/product_views.xml index 14c09c7f396c1..6077f51f5847a 100644 --- a/addons/stock_account/views/product_views.xml +++ b/addons/stock_account/views/product_views.xml @@ -50,7 +50,7 @@ groups="stock_account.group_inventory_valuation"/>
-