trytond-production_drawing/production.py

98 lines
3.7 KiB
Python
Raw Normal View History

from trytond.model import ModelSQL, ModelView, fields, Unique
2017-08-25 11:45:25 +02:00
from trytond.pyson import Eval, Bool
from trytond.pool import PoolMeta, Pool
__all__ = ['Production', 'ProductionDrawingLine']
2018-08-24 12:21:54 +02:00
class Production(metaclass=PoolMeta):
2017-08-25 11:45:25 +02:00
__name__ = 'production'
drawing = fields.Many2One('production.drawing', 'Drawing',
ondelete='RESTRICT', states={
'readonly': True,
})
drawing_lines = fields.One2Many('production.drawing.line',
'production', 'Drawing Positions', states={
'invisible': ~Bool(Eval('drawing')),
})
drawing_image = fields.Function(fields.Binary('Drawing Image'),
'on_change_with_drawing_image')
@fields.depends('bom', 'drawing_lines')
2017-08-25 11:45:25 +02:00
def on_change_bom(self):
2017-08-25 12:01:35 +02:00
super(Production, self).on_change_bom()
2017-08-25 11:45:25 +02:00
self.drawing = (self.bom.drawing.id if self.bom and self.bom.drawing
else None)
2017-08-25 12:01:35 +02:00
self.on_change_with_drawing_image()
self.on_change_with_drawing_lines()
2017-08-25 11:45:25 +02:00
2018-07-01 18:10:47 +02:00
@fields.depends('bom', 'drawing', 'drawing_lines')
2017-08-25 11:45:25 +02:00
def on_change_with_drawing_lines(self):
Line = Pool().get('production.drawing.line')
2018-07-01 18:10:47 +02:00
if not self.bom or not self.bom.drawing and hasattr(self, 'drawing_lines'):
return []
res = []
2017-08-25 11:45:25 +02:00
for line in self.bom.drawing_lines:
res.append(Line(
position=line.position,
product=line.product,
))
return res
2017-08-25 11:45:25 +02:00
@fields.depends('bom')
def on_change_with_drawing(self):
return self.bom.drawing.id if self.bom and self.bom.drawing else None
@fields.depends('drawing')
def on_change_with_drawing_image(self, name=None):
return self.drawing.image if self.drawing else None
@classmethod
def compute_request(cls, product, warehouse, quantity, date, company,
order_point=None):
2017-08-25 11:45:25 +02:00
Line = Pool().get('production.drawing.line')
production = super(Production, cls).compute_request(product, warehouse,
quantity, date, company, order_point)
drawing_lines = []
2017-08-25 11:45:25 +02:00
if production.bom and production.bom.drawing:
production.drawing = production.bom.drawing
for line in production.bom.drawing_lines:
drawing_lines.append(Line(
2017-08-25 12:01:35 +02:00
position=line.position,
product=line.product,
2017-08-25 11:45:25 +02:00
))
production.drawing_lines = drawing_lines
2017-08-25 11:45:25 +02:00
return production
class ProductionDrawingLine(ModelSQL, ModelView):
'Production Drawing Line'
__name__ = 'production.drawing.line'
production = fields.Many2One('production', 'Production', required=True,
ondelete='CASCADE')
position = fields.Many2One('production.drawing.position',
'Drawing Position', required=True, ondelete='RESTRICT', states={
'readonly': True,
})
product = fields.Many2One('product.product', 'Product')
lot = fields.Many2One('stock.lot', 'Lot', ondelete='RESTRICT', domain=[
('product', '=', Eval('product', -1)),
('id', 'in', Eval('valid_lots', [])),
], depends=['product', 'valid_lots'])
valid_lots = fields.Function(fields.One2Many('stock.lot', None,
'Product'), 'get_valid_lots')
@classmethod
def __setup__(cls):
super(ProductionDrawingLine, cls).__setup__()
t = cls.__table__()
2017-08-25 11:45:25 +02:00
cls._sql_constraints += [
('check_bom_drawing_position_uniq', Unique(t, t.production,
t.position),
2017-08-25 11:45:25 +02:00
'Drawing Position must be unique per BOM.'),
]
def get_valid_lots(self, name):
return [x.lot.id for x in self.production.inputs if x.lot]