trytond-farm/production.py

64 lines
2.3 KiB
Python
Raw Normal View History

2013-08-06 09:52:20 +02:00
#The COPYRIGHT file at the top level of this repository contains the full
#copyright notices and license terms.
from trytond.model import fields
from trytond.pyson import Bool, Eval, Id, If, Not, Or
from trytond.pool import PoolMeta
__all__ = ['BOM']
class BOM:
__name__ = 'production.bom'
2019-06-05 01:04:43 +02:00
__metaclass__ = PoolMeta
2013-08-06 09:52:20 +02:00
semen_dose = fields.Boolean('Semen Dose')
specie = fields.Many2One('farm.specie', 'Dose Specie', states={
'required': Bool(Eval('semen_dose', 0)),
'invisible': Not(Bool(Eval('semen_dose', 0))),
}, depends=['semen_dose'])
@classmethod
def __setup__(cls):
super(BOM, cls).__setup__()
for field in (cls.inputs, cls.outputs):
states = field.states or {}
if 'required' in states:
states['required'] = Or(Bool(Eval('semen_dose', 0)),
states['required'])
else:
states['required'] = Bool(Eval('semen_dose', 0))
field.states = states
field.depends.append('semen_dose')
cls.outputs.size = If(Bool(Eval('semen_dose', 0)), 1,
cls.outputs.size or -1)
cls.outputs.domain.append(If(Bool(Eval('semen_dose', 0)),
('uom', '=', Id('product', 'uom_unit')), ()))
2013-08-06 09:52:20 +02:00
cls._error_messages.update({
'missing_semen_input': 'The Semen Dose BOM "%s" doesn\'t have '
'any input for the Specie\'s Semen Product.',
})
@classmethod
def validate(cls, boms):
super(BOM, cls).validate(boms)
for bom in boms:
bom.check_specie_semen_in_inputs()
def check_specie_semen_in_inputs(self):
if not self.semen_dose:
return
if not self.check_specie_semen_in_inputs_recursive():
self.raise_user_error('missing_semen_input', self.rec_name)
def check_specie_semen_in_inputs_recursive(self):
2013-08-06 09:52:20 +02:00
if self.semen_dose:
semen_product = self.specie.semen_product
for i in self.inputs:
if i.product.boms:
for l in i.product.boms:
if l.bom.check_specie_semen_in_inputs_recursive():
return True
elif i.product == semen_product:
return True
return False