From b8999084c5dd9aa194d7228638f2918f2d3b2a65 Mon Sep 17 00:00:00 2001 From: florian-dacosta Date: Wed, 22 Apr 2015 20:28:48 +0200 Subject: [PATCH] ADD stock_dynamic_mto --- stock_dynamic_mto/__init__.py | 1 + stock_dynamic_mto/__openerp__.py | 42 +++++++++++++++ stock_dynamic_mto/procurement.py | 80 ++++++++++++++++++++++++++++ stock_dynamic_mto/pull_rule_view.xml | 18 +++++++ 4 files changed, 141 insertions(+) create mode 100644 stock_dynamic_mto/__init__.py create mode 100644 stock_dynamic_mto/__openerp__.py create mode 100644 stock_dynamic_mto/procurement.py create mode 100644 stock_dynamic_mto/pull_rule_view.xml diff --git a/stock_dynamic_mto/__init__.py b/stock_dynamic_mto/__init__.py new file mode 100644 index 000000000..2cac6b055 --- /dev/null +++ b/stock_dynamic_mto/__init__.py @@ -0,0 +1 @@ +from . import procurement diff --git a/stock_dynamic_mto/__openerp__.py b/stock_dynamic_mto/__openerp__.py new file mode 100644 index 000000000..bb542d353 --- /dev/null +++ b/stock_dynamic_mto/__openerp__.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# Module for OpenERP +# Copyright (C) 2015 Akretion (http://www.akretion.com). All Rights Reserved +# @author Florian DA COSTA +# +# 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################### +{ + "name": "Stock Dynamic MTO", + "version": "1.0", + "category": "Stock", + "description": """ +Stock Dynamic MTO +====================== +The purpose of the module is to give the possibility to a pull rule +make to order or make to stock depending of the virtual stock of a product. +""", + "license": "AGPL-3", + "author": "Akretion,Odoo Community Association (OCA)", + "website": "http://www.akretion.com/", + "depends": [ + "stock", + ], + "data": [ + "pull_rule_view.xml", + ], + "installable": True, +} diff --git a/stock_dynamic_mto/procurement.py b/stock_dynamic_mto/procurement.py new file mode 100644 index 000000000..a1d44c9c9 --- /dev/null +++ b/stock_dynamic_mto/procurement.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# Module for OpenERP +# Copyright (C) 2015 Akretion (http://www.akretion.com). All Rights Reserved +# @author BenoƮt GUILLOT +# +# 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################### +from openerp import models, fields, api + + +class ProcurementRule(models.Model): + _inherit = 'procurement.rule' + + mts_pull_rule_id = fields.Many2one('procurement.rule', + string="MTS Rule") + + +class ProcurementOrder(models.Model): + _inherit = 'procurement.order' + + @api.multi + def get_mto_qty_to_order(self): + self.ensure_one() + uom_obj = self.env['product.uom'] + proc_warehouse = self.with_context(warehouse=self.warehouse_id.id) + virtual_available = proc_warehouse.product_id.virtual_available + qty_available = uom_obj._compute_qty(self.product_id.uom_id.id, + virtual_available, + self.product_uom.id) + if qty_available > 0: + if qty_available >= self.product_qty: + return 0.0 + else: + return self.product_qty - qty_available + return self.product_qty + + @api.model + def _run(self, procurement): + uom_obj = self.env['product.uom'] + rule_id = procurement.rule_id + rule_mts = rule_id and rule_id.mts_pull_rule_id or False + if rule_mts: + needed_qty = procurement.get_mto_qty_to_order() + if needed_qty == 0.0: + procurement.write({'rule_id': rule_mts.id}) + return super(ProcurementOrder, self)._run(procurement) + else: + if needed_qty != procurement.product_qty: + mts_qty = procurement.product_qty - needed_qty + mts_uos_qty = uom_obj._compute_qty( + procurement.product_uom.id, + mts_qty, + procurement.product_uos.id) + default_vals = { + 'product_qty': mts_qty, + 'product_uos_qty': mts_uos_qty, + } + uos_qty = procurement.product_uos_qty + update_vals = { + 'product_qty': needed_qty, + 'product_uos_qty': uos_qty - mts_uos_qty, + } + mts_proc = procurement.copy(default=default_vals) + mts_proc.run() + procurement.write(update_vals) + return super(ProcurementOrder, self)._run(procurement) diff --git a/stock_dynamic_mto/pull_rule_view.xml b/stock_dynamic_mto/pull_rule_view.xml new file mode 100644 index 000000000..43f252a93 --- /dev/null +++ b/stock_dynamic_mto/pull_rule_view.xml @@ -0,0 +1,18 @@ + + + + + procurement.rule.dynamic.mto + procurement.rule + + + + + + + + + +