From 3bc8426f1ef46c8c0d1c444eb12d9c07154c82aa Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Mon, 17 Apr 2023 12:29:08 +0200 Subject: [PATCH] [IMP] stock_location_orderpoint: Add access to orderpoints from location form --- stock_location_orderpoint/__manifest__.py | 1 + stock_location_orderpoint/models/__init__.py | 1 + .../models/stock_location.py | 42 +++++++++++++++++++ .../tests/test_location_orderpoint.py | 10 +++++ .../views/stock_location.xml | 28 +++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 stock_location_orderpoint/models/stock_location.py create mode 100644 stock_location_orderpoint/views/stock_location.xml diff --git a/stock_location_orderpoint/__manifest__.py b/stock_location_orderpoint/__manifest__.py index f0353e258..acb853bf4 100644 --- a/stock_location_orderpoint/__manifest__.py +++ b/stock_location_orderpoint/__manifest__.py @@ -14,6 +14,7 @@ "data/queue_job_channel.xml", "data/queue_job_function.xml", "views/stock_location_orderpoint_views.xml", + "views/stock_location.xml", "views/menu.xml", ], "depends": [ diff --git a/stock_location_orderpoint/models/__init__.py b/stock_location_orderpoint/models/__init__.py index 25c4af750..ed2c33269 100644 --- a/stock_location_orderpoint/models/__init__.py +++ b/stock_location_orderpoint/models/__init__.py @@ -1,3 +1,4 @@ from . import stock_location_orderpoint from . import stock_rule from . import stock_move +from . import stock_location diff --git a/stock_location_orderpoint/models/stock_location.py b/stock_location_orderpoint/models/stock_location.py new file mode 100644 index 000000000..ab7b4d757 --- /dev/null +++ b/stock_location_orderpoint/models/stock_location.py @@ -0,0 +1,42 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import fields, models +from odoo.tools.safe_eval import safe_eval + + +class StockLocation(models.Model): + _inherit = "stock.location" + + location_orderpoint_ids = fields.One2many( + comodel_name="stock.location.orderpoint", + inverse_name="location_id", + string="Location Orderpoints", + help="Location Orderpoints. Rules that allows this location to be replenished.", + ) + location_orderpoint_count = fields.Integer( + compute="_compute_location_orderpoint_count", + ) + + def _compute_location_orderpoint_count(self): + groups = self.env["stock.location.orderpoint"].read_group( + [("location_id", "in", self.ids)], ["location_id"], ["location_id"] + ) + result = { + data["location_id"][0]: (data["location_id_count"]) for data in groups + } + for location in self: + location.location_orderpoint_count = result.get(location.id, 0) + + def action_open_location_orderpoints(self): + action = self.env["ir.actions.act_window"]._for_xml_id( + "stock_location_orderpoint.action_stock_location_orderpoint" + ) + action["domain"] = [("location_id", "in", self.ids)] + if len(self.ids) == 1: + if "context" in action: + context = safe_eval(action["context"]) + context.update({"default_location_id": self.id}) + action["context"] = str(context) + else: + action["context"] = str({"default_location_id": self.id}) + return action diff --git a/stock_location_orderpoint/tests/test_location_orderpoint.py b/stock_location_orderpoint/tests/test_location_orderpoint.py index e015c0968..315073656 100644 --- a/stock_location_orderpoint/tests/test_location_orderpoint.py +++ b/stock_location_orderpoint/tests/test_location_orderpoint.py @@ -154,3 +154,13 @@ class TestLocationOrderpoint(TestLocationOrderpointCommon): replenish_move_new = self._get_replenishment_move(orderpoint) self.assertEqual(replenish_move, replenish_move_new) self._check_replenishment_move(replenish_move, move_qty * 2, orderpoint) + + def test_orderpoint_count(self): + """ + One orderpoint has already been created in demo data. + Check after each creation that count is increasing. + """ + _, _ = self._create_orderpoint_complete("Stock2", trigger="cron") + self.assertEqual(1, self.location_dest.location_orderpoint_count) + _, _ = self._create_orderpoint_complete("Stock3", trigger="cron") + self.assertEqual(2, self.location_dest.location_orderpoint_count) diff --git a/stock_location_orderpoint/views/stock_location.xml b/stock_location_orderpoint/views/stock_location.xml new file mode 100644 index 000000000..5c2ebd989 --- /dev/null +++ b/stock_location_orderpoint/views/stock_location.xml @@ -0,0 +1,28 @@ + + + + + stock.location.form (in stock_location_orderpoint) + stock.location + + +
+ +
+
+
+