2010-06-03 09:47:02 +00:00
##############################################################################
2010-06-10 04:11:30 +00:00
#
2010-06-03 09:47:02 +00:00
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
2010-06-10 04:11:30 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2010-06-03 09:47:02 +00:00
#
##############################################################################
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv
2010-06-03 09:47:02 +00:00
class sale_order_line ( osv . osv ) :
_inherit = " sale.order.line "
2010-06-22 10:18:03 +00:00
2010-06-24 09:45:18 +00:00
def product_id_change ( self , cr , uid , ids , pricelist , product , qty = 0 ,
uom = False , qty_uos = 0 , uos = False , name = ' ' , partner_id = False ,
2011-07-13 11:12:31 +00:00
lang = False , update_tax = True , date_order = False , packaging = False , fiscal_position = False , flag = False , context = None ) :
2010-06-24 09:45:18 +00:00
res = super ( sale_order_line , self ) . product_id_change ( cr , uid , ids , pricelist , product , qty = qty ,
uom = uom , qty_uos = qty_uos , uos = uos , name = name , partner_id = partner_id ,
2011-07-18 05:05:28 +00:00
lang = lang , update_tax = update_tax , date_order = date_order , packaging = packaging , fiscal_position = fiscal_position , flag = flag , context = context )
2011-07-01 11:56:21 +00:00
if not pricelist :
return res
2014-08-25 15:16:35 +00:00
if context is None :
context = { }
2010-12-21 10:24:10 +00:00
frm_cur = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id . currency_id . id
2011-07-01 11:56:21 +00:00
to_cur = self . pool . get ( ' product.pricelist ' ) . browse ( cr , uid , [ pricelist ] ) [ 0 ] . currency_id . id
2010-07-07 07:13:23 +00:00
if product :
2014-10-27 17:02:59 +00:00
product = self . pool [ ' product.product ' ] . browse ( cr , uid , product , context = context )
purchase_price = product . standard_price
to_uom = res . get ( ' product_uom ' , uom )
if to_uom != product . uom_id . id :
purchase_price = self . pool [ ' product.uom ' ] . _compute_price ( cr , uid , product . uom_id . id , purchase_price , to_uom )
2014-08-25 15:16:35 +00:00
ctx = context . copy ( )
ctx [ ' date ' ] = date_order
price = self . pool . get ( ' res.currency ' ) . compute ( cr , uid , frm_cur , to_cur , purchase_price , round = False , context = ctx )
2010-12-21 10:24:10 +00:00
res [ ' value ' ] . update ( { ' purchase_price ' : price } )
2010-06-24 09:45:18 +00:00
return res
2010-06-22 10:18:03 +00:00
def _product_margin ( self , cr , uid , ids , field_name , arg , context = None ) :
2010-06-03 09:47:02 +00:00
res = { }
2010-11-22 10:37:53 +00:00
for line in self . browse ( cr , uid , ids , context = context ) :
2010-06-03 09:47:02 +00:00
res [ line . id ] = 0
if line . product_id :
2014-12-12 14:39:35 +00:00
res [ line . id ] = round ( line . price_subtotal - ( ( line . purchase_price or line . product_id . standard_price ) * line . product_uos_qty ) , 2 )
2010-06-03 09:47:02 +00:00
return res
_columns = {
2011-09-17 10:36:57 +00:00
' margin ' : fields . function ( _product_margin , string = ' Margin ' ,
2011-03-10 14:45:22 +00:00
store = True ) ,
2010-06-03 09:47:02 +00:00
' purchase_price ' : fields . float ( ' Cost Price ' , digits = ( 16 , 2 ) )
}
2010-06-10 04:11:30 +00:00
2010-06-03 09:47:02 +00:00
class sale_order ( osv . osv ) :
_inherit = " sale.order "
2010-06-10 04:11:30 +00:00
2010-06-22 10:18:03 +00:00
def _product_margin ( self , cr , uid , ids , field_name , arg , context = None ) :
2010-06-03 09:47:02 +00:00
result = { }
for sale in self . browse ( cr , uid , ids , context = context ) :
result [ sale . id ] = 0.0
for line in sale . order_line :
result [ sale . id ] + = line . margin or 0.0
return result
2011-03-10 14:45:22 +00:00
def _get_order ( self , cr , uid , ids , context = None ) :
2012-12-16 18:27:00 +00:00
result = { }
for line in self . pool . get ( ' sale.order.line ' ) . browse ( cr , uid , ids , context = context ) :
result [ line . order_id . id ] = True
return result . keys ( )
2011-03-10 14:45:22 +00:00
2010-06-03 09:47:02 +00:00
_columns = {
2012-10-23 12:04:15 +00:00
' margin ' : fields . function ( _product_margin , string = ' Margin ' , help = " It gives profitability by calculating the difference between the Unit Price and the cost price. " , store = {
2014-09-30 15:19:00 +00:00
' sale.order.line ' : ( _get_order , [ ' margin ' , ' purchase_price ' ] , 20 ) ,
2011-03-10 14:45:22 +00:00
' sale.order ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' order_line ' ] , 20 ) ,
} ) ,
2010-06-03 09:47:02 +00:00
}
2010-06-10 04:11:30 +00:00
2010-06-03 09:47:02 +00:00
2012-12-16 18:27:00 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: