diff --git a/shipment_advice_planner/hooks.py b/shipment_advice_planner/hooks.py index 981a2fff..d26405ff 100644 --- a/shipment_advice_planner/hooks.py +++ b/shipment_advice_planner/hooks.py @@ -33,22 +33,3 @@ def pre_init_hook(cr): """ ) _logger.info(f"{cr.rowcount} rows updated in stock_picking") - - if not sql.column_exists(cr, "stock_picking", "warehouse_id"): - _logger.info("Creating column warehouse_id into stock_picking") - cr.execute( - """ - ALTER TABLE stock_picking ADD COLUMN warehouse_id integer; - """ - ) - cr.execute( - """ - UPDATE stock_picking - SET warehouse_id = stock_location.warehouse_id - FROM stock_location - WHERE - stock_picking.location_id = stock_location.id - AND stock_location.warehouse_id is not null; - """ - ) - _logger.info(f"{cr.rowcount} rows updated in stock_picking") diff --git a/shipment_advice_planner/models/stock_picking.py b/shipment_advice_planner/models/stock_picking.py index 99a11548..b57d3930 100644 --- a/shipment_advice_planner/models/stock_picking.py +++ b/shipment_advice_planner/models/stock_picking.py @@ -7,9 +7,6 @@ class StockPicking(models.Model): _inherit = "stock.picking" - warehouse_id = fields.Many2one( - comodel_name="stock.warehouse", related="location_id.warehouse_id", store=True - ) can_be_planned_in_shipment_advice = fields.Boolean( compute="_compute_can_be_planned_in_shipment_advice", store=True ) diff --git a/shipment_advice_planner/tests/test_shipment_advice_planner.py b/shipment_advice_planner/tests/test_shipment_advice_planner.py index 8edbf287..bb1547e3 100644 --- a/shipment_advice_planner/tests/test_shipment_advice_planner.py +++ b/shipment_advice_planner/tests/test_shipment_advice_planner.py @@ -33,7 +33,7 @@ def test_shipment_advice_planner_multi_warehouse(self): self.assertEqual(len(shipments), 2) self.assertEqual(len(shipments.mapped("warehouse_id")), 2) self.assertEqual( - shipments.mapped("warehouse_id"), self.pickings.mapped("warehouse_id") + shipments.warehouse_id, self.pickings.picking_type_id.warehouse_id ) def test_shipment_advice_planner_one_warehouse(self): @@ -41,7 +41,9 @@ def test_shipment_advice_planner_one_warehouse(self): self.assertEqual(len(self.wizard_form.picking_to_plan_ids), 9) wizard = self.wizard_form.save() action = wizard.button_plan_shipments() - self.assertEqual(wizard.picking_to_plan_ids.warehouse_id, self.warehouse) + self.assertEqual( + wizard.picking_to_plan_ids.picking_type_id.warehouse_id, self.warehouse + ) shipment = self.env[action.get("res_model")].search(action.get("domain")) self.assertEqual(len(shipment), 1) self.assertEqual(shipment.warehouse_id, self.warehouse) @@ -55,7 +57,9 @@ def test_shipment_advice_planner_dock(self): self.wizard_form.dock_id = self.dock wizard = self.wizard_form.save() action = wizard.button_plan_shipments() - self.assertEqual(wizard.picking_to_plan_ids.warehouse_id, self.warehouse) + self.assertEqual( + wizard.picking_to_plan_ids.picking_type_id.warehouse_id, self.warehouse + ) shipment = self.env[action.get("res_model")].search(action.get("domain")) self.assertEqual(shipment.dock_id, self.dock) @@ -66,7 +70,7 @@ def test_check_warehouse(self): ): self.wizard_form.picking_to_plan_ids.add( self.pickings.filtered( - lambda p, w=self.warehouse2: p.warehouse_id == w + lambda p, w=self.warehouse2: p.picking_type_id.warehouse_id == w )[0] ) self.wizard_form.warehouse_id = self.warehouse2 diff --git a/shipment_advice_planner/wizards/shipment_advice_planner.py b/shipment_advice_planner/wizards/shipment_advice_planner.py index 6c3cd3a9..084b62d5 100644 --- a/shipment_advice_planner/wizards/shipment_advice_planner.py +++ b/shipment_advice_planner/wizards/shipment_advice_planner.py @@ -14,7 +14,7 @@ class ShipmentAdvicePlanner(models.TransientModel): string="Pickings to plan", required=True, domain='[("can_be_planned_in_shipment_advice", "=", True),' - '("warehouse_id", "=?", warehouse_id),]', + '("picking_type_id.warehouse_id", "=?", warehouse_id),]', compute="_compute_picking_to_plan_ids", store=True, readonly=False, @@ -38,7 +38,8 @@ def _check_warehouse(self): ) if ( rec.picking_to_plan_ids - and rec.picking_to_plan_ids.warehouse_id != rec.warehouse_id + and rec.picking_to_plan_ids.picking_type_id.warehouse_id + != rec.warehouse_id ): raise ValidationError( _("The transfers don't belong to the selected warehouse.") @@ -80,7 +81,7 @@ def _compute_picking_to_plan_ids(self): [ ("id", "in", active_ids), ("can_be_planned_in_shipment_advice", "=", True), - ("warehouse_id", "=?", self.warehouse_id.id), + ("picking_type_id.warehouse_id", "=?", self.warehouse_id.id), ] ) @@ -112,40 +113,40 @@ def _plan_shipments_for_method(self): shipment_advice_model = self.env["shipment.advice"] create_vals = [] for ( - warehouse, + picking_type, pickings_to_plan, - ) in self._get_picking_to_plan_by_warehouse().items(): - create_vals.extend(prepare_method(warehouse, pickings_to_plan)) + ) in self._get_picking_to_plan_by_picking_type().items(): + create_vals.extend(prepare_method(picking_type, pickings_to_plan)) return shipment_advice_model.create(create_vals) def _get_prepare_method_name(self): return f"_prepare_shipment_advice_{self.shipment_planning_method}_vals_list" - def _get_picking_to_plan_by_warehouse(self): + def _get_picking_to_plan_by_picking_type(self): self.ensure_one() - warehouse_model = self.env["stock.warehouse"] + picking_type_model = self.env["stock.picking.type"] picking_model = self.env["stock.picking"] res = {} for group in picking_model.read_group( [("id", "in", self.picking_to_plan_ids.ids)], - ["warehouse_id"], - ["warehouse_id"], + ["picking_type_id"], + ["picking_type_id"], ): - warehouse = warehouse_model.browse(group.get("warehouse_id")[0]) - res[warehouse] = picking_model.search(group.get("__domain")) + picking_type = picking_type_model.browse(group.get("picking_type_id")[0]) + res[picking_type] = picking_model.search(group.get("__domain")) return res - def _prepare_shipment_advice_simple_vals_list(self, warehouse, pickings_to_plan): + def _prepare_shipment_advice_simple_vals_list(self, picking_type, pickings_to_plan): self.ensure_one() - vals = self._prepare_shipment_advice_common_vals(warehouse) + vals = self._prepare_shipment_advice_common_vals(picking_type) vals["planned_move_ids"] = [Command.set(pickings_to_plan.move_ids.ids)] return [vals] - def _prepare_shipment_advice_common_vals(self, warehouse): + def _prepare_shipment_advice_common_vals(self, picking_type): self.ensure_one() return { "shipment_type": "outgoing", - "warehouse_id": warehouse.id, + "warehouse_id": picking_type.warehouse_id.id, "dock_id": self.dock_id.id, - "company_id": warehouse.company_id.id, + "company_id": picking_type.company_id.id, }