From bcd617dff3858c8911194d5a019198de39f4c89e Mon Sep 17 00:00:00 2001 From: Sergio Morillo Date: Mon, 25 Jan 2021 12:23:41 +0100 Subject: [PATCH] Allow to get default location by parent category. This commit refs #13743 --- product.py | 26 +++++++++----- tests/test_stock_product_category_location.py | 34 +++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/product.py b/product.py index e4f3ca1..4f3e80f 100644 --- a/product.py +++ b/product.py @@ -17,21 +17,31 @@ class Category(metaclass=PoolMeta): def get_default_location(self, **kwargs): pool = Pool() - Location = pool.get('stock.product.category.location') + CategoryLocation = pool.get('stock.product.category.location') _domain = self._get_default_location_domain(**kwargs) - locations = Location.search( - _domain, order=self._get_default_location_order(), - limit=2) - if locations: - return locations[0].location.id - return None + cat_locations = CategoryLocation.search( + _domain, order=self._get_default_location_order()) + location = None + + if kwargs.get('with_parent', True): + for cat_location in cat_locations: + if cat_location.category.id == self.id: + location = cat_location.location + break + + if not location and cat_locations: + location = cat_locations[0].location + return location def _get_default_location_domain(self, **kwargs): _domain = [ - ('category', '=', self.id), ('location.type', '=', kwargs.get('location_type', 'storage')) ] + if kwargs.get('with_parent', True): + _domain.append(('category', 'parent_of', self.id, 'parent')) + else: + _domain.append(('category', '=', self.id)) if kwargs.get('warehouse', None): _domain.append(('warehouse', '=', kwargs['warehouse'].id)) return _domain diff --git a/tests/test_stock_product_category_location.py b/tests/test_stock_product_category_location.py index 576ceb5..3c7d9ca 100644 --- a/tests/test_stock_product_category_location.py +++ b/tests/test_stock_product_category_location.py @@ -52,6 +52,40 @@ class StockProductCategoryLocationTestCase(ModuleTestCase): self.assertEqual( cm.exception.message, 'Category and location must be unique.') + @with_transaction() + def test_add_parent_category_default_location(self): + """Add the category default location for a warehouse""" + pool = Pool() + Location = pool.get('stock.location') + Category = pool.get('product.category') + CatLoc = pool.get('stock.product.category.location') + + storage, = Location.search([('code', '=', 'STO')]) + storage_new, = Location.create([{ + 'code': 'STO2', + 'name': 'Storage 2', + 'type': 'storage', + 'parent': storage.id + }]) + new_cat, = Category.create([{'name': 'CAT1'}]) + child_cat, = Category.create([{'name': 'CAT2', 'parent': new_cat.id}]) + cat_loc_rec = { + 'category': new_cat.id, + 'warehouse': storage.parent.id, + 'location': storage.id + } + CatLoc.create([cat_loc_rec]) + + self.assertEqual(child_cat.get_default_location(), storage) + + CatLoc.create([{ + 'category': child_cat.id, + 'warehouse': storage_new.warehouse.id, + 'location': storage_new.id + }]) + + self.assertEqual(child_cat.get_default_location(), storage_new) + def suite(): suite = trytond.tests.test_tryton.suite()