diff --git a/CHANGELOG b/CHANGELOG index d4cc8a76ec..3297ab0918 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 --git a/stock.py b/stock.py index 322902a150..b4ae0d884a 100644 --- a/stock.py +++ b/stock.py @@ -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.model.exceptions import AccessError 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 @@ class Move(metaclass=PoolMeta): 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()