|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# © 2016 Cyril Gaudin (Camptocamp) |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 4 | + |
| 5 | +from openerp import _, api, exceptions, fields, models |
| 6 | + |
| 7 | + |
| 8 | +class MrpBom(models.Model): |
| 9 | + _inherit = 'mrp.bom' |
| 10 | + |
| 11 | + dismantling = fields.Boolean(string='Dismantling', default=False) |
| 12 | + dismantled_product_id = fields.Many2one( |
| 13 | + comodel_name='product.product', |
| 14 | + string='Dismantled product' |
| 15 | + ) |
| 16 | + |
| 17 | + _sql_constraints = [ |
| 18 | + ('bom_dismantled_product_id', |
| 19 | + 'CHECK(dismantled_product_id is not null = dismantling)', |
| 20 | + "Dismantling BoM should have a dismantled product."), |
| 21 | + ] |
| 22 | + |
| 23 | + @api.multi |
| 24 | + def create_mrp_production(self): |
| 25 | + """ Create a manufacturing order from this BoM |
| 26 | + """ |
| 27 | + self.ensure_one() |
| 28 | + |
| 29 | + product = self._get_bom_product() |
| 30 | + |
| 31 | + production = self.env['mrp.production'].create({ |
| 32 | + 'bom_id': self.id, |
| 33 | + 'product_id': product.id, |
| 34 | + 'product_qty': self.product_qty, |
| 35 | + 'product_uom': self.product_uom.id, |
| 36 | + }) |
| 37 | + |
| 38 | + return self._get_form_view('mrp.production', production) |
| 39 | + |
| 40 | + @api.multi |
| 41 | + def create_dismantling_bom(self): |
| 42 | + """ Create a dismantling BoM based on this BoM |
| 43 | + """ |
| 44 | + self.ensure_one() |
| 45 | + |
| 46 | + self._check_bom_validity(check_dismantling=True) |
| 47 | + |
| 48 | + product = self._get_bom_product() |
| 49 | + components = self._get_components_tuples() |
| 50 | + |
| 51 | + # Create the BoM on first component (sorted by Id) |
| 52 | + first_component, first_component_needs = components.pop(0) |
| 53 | + dismantling_bom = self.create({ |
| 54 | + 'product_tmpl_id': first_component.product_tmpl_id.id, |
| 55 | + 'product_id': first_component.id, |
| 56 | + 'dismantling': True, |
| 57 | + 'dismantled_product_id': product.id, |
| 58 | + 'product_qty': first_component_needs, |
| 59 | + }) |
| 60 | + |
| 61 | + # Create BoM line for self.product_tmpl_id |
| 62 | + self.env['mrp.bom.line'].create({ |
| 63 | + 'bom_id': dismantling_bom.id, |
| 64 | + 'product_id': product.id, |
| 65 | + 'product_qty': self.product_qty, |
| 66 | + 'product_uom': self.product_uom.id, |
| 67 | + }) |
| 68 | + |
| 69 | + # Add others component as By-products |
| 70 | + subproduct_model = self.env['mrp.subproduct'] |
| 71 | + for component, needs in components: |
| 72 | + subproduct_model.create({ |
| 73 | + 'bom_id': dismantling_bom.id, |
| 74 | + 'product_id': component.id, |
| 75 | + 'product_qty': needs, |
| 76 | + 'product_uom': self.env.ref('product.product_uom_unit').id, |
| 77 | + }) |
| 78 | + |
| 79 | + return self._get_form_view('mrp.bom', dismantling_bom) |
| 80 | + |
| 81 | + def _get_form_view(self, model_name, entity): |
| 82 | + return { |
| 83 | + 'type': 'ir.actions.act_window', |
| 84 | + 'view_type': 'form', |
| 85 | + 'view_mode': 'form', |
| 86 | + 'res_model': model_name, |
| 87 | + 'target': 'current', |
| 88 | + 'res_id': entity.id, |
| 89 | + 'context': self.env.context |
| 90 | + } |
| 91 | + |
| 92 | + def _check_bom_validity(self, check_dismantling=False): |
| 93 | + """ Ensure this BoM can be use for creating a dismantling BoM |
| 94 | + or a manufacturing order. |
| 95 | +
|
| 96 | + :type check_dismantling: bool |
| 97 | + :raise exceptions.UserError: If this BoM is not valid. |
| 98 | + """ |
| 99 | + warning = None |
| 100 | + if check_dismantling and self.dismantling: |
| 101 | + warning = 'This BoM is already a dismantling Bom.' |
| 102 | + |
| 103 | + if not len(self.bom_line_ids): |
| 104 | + warning = 'This BoM does not have components.' |
| 105 | + |
| 106 | + if not self.product_id \ |
| 107 | + and len(self.product_tmpl_id.product_variant_ids) > 1: |
| 108 | + warning = 'This product has several variants: ' \ |
| 109 | + 'you need to specify one.' |
| 110 | + |
| 111 | + if warning: |
| 112 | + raise exceptions.UserError(_(warning)) |
| 113 | + |
| 114 | + def _get_components_tuples(self): |
| 115 | + """ Return this BoM components and their needed qties |
| 116 | + sorted by component id. |
| 117 | +
|
| 118 | + The result is like [(component_1, 1), (component_2, 5), ...] |
| 119 | +
|
| 120 | + :rtype: list of tuple |
| 121 | + """ |
| 122 | + components = self.product_id._get_components_needs( |
| 123 | + product=self.product_id, bom=self |
| 124 | + ) |
| 125 | + components = sorted(components.items(), key=lambda t: t[0].id) |
| 126 | + return components |
| 127 | + |
| 128 | + def _get_bom_product(self): |
| 129 | + """ Get the product of this BoM. |
| 130 | +
|
| 131 | + If BoM does not have product_id, return first template variant. |
| 132 | +
|
| 133 | + :rtype: product_product |
| 134 | + """ |
| 135 | + if not self.product_id: |
| 136 | + product = self.product_tmpl_id.product_variant_ids[0] |
| 137 | + else: |
| 138 | + product = self.product_id |
| 139 | + return product |
0 commit comments