From ec1cbb0e760725d88075db2ae2d78372c51ee4d0 Mon Sep 17 00:00:00 2001 From: Iryna Vushnevska Date: Thu, 23 Apr 2020 16:10:14 +0300 Subject: [PATCH] [IMP] add posibility to move only not reserved quantity --- stock_move_location/__manifest__.py | 5 +- .../wizard/stock_move_location.py | 70 ++++++++++++++----- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/stock_move_location/__manifest__.py b/stock_move_location/__manifest__.py index 019e55a8a..c80971308 100644 --- a/stock_move_location/__manifest__.py +++ b/stock_move_location/__manifest__.py @@ -6,10 +6,7 @@ { "name": "Move Stock Location", "version": "14.0.1.0.1", - "author": "Julius Network Solutions, " - "BCIM," - "Camptocamp," - "Odoo Community Association (OCA)", + "author": "Julius Network Solutions, BCIM, Camptocamp, Odoo Community Association (OCA)", "summary": "This module allows to move all stock " "in a stock location to an other one.", "website": "https://github.com/OCA/stock-logistics-warehouse", diff --git a/stock_move_location/wizard/stock_move_location.py b/stock_move_location/wizard/stock_move_location.py index b4984e0f4..3634014b6 100644 --- a/stock_move_location/wizard/stock_move_location.py +++ b/stock_move_location/wizard/stock_move_location.py @@ -3,6 +3,8 @@ # Copyright 2019 Sergio Teruel - Tecnativa # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) +from itertools import groupby + from odoo import api, fields, models from odoo.fields import first @@ -82,26 +84,60 @@ class StockMoveLocationWizard(models.TransientModel): quants = self.env["stock.quant"].browse( self.env.context.get("active_ids", False) ) - res["stock_move_location_line_ids"] = [ - ( - 0, - 0, - { - "product_id": quant.product_id.id, - "move_quantity": quant.quantity, - "max_quantity": quant.quantity, - "reserved_quantity": quant.reserved_quantity, - "origin_location_id": quant.location_id.id, - "lot_id": quant.lot_id.id, - "product_uom_id": quant.product_uom_id.id, - "custom": False, - }, - ) - for quant in quants - ] + res["stock_move_location_line_ids"] = self._prepare_wizard_move_lines(quants) res["origin_location_id"] = first(quants).location_id.id return res + @api.model + def _prepare_wizard_move_lines(self, quants): + res = [] + exclude_reserved_qty = self.env.context.get("only_reserved_qty", False) + if not exclude_reserved_qty: + res = [ + ( + 0, + 0, + { + "product_id": quant.product_id.id, + "move_quantity": quant.quantity, + "max_quantity": quant.quantity, + "reserved_quantity": quant.reserved_quantity, + "origin_location_id": quant.location_id.id, + "lot_id": quant.lot_id.id, + "product_uom_id": quant.product_uom_id.id, + "custom": False, + }, + ) + for quant in quants + ] + else: + # if need move only available qty per product on location + for _product, quant in groupby(quants, lambda r: r.product_id): + # we need only one quant per product + quant = list(quant)[0] + qty = quant._get_available_quantity( + quant.product_id, + quant.location_id, + ) + if qty: + res.append( + ( + 0, + 0, + { + "product_id": quant.product_id.id, + "move_quantity": qty, + "max_quantity": qty, + "reserved_quantity": quant.reserved_quantity, + "origin_location_id": quant.location_id.id, + "lot_id": quant.lot_id.id, + "product_uom_id": quant.product_uom_id.id, + "custom": False, + }, + ) + ) + return res + @api.onchange("origin_location_id") def _onchange_origin_location_id(self): if not self.env.context.get("origin_location_disable", False):