Skip to content

Commit ca102d1

Browse files
committed
[ADD] mrp_llc: fix mrp calculation by use of low level code
1 parent 750b7c7 commit ca102d1

6 files changed

Lines changed: 293 additions & 0 deletions

File tree

mrp_llc/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from . import mrp
4+
from . import procurement
5+
from . import product
6+
from . import stock

mrp_llc/__openerp__.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
{
25+
"name": "MRP LLC",
26+
"version": "0.1",
27+
"summary": "MRP Low Level Code",
28+
"category": "Manufacturing",
29+
"author": "scosist",
30+
"website": "http://www.github.com/asphaltzipper/azi-odoo-modules",
31+
'description': """
32+
MRP Calculation by low level code.
33+
""",
34+
"depends": ["mrp"],
35+
"data": [],
36+
"demo": [],
37+
"test": [],
38+
"js": [],
39+
"css": [],
40+
"installable": True,
41+
"auto_install": False,
42+
}

mrp_llc/mrp.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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:

mrp_llc/procurement.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
from openerp import models, api
25+
26+
27+
class procurement_order(models.Model):
28+
_inherit = 'procurement.order'
29+
30+
@api.model
31+
def _procure_orderpoint_confirm(self, use_new_cursor=False,
32+
company_id=False):
33+
bom_obj = self.env['mrp.bom']
34+
bom_obj.compute_llc()
35+
if use_new_cursor:
36+
self._cr.commit()
37+
return super(procurement_order, self)._procure_orderpoint_confirm(
38+
use_new_cursor, company_id)
39+
40+
41+
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

mrp_llc/product.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
from openerp import models, fields
25+
26+
27+
class product_product(models.Model):
28+
_inherit = "product.product"
29+
30+
low_level_code = fields.Integer('LLC', required=True, default=0)
31+
32+
33+
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

mrp_llc/stock.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
from openerp import models, fields
25+
26+
27+
class stock_warehouse_orderpoint(models.Model):
28+
_inherit = 'stock.warehouse.orderpoint'
29+
_order = 'product_llc'
30+
31+
product_llc = fields.Integer(string='Product LLC', store=True,
32+
related='product_id.low_level_code',
33+
readonly=True)
34+
35+
36+
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

0 commit comments

Comments
 (0)