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"
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),
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 'product_tmpl_id': fields.many2one('product.template', 'Product Template', readonly=True),
44 'categ_id': fields.many2one('product.category','Product Category', readonly=True),
45 'nbr': fields.integer('# of Lines', readonly=True), # TDE FIXME master: rename into nbr_lines
46 'state': fields.selection([
47 ('draft', 'Quotation'),
48 ('waiting_date', 'Waiting Schedule'),
49 ('manual', 'Manual In Progress'),
50 ('progress', 'In Progress'),
51 ('invoice_except', 'Invoice Exception'),
53 ('cancel', 'Cancelled')
54 ], 'Order Status', readonly=True),
55 'pricelist_id': fields.many2one('product.pricelist', 'Pricelist', readonly=True),
56 'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account', readonly=True),
57 'invoiced': fields.boolean('Paid', readonly=True),
58 'nbr_paid': fields.integer('# of Paid Lines', readonly=True),
59 'team_id': fields.many2one('crm.team', 'Sales Team', readonly=True, oldname='section_id'),
60 'country_id': fields.many2one('res.country', 'Partner Country', readonly=True),
61 'commercial_partner_id': fields.many2one('res.partner', 'Commercial Entity', readonly=True),
67 SELECT min(l.id) as id,
68 l.product_id as product_id,
69 t.uom_id as product_uom,
70 sum(l.product_uom_qty / u.factor * u2.factor) as product_uom_qty,
71 sum(l.product_uom_qty * l.price_unit * (100.0-l.discount) / 100.0) as price_total,
74 s.date_confirm as date_confirm,
75 s.partner_id as partner_id,
77 s.company_id as company_id,
78 extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay,
80 t.categ_id as categ_id,
81 s.pricelist_id as pricelist_id,
82 s.project_id as analytic_account_id,
85 l.invoiced::integer as nbr_paid,
87 partner.country_id as country_id,
88 partner.commercial_partner_id as commercial_partner_id
95 join sale_order s on (l.order_id=s.id)
96 join res_partner partner on s.partner_id = partner.id
97 left join product_product p on (l.product_id=p.id)
98 left join product_template t on (p.product_tmpl_id=t.id)
99 left join product_uom u on (u.id=l.product_uom)
100 left join product_uom u2 on (u2.id=t.uom_id)
106 GROUP BY l.product_id,
122 partner.commercial_partner_id
127 # self._table = sale_report
128 tools.drop_view_if_exists(cr, self._table)
129 cr.execute("""CREATE or REPLACE VIEW %s as (
133 )""" % (self._table, self._select(), self._from(), self._group_by()))
135 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: