diff --git a/ssi_stock/models/stock_picking.py b/ssi_stock/models/stock_picking.py index 2ec3b53..7df07f9 100644 --- a/ssi_stock/models/stock_picking.py +++ b/ssi_stock/models/stock_picking.py @@ -33,13 +33,15 @@ class StockPicking(models.Model): allowed_source_location_ids = fields.Many2many( string="Allowed Source Locations", comodel_name="stock.location", - related="picking_type_id.allowed_source_location_ids", + compute="_compute_allowed_source_location_ids", + related=False, store=False, ) allowed_destination_location_ids = fields.Many2many( string="Allowed Destination Locations", comodel_name="stock.location", - related="picking_type_id.allowed_destination_location_ids", + compute="_compute_allowed_destination_location_ids", + related=False, store=False, ) allowed_product_category_ids = fields.Many2many( @@ -94,6 +96,58 @@ def _compute_policy(self): _super = super(StockPicking, self) _super._compute_policy() + @api.depends( + "picking_type_id", + ) + def _compute_allowed_source_location_ids(self): + Location = self.env["stock.location"] + for record in self: + result = [] + if record.picking_type_id: + ptype = record.picking_type_id + result += ptype.allowed_source_location_ids.ids + + for loc_type in ptype.allowed_source_location_type_ids: + criteria = False + if loc_type.is_warehouse_location and ptype.warehouse_id: + criteria = [ + ("type_id", "=", loc_type.id), + ("warehouse_id", "=", ptype.warehouse_id.id), + ] + elif not loc_type.is_warehouse_location: + criteria = [ + ("type_id", "=", loc_type.id), + ] + if criteria: + result += Location.search(criteria).ids + record.allowed_source_location_ids = result + + @api.depends( + "picking_type_id", + ) + def _compute_allowed_destination_location_ids(self): + Location = self.env["stock.location"] + for record in self: + result = [] + if record.picking_type_id: + ptype = record.picking_type_id + result += ptype.allowed_destination_location_ids.ids + + for loc_type in ptype.allowed_destination_location_type_ids: + criteria = False + if loc_type.is_warehouse_location and ptype.warehouse_id: + criteria = [ + ("type_id", "=", loc_type.id), + ("warehouse_id", "=", ptype.warehouse_id.id), + ] + elif not loc_type.is_warehouse_location: + criteria = [ + ("type_id", "=", loc_type.id), + ] + if criteria: + result += Location.search(criteria).ids + record.allowed_destination_location_ids = result + @api.model def _get_policy_field(self): res = super(StockPicking, self)._get_policy_field() diff --git a/ssi_stock/models/stock_picking_type.py b/ssi_stock/models/stock_picking_type.py index 887fd6d..79535ef 100644 --- a/ssi_stock/models/stock_picking_type.py +++ b/ssi_stock/models/stock_picking_type.py @@ -43,6 +43,20 @@ class StockPickingType(models.Model): column1="picking_type_id", column2="location_id", ) + allowed_source_location_type_ids = fields.Many2many( + string="Allowed Source Location Types", + comodel_name="location_type", + relation="rel_picking_type_2_source_location_type", + column1="picking_type_id", + column2="location_type_id", + ) + allowed_destination_location_type_ids = fields.Many2many( + string="Allowed Destination Location Types", + comodel_name="location_type", + relation="rel_picking_type_2_destination_location_type", + column1="picking_type_id", + column2="location_type_id", + ) allowed_product_category_ids = fields.Many2many( string="Allowed Product Categories", comodel_name="product.category", diff --git a/ssi_stock/views/stock_picking_type_views.xml b/ssi_stock/views/stock_picking_type_views.xml index f2ec0f0..7937ed0 100644 --- a/ssi_stock/views/stock_picking_type_views.xml +++ b/ssi_stock/views/stock_picking_type_views.xml @@ -63,12 +63,20 @@ + +