[IMP] Rounding of the UoM should have an unlimited precision
[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         'currency_id': fields.many2one('res.currency', 'Currency', readonly=True),
32         'journal_id': fields.many2one('account.journal', 'Journal', readonly=True),
33         'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
34         'company_id': fields.many2one('res.company', 'Company', readonly=True),
35         'user_id': fields.many2one('res.users', 'Salesperson', readonly=True),
36         'price_total': fields.float('Total Without Tax', readonly=True),
37         'price_total_tax': fields.float('Total With Tax', readonly=True),
38         'nbr':fields.integer('# of Voucher Lines', readonly=True),
39         'type': fields.selection([
40             ('sale','Sale'),
41             ('purchase','Purchase'),
42             ('payment','Payment'),
43             ('receipt','Receipt'),
44             ],'Type', readonly=True),
45         'state': fields.selection([
46             ('draft','Draft'),
47              ('proforma','Pro-forma'),
48              ('posted','Posted'),
49              ('cancel','Cancelled')
50             ], 'Voucher Status', readonly=True),
51         'pay_now':fields.selection([
52             ('pay_now','Pay Directly'),
53             ('pay_later','Pay Later or Group Funds'),
54         ],'Payment', readonly=True),
55         'date_due': fields.date('Due Date', readonly=True),
56         'account_id': fields.many2one('account.account', 'Account',readonly=True),
57         'delay_to_pay': fields.float('Avg. Delay To Pay', readonly=True, group_operator="avg"),
58         'due_delay': fields.float('Avg. Due Delay', readonly=True, group_operator="avg")
59     }
60     _order = 'date desc'
61     def init(self, cr):
62         tools.drop_view_if_exists(cr, 'sale_receipt_report')
63         cr.execute("""
64             create or replace view sale_receipt_report as (
65                 select min(avl.id) as id,
66                     av.date as date,
67                     av.partner_id as partner_id,
68                     aj.currency as currency_id,
69                     av.journal_id as journal_id,
70                     rp.user_id as user_id,
71                     av.company_id as company_id,
72                     count(avl.*) as nbr,
73                     av.type as type,
74                     av.state,
75                     av.pay_now,
76                     av.date_due as date_due,
77                     av.account_id as account_id,
78                     sum(av.amount-av.tax_amount)/(select count(l.id) from account_voucher_line as l
79                             left join account_voucher as a ON (a.id=l.voucher_id)
80                             where a.id=av.id) as price_total,
81                     sum(av.amount)/(select count(l.id) from account_voucher_line as l
82                             left join account_voucher as a ON (a.id=l.voucher_id)
83                             where a.id=av.id) as price_total_tax,
84                     sum((select extract(epoch from avg(date_trunc('day',aml.date_created)-date_trunc('day',l.create_date)))/(24*60*60)::decimal(16,2)
85                         from account_move_line as aml
86                         left join account_voucher as a ON (a.move_id=aml.move_id)
87                         left join account_voucher_line as l ON (a.id=l.voucher_id)
88                         where a.id=av.id)) as delay_to_pay,
89                     sum((select extract(epoch from avg(date_trunc('day',a.date_due)-date_trunc('day',a.date)))/(24*60*60)::decimal(16,2)
90                         from account_move_line as aml
91                         left join account_voucher as a ON (a.move_id=aml.move_id)
92                         left join account_voucher_line as l ON (a.id=l.voucher_id)
93                         where a.id=av.id)) as due_delay
94                 from account_voucher_line as avl
95                 left join account_voucher as av on (av.id=avl.voucher_id)
96                 left join res_partner as rp ON (rp.id=av.partner_id)
97                 left join account_journal as aj ON (aj.id=av.journal_id)
98                 where av.type='sale' and aj.type in ('sale','sale_refund')
99                 group by
100                     av.date,
101                     av.id,
102                     av.partner_id,
103                     aj.currency,
104                     av.journal_id,
105                     rp.user_id,
106                     av.company_id,
107                     av.type,
108                     av.state,
109                     av.date_due,
110                     av.account_id,
111                     av.tax_amount,
112                     av.amount,
113                     av.tax_amount,
114                     av.pay_now
115             )
116         """)
117
118
119 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: