diff --git a/__init__.py b/__init__.py index bb2a007..ea4e609 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,5 @@ from trytond.pool import Pool -from . import product, sale +from . import product, sale, production __all__ = ['register'] @@ -9,6 +9,7 @@ def register(): product.Product, sale.Sale, sale.Line, + production.Production, module='sale_fast_food', type_='model') Pool.register( module='sale_fast_food', type_='wizard') diff --git a/product.py b/product.py index 4713893..b89832e 100644 --- a/product.py +++ b/product.py @@ -1,5 +1,6 @@ from trytond.pool import Pool, PoolMeta from trytond.model import fields +from trytond.pyson import Eval, Bool class Product(metaclass=PoolMeta): @@ -8,4 +9,5 @@ class Product(metaclass=PoolMeta): pizza = fields.Boolean("Pizza") pizza_topping = fields.Boolean("Topping Pizza") - boms = fields.Many2One('production.bom', "Boms") + boms = fields.Many2One('production.bom', "Boms", + states={'required': Eval('producible', True)}) diff --git a/production.py b/production.py new file mode 100644 index 0000000..22ff005 --- /dev/null +++ b/production.py @@ -0,0 +1,63 @@ +# This file is part of Tryton. The COPYRIGHT file at the top level of +# this repository contains the full copyright notices and license terms. +from decimal import Decimal + +from trytond.pool import Pool, PoolMeta +from trytond.model import fields +from trytond.modules.product import price_digits, round_price + +from trytond.exceptions import UserError + + +BOM_CHANGES = ['bom', 'product', 'quantity', 'uom', 'warehouse', 'location', + 'company', 'inputs', 'outputs'] + +class Production(metaclass=PoolMeta): + "Production" + __name__ = 'production' + + + @fields.depends(*BOM_CHANGES) + def explode_bom(self): + pool = Pool() + Uom = pool.get('product.uom') + if not (self.bom and self.product and self.uom): + return + + factor = self.bom.compute_factor(self.product, self.quantity or 0, + self.uom) + + inputs = [] + for input_ in self.bom.inputs: + if input_.product.producible: + for input_ in input_.product.template.boms.inputs: + quantity = input_.compute_quantity(factor) + move = self._explode_move_values( + self.picking_location, self.location, self.company, + input_, quantity) + if move: + inputs.append(move) + quantity = Uom.compute_qty(input_.uom, quantity, + input_.product.default_uom, round=False) + else: + raise UserError(str('problemas')) + quantity = input_.compute_quantity(factor) + move = self._explode_move_values( + self.picking_location, self.location, self.company, + input_, quantity) + if move: + inputs.append(move) + quantity = Uom.compute_qty(input_.uom, quantity, + input_.product.default_uom, round=False) + self.inputs = inputs + + outputs = [] + for output in self.bom.outputs: + quantity = output.compute_quantity(factor) + move = self._explode_move_values( + self.location, self.output_location, self.company, output, + quantity) + if move: + move.unit_price = Decimal(0) + outputs.append(move) + self.outputs = outputs diff --git a/sale.py b/sale.py index ccb20c7..8c78a2b 100644 --- a/sale.py +++ b/sale.py @@ -44,3 +44,10 @@ class Line(metaclass=PoolMeta): super(Line, self).on_change_product() if self.product.pizza: self.pizza = self.sale.pizza_number + + def get_production(self): + "Return production for the sale line" + Production = super(Line, self).get_production() + #Production.bom = self.product.template.boms.id + + return Production