[FIX] /web/login restore request.uid in case of authentication failure
[odoo/odoo.git] / addons / account_voucher / report / account_voucher_sales_receipt.py
1 ##############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ##############################################################################
20
21 from openerp.osv import fields, osv
22 from openerp import tools
23
24 class sale_receipt_report(osv.osv):
25     _name = "sale.receipt.report"
26     _description = "Sales Receipt Statistics"
27     _auto = False
28     _rec_name = 'date'
29     _columns = {
30         'date': fields.date('Date', readonly=True),
31         'year': fields.char('Year', size=4, readonly=True),
32         'day': fields.char('Day', size=128, 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         'currency_id': fields.many2one('res.currency', 'Currency', readonly=True),
37         'journal_id': fields.many2one('account.journal', 'Journal', 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', 'Salesperson', readonly=True),
41         'price_total': fields.float('Total Without Tax', readonly=True),
42         'price_total_tax': fields.float('Total With Tax', readonly=True),
43         'nbr':fields.integer('# of Voucher Lines', readonly=True),
44         'type': fields.selection([
45             ('sale','Sale'),
46             ('purchase','Purchase'),
47             ('payment','Payment'),
48             ('receipt','Receipt'),
49             ],'Type', readonly=True),
50         'state': fields.selection([
51             ('draft','Draft'),
52              ('proforma','Pro-forma'),
53              ('posted','Posted'),
54              ('cancel','Cancelled')
55             ], 'Voucher Status', readonly=True),
56         'pay_now':fields.selection([
57             ('pay_now','Pay Directly'),
58             ('pay_later','Pay Later or Group Funds'),
59         ],'Payment', readonly=True),
60         'date_due': fields.date('Due Date', readonly=True),
61         'account_id': fields.many2one('account.account', 'Account',readonly=True),
62         'delay_to_pay': fields.float('Avg. Delay To Pay', readonly=True, group_operator="avg"),
63         'due_delay': fields.float('Avg. Due Delay', readonly=True, group_operator="avg")
64     }
65     _order = 'date desc'
66     def init(self, cr):
67         tools.drop_view_if_exists(cr, 'sale_receipt_report')
68         cr.execute("""
69             create or replace view sale_receipt_report as (
70                 select min(avl.id) as id,
71                     av.date as date,
72                     to_char(av.date, 'YYYY') as year,
73                     to_char(av.date, 'MM') as month,
74                     to_char(av.date, 'YYYY-MM-DD') as day,
75                     av.partner_id as partner_id,
76                     aj.currency as currency_id,
77                     av.journal_id as journal_id,
78                     rp.user_id as user_id,
79                     av.company_id as company_id,
80                     count(avl.*) as nbr,
81                     av.type as type,
82                     av.state,
83                     av.pay_now,
84                     av.date_due as date_due,
85                     av.account_id as account_id,
86                     sum(av.amount-av.tax_amount)/(select count(l.id) from account_voucher_line as l
87                             left join account_voucher as a ON (a.id=l.voucher_id)
88                             where a.id=av.id) as price_total,
89                     sum(av.amount)/(select count(l.id) from account_voucher_line as l
90                             left join account_voucher as a ON (a.id=l.voucher_id)
91                             where a.id=av.id) as price_total_tax,
92                     sum((select extract(epoch from avg(date_trunc('day',aml.date_created)-date_trunc('day',l.create_date)))/(24*60*60)::decimal(16,2)
93                         from account_move_line as aml
94                         left join account_voucher as a ON (a.move_id=aml.move_id)
95                         left join account_voucher_line as l ON (a.id=l.voucher_id)
96                         where a.id=av.id)) as delay_to_pay,
97                     sum((select extract(epoch from avg(date_trunc('day',a.date_due)-date_trunc('day',a.date)))/(24*60*60)::decimal(16,2)
98                         from account_move_line as aml
99                         left join account_voucher as a ON (a.move_id=aml.move_id)
100                         left join account_voucher_line as l ON (a.id=l.voucher_id)
101                         where a.id=av.id)) as due_delay
102                 from account_voucher_line as avl
103                 left join account_voucher as av on (av.id=avl.voucher_id)
104                 left join res_partner as rp ON (rp.id=av.partner_id)
105                 left join account_journal as aj ON (aj.id=av.journal_id)
106                 where av.type='sale' and aj.type in ('sale','sale_refund')
107                 group by
108                     av.date,
109                     av.id,
110                     to_char(av.date, 'YYYY'),
111                     to_char(av.date, 'MM'),
112                     to_char(av.date, 'YYYY-MM-DD'),
113                     av.partner_id,
114                     aj.currency,
115                     av.journal_id,
116                     rp.user_id,
117                     av.company_id,
118                     av.type,
119                     av.state,
120                     av.date_due,
121                     av.account_id,
122                     av.tax_amount,
123                     av.amount,
124                     av.tax_amount,
125                     av.pay_now
126             )
127         """)
128
129
130 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: