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