Allow to get default location by parent category.

This commit refs #13743
This commit is contained in:
Sergio Morillo 2021-01-25 12:23:41 +01:00
parent 1f930234f1
commit bcd617dff3
2 changed files with 52 additions and 8 deletions

View File

@ -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

View File

@ -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()