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
#
##############################################################################
2010-10-22 07:23:46 +00:00
from 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 ,
lang = False , update_tax = True , date_order = False , packaging = False , fiscal_position = False , flag = False ) :
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 ,
lang = lang , update_tax = update_tax , date_order = date_order , packaging = packaging , fiscal_position = fiscal_position , flag = flag )
2010-07-07 07:13:23 +00:00
if product :
purchase_price = self . pool . get ( ' product.product ' ) . browse ( cr , uid , product ) . standard_price
2010-10-22 07:23:46 +00:00
res [ ' value ' ] . update ( { ' purchase_price ' : purchase_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 = { }
for line in self . browse ( cr , uid , ids ) :
res [ line . id ] = 0
if line . product_id :
if line . purchase_price :
2010-10-22 07:23:46 +00:00
res [ line . id ] = round ( ( line . price_unit * line . product_uos_qty * ( 100.0 - line . discount ) / 100.0 ) - ( line . purchase_price * line . product_uos_qty ) , 2 )
2010-06-03 09:47:02 +00:00
else :
2010-10-22 07:23:46 +00:00
res [ line . id ] = round ( ( line . price_unit * line . product_uos_qty * ( 100.0 - line . discount ) / 100.0 ) - ( line . product_id . standard_price * line . product_uos_qty ) , 2 )
2010-06-03 09:47:02 +00:00
return res
_columns = {
2010-06-24 11:25:15 +00:00
' margin ' : fields . function ( _product_margin , method = True , string = ' Margin ' , 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
sale_order_line ( )
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
_columns = {
2010-07-07 14:38:23 +00:00
' margin ' : fields . function ( _product_margin , method = True , string = ' Margin ' , store = True , help = " It gives profitability by calculating the difference between the Unit Price and Cost Price " ) ,
2010-06-03 09:47:02 +00:00
}
2010-06-10 04:11:30 +00:00
2010-06-03 09:47:02 +00:00
sale_order ( )
class stock_picking ( osv . osv ) :
_inherit = ' stock.picking '
_columns = {
2010-10-22 07:23:46 +00:00
' invoice_ids ' : fields . many2many ( ' account.invoice ' , ' picking_invoice_rel ' , ' picking_id ' , ' invoice_id ' , ' Invoices ' , domain = [ ( ' type ' , ' = ' , ' out_invoice ' ) ] ) ,
2010-06-03 09:47:02 +00:00
}
def create_invoice ( self , cr , uid , ids , * args ) :
# need to carify with new requirement
invoice_ids = [ ]
margin_deduce = 0.0
picking_obj = self . pool . get ( ' stock.picking ' )
2010-10-15 12:11:22 +00:00
picking_obj . write ( cr , uid , ids , { ' invoice_state ' : ' 2binvoiced ' } )
2010-06-03 09:47:02 +00:00
res = picking_obj . action_invoice_create ( cr , uid , ids , type = ' out_invoice ' , context = { } )
invoice_ids = res . values ( )
2010-10-22 07:23:46 +00:00
picking_obj . write ( cr , uid , ids , { ' invoice_ids ' : [ [ 6 , 0 , invoice_ids ] ] } )
2010-06-03 09:47:02 +00:00
return True
2010-06-10 04:11:30 +00:00
stock_picking ( )
2010-07-07 14:38:23 +00:00
class account_invoice_line ( osv . osv ) :
_inherit = " account.invoice.line "
_columns = {
' cost_price ' : fields . float ( ' Cost Price ' , digits = ( 16 , 2 ) ) ,
}
2010-11-27 20:16:16 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
2010-07-07 14:38:23 +00:00
if vals . get ( ' product_id ' , False ) :
res = self . pool . get ( ' product.product ' ) . read ( cr , uid , [ vals [ ' product_id ' ] ] , [ ' standard_price ' ] )
vals [ ' cost_price ' ] = res [ 0 ] [ ' standard_price ' ]
return super ( account_invoice_line , self ) . write ( cr , uid , ids , vals , context )
2010-11-27 20:16:16 +00:00
def create ( self , cr , uid , vals , context = None ) :
2010-07-07 14:38:23 +00:00
if vals . get ( ' product_id ' , False ) :
res = self . pool . get ( ' product.product ' ) . read ( cr , uid , [ vals [ ' product_id ' ] ] , [ ' standard_price ' ] )
vals [ ' cost_price ' ] = res [ 0 ] [ ' standard_price ' ]
return super ( account_invoice_line , self ) . create ( cr , uid , vals , context )
2010-10-15 12:11:22 +00:00
2010-07-07 14:38:23 +00:00
account_invoice_line ( )
2010-10-25 09:24:24 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: