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 ##############################################################################
24 from openerp.osv import fields, osv
27 class product_product(osv.osv):
28 _inherit = "product.product"
30 def _product_margin(self, cr, uid, ids, field_names, arg, context=None):
35 for val in self.browse(cr, uid, ids, context=context):
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
48 if invoice_state == 'paid':
50 elif invoice_state == 'open_paid':
51 states = ('open', 'paid')
52 elif invoice_state == 'draft_open_paid':
53 states = ('draft', 'open', 'paid')
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_subtotal/l.quantity)) as total,
59 sum(l.quantity * pt.list_price) as sale_expected,
60 sum(l.quantity * pt.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_product product on (product.id=l.product_id)
64 left join product_template pt on (pt.id=product.product_tmpl_id)
65 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))
67 invoice_types = ('out_invoice', 'in_refund')
68 cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to))
69 result = cr.fetchall()[0]
70 res[val.id]['sale_avg_price'] = result[0] and result[0] or 0.0
71 res[val.id]['sale_num_invoiced'] = result[1] and result[1] or 0.0
72 res[val.id]['turnover'] = result[2] and result[2] or 0.0
73 res[val.id]['sale_expected'] = result[3] and result[3] or 0.0
74 res[val.id]['sales_gap'] = res[val.id]['sale_expected']-res[val.id]['turnover']
76 invoice_types = ('in_invoice', 'out_refund')
77 cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to))
78 result = cr.fetchall()[0]
79 res[val.id]['purchase_avg_price'] = result[0] and result[0] or 0.0
80 res[val.id]['purchase_num_invoiced'] = result[1] and result[1] or 0.0
81 res[val.id]['total_cost'] = result[2] and result[2] or 0.0
82 res[val.id]['normal_cost'] = result[4] and result[4] or 0.0
83 res[val.id]['purchase_gap'] = res[val.id]['normal_cost']-res[val.id]['total_cost']
85 if 'total_margin' in field_names:
86 res[val.id]['total_margin'] = res[val.id]['turnover'] - res[val.id]['total_cost']
87 if 'expected_margin' in field_names:
88 res[val.id]['expected_margin'] = res[val.id]['sale_expected'] - res[val.id]['normal_cost']
89 if 'total_margin_rate' in field_names:
90 res[val.id]['total_margin_rate'] = res[val.id]['turnover'] and res[val.id]['total_margin'] * 100 / res[val.id]['turnover'] or 0.0
91 if 'expected_margin_rate' in field_names:
92 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
96 'date_from': fields.function(_product_margin, type='date', string='Margin Date From', multi='product_margin'),
97 'date_to': fields.function(_product_margin, type='date', string='Margin Date To', multi='product_margin'),
98 'invoice_state': fields.function(_product_margin, type='selection', selection=[
99 ('paid','Paid'),('open_paid','Open and Paid'),('draft_open_paid','Draft, Open and Paid')
100 ], string='Invoice State',multi='product_margin', readonly=True),
101 'sale_avg_price' : fields.function(_product_margin, type='float', string='Avg. Unit Price', multi='product_margin',
102 help="Avg. Price in Customer Invoices."),
103 'purchase_avg_price' : fields.function(_product_margin, type='float', string='Avg. Unit Price', multi='product_margin',
104 help="Avg. Price in Supplier Invoices "),
105 'sale_num_invoiced' : fields.function(_product_margin, type='float', string='# Invoiced in Sale', multi='product_margin',
106 help="Sum of Quantity in Customer Invoices"),
107 'purchase_num_invoiced' : fields.function(_product_margin, type='float', string='# Invoiced in Purchase', multi='product_margin',
108 help="Sum of Quantity in Supplier Invoices"),
109 'sales_gap' : fields.function(_product_margin, type='float', string='Sales Gap', multi='product_margin',
110 help="Expected Sale - Turn Over"),
111 'purchase_gap' : fields.function(_product_margin, type='float', string='Purchase Gap', multi='product_margin',
112 help="Normal Cost - Total Cost"),
113 'turnover' : fields.function(_product_margin, type='float', string='Turnover' ,multi='product_margin',
114 help="Sum of Multiplication of Invoice price and quantity of Customer Invoices"),
115 'total_cost' : fields.function(_product_margin, type='float', string='Total Cost', multi='product_margin',
116 help="Sum of Multiplication of Invoice price and quantity of Supplier Invoices "),
117 'sale_expected' : fields.function(_product_margin, type='float', string='Expected Sale', multi='product_margin',
118 help="Sum of Multiplication of Sale Catalog price and quantity of Customer Invoices"),
119 'normal_cost' : fields.function(_product_margin, type='float', string='Normal Cost', multi='product_margin',
120 help="Sum of Multiplication of Cost price and quantity of Supplier Invoices"),
121 'total_margin' : fields.function(_product_margin, type='float', string='Total Margin', multi='product_margin',
122 help="Turnover - Standard price"),
123 'expected_margin' : fields.function(_product_margin, type='float', string='Expected Margin', multi='product_margin',
124 help="Expected Sale - Normal Cost"),
125 'total_margin_rate' : fields.function(_product_margin, type='float', string='Total Margin Rate(%)', multi='product_margin',
126 help="Total margin * 100 / Turnover"),
127 'expected_margin_rate' : fields.function(_product_margin, type='float', string='Expected Margin (%)', multi='product_margin',
128 help="Expected margin * 100 / Expected Sale"),
132 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: