Task-ID 880,881,883, merge sale_margin,move product_visible_discount ,merge account_i...
[odoo/odoo.git] / addons / product_visible_discount / product.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 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 General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22 from osv import fields, osv
23 import pooler
24 from tools import config
25 import time
26 from osv.osv import except_osv
27 from tools.translate import _
28
29 class product_pricelist(osv.osv):
30     _name = 'product.pricelist'
31     _inherit = 'product.pricelist'
32     _columns ={
33         'visible_discount':fields.boolean('Visible Discount'),
34     }
35
36     _defaults = {
37          'visible_discount':lambda *a: True,
38    }
39 product_pricelist()
40
41 class sale_order_line(osv.osv):
42     _name = "sale.order.line"
43     _inherit = "sale.order.line"
44
45     def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
46             uom=False, qty_uos=0, uos=False, name='', partner_id=False,
47             lang=False, update_tax=True,date_order=False,packaging=False,fiscal_position=False, flag=False):
48         res=super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty,
49             uom, qty_uos, uos, name, partner_id,
50             lang, update_tax,date_order,fiscal_position=fiscal_position,flag=flag)
51
52         context = {'lang': lang, 'partner_id': partner_id}
53         result=res['value']
54         pricelist_obj=self.pool.get('product.pricelist')
55         product_obj = self.pool.get('product.product')
56         product_uom_obj = self.pool.get('product.uom')
57         if product:
58             if result.get('price_unit',False):
59                 price=result['price_unit']
60             else:
61                 return res
62
63             product = product_obj.browse(cr, uid, product, context)
64 #            product_tmpl_id = product.product_tmpl_id.id
65 #            pricetype_id = pricelist_obj.browse(cr, uid, pricelist).version_id[0].items_id[0].base
66 #            field_name = 'list_price'
67 #            product_read = self.pool.get('product.template').read(cr, uid, product_tmpl_id, [field_name], context)
68 #            list_price = product_read[field_name]
69             list_price = pricelist_obj.price_get(cr, uid, [pricelist],
70                     product.id, qty or 1.0, partner_id, {'uom': uom,'date': date_order })[pricelist]
71
72             pricelists=pricelist_obj.read(cr,uid,[pricelist],['visible_discount'])
73
74             old_uom = product.uos_id or product.uom_id
75             new_list_price = product_uom_obj._compute_price(cr,
76                         uid, old_uom.id, list_price, uom)
77             if(len(pricelists)>0 and pricelists[0]['visible_discount'] and list_price != 0):
78                 discount=(new_list_price-price) / new_list_price * 100
79                 result['price_unit']=new_list_price
80                 result['discount']=discount
81             else:
82                 result['discount']=0.0
83         return res
84
85
86
87 sale_order_line()
88
89 class account_invoice_line(osv.osv):
90     _name = "account.invoice.line"
91     _inherit = "account.invoice.line"
92
93     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={}):
94         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)
95
96
97         def get_real_price(res_dict, product_id, pricelist):
98             item_obj = self.pool.get('product.pricelist.item')
99             price_type_obj = self.pool.get('product.price.type')
100             product_obj=self.pool.get('product.product')
101             template_obj=self.pool.get('product.template')
102             field_name= 'list_price'
103             
104             if res_dict.get('item_id',False) and res_dict['item_id'].get(pricelist,False):
105                 item = res_dict['item_id'].get(pricelist,False)
106                 item_base = item_obj.read(cr, uid, [item], ['base'])[0]['base']
107                 if item_base > 0:
108                     field_name = price_type_obj.browse(cr, uid, item_base).field
109
110             product_tmpl_id = product_obj.browse(cr, uid, product_id, context).product_tmpl_id.id
111                 
112             product_read = template_obj.read(cr, uid, product_tmpl_id, [field_name], context)
113             return product_read[field_name]
114
115         
116         if product:
117             product = self.pool.get('product.product').browse(cr, uid, product, context=context)
118             pricelist_obj=self.pool.get('product.pricelist')
119             partner_obj=self.pool.get('res.partner')
120             result=res['value']
121             pricelist = False
122             real_price = 0.00
123             if type in ('in_invoice', 'in_refund'):
124                 if not price_unit and partner_id:
125                     pricelist =partner_obj.browse(cr, uid, partner_id).property_product_pricelist_purchase.id
126                     if not pricelist:
127                         raise osv.except_osv(_('No Purchase Pricelist Found !'),_("You must first define a pricelist for Supplier !"))
128                     price_unit_res = pricelist_obj.price_get(cr, uid, [pricelist], product.id, qty or 1.0, partner_id, {'uom': uom})[pricelist]
129                     price_unit = price_unit_res[pricelist]
130                     real_price= get_real_price(price_unit_res, product.id, pricelist)
131             else:
132                 if partner_id:
133                     pricelist =partner_obj.browse(cr, uid, partner_id).property_product_pricelist.id
134                     if not pricelist:
135                         raise osv.except_osv(_('No Sale Pricelist Found '),_("You must first define a pricelist for Customer !"))
136                     price_unit_res = pricelist_obj.price_get(cr, uid, [pricelist], product.id, qty or 1.0, partner_id, {'uom': uom})
137                     price_unit = price_unit_res[pricelist]
138                     
139                     real_price = get_real_price(price_unit_res, product.id, pricelist)
140             if pricelist:
141                 pricelists=pricelist_obj.read(cr,uid,[pricelist],['visible_discount'])
142                 if(len(pricelists)>0 and pricelists[0]['visible_discount'] and real_price != 0):
143                     discount=(real_price-price_unit) / real_price * 100
144                     result['price_unit']=real_price
145                     result['discount']=discount
146                 else:
147                     result['discount']=0.0
148         return res
149
150
151 account_invoice_line()
152 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
153