1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero 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.
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 Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from openerp.osv import fields, osv
24 from openerp.tools.translate import _
26 class product_pricelist(osv.osv):
27 _inherit = 'product.pricelist'
30 'visible_discount': fields.boolean('Visible Discount'),
33 'visible_discount': True,
37 class sale_order_line(osv.osv):
38 _inherit = "sale.order.line"
40 def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
41 uom=False, qty_uos=0, uos=False, name='', partner_id=False,
42 lang=False, update_tax=True, date_order=False, packaging=False,
43 fiscal_position=False, flag=False, context=None):
45 def get_real_price(res_dict, product_id, qty, uom, pricelist):
46 """Retrieve the price before applying the pricelist"""
47 item_obj = self.pool.get('product.pricelist.item')
48 price_type_obj = self.pool.get('product.price.type')
49 product_obj = self.pool.get('product.product')
50 field_name = 'list_price'
52 product = product_obj.browse(cr, uid, product_id, context)
53 product_read = product_obj.read(cr, uid, product_id, [field_name], context=context)
56 if uom and uom != product.uom_id.id:
57 # the unit price is in a different uom
58 factor = self.pool['product.uom']._compute_qty(cr, uid, uom, 1.0, product.uom_id.id)
59 return product_read[field_name] * factor
62 res=super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty,
63 uom, qty_uos, uos, name, partner_id,
64 lang, update_tax, date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
66 context = {'lang': lang, 'partner_id': partner_id}
68 pricelist_obj=self.pool.get('product.pricelist')
69 product_obj = self.pool.get('product.product')
70 if product and pricelist:
71 if result.get('price_unit',False):
72 price=result['price_unit']
75 uom = result.get('product_uom', uom)
76 product = product_obj.browse(cr, uid, product, context)
77 list_price = pricelist_obj.price_get(cr, uid, [pricelist],
78 product.id, qty or 1.0, partner_id, {'uom': uom,'date': date_order })
80 so_pricelist = pricelist_obj.browse(cr, uid, pricelist, context=context)
82 new_list_price = get_real_price(list_price, product.id, qty, uom, pricelist)
83 if so_pricelist.visible_discount and list_price[pricelist] != 0 and new_list_price != 0:
84 if product.company_id and so_pricelist.currency_id.id != product.company_id.currency_id.id:
85 # new_list_price is in company's currency while price in pricelist currency
87 ctx['date'] = date_order
88 new_list_price = self.pool['res.currency'].compute(cr, uid,
89 product.company_id.currency_id.id, so_pricelist.currency_id.id,
90 new_list_price, context=ctx)
91 discount = (new_list_price - price) / new_list_price * 100
93 result['price_unit'] = new_list_price
94 result['discount'] = discount
96 result['discount'] = 0.0
98 result['discount'] = 0.0
100 result['discount'] = 0.0