1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import osv, fields
23 from tools.translate import _
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"
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."),
44 'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'stock.sale.forecast.createlines', context=c),
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')
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:
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) )
81 prod_uom = prod_uom or p.uom_id.id
82 prod_uos_categ = False
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,
90 'product_qty': forecast_qty,
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,
99 'domain': "[('id','in', ["+','.join(map(str, forecast_lines))+"])]",
101 "view_mode": 'tree,form',
102 'res_model': 'stock.sale.forecast',
103 'type': 'ir.actions.act_window',
106 stock_sale_forecast_createlines()
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: