trytond-patches/product-cost-fifo-Use-origi...

127 lines
5.5 KiB
Diff

From d23677f089ff7e283e30a1aaa0e1ed2e42c31fa9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=80ngel=20=C3=80lvarez?= <angel@nan-tic.com>
Date: Thu, 8 Apr 2021 11:53:18 +0200
Subject: [PATCH] Use original cost price for returned move
and factorize the cost price of move for cost computation
issue9440
review327491003
---
move.py | 23 +++++------------------
product.py | 23 ++---------------------
2 files changed, 7 insertions(+), 39 deletions(-)
diff --git a/move.py b/move.py
index 9e20a1d..423a4bd 100644
--- a/trytond/trytond/modules/product_cost_fifo/move.py
+++ b/trytond/trytond/modules/product_cost_fifo/move.py
@@ -68,7 +68,6 @@ class Move(metaclass=PoolMeta):
'''
pool = Pool()
Uom = pool.get('product.uom')
- Currency = pool.get('currency.currency')
total_qty = Uom.compute_qty(self.uom, self.quantity,
self.product.default_uom, round=False)
@@ -81,16 +80,7 @@ class Move(metaclass=PoolMeta):
to_save = []
for move, move_qty in fifo_moves:
consumed_qty += move_qty
- if move.from_location.type in {'supplier', 'production'}:
- with Transaction().set_context(date=move.effective_date):
- move_unit_price = Currency.compute(
- move.currency, move.unit_price,
- self.company.currency, round=False)
- move_unit_price = Uom.compute_price(
- move.uom, move_unit_price, move.product.default_uom)
- else:
- move_unit_price = move.cost_price or 0
- cost_price += move_unit_price * Decimal(str(move_qty))
+ cost_price += move.get_cost_price() * Decimal(str(move_qty))
move_qty = Uom.compute_qty(self.product.default_uom, move_qty,
move.uom, round=False)
@@ -110,12 +100,9 @@ class Move(metaclass=PoolMeta):
'cost_price', **self._cost_price_pattern)
# Compute average cost price
- unit_price = self.unit_price
- self.unit_price = Uom.compute_price(
- self.product.default_uom, cost_price, self.uom)
- average_cost_price = self._compute_product_cost_price('out')
- self.unit_price = unit_price
-
+ average_cost_price = self._compute_product_cost_price(
+ 'out', product_cost_price=cost_price)
+
if cost_price:
digits = self.__class__.cost_price.digits
cost_price = cost_price.quantize(
@@ -126,7 +113,7 @@ class Move(metaclass=PoolMeta):
def _do(self):
cost_price = super(Move, self)._do()
- if (self.from_location.type in ('supplier', 'production')
+ if (self.from_location.type != 'storage'
and self.to_location.type == 'storage'
and self.product.cost_price_method == 'fifo'):
cost_price = self._compute_product_cost_price('in')
diff --git a/product.py b/product.py
index b7e25bb..13e7d95 100644
--- a/trytond/trytond/modules/product_cost_fifo/product.py
+++ b/trytond/trytond/modules/product_cost_fifo/product.py
@@ -4,7 +4,6 @@ import datetime as dt
from decimal import Decimal
from trytond.config import config
-from trytond.transaction import Transaction
from trytond.pool import Pool, PoolMeta
__all__ = ['Template', 'Product']
@@ -92,7 +91,6 @@ class Product(metaclass=PoolMeta):
def recompute_cost_price_fifo(self, start=None):
pool = Pool()
Move = pool.get('stock.move')
- Currency = pool.get('currency.currency')
Uom = pool.get('product.uom')
digits = self.__class__.cost_price.digits
@@ -148,16 +146,7 @@ class Product(metaclass=PoolMeta):
consumed_qty = 0
for move, move_qty in fifo_moves:
consumed_qty += move_qty
- if move.from_location.type in {'supplier', 'production'}:
- with Transaction().set_context(date=move.effective_date):
- unit_price = Currency.compute(
- move.currency, move.unit_price,
- move.company.currency, round=False)
- unit_price = Uom.compute_price(
- move.uom, unit_price, move.product.default_uom)
- else:
- unit_price = move.cost_price or 0
- cost_price += unit_price * Decimal(str(move_qty))
+ cost_price += move.get_cost_price() * Decimal(str(move_qty))
if consumed_qty:
return (cost_price / Decimal(str(consumed_qty))).quantize(
Decimal(str(10.0 ** -digits[1])))
@@ -203,15 +192,7 @@ class Product(metaclass=PoolMeta):
if move.from_location.type == 'storage':
qty *= -1
if in_move(move):
- if move.from_location.type in {'supplier', 'production'}:
- with Transaction().set_context(date=move.effective_date):
- unit_price = Currency.compute(
- move.currency, move.unit_price,
- move.company.currency, round=False)
- unit_price = Uom.compute_price(
- move.uom, unit_price, self.default_uom)
- else:
- unit_price = cost_price
+ unit_price = move.get_cost_price(product_cost_price=cost_price)
if quantity + qty > 0 and quantity >= 0:
cost_price = (
(cost_price * quantity) + (unit_price * qty)
--
2.25.1