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 osv import fields, osv
24 from tools.translate import _
26 class product_pricelist(osv.osv):
27 _inherit = 'product.pricelist'
30 'visible_discount': fields.boolean('Visible Discount'),
33 'visible_discount': True,
38 class sale_order_line(osv.osv):
39 _inherit = "sale.order.line"
41 def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
42 uom=False, qty_uos=0, uos=False, name='', partner_id=False,
43 lang=False, update_tax=True,date_order=False,packaging=False,fiscal_position=False, flag=False):
45 def get_real_price(res_dict, product_id, qty, uom, pricelist):
46 item_obj = self.pool.get('product.pricelist.item')
47 price_type_obj = self.pool.get('product.price.type')
48 product_obj = self.pool.get('product.product')
49 template_obj = self.pool.get('product.template')
50 field_name = 'list_price'
52 if res_dict.get('item_id',False) and res_dict['item_id'].get(pricelist,False):
53 item = res_dict['item_id'].get(pricelist,False)
54 item_base = item_obj.read(cr, uid, [item], ['base'])[0]['base']
56 field_name = price_type_obj.browse(cr, uid, item_base).field
58 product = product_obj.browse(cr, uid, product_id, context)
59 product_tmpl_id = product.product_tmpl_id.id
61 product_read = template_obj.read(cr, uid, product_tmpl_id, [field_name], context)
64 if uom and uom != product.uom_id.id:
65 product_uom_obj = self.pool.get('product.uom')
66 uom_data = product_uom_obj.browse(cr, uid, product.uom_id.id)
67 factor = uom_data.factor
68 return product_read[field_name] * factor
71 res=super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty,
72 uom, qty_uos, uos, name, partner_id,
73 lang, update_tax,date_order,fiscal_position=fiscal_position,flag=flag)
75 context = {'lang': lang, 'partner_id': partner_id}
77 pricelist_obj=self.pool.get('product.pricelist')
78 product_obj = self.pool.get('product.product')
80 if result.get('price_unit',False):
81 price=result['price_unit']
85 product = product_obj.browse(cr, uid, product, context)
86 list_price = pricelist_obj.price_get(cr, uid, [pricelist],
87 product.id, qty or 1.0, partner_id, {'uom': uom,'date': date_order })
89 pricelists = pricelist_obj.read(cr,uid,[pricelist],['visible_discount'])
91 new_list_price = get_real_price(list_price, product.id, qty, uom, pricelist)
92 if(len(pricelists)>0 and pricelists[0]['visible_discount'] and list_price[pricelist] != 0):
93 discount = (new_list_price - price) / new_list_price * 100
94 result['price_unit'] = new_list_price
95 result['discount'] = discount
97 result['discount'] = 0.0
102 class account_invoice_line(osv.osv):
103 _inherit = "account.invoice.line"
105 def product_id_change(self, cr, uid, ids, product, uom, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, address_invoice_id=False, currency_id=False, context=None):
106 res = super(account_invoice_line, self).product_id_change(cr, uid, ids, product, uom, qty, name, type, partner_id, fposition_id, price_unit, address_invoice_id, currency_id, context=context)
108 def get_real_price(res_dict, product_id, qty, uom, pricelist):
109 item_obj = self.pool.get('product.pricelist.item')
110 price_type_obj = self.pool.get('product.price.type')
111 product_obj = self.pool.get('product.product')
112 template_obj = self.pool.get('product.template')
113 field_name = 'list_price'
115 if res_dict.get('item_id',False) and res_dict['item_id'].get(pricelist,False):
116 item = res_dict['item_id'].get(pricelist,False)
117 item_base = item_obj.read(cr, uid, [item], ['base'])[0]['base']
119 field_name = price_type_obj.browse(cr, uid, item_base).field
121 product = product_obj.browse(cr, uid, product_id, context)
122 product_tmpl_id = product.product_tmpl_id.id
124 product_read = template_obj.read(cr, uid, product_tmpl_id, [field_name], context)
127 if uom and uom != product.uom_id.id:
128 product_uom_obj = self.pool.get('product.uom')
129 uom_data = product_uom_obj.browse(cr, uid, product.uom_id.id)
130 factor = uom_data.factor
131 return product_read[field_name] * factor
134 pricelist_obj = self.pool.get('product.pricelist')
135 partner_obj = self.pool.get('res.partner')
136 product = self.pool.get('product.product').browse(cr, uid, product, context=context)
137 result = res['value']
140 if type in ('in_invoice', 'in_refund'):
141 if not price_unit and partner_id:
142 pricelist =partner_obj.browse(cr, uid, partner_id).property_product_pricelist_purchase.id
144 raise osv.except_osv(_('No Purchase Pricelist Found !'),_("You must first define a pricelist for Supplier !"))
145 price_unit_res = pricelist_obj.price_get(cr, uid, [pricelist], product.id, qty or 1.0, partner_id, {'uom': uom})
146 price_unit = price_unit_res[pricelist]
147 real_price = get_real_price(price_unit_res, product.id, qty, uom, pricelist)
150 pricelist = partner_obj.browse(cr, uid, partner_id).property_product_pricelist.id
152 raise osv.except_osv(_('No Sale Pricelist Found '),_("You must first define a pricelist for Customer !"))
153 price_unit_res = pricelist_obj.price_get(cr, uid, [pricelist], product.id, qty or 1.0, partner_id, {'uom': uom})
154 price_unit = price_unit_res[pricelist]
156 real_price = get_real_price(price_unit_res, product.id, qty, uom, pricelist)
158 pricelists=pricelist_obj.read(cr,uid,[pricelist],['visible_discount'])
159 if(len(pricelists)>0 and pricelists[0]['visible_discount'] and real_price != 0):
160 discount=(real_price-price_unit) / real_price * 100
161 result['price_unit'] = real_price
162 result['discount'] = discount
164 result['discount']=0.0
167 account_invoice_line()
169 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: