changeset 0b3626d50ad8 in modules/sale_shipment_cost:default
details: 
https://hg.tryton.org/modules/sale_shipment_cost?cmd=changeset&node=0b3626d50ad8
description:
        Add cost method to shipment

        issue10774
        review352861003
diffstat:

 CHANGELOG                             |   2 +
 sale.py                               |  12 ++++++++
 stock.py                              |  49 ++++++++++++++++++++++++++++------
 tests/scenario_sale_shipment_cost.rst |   3 +-
 tryton.cfg                            |   1 +
 5 files changed, 57 insertions(+), 10 deletions(-)

diffs (174 lines):

diff -r d35fb4d82cc8 -r 0b3626d50ad8 CHANGELOG
--- a/CHANGELOG Fri Oct 08 19:21:06 2021 +0200
+++ b/CHANGELOG Fri Oct 08 19:58:17 2021 +0200
@@ -1,3 +1,5 @@
+* Add cost method to shipment
+
 Version 6.0.0 - 2021-05-03
 * Bug fixes (see mercurial logs for details)
 * Allow to add a sale shipment line with cost 0
diff -r d35fb4d82cc8 -r 0b3626d50ad8 sale.py
--- a/sale.py   Fri Oct 08 19:21:06 2021 +0200
+++ b/sale.py   Fri Oct 08 19:58:17 2021 +0200
@@ -281,6 +281,10 @@
         return cost_line
 
     @property
+    def _shipment_grouping_fields(self):
+        return super()._shipment_grouping_fields + ('cost_method',)
+
+    @property
     def shipment_cost_amount(self):
         cost = Decimal(0)
         for line in self.lines:
@@ -288,6 +292,14 @@
                 cost += line.amount
         return cost
 
+    def _get_shipment_sale(self, Shipment, key):
+        pool = Pool()
+        ShipmentOut = pool.get('stock.shipment.out')
+        shipment = super()._get_shipment_sale(Shipment, key)
+        if isinstance(shipment, ShipmentOut):
+            shipment.cost_method = self.shipment_cost_method
+        return shipment
+
     def create_shipment(self, shipment_type):
         shipments = super().create_shipment(shipment_type)
         if shipment_type == 'out' and shipments and self.carrier:
diff -r d35fb4d82cc8 -r 0b3626d50ad8 stock.py
--- a/stock.py  Fri Oct 08 19:21:06 2021 +0200
+++ b/stock.py  Fri Oct 08 19:58:17 2021 +0200
@@ -16,37 +16,52 @@
     cost_sale_currency_used = fields.Function(fields.Many2One(
             'currency.currency', "Cost Sale Currency",
             states={
-                'invisible': Eval('cost_edit', False),
+                'invisible': (
+                    Eval('cost_edit', False)
+                    | (Eval('cost_method') != 'shipment')),
                 },
-            depends=['cost_edit']),
+            depends=['cost_edit', 'cost_method']),
         'on_change_with_cost_sale_currency_used')
     cost_sale_currency = fields.Many2One(
         'currency.currency', "Cost Sale Currency",
         states={
-            'invisible': ~Eval('cost_edit', False),
+            'invisible': (
+                ~Eval('cost_edit', False)
+                | (Eval('cost_method') != 'shipment')),
             'required': Bool(Eval('cost_sale')),
             'readonly': Eval('state').in_(['done', 'cancelled']),
-            }, depends=['cost_sale', 'state'])
+            },
+        depends=['cost_method', 'cost_sale', 'state'])
     cost_sale_used = fields.Function(fields.Numeric(
             "Cost Sale", digits=price_digits,
             states={
-                'invisible': Eval('cost_edit', False),
+                'invisible': (
+                    Eval('cost_edit', False)
+                    | (Eval('cost_method') != 'shipment')),
                 },
-            depends=['cost_edit']),
+            depends=['cost_edit', 'cost_method']),
         'on_change_with_cost_sale_used')
     cost_sale = fields.Numeric(
         "Cost Sale", digits=price_digits,
         states={
-            'invisible': ~Eval('cost_edit', False),
+            'invisible': (
+                ~Eval('cost_edit', False)
+                | (Eval('cost_method') != 'shipment')),
             'readonly': ~Eval('state').in_(['done', 'cancelled']),
-            }, depends=['state'])
+            },
+        depends=['cost_method', 'state'])
 
     cost_invoice_line = fields.Many2One('account.invoice.line',
             'Cost Invoice Line', readonly=True)
