Update fifo cost price patch

This commit is contained in:
Bernat Brunet 2020-07-21 09:45:46 +02:00
parent 4b9c342e78
commit 85485ca6bc
1 changed files with 29 additions and 9 deletions

View File

@ -91,7 +91,7 @@ index 1d4bd63..67dbd0c 100644
and self.product.cost_price_method == 'fifo'):
cost_price = self._compute_product_cost_price('in')
diff --git a/product.py b/product.py
index 9eaceac..fdb3040 100644
index 9eaceac..aa1b06c 100644
--- a/trytond/trytond/modules/product_cost_fifo/product.py
+++ b/trytond/trytond/modules/product_cost_fifo/product.py
@@ -7,6 +7,8 @@ from trytond.config import config
@ -190,7 +190,7 @@ index 9eaceac..fdb3040 100644
moves = Move.search(
domain, order=[('effective_date', 'ASC'), ('id', 'ASC')])
@@ -135,11 +183,10 @@ class Product(metaclass=PoolMeta):
@@ -135,32 +183,27 @@ class Product(metaclass=PoolMeta):
quantity = Decimal(str(quantity))
def in_move(move):
@ -203,8 +203,12 @@ index 9eaceac..fdb3040 100644
+ return move.from_location.type == 'storage'
def compute_fifo_cost_price(quantity, date):
fifo_moves = self.get_fifo_move(
@@ -150,16 +197,9 @@ class Product(metaclass=PoolMeta):
- fifo_moves = self.get_fifo_move(
- float(quantity),
- date=current_moves[-1].effective_date)
+ fifo_moves = self.get_fifo_move(float(quantity), date=date)
cost_price = Decimal(0)
consumed_qty = 0
for move, move_qty in fifo_moves:
consumed_qty += move_qty
@ -221,9 +225,15 @@ index 9eaceac..fdb3040 100644
- Decimal(str(10.0 ** -digits[1])))
+ return round_price(cost_price / Decimal(str(consumed_qty)))
+ # Process first the incoming per day
+ # in order to keep quantity positive as much as possible
+ # We do no re-browse because we expect only few permutations
+ moves = sorted(moves, key=lambda m: (
+ m.effective_date, out_move(m), m.id))
current_moves = []
current_out_qty = 0
@@ -183,40 +223,32 @@ class Product(metaclass=PoolMeta):
current_cost_price = cost_price
@@ -183,42 +226,33 @@ class Product(metaclass=PoolMeta):
m for m in out_moves
if m.cost_price != fifo_cost_price],
dict(cost_price=fifo_cost_price))
@ -265,13 +275,23 @@ index 9eaceac..fdb3040 100644
- current_cost_price = cost_price.quantize(
- Decimal(str(10.0 ** -digits[1])))
- else:
- current_out_qty += -qty
- quantity += qty
+ current_cost_price = round_price(cost_price)
+ elif out_move(move):
+ qty *= -1
current_out_qty += -qty
quantity += qty
+ current_out_qty += qty
+ quantity += -qty if out_move(move) else qty
@@ -241,8 +273,4 @@ class Product(metaclass=PoolMeta):
Move.write([
m for m in filter(in_move, current_moves)
@@ -235,14 +269,10 @@ class Product(metaclass=PoolMeta):
m for m in out_moves
if m.cost_price != fifo_cost_price],
dict(cost_price=fifo_cost_price))
- if quantity:
+ if quantity > 0 and quantity + current_out_qty >= 0:
cost_price = (
((current_cost_price * (
quantity + current_out_qty))
- (fifo_cost_price * current_out_qty))
/ quantity)