~numerigraphe-team/stock-logistic-warehouse/7.0-inventory-location

« back to all changes in this revision

Viewing changes to stock_reord_rule/stock_reord_rule.py

  • Committer: sergiocorato at gmail
  • Date: 2013-02-17 22:02:37 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: sergiocorato@gmail.com-20130217220237-ucrrk03m10mni6h6
[ADD] Improved reordering rules on sales stats

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Automatic Stock Procurement by days for OpenERP
 
5
#    Copyright (C) 2012 Sergio Corato (<http://www.icstools.it>)
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
from osv import osv, fields
 
23
 
 
24
class stock_warehouse_orderpoint(osv.osv):
 
25
    _inherit = "stock.warehouse.orderpoint"
 
26
 
 
27
    def _qty_orderpoint_days(self, cr, uid, ids, context=None):
 
28
        """Calculate quantity to create warehouse stock for n days of sales.
 
29
        integer (( Qty sold in days_stats * (1+forecast_gap)) / days_stats * days_warehouse)"""
 
30
 
 
31
        res = {}
 
32
        obj_product = self.pool.get('product.product')
 
33
        product_ids = tuple(obj_product.search(cr, uid, []))
 
34
        if len(product_ids) > 1:
 
35
            sql= """SELECT sol.product_id AS product_id, (sum( product_uos_qty )/pp.days_stats*(1+pp.forecast_gap/100) * pp.days_warehouse) AS quantity FROM sale_order_line sol JOIN sale_order so ON so.id = sol.order_id JOIN product_product pp ON pp.id = sol.product_id WHERE sol.state in ('done','confirmed') AND sol.product_id IN {product_ids} AND date_order > (date(now()) - pp.days_stats) GROUP BY sol.product_uom, sol.product_id, pp.days_stats, pp.forecast_gap, pp.days_warehouse;""".format(product_ids=product_ids)
 
36
            cr.execute(sql)
 
37
            sql_res = cr.fetchall()
 
38
            for val in sql_res:
 
39
                if val:
 
40
                    reord_rules_ids = self.search(cr, uid, [('product_id','=', val[0])])
 
41
                    if reord_rules_ids:
 
42
                        res['product_max_qty'] = val[1]
 
43
                        self.write(cr, uid, reord_rules_ids, res)
 
44
        return True
 
45
 
 
46
stock_warehouse_orderpoint()
 
47
 
 
48
class product_product(osv.osv):
 
49
    _inherit = "product.product"
 
50
 
 
51
    _columns = {
 
52
        'days_warehouse': fields.integer('Nr of days of warehouse stock'),
 
53
        'days_stats':fields.integer('Nr of days on which calculate stats'),
 
54
        'forecast_gap':fields.float('Forecast gap%', digits=(6,int(3))),
 
55
        }
 
56
 
 
57
product_product()