changeset a3642ff66705 in modules/purchase_shipment_cost:default
details: 
https://hg.tryton.org/modules/purchase_shipment_cost?cmd=changeset;node=a3642ff66705
description:
        Simplify manual alteration of shipment cost

        issue10142
        review340111002
diffstat:

 CHANGELOG                                                                |    
2 +
 stock.py                                                                 |  
119 +++++++--
 tests/scenario_purchase_shipment_cost.rst                                |   
14 +-
 tests/scenario_purchase_shipment_cost_with_account_stock.rst             |   
10 +-
 tests/scenario_purchase_shipment_cost_with_account_stock_anglo_saxon.rst |    
6 +-
 view/shipment_in_form.xml                                                |    
8 +-
 6 files changed, 107 insertions(+), 52 deletions(-)

diffs (271 lines):

diff -r 4e6970274a9e -r a3642ff66705 CHANGELOG
--- a/CHANGELOG Sat Dec 19 17:08:46 2020 +0100
+++ b/CHANGELOG Sat Mar 13 23:43:51 2021 +0100
@@ -1,3 +1,5 @@
+* Simplify manual alteration of shipment cost
+
 Version 5.8.0 - 2020-11-02
 * Bug fixes (see mercurial logs for details)
 * Remove support for Python 3.5
diff -r 4e6970274a9e -r a3642ff66705 stock.py
--- a/stock.py  Sat Dec 19 17:08:46 2020 +0100
+++ b/stock.py  Sat Mar 13 23:43:51 2021 +0100
@@ -17,59 +17,96 @@
             'readonly': Eval('state') != 'draft',
             },
         depends=['state'])
-    cost_currency = fields.Many2One('currency.currency', 'Cost Currency',
+
+    cost_currency_used = fields.Function(fields.Many2One(
+            'currency.currency', "Cost Currency",
+            states={
+                'invisible': Eval('cost_edit', False),
+                },
+            depends=['cost_edit']),
+        'on_change_with_cost_currency_used')
+    cost_currency = fields.Many2One(
+        'currency.currency', "Cost Currency",
         states={
             'required': Bool(Eval('cost')),
-            'readonly': ~Eval('state').in_(['draft', 'assigned', 'packed']),
-            }, depends=['cost', 'state'])
-    cost_currency_digits = fields.Function(fields.Integer(
-            'Cost Currency Digits'),
-        'on_change_with_cost_currency_digits')
-    cost = fields.Numeric('Cost', digits=(16, Eval('cost_currency_digits', 2)),
+            'invisible': ~Eval('cost_edit', False),
+            'readonly': ~Eval('state').in_(['draft']),
+            },
+        depends=['cost', 'cost_edit', 'state'])
+    cost_used = fields.Function(fields.Numeric(
+            "Cost", digits=price_digits,
+            states={
+                'invisible': Eval('cost_edit', False),
+                },
+            depends=['cost_edit']),
+        'on_change_with_cost_used')
+    cost = fields.Numeric(
+        "Cost", digits=price_digits,
         states={
-            'readonly': ~Eval('state').in_(['draft', 'assigned', 'packed']),
-            }, depends=['carrier', 'state', 'cost_currency_digits'])
+            'invisible': ~Eval('cost_edit', False),
+            'readonly': ~Eval('state').in_(['draft']),
+            },
+        depends=['cost_edit', 'state'])
 
-    @fields.depends('cost_currency')
-    def on_change_with_cost_currency_digits(self, name=None):
-        if self.cost_currency:
-            return self.cost_currency.digits
-        return 2
+    cost_edit = fields.Boolean(
+        "Edit Cost",
+        states={
+            'readonly': ~Eval('state').in_(['draft']),
+            },
+        depends=['state'],
+        help="Check to edit the cost.")
 
     def _get_carrier_context(self):
         return {}
 
-    @fields.depends(methods=['on_change_incoming_moves'])
-    def on_change_carrier(self):
-        self.on_change_incoming_moves()
+    @fields.depends('carrier', methods=['_get_carrier_context'])
+    def _compute_costs(self):
+        costs = {
+            'cost': None,
+            'cost_currency': None,
+            }
+        if self.carrier:
+            with Transaction().set_context(self._get_carrier_context()):
+                cost, currency_id = self.carrier.get_purchase_price()
+            costs['cost'] = round_price(cost)
+            costs['cost_currency'] = currency_id
+        return costs
 
-    @fields.depends('carrier', methods=['_get_carrier_context'])
-    def on_change_incoming_moves(self):
-        try:
-            super(ShipmentIn, self).on_change_incoming_moves()
-        except AttributeError:
-            pass
-        if not self.carrier:
-            return
-        with Transaction().set_context(self._get_carrier_context()):
-            cost, currency_id = self.carrier.get_purchase_price()
-        self.cost = cost
-        self.cost_currency = currency_id
-        if self.cost_currency:
-            self.cost_currency_digits = self.cost_currency.digits
+    @fields.depends(
+        'state', 'cost_currency', 'cost_edit', methods=['_compute_costs'])
+    def on_change_with_cost_currency_used(self, name=None):
+        if (not self.cost_edit
+                and self.state not in {'cancelled', 'received', 'done'}):
+            return self._compute_costs()['cost_currency']
+        elif self.cost_currency:
+            return self.cost_currency.id
+
+    @fields.depends('state', 'cost', 'cost_edit', methods=['_compute_costs'])
+    def on_change_with_cost_used(self, name=None):
+        if (not self.cost_edit
+                and self.state not in {'cancelled', 'received', 'done'}):
+            return self._compute_costs()['cost']
         else:
-            self.cost_currency_digits = 2
+            return self.cost
+
+    @fields.depends(
+        'cost_edit', 'cost_used', 'cost_currency_used')
+    def on_change_cost_edit(self):
+        if self.cost_edit:
+            self.cost = self.cost_used
+            self.cost_currency = self.cost_currency_used
 
     def allocate_cost_by_value(self):
         pool = Pool()
         Currency = pool.get('currency.currency')
         Move = pool.get('stock.move')
 
-        if not self.cost:
+        if not self.cost_used:
             return
 
-        cost = Currency.compute(self.cost_currency, self.cost,
-            self.company.currency, round=False)
+        cost = Currency.compute(
+            self.cost_currency_used, self.cost_used, self.company.currency,
+            round=False)
         moves = [m for m in self.incoming_moves
             if m.state not in ('done', 'cancelled')]
 
@@ -126,6 +163,8 @@
     def receive(cls, shipments):
         Carrier = Pool().get('carrier')
         for shipment in shipments:
+            shipment.cost = shipment.cost_used
+            shipment.cost_currency = shipment.cost_currency_used
             if shipment.carrier:
                 allocation_method = \
                     shipment.carrier.carrier_cost_allocation_method
@@ -135,6 +174,16 @@
             getattr(shipment, 'allocate_cost_by_%s' % allocation_method)()
         super(ShipmentIn, cls).receive(shipments)
 
+    @classmethod
+    @ModelView.button
+    @Workflow.transition('cancelled')
+    def cancel(cls, shipments):
+        for shipment in shipments:
+            shipment.cost = None
+            shipment.cost_currency = None
+        cls.save(shipments)
+        super().cancel(shipments)
+
 
 class Move(metaclass=PoolMeta):
     __name__ = 'stock.move'
diff -r 4e6970274a9e -r a3642ff66705 tests/scenario_purchase_shipment_cost.rst
--- a/tests/scenario_purchase_shipment_cost.rst Sat Dec 19 17:08:46 2020 +0100
+++ b/tests/scenario_purchase_shipment_cost.rst Sat Mar 13 23:43:51 2021 +0100
@@ -82,9 +82,9 @@
     >>> move.quantity = 50
     >>> move.unit_price = Decimal('8')
     >>> shipment.carrier = carrier
-    >>> shipment.cost
-    Decimal('3')
-    >>> shipment.cost_currency == company.currency
+    >>> shipment.cost_used
+    Decimal('3.0000')
+    >>> shipment.cost_currency_used == company.currency
     True
     >>> shipment.click('receive')
     >>> shipment.state
@@ -106,8 +106,8 @@
     ...     move.quantity = quantity
     ...     move.unit_price = Decimal('8')
     >>> shipment.carrier = carrier
-    >>> shipment.cost
-    Decimal('3')
+    >>> shipment.cost_used
+    Decimal('3.0000')
     >>> shipment.click('receive')
     >>> shipment.state
     'received'
@@ -132,8 +132,8 @@
     >>> move.quantity = 25
     >>> move.unit_price = Decimal('0.0')
     >>> shipment.carrier = carrier
-    >>> shipment.cost
-    Decimal('3')
+    >>> shipment.cost_used
+    Decimal('3.0000')
     >>> shipment.click('receive')
     >>> shipment.state
     'received'
diff -r 4e6970274a9e -r a3642ff66705 
tests/scenario_purchase_shipment_cost_with_account_stock.rst
--- a/tests/scenario_purchase_shipment_cost_with_account_stock.rst      Sat Dec 
19 17:08:46 2020 +0100
+++ b/tests/scenario_purchase_shipment_cost_with_account_stock.rst      Sat Mar 
13 23:43:51 2021 +0100
@@ -136,9 +136,9 @@
     >>> move.quantity = 20
     >>> move.unit_price = Decimal('8')
     >>> shipment.carrier = carrier
-    >>> shipment.cost
-    Decimal('3')
-    >>> shipment.cost_currency == company.currency
+    >>> shipment.cost_used
+    Decimal('3.0000')
+    >>> shipment.cost_currency_used == company.currency
     True
     >>> shipment.click('receive')
     >>> shipment.state
@@ -174,8 +174,8 @@
     ...     move.quantity = quantity
     ...     move.unit_price = Decimal('8')
     >>> shipment.carrier = carrier
-    >>> shipment.cost
-    Decimal('3')
+    >>> shipment.cost_used
+    Decimal('3.0000')
     >>> shipment.click('receive')
     >>> shipment.state
     'received'
diff -r 4e6970274a9e -r a3642ff66705 
tests/scenario_purchase_shipment_cost_with_account_stock_anglo_saxon.rst
--- a/tests/scenario_purchase_shipment_cost_with_account_stock_anglo_saxon.rst  
Sat Dec 19 17:08:46 2020 +0100
+++ b/tests/scenario_purchase_shipment_cost_with_account_stock_anglo_saxon.rst  
Sat Mar 13 23:43:51 2021 +0100
@@ -149,9 +149,9 @@
     >>> move.quantity = 4.0
     >>> shipment.incoming_moves.append(move)
     >>> shipment.carrier = carrier
-    >>> shipment.cost
-    Decimal('3')
-    >>> shipment.cost_currency == company.currency
+    >>> shipment.cost_used
+    Decimal('3.0000')
+    >>> shipment.cost_currency_used == company.currency
     True
     >>> shipment.click('receive')
     >>> shipment.state
diff -r 4e6970274a9e -r a3642ff66705 view/shipment_in_form.xml
--- a/view/shipment_in_form.xml Sat Dec 19 17:08:46 2020 +0100
+++ b/view/shipment_in_form.xml Sat Mar 13 23:43:51 2021 +0100
@@ -5,10 +5,14 @@
     <xpath expr="/form/notebook" position="after">
         <label name="carrier"/>
         <field name="carrier"/>
-        <label name="cost"/>
-        <group col="-1" id="cost">
+        <group id="costs" col="-1" colspan="2">
+            <label name="cost"/>
+            <label name="cost_used"/>
             <field name="cost"/>
+            <field name="cost_used"/>
             <field name="cost_currency"/>
+            <field name="cost_currency_used"/>
+            <field name="cost_edit"/>
         </group>
     </xpath>
 </data>

Reply via email to