diff --git a/CHANGELOG b/CHANGELOG index 5f7351f523..2a0f298541 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Let the user select a default warehouse +* Add method to get default warehouse * Improve moves synchronisation on shipments * Allow move as move's origin * Open product quantities by warehouse to show moves diff --git a/__init__.py b/__init__.py index 9548825e73..daf57842ca 100644 --- a/__init__.py +++ b/__init__.py @@ -10,6 +10,7 @@ from . import product from . import inventory from . import configuration from . import party +from . import res from .move import StockMixin @@ -47,6 +48,7 @@ def register(): configuration.Configuration, configuration.ConfigurationSequence, configuration.ConfigurationLocation, + res.User, module='stock', type_='model') Pool.register( shipment.AssignShipmentOut, diff --git a/location.py b/location.py index 4c08adbf5f..1066f79907 100644 --- a/location.py +++ b/location.py @@ -9,6 +9,7 @@ from trytond.model import ( ModelView, ModelSQL, MatchMixin, ValueMixin, DeactivableMixin, fields, sequence_ordered, tree) from trytond import backend +from trytond.cache import Cache from trytond.pyson import Eval, If from trytond.transaction import Transaction from trytond.pool import Pool, PoolMeta @@ -29,6 +30,9 @@ DEPENDS = ['active'] class Location(DeactivableMixin, tree(), ModelSQL, ModelView): "Stock Location" __name__ = 'stock.location' + _default_warehouse_cache = Cache('stock.location.default_warehouse', + context=False) + name = fields.Char("Name", size=None, required=True, states=STATES, depends=DEPENDS, translate=True) code = fields.Char("Code", size=None, states=STATES, depends=DEPENDS, @@ -295,6 +299,24 @@ class Location(DeactivableMixin, tree(), ModelSQL, ModelView): if locations: return locations[0].id + @classmethod + def get_default_warehouse(cls): + warehouse = Transaction().context.get('warehouse') + if warehouse: + return warehouse + + warehouse = cls._default_warehouse_cache.get(None, -1) + if warehouse == -1: + warehouses = cls.search([ + ('type', '=', 'warehouse'), + ], limit=2) + if len(warehouses) == 1: + warehouse = warehouses[0].id + else: + warehouse = None + cls._default_warehouse_cache.set(None, warehouse) + return warehouse + @classmethod def search_rec_name(cls, name, clause): if clause[1].startswith('!') or clause[1].startswith('not '): @@ -436,6 +458,7 @@ class Location(DeactivableMixin, tree(), ModelSQL, ModelView): def create(cls, vlist): locations = super(Location, cls).create(vlist) cls._set_warehouse_parent(locations) + cls._default_warehouse_cache.clear() return locations @classmethod @@ -443,6 +466,7 @@ class Location(DeactivableMixin, tree(), ModelSQL, ModelView): super(Location, cls).write(*args) locations = sum(args[::2], []) cls._set_warehouse_parent(locations) + cls._default_warehouse_cache.clear() ids = [l.id for l in locations] warehouses = cls.search([ @@ -469,6 +493,11 @@ class Location(DeactivableMixin, tree(), ModelSQL, ModelView): location=location.rec_name, warehouse=warehouse.rec_name)) + @classmethod + def delete(cls, *args): + super().delete(*args) + cls._default_warehouse_cache.clear() + @classmethod def copy(cls, locations, default=None): if default is None: diff --git a/product.py b/product.py index 7b78956b67..2afb9261cc 100644 --- a/product.py +++ b/product.py @@ -462,14 +462,10 @@ class ProductQuantitiesByWarehouseContext(ModelView): ], help="The warehouse for which the quantities will be calculated.") - @staticmethod - def default_warehouse(): + @classmethod + def default_warehouse(cls): Location = Pool().get('stock.location') - warehouses = Location.search([ - ('type', '=', 'warehouse'), - ]) - if len(warehouses) == 1: - return warehouses[0].id + return Location.get_default_warehouse() class OpenProductQuantitiesByWarehouse(Wizard): diff --git a/res.py b/res.py new file mode 100644 index 0000000000..8124a0c7c4 --- /dev/null +++ b/res.py @@ -0,0 +1,25 @@ +# This file is part of Tryton. The COPYRIGHT file at the top level of +# this repository contains the full copyright notices and license terms. +from trytond.model import fields +from trytond.pool import PoolMeta + + +class User(metaclass=PoolMeta): + __name__ = 'res.user' + + warehouse = fields.Many2One('stock.location', + "Current Warehouse", + domain=[('type', '=', 'warehouse')], + help="The warehouse that the user works at.") + + @classmethod + def __setup__(cls): + super(User, cls).__setup__() + cls._context_fields.insert(0, 'warehouse') + + @classmethod + def _get_preferences(cls, user, context_only=False): + preferences = super()._get_preferences(user, context_only=context_only) + if user.warehouse: + preferences['warehouse'] = user.warehouse.id + return preferences diff --git a/res.xml b/res.xml new file mode 100644 index 0000000000..e75b801bee --- /dev/null +++ b/res.xml @@ -0,0 +1,17 @@ + + + + + + res.user + + user_form + + + res.user + + user_form_preferences + + + diff --git a/shipment.py b/shipment.py index c9cb2f1c20..7fdac8c643 100644 --- a/shipment.py +++ b/shipment.py @@ -236,9 +236,7 @@ class ShipmentIn(Workflow, ModelSQL, ModelView): @classmethod def default_warehouse(cls): Location = Pool().get('stock.location') - locations = Location.search(cls.warehouse.domain) - if len(locations) == 1: - return locations[0].id + return Location.get_default_warehouse() @staticmethod def default_company(): @@ -995,9 +993,7 @@ class ShipmentOut(Workflow, ModelSQL, ModelView): @classmethod def default_warehouse(cls): Location = Pool().get('stock.location') - locations = Location.search(cls.warehouse.domain) - if len(locations) == 1: - return locations[0].id + return Location.get_default_warehouse() @staticmethod def default_company(): @@ -1525,9 +1521,7 @@ class ShipmentOutReturn(Workflow, ModelSQL, ModelView): @classmethod def default_warehouse(cls): Location = Pool().get('stock.location') - locations = Location.search(cls.warehouse.domain) - if len(locations) == 1: - return locations[0].id + return Location.get_default_warehouse() @staticmethod def default_company(): diff --git a/tryton.cfg b/tryton.cfg index 53176cfeb3..ebab9bbff2 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -17,3 +17,4 @@ xml: configuration.xml period.xml message.xml + res.xml diff --git a/view/user_form.xml b/view/user_form.xml new file mode 100644 index 0000000000..bedf434a70 --- /dev/null +++ b/view/user_form.xml @@ -0,0 +1,9 @@ + + + + + + diff --git a/view/user_form_preferences.xml b/view/user_form_preferences.xml new file mode 100644 index 0000000000..860e37e7e9 --- /dev/null +++ b/view/user_form_preferences.xml @@ -0,0 +1,9 @@ + + + + + +