Skip to content

Commit

Permalink
[IMP] fieldservice_stock: populate fsm.order.warehouse_id from the re…
Browse files Browse the repository at this point in the history
…s.territory
  • Loading branch information
ivantodorovich committed Dec 3, 2024
1 parent b56c1bb commit 0410172
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
30 changes: 21 additions & 9 deletions fieldservice_stock/models/fsm_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
class FSMOrder(models.Model):
_inherit = "fsm.order"

@api.model
def _default_warehouse_id(self):
company = self.env.user.company_id
warehouse_ids = self.env["stock.warehouse"].search(
[("company_id", "=", company.id)], limit=1
)
return warehouse_ids and warehouse_ids.id

@api.model
def _get_move_domain(self):
return [("picking_id.picking_type_id.code", "in", ("outgoing", "incoming"))]
Expand All @@ -32,8 +24,11 @@ def _get_move_domain(self):
warehouse_id = fields.Many2one(
"stock.warehouse",
string="Warehouse",
compute="_compute_warehouse_id",
precompute=True,
store=True,
readonly=False,
required=True,
default=_default_warehouse_id,
help="Warehouse used to ship the materials",
)
return_count = fields.Integer(
Expand All @@ -43,6 +38,23 @@ def _get_move_domain(self):
"stock.move", "fsm_order_id", string="Operations", domain=_get_move_domain
)

@api.depends("company_id", "territory_id")
def _compute_warehouse_id(self):
"""Compute the warehouse from the territory"""
for rec in self:
# If there's already a warehouse set matching the company, keep it.
if rec.warehouse_id and rec.warehouse_id.company_id == rec.company_id:
continue
# If the territory's warehouse is set and it matches the company, use it.
if rec.territory_id.warehouse_id.company_id == rec.company_id:
rec.warehouse_id = rec.territory_id.warehouse_id
continue
# Otherwise, use the company's warehouse
company = rec.company_id or self.env.user.company_id
rec.warehouse_id = self.env["stock.warehouse"].search(
[("company_id", "=", company.id)], limit=1
)

@api.depends("picking_ids")
def _compute_picking_ids(self):
for order in self:
Expand Down
30 changes: 29 additions & 1 deletion fieldservice_stock/tests/test_fsm_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ def setUp(self):
self.location = self.env["fsm.location"]
self.FSMOrder = self.env["fsm.order"]
self.Product = self.env["product.product"].search([], limit=1)
self.warehouse = self.env["stock.warehouse"].search(
[("company_id", "=", self.env.company.id)], limit=1
)
self.stock_cust_loc = self.env.ref("stock.stock_location_customers")
self.stock_location = self.env.ref("stock.stock_location_stock")
self.customer_location = self.env.ref("stock.stock_location_customers")
self.test_location = self.env.ref("fieldservice.test_location")
self.test_territory = self.env.ref("base_territory.test_territory")
self.partner_1 = (
self.env["res.partner"]
.with_context(tracking_disable=True)
Expand Down Expand Up @@ -144,8 +148,32 @@ def test_fsm_orders(self):
order.picking_ids = [(6, 0, order_pick_list2)]
order._compute_picking_ids()
order.location_id._onchange_fsm_parent_id()
order._default_warehouse_id()
order.action_view_delivery()
order2.action_view_delivery()
order3.action_view_returns()
order.action_view_returns()

def test_order_warehouse_from_territory(self):
self.test_territory.warehouse_id = self.warehouse
order = self.env["fsm.order"].create(
{
"location_id": self.test_location.id,
}
)
self.assertEqual(
order.warehouse_id,
self.test_territory.warehouse_id,
"Warehouse should be assigned from territory",
)

def test_order_warehouse_default_from_company(self):
order = self.env["fsm.order"].create(
{
"location_id": self.test_location.id,
}
)
self.assertEqual(
order.warehouse_id,
self.warehouse,
"Warehouse should have a default value from company",
)

0 comments on commit 0410172

Please sign in to comment.