Use product.product instead of product.template when we go through the ORM.
[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         forecast_lines = []
53         for f in self.browse(cr, uid, ids, context=context):
54             categ_ids =  f.product_categ_id.id and [f.product_categ_id.id] or []
55             prod_categ_ids = prod_categ_obj.search(cr, uid, [('parent_id','child_of', categ_ids)])
56             products_ids = product_obj.search(cr, uid, [('categ_id','in',prod_categ_ids)])
57             if len(products_ids) == 0:
58                 raise osv.except_osv(_('Error !'), _('No products in selected category !'))
59             copy = f.copy_forecast
60             for p in product_obj.browse(cr, uid, products_ids,{}):
61                 if len(forecast_obj.search(cr, uid, [('product_id','=',p.id) , \
62                                                        ('period_id','=',f.period_id.id), \
63                                                        ('user_id','=',uid), \
64                                                        ('warehouse_id','=',f.warehouse_id.id)]))== 0:
65                     forecast_qty = 0.0
66                     prod_uom = False
67                     if copy:
68                         cr.execute("SELECT period.date_stop, forecast.product_qty, forecast.product_uom \
69                                     FROM stock_sale_forecast AS forecast \
70                                     LEFT JOIN stock_period AS period \
71                                     ON forecast.period_id = period.id \
72                                     WHERE (forecast.user_id = %s OR forecast.create_uid = %s OR forecast.write_uid = %s) \
73                                         AND forecast.warehouse_id = %s AND forecast.product_id = %s \
74                                         AND period.date_stop < %s \
75                                     ORDER BY period.date_stop DESC",
76                                     (uid, uid, uid, f.warehouse_id.id, p.id, f.period_id.date_stop) )
77                         ret = cr.fetchone()
78                         if ret:
79                             forecast_qty = ret[1]
80                             prod_uom = ret[2]
81                     prod_uom = prod_uom or p.uom_id.id
82                     prod_uos_categ = False
83                     if p.uos_id:
84                         prod_uos_categ = p.uos_id.category_id.id
85                     forecast_lines.append(forecast_obj.create(cr, uid, {
86                         'company_id': f.warehouse_id.company_id.id,
87                         'period_id': f.period_id.id,
88                         'warehouse_id': f.warehouse_id.id,
89                         'product_id': p.id,
90                         'product_qty': forecast_qty,
91                         'product_amt': 0.0,
92                         'product_uom': prod_uom,
93                         'active_uom': prod_uom,
94                         'product_uom_categ': p.uom_id.category_id.id,
95                         'product_uos_categ': prod_uos_categ,
96                      }))
97
98         return {
99             'domain': "[('id','in', ["+','.join(map(str, forecast_lines))+"])]",
100             'view_type': 'form',
101             "view_mode": 'tree,form',
102             'res_model': 'stock.sale.forecast',
103             'type': 'ir.actions.act_window',
104         }
105
106 stock_sale_forecast_createlines()
107
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: