2010-06-03 09:47:02 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2010-10-28 06:54:18 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2010-06-03 09:47:02 +00:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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
|
2010-10-28 06:54:18 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2010-06-03 09:47:02 +00:00
|
|
|
#
|
2010-10-28 06:54:18 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2010-06-03 09:47:02 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
2010-08-13 07:28:43 +00:00
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.osv import fields, osv
|
|
|
|
from openerp.tools.translate import _
|
2010-06-03 09:47:02 +00:00
|
|
|
|
|
|
|
class product_pricelist(osv.osv):
|
|
|
|
_inherit = 'product.pricelist'
|
2010-06-10 04:11:30 +00:00
|
|
|
|
2010-06-03 09:47:02 +00:00
|
|
|
_columns ={
|
2010-08-13 07:28:43 +00:00
|
|
|
'visible_discount': fields.boolean('Visible Discount'),
|
2010-06-03 09:47:02 +00:00
|
|
|
}
|
|
|
|
_defaults = {
|
2010-06-10 04:11:30 +00:00
|
|
|
'visible_discount': True,
|
2010-08-13 07:28:43 +00:00
|
|
|
}
|
2010-06-10 04:11:30 +00:00
|
|
|
|
2010-06-03 09:47:02 +00:00
|
|
|
|
|
|
|
class sale_order_line(osv.osv):
|
|
|
|
_inherit = "sale.order.line"
|
|
|
|
|
|
|
|
def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
|
|
|
|
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
|
2011-09-12 09:16:30 +00:00
|
|
|
lang=False, update_tax=True, date_order=False, packaging=False,
|
2011-07-18 09:53:44 +00:00
|
|
|
fiscal_position=False, flag=False, context=None):
|
2010-06-10 04:11:30 +00:00
|
|
|
|
2010-11-03 12:38:55 +00:00
|
|
|
def get_real_price(res_dict, product_id, qty, uom, pricelist):
|
2010-06-21 12:28:39 +00:00
|
|
|
item_obj = self.pool.get('product.pricelist.item')
|
|
|
|
price_type_obj = self.pool.get('product.price.type')
|
|
|
|
product_obj = self.pool.get('product.product')
|
|
|
|
field_name = 'list_price'
|
|
|
|
|
2010-11-03 12:38:55 +00:00
|
|
|
product = product_obj.browse(cr, uid, product_id, context)
|
2014-07-06 14:44:26 +00:00
|
|
|
product_read = product_obj.read(cr, uid, [product_id], [field_name], context=context)[0]
|
2011-09-12 09:16:30 +00:00
|
|
|
|
2010-11-03 12:38:55 +00:00
|
|
|
factor = 1.0
|
|
|
|
if uom and uom != product.uom_id.id:
|
|
|
|
product_uom_obj = self.pool.get('product.uom')
|
|
|
|
uom_data = product_uom_obj.browse(cr, uid, product.uom_id.id)
|
|
|
|
factor = uom_data.factor
|
|
|
|
return product_read[field_name] * factor
|
2010-06-21 12:28:39 +00:00
|
|
|
|
|
|
|
|
2010-06-03 09:47:02 +00:00
|
|
|
res=super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty,
|
|
|
|
uom, qty_uos, uos, name, partner_id,
|
2011-09-12 09:16:30 +00:00
|
|
|
lang, update_tax, date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
|
2010-06-03 09:47:02 +00:00
|
|
|
|
|
|
|
context = {'lang': lang, 'partner_id': partner_id}
|
|
|
|
result=res['value']
|
|
|
|
pricelist_obj=self.pool.get('product.pricelist')
|
|
|
|
product_obj = self.pool.get('product.product')
|
2014-08-02 01:32:09 +00:00
|
|
|
if product and pricelist:
|
2010-06-03 09:47:02 +00:00
|
|
|
if result.get('price_unit',False):
|
|
|
|
price=result['price_unit']
|
|
|
|
else:
|
|
|
|
return res
|
|
|
|
|
|
|
|
product = product_obj.browse(cr, uid, product, context)
|
|
|
|
list_price = pricelist_obj.price_get(cr, uid, [pricelist],
|
2010-06-21 12:28:39 +00:00
|
|
|
product.id, qty or 1.0, partner_id, {'uom': uom,'date': date_order })
|
2010-06-03 09:47:02 +00:00
|
|
|
|
2014-08-01 10:43:31 +00:00
|
|
|
so_pricelist = pricelist_obj.browse(cr, uid, pricelist, context=context)
|
2010-06-03 09:47:02 +00:00
|
|
|
|
2010-11-03 12:38:55 +00:00
|
|
|
new_list_price = get_real_price(list_price, product.id, qty, uom, pricelist)
|
2014-08-01 10:43:31 +00:00
|
|
|
if so_pricelist.visible_discount and list_price[pricelist] != 0 and new_list_price != 0:
|
2014-08-02 01:32:09 +00:00
|
|
|
if product.company_id and so_pricelist.currency_id.id != product.company_id.currency_id.id:
|
2014-08-01 10:43:31 +00:00
|
|
|
# new_list_price is in company's currency while price in pricelist currency
|
2014-08-25 15:19:43 +00:00
|
|
|
ctx = context.copy()
|
|
|
|
ctx['date'] = date_order
|
2014-08-01 10:43:31 +00:00
|
|
|
new_list_price = self.pool['res.currency'].compute(cr, uid,
|
|
|
|
product.company_id.currency_id.id, so_pricelist.currency_id.id,
|
2014-08-25 15:19:43 +00:00
|
|
|
new_list_price, context=ctx)
|
2010-06-21 12:28:39 +00:00
|
|
|
discount = (new_list_price - price) / new_list_price * 100
|
2013-10-23 13:26:46 +00:00
|
|
|
if discount > 0:
|
2013-10-14 10:07:46 +00:00
|
|
|
result['price_unit'] = new_list_price
|
2010-06-10 04:11:30 +00:00
|
|
|
result['discount'] = discount
|
2010-06-03 09:47:02 +00:00
|
|
|
else:
|
2013-10-23 13:26:46 +00:00
|
|
|
result['discount'] = 0.0
|
|
|
|
else:
|
|
|
|
result['discount'] = 0.0
|
2014-08-02 01:32:09 +00:00
|
|
|
else:
|
|
|
|
result['discount'] = 0.0
|
2010-06-03 09:47:02 +00:00
|
|
|
return res
|