merged 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 osv import fields, osv
24 from osv.osv import except_osv
25 from tools.translate import _
26
27 class product_pricelist(osv.osv):
28     _inherit = 'product.pricelist'
29
30     _columns ={
31         'visible_discount': fields.boolean('Visible Discount'),
32     }
33     _defaults = {
34          'visible_discount': True,
35     }
36
37 product_pricelist()
38
39 class sale_order_line(osv.osv):
40     _inherit = "sale.order.line"
41
42     def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
43             uom=False, qty_uos=0, uos=False, name='', partner_id=False,
44             lang=False, update_tax=True,date_order=False,packaging=False,fiscal_position=False, flag=False):
45
46         def get_real_price(res_dict, product_id, qty, uom, 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             template_obj = self.pool.get('product.template')
51             field_name = 'list_price'
52
53             if res_dict.get('item_id',False) and res_dict['item_id'].get(pricelist,False):
54                 item = res_dict['item_id'].get(pricelist,False)
55                 item_base = item_obj.read(cr, uid, [item], ['base'])[0]['base']
56                 if item_base > 0:
57                     field_name = price_type_obj.browse(cr, uid, item_base).field
58
59             product = product_obj.browse(cr, uid, product_id, context)
60             product_tmpl_id = product.product_tmpl_id.id
61
62             product_read = template_obj.read(cr, uid, product_tmpl_id, [field_name], context)
63             
64             factor = 1.0
65             if uom and uom != product.uom_id.id:
66                 product_uom_obj = self.pool.get('product.uom')
67                 uom_data = product_uom_obj.browse(cr, uid,  product.uom_id.id)
68                 factor = uom_data.factor
69             return product_read[field_name] * factor
70
71
72         res=super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty,
73             uom, qty_uos, uos, name, partner_id,
74             lang, update_tax,date_order,fiscal_position=fiscal_position,flag=flag)
75
76         context = {'lang': lang, 'partner_id': partner_id}
77         result=res['value']
78         pricelist_obj=self.pool.get('product.pricelist')
79         product_obj = self.pool.get('product.product')
80         product_uom_obj = self.pool.get('product.uom')
81         if product:
82             if result.get('price_unit',False):
83                 price=result['price_unit']
84             else:
85                 return res
86
87             product = product_obj.browse(cr, uid, product, context)
88             list_price = pricelist_obj.price_get(cr, uid, [pricelist],
89                     product.id, qty or 1.0, partner_id, {'uom': uom,'date': date_order })
90
91             pricelists = pricelist_obj.read(cr,uid,[pricelist],['visible_discount'])
92
93             old_uom = product.uos_id or product.uom_id
94             new_list_price = get_real_price(list_price, product.id, qty, uom, pricelist)
95             if(len(pricelists)>0 and pricelists[0]['visible_discount'] and list_price[pricelist] != 0):
96                 discount = (new_list_price - price) / new_list_price * 100
97                 result['price_unit'] = new_list_price
98                 result['discount'] = discount
99             else:
100                 result['discount'] = 0.0
101         return res
102
103 sale_order_line()
104
105 class account_invoice_line(osv.osv):
106     _inherit = "account.invoice.line"
107
108     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):
109         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)
110
111         def get_real_price(res_dict, product_id, qty, uom, pricelist):
112             item_obj = self.pool.get('product.pricelist.item')
113             price_type_obj = self.pool.get('product.price.type')
114             product_obj = self.pool.get('product.product')
115             template_obj = self.pool.get('product.template')
116             field_name = 'list_price'
117
118             if res_dict.get('item_id',False) and res_dict['item_id'].get(pricelist,False):
119                 item = res_dict['item_id'].get(pricelist,False)
120                 item_base = item_obj.read(cr, uid, [item], ['base'])[0]['base']
121                 if item_base > 0:
122                     field_name = price_type_obj.browse(cr, uid, item_base).field
123
124             product = product_obj.browse(cr, uid, product_id, context)
125             product_tmpl_id = product.product_tmpl_id.id
126
127             product_read = template_obj.read(cr, uid, product_tmpl_id, [field_name], context)
128
129             factor = 1.0
130             if uom and uom != product.uom_id.id:
131                 product_uom_obj = self.pool.get('product.uom')
132                 uom_data = product_uom_obj.browse(cr, uid,  product.uom_id.id)
133                 factor = uom_data.factor
134             return product_read[field_name] * factor
135
136         if product:
137             pricelist_obj = self.pool.get('product.pricelist')
138             partner_obj = self.pool.get('res.partner')
139             product = self.pool.get('product.product').browse(cr, uid, product, context=context)
140             result = res['value']
141             pricelist = False
142             real_price = 0.00
143             if type in ('in_invoice', 'in_refund'):
144                 if not price_unit and partner_id:
145                     pricelist =partner_obj.browse(cr, uid, partner_id).property_product_pricelist_purchase.id
146                     if not pricelist:
147                         raise osv.except_osv(_('No Purchase Pricelist Found !'),_("You must first define a pricelist for Supplier !"))
148                     price_unit_res = pricelist_obj.price_get(cr, uid, [pricelist], product.id, qty or 1.0, partner_id, {'uom': uom})[pricelist]
149                     price_unit = price_unit_res[pricelist]
150                     real_price = get_real_price(price_unit_res, product.id, qty, uom, pricelist)
151             else:
152                 if partner_id:
153                     pricelist = partner_obj.browse(cr, uid, partner_id).property_product_pricelist.id
154                     if not pricelist:
155                         raise osv.except_osv(_('No Sale Pricelist Found '),_("You must first define a pricelist for Customer !"))
156                     price_unit_res = pricelist_obj.price_get(cr, uid, [pricelist], product.id, qty or 1.0, partner_id, {'uom': uom})
157                     price_unit = price_unit_res[pricelist]
158
159                     real_price = get_real_price(price_unit_res, product.id, qty, uom, pricelist)
160             if pricelist:
161                 pricelists=pricelist_obj.read(cr,uid,[pricelist],['visible_discount'])
162                 if(len(pricelists)>0 and pricelists[0]['visible_discount'] and real_price != 0):
163                     discount=(real_price-price_unit) / real_price * 100
164                     result['price_unit'] = real_price
165                     result['discount'] = discount
166                 else:
167                     result['discount']=0.0
168         return res
169
170 account_invoice_line()
171
172 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: