|
| 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, api |
| 26 | +from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT |
| 27 | +from openerp.exceptions import ValidationError |
| 28 | +from openerp.tools.translate import _ |
| 29 | + |
| 30 | + |
| 31 | +class mrp_bom(models.Model): |
| 32 | + _inherit = ['mrp.bom'] |
| 33 | + |
| 34 | + @api.model |
| 35 | + def _bom_explode_llc(self, bom, product, result=None, properties=None, |
| 36 | + llc=1, previous_products=None, master_bom=None): |
| 37 | + master_bom = master_bom or bom |
| 38 | + result = result or {} |
| 39 | + |
| 40 | + for bom_line_id in bom.bom_line_ids: |
| 41 | + if (bom_line_id.date_start and bom_line_id.date_start > |
| 42 | + time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)) or ( |
| 43 | + bom_line_id.date_stop and bom_line_id.date_stop < |
| 44 | + time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)): |
| 45 | + continue |
| 46 | + # all bom_line_id variant values must be in the product |
| 47 | + if bom_line_id.attribute_value_ids: |
| 48 | + if not product or ( |
| 49 | + set(map(int, bom_line_id.attribute_value_ids or [])) - set( |
| 50 | + map(int, product.attribute_value_ids))): |
| 51 | + continue |
| 52 | + |
| 53 | + if ( |
| 54 | + previous_products and |
| 55 | + bom_line_id.product_id.product_tmpl_id.id in |
| 56 | + previous_products |
| 57 | + ): |
| 58 | + raise ValidationError( |
| 59 | + _('Invalid Action!'), |
| 60 | + _('Bom "%s" contains a BoM line with a product' |
| 61 | + ' recursion: "%s".' |
| 62 | + ) % (master_bom.name, |
| 63 | + bom_line_id.product_id.name_get()[0][1])) |
| 64 | + |
| 65 | + bom_id = self._bom_find(product_id=bom_line_id.product_id.id, |
| 66 | + properties=properties) |
| 67 | + |
| 68 | + if bom_id: |
| 69 | + all_prod = [bom.product_tmpl_id.id] + (previous_products or []) |
| 70 | + bom2 = self.browse(bom_id) |
| 71 | + if bom_line_id.type != "phantom" and bom2.type != "phantom": |
| 72 | + if result[bom_line_id.product_id.id] < llc: |
| 73 | + result[bom_line_id.product_id.id] = llc |
| 74 | + res = self._bom_explode_llc(bom2, bom_line_id.product_id, |
| 75 | + result, properties, llc + 1, |
| 76 | + all_prod, master_bom) |
| 77 | + result.update(res) |
| 78 | + else: |
| 79 | + res = self._bom_explode_llc(bom2, bom_line_id.product_id, |
| 80 | + result, properties, llc, |
| 81 | + all_prod, master_bom) |
| 82 | + result.update(res) |
| 83 | + elif bom_line_id.type != "phantom": |
| 84 | + if result[bom_line_id.product_id.id] < llc: |
| 85 | + result[bom_line_id.product_id.id] = llc |
| 86 | + else: |
| 87 | + raise ValidationError( |
| 88 | + _('Invalid Action!'), |
| 89 | + _('BoM "%s" contains a phantom BoM line but the product' |
| 90 | + ' "%s" does not have any BoM defined.' |
| 91 | + ) % (master_bom.name, |
| 92 | + bom_line_id.product_id.name_get()[0][1])) |
| 93 | + return result |
| 94 | + |
| 95 | + @api.model |
| 96 | + def _top_level_boms(self): |
| 97 | + bom_line_product_ids = [] |
| 98 | + top_level_boms = set() |
| 99 | + bom_ids = self.search([]) |
| 100 | + bom_line_obj = self.env['mrp.bom.line'] |
| 101 | + bom_line_ids = bom_line_obj.search([]) |
| 102 | + for bom_line in bom_line_ids: |
| 103 | + bom_line_product_ids.append(bom_line.product_id.id) |
| 104 | + for bom in bom_ids: |
| 105 | + bom_product_ids = [] |
| 106 | + if bom.product_id: |
| 107 | + bom_product_ids.append(bom.product_id.id) |
| 108 | + else: |
| 109 | + for variant in bom.product_tmpl_id.product_variant_ids: |
| 110 | + bom_product_ids.append(variant.id) |
| 111 | + # if any variants are in bom_line, do not add bom |
| 112 | + if not set(bom_product_ids).intersection(bom_line_product_ids): |
| 113 | + top_level_boms.add(bom.id) |
| 114 | + return top_level_boms |
| 115 | + |
| 116 | + @api.model |
| 117 | + def compute_llc(self, properties=None): |
| 118 | + llc_updates = {} |
| 119 | + # reset low level code first, then compute |
| 120 | + product_obj = self.env['product.product'] |
| 121 | + product_ids = product_obj.search([]) |
| 122 | + for product in product_ids: |
| 123 | + llc_updates[product.id] = 0 |
| 124 | + top_level_boms = self._top_level_boms() |
| 125 | + for bom_point in self.browse(top_level_boms): |
| 126 | + res = self._bom_explode_llc(bom_point, bom_point.product_id, |
| 127 | + llc_updates, properties) |
| 128 | + for (key, val) in res.items(): |
| 129 | + if val > llc_updates[key]: |
| 130 | + llc_updates[key] = val |
| 131 | + for (key, val) in llc_updates.items(): |
| 132 | + product_obj.browse([key]).write({'low_level_code': val}) |
| 133 | + |
| 134 | + |
| 135 | +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
0 commit comments