Change substring to to_char for pg8.3
[odoo/odoo.git] / addons / report_purchase / report_purchase.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # $Id: purchase.py 1005 2005-07-25 08:41:42Z nicoe $
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 #
28 ##############################################################################
29
30 #
31 # Please note that these reports are not multi-currency !!!
32 #
33
34 from osv import fields,osv
35
36 class report_purchase_order_product(osv.osv):
37         _name = "report.purchase.order.product"
38         _description = "Purchases Orders by Products"
39         _auto = False
40         _columns = {
41                 'name': fields.date('Month', readonly=True),
42                 'state': fields.selection([
43                         ('draft','Quotation'),
44                         ('waiting_date','Waiting Schedule'),
45                         ('manual','Manual in progress'),
46                         ('progress','In progress'),
47                         ('shipping_except','Shipping Exception'),
48                         ('invoice_except','Invoice Exception'),
49                         ('done','Done'),
50                         ('cancel','Cancel')
51                 ], 'Order State', readonly=True),
52                 'product_id':fields.many2one('product.product', 'Product', readonly=True),
53                 'quantity': fields.float('# of Products', readonly=True),
54                 'price_total': fields.float('Total Price', readonly=True),
55                 'price_average': fields.float('Average Price', readonly=True),
56                 'count': fields.integer('# of Lines', readonly=True),
57         }
58         _order = 'name desc,price_total desc'
59         def init(self, cr):
60                 cr.execute("""
61                         create or replace view report_purchase_order_product as (
62                                 select
63                                         min(l.id) as id,
64                                         to_char(s.date_order, 'YYYY-MM-01') as name,
65                                         s.state,
66                                         l.product_id,
67                                         sum(l.product_qty*u.factor) as quantity,
68                                         count(*),
69                                         sum(l.product_qty*l.price_unit) as price_total,
70                                         (sum(l.product_qty*l.price_unit)/sum(l.product_qty*u.factor))::decimal(16,2) as price_average
71                                 from purchase_order s
72                                         left join purchase_order_line l on (s.id=l.order_id)
73                                         left join product_uom u on (u.id=l.product_uom)
74                                 group by l.product_id, to_char(s.date_order, 'YYYY-MM-01'),s.state
75                         )
76                 """)
77 report_purchase_order_product()
78
79 class report_purchase_order_category(osv.osv):
80         _name = "report.purchase.order.category"
81         _description = "Purchases Orders by Categories"
82         _auto = False
83         _columns = {
84                 'name': fields.date('Month', readonly=True),
85                 'state': fields.selection([
86                         ('draft','Quotation'),
87                         ('waiting_date','Waiting Schedule'),
88                         ('manual','Manual in progress'),
89                         ('progress','In progress'),
90                         ('shipping_except','Shipping Exception'),
91                         ('invoice_except','Invoice Exception'),
92                         ('done','Done'),
93                         ('cancel','Cancel')
94                 ], 'Order State', readonly=True),
95                 'category_id': fields.many2one('product.category', 'Categories', readonly=True),
96                 'quantity': fields.float('# of Products', readonly=True),
97                 'price_total': fields.float('Total Price', readonly=True),
98                 'price_average': fields.float('Average Price', readonly=True),
99                 'count': fields.integer('# of Lines', readonly=True),
100         }
101         _order = 'name desc,price_total desc'
102         def init(self, cr):
103                 cr.execute("""
104                         create or replace view report_purchase_order_category as (
105                                 select
106                                         min(l.id) as id,
107                                         to_char(s.date_order, 'YYYY-MM-01') as name,
108                                         s.state,
109                                         t.categ_id as category_id,
110                                         sum(l.product_qty*u.factor) as quantity,
111                                         count(*),
112                                         sum(l.product_qty*l.price_unit) as price_total,
113                                         (sum(l.product_qty*l.price_unit)/sum(l.product_qty*u.factor))::decimal(16,2) as price_average
114                                 from purchase_order s
115                                         left join purchase_order_line l on (s.id=l.order_id)
116                                         left join product_product p on (p.id=l.product_id)
117                                         left join product_template t on (t.id=p.product_tmpl_id)
118                                         left join product_uom u on (u.id=l.product_uom)
119                                 group by t.categ_id, to_char(s.date_order, 'YYYY-MM-01'),s.state
120                          )
121                 """)
122 report_purchase_order_category()