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 ##############################################################################
23 # Please note that these reports are not multi-currency !!!
26 from openerp.osv import fields,osv
27 from openerp import tools
29 class purchase_report(osv.osv):
30 _name = "purchase.report"
31 _description = "Purchases Orders"
34 'date': fields.datetime('Order Date', readonly=True, help="Date on which this document has been created"), # TDE FIXME master: rename into date_order
35 'state': fields.selection([('draft', 'Request for Quotation'),
36 ('confirmed', 'Waiting Supplier Ack'),
37 ('approved', 'Approved'),
38 ('except_picking', 'Shipping Exception'),
39 ('except_invoice', 'Invoice Exception'),
41 ('cancel', 'Cancelled')],'Order Status', readonly=True),
42 'product_id':fields.many2one('product.product', 'Product', readonly=True),
43 'picking_type_id': fields.many2one('stock.warehouse', 'Warehouse', readonly=True),
44 'location_id': fields.many2one('stock.location', 'Destination', readonly=True),
45 'partner_id':fields.many2one('res.partner', 'Supplier', readonly=True),
46 'pricelist_id':fields.many2one('product.pricelist', 'Pricelist', readonly=True),
47 'date_approve':fields.date('Date Approved', readonly=True),
48 'expected_date':fields.date('Expected Date', readonly=True),
49 'validator' : fields.many2one('res.users', 'Validated By', readonly=True),
50 'product_uom' : fields.many2one('product.uom', 'Reference Unit of Measure', required=True),
51 'company_id':fields.many2one('res.company', 'Company', readonly=True),
52 'user_id':fields.many2one('res.users', 'Responsible', readonly=True),
53 'delay':fields.float('Days to Validate', digits=(16,2), readonly=True),
54 'delay_pass':fields.float('Days to Deliver', digits=(16,2), readonly=True),
55 'quantity': fields.integer('Unit Quantity', readonly=True), # TDE FIXME master: rename into unit_quantity
56 'price_total': fields.float('Total Price', readonly=True),
57 'price_average': fields.float('Average Price', readonly=True, group_operator="avg"),
58 'negociation': fields.float('Purchase-Standard Price', readonly=True, group_operator="avg"),
59 'price_standard': fields.float('Products Value', readonly=True, group_operator="sum"),
60 'nbr': fields.integer('# of Lines', readonly=True), # TDE FIXME master: rename into nbr_lines
61 'category_id': fields.many2one('product.category', 'Category', readonly=True)
64 _order = 'date desc, price_total desc'
66 tools.sql.drop_view_if_exists(cr, 'purchase_report')
68 create or replace view purchase_report as (
74 s.minimum_planned_date as expected_date,
78 s.picking_type_id as picking_type_id,
79 s.partner_id as partner_id,
80 s.create_uid as user_id,
81 s.company_id as company_id,
83 t.categ_id as category_id,
84 t.uom_id as product_uom,
85 s.location_id as location_id,
86 sum(l.product_qty/u.factor*u2.factor) as quantity,
87 extract(epoch from age(s.date_approve,s.date_order))/(24*60*60)::decimal(16,2) as delay,
88 extract(epoch from age(l.date_planned,s.date_order))/(24*60*60)::decimal(16,2) as delay_pass,
90 sum(l.price_unit*l.product_qty)::decimal(16,2) as price_total,
91 avg(100.0 * (l.price_unit*l.product_qty) / NULLIF(ip.value_float*l.product_qty/u.factor*u2.factor, 0.0))::decimal(16,2) as negociation,
92 sum(ip.value_float*l.product_qty/u.factor*u2.factor)::decimal(16,2) as price_standard,
93 (sum(l.product_qty*l.price_unit)/NULLIF(sum(l.product_qty/u.factor*u2.factor),0.0))::decimal(16,2) as price_average
94 from purchase_order_line l
95 join purchase_order s on (l.order_id=s.id)
96 left join product_product p on (l.product_id=p.id)
97 left join product_template t on (p.product_tmpl_id=t.id)
98 LEFT JOIN ir_property ip ON (ip.name='standard_price' AND ip.res_id=CONCAT('product.template,',t.id) AND ip.company_id=s.company_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)
111 s.minimum_planned_date,
129 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: