diff --git a/purchase_request/models/purchase_request.py b/purchase_request/models/purchase_request.py index 9d028d9ae78..65687b212e1 100644 --- a/purchase_request/models/purchase_request.py +++ b/purchase_request/models/purchase_request.py @@ -157,6 +157,19 @@ def _compute_is_editable(self): store=True, ) + def _track_subtype(self, init_values): + if self: + record = self[0] + if "state" in init_values and record.state == "to_approve": + return self.env.ref("purchase_request.mt_request_to_approve") + elif "state" in init_values and record.state == "approved": + return self.env.ref("purchase_request.mt_request_approved") + elif "state" in init_values and record.state == "rejected": + return self.env.ref("purchase_request.mt_request_rejected") + elif "state" in init_values and record.state == "done": + return self.env.ref("purchase_request.mt_request_done") + return super()._track_subtype(init_values) + @api.depends("line_ids", "line_ids.estimated_cost") def _compute_estimated_cost(self): for rec in self: diff --git a/purchase_request/tests/test_purchase_request.py b/purchase_request/tests/test_purchase_request.py index 5935beddaf7..6ae63c50b90 100644 --- a/purchase_request/tests/test_purchase_request.py +++ b/purchase_request/tests/test_purchase_request.py @@ -242,3 +242,15 @@ def test_purchase_request_unlink(self): pr.button_draft() self.assertEqual(pr.state, "draft", "Should be in state draft") pr_lines.unlink() + + def test_tracking(self): + """Tests Purchase Request tracking. + changes on status should use the proper mail subtypes""" + purchase_request = self.purchase_request + purchase_request.message_subscribe( + partner_ids=[self.env.ref("base.user_demo").partner_id.id] + ) + purchase_request.button_to_approve() + subtype = purchase_request._track_subtype({"state": "to_approve"}) + to_approve_subtype = self.env.ref("purchase_request.mt_request_to_approve") + self.assertEqual(subtype, to_approve_subtype)