Files
tryton/modules/production/bom.py
T

282 lines
9.2 KiB
Python
Raw Normal View History

2014-12-02 12:55:39 +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.
2021-12-11 17:58:43 +01:00
from trytond.model import DeactivableMixin, ModelSQL, ModelView, fields
2012-05-13 23:00:54 +02:00
from trytond.pool import Pool
2021-12-11 17:58:43 +01:00
from trytond.pyson import Eval
from trytond.wizard import Button, StateView, Wizard
2012-05-13 23:00:54 +02:00
2018-03-26 14:11:26 +02:00
class BOM(DeactivableMixin, ModelSQL, ModelView):
2012-05-13 23:00:54 +02:00
"Bill of Material"
2012-09-24 22:12:50 +02:00
__name__ = 'production.bom'
2012-05-13 23:00:54 +02:00
name = fields.Char('Name', required=True, translate=True)
inputs = fields.One2Many('production.bom.input', 'bom', 'Inputs')
outputs = fields.One2Many('production.bom.output', 'bom', 'Outputs')
output_products = fields.Many2Many('production.bom.output',
'bom', 'product', 'Output Products')
2023-08-20 01:39:36 +02:00
def compute_factor(self, product, quantity, unit):
2012-05-13 23:00:54 +02:00
'''
Compute factor for an output product
'''
2012-09-24 22:12:50 +02:00
Uom = Pool().get('product.uom')
2021-05-18 22:19:09 +02:00
output_quantity = 0
2012-09-24 22:12:50 +02:00
for output in self.outputs:
2012-05-13 23:00:54 +02:00
if output.product == product:
2021-05-18 22:19:09 +02:00
output_quantity += Uom.compute_qty(
2023-08-20 01:39:36 +02:00
output.unit, output.quantity, unit, round=False)
2021-05-18 22:19:09 +02:00
if output_quantity:
return quantity / output_quantity
else:
return 0
2012-05-13 23:00:54 +02:00
2012-09-24 22:12:50 +02:00
@classmethod
def copy(cls, records, default=None):
2012-05-13 23:00:54 +02:00
if default is None:
default = {}
else:
default = default.copy()
2018-09-01 01:02:03 +02:00
default.setdefault('output_products', None)
2012-09-24 22:12:50 +02:00
return super(BOM, cls).copy(records, default=default)
2012-05-13 23:00:54 +02:00
class BOMInput(ModelSQL, ModelView):
"Bill of Material Input"
2012-09-24 22:12:50 +02:00
__name__ = 'production.bom.input'
2012-05-13 23:00:54 +02:00
bom = fields.Many2One(
'production.bom', "BOM", required=True, ondelete='CASCADE')
product = fields.Many2One('product.product', 'Product', required=True)
2012-05-13 23:00:54 +02:00
uom_category = fields.Function(fields.Many2One(
2014-01-26 14:29:24 +01:00
'product.uom.category', 'Uom Category'), 'on_change_with_uom_category')
2023-08-20 01:39:36 +02:00
unit = fields.Many2One(
'product.uom', "Unit", required=True,
2012-05-13 23:00:54 +02:00
domain=[
('category', '=', Eval('uom_category')),
])
2023-08-20 01:39:36 +02:00
quantity = fields.Float(
"Quantity", digits='unit', required=True,
domain=['OR',
('quantity', '>=', 0),
('quantity', '=', None),
2021-08-30 00:27:13 +02:00
])
2012-05-13 23:00:54 +02:00
2012-09-24 22:12:50 +02:00
@classmethod
def __setup__(cls):
super(BOMInput, cls).__setup__()
cls.product.domain = [('type', 'in', cls.get_product_types())]
2021-03-07 11:13:53 +01:00
cls.__access__.add('bom')
2021-05-18 22:19:09 +02:00
@classmethod
def __register__(cls, module):
2023-08-20 01:39:36 +02:00
table_h = cls.__table_handler__(module)
# Migration from 6.8: rename uom to unit
if (table_h.column_exist('uom')
and not table_h.column_exist('unit')):
table_h.column_rename('uom', 'unit')
2021-05-18 22:19:09 +02:00
super().__register__(module)
table_h = cls.__table_handler__(module)
# Migration from 6.0: remove unique constraint
table_h.drop_constraint('product_bom_uniq')
2012-05-13 23:00:54 +02:00
@classmethod
def get_product_types(cls):
return ['goods', 'assets']
2023-08-20 01:39:36 +02:00
@fields.depends('product', 'unit')
2012-09-24 22:12:50 +02:00
def on_change_product(self):
if self.product:
category = self.product.default_uom.category
2023-08-20 01:39:36 +02:00
if not self.unit or self.unit.category != category:
self.unit = self.product.default_uom
2012-05-13 23:00:54 +02:00
else:
2023-08-20 01:39:36 +02:00
self.unit = None
2012-05-13 23:00:54 +02:00
2014-01-26 14:29:24 +01:00
@fields.depends('product')
2012-09-24 22:12:50 +02:00
def on_change_with_uom_category(self, name=None):
2023-03-26 09:49:00 +02:00
return self.product.default_uom.category if self.product else None
2012-05-13 23:00:54 +02:00
2017-07-26 23:43:23 +02:00
def get_rec_name(self, name):
return self.product.rec_name
@classmethod
def search_rec_name(cls, name, clause):
2017-09-27 18:37:30 +02:00
return [('product.rec_name',) + tuple(clause[1:])]
2017-07-26 23:43:23 +02:00
@classmethod
def validate(cls, boms):
super(BOMInput, cls).validate(boms)
for bom in boms:
bom.check_bom_recursion()
2012-09-24 22:12:50 +02:00
def check_bom_recursion(self):
2012-05-13 23:00:54 +02:00
'''
Check BOM recursion
'''
self.product.check_bom_recursion()
2012-05-13 23:00:54 +02:00
2012-09-24 22:12:50 +02:00
def compute_quantity(self, factor):
2023-08-20 01:39:36 +02:00
return self.unit.ceil(self.quantity * factor)
2012-05-13 23:00:54 +02:00
class BOMOutput(BOMInput):
"Bill of Material Output"
2012-09-24 22:12:50 +02:00
__name__ = 'production.bom.output'
_table = 'production_bom_output' # Needed to override BOMInput._table
2012-05-13 23:00:54 +02:00
2017-02-02 12:12:19 +01:00
def compute_quantity(self, factor):
2023-08-20 01:39:36 +02:00
return self.unit.floor(self.quantity * factor)
2017-02-02 12:12:19 +01:00
@classmethod
def delete(cls, outputs):
pool = Pool()
ProductBOM = pool.get('product.product-production.bom')
bom_products = [b for o in outputs for b in o.product.boms]
super(BOMOutput, cls).delete(outputs)
# Validate that output_products domain on bom is still valid
ProductBOM._validate(bom_products, ['bom'])
@classmethod
def write(cls, *args):
pool = Pool()
ProductBOM = pool.get('product.product-production.bom')
actions = iter(args)
bom_products = []
for outputs, values in zip(actions, actions):
if 'product' in values:
bom_products.extend(
[b for o in outputs for b in o.product.boms])
super().write(*args)
# Validate that output_products domain on bom is still valid
ProductBOM._validate(bom_products, ['bom'])
2012-05-13 23:00:54 +02:00
class BOMTree(ModelView):
'BOM Tree'
2012-09-24 22:12:50 +02:00
__name__ = 'production.bom.tree'
2012-05-13 23:00:54 +02:00
product = fields.Many2One('product.product', 'Product')
2023-08-20 01:39:36 +02:00
quantity = fields.Float('Quantity', digits='unit')
unit = fields.Many2One('product.uom', "Unit")
2012-05-13 23:00:54 +02:00
childs = fields.One2Many('production.bom.tree', None, 'Childs')
2012-09-24 22:12:50 +02:00
@classmethod
2023-08-20 01:39:36 +02:00
def tree(cls, product, quantity, unit, bom=None):
2012-09-24 22:12:50 +02:00
Input = Pool().get('production.bom.input')
2012-05-13 23:00:54 +02:00
result = []
if bom is None:
if not product.boms:
return result
bom = product.boms[0].bom
2023-08-20 01:39:36 +02:00
factor = bom.compute_factor(product, quantity, unit)
2012-09-24 22:12:50 +02:00
for input_ in bom.inputs:
quantity = Input.compute_quantity(input_, factor)
2023-08-20 01:39:36 +02:00
childs = cls.tree(input_.product, quantity, input_.unit)
2012-05-13 23:00:54 +02:00
values = {
2012-09-24 22:12:50 +02:00
'product': input_.product.id,
'product.': {
'rec_name': input_.product.rec_name,
},
2012-05-13 23:00:54 +02:00
'quantity': quantity,
2023-08-20 01:39:36 +02:00
'unit': input_.unit.id,
'unit.': {
'rec_name': input_.unit.rec_name,
},
2012-05-13 23:00:54 +02:00
'childs': childs,
}
result.append(values)
return result
class OpenBOMTreeStart(ModelView):
'Open BOM Tree'
2012-09-24 22:12:50 +02:00
__name__ = 'production.bom.tree.open.start'
2012-05-13 23:00:54 +02:00
2023-08-20 01:39:36 +02:00
quantity = fields.Float('Quantity', digits='unit', required=True)
unit = fields.Many2One(
'product.uom', "Unit", required=True,
2012-05-13 23:00:54 +02:00
domain=[
('category', '=', Eval('category')),
])
2012-05-13 23:00:54 +02:00
category = fields.Many2One('product.uom.category', 'Category',
readonly=True)
bom = fields.Many2One('product.product-production.bom',
'BOM', required=True, domain=[
('product', '=', Eval('product')),
])
2012-05-13 23:00:54 +02:00
product = fields.Many2One('product.product', 'Product', readonly=True)
class OpenBOMTreeTree(ModelView):
'Open BOM Tree'
2012-09-24 22:12:50 +02:00
__name__ = 'production.bom.tree.open.tree'
2012-05-13 23:00:54 +02:00
2015-12-15 09:54:49 +01:00
bom_tree = fields.One2Many('production.bom.tree', None, 'BOM Tree',
readonly=True)
2012-05-13 23:00:54 +02:00
2012-09-24 22:12:50 +02:00
@classmethod
2023-08-20 01:39:36 +02:00
def tree(cls, bom, product, quantity, unit):
2012-05-13 23:00:54 +02:00
pool = Pool()
2012-09-24 22:12:50 +02:00
Tree = pool.get('production.bom.tree')
2012-05-13 23:00:54 +02:00
2023-08-20 01:39:36 +02:00
childs = Tree.tree(product, quantity, unit, bom=bom)
2012-05-13 23:00:54 +02:00
bom_tree = [{
2012-09-24 22:12:50 +02:00
'product': product.id,
'product.': {
'rec_name': product.rec_name,
},
2012-09-24 22:12:50 +02:00
'quantity': quantity,
2023-08-20 01:39:36 +02:00
'unit': unit.id,
'unit.': {
'rec_name': unit.rec_name,
},
2012-09-24 22:12:50 +02:00
'childs': childs,
}]
2012-05-13 23:00:54 +02:00
return {
'bom_tree': bom_tree,
2012-09-24 22:12:50 +02:00
}
2012-05-13 23:00:54 +02:00
class OpenBOMTree(Wizard):
'Open BOM Tree'
2012-09-24 22:12:50 +02:00
__name__ = 'production.bom.tree.open'
2012-05-13 23:00:54 +02:00
start = StateView('production.bom.tree.open.start',
'production.bom_tree_open_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
2015-03-10 18:42:41 +01:00
Button('OK', 'tree', 'tryton-ok', True),
2012-05-13 23:00:54 +02:00
])
tree = StateView('production.bom.tree.open.tree',
'production.bom_tree_open_tree_view_form', [
2018-08-19 20:02:58 +02:00
Button('Change', 'start', 'tryton-back'),
2012-05-13 23:00:54 +02:00
Button('Close', 'end', 'tryton-close'),
])
2012-09-24 22:12:50 +02:00
def default_start(self, fields):
2012-05-13 23:00:54 +02:00
defaults = {}
product = self.record
2012-05-13 23:00:54 +02:00
defaults['category'] = product.default_uom.category.id
2023-08-20 01:39:36 +02:00
if getattr(self.start, 'unit', None):
defaults['unit'] = self.start.unit.id
2012-05-13 23:00:54 +02:00
else:
2023-08-20 01:39:36 +02:00
defaults['unit'] = product.default_uom.id
2012-05-13 23:00:54 +02:00
defaults['product'] = product.id
2012-09-24 22:12:50 +02:00
if getattr(self.start, 'bom', None):
defaults['bom'] = self.start.bom.id
2012-05-13 23:00:54 +02:00
elif product.boms:
defaults['bom'] = product.boms[0].id
2012-09-24 22:12:50 +02:00
defaults['quantity'] = getattr(self.start, 'quantity', None)
2012-05-13 23:00:54 +02:00
return defaults
2012-09-24 22:12:50 +02:00
def default_tree(self, fields):
2012-05-13 23:00:54 +02:00
pool = Pool()
2012-09-24 22:12:50 +02:00
BomTree = pool.get('production.bom.tree.open.tree')
return BomTree.tree(self.start.bom.bom, self.start.product,
2023-08-20 01:39:36 +02:00
self.start.quantity, self.start.unit)