|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +############################################################################## |
| 3 | +# |
| 4 | +# OpenERP Module |
| 5 | +# |
| 6 | +# Copyright (C) 2014 Asphalt Zipper, Inc. |
| 7 | +# Author scosist |
| 8 | +# |
| 9 | +# This program is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU Affero General Public License as |
| 11 | +# published by the Free Software Foundation, either version 3 of the |
| 12 | +# License, or (at your option) any later version. |
| 13 | +# |
| 14 | +# This program is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU Affero General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU Affero General Public License |
| 20 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +# |
| 22 | +############################################################################# |
| 23 | + |
| 24 | +import time |
| 25 | +from openerp import models, fields, api |
| 26 | +from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT |
| 27 | +from openerp.exceptions import ValidationError |
| 28 | +from openerp.tools.translate import _ |
| 29 | + |
| 30 | +class mrp_bom(models.Model): |
| 31 | + _inherit = ['mrp.bom'] |
| 32 | + |
| 33 | + @api.model |
| 34 | + def _bom_explode_llc(self, bom, product, result=None, properties=None, llc=1, previous_products=None, master_bom=None): |
| 35 | + master_bom = master_bom or bom |
| 36 | + result = result or {} |
| 37 | + |
| 38 | + for bom_line_id in bom.bom_line_ids: |
| 39 | + if bom_line_id.date_start and bom_line_id.date_start > time.strftime(DEFAULT_SERVER_DATETIME_FORMAT) or \ |
| 40 | + bom_line_id.date_stop and bom_line_id.date_stop < time.strftime(DEFAULT_SERVER_DATETIME_FORMAT): |
| 41 | + continue |
| 42 | + # all bom_line_id variant values must be in the product |
| 43 | + if bom_line_id.attribute_value_ids: |
| 44 | + if not product or (set(map(int,bom_line_id.attribute_value_ids or [])) - set(map(int,product.attribute_value_ids))): |
| 45 | + continue |
| 46 | + |
| 47 | + if previous_products and bom_line_id.product_id.product_tmpl_id.id in previous_products: |
| 48 | + raise ValidationError(_('Invalid Action!'), _('Bom "%s" contains a BoM line with a product recursion: "%s".') % (master_bom.name,bom_line_id.product_id.name_get()[0][1])) |
| 49 | + |
| 50 | + bom_id = self._bom_find(product_id=bom_line_id.product_id.id, properties=properties) |
| 51 | + |
| 52 | + if bom_id: |
| 53 | + all_prod = [bom.product_tmpl_id.id] + (previous_products or []) |
| 54 | + bom2 = self.browse(bom_id) |
| 55 | + if bom_line_id.type != "phantom" and bom2.type != "phantom": |
| 56 | + if result[bom_line_id.product_id.id] < llc: |
| 57 | + result[bom_line_id.product_id.id] = llc |
| 58 | + res = self._bom_explode_llc(bom2, bom_line_id.product_id, result, properties, llc + 1, all_prod, master_bom) |
| 59 | + result.update(res) |
| 60 | + else: |
| 61 | + res = self._bom_explode_llc(bom2, bom_line_id.product_id, result, properties, llc, all_prod, master_bom) |
| 62 | + result.update(res) |
| 63 | + elif bom_line_id.type != "phantom": |
| 64 | + if result[bom_line_id.product_id.id] < llc: |
| 65 | + result[bom_line_id.product_id.id] = llc |
| 66 | + else: |
| 67 | + raise ValidationError(_('Invalid Action!'), _('BoM "%s" contains a phantom BoM line but the product "%s" does not have any BoM defined.') % (master_bom.name,bom_line_id.product_id.name_get()[0][1])) |
| 68 | + return result |
| 69 | + |
| 70 | + @api.model |
| 71 | + def _top_level_boms(self): |
| 72 | + bom_line_product_ids = [] |
| 73 | + top_level_boms = set() |
| 74 | + bom_ids = self.search([]) |
| 75 | + bom_line_obj = self.env['mrp.bom.line'] |
| 76 | + bom_line_ids = bom_line_obj.search([]) |
| 77 | + for bom_line in bom_line_ids: |
| 78 | + bom_line_product_ids.append(bom_line.product_id.id) |
| 79 | + for bom in bom_ids: |
| 80 | + bom_product_ids = [] |
| 81 | + if bom.product_id: |
| 82 | + bom_product_ids.append(bom.product_id.id) |
| 83 | + else: |
| 84 | + for variant in bom.product_tmpl_id.product_variant_ids: |
| 85 | + bom_product_ids.append(variant.id) |
| 86 | + #if any variants are in bom_line, do not add bom |
| 87 | + if not set(bom_product_ids).intersection(bom_line_product_ids): |
| 88 | + top_level_boms.add(bom.id) |
| 89 | + return top_level_boms |
| 90 | + |
| 91 | + @api.model |
| 92 | + def compute_llc(self, properties=None): |
| 93 | + llc_updates = {} |
| 94 | + # reset low level code first, then compute |
| 95 | + product_obj = self.env['product.product'] |
| 96 | + product_ids = product_obj.search([]) |
| 97 | + for product in product_ids: |
| 98 | + llc_updates[product.id] = 0 |
| 99 | + top_level_boms = self._top_level_boms() |
| 100 | + for bom_point in self.browse(top_level_boms): |
| 101 | + res = self._bom_explode_llc(bom_point, bom_point.product_id, llc_updates, properties) |
| 102 | + for (k,v) in res.items(): |
| 103 | + if v > llc_updates[k]: |
| 104 | + llc_updates[k] = v |
| 105 | + for (k,v) in llc_updates.items(): |
| 106 | + product_obj.browse([k]).write({'low_level_code': v}) |
| 107 | + |
| 108 | + |
| 109 | +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
0 commit comments