trytond-stock_lot_cost/stock.py

114 lines
3.4 KiB
Python
Raw Normal View History

# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from decimal import Decimal
2016-07-22 11:34:33 +02:00
from trytond.model import ModelSQL, ModelView, Unique, fields
2013-09-18 10:09:46 +02:00
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
class LotCostCategory(ModelSQL, ModelView):
'''Stock Lot Cost Category'''
__name__ = 'stock.lot.cost_category'
name = fields.Char('Name', translate=True, required=True)
@classmethod
def __setup__(cls):
super(LotCostCategory, cls).__setup__()
2016-07-22 11:34:33 +02:00
t = cls.__table__()
2013-09-18 10:09:46 +02:00
cls._sql_constraints += [
2016-07-22 11:34:33 +02:00
('name_uniq', Unique(t, t.name),
2019-06-17 23:51:19 +02:00
'stock_lot_cost.msg_category_name_unique'),
2013-09-18 10:09:46 +02:00
]
class LotCostLine(ModelSQL, ModelView):
'''Stock Lot Cost Line'''
__name__ = 'stock.lot.cost_line'
lot = fields.Many2One('stock.lot', 'Lot', required=True,
2013-09-18 10:09:46 +02:00
ondelete='CASCADE')
category = fields.Many2One('stock.lot.cost_category', 'Category',
required=True)
unit_price = fields.Numeric('Unit Price', required=True)
origin = fields.Reference('Origin', selection='get_origin', readonly=True)
2013-09-18 10:09:46 +02:00
@classmethod
def _get_origin(cls):
'Return list of Model names for origin Reference'
return [
'stock.move',
]
@classmethod
def get_origin(cls):
pool = Pool()
Model = pool.get('ir.model')
models = cls._get_origin()
models = Model.search([
('model', 'in', models),
])
return [('', '')] + [(m.model, m.name) for m in models]
2013-09-18 10:09:46 +02:00
2018-10-15 00:15:37 +02:00
class Lot(metaclass=PoolMeta):
2013-09-18 10:09:46 +02:00
__name__ = 'stock.lot'
cost_lines = fields.One2Many('stock.lot.cost_line', 'lot', 'Cost Lines')
cost_price = fields.Function(fields.Numeric('Cost Price'),
2013-09-18 10:09:46 +02:00
'get_cost_price')
def get_cost_price(self, name):
2020-01-09 16:28:26 +01:00
if not self.cost_lines:
return
return sum(l.unit_price for l in self.cost_lines if l.unit_price is not
None)
2013-09-18 10:09:46 +02:00
@fields.depends('product', 'cost_lines')
2013-09-18 10:09:46 +02:00
def on_change_product(self):
try:
super(Lot, self).on_change_product()
2013-09-18 10:09:46 +02:00
except AttributeError:
pass
2013-09-18 10:09:46 +02:00
if not self.id or self.id <= 0:
return
2013-09-18 10:09:46 +02:00
cost_lines = self._on_change_product_cost_lines()
if cost_lines:
2016-07-27 10:03:58 +02:00
cost_lines = cost_lines.get('add')
LotCostLine = Pool().get('stock.lot.cost_line')
lot_cost_lines = LotCostLine.search([
('lot', '=', self.id),
2016-07-27 10:03:58 +02:00
('category', '=', cost_lines[0][1]['category']),
('unit_price', '=', cost_lines[0][1]['unit_price']),
])
if lot_cost_lines:
self.cost_lines = lot_cost_lines
2013-09-18 10:09:46 +02:00
def _on_change_product_cost_lines(self):
pool = Pool()
ModelData = pool.get('ir.model.data')
if not self.product:
return {}
2013-09-18 10:09:46 +02:00
category_id = ModelData.get_id('stock_lot_cost',
'cost_category_standard_price')
return {
'add': [(0, {
'category': category_id,
'unit_price': self.product.cost_price,
})],
2013-09-18 10:09:46 +02:00
}
2018-10-15 00:15:37 +02:00
class Move(metaclass=PoolMeta):
2013-09-18 10:09:46 +02:00
__name__ = 'stock.move'
@classmethod
def __setup__(cls):
super(Move, cls).__setup__()
cls.lot.context['from_move'] = Eval('id')