+    cost_method = fields.Selection(
+        'get_cost_methods', 'Cost Method', readonly=True)
 
     @classmethod
     def __register__(cls, module):
         table_h = cls.__table_handler__(module)
+        cursor = Transaction().connection.cursor()
+        table = cls.__table__()
+
         # Migration from 5.8: rename cost into cost_sale
         if (table_h.column_exist('cost')
                 and not table_h.column_exist('cost_sale')):
@@ -54,8 +69,17 @@
         if (table_h.column_exist('cost_currency')
                 and not table_h.column_exist('cost_sale_currency')):
             table_h.column_rename('cost_currency', 'cost_sale_currency')
+
+        cost_method_exists = table_h.column_exist('cost_method')
+
         super().__register__(module)
 
+        # Migration from 6.0: fill new cost_method field
+        if not cost_method_exists:
+            cursor.execute(*table.update(
+                    columns=[table.cost_method],
+                    values=['shipment']))
+
     @fields.depends('carrier', 'company', methods=['_get_carrier_context'])
     def _compute_costs(self):
         costs = super()._compute_costs()
@@ -63,7 +87,7 @@
                 'cost_sale': None,
                 'cost_sale_currency': None,
                 })
-        if self.carrier:
+        if self.carrier and self.cost_method == 'shipment':
             with Transaction().set_context(self._get_carrier_context()):
                 cost_sale, sale_currency_id = self.carrier.get_sale_price()
             if cost_sale is not None:
@@ -94,6 +118,13 @@
             self.cost_sale = self.cost_sale_used
             self.cost_sale_currency = self.cost_sale_currency_used
 
+    @classmethod
+    def get_cost_methods(cls):
+        pool = Pool()
+        Sale = pool.get('sale.sale')
+        fieldname = 'shipment_cost_method'
+        return Sale.fields_get([fieldname])[fieldname]['selection']
+
     def _get_cost_tax_rule_pattern(self):
         'Get tax rule pattern for invoice line'
         return {}
diff -r d35fb4d82cc8 -r 0b3626d50ad8 tests/scenario_sale_shipment_cost.rst
--- a/tests/scenario_sale_shipment_cost.rst     Fri Oct 08 19:21:06 2021 +0200
+++ b/tests/scenario_sale_shipment_cost.rst     Fri Oct 08 19:58:17 2021 +0200
@@ -139,6 +139,8 @@
 
     >>> ShipmentOut = Model.get('stock.shipment.out')
     >>> shipment, = sale.shipments
+    >>> shipment.cost_method
+    'shipment'
     >>> shipment.carrier == carrier
     True
     >>> shipment.cost_used
@@ -317,7 +319,6 @@
     >>> shipment.cost_used
     Decimal('2.0000')
     >>> shipment.cost_sale_used
-    Decimal('3.0000')
     >>> shipment.click('assign_force')
     >>> shipment.click('pick')
     >>> shipment.click('pack')
diff -r d35fb4d82cc8 -r 0b3626d50ad8 tryton.cfg
--- a/tryton.cfg        Fri Oct 08 19:21:06 2021 +0200
+++ b/tryton.cfg        Fri Oct 08 19:58:17 2021 +0200
@@ -12,6 +12,7 @@
     stock_shipment_cost
 extras_depend:
     sale_promotion
+    sale_shipment_grouping
 xml:
     sale.xml
     stock.xml

Reply via email to