1855ed173ca8df1cc55374c38a399885cdd2ce20
[odoo/odoo.git] / addons / purchase / report / purchase_report.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 #
23 # Please note that these reports are not multi-currency !!!
24 #
25
26 from openerp.osv import fields,osv
27 from openerp import tools
28
29 class purchase_report(osv.osv):
30     _name = "purchase.report"
31     _description = "Purchases Orders"
32     _auto = False
33     _columns = {
34         'date': fields.date('Order Date', readonly=True, help="Date on which this document has been created"),
35         'name': fields.char('Year',size=64,required=False, readonly=True),
36         'day': fields.char('Day', size=128, readonly=True),
37         'state': fields.selection([('draft', 'Request for Quotation'),
38                                      ('confirmed', 'Waiting Supplier Ack'),
39                                       ('approved', 'Approved'),
40                                       ('except_picking', 'Shipping Exception'),
41                                       ('except_invoice', 'Invoice Exception'),
42                                       ('done', 'Done'),
43                                       ('cancel', 'Cancelled')],'Order Status', readonly=True),
44         'product_id':fields.many2one('product.product', 'Product', readonly=True),
45         'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', readonly=True),
46         'location_id': fields.many2one('stock.location', 'Destination', readonly=True),
47         'partner_id':fields.many2one('res.partner', 'Supplier', readonly=True),
48         'pricelist_id':fields.many2one('product.pricelist', 'Pricelist', readonly=True),
49         'date_approve':fields.date('Date Approved', readonly=True),
50         'expected_date':fields.date('Expected Date', readonly=True),
51         'validator' : fields.many2one('res.users', 'Validated By', readonly=True),
52         'product_uom' : fields.many2one('product.uom', 'Reference Unit of Measure', required=True),
53         'company_id':fields.many2one('res.company', 'Company', readonly=True),
54         'user_id':fields.many2one('res.users', 'Responsible', readonly=True),
55         'delay':fields.float('Days to Validate', digits=(16,2), readonly=True),
56         'delay_pass':fields.float('Days to Deliver', digits=(16,2), readonly=True),
57         'quantity': fields.float('Quantity', readonly=True),
58         'price_total': fields.float('Total Price', readonly=True),
59         'price_average': fields.float('Average Price', readonly=True, group_operator="avg"),
60         'negociation': fields.float('Purchase-Standard Price', readonly=True, group_operator="avg"),
61         'price_standard': fields.float('Products Value', readonly=True, group_operator="sum"),
62         'nbr': fields.integer('# of Lines', readonly=True),
63         'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'), ('05','May'), ('06','June'),
64                           ('07','July'), ('08','August'), ('09','September'), ('10','October'), ('11','November'), ('12','December')],'Month',readonly=True),
65         'category_id': fields.many2one('product.category', 'Category', readonly=True)
66
67     }
68     _order = 'name desc,price_total desc'
69     def init(self, cr):
70         tools.sql.drop_view_if_exists(cr, 'purchase_report')
71         cr.execute("""
72             create or replace view purchase_report as (
73                 select
74                     min(l.id) as id,
75                     s.date_order as date,
76                     to_char(s.date_order, 'YYYY') as name,
77                     to_char(s.date_order, 'MM') as month,
78                     to_char(s.date_order, 'YYYY-MM-DD') as day,
79                     s.state,
80                     s.date_approve,
81                     s.minimum_planned_date as expected_date,
82                     s.dest_address_id,
83                     s.pricelist_id,
84                     s.validator,
85                     s.warehouse_id as warehouse_id,
86                     s.partner_id as partner_id,
87                     s.create_uid as user_id,
88                     s.company_id as company_id,
89                     l.product_id,
90                     t.categ_id as category_id,
91                     t.uom_id as product_uom,
92                     s.location_id as location_id,
93                     sum(l.product_qty/u.factor*u2.factor) as quantity,
94                     extract(epoch from age(s.date_approve,s.date_order))/(24*60*60)::decimal(16,2) as delay,
95                     extract(epoch from age(l.date_planned,s.date_order))/(24*60*60)::decimal(16,2) as delay_pass,
96                     count(*) as nbr,
97                     (l.price_unit*l.product_qty)::decimal(16,2) as price_total,
98                     avg(100.0 * (l.price_unit*l.product_qty) / NULLIF(t.standard_price*l.product_qty/u.factor*u2.factor, 0.0))::decimal(16,2) as negociation,
99
100                     sum(t.standard_price*l.product_qty/u.factor*u2.factor)::decimal(16,2) as price_standard,
101                     (sum(l.product_qty*l.price_unit)/NULLIF(sum(l.product_qty/u.factor*u2.factor),0.0))::decimal(16,2) as price_average
102                 from purchase_order s
103                     left join purchase_order_line l on (s.id=l.order_id)
104                         left join product_product p on (l.product_id=p.id)
105                             left join product_template t on (p.product_tmpl_id=t.id)
106                     left join product_uom u on (u.id=l.product_uom)
107                     left join product_uom u2 on (u2.id=t.uom_id)
108                 where l.product_id is not null
109                 group by
110                     s.company_id,
111                     s.create_uid,
112                     s.partner_id,
113                     l.product_qty,
114                     u.factor,
115                     s.location_id,
116                     l.price_unit,
117                     s.date_approve,
118                     l.date_planned,
119                     l.product_uom,
120                     s.minimum_planned_date,
121                     s.pricelist_id,
122                     s.validator,
123                     s.dest_address_id,
124                     l.product_id,
125                     t.categ_id,
126                     s.date_order,
127                     to_char(s.date_order, 'YYYY'),
128                     to_char(s.date_order, 'MM'),
129                     to_char(s.date_order, 'YYYY-MM-DD'),
130                     s.state,
131                     s.warehouse_id,
132                     u.uom_type,
133                     u.category_id,
134                     t.uom_id,
135                     u.id,
136                     u2.factor
137             )
138         """)
139 purchase_report()
140
141
142 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: