changeset c819aa53a503 in modules/account_stock_landed_cost:default
details: 
https://hg.tryton.org/modules/account_stock_landed_cost?cmd=changeset;node=c819aa53a503
description:
        Allow posted landed cost to be cancelled

        issue9605
        review319961002
diffstat:

 CHANGELOG                                    |   1 +
 account.py                                   |  22 +++++++++++++++++-----
 account.xml                                  |   2 +-
 tests/scenario_account_stock_landed_cost.rst |  13 +++++++++++++
 4 files changed, 32 insertions(+), 6 deletions(-)

diffs (115 lines):

diff -r 937e1114ce40 -r c819aa53a503 CHANGELOG
--- a/CHANGELOG Sun Oct 04 00:37:27 2020 +0200
+++ b/CHANGELOG Sun Oct 11 13:37:32 2020 +0200
@@ -1,3 +1,4 @@
+* Allow posted landed cost to be cancelled
 * Add landed cost relate from supplier shipment
 * Rename landed cost state from cancel to cancelled
 
diff -r 937e1114ce40 -r c819aa53a503 account.py
--- a/account.py        Sun Oct 04 00:37:27 2020 +0200
+++ b/account.py        Sun Oct 11 13:37:32 2020 +0200
@@ -129,11 +129,12 @@
         cls._transitions |= set((
                 ('draft', 'posted'),
                 ('draft', 'cancelled'),
+                ('posted', 'cancelled'),
                 ('cancelled', 'draft'),
                 ))
         cls._buttons.update({
                 'cancel': {
-                    'invisible': Eval('state') != 'draft',
+                    'invisible': Eval('state') == 'cancelled',
                     'depends': ['state'],
                     },
                 'draft': {
@@ -179,7 +180,14 @@
     @ModelView.button
     @Workflow.transition('cancelled')
     def cancel(cls, landed_costs):
-        pass
+        for landed_cost in landed_costs:
+            if landed_cost.state == 'posted':
+                getattr(landed_cost, 'unallocate_cost_by_%s' %
+                    landed_cost.allocation_method)()
+        cls.write(landed_costs, {
+                'posted_date': None,
+                'state': 'cancelled',
+                })
 
     @classmethod
     @ModelView.button
@@ -204,6 +212,9 @@
     def allocate_cost_by_value(self):
         self._allocate_cost(self._get_value_factors())
 
+    def unallocate_cost_by_value(self):
+        self._allocate_cost(self._get_value_factors(), sign=-1)
+
     def _get_value_factors(self):
         "Return the factor for each move based on value"
         pool = Pool()
@@ -232,11 +243,12 @@
                 factors[move.id] = quantity * unit_prices[move.id] / sum_value
         return factors
 
-    def _allocate_cost(self, factors):
+    def _allocate_cost(self, factors, sign=1):
         "Allocate cost on moves using factors"
         pool = Pool()
         Move = pool.get('stock.move')
         Currency = pool.get('currency.currency')
+        assert sign in {1, -1}
 
         cost = self.cost
         currency = self.company.currency
@@ -278,8 +290,8 @@
                 unit_landed_cost, rounding=ROUND_HALF_EVEN)
             if move.unit_landed_cost is None:
                 move.unit_landed_cost = 0
-            move.unit_price += unit_landed_cost
-            move.unit_landed_cost += unit_landed_cost
+            move.unit_price += unit_landed_cost * sign
+            move.unit_landed_cost += unit_landed_cost * sign
         Move.save(moves)
 
     @classmethod
diff -r 937e1114ce40 -r c819aa53a503 account.xml
--- a/account.xml       Sun Oct 04 00:37:27 2020 +0200
+++ b/account.xml       Sun Oct 11 13:37:32 2020 +0200
@@ -127,7 +127,7 @@
         <record model="ir.model.button" id="landed_cost_post_button">
             <field name="name">post</field>
             <field name="string">Post</field>
-            <field name="confirm">Are you sure you want to post the landed 
cost?</field>
+            <field name="confirm" eval="None"/>
             <field name="model"
                 search="[('model', '=', 'account.landed_cost')]"/>
         </record>
diff -r 937e1114ce40 -r c819aa53a503 
tests/scenario_account_stock_landed_cost.rst
--- a/tests/scenario_account_stock_landed_cost.rst      Sun Oct 04 00:37:27 
2020 +0200
+++ b/tests/scenario_account_stock_landed_cost.rst      Sun Oct 11 13:37:32 
2020 +0200
@@ -155,6 +155,8 @@
     >>> landed_cost.click('post')
     >>> landed_cost.state
     'posted'
+    >>> bool(landed_cost.posted_date)
+    True
 
 Check move unit price is 101::
 
@@ -168,3 +170,14 @@
     >>> landed_cost.reload()
     >>> len(landed_cost.invoice_lines)
     1
+
+Cancel landed cost reset unit price::
+
+    >>> landed_cost.click('cancel')
+    >>> landed_cost.state
+    'cancelled'
+    >>> landed_cost.posted_date
+
+    >>> move.reload()
+    >>> move.unit_price
+    Decimal('100.0000')

Reply via email to