From 5ddcbde68dfa937f438ee807cd46bed5c23edb90 Mon Sep 17 00:00:00 2001 From: oihane Date: Wed, 9 Dec 2015 13:56:46 +0100 Subject: [PATCH 1/2] [FIX] mrp_bom_reference_selection: Fixed onchange error closes #62 --- .../models/mrp_bom_line.py | 36 +++++++++---------- .../views/mrp_bom_view.xml | 3 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/mrp_bom_reference_selection/models/mrp_bom_line.py b/mrp_bom_reference_selection/models/mrp_bom_line.py index 5f2c73a35..a39c20dd8 100644 --- a/mrp_bom_reference_selection/models/mrp_bom_line.py +++ b/mrp_bom_reference_selection/models/mrp_bom_line.py @@ -35,10 +35,11 @@ class MrpBillOfMaterialLine(models.Model): 'product.product', required=True, string='Product') @api.multi - def onchange_product_id(self, product_id): + def onchange_product_id(self, product_id, product_qty=0): product = self.env['product.product'].browse(product_id) - res = {'value': {}} + res = super(MrpBillOfMaterialLine, self).onchange_product_id( + product_id, product_qty=product_qty) if not product: res['value']['reference_id'] = False @@ -49,23 +50,22 @@ class MrpBillOfMaterialLine(models.Model): return res - @api.one @api.depends('product_id') - def _get_child_bom_lines(self): - if self.reference_id: - self.child_line_ids = self.reference_id.bom_id.bom_line_ids.ids - else: - bom_obj = self.env['mrp.bom'] - bom_id = bom_obj._bom_find( - product_tmpl_id=self.product_id.product_tmpl_id.id, - product_id=self.product_id.id) - self.child_line_ids = bom_id and [ - (6, 0, child_id) for child_id in - bom_obj.browse(bom_id).bom_line_ids.ids - ] or False + def _compute_child_bom_lines(self): + for record in self: + if record.reference_id: + record.child_line_ids = record.reference_id.bom_id.bom_line_ids + else: + bom_obj = self.env['mrp.bom'] + bom_id = bom_obj._bom_find( + product_tmpl_id=record.product_id.product_tmpl_id.id, + product_id=record.product_id.id) + record.child_line_ids = bom_id and [ + (6, 0, child_id) for child_id in + bom_obj.browse(bom_id).bom_line_ids.ids + ] or False child_line_ids = fields.One2many( - relation='mrp.bom.line', - compute='_get_child_bom_lines', - string="BOM lines of the referred bom", + relation='mrp.bom.line', compute='_compute_child_bom_lines', + string="BoM lines of the referred BoM", ) diff --git a/mrp_bom_reference_selection/views/mrp_bom_view.xml b/mrp_bom_reference_selection/views/mrp_bom_view.xml index 25e408cef..df085c006 100644 --- a/mrp_bom_reference_selection/views/mrp_bom_view.xml +++ b/mrp_bom_reference_selection/views/mrp_bom_view.xml @@ -19,8 +19,7 @@ mrp.bom - - + \n" -"Language-Team: \n" -"MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=UTF-8\n" -"Content-Transfer-Encoding: \n" -"Plural-Forms: \n" - -#. module: mrp_bom_reference_selection -#: model:ir.model,name:mrp_bom_reference_selection.model_mrp_bom -#: field:mrp.bom.reference,bom_id:0 -msgid "Bill of Material" -msgstr "" - -#. module: mrp_bom_reference_selection -#: field:mrp.bom,reference_id:0 -msgid "BoM Reference" -msgstr "" - -#. module: mrp_bom_reference_selection -#: field:mrp.bom.reference,create_uid:0 -msgid "Created by" -msgstr "" - -#. module: mrp_bom_reference_selection -#: field:mrp.bom.reference,create_date:0 -msgid "Created on" -msgstr "" - -#. module: mrp_bom_reference_selection -#: field:mrp.bom.reference,id:0 -msgid "ID" -msgstr "" - -#. module: mrp_bom_reference_selection -#: field:mrp.bom.reference,write_uid:0 -msgid "Last Updated by" -msgstr "" - -#. module: mrp_bom_reference_selection -#: field:mrp.bom.reference,write_date:0 -msgid "Last Updated on" -msgstr "" - -#. module: mrp_bom_reference_selection -#: model:ir.model,name:mrp_bom_reference_selection.model_mrp_bom_reference -msgid "MRP Bill of Material Reference" -msgstr "" - -#. module: mrp_bom_reference_selection -#: field:mrp.bom.line,product_tmpl_id:0 -msgid "Product Template" -msgstr "" - -#. module: mrp_bom_reference_selection -#: field:mrp.bom.reference,name:0 -msgid "Reference" -msgstr "" - diff --git a/mrp_bom_reference_selection/models/mrp_bom.py b/mrp_bom_reference_selection/models/mrp_bom.py index 550fa197a..a675df9f1 100644 --- a/mrp_bom_reference_selection/models/mrp_bom.py +++ b/mrp_bom_reference_selection/models/mrp_bom.py @@ -1,26 +1,8 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2015 Savoir-faire Linux -# (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# -*- coding: utf-8 -*- +# (c) 2015 Savoir-faire Linux - +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html -from openerp import models, fields, api +from openerp import api, fields, models class MrpBillOfMaterial(models.Model): @@ -34,4 +16,5 @@ class MrpBillOfMaterial(models.Model): return res reference_id = fields.One2many( - 'mrp.bom.reference', 'bom_id', string="BoM Reference") + comodel_name='mrp.bom.reference', inverse_name='bom_id', + string="BoM Reference") diff --git a/mrp_bom_reference_selection/models/mrp_bom_line.py b/mrp_bom_reference_selection/models/mrp_bom_line.py index a39c20dd8..88510f89e 100644 --- a/mrp_bom_reference_selection/models/mrp_bom_line.py +++ b/mrp_bom_reference_selection/models/mrp_bom_line.py @@ -1,32 +1,15 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2015 Savoir-faire Linux -# (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# -*- coding: utf-8 -*- +# (c) 2015 Savoir-faire Linux - +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html -from openerp import models, fields, api +from openerp import api, exceptions, fields, models, _ class MrpBillOfMaterialLine(models.Model): _inherit = 'mrp.bom.line' - reference_id = fields.Many2one('mrp.bom.reference', 'Ref') + reference_id = fields.Many2one( + comodel_name='mrp.bom.reference', string='Ref') product_tmpl_id = fields.Many2one( 'product.template', 'Product Template', related='product_id.product_tmpl_id', store=True) @@ -34,20 +17,35 @@ class MrpBillOfMaterialLine(models.Model): product_id = fields.Many2one( 'product.product', required=True, string='Product') + @api.constrains('reference_id') + def _check_reference_id(self): + if (self.reference_id and + (self.reference_id.bom_id.product_tmpl_id != + self.product_id.product_tmpl_id)): + raise exceptions.Warning( + _('Product %s from %s reference BoM must be equal to product' + ' %s in BoM line.' % + (self.reference_id.bom_id.product_tmpl_id.name, + self.reference_id.name, + self.product_id.product_tmpl_id.name))) + @api.multi def onchange_product_id(self, product_id, product_qty=0): - product = self.env['product.product'].browse(product_id) - res = super(MrpBillOfMaterialLine, self).onchange_product_id( product_id, product_qty=product_qty) - - if not product: - res['value']['reference_id'] = False - else: + if 'value' not in res: + res['value'] = {} + if 'domain' not in res: + res['domain'] = {} + if product_id: + product = self.env['product.product'].browse(product_id) refs = self.env['mrp.bom.reference'].search( [('bom_id.product_tmpl_id', '=', product.product_tmpl_id.id)]) - res['value']['reference_id'] = refs and refs[0] - + res['value']['reference_id'] = refs[:1] + res['domain']['reference_id'] = [('id', 'in', refs.ids)] + else: + res['value']['reference_id'] = False + res['domain']['reference_id'] = False return res @api.depends('product_id') diff --git a/mrp_bom_reference_selection/models/mrp_bom_reference.py b/mrp_bom_reference_selection/models/mrp_bom_reference.py index 8059c25f9..be9b9df59 100644 --- a/mrp_bom_reference_selection/models/mrp_bom_reference.py +++ b/mrp_bom_reference_selection/models/mrp_bom_reference.py @@ -1,34 +1,15 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2015 Savoir-faire Linux -# (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# -*- coding: utf-8 -*- +# (c) 2015 Savoir-faire Linux - +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html -from openerp import models, fields +from openerp import fields, models class MrpBillOfMaterialReference(models.Model): - _name = 'mrp.bom.reference' _description = 'MRP Bill of Material Reference' bom_id = fields.Many2one( - 'mrp.bom', required=True, ondelete='cascade', + comodel_name='mrp.bom', required=True, ondelete='cascade', string='Bill of Material') name = fields.Char(related='bom_id.code', store=True) diff --git a/mrp_bom_reference_selection/models/mrp_production.py b/mrp_bom_reference_selection/models/mrp_production.py index 931a7cd73..39e4469ad 100644 --- a/mrp_bom_reference_selection/models/mrp_production.py +++ b/mrp_bom_reference_selection/models/mrp_production.py @@ -1,29 +1,8 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2015 Savoir-faire Linux -# (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# -*- coding: utf-8 -*- +# (c) 2015 Savoir-faire Linux - +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html -import logging -from openerp import models, api - -_logger = logging.getLogger(__name__) +from openerp import api, models class MrpProduction(models.Model): @@ -37,11 +16,7 @@ class MrpProduction(models.Model): """ res = super(MrpProduction, self).action_produce( production_id, production_qty, production_mode, wiz) - production = self.browse(production_id) - - for move in production.move_created_ids2: - prod_lots = move.lot_ids - prod_lots.write({'bom_id': production.bom_id.id}) - + prod_lots = production.mapped('move_created_ids2.lot_ids') + prod_lots.write({'bom_id': production.bom_id.id}) return res diff --git a/mrp_bom_reference_selection/models/stock_production_lot.py b/mrp_bom_reference_selection/models/stock_production_lot.py index a1c38988e..e3771314a 100644 --- a/mrp_bom_reference_selection/models/stock_production_lot.py +++ b/mrp_bom_reference_selection/models/stock_production_lot.py @@ -1,29 +1,12 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2015 Savoir-faire Linux -# (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# -*- coding: utf-8 -*- +# (c) 2015 Savoir-faire Linux - +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html -from openerp import models, fields +from openerp import fields, models class StockProductionLot(models.Model): _inherit = 'stock.production.lot' - bom_id = fields.Many2one('mrp.bom', 'Bill of Material') + bom_id = fields.Many2one( + comodel_name='mrp.bom', string='Bill of Material') diff --git a/mrp_bom_reference_selection/tests/test_mrp_bom.py b/mrp_bom_reference_selection/tests/test_mrp_bom.py index 1cee04866..bc73a50f2 100644 --- a/mrp_bom_reference_selection/tests/test_mrp_bom.py +++ b/mrp_bom_reference_selection/tests/test_mrp_bom.py @@ -1,24 +1,6 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2015 Savoir-faire Linux -# (). -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# -*- coding: utf-8 -*- +# (c) 2015 Savoir-faire Linux - +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html from openerp.tests import common