[MERGE] Sync with trunk
[odoo/odoo.git] / addons / product_visible_discount / product_visible_discount.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 from openerp.osv import fields, osv
24 from openerp.tools.translate import _
25
26 class product_pricelist(osv.osv):
27     _inherit = 'product.pricelist'
28
29     _columns ={
30         'visible_discount': fields.boolean('Visible Discount'),
31     }
32     _defaults = {
33          'visible_discount': True,
34     }
35
36
37 class sale_order_line(osv.osv):
38     _inherit = "sale.order.line"
39
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):
44
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             field_name = 'list_price'
50
51             if res_dict.get('item_id',False) and res_dict['item_id'].get(pricelist,False):
52                 item = res_dict['item_id'].get(pricelist,False)
53                 item_base = item_obj.read(cr, uid, [item], ['base'])[0]['base']
54                 if item_base > 0:
55                     field_name = price_type_obj.browse(cr, uid, item_base).field
56
57             product = product_obj.browse(cr, uid, product_id, context)
58             product_read = product_obj.read(cr, uid, product_id, [field_name], context=context)
59
60             factor = 1.0
61             if uom and uom != product.uom_id.id:
62                 product_uom_obj = self.pool.get('product.uom')
63                 uom_data = product_uom_obj.browse(cr, uid,  product.uom_id.id)
64                 factor = uom_data.factor
65             return product_read[field_name] * factor
66
67
68         res=super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty,
69             uom, qty_uos, uos, name, partner_id,
70             lang, update_tax, date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
71
72         context = {'lang': lang, 'partner_id': partner_id}
73         result=res['value']
74         pricelist_obj=self.pool.get('product.pricelist')
75         product_obj = self.pool.get('product.product')
76         if product:
77             if result.get('price_unit',False):
78                 price=result['price_unit']
79             else:
80                 return res
81
82             product = product_obj.browse(cr, uid, product, context)
83             list_price = pricelist_obj.price_get(cr, uid, [pricelist],
84                     product.id, qty or 1.0, partner_id, {'uom': uom,'date': date_order })
85
86             pricelists = pricelist_obj.read(cr,uid,[pricelist],['visible_discount'])
87
88             new_list_price = get_real_price(list_price, product.id, qty, uom, pricelist)
89             if(len(pricelists)>0 and pricelists[0]['visible_discount'] and list_price[pricelist] != 0):
90                 discount = (new_list_price - price) / new_list_price * 100
91                 result['price_unit'] = new_list_price
92                 result['discount'] = discount
93             else:
94                 result['discount'] = 0.0
95         return res
96
97
98 class account_invoice_line(osv.osv):
99     _inherit = "account.invoice.line"
100
101     def product_id_change(self, cr, uid, ids, product, uom_id, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, currency_id=False, context=None, company_id=None):
102         res = super(account_invoice_line, self).product_id_change(cr, uid, ids, product, uom_id, qty, name, type, partner_id, fposition_id, price_unit,currency_id, context=context, company_id=company_id)
103
104         def get_real_price(res_dict, product_id, qty, uom_id, pricelist):
105             item_obj = self.pool.get('product.pricelist.item')
106             price_type_obj = self.pool.get('product.price.type')
107             product_obj = self.pool.get('product.product')
108             field_name = 'list_price'
109
110             if res_dict.get('item_id',False) and res_dict['item_id'].get(pricelist,False):
111                 item = res_dict['item_id'].get(pricelist,False)
112                 item_read = item_obj.read(cr, uid, [item], ['base'])
113                 if item_read:
114                     item_base = item_read[0]['base']
115                     if item_base > 0:
116                         field_name = price_type_obj.browse(cr, uid, item_base).field
117
118             product = product_obj.browse(cr, uid, product_id, context)
119             product_read = product_obj.read(cr, uid, product_id, [field_name], context=context)
120
121             factor = 1.0
122             if uom_id and uom_id != product.uom_id.id:
123                 product_uom_obj = self.pool.get('product.uom')
124                 uom_data = product_uom_obj.browse(cr, uid,  product.uom_id.id)
125                 factor = uom_data.factor
126             return product_read[field_name] * factor
127
128         if product:
129             pricelist_obj = self.pool.get('product.pricelist')
130             partner_obj = self.pool.get('res.partner')
131             product = self.pool.get('product.product').browse(cr, uid, product, context=context)
132             result = res['value']
133             pricelist = False
134             real_price = 0.00
135             if type in ('in_invoice', 'in_refund'):
136                 if not price_unit and partner_id:
137                     pricelist =partner_obj.browse(cr, uid, partner_id).property_product_pricelist_purchase.id
138                     if not pricelist:
139                         raise osv.except_osv(_('No Purchase Pricelist Found!'),_("You must first define a pricelist on the supplier form!"))
140                     price_unit_res = pricelist_obj.price_get(cr, uid, [pricelist], product.id, qty or 1.0, partner_id, {'uom': uom_id})
141                     price_unit = price_unit_res[pricelist]
142                     real_price = get_real_price(price_unit_res, product.id, qty, uom_id, pricelist)
143             else:
144                 if partner_id:
145                     pricelist = partner_obj.browse(cr, uid, partner_id).property_product_pricelist.id
146                     if not pricelist:
147                         raise osv.except_osv(_('No Sale Pricelist Found!'),_("You must first define a pricelist on the customer form!"))
148                     price_unit_res = pricelist_obj.price_get(cr, uid, [pricelist], product.id, qty or 1.0, partner_id, {'uom': uom_id})
149                     price_unit = price_unit_res[pricelist]
150
151                     real_price = get_real_price(price_unit_res, product.id, qty, uom_id, pricelist)
152             if pricelist:
153                 pricelists=pricelist_obj.read(cr,uid,[pricelist],['visible_discount'])
154                 if(len(pricelists)>0 and pricelists[0]['visible_discount'] and real_price != 0):
155                     discount=(real_price-price_unit) / real_price * 100
156                     result['price_unit'] = real_price
157                     result['discount'] = discount
158                 else:
159                     result['discount']=0.0
160         return res
161
162
163 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: