[FIX] Change the version - 5.0.1
[odoo/odoo.git] / addons / product_margin / product_margin.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields,osv
24 import pooler
25 from tools import config
26 import time
27
28 class product_product(osv.osv):
29     _inherit = "product.product"     
30     
31     
32     def _product_margin(self, cr, uid, ids, field_names, arg, context):
33         res = {}               
34         for val in self.browse(cr, uid, ids,context=context):
35             res[val.id] = {}
36             date_from=context.get('date_from', time.strftime('%Y-01-01'))       
37             date_to=context.get('date_to', time.strftime('%Y-12-31'))   
38             invoice_state=context.get('invoice_state', 'open_paid')     
39             if 'date_from' in field_names:
40                 res[val.id]['date_from']=date_from
41             if 'date_to' in field_names:
42                 res[val.id]['date_to']=date_to
43             if 'invoice_state' in field_names:
44                 res[val.id]['invoice_state']=invoice_state
45
46             
47             invoice_types=[]
48             states=[]
49             if invoice_state=='paid':
50                 states=['paid']
51             elif invoice_state=='open_paid':
52                 states=['open','paid']
53             elif invoice_state=='draft_open_paid':
54                 states=['draft','open','paid']
55             
56             if 'sale_avg_price' in field_names or 'sale_num_invoiced' in field_names or 'turnover' in field_names or 'sale_expected' in field_names:
57                 invoice_types=['out_invoice','in_refund']
58             if 'purchase_avg_price' in field_names or 'purchase_num_invoiced' in field_names or 'total_cost' in field_names or 'normal_cost' in field_names:
59                 invoice_types=['in_invoice','out_refund']
60             if len(invoice_types):
61                 sql="""
62                 select 
63                         avg(l.price_unit) as avg_unit_price,
64                         sum(l.quantity) as num_qty,
65                         sum(l.quantity * l.price_unit) as total,
66                         sum(l.quantity * product.list_price) as sale_expected,
67                         sum(l.quantity * product.standard_price) as normal_cost 
68                 from account_invoice_line l
69                 left join account_invoice i on (l.invoice_id = i.id)
70                 left join product_template product on (product.id=l.product_id)                
71                 where l.product_id = %s and i.state in ('%s') and i.type in ('%s') and i.date_invoice>='%s' and i.date_invoice<='%s'           
72                 """%(val.id,"','".join(states),"','".join(invoice_types),date_from,date_to)                
73                 cr.execute(sql)
74                 result=cr.fetchall()[0]                
75                 if 'sale_avg_price' in field_names or 'sale_num_invoiced' in field_names or 'turnover' in field_names or 'sale_expected' in field_names:
76                     res[val.id]['sale_avg_price']=result[0] and result[0] or 0.0
77                     res[val.id]['sale_num_invoiced']=result[1] and result[1] or 0.0
78                     res[val.id]['turnover']=result[2] and result[2] or 0.0
79                     res[val.id]['sale_expected']=result[3] and result[3] or 0.0
80                     res[val.id]['sales_gap']=res[val.id]['sale_expected']-res[val.id]['turnover']
81                 if 'purchase_avg_price' in field_names or 'purchase_num_invoiced' in field_names or 'total_cost' in field_names or 'normal_cost' in field_names:
82                     res[val.id]['purchase_avg_price']=result[0] and result[0] or 0.0
83                     res[val.id]['purchase_num_invoiced']=result[1] and result[1] or 0.0
84                     res[val.id]['total_cost']=result[2] and result[2] or 0.0
85                     res[val.id]['normal_cost']=result[4] and result[4] or 0.0
86                     res[val.id]['purchase_gap']=res[val.id]['normal_cost']-res[val.id]['total_cost']                 
87             
88             if 'total_margin' in field_names:
89                 res[val.id]['total_margin']=val.turnover-val.total_cost
90             if 'expected_margin' in field_names:
91                 res[val.id]['expected_margin']=val.sale_expected-val.normal_cost
92             if 'total_margin_rate' in field_names:
93                 res[val.id]['total_margin_rate']=val.turnover and val.total_margin * 100 / val.turnover or 0.0
94             if 'expected_margin_rate' in field_names:
95                 res[val.id]['expected_margin_rate']=val.sale_expected and val.expected_margin * 100 / val.sale_expected or 0.0 
96         return res
97     
98     _columns = {
99         'date_from': fields.function(_product_margin, method=True, type='date', string='From Date', multi=True),
100         'date_to': fields.function(_product_margin, method=True, type='date', string='To Date', multi=True),
101         'invoice_state': fields.function(_product_margin, method=True, type='selection', selection=[
102                         ('paid','Paid'),('open_paid','Open and Paid'),('draft_open_paid','Draft, Open and Paid')
103                         ], string='Invoice State',multi=True, readonly=True),        
104         'sale_avg_price' : fields.function(_product_margin, method=True, type='float', string='Avg. Unit Price', multi='sale',help="Avg. Price in Customer Invoices)"),
105         'purchase_avg_price' : fields.function(_product_margin, method=True, type='float', string='Avg. Unit Price', multi='purchase',help="Avg. Price in Supplier Invoices "),
106         'sale_num_invoiced' : fields.function(_product_margin, method=True, type='float', string='# Invoiced', multi='sale',help="Sum of Quantity in Customer Invoices"),
107         'purchase_num_invoiced' : fields.function(_product_margin, method=True, type='float', string='# Invoiced', multi='purchase',help="Sum of Quantity in Supplier Invoices"),
108         'sales_gap' : fields.function(_product_margin, method=True, type='float', string='Sales Gap', multi='sale',help="Excepted Sale - Turn Over"),
109         'purchase_gap' : fields.function(_product_margin, method=True, type='float', string='Purchase Gap', multi='purchase',help="Normal Cost - Total Cost"),
110         'turnover' : fields.function(_product_margin, method=True, type='float', string='Turnover' ,multi='sale',help="Sum of Multification of Invoice price and quantity of Customer Invoices"),
111         'total_cost'  : fields.function(_product_margin, method=True, type='float', string='Total Cost', multi='purchase',help="Sum of Multification of Invoice price and quantity of Supplier Invoices "),
112         'sale_expected' :  fields.function(_product_margin, method=True, type='float', string='Expected Sale', multi='sale',help="Sum of Multification of Sale Catalog price and quantity of Customer Invoices"),
113         'normal_cost'  : fields.function(_product_margin, method=True, type='float', string='Normal Cost', multi='purchase',help="Sum of Multification of Cost price and quantity of Supplier Invoices"),
114         'total_margin' : fields.function(_product_margin, method=True, type='float', string='Total Margin', multi='total',help="Turnorder - Total Cost"),
115         'expected_margin' : fields.function(_product_margin, method=True, type='float', string='Expected Margin', multi='total',help="Excepted Sale - Normal Cost"),
116         'total_margin_rate' : fields.function(_product_margin, method=True, type='float', string='Total Margin (%)', multi='margin',help="Total margin * 100 / Turnover"),
117         'expected_margin_rate' : fields.function(_product_margin, method=True, type='float', string='Expected Margin (%)', multi='margin',help="Expected margin * 100 / Expected Sale"),
118     }
119 product_product()
120
121
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
123