diff --git a/stock_location_simple/__init__.py b/stock_location_simple/__init__.py index 0650744..cc6b635 100644 --- a/stock_location_simple/__init__.py +++ b/stock_location_simple/__init__.py @@ -1 +1,2 @@ from . import models +from .hooks import post_init_hook diff --git a/stock_location_simple/__manifest__.py b/stock_location_simple/__manifest__.py index 1d3a227..e857d0d 100644 --- a/stock_location_simple/__manifest__.py +++ b/stock_location_simple/__manifest__.py @@ -10,4 +10,5 @@ "website": "http://akretion.com", "depends": ["stock"], "data": ["views/stock_location_views.xml"], + "post_init_hook": "post_init_hook", } diff --git a/stock_location_simple/hooks.py b/stock_location_simple/hooks.py new file mode 100644 index 0000000..b7b8364 --- /dev/null +++ b/stock_location_simple/hooks.py @@ -0,0 +1,10 @@ +# Copyright 2021 Akretion (https://www.akretion.com). +# @author Sébastien BEAU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import SUPERUSER_ID, api + + +def post_init_hook(cr, registry): + env = api.Environment(cr, SUPERUSER_ID, {}) + env["stock.warehouse"].search([])._check_locations_created_by_warehouse() diff --git a/stock_location_simple/models/__init__.py b/stock_location_simple/models/__init__.py index 88493e3..a2e4226 100644 --- a/stock_location_simple/models/__init__.py +++ b/stock_location_simple/models/__init__.py @@ -1 +1,2 @@ from . import stock_location +from . import stock_warehouse diff --git a/stock_location_simple/models/stock_location.py b/stock_location_simple/models/stock_location.py index 6cfa2f2..8982509 100644 --- a/stock_location_simple/models/stock_location.py +++ b/stock_location_simple/models/stock_location.py @@ -7,4 +7,4 @@ from odoo import _, api, fields, models class StockLocation(models.Model): _inherit = "stock.location" - # TODO + is_created_by_warehouse = fields.Boolean() diff --git a/stock_location_simple/models/stock_warehouse.py b/stock_location_simple/models/stock_warehouse.py new file mode 100644 index 0000000..6e794c9 --- /dev/null +++ b/stock_location_simple/models/stock_warehouse.py @@ -0,0 +1,34 @@ +# Copyright 2024 Akretion +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, api, fields, models +import pprint + + +class StockWarehouse(models.Model): + _inherit = "stock.warehouse" + + def _get_location_fields(self): + location_fields = [] + + for field, definition in self.fields_get().items(): + if definition.get("relation") == "stock.location": + location_fields.append(field) + + return location_fields + + def _check_locations_created_by_warehouse(self): + location_ids = self.env["stock.location"] + location_fields = self._get_location_fields() + + for rec in self: + for field in location_fields: + location_ids |= getattr(rec, field) + + location_ids.write({"is_created_by_warehouse": True}) + + @api.model + def create(self, vals): + res = super().create(vals) + res._check_locations_created_by_warehouse() + return res diff --git a/stock_location_simple/tests/__init__.py b/stock_location_simple/tests/__init__.py new file mode 100644 index 0000000..51bfc49 --- /dev/null +++ b/stock_location_simple/tests/__init__.py @@ -0,0 +1 @@ +from . import test_stock_location_simple diff --git a/stock_location_simple/tests/test_stock_location_simple.py b/stock_location_simple/tests/test_stock_location_simple.py new file mode 100644 index 0000000..eee2bf1 --- /dev/null +++ b/stock_location_simple/tests/test_stock_location_simple.py @@ -0,0 +1,19 @@ +# Copyright 2018-2022 Camptocamp +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests import TransactionCase + + +class TestStockLocationSimple(TransactionCase): + def setUp(self): + super().setUp() + self.env["stock.warehouse"].search([])._check_locations_created_by_warehouse() + + def test_location_checked_at_warehouse_creation(self): + warehouse = self.env["stock.warehouse"].create({"name": "Test", "code": "TEST"}) + self.assertTrue(warehouse.view_location_id.is_created_by_warehouse) + + def test_native_location_checked(self): + location_id = self.env.ref("stock.warehouse0").view_location_id + + self.assertTrue(location_id.is_created_by_warehouse)