2009-10-13 05:58:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-10-03 14:28:28 +00:00
|
|
|
##############################################################################
|
2009-11-24 11:15:43 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2010-01-12 09:18:39 +00:00
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
2008-10-03 14:28:28 +00:00
|
|
|
#
|
2008-11-03 19:18:56 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 11:15:34 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2008-10-03 14:28:28 +00:00
|
|
|
#
|
2008-11-03 19:18:56 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 11:15:34 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2008-10-03 14:28:28 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2009-11-24 11:15:43 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-10-03 14:28:28 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.osv import fields, osv
|
2010-08-13 07:28:43 +00:00
|
|
|
|
2010-09-28 12:28:45 +00:00
|
|
|
|
2008-10-03 14:28:28 +00:00
|
|
|
class product_product(osv.osv):
|
2009-11-24 11:15:43 +00:00
|
|
|
_inherit = "product.product"
|
2010-08-13 07:28:43 +00:00
|
|
|
|
2015-07-24 14:32:44 +00:00
|
|
|
def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):
|
|
|
|
"""
|
|
|
|
Inherit read_group to calculate the sum of the non-stored fields, as it is not automatically done anymore through the XML.
|
|
|
|
"""
|
|
|
|
res = super(product_product, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby, lazy=lazy)
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
fields_list = ['turnover', 'sale_avg_price', 'sale_purchase_price', 'sale_num_invoiced', 'purchase_num_invoiced',
|
|
|
|
'sales_gap', 'purchase_gap', 'total_cost', 'sale_expected', 'normal_cost', 'total_margin',
|
|
|
|
'expected_margin', 'total_margin_rate', 'expected_margin_rate']
|
|
|
|
if any(x in fields for x in fields_list):
|
|
|
|
# Calculate first for every product in which line it needs to be applied
|
|
|
|
re_ind = 0
|
|
|
|
prod_re = {}
|
|
|
|
tot_products = []
|
|
|
|
for re in res:
|
|
|
|
if re.get('__domain'):
|
|
|
|
products = self.search(cr, uid, re['__domain'], context=context)
|
|
|
|
tot_products += products
|
|
|
|
for prod in products:
|
|
|
|
prod_re[prod] = re_ind
|
|
|
|
re_ind += 1
|
|
|
|
|
|
|
|
res_val = self._product_margin(cr, uid, tot_products, [x for x in fields if fields in fields_list], '', context=context)
|
|
|
|
for key in res_val.keys():
|
|
|
|
for l in res_val[key].keys():
|
|
|
|
re = res[prod_re[key]]
|
|
|
|
if re.get(l):
|
|
|
|
re[l] += res_val[key][l]
|
|
|
|
else:
|
|
|
|
re[l] = res_val[key][l]
|
|
|
|
return res
|
|
|
|
|
2010-06-08 13:06:11 +00:00
|
|
|
def _product_margin(self, cr, uid, ids, field_names, arg, context=None):
|
2009-11-24 11:15:43 +00:00
|
|
|
res = {}
|
2010-08-13 07:28:43 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2010-09-28 12:28:45 +00:00
|
|
|
|
|
|
|
for val in self.browse(cr, uid, ids, context=context):
|
2008-10-03 14:28:28 +00:00
|
|
|
res[val.id] = {}
|
2010-08-13 07:28:43 +00:00
|
|
|
date_from = context.get('date_from', time.strftime('%Y-01-01'))
|
|
|
|
date_to = context.get('date_to', time.strftime('%Y-12-31'))
|
|
|
|
invoice_state = context.get('invoice_state', 'open_paid')
|
2008-10-03 14:28:28 +00:00
|
|
|
if 'date_from' in field_names:
|
2010-09-28 12:28:45 +00:00
|
|
|
res[val.id]['date_from'] = date_from
|
2008-10-03 14:28:28 +00:00
|
|
|
if 'date_to' in field_names:
|
2010-09-28 12:28:45 +00:00
|
|
|
res[val.id]['date_to'] = date_to
|
2008-10-03 14:28:28 +00:00
|
|
|
if 'invoice_state' in field_names:
|
2010-09-28 12:28:45 +00:00
|
|
|
res[val.id]['invoice_state'] = invoice_state
|
2010-08-13 07:28:43 +00:00
|
|
|
invoice_types = ()
|
|
|
|
states = ()
|
2010-09-28 12:28:45 +00:00
|
|
|
if invoice_state == 'paid':
|
|
|
|
states = ('paid',)
|
|
|
|
elif invoice_state == 'open_paid':
|
|
|
|
states = ('open', 'paid')
|
|
|
|
elif invoice_state == 'draft_open_paid':
|
|
|
|
states = ('draft', 'open', 'paid')
|
2013-05-28 14:25:50 +00:00
|
|
|
if "force_company" in context:
|
|
|
|
company_id = context['force_company']
|
|
|
|
else:
|
|
|
|
company_id = self.pool.get("res.users").browse(cr, uid, uid, context=context).company_id.id
|
2009-11-24 11:15:43 +00:00
|
|
|
|
2013-06-03 15:15:21 +00:00
|
|
|
#Cost price is calculated afterwards as it is a property
|
2011-03-09 12:35:38 +00:00
|
|
|
sqlstr="""select
|
2014-03-26 16:22:08 +00:00
|
|
|
sum(l.price_unit * l.quantity)/sum(nullif(l.quantity * pu.factor / pu2.factor,0)) as avg_unit_price,
|
2013-06-03 15:15:21 +00:00
|
|
|
sum(l.quantity * pu.factor / pu2.factor) as num_qty,
|
2013-04-29 10:58:59 +00:00
|
|
|
sum(l.quantity * (l.price_subtotal/(nullif(l.quantity,0)))) as total,
|
2013-06-03 15:15:21 +00:00
|
|
|
sum(l.quantity * pu.factor * pt.list_price / pu2.factor) as sale_expected
|
2008-10-03 14:28:28 +00:00
|
|
|
from account_invoice_line l
|
|
|
|
left join account_invoice i on (l.invoice_id = i.id)
|
2012-11-05 10:36:12 +00:00
|
|
|
left join product_product product on (product.id=l.product_id)
|
2015-07-24 14:32:44 +00:00
|
|
|
left join product_template pt on (pt.id = product.product_tmpl_id)
|
2013-06-03 15:15:21 +00:00
|
|
|
left join product_uom pu on (pt.uom_id = pu.id)
|
2013-06-05 08:56:26 +00:00
|
|
|
left join product_uom pu2 on (l.uos_id = pu2.id)
|
2013-05-28 14:25:50 +00:00
|
|
|
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 and i.company_id=%s))
|
2011-03-09 12:35:38 +00:00
|
|
|
"""
|
|
|
|
invoice_types = ('out_invoice', 'in_refund')
|
2013-05-28 14:25:50 +00:00
|
|
|
cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to, company_id))
|
2011-03-09 12:35:38 +00:00
|
|
|
result = cr.fetchall()[0]
|
|
|
|
res[val.id]['sale_avg_price'] = result[0] and result[0] or 0.0
|
|
|
|
res[val.id]['sale_num_invoiced'] = result[1] and result[1] or 0.0
|
|
|
|
res[val.id]['turnover'] = result[2] and result[2] or 0.0
|
|
|
|
res[val.id]['sale_expected'] = result[3] and result[3] or 0.0
|
|
|
|
res[val.id]['sales_gap'] = res[val.id]['sale_expected']-res[val.id]['turnover']
|
2013-05-28 14:25:50 +00:00
|
|
|
prod_obj = self.pool.get("product.product")
|
2014-03-26 16:22:08 +00:00
|
|
|
ctx = context.copy()
|
2013-05-28 14:25:50 +00:00
|
|
|
ctx['force_company'] = company_id
|
|
|
|
prod = prod_obj.browse(cr, uid, val.id, context=ctx)
|
2011-03-09 12:35:38 +00:00
|
|
|
invoice_types = ('in_invoice', 'out_refund')
|
2013-05-28 14:25:50 +00:00
|
|
|
cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to, company_id))
|
2011-03-09 12:35:38 +00:00
|
|
|
result = cr.fetchall()[0]
|
|
|
|
res[val.id]['purchase_avg_price'] = result[0] and result[0] or 0.0
|
|
|
|
res[val.id]['purchase_num_invoiced'] = result[1] and result[1] or 0.0
|
|
|
|
res[val.id]['total_cost'] = result[2] and result[2] or 0.0
|
2013-05-28 14:25:50 +00:00
|
|
|
res[val.id]['normal_cost'] = prod.standard_price * res[val.id]['purchase_num_invoiced']
|
|
|
|
res[val.id]['purchase_gap'] = res[val.id]['normal_cost'] - res[val.id]['total_cost']
|
2009-11-24 11:15:43 +00:00
|
|
|
|
2008-10-03 14:28:28 +00:00
|
|
|
if 'total_margin' in field_names:
|
2011-03-09 12:35:38 +00:00
|
|
|
res[val.id]['total_margin'] = res[val.id]['turnover'] - res[val.id]['total_cost']
|
2008-10-03 14:28:28 +00:00
|
|
|
if 'expected_margin' in field_names:
|
2011-03-09 12:35:38 +00:00
|
|
|
res[val.id]['expected_margin'] = res[val.id]['sale_expected'] - res[val.id]['normal_cost']
|
2008-10-03 14:28:28 +00:00
|
|
|
if 'total_margin_rate' in field_names:
|
2011-03-09 12:35:38 +00:00
|
|
|
res[val.id]['total_margin_rate'] = res[val.id]['turnover'] and res[val.id]['total_margin'] * 100 / res[val.id]['turnover'] or 0.0
|
2008-10-03 14:28:28 +00:00
|
|
|
if 'expected_margin_rate' in field_names:
|
2011-03-09 12:35:38 +00:00
|
|
|
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
|
2008-10-03 14:28:28 +00:00
|
|
|
return res
|
2009-11-24 11:15:43 +00:00
|
|
|
|
2008-10-03 14:28:28 +00:00
|
|
|
_columns = {
|
2012-12-06 07:14:10 +00:00
|
|
|
'date_from': fields.function(_product_margin, type='date', string='Margin Date From', multi='product_margin'),
|
|
|
|
'date_to': fields.function(_product_margin, type='date', string='Margin Date To', multi='product_margin'),
|
2011-07-01 23:41:24 +00:00
|
|
|
'invoice_state': fields.function(_product_margin, type='selection', selection=[
|
2010-09-28 12:28:45 +00:00
|
|
|
('paid','Paid'),('open_paid','Open and Paid'),('draft_open_paid','Draft, Open and Paid')
|
|
|
|
], string='Invoice State',multi='product_margin', readonly=True),
|
2011-07-01 23:41:24 +00:00
|
|
|
'sale_avg_price' : fields.function(_product_margin, type='float', string='Avg. Unit Price', multi='product_margin',
|
2012-07-13 10:17:51 +00:00
|
|
|
help="Avg. Price in Customer Invoices."),
|
2011-07-01 23:41:24 +00:00
|
|
|
'purchase_avg_price' : fields.function(_product_margin, type='float', string='Avg. Unit Price', multi='product_margin',
|
2010-09-28 12:28:45 +00:00
|
|
|
help="Avg. Price in Supplier Invoices "),
|
2012-12-06 07:14:10 +00:00
|
|
|
'sale_num_invoiced' : fields.function(_product_margin, type='float', string='# Invoiced in Sale', multi='product_margin',
|
2010-09-28 12:28:45 +00:00
|
|
|
help="Sum of Quantity in Customer Invoices"),
|
2012-12-06 07:14:10 +00:00
|
|
|
'purchase_num_invoiced' : fields.function(_product_margin, type='float', string='# Invoiced in Purchase', multi='product_margin',
|
2010-09-28 12:28:45 +00:00
|
|
|
help="Sum of Quantity in Supplier Invoices"),
|
2011-07-01 23:41:24 +00:00
|
|
|
'sales_gap' : fields.function(_product_margin, type='float', string='Sales Gap', multi='product_margin',
|
2010-09-28 12:28:45 +00:00
|
|
|
help="Expected Sale - Turn Over"),
|
2011-07-01 23:41:24 +00:00
|
|
|
'purchase_gap' : fields.function(_product_margin, type='float', string='Purchase Gap', multi='product_margin',
|
2010-09-28 12:28:45 +00:00
|
|
|
help="Normal Cost - Total Cost"),
|
2011-07-01 23:41:24 +00:00
|
|
|
'turnover' : fields.function(_product_margin, type='float', string='Turnover' ,multi='product_margin',
|
2012-07-20 09:26:53 +00:00
|
|
|
help="Sum of Multiplication of Invoice price and quantity of Customer Invoices"),
|
2011-07-01 23:41:24 +00:00
|
|
|
'total_cost' : fields.function(_product_margin, type='float', string='Total Cost', multi='product_margin',
|
2012-07-20 09:26:53 +00:00
|
|
|
help="Sum of Multiplication of Invoice price and quantity of Supplier Invoices "),
|
2011-07-01 23:41:24 +00:00
|
|
|
'sale_expected' : fields.function(_product_margin, type='float', string='Expected Sale', multi='product_margin',
|
2012-07-20 09:26:53 +00:00
|
|
|
help="Sum of Multiplication of Sale Catalog price and quantity of Customer Invoices"),
|
2011-07-01 23:41:24 +00:00
|
|
|
'normal_cost' : fields.function(_product_margin, type='float', string='Normal Cost', multi='product_margin',
|
2012-07-20 09:26:53 +00:00
|
|
|
help="Sum of Multiplication of Cost price and quantity of Supplier Invoices"),
|
2011-07-01 23:41:24 +00:00
|
|
|
'total_margin' : fields.function(_product_margin, type='float', string='Total Margin', multi='product_margin',
|
2012-07-20 09:26:53 +00:00
|
|
|
help="Turnover - Standard price"),
|
2011-07-01 23:41:24 +00:00
|
|
|
'expected_margin' : fields.function(_product_margin, type='float', string='Expected Margin', multi='product_margin',
|
2010-09-28 12:28:45 +00:00
|
|
|
help="Expected Sale - Normal Cost"),
|
2012-12-06 07:14:10 +00:00
|
|
|
'total_margin_rate' : fields.function(_product_margin, type='float', string='Total Margin Rate(%)', multi='product_margin',
|
2010-09-28 12:28:45 +00:00
|
|
|
help="Total margin * 100 / Turnover"),
|
2011-07-01 23:41:24 +00:00
|
|
|
'expected_margin_rate' : fields.function(_product_margin, type='float', string='Expected Margin (%)', multi='product_margin',
|
2010-09-28 12:28:45 +00:00
|
|
|
help="Expected margin * 100 / Expected Sale"),
|
2008-11-05 13:47:14 +00:00
|
|
|
}
|
2008-10-03 14:28:28 +00:00
|
|
|
|
|
|
|
|
2010-09-24 11:32:32 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|