Skip to content

Commit

Permalink
[FIX] - group pickings to plan by picking type instead of the warehouse
Browse files Browse the repository at this point in the history
  • Loading branch information
sbejaoui committed Apr 1, 2023
1 parent a590676 commit 0239264
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 43 deletions.
19 changes: 0 additions & 19 deletions shipment_advice_planner/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
3 changes: 0 additions & 3 deletions shipment_advice_planner/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
12 changes: 8 additions & 4 deletions shipment_advice_planner/tests/test_shipment_advice_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ 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):
self.wizard_form.warehouse_id = self.warehouse
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)
Expand All @@ -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)

Expand All @@ -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
Expand Down
35 changes: 18 additions & 17 deletions shipment_advice_planner/wizards/shipment_advice_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.")
Expand Down Expand Up @@ -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),
]
)

Expand Down Expand Up @@ -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,
}

0 comments on commit 0239264

Please sign in to comment.