diff --git a/__init__.py b/__init__.py index d04e623..52529e9 100644 --- a/__init__.py +++ b/__init__.py @@ -9,6 +9,7 @@ module = 'product_bulk' def register(): Pool.register( + product.ExtraProductPackaging, product.Product, product.TemplateProductPackaging, product.Template, diff --git a/product.py b/product.py index c2d84da..13fa0c7 100644 --- a/product.py +++ b/product.py @@ -9,7 +9,8 @@ from trytond.wizard import (Wizard, StateAction, StateView, StateTransition, Button) from trytond.exceptions import UserWarning -__all__ = ['Template', 'Product', 'TemplateProductPackaging'] +__all__ = ['Template', 'Product', 'TemplateProductPackaging', + 'ExtraProductPackaging'] from trytond.modules.product.product import STATES, DEPENDS @@ -34,6 +35,28 @@ class TemplateProductPackaging(ModelSQL, ModelView): }, ) +class ExtraProductPackaging(ModelSQL, ModelView): + "Template - Product Packaging" + __name__ = 'product.template-extra.product' + _rec_name = 'packaging_product' + extra_product = fields.Many2One('product.template', 'Extra Product', + required=True, ondelete='CASCADE', + states = { + 'readonly': Bool(Eval('packaged_product')), + }) + product = fields.Many2One('product.template', 'Product', required=True) + unit_digits = fields.Function(fields.Integer('Unit Digits'), + 'on_change_with_unit_digits') + quantity = fields.Float('Quantity', + digits=(16, Eval('unit_digits', 2)), + depends=['unit_digits']) + + @fields.depends('product') + def on_change_with_unit_digits(self, name=None): + if self.extra_product.default_uom: + return self.extra_product.default_uom.digits + return 2 + class Template(metaclass=PoolMeta): __name__ = 'product.template' @@ -86,6 +109,11 @@ class Template(metaclass=PoolMeta): depends=['type', 'netweight']) netweight_digits = fields.Function(fields.Integer('Net Weight Digits'), 'on_change_with_netweight_digits') + extra_products = fields.One2Many('product.template-extra.product', + 'product', 'Extra Products', + states = { + 'readonly': (~Eval('active', True) | Eval('bulk_type') != True), + }) def sum_product(self, name): if name in ('bulk_quantity'): @@ -155,6 +183,7 @@ class Template(metaclass=PoolMeta): product_to_save = [] bom_to_save = [] output_to_save = [] + inputs = [] for bulk_product in products: for package_product in bulk_product.packaging_products: @@ -214,17 +243,30 @@ class Template(metaclass=PoolMeta): product=bulk_product.products[0].id, uom=bulk_product.default_uom, quantity=netweight) + inputs.append(bulk_input) package_input = BOMInput( bom=bom, product=package_product.packaging_product.id, uom=package_product.packaging_product.default_uom, quantity=1.0) + inputs.append(package_input) + + if bulk_product.extra_products: + for extra in bulk_product.extra_products: + extra_input = BOMInput( + bom=bom, + product=extra.extra_product.products[0].id, + uom=extra.extra_product.default_uom, + quantity=extra.quantity) + inputs.append(extra_input) + output = BOMOutput( bom=bom, product=output_product.id, uom=uom_unit, quantity=1.0) - bom.inputs = [bulk_input, package_input] + + bom.inputs = inputs bom.outputs = [output] bom_to_save.append(bom) diff --git a/product.xml b/product.xml index b99c5f1..9134ebb 100644 --- a/product.xml +++ b/product.xml @@ -73,8 +73,12 @@ product.template-product.packaging tree - product_packaging_list + + product.template-extra.product + tree + product_extra_packaging_list + diff --git a/view/product_extra_packaging_list.xml b/view/product_extra_packaging_list.xml new file mode 100644 index 0000000..7f71dfc --- /dev/null +++ b/view/product_extra_packaging_list.xml @@ -0,0 +1,7 @@ + + + + + + diff --git a/view/template_form.xml b/view/template_form.xml index 722135e..f4143cc 100644 --- a/view/template_form.xml +++ b/view/template_form.xml @@ -29,7 +29,9 @@ - + + +