2014-04-10 17:19:39 +02:00
|
|
|
# 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'
|
|
|
|
|
2022-11-09 17:50:43 +01:00
|
|
|
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)
|
2020-01-09 16:13:49 +01:00
|
|
|
unit_price = fields.Numeric('Unit Price', required=True)
|
2022-11-09 17:50:43 +01:00
|
|
|
origin = fields.Reference('Origin', selection='get_origin', readonly=True)
|
2013-09-18 10:09:46 +02:00
|
|
|
|
2013-10-10 12:35:20 +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')
|
2020-01-09 16:13:49 +01:00
|
|
|
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
|
2020-01-16 11:56:07 +01:00
|
|
|
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
|
|
|
|
2014-06-03 13:00:44 +02:00
|
|
|
@fields.depends('product', 'cost_lines')
|
2013-09-18 10:09:46 +02:00
|
|
|
def on_change_product(self):
|
|
|
|
try:
|
2016-07-22 12:53:45 +02:00
|
|
|
super(Lot, self).on_change_product()
|
2013-09-18 10:09:46 +02:00
|
|
|
except AttributeError:
|
2016-07-22 12:53:45 +02:00
|
|
|
pass
|
2013-09-18 10:09:46 +02:00
|
|
|
|
2019-06-17 23:53:35 +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([
|
2019-06-17 23:53:35 +02:00
|
|
|
('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:
|
2019-06-17 23:56:45 +02:00
|
|
|
return {}
|
2013-09-18 10:09:46 +02:00
|
|
|
|
|
|
|
category_id = ModelData.get_id('stock_lot_cost',
|
|
|
|
'cost_category_standard_price')
|
|
|
|
return {
|
2014-06-03 13:00:44 +02:00
|
|
|
'add': [(0, {
|
2014-04-10 17:19:39 +02:00
|
|
|
'category': category_id,
|
|
|
|
'unit_price': self.product.cost_price,
|
2014-06-03 13:00:44 +02:00
|
|
|
})],
|
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')
|