[ADD]:ADD SQL View report for Invoices.
[odoo/odoo.git] / addons / account / report / account_invoice_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 import tools
23 from osv import fields,osv
24
25 class account_invoice_report(osv.osv):
26     _name = "account.invoice.report"
27     _description = "Invoices Statistics"
28     _auto = False
29     _rec_name = 'date'
30     _columns = {
31         'date': fields.date('Date', readonly=True),
32         'year': fields.char('Year', size=4, readonly=True),
33         'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'),
34             ('05','May'), ('06','June'), ('07','July'), ('08','August'), ('09','September'),
35             ('10','October'), ('11','November'), ('12','December')], 'Month',readonly=True),
36         'product_id':fields.many2one('product.product', 'Product', readonly=True),
37         'product_qty':fields.float('Qty', readonly=True),
38         'partner_id':fields.many2one('res.partner', 'Partner', readonly=True),
39         'company_id':fields.many2one('res.company', 'Company', readonly=True),
40         'user_id':fields.many2one('res.users', 'Salesman', readonly=True),
41         'price_total':fields.float('Total Price', readonly=True),
42         'price_average':fields.float('Average Price', readonly=True),
43         'nbr':fields.integer('# of Lines', readonly=True),
44         'state': fields.selection([
45             ('draft','Draft'),
46             ('proforma','Pro-forma'),
47             ('proforma2','Pro-forma'),
48             ('open','Open'),
49             ('paid','Done'),
50             ('cancel','Cancelled')
51             ], 'Order State', readonly=True),
52     }
53     _order = 'date desc'
54     def init(self, cr):
55         tools.drop_view_if_exists(cr, 'report_account_invoice_report')
56         cr.execute("""
57             create or replace view account_invoice_report as (
58                  select
59                      min(l.id) as id,
60                      s.date_invoice as date,
61                      to_char(s.date_invoice, 'YYYY') as year,
62                      to_char(s.date_invoice, 'MM') as month,
63                      l.product_id as product_id,
64                      sum(l.quantity * u.factor) as product_qty,
65                      s.partner_id as partner_id,
66                      s.user_id as user_id,
67                      s.company_id as company_id,
68                      sum(l.quantity*l.price_unit) as price_total,
69                      (sum(l.quantity*l.price_unit)/sum(l.quantity * u.factor))::decimal(16,2) as price_average,
70                      count(*) as nbr,
71                      s.state
72                      from
73                  account_invoice_line l
74                  left join
75                      account_invoice s on (s.id=l.invoice_id)
76                      left join product_uom u on (u.id=l.uos_id)
77                  group by
78                      s.date_invoice, s.partner_id, l.product_id,
79                      l.uos_id, s.user_id, s.state,
80                      s.company_id
81             )
82         """)
83 account_invoice_report()