Files
tryton/move.py
T

114 lines
4.2 KiB
Python
Raw Normal View History

2014-12-02 12:55:33 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2010-08-19 22:16:13 +02:00
from decimal import Decimal
from trytond.model import Workflow, ModelView, fields, Check
2012-09-24 22:09:23 +02:00
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
2012-09-24 22:09:23 +02:00
__all__ = ['Move']
__metaclass__ = PoolMeta
2008-11-03 18:51:00 +01:00
2012-09-24 22:09:23 +02:00
class Move:
__name__ = 'stock.move'
fifo_quantity = fields.Float('FIFO Quantity')
2008-11-03 18:51:00 +01:00
2012-09-24 22:09:23 +02:00
@classmethod
def __setup__(cls):
super(Move, cls).__setup__()
t = cls.__table__()
2012-09-24 22:09:23 +02:00
cls._sql_constraints += [
('check_fifo_quantity_out',
Check(t, t.quantity >= t.fifo_quantity),
'FIFO quantity can not be greater than quantity.'),
]
2012-09-24 22:09:23 +02:00
cls._error_messages.update({
'del_move_fifo': ('You can not delete move "%s" that is used '
'for FIFO cost price.'),
2012-09-24 22:09:23 +02:00
})
2008-11-03 18:51:00 +01:00
2012-09-24 22:09:23 +02:00
@staticmethod
def default_fifo_quantity():
2010-12-12 16:50:44 +01:00
return 0.0
2008-11-03 18:51:00 +01:00
def _update_fifo_out_product_cost_price(self):
2008-11-03 18:51:00 +01:00
'''
Update the product cost price of the given product on the move. Update
fifo_quantity on the concerned incomming moves. Return the
cost price for outputing the given product and quantity.
2008-11-03 18:51:00 +01:00
'''
pool = Pool()
Uom = pool.get('product.uom')
Currency = pool.get('currency.currency')
2008-11-03 18:51:00 +01:00
total_qty = Uom.compute_qty(self.uom, self.quantity,
self.product.default_uom, round=False)
2008-11-03 18:51:00 +01:00
fifo_moves = self.product.template.get_fifo_move(total_qty)
2008-11-03 18:51:00 +01:00
cost_price = Decimal("0.0")
consumed_qty = 0.0
for move, move_qty in fifo_moves:
consumed_qty += move_qty
2008-11-03 18:51:00 +01:00
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,
2010-08-19 22:16:13 +02:00
move.product.default_uom)
cost_price += move_unit_price * Decimal(str(move_qty))
move.quantity = move_qty
move._update_product_cost_price('out')
move_qty = Uom.compute_qty(self.product.default_uom, move_qty,
2010-08-19 22:16:13 +02:00
move.uom, round=False)
# Use write as move instance quantity was modified to call
# _update_product_cost_price
self.write([self.__class__(move.id)], {
'fifo_quantity': (move.fifo_quantity or 0.0) + move_qty,
})
if Decimal(str(consumed_qty)) != Decimal("0"):
cost_price = cost_price / Decimal(str(consumed_qty))
if cost_price != Decimal("0"):
digits = self.__class__.cost_price.digits
return cost_price.quantize(
2012-04-03 20:07:21 +02:00
Decimal(str(10.0 ** -digits[1])))
else:
return self.product.cost_price
2008-11-03 18:51:00 +01:00
2014-08-29 16:24:28 +02:00
def _do(self):
if (self.from_location.type in ('supplier', 'production')
and self.to_location.type == 'storage'
and self.product.cost_price_method == 'fifo'):
self._update_product_cost_price('in')
elif (self.to_location.type == 'supplier'
and self.from_location.type == 'storage'
and self.product.cost_price_method == 'fifo'):
self._update_product_cost_price('out')
elif (self.from_location.type == 'storage'
and self.to_location.type != 'storage'
and self.product.cost_price_method == 'fifo'):
cost_price = self._update_fifo_out_product_cost_price()
2015-10-22 11:02:49 +02:00
if self.cost_price is None:
2014-08-29 16:24:28 +02:00
self.cost_price = cost_price
super(Move, self)._do()
2008-11-03 18:51:00 +01:00
2012-09-24 22:09:23 +02:00
@classmethod
2012-12-27 21:54:05 +01:00
@ModelView.button
@Workflow.transition('cancel')
def cancel(cls, moves):
pass
2008-11-03 18:51:00 +01:00
2012-09-24 22:09:23 +02:00
@classmethod
def delete(cls, moves):
2013-07-02 15:59:48 +02:00
fifo_moves = cls.search([
2012-09-24 22:09:23 +02:00
('id', 'in', [m.id for m in moves]),
('fifo_quantity', '!=', 0.0),
])
2013-07-02 15:59:48 +02:00
if fifo_moves:
cls.raise_user_error('del_move_fifo', (fifo_moves[0].rec_name,))
2012-09-24 22:09:23 +02:00
super(Move, cls).delete(moves)