2015-06-25 14:40:56 +02:00
|
|
|
# The COPYRIGHT file at the top level of
|
|
|
|
# this repository contains the full copyright notices and license terms.
|
|
|
|
from trytond.model import fields
|
2015-07-20 12:27:03 +02:00
|
|
|
from trytond.pool import PoolMeta, Pool
|
2015-06-25 14:40:56 +02:00
|
|
|
|
|
|
|
__all__ = ['Category']
|
|
|
|
|
|
|
|
|
|
|
|
class Category:
|
|
|
|
__name__ = 'product.category'
|
2016-06-13 18:05:49 +02:00
|
|
|
__metaclass__ = PoolMeta
|
2015-07-20 12:27:03 +02:00
|
|
|
|
2015-07-25 12:53:56 +02:00
|
|
|
locations = fields.One2Many(
|
|
|
|
'stock.product.category.location', 'category', 'Default Locations')
|
2015-07-20 12:27:03 +02:00
|
|
|
default_location = fields.Function(fields.Many2One('stock.location', 'Default location'),
|
|
|
|
'get_default_location')
|
|
|
|
|
2016-06-13 18:05:49 +02:00
|
|
|
def get_default_location(self, **kwargs):
|
2015-07-20 12:27:03 +02:00
|
|
|
pool = Pool()
|
|
|
|
Location = pool.get('stock.product.category.location')
|
|
|
|
|
2016-06-13 18:05:49 +02:00
|
|
|
_domain = self._get_default_location_domain(**kwargs)
|
|
|
|
locations = Location.search(
|
|
|
|
_domain, order=self._get_default_location_order(),
|
|
|
|
limit=2)
|
2015-07-20 12:27:03 +02:00
|
|
|
if locations:
|
|
|
|
return locations[0].location.id
|
|
|
|
return None
|
2016-06-13 18:05:49 +02:00
|
|
|
|
|
|
|
def _get_default_location_domain(self, **kwargs):
|
|
|
|
_domain = [('category', '=', self.id),
|
|
|
|
('location.type', '=', kwargs.get('location_type', 'storage'))]
|
|
|
|
if kwargs.get('warehouse', None):
|
|
|
|
_domain.append(('warehouse', '=', kwargs['warehouse'].id))
|
|
|
|
return _domain
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_default_location_order():
|
|
|
|
return [('sequence', 'ASC')]
|