[IMP]:improved the SQL account report with type of invoice.(Extended options...).
[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         'type': fields.selection([
45             ('out_invoice','Customer Invoice'),
46             ('in_invoice','Supplier Invoice'),
47             ('out_refund','Customer Refund'),
48             ('in_refund','Supplier Refund'),
49             ],'Type', readonly=True),
50         'state': fields.selection([
51             ('draft','Draft'),
52             ('proforma','Pro-forma'),
53             ('proforma2','Pro-forma'),
54             ('open','Open'),
55             ('paid','Done'),
56             ('cancel','Cancelled')
57             ], 'Order State', readonly=True),
58     }
59     _order = 'date desc'
60     def init(self, cr):
61         tools.drop_view_if_exists(cr, 'report_account_invoice_report')
62         cr.execute("""
63             create or replace view account_invoice_report as (
64                  select
65                      min(l.id) as id,
66                      s.date_invoice as date,
67                      to_char(s.date_invoice, 'YYYY') as year,
68                      to_char(s.date_invoice, 'MM') as month,
69                      l.product_id as product_id,
70                      sum(l.quantity * u.factor) as product_qty,
71                      s.partner_id as partner_id,
72                      s.user_id as user_id,
73                      s.company_id as company_id,
74                      sum(l.quantity*l.price_unit) as price_total,
75                      (sum(l.quantity*l.price_unit)/sum(l.quantity * u.factor))::decimal(16,2) as price_average,
76                      count(*) as nbr,
77                      s.type as type,
78                      s.state
79                      from
80                  account_invoice_line l
81                  left join
82                      account_invoice s on (s.id=l.invoice_id)
83                      left join product_uom u on (u.id=l.uos_id)
84                  group by
85                      s.type,s.date_invoice, s.partner_id, l.product_id,
86                      l.uos_id, s.user_id, s.state,
87                      s.company_id
88             )
89         """)
90 account_invoice_report()