[MERGE] Harmonization of noupdate flag on security XML data, courtesy of Alexis de...
[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     _columns = {
31         'date': fields.date('Date Order', readonly=True),
32         'date_confirm': fields.date('Date Confirm', readonly=True),
33         'year': fields.char('Year', size=4, readonly=True),
34         'month': fields.selection([('01', 'January'), ('02', 'February'), ('03', 'March'), ('04', 'April'),
35             ('05', 'May'), ('06', 'June'), ('07', 'July'), ('08', 'August'), ('09', 'September'),
36             ('10', 'October'), ('11', 'November'), ('12', 'December')], 'Month', readonly=True),
37         'day': fields.char('Day', size=128, readonly=True),
38         'product_id': fields.many2one('product.product', 'Product', readonly=True),
39         'product_uom': fields.many2one('product.uom', 'Unit of Measure', readonly=True),
40         'product_uom_qty': fields.float('# of Qty', readonly=True),
41
42         'partner_id': fields.many2one('res.partner', 'Partner', readonly=True),
43         'shop_id': fields.many2one('sale.shop', 'Shop', readonly=True),
44         'company_id': fields.many2one('res.company', 'Company', readonly=True),
45         'user_id': fields.many2one('res.users', 'Salesperson', readonly=True),
46         'price_total': fields.float('Total Price', readonly=True),
47         'delay': fields.float('Commitment Delay', digits=(16,2), readonly=True),
48         'categ_id': fields.many2one('product.category','Category of Product', readonly=True),
49         'nbr': fields.integer('# of Lines', readonly=True),
50         'state': fields.selection([
51             ('draft', 'Quotation'),
52             ('waiting_date', 'Waiting Schedule'),
53             ('manual', 'Manual In Progress'),
54             ('progress', 'In Progress'),
55             ('invoice_except', 'Invoice Exception'),
56             ('done', 'Done'),
57             ('cancel', 'Cancelled')
58             ], 'Order Status', readonly=True),
59         'pricelist_id': fields.many2one('product.pricelist', 'Pricelist', readonly=True),
60         'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
61     }
62     _order = 'date desc'
63
64     def init(self, cr):
65         tools.drop_view_if_exists(cr, 'sale_report')
66         cr.execute("""
67             create or replace view sale_report as (
68                 select
69                     min(l.id) as id,
70                     l.product_id as product_id,
71                     t.uom_id as product_uom,
72                     sum(l.product_uom_qty / u.factor * u2.factor) as product_uom_qty,
73                     sum(l.product_uom_qty * l.price_unit * (100.0-l.discount) / 100.0) as price_total,
74                     1 as nbr,
75                     s.date_order as date,
76                     s.date_confirm as date_confirm,
77                     to_char(s.date_order, 'YYYY') as year,
78                     to_char(s.date_order, 'MM') as month,
79                     to_char(s.date_order, 'YYYY-MM-DD') as day,
80                     s.partner_id as partner_id,
81                     s.user_id as user_id,
82                     s.shop_id as shop_id,
83                     s.company_id as company_id,
84                     extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay,
85                     s.state,
86                     t.categ_id as categ_id,
87                     s.pricelist_id as pricelist_id,
88                     s.project_id as analytic_account_id
89                 from
90                     sale_order s
91                     left join sale_order_line l on (s.id=l.order_id)
92                         left join product_product p on (l.product_id=p.id)
93                             left join product_template t on (p.product_tmpl_id=t.id)
94                     left join product_uom u on (u.id=l.product_uom)
95                     left join product_uom u2 on (u2.id=t.uom_id)
96                 where l.product_id is not null
97                 group by
98                     l.product_id,
99                     l.product_uom_qty,
100                     l.order_id,
101                     t.uom_id,
102                     t.categ_id,
103                     s.date_order,
104                     s.date_confirm,
105                     s.partner_id,
106                     s.user_id,
107                     s.shop_id,
108                     s.company_id,
109                     s.state,
110                     s.pricelist_id,
111                     s.project_id
112             )
113         """)
114 sale_report()
115
116 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: