bb32fd37eb3368e9e22ea33bf895c68cbe8a07a2
[odoo/odoo.git] / addons / sale / report / sale_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 from openerp import tools
23 from openerp.osv import fields, osv
24
25 class sale_report(osv.osv):
26     _name = "sale.report"
27     _description = "Sales Orders Statistics"
28     _auto = False
29     _rec_name = 'date'
30
31     _columns = {
32         'date': fields.datetime('Date Order', readonly=True),  # TDE FIXME master: rename into date_order
33         'date_confirm': fields.date('Date Confirm', readonly=True),
34         'product_id': fields.many2one('product.product', 'Product', readonly=True),
35         'product_uom': fields.many2one('product.uom', 'Unit of Measure', readonly=True),
36         'product_uom_qty': fields.float('# of Qty', readonly=True),
37
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 Price', readonly=True),
42         'delay': fields.float('Commitment Delay', digits=(16,2), readonly=True),
43         'categ_id': fields.many2one('product.category','Category of Product', readonly=True),
44         'nbr': fields.integer('# of Lines', readonly=True),  # TDE FIXME master: rename into nbr_lines
45         'state': fields.selection([
46             ('draft', 'Quotation'),
47             ('waiting_date', 'Waiting Schedule'),
48             ('manual', 'Manual In Progress'),
49             ('progress', 'In Progress'),
50             ('invoice_except', 'Invoice Exception'),
51             ('done', 'Done'),
52             ('cancel', 'Cancelled')
53             ], 'Order Status', readonly=True),
54         'pricelist_id': fields.many2one('product.pricelist', 'Pricelist', readonly=True),
55         'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
56         'team_id': fields.many2one('crm.team', 'Sales Team', oldname='section_id'),
57     }
58     _order = 'date desc'
59
60     def _select(self):
61         select_str = """
62              SELECT min(l.id) as id,
63                     l.product_id as product_id,
64                     t.uom_id as product_uom,
65                     sum(l.product_uom_qty / u.factor * u2.factor) as product_uom_qty,
66                     sum(l.product_uom_qty * l.price_unit * (100.0-l.discount) / 100.0) as price_total,
67                     count(*) as nbr,
68                     s.date_order as date,
69                     s.date_confirm as date_confirm,
70                     s.partner_id as partner_id,
71                     s.user_id as user_id,
72                     s.company_id as company_id,
73                     extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay,
74                     s.state,
75                     t.categ_id as categ_id,
76                     s.pricelist_id as pricelist_id,
77                     s.project_id as analytic_account_id,
78                     s.team_id as team_id
79         """
80         return select_str
81
82     def _from(self):
83         from_str = """
84                 sale_order_line l
85                       join sale_order s on (l.order_id=s.id)
86                         left join product_product p on (l.product_id=p.id)
87                             left join product_template t on (p.product_tmpl_id=t.id)
88                     left join product_uom u on (u.id=l.product_uom)
89                     left join product_uom u2 on (u2.id=t.uom_id)
90         """
91         return from_str
92
93     def _group_by(self):
94         group_by_str = """
95             GROUP BY l.product_id,
96                     l.order_id,
97                     t.uom_id,
98                     t.categ_id,
99                     s.date_order,
100                     s.date_confirm,
101                     s.partner_id,
102                     s.user_id,
103                     s.company_id,
104                     s.state,
105                     s.pricelist_id,
106                     s.project_id,
107                     s.team_id
108         """
109         return group_by_str
110
111     def init(self, cr):
112         # self._table = sale_report
113         tools.drop_view_if_exists(cr, self._table)
114         cr.execute("""CREATE or REPLACE VIEW %s as (
115             %s
116             FROM ( %s )
117             %s
118             )""" % (self._table, self._select(), self._from(), self._group_by()))
119
120 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: