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.date('Order Date', readonly=True, help="Date on which this document has been created"),
35 'name': fields.char('Year',size=64,required=False, readonly=True),
36 'day': fields.char('Day', size=128, readonly=True),
37 'state': fields.selection([('draft', 'Request for Quotation'),
38 ('confirmed', 'Waiting Supplier Ack'),
39 ('approved', 'Approved'),
40 ('except_picking', 'Shipping Exception'),
41 ('except_invoice', 'Invoice Exception'),
43 ('cancel', 'Cancelled')],'Order Status', readonly=True),
44 'product_id':fields.many2one('product.product', 'Product', readonly=True),
45 'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', readonly=True),
46 'location_id': fields.many2one('stock.location', 'Destination', readonly=True),
47 'partner_id':fields.many2one('res.partner', 'Supplier', readonly=True),
48 'pricelist_id':fields.many2one('product.pricelist', 'Pricelist', readonly=True),
49 'date_approve':fields.date('Date Approved', readonly=True),
50 'expected_date':fields.date('Expected Date', readonly=True),
51 'validator' : fields.many2one('res.users', 'Validated By', readonly=True),
52 'product_uom' : fields.many2one('product.uom', 'Reference Unit of Measure', required=True),
53 'company_id':fields.many2one('res.company', 'Company', readonly=True),
54 'user_id':fields.many2one('res.users', 'Responsible', readonly=True),
55 'delay':fields.float('Days to Validate', digits=(16,2), readonly=True),
56 'delay_pass':fields.float('Days to Deliver', digits=(16,2), readonly=True),
57 'quantity': fields.float('Quantity', readonly=True),
58 'price_total': fields.float('Total Price', readonly=True),
59 'price_average': fields.float('Average Price', readonly=True, group_operator="avg"),
60 'negociation': fields.float('Purchase-Standard Price', readonly=True, group_operator="avg"),
61 'price_standard': fields.float('Products Value', readonly=True, group_operator="sum"),
62 'nbr': fields.integer('# of Lines', readonly=True),
63 'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'), ('05','May'), ('06','June'),
64 ('07','July'), ('08','August'), ('09','September'), ('10','October'), ('11','November'), ('12','December')],'Month',readonly=True),
65 'category_id': fields.many2one('product.category', 'Category', readonly=True)
68 _order = 'name desc,price_total desc'
70 tools.sql.drop_view_if_exists(cr, 'purchase_report')
72 create or replace view purchase_report as (
76 to_char(s.date_order, 'YYYY') as name,
77 to_char(s.date_order, 'MM') as month,
78 to_char(s.date_order, 'YYYY-MM-DD') as day,
81 s.minimum_planned_date as expected_date,
85 s.warehouse_id as warehouse_id,
86 s.partner_id as partner_id,
87 s.create_uid as user_id,
88 s.company_id as company_id,
90 t.categ_id as category_id,
91 t.uom_id as product_uom,
92 s.location_id as location_id,
93 sum(l.product_qty/u.factor*u2.factor) as quantity,
94 extract(epoch from age(s.date_approve,s.date_order))/(24*60*60)::decimal(16,2) as delay,
95 extract(epoch from age(l.date_planned,s.date_order))/(24*60*60)::decimal(16,2) as delay_pass,
97 (l.price_unit*l.product_qty)::decimal(16,2) as price_total,
98 avg(100.0 * (l.price_unit*l.product_qty) / NULLIF(t.standard_price*l.product_qty/u.factor*u2.factor, 0.0))::decimal(16,2) as negociation,
99 sum(t.standard_price*l.product_qty/u.factor*u2.factor)::decimal(16,2) as price_standard,
100 (sum(l.product_qty*l.price_unit)/NULLIF(sum(l.product_qty/u.factor*u2.factor),0.0))::decimal(16,2) as price_average
101 from purchase_order_line l
102 join purchase_order s on (l.order_id=s.id)
103 left join product_product p on (l.product_id=p.id)
104 left join product_template t on (p.product_tmpl_id=t.id)
105 left join product_uom u on (u.id=l.product_uom)
106 left join product_uom u2 on (u2.id=t.uom_id)
118 s.minimum_planned_date,
125 to_char(s.date_order, 'YYYY'),
126 to_char(s.date_order, 'MM'),
127 to_char(s.date_order, 'YYYY-MM-DD'),
140 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: