changeset 0173ea044cf9 in modules/sale:default
details: https://hg.tryton.org/modules/sale?cmd=changeset;node=0173ea044cf9
description:
        Use original cost price for returned move

        and factorize the cost price of move for cost computation

        issue9440
        review327491003
diffstat:

 CHANGELOG |   1 +
 stock.py  |  30 ++++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 0 deletions(-)

diffs (61 lines):

diff -r 12d357664af6 -r 0173ea044cf9 CHANGELOG
--- a/CHANGELOG Wed Jul 29 22:51:13 2020 +0200
+++ b/CHANGELOG Mon Aug 10 23:38:04 2020 +0200
@@ -1,3 +1,4 @@
+* Use original cost price for returned move
 * Add shipping date
 * Rename sale state from cancel to cancelled
 * Add window tab for exception states
diff -r 12d357664af6 -r 0173ea044cf9 stock.py
--- a/stock.py  Wed Jul 29 22:51:13 2020 +0200
+++ b/stock.py  Mon Aug 10 23:38:04 2020 +0200
@@ -1,5 +1,6 @@
 # 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 decimal import Decimal
 from functools import wraps
 
 from trytond.i18n import gettext
@@ -8,6 +9,8 @@
 from trytond.transaction import Transaction
 from trytond.pool import Pool, PoolMeta
 
+from trytond.modules.product import round_price
+
 
 def process_sale(moves_field):
     def _process_sale(func):
@@ -153,6 +156,33 @@
             category = self.origin.unit.category.id
         return category
 
+    def get_cost_price(self, product_cost_price=None):
+        pool = Pool()
+        SaleLine = pool.get('sale.line')
+        Sale = pool.get('sale.sale')
+        # For return sale's move use the cost price of the original sale
+        if (isinstance(self.origin, SaleLine)
+                and self.origin.quantity < 0
+                and self.from_location.type != 'storage'
+                and self.to_location.type == 'storage'
+                and isinstance(self.origin.origin, Sale)):
+            sale = self.origin.origin
+            cost = Decimal(0)
+            qty = Decimal(0)
+            for move in sale.moves:
+                if (move.state == 'done'
+                        and move.from_location.type == 'storage'
+                        and move.to_location.type == 'customer'
+                        and move.product == self.product):
+                    move_quantity = Decimal(str(move.internal_quantity))
+                    cost_price = move.get_cost_price(
+                        product_cost_price=move.cost_price)
+                    qty += move_quantity
+                    cost += cost_price * move_quantity
+            if qty:
+                product_cost_price = round_price(cost / qty)
+        return super().get_cost_price(product_cost_price=product_cost_price)
+
     @property
     def origin_name(self):
         pool = Pool()

Reply via email to