Launchpad automatic translations update.
[odoo/odoo.git] / addons / product_margin / product_margin.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import time
23
24 from osv import fields, osv
25
26
27 class product_product(osv.osv):
28     _inherit = "product.product"
29
30     def _product_margin(self, cr, uid, ids, field_names, arg, context=None):
31         res = {}
32         if context is None:
33             context = {}
34
35         for val in self.browse(cr, uid, ids, context=context):
36             res[val.id] = {}
37             date_from = context.get('date_from', time.strftime('%Y-01-01'))
38             date_to = context.get('date_to', time.strftime('%Y-12-31'))
39             invoice_state = context.get('invoice_state', 'open_paid')
40             if 'date_from' in field_names:
41                 res[val.id]['date_from'] = date_from
42             if 'date_to' in field_names:
43                 res[val.id]['date_to'] = date_to
44             if 'invoice_state' in field_names:
45                 res[val.id]['invoice_state'] = invoice_state
46             invoice_types = ()
47             states = ()
48             if invoice_state == 'paid':
49                 states = ('paid',)
50             elif invoice_state == 'open_paid':
51                 states = ('open', 'paid')
52             elif invoice_state == 'draft_open_paid':
53                 states = ('draft', 'open', 'paid')
54
55             sqlstr="""select
56                     sum(l.price_unit * l.quantity)/sum(l.quantity) as avg_unit_price,
57                     sum(l.quantity) as num_qty,
58                     sum(l.quantity * l.price_unit) as total,
59                     sum(l.quantity * product.list_price) as sale_expected,
60                     sum(l.quantity * product.standard_price) as normal_cost
61                 from account_invoice_line l
62                 left join account_invoice i on (l.invoice_id = i.id)
63                 left join product_template product on (product.id=l.product_id)
64                 where l.product_id = %s and i.state in %s and i.type IN %s and (i.date_invoice IS NULL or (i.date_invoice>=%s and i.date_invoice<=%s))
65                 """
66             invoice_types = ('out_invoice', 'in_refund')
67             cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to))
68             result = cr.fetchall()[0]
69             res[val.id]['sale_avg_price'] = result[0] and result[0] or 0.0
70             res[val.id]['sale_num_invoiced'] = result[1] and result[1] or 0.0
71             res[val.id]['turnover'] = result[2] and result[2] or 0.0
72             res[val.id]['sale_expected'] = result[3] and result[3] or 0.0
73             res[val.id]['sales_gap'] = res[val.id]['sale_expected']-res[val.id]['turnover']
74
75             invoice_types = ('in_invoice', 'out_refund')
76             cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to))
77             result = cr.fetchall()[0]
78             res[val.id]['purchase_avg_price'] = result[0] and result[0] or 0.0
79             res[val.id]['purchase_num_invoiced'] = result[1] and result[1] or 0.0
80             res[val.id]['total_cost'] = result[2] and result[2] or 0.0
81             res[val.id]['normal_cost'] = result[4] and result[4] or 0.0
82             res[val.id]['purchase_gap'] = res[val.id]['normal_cost']-res[val.id]['total_cost']
83
84             if 'total_margin' in field_names:
85                 res[val.id]['total_margin'] = res[val.id]['turnover'] - res[val.id]['total_cost']
86             if 'expected_margin' in field_names:
87                 res[val.id]['expected_margin'] = res[val.id]['sale_expected'] - res[val.id]['normal_cost']
88             if 'total_margin_rate' in field_names:
89                 res[val.id]['total_margin_rate'] = res[val.id]['turnover'] and (res[val.id]['total_margin'] * 100 / res[val.id]['turnover']) or 0.0
90             if 'expected_margin_rate' in field_names:
91                 res[val.id]['expected_margin_rate'] = res[val.id]['sale_expected'] and (res[val.id]['expected_margin'] * 100 / res[val.id]['sale_expected']) or 0.0
92         return res
93
94     _columns = {
95         'date_from': fields.function(_product_margin, method=True, type='date', string='From Date', multi='product_margin'),
96         'date_to': fields.function(_product_margin, method=True, type='date', string='To Date', multi='product_margin'),
97         'invoice_state': fields.function(_product_margin, method=True, type='selection', selection=[
98                 ('paid','Paid'),('open_paid','Open and Paid'),('draft_open_paid','Draft, Open and Paid')
99             ], string='Invoice State',multi='product_margin', readonly=True),
100         'sale_avg_price' : fields.function(_product_margin, method=True, type='float', string='Avg. Unit Price', multi='product_margin',
101             help="Avg. Price in Customer Invoices)"),
102         'purchase_avg_price' : fields.function(_product_margin, method=True, type='float', string='Avg. Unit Price', multi='product_margin',
103             help="Avg. Price in Supplier Invoices "),
104         'sale_num_invoiced' : fields.function(_product_margin, method=True, type='float', string='# Invoiced', multi='product_margin',
105             help="Sum of Quantity in Customer Invoices"),
106         'purchase_num_invoiced' : fields.function(_product_margin, method=True, type='float', string='# Invoiced', multi='product_margin',
107             help="Sum of Quantity in Supplier Invoices"),
108         'sales_gap' : fields.function(_product_margin, method=True, type='float', string='Sales Gap', multi='product_margin',
109             help="Expected Sale - Turn Over"),
110         'purchase_gap' : fields.function(_product_margin, method=True, type='float', string='Purchase Gap', multi='product_margin',
111             help="Normal Cost - Total Cost"),
112         'turnover' : fields.function(_product_margin, method=True, type='float', string='Turnover' ,multi='product_margin',
113             help="Sum of Multification of Invoice price and quantity of Customer Invoices"),
114         'total_cost'  : fields.function(_product_margin, method=True, type='float', string='Total Cost', multi='product_margin',
115             help="Sum of Multification of Invoice price and quantity of Supplier Invoices "),
116         'sale_expected' :  fields.function(_product_margin, method=True, type='float', string='Expected Sale', multi='product_margin',
117             help="Sum of Multification of Sale Catalog price and quantity of Customer Invoices"),
118         'normal_cost'  : fields.function(_product_margin, method=True, type='float', string='Normal Cost', multi='product_margin',
119             help="Sum of Multification of Cost price and quantity of Supplier Invoices"),
120         'total_margin' : fields.function(_product_margin, method=True, type='float', string='Total Margin', multi='product_margin',
121             help="Turnorder - Standard price"),
122         'expected_margin' : fields.function(_product_margin, method=True, type='float', string='Expected Margin', multi='product_margin',
123             help="Expected Sale - Normal Cost"),
124         'total_margin_rate' : fields.function(_product_margin, method=True, type='float', string='Total Margin (%)', multi='product_margin',
125             help="Total margin * 100 / Turnover"),
126         'expected_margin_rate' : fields.function(_product_margin, method=True, type='float', string='Expected Margin (%)', multi='product_margin',
127             help="Expected margin * 100 / Expected Sale"),
128     }
129
130 product_product()
131
132 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: