1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from openerp import tools
23 from openerp.osv import fields, osv
25 class sale_report(osv.osv):
27 _description = "Sales Orders Statistics"
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),
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'),
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),
65 tools.drop_view_if_exists(cr, 'sale_report')
67 create or replace view sale_report as (
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,
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,
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,
86 t.categ_id as categ_id,
87 s.pricelist_id as pricelist_id,
88 s.project_id as analytic_account_id
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
116 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: