From f7ffb713a41d37df829ef4d193a4c1386a6205bf Mon Sep 17 00:00:00 2001 From: Albert Cervera i Areny Date: Wed, 9 Apr 2014 02:00:05 +0200 Subject: [PATCH] Improve BOM creation wizard. --- plan.py | 98 +++++++++++++++------------- tests/scenario_product_cost_plan.rst | 1 - view/create_bom_start_form.xml | 3 +- 3 files changed, 55 insertions(+), 47 deletions(-) diff --git a/plan.py b/plan.py index bf8d272..46e2649 100644 --- a/plan.py +++ b/plan.py @@ -94,6 +94,11 @@ class Plan(Workflow, ModelSQL, ModelView): 'icon': 'tryton-go-previous', } }) + cls._error_messages.update({ + 'bom_already_exists': ('A bom already exists for cost plan ' + '"%s".'), + }) + @staticmethod def default_active(): @@ -355,6 +360,47 @@ class Plan(Workflow, ModelSQL, ModelView): CostLine.delete(to_delete) super(Plan, cls).delete(plans) + def create_bom(self, name): + BOM = Pool().get('production.bom') + if self.bom: + self.raise_user_error('bom_already_exists', self.rec_name) + bom = BOM() + bom.name = name + bom.inputs = self._get_bom_inputs() + bom.outputs = self._get_bom_outputs() + bom.save() + self.bom = bom + self.save() + return bom + + def _get_bom_outputs(self): + BOMOutput = Pool().get('production.bom.output') + outputs = [] + if self.product: + output = BOMOutput() + output.product = self.product + output.uom = self.product.default_uom + output.quantity = self.quantity + outputs.append(output) + return outputs + + def _get_bom_inputs(self): + inputs = [] + for line in self.products: + if not line.product: + continue + inputs.append(self._get_input_line(line)) + return inputs + + def _get_input_line(self, line): + 'Return the BOM Input line for a product line' + BOMInput = Pool().get('production.bom.input') + input_ = BOMInput() + input_.product = line.product + input_.uom = line.uom + input_.quantity = line.quantity + return input_ + class PlanBOM(ModelSQL, ModelView): 'Product Cost Plan BOM' @@ -522,7 +568,6 @@ class CreateBomStart(ModelView): __name__ = 'product.cost.plan.create_bom.start' name = fields.Char('Name', required=True) - inputs = fields.One2Many('production.bom.input', 'bom', 'Inputs') class CreateBom(Wizard): @@ -538,52 +583,17 @@ class CreateBom(Wizard): def default_start(self, fields): CostPlan = Pool().get('product.cost.plan') - inputs = [] plan = CostPlan(Transaction().context.get('active_id')) - for line in plan.products: - if not line.product: - continue - inputs.append(self._get_input_line(line)) - return {'inputs': inputs} + return { + 'name': plan.product.rec_name, + } def do_bom(self, action): - pool = Pool() - BOM = pool.get('production.bom') - CostPlan = pool.get('product.cost.plan') - + CostPlan = Pool().get('product.cost.plan') plan = CostPlan(Transaction().context.get('active_id')) - bom = BOM() - bom.name = self.start.name - bom.inputs = self.start.inputs - bom.outputs = self._get_bom_outputs() - bom.save() - plan.bom = bom - plan.save() - data = {'res_id': [bom.id]} + bom = plan.create_bom(self.start.name) + data = { + 'res_id': [bom.id] + } action['views'].reverse() return action, data - - def _get_input_line(self, line): - 'Return the BOM Input line for a product line' - BOMInput = Pool().get('production.bom.input') - res = BOMInput.default_get(BOMInput._fields.keys()) - res.update({ - 'product': line.product.id, - 'uom': line.uom.id, - 'quantity': line.quantity, - }) - return res - - def _get_bom_outputs(self): - pool = Pool() - CostPlan = pool.get('product.cost.plan') - - plan = CostPlan(Transaction().context.get('active_id')) - outputs = [] - if plan.product: - outputs.append({ - 'product': plan.product.id, - 'uom': plan.product.default_uom.id, - 'quantity': plan.quantity, - }) - return outputs diff --git a/tests/scenario_product_cost_plan.rst b/tests/scenario_product_cost_plan.rst index e610022..6d3d54a 100644 --- a/tests/scenario_product_cost_plan.rst +++ b/tests/scenario_product_cost_plan.rst @@ -217,7 +217,6 @@ Create a cost plan for product (without child boms):: >>> cost, = plan.costs >>> cost.rec_name == 'Raw materials' True - >>> cost.cost >>> cost.cost == Decimal('17.5') True >>> plan.cost_price == Decimal('17.5') diff --git a/view/create_bom_start_form.xml b/view/create_bom_start_form.xml index 4b80379..6bf6f5b 100644 --- a/view/create_bom_start_form.xml +++ b/view/create_bom_start_form.xml @@ -3,6 +3,5 @@ copyright notices and license terms. -->