[IMP] Reportings Review
[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.datetime('Order Date', readonly=True, help="Date on which this document has been created"),  # TDE FIXME master: rename into date_order
35         'state': fields.selection([('draft', 'Request for Quotation'),
36                                      ('confirmed', 'Waiting Supplier Ack'),
37                                       ('approved', 'Approved'),
38                                       ('except_picking', 'Shipping Exception'),
39                                       ('except_invoice', 'Invoice Exception'),
40                                       ('done', 'Done'),
41                                       ('cancel', 'Cancelled')],'Order Status', readonly=True),
42         'product_id':fields.many2one('product.product', 'Product', readonly=True),
43         'picking_type_id': fields.many2one('stock.warehouse', 'Warehouse', readonly=True),
44         'location_id': fields.many2one('stock.location', 'Destination', readonly=True),
45         'partner_id':fields.many2one('res.partner', 'Supplier', readonly=True),
46         'pricelist_id':fields.many2one('product.pricelist', 'Pricelist', readonly=True),
47         'date_approve':fields.date('Date Approved', readonly=True),
48         'expected_date':fields.date('Expected Date', readonly=True),
49         'validator' : fields.many2one('res.users', 'Validated By', readonly=True),
50         'product_uom' : fields.many2one('product.uom', 'Reference Unit of Measure', required=True),
51         'company_id':fields.many2one('res.company', 'Company', readonly=True),
52         'user_id':fields.many2one('res.users', 'Responsible', readonly=True),
53         'delay':fields.float('Days to Validate', digits=(16,2), readonly=True),
54         'delay_pass':fields.float('Days to Deliver', digits=(16,2), readonly=True),
55         'quantity': fields.float('Product Quantity', readonly=True),  # TDE FIXME master: rename into unit_quantity
56         'price_total': fields.float('Total Price', readonly=True),
57         'price_average': fields.float('Average Price', readonly=True, group_operator="avg"),
58         'negociation': fields.float('Purchase-Standard Price', readonly=True, group_operator="avg"),
59         'price_standard': fields.float('Products Value', readonly=True, group_operator="sum"),
60         'nbr': fields.integer('# of Lines', readonly=True),  # TDE FIXME master: rename into nbr_lines
61         'category_id': fields.many2one('product.category', 'Product Category', readonly=True),
62         'product_tmpl_id': fields.many2one('product.template', 'Product Template', readonly=True),
63         'country_id': fields.many2one('res.country', 'Partner Country', readonly=True),
64         'fiscal_position': fields.many2one('account.fiscal.position', 'Fiscal Position', readonly=True),
65         'account_analytic_id': fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
66         'commercial_partner_id': fields.many2one('res.partner', 'Commercial Entity', readonly=True),
67     }
68     _order = 'date 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                     s.state,
77                     s.date_approve,
78                     s.minimum_planned_date as expected_date,
79                     s.dest_address_id,
80                     s.pricelist_id,
81                     s.validator,
82                     s.picking_type_id as picking_type_id,
83                     s.partner_id as partner_id,
84                     s.create_uid as user_id,
85                     s.company_id as company_id,
86                     s.fiscal_position as fiscal_position,
87                     l.product_id,
88                     p.product_tmpl_id,
89                     t.categ_id as category_id,
90                     t.uom_id as product_uom,
91                     s.location_id as location_id,
92                     sum(l.product_qty/u.factor*u2.factor) as quantity,
93                     extract(epoch from age(s.date_approve,s.date_order))/(24*60*60)::decimal(16,2) as delay,
94                     extract(epoch from age(l.date_planned,s.date_order))/(24*60*60)::decimal(16,2) as delay_pass,
95                     count(*) as nbr,
96                     sum(l.price_unit*l.product_qty)::decimal(16,2) as price_total,
97                     avg(100.0 * (l.price_unit*l.product_qty) / NULLIF(ip.value_float*l.product_qty/u.factor*u2.factor, 0.0))::decimal(16,2) as negociation,
98                     sum(ip.value_float*l.product_qty/u.factor*u2.factor)::decimal(16,2) as price_standard,
99                     (sum(l.product_qty*l.price_unit)/NULLIF(sum(l.product_qty/u.factor*u2.factor),0.0))::decimal(16,2) as price_average,
100                     partner.country_id as country_id,
101                     partner.commercial_partner_id as commercial_partner_id
102                 from purchase_order_line l
103                     join purchase_order s on (l.order_id=s.id)
104                     join res_partner partner on s.partner_id = partner.id
105                         left join product_product p on (l.product_id=p.id)
106                             left join product_template t on (p.product_tmpl_id=t.id)
107                             LEFT JOIN ir_property ip ON (ip.name='standard_price' AND ip.res_id=CONCAT('product.template,',t.id) AND ip.company_id=s.company_id)
108                     left join product_uom u on (u.id=l.product_uom)
109                     left join product_uom u2 on (u2.id=t.uom_id)
110                 group by
111                     s.company_id,
112                     s.create_uid,
113                     s.partner_id,
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                     s.fiscal_position,
125                     l.product_id,
126                     p.product_tmpl_id,
127                     t.categ_id,
128                     s.date_order,
129                     s.state,
130                     s.picking_type_id,
131                     u.uom_type,
132                     u.category_id,
133                     t.uom_id,
134                     u.id,
135                     u2.factor,
136                     partner.country_id,
137                     partner.commercial_partner_id
138             )
139         """)
140
141
142 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: