Added date param to get_cost_price method and return purchase cost price.

This commit refs #22596
This commit is contained in:
José Miguel Pardo Salar 2022-04-12 09:48:09 +02:00
parent 8f1f4ba4f0
commit b0c17e9d91

View file

@ -3,9 +3,6 @@
from trytond.pool import PoolMeta, Pool
from trytond.model import fields
from trytond.modules.product import price_digits
from trytond.transaction import Transaction
__all__ = ['Lot']
class Lot(metaclass=PoolMeta):
@ -14,18 +11,18 @@ class Lot(metaclass=PoolMeta):
cost_price = fields.Function(fields.Numeric(
'Cost Price', digits=price_digits), 'get_cost_price')
def get_cost_price(self, name=None):
def get_cost_price(self, name=None, date=None):
pool = Pool()
Move = pool.get('stock.move')
Date = pool.get('ir.date')
date = Transaction().context.get('date') or Date.today()
if not date:
date = Date.today()
moves = Move.search([
('lot', '=', self),
('state', 'in', ['assigned', 'done']),
('origin', 'like', 'purchase.line,%'),
('effective_date', '<=', date)
])
], order=[('effective_date', 'DESC')])
if moves:
return moves[0].unit_price
return moves[0].purchase_unit_price