Merge branch 'master' of https://github.com/odoo/odoo
[odoo/odoo.git] / addons / product / report / product_pricelist.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 import time
23 from openerp.osv import osv
24 from openerp.report import report_sxw
25
26
27 class product_pricelist(report_sxw.rml_parse):
28     def __init__(self, cr, uid, name, context):
29         super(product_pricelist, self).__init__(cr, uid, name, context=context)
30         self.pricelist=False
31         self.quantity=[]
32         self.localcontext.update({
33             'time': time,
34             'get_pricelist': self._get_pricelist,
35             'get_currency': self._get_currency,
36             'get_categories': self._get_categories,
37             'get_price': self._get_price,
38             'get_titles': self._get_titles,
39         })
40
41     def _get_titles(self, form):
42         lst = []
43         vals = {}
44         qtys = 1
45
46         for i in range(1,6):
47             if form['qty'+str(i)]!=0:
48                 vals['qty'+str(qtys)] = str(form['qty'+str(i)]) + ' units'
49             qtys += 1
50         lst.append(vals)
51         return lst
52
53     def _set_quantity(self, form):
54         for i in range(1,6):
55             q = 'qty%d'%i
56             if form[q] >0 and form[q] not in self.quantity:
57                 self.quantity.append(form[q])
58             else:
59                 self.quantity.append(0)
60         return True
61
62     def _get_pricelist(self, pricelist_id):
63         pricelist = self.pool.get('product.pricelist').read(self.cr, self.uid, [pricelist_id], ['name'], context=self.localcontext)[0]
64         return pricelist['name']
65
66     def _get_currency(self, pricelist_id):
67         pricelist = self.pool.get('product.pricelist').read(self.cr, self.uid, [pricelist_id], ['currency_id'], context=self.localcontext)[0]
68         return pricelist['currency_id'][1]
69
70     def _get_categories(self, products, form):
71         cat_ids=[]
72         res=[]
73         self.pricelist = form['price_list']
74         self._set_quantity(form)
75         pro_ids=[]
76         for product in products:
77             pro_ids.append(product.id)
78             if product.categ_id.id not in cat_ids:
79                 cat_ids.append(product.categ_id.id)
80
81         cats = self.pool.get('product.category').name_get(self.cr, self.uid, cat_ids, context=self.localcontext)
82         if not cats:
83             return res
84         for cat in cats:
85             product_ids=self.pool.get('product.product').search(self.cr, self.uid, [('id', 'in', pro_ids), ('categ_id', '=', cat[0])], context=self.localcontext)
86             products = []
87             for product in self.pool.get('product.product').read(self.cr, self.uid, product_ids, ['name', 'code'], context=self.localcontext):
88                 val = {
89                      'id':product['id'],
90                      'name':product['name'],
91                      'code':product['code']
92                 }
93                 i = 1
94                 for qty in self.quantity:
95                     if qty == 0:
96                         val['qty'+str(i)] = 0.0
97                     else:
98                         val['qty'+str(i)]=self._get_price(self.pricelist, product['id'], qty)
99                     i += 1
100                 products.append(val)
101             res.append({'name':cat[1],'products': products})
102         return res
103
104     def _get_price(self, pricelist_id, product_id, qty):
105         sale_price_digits = self.get_digits(dp='Product Price')
106         pricelist = self.pool.get('product.pricelist').browse(self.cr, self.uid, [pricelist_id], context=self.localcontext)[0]
107         price_dict = self.pool.get('product.pricelist').price_get(self.cr, self.uid, [pricelist_id], product_id, qty, context=self.localcontext)
108         if price_dict[pricelist_id]:
109             price = self.formatLang(price_dict[pricelist_id], digits=sale_price_digits, currency_obj=pricelist.currency_id)
110         else:
111             res = self.pool.get('product.product').read(self.cr, self.uid, [product_id])
112             price =  self.formatLang(res[0]['list_price'], digits=sale_price_digits, currency_obj=pricelist.currency_id)
113         return price
114
115
116 class report_product_pricelist(osv.AbstractModel):
117     _name = 'report.product.report_pricelist'
118     _inherit = 'report.abstract_report'
119     _template = 'product.report_pricelist'
120     _wrapped_report_class = product_pricelist
121
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: