From 63fb663bf3d3cc577c4a5325df66e8015206be63 Mon Sep 17 00:00:00 2001 From: John Walsh Date: Wed, 23 Sep 2015 12:27:50 -0700 Subject: [PATCH] [ADD] mrp_mto_with_stock - initial commit --- mrp_mto_with_stock/__init__.py | 18 ++++++ mrp_mto_with_stock/__openerp__.py | 47 +++++++++++++++ mrp_mto_with_stock/models/__init__.py | 19 ++++++ mrp_mto_with_stock/models/mrp.py | 86 +++++++++++++++++++++++++++ mrp_mto_with_stock/models/stock.py | 17 ++++++ 5 files changed, 187 insertions(+) create mode 100644 mrp_mto_with_stock/__init__.py create mode 100644 mrp_mto_with_stock/__openerp__.py create mode 100644 mrp_mto_with_stock/models/__init__.py create mode 100644 mrp_mto_with_stock/models/mrp.py create mode 100644 mrp_mto_with_stock/models/stock.py diff --git a/mrp_mto_with_stock/__init__.py b/mrp_mto_with_stock/__init__.py new file mode 100644 index 000000000..d0c2e2f37 --- /dev/null +++ b/mrp_mto_with_stock/__init__.py @@ -0,0 +1,18 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. +# +############################################################################## +import models diff --git a/mrp_mto_with_stock/__openerp__.py b/mrp_mto_with_stock/__openerp__.py new file mode 100644 index 000000000..5e408e726 --- /dev/null +++ b/mrp_mto_with_stock/__openerp__.py @@ -0,0 +1,47 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. +# +############################################################################## +{ + 'name': "mrp_mto_with_stock", + + 'summary': """ + Fix Manufacturing orders to pull from stock until qty is zero, + and then create a procurement for them""", + + 'description': """ + Long description of module's purpose + """, + + 'author': "John Walsh", + 'website': "http://github.com/michaeljohn32", + + # Categories can be used to filter modules in modules listing + # Check https://github.com/odoo/odoo/blob/master/openerp/addons/base/module/module_data.xml + # for the full list + 'category': 'Hidden/Dependency', + 'version': '0.1', + + # any module necessary for this one to work correctly + 'depends': ['mrp', 'stock_mts_mto_rule'], + + # always loaded + 'data': [ + ], + # only loaded in demonstration mode + 'demo': [ + ], +} diff --git a/mrp_mto_with_stock/models/__init__.py b/mrp_mto_with_stock/models/__init__.py new file mode 100644 index 000000000..e9ebf62ff --- /dev/null +++ b/mrp_mto_with_stock/models/__init__.py @@ -0,0 +1,19 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. +# +############################################################################## +import stock +import mrp diff --git a/mrp_mto_with_stock/models/mrp.py b/mrp_mto_with_stock/models/mrp.py new file mode 100644 index 000000000..cec522e6c --- /dev/null +++ b/mrp_mto_with_stock/models/mrp.py @@ -0,0 +1,86 @@ +# -*- encoding: utf-8 -*- +from openerp import fields, models, api +import pdb +import logging +_logger = logging.getLogger(__name__) + +class mrp_production(models.Model): + _inherit = 'mrp.production' + +# @api.model +# def _make_consume_line_from_data(self, production, product, uom_id, qty, uos_id, uos_qty): +# '''Confirms stock move or put it in waiting if it's linked to another move. +# @returns list of ids''' +# pdb.set_trace() +# # change the qty to make two moves (if needed) +# res = super(mrp_production, self)._make_consume_line_from_data(production, product, uom_id, uos_id, uos_qty) +# return res + @api.one + def action_confirm(self): + '''Confirms stock move or put it in waiting if it's linked to another move. + @returns list of ids''' +# pdb.set_trace() + # change the qty to make two moves (if needed) + res = super(mrp_production, self).action_confirm() + # try to assign moves (and generate procurements!) + self.action_assign() + return res + + @api.one + def action_assign(self): + '''Reserves available products to the production order + but also creates procurements for more items if we + cannot reserve enough (MTO with stock) + @returns list of ids''' + # reserve all that is available + res = super(mrp_production, self).action_assign() + mtos_route = self.env.ref('stock_mts_mto_rule.route_mto_mts') + for move in self.move_lines: + if move.state == 'confirmed' and mtos_route.id in move.product_id.route_ids.ids: + #This move is waiting availability + + #create a domain + #TODO: check other possible states confirmed/exception? + domain = [('product_id','=', move.product_id.id),('state','=','running'),('move_dest_id','=',move.id)] + if move.group_id: + domain.append(('group_id','=',move.group_id.id)) + procurement = self.env['procurement.order'].search(domain) + if not procurement: + # we need to create a procurement + qty_to_procure = move.remaining_qty - move.reserved_availability + proc_dict = self._prepare_mto_procurement(move, qty_to_procure) + procurement = self.env['procurement.order'].create(proc_dict) + return res + + def _prepare_mto_procurement(self, move, qty): + '''Prepares a procurement for a MTO move + using similar logic to /stock/stock.py/class stock_move/_prepare_procurement_from_move() + + ''' + origin = ((move.group_id and (move.group_id.name) + ":") or "") + ((move.name and move.name + ":") or "") + ('MTO -> Production') + group_id = move.group_id and move.group_id.id or False + + route_ids = [self.env.ref('stock.route_warehouse0_mto')] + return{ + 'name': move.name + ':' + str(move.id), + 'origin': origin, + 'company_id': move.company_id and move.company_id.id or False, + 'date_planned': move.date, + 'product_id': move.product_id.id, + 'product_qty': qty, + 'product_uom': move.product_uom.id, + 'product_uos_qty': qty, #FIXME: (move.product_uos and move.product_uos_qty) or move.product_uom_qty, + 'product_uos': move.product_uom.id, #FIXME:(move.product_uos and move.product_uos.id) or move.product_uom.id, + 'location_id': move.location_id.id, + 'move_dest_id': move.id, + 'group_id': group_id, + 'route_ids':[(4, x.id) for x in route_ids], + 'warehouse_id': move.warehouse_id.id or (move.picking_type_id and move.picking_type_id.warehouse_id.id or False), + 'priority': move.priority, + } + + + + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: + diff --git a/mrp_mto_with_stock/models/stock.py b/mrp_mto_with_stock/models/stock.py new file mode 100644 index 000000000..f67c7b522 --- /dev/null +++ b/mrp_mto_with_stock/models/stock.py @@ -0,0 +1,17 @@ +# -*- encoding: utf-8 -*- +from openerp import fields, models, api +import pdb + +class stock_move(models.Model): + _inherit = 'stock.move' + + @api.multi + def action_confirm(self): + '''Confirms stock move or put it in waiting if it's linked to another move. + @returns list of ids''' + res = super(stock_move, self).action_confirm() + return res + + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: +