9f73cf257ebb94fba281dce2af16c3cfd4b98245
[odoo/odoo.git] / addons / stock_planning / wizard / stock_planning_forecast.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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 from tools.translate import _
24
25 # Creates forecasts records for products from selected Product Category for selected 'Warehouse - Period'
26 # Object added by contributor in ver 1.1
27 class stock_sale_forecast_createlines(osv.osv_memory):
28     _name = "stock.sale.forecast.createlines"
29     _description = "stock.sale.forecast.createlines"
30
31
32     _columns = {
33         'company_id': fields.many2one('res.company', 'Company', required=True, select=1),
34         'warehouse_id': fields.many2one('stock.warehouse' , 'Warehouse', required=True, \
35                                 help='Warehouse which forecasts will concern. '\
36                                    'If during stock planning you will need sales forecast for all warehouses choose any warehouse now.'),
37         'period_id': fields.many2one('stock.period', 'Period', required=True, help='Period which forecasts will concern.'),
38         'product_categ_id': fields.many2one('product.category' , 'Product Category', required=True, \
39                                 help ='Product Category of products which created forecasts will concern.'),
40         'copy_forecast': fields.boolean('Copy Last Forecast', help="Copy quantities from last Stock and Sale Forecast."),
41     }
42
43     _defaults = {
44         'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.sale.forecast.createlines', context=c),
45     }
46
47     def create_forecast(self, cr, uid, ids, context=None):
48         product_obj = self.pool.get('product.product')
49         forecast_obj = self.pool.get('stock.sale.forecast')
50         mod_obj = self.pool.get('ir.model.data')
51         prod_categ_obj = self.pool.get('product.category')
52         template_obj = self.pool.get('product.template')
53         forecast_lines = []
54         for f in self.browse(cr, uid, ids, context=context):
55             categ_ids =  f.product_categ_id.id and [f.product_categ_id.id] or []
56             prod_categ_ids = prod_categ_obj.search(cr, uid, [('parent_id','child_of', categ_ids)])
57             templates_ids = template_obj.search(cr, uid, [('categ_id','in',prod_categ_ids)])
58             products_ids = product_obj.search(cr, uid, [('product_tmpl_id','in',templates_ids)])
59             if len(products_ids) == 0:
60                 raise osv.except_osv(_('Error !'), _('No products in selected category !'))
61             copy = f.copy_forecast
62             for p in product_obj.browse(cr, uid, products_ids,{}):
63                 if len(forecast_obj.search(cr, uid, [('product_id','=',p.id) , \
64                                                        ('period_id','=',f.period_id.id), \
65                                                        ('user_id','=',uid), \
66                                                        ('warehouse_id','=',f.warehouse_id.id)]))== 0:
67                     forecast_qty = 0.0
68                     prod_uom = False
69                     if copy:
70                         cr.execute("SELECT period.date_stop, forecast.product_qty, forecast.product_uom \
71                                     FROM stock_sale_forecast AS forecast \
72                                     LEFT JOIN stock_period AS period \
73                                     ON forecast.period_id = period.id \
74                                     WHERE (forecast.user_id = %s OR forecast.create_uid = %s OR forecast.write_uid = %s) \
75                                         AND forecast.warehouse_id = %s AND forecast.product_id = %s \
76                                         AND period.date_stop < %s \
77                                     ORDER BY period.date_stop DESC",
78                                     (uid, uid, uid, f.warehouse_id.id, p.id, f.period_id.date_stop) )
79                         ret = cr.fetchone()
80                         if ret:
81                             forecast_qty = ret[1]
82                             prod_uom = ret[2]
83                     prod_uom = prod_uom or p.uom_id.id
84                     prod_uos_categ = False
85                     if p.uos_id:
86                         prod_uos_categ = p.uos_id.category_id.id
87                     forecast_lines.append(forecast_obj.create(cr, uid, {
88                         'company_id': f.warehouse_id.company_id.id,
89                         'period_id': f.period_id.id,
90                         'warehouse_id': f.warehouse_id.id,
91                         'product_id': p.id,
92                         'product_qty': forecast_qty,
93                         'product_amt': 0.0,
94                         'product_uom': prod_uom,
95                         'active_uom': prod_uom,
96                         'product_uom_categ': p.uom_id.category_id.id,
97                         'product_uos_categ': prod_uos_categ,
98                      }))
99
100         return {
101             'domain': "[('id','in', ["+','.join(map(str, forecast_lines))+"])]",
102             'view_type': 'form',
103             "view_mode": 'tree,form',
104             'res_model': 'stock.sale.forecast',
105             'type': 'ir.actions.act_window',
106         }
107
108 stock_sale_forecast_createlines()
109
110 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: