changeset ab65dc403e76 in modules/stock:default
details: https://hg.tryton.org/modules/stock?cmd=changeset;node=ab65dc403e76
description:
        Add a global method to set default warehouse

        issue8396
        review277371003
diffstat:

 CHANGELOG                      |   2 ++
 __init__.py                    |   2 ++
 location.py                    |  29 +++++++++++++++++++++++++++++
 product.py                     |  10 +++-------
 res.py                         |  25 +++++++++++++++++++++++++
 res.xml                        |  17 +++++++++++++++++
 shipment.py                    |  12 +++---------
 tryton.cfg                     |   1 +
 view/user_form.xml             |   9 +++++++++
 view/user_form_preferences.xml |   9 +++++++++
 10 files changed, 100 insertions(+), 16 deletions(-)

diffs (243 lines):

diff -r 594f5c141aaa -r ab65dc403e76 CHANGELOG
--- a/CHANGELOG Mon Sep 02 18:26:02 2019 +0200
+++ b/CHANGELOG Sat Sep 07 23:13:33 2019 +0200
@@ -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 -r 594f5c141aaa -r ab65dc403e76 __init__.py
--- a/__init__.py       Mon Sep 02 18:26:02 2019 +0200
+++ b/__init__.py       Sat Sep 07 23:13:33 2019 +0200
@@ -10,6 +10,7 @@
 from . import inventory
 from . import configuration
 from . import party
+from . import res
 
 from .move import StockMixin
 
@@ -47,6 +48,7 @@
         configuration.Configuration,
         configuration.ConfigurationSequence,
         configuration.ConfigurationLocation,
+        res.User,
         module='stock', type_='model')
     Pool.register(
         shipment.AssignShipmentOut,
diff -r 594f5c141aaa -r ab65dc403e76 location.py
--- a/location.py       Mon Sep 02 18:26:02 2019 +0200
+++ b/location.py       Sat Sep 07 23:13:33 2019 +0200
@@ -9,6 +9,7 @@
     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 @@
 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,
@@ -296,6 +300,24 @@
             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 '):
             bool_op = 'AND'
@@ -436,6 +458,7 @@
     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 @@
         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([
@@ -470,6 +494,11 @@
                             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:
             default = {}
diff -r 594f5c141aaa -r ab65dc403e76 product.py
--- a/product.py        Mon Sep 02 18:26:02 2019 +0200
+++ b/product.py        Sat Sep 07 23:13:33 2019 +0200
@@ -462,14 +462,10 @@
             ],
         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 -r 594f5c141aaa -r ab65dc403e76 res.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/res.py    Sat Sep 07 23:13:33 2019 +0200
@@ -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 -r 594f5c141aaa -r ab65dc403e76 res.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/res.xml   Sat Sep 07 23:13:33 2019 +0200
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+<!-- This file is part of Tryton.  The COPYRIGHT file at the top level of
+this repository contains the full copyright notices and license terms. -->
+<tryton>
+    <data>
+        <record model="ir.ui.view" id="user_view_form">
+            <field name="model">res.user</field>
+            <field name="inherit" ref="res.user_view_form"/>
+            <field name="name">user_form</field>
+        </record>
+        <record model="ir.ui.view" id="user_view_form_preferences">
+            <field name="model">res.user</field>
+            <field name="inherit" ref="res.user_view_form_preferences"/>
+            <field name="name">user_form_preferences</field>
+        </record>
+    </data>
+</tryton>
diff -r 594f5c141aaa -r ab65dc403e76 shipment.py
--- a/shipment.py       Mon Sep 02 18:26:02 2019 +0200
+++ b/shipment.py       Sat Sep 07 23:13:33 2019 +0200
@@ -236,9 +236,7 @@
     @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 @@
     @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 @@
     @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 -r 594f5c141aaa -r ab65dc403e76 tryton.cfg
--- a/tryton.cfg        Mon Sep 02 18:26:02 2019 +0200
+++ b/tryton.cfg        Sat Sep 07 23:13:33 2019 +0200
@@ -17,3 +17,4 @@
     configuration.xml
     period.xml
     message.xml
+    res.xml
diff -r 594f5c141aaa -r ab65dc403e76 view/user_form.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/view/user_form.xml        Sat Sep 07 23:13:33 2019 +0200
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<!-- This file is part of Tryton.  The COPYRIGHT file at the top level of
+this repository contains the full copyright notices and license terms. -->
+<data>
+    <xpath expr="/form/notebook/page[@id='user']/field[@name='employee']" 
position="after">
+        <label name="warehouse"/>
+        <field name="warehouse"/>
+    </xpath>
+</data>
diff -r 594f5c141aaa -r ab65dc403e76 view/user_form_preferences.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/view/user_form_preferences.xml    Sat Sep 07 23:13:33 2019 +0200
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<!-- This file is part of Tryton.  The COPYRIGHT file at the top level of
+this repository contains the full copyright notices and license terms. -->
+<data>
+    <xpath expr="/form/notebook/page[@id='user']/field[@name='employee']" 
position="after">
+        <label name="warehouse"/>
+        <field name="warehouse" help="The warehouse that you work at."/>
+    </xpath>
+</data>

Reply via email to