changeset 682fe06f9774 in modules/stock_package_shipping:default
details: 
https://hg.tryton.org/modules/stock_package_shipping?cmd=changeset;node=682fe06f9774
description:
        Manage shipping for supplier shipment returns

        We move the shipping code in mixin's such that it can be applied on 
other
        shipments.
        We define properties to abstract the information needed by the create 
shipping
        wizards. Such that they can be execute on any shipment.

        issue9850
        review324521002
diffstat:

 CHANGELOG                        |    2 +
 __init__.py                      |    1 +
 stock.py                         |  150 ++++++++++++++++++++++++++++----------
 stock.xml                        |   12 +++
 view/shipment_in_return_form.xml |   16 ++++
 5 files changed, 141 insertions(+), 40 deletions(-)

diffs (249 lines):

diff -r 0883840baa36 -r 682fe06f9774 CHANGELOG
--- a/CHANGELOG Sat Dec 19 17:08:47 2020 +0100
+++ b/CHANGELOG Sun Dec 20 19:54:48 2020 +0100
@@ -1,3 +1,5 @@
+* Manage supplier shipment returns
+
 Version 5.8.0 - 2020-11-02
 * Bug fixes (see mercurial logs for details)
 * Remove support for Python 3.5
diff -r 0883840baa36 -r 682fe06f9774 __init__.py
--- a/__init__.py       Sat Dec 19 17:08:47 2020 +0100
+++ b/__init__.py       Sun Dec 20 19:54:48 2020 +0100
@@ -12,6 +12,7 @@
         stock.PackageType,
         stock.Package,
         stock.ShipmentOut,
+        stock.ShipmentInReturn,
         carrier.Carrier,
         module='stock_package_shipping', type_='model')
     Pool.register(
diff -r 0883840baa36 -r 682fe06f9774 stock.py
--- a/stock.py  Sat Dec 19 17:08:47 2020 +0100
+++ b/stock.py  Sun Dec 20 19:54:48 2020 +0100
@@ -107,8 +107,7 @@
         return super(Package, cls).copy(packages, default=default)
 
 
-class ShipmentOut(metaclass=PoolMeta):
-    __name__ = 'stock.shipment.out'
+class ShippingMixin:
 
     shipping_description = fields.Char('Shipping Description',
         states={
@@ -118,7 +117,7 @@
 
     @classmethod
     def __setup__(cls):
-        super(ShipmentOut, cls).__setup__()
+        super().__setup__()
         # The shipment reference will be set by the shipping service
         cls.reference.readonly = True
         cls._buttons.update({
@@ -135,6 +134,66 @@
                 })
 
     @classmethod
+    def search_rec_name(cls, name, clause):
+        domain = super().search_rec_name(name, clause)
+        return ['OR', domain,
+            ('reference',) + tuple(clause[1:]),
+            ]
+
+    @classmethod
+    def validate(cls, shipments):
+        super().validate(shipments)
+        for shipment in shipments:
+            if shipment.carrier and shipment.carrier.shipping_service:
+                method_name = ('validate_packing_%s'
+                    % shipment.carrier.shipping_service)
+                validator = getattr(shipment, method_name)
+                validator()
+
+    @classmethod
+    def check_no_carrier(cls, shipments):
+        pool = Pool()
+        Warning = pool.get('res.user.warning')
+        for shipment in shipments:
+            if not shipment.carrier:
+                name = 'shipment_out_no_carrier_%s' % shipment
+                if Warning.check(name):
+                    raise PackWarning(name,
+                        gettext('stock_package_shipping'
+                            '.msg_shipment_without_carrier',
+                            shipment=shipment.rec_name))
+
+    @classmethod
+    @ModelView.button_action(
+        'stock_package_shipping.act_create_shipping_wizard')
+    def create_shipping(cls, shipments):
+        for shipment in shipments:
+            if shipment.state not in shipment.shipping_allowed:
+                raise AccessError(
+                    gettext('stock_package_shipping.msg_shipment_not_packed',
+                        shipment=shipment.rec_name))
+
+    @property
+    def shipping_allowed(self):
+        raise NotImplementedError
+
+    @property
+    def shipping_warehouse(self):
+        raise NotImplementedError
+
+    @property
+    def shipping_to(self):
+        raise NotImplementedError
+
+    @property
+    def shipping_to_address(self):
+        raise NotImplementedError
+
+
+class ShipmentOut(ShippingMixin, metaclass=PoolMeta):
+    __name__ = 'stock.shipment.out'
+
+    @classmethod
     def __register__(cls, module):
         pool = Pool()
         ModelData = pool.get('ir.model.data')
@@ -152,49 +211,60 @@
                     & (model_data.fs_id == 'create_shipping_button')))
 
     @classmethod
-    def search_rec_name(cls, name, clause):
-        domain = super(ShipmentOut, cls).search_rec_name(name, clause)
-        return ['OR', domain,
-            ('reference',) + tuple(clause[1:]),
-            ]
-
-    @classmethod
-    def validate(cls, shipments):
-        super(ShipmentOut, cls).validate(shipments)
-        for shipment in shipments:
-            if shipment.carrier and shipment.carrier.shipping_service:
-                method_name = ('validate_packing_%s'
-                    % shipment.carrier.shipping_service)
-                validator = getattr(shipment, method_name)
-                validator()
-
-    @classmethod
     @Workflow.transition('packed')
     def pack(cls, shipments):
-        pool = Pool()
-        Warning = pool.get('res.user.warning')
-        super(ShipmentOut, cls).pack(shipments)
-        for shipment in shipments:
-            if not shipment.carrier:
-                name = 'shipment_out_no_carrier_%s' % shipment.id
-                if Warning.check(name):
-                    raise PackWarning(name,
-                        gettext('stock_package_shipping'
-                            '.msg_shipment_without_carrier',
-                            shipment=shipment.rec_name))
+        super().pack(shipments)
+        cls.check_no_carrier(shipments)
+
+    @property
+    def shipping_allowed(self):
+        return {'packed', 'done'}
+
+    @property
+    def shipping_warehouse(self):
+        return self.warehouse
+
+    @property
+    def shipping_to(self):
+        return self.customer
+
+    @property
+    def shipping_to_address(self):
+        return self.delivery_address
+
+
+class ShipmentInReturn(ShippingMixin, metaclass=PoolMeta):
+    __name__ = 'stock.shipment.in.return'
+
+    carrier = fields.Many2One(
+        'carrier', "Carrier",
+        states={
+            'readonly': ~Eval('state').in_(['draft', 'waiting', 'assigned']),
+            },
+        depends=['state'])
 
     @classmethod
-    @ModelView.button_action(
-        'stock_package_shipping.act_create_shipping_wizard')
-    def create_shipping(cls, shipments):
-        for shipment in shipments:
-            if shipment.state not in {'packed', 'done'}:
-                raise AccessError(
-                    gettext('stock_package_shipping.msg_shipment_not_packed',
-                        shipment=shipment.rec_name))
+    @ModelView.button
+    @Workflow.transition('done')
+    def done(cls, shipments):
+        super().done(shipments)
+        cls.check_no_carrier(shipments)
+
+    @property
+    def shipping_allowed(self):
+        return {'assigned', 'done'}
 
+    @property
+    def shipping_warehouse(self):
+        return self.from_location.warehouse
 
-# TODO Implement ShipmentInReturn
+    @property
+    def shipping_to(self):
+        return self.supplier
+
+    @property
+    def shipping_to_address(self):
+        return self.delivery_address
 
 
 class CreateShipping(Wizard):
diff -r 0883840baa36 -r 682fe06f9774 stock.xml
--- a/stock.xml Sat Dec 19 17:08:47 2020 +0100
+++ b/stock.xml Sun Dec 20 19:54:48 2020 +0100
@@ -35,6 +35,18 @@
                 search="[('model', '=', 'stock.shipment.out')]"/>
         </record>
 
+        <record model="ir.ui.view" id="shipment_in_return_view_form">
+            <field name="model">stock.shipment.in.return</field>
+            <field name="inherit" ref="stock.shipment_in_return_view_form"/>
+            <field name="name">shipment_in_return_form</field>
+        </record>
+
+        <record model="ir.model.button" 
id="shipment_in_return_create_shipping_button">
+            <field name="name">create_shipping</field>
+            <field name="string">Create Shipping for Packages</field>
+            <field name="model" search="[('model', '=', 
'stock.shipment.in.return')]"/>
+        </record>
+
         <record model="ir.action.wizard" id="act_create_shipping_wizard">
             <field name="name">Create Shipping</field>
             <field name="wiz_name">stock.shipment.create_shipping</field>
diff -r 0883840baa36 -r 682fe06f9774 view/shipment_in_return_form.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/view/shipment_in_return_form.xml  Sun Dec 20 19:54:48 2020 +0100
@@ -0,0 +1,16 @@
+<?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" position="after">
+        <label name="carrier"/>
+        <field name="carrier"/>
+        <newline/>
+
+        <label name="shipping_description"/>
+        <field name="shipping_description" colspan="3"/>
+    </xpath>
+    <xpath expr="//group[@id='buttons']" position="inside">
+        <button name="create_shipping"/>
+    </xpath>
+</data>

Reply via email to