2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2007-08-06 09:45:52 +00:00
##############################################################################
2009-11-27 11:02:43 +00:00
#
2008-11-04 12:35:47 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-06-16 11:00:21 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# 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.
2007-08-06 09:45:52 +00:00
#
2008-11-03 19:18:56 +00:00
# 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
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2007-08-06 09:45:52 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-11-27 11:02:43 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2007-08-06 09:45:52 +00:00
#
##############################################################################
2010-10-11 12:38:01 +00:00
2007-08-06 09:45:52 +00:00
import time
2007-10-04 05:59:39 +00:00
2010-06-11 09:54:14 +00:00
from osv import osv , fields
import netsvc
2007-10-04 05:59:39 +00:00
2007-08-06 09:45:52 +00:00
class payment_mode ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = ' payment.mode '
2010-05-19 18:32:32 +00:00
_description = ' Payment Mode '
2010-10-22 12:08:56 +00:00
_columns = {
2010-06-16 11:51:39 +00:00
' name ' : fields . char ( ' Name ' , size = 64 , required = True , help = ' Mode of Payment ' ) ,
2008-07-22 15:11:28 +00:00
' bank_id ' : fields . many2one ( ' res.partner.bank ' , " Bank account " ,
2008-09-22 10:32:12 +00:00
required = True , help = ' Bank Account for the Payment Mode ' ) ,
2008-07-22 15:11:28 +00:00
' journal ' : fields . many2one ( ' account.journal ' , ' Journal ' , required = True ,
2010-11-03 10:49:56 +00:00
domain = [ ( ' type ' , ' in ' , ( ' bank ' , ' cash ' ) ) ] , help = ' Bank or Cash Journal for the Payment Mode ' ) ,
2011-04-05 12:39:58 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True ) ,
' partner_id ' : fields . related ( ' company_id ' , ' partner_id ' , type = ' many2one ' , relation = ' res.partner ' , string = ' Partner ' , store = True , ) ,
2008-07-22 15:11:28 +00:00
}
2010-08-20 11:32:18 +00:00
_defaults = {
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , c ) . company_id . id
}
2010-09-06 03:38:01 +00:00
2010-11-19 13:48:01 +00:00
def suitable_bank_types ( self , cr , uid , payment_code = None , context = None ) :
2008-07-22 15:11:28 +00:00
""" Return the codes of the bank type that are suitable
for the given payment type code """
2008-08-29 15:20:06 +00:00
if not payment_code :
return [ ]
2010-10-22 12:08:56 +00:00
cr . execute ( """ SELECT pb.state
FROM res_partner_bank pb
JOIN payment_mode pm ON ( pm . bank_id = pb . id )
WHERE pm . id = % s """ , [payment_code])
2008-07-22 15:11:28 +00:00
return [ x [ 0 ] for x in cr . fetchall ( ) ]
2011-04-05 12:39:58 +00:00
2011-04-06 05:13:37 +00:00
def onchange_company_id ( self , cr , uid , ids , company_id = False , context = None ) :
result = { }
2011-04-05 12:39:58 +00:00
if company_id :
2011-04-20 13:43:23 +00:00
partner_id = self . pool . get ( ' res.company ' ) . browse ( cr , uid , company_id , context = context ) . partner_id . id
2011-04-05 13:36:31 +00:00
result [ ' partner_id ' ] = partner_id
return { ' value ' : result }
2011-04-05 12:39:58 +00:00
2007-08-06 09:45:52 +00:00
payment_mode ( )
class payment_order ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = ' payment.order '
_description = ' Payment Order '
2008-10-01 13:57:13 +00:00
_rec_name = ' reference '
2011-05-10 08:42:45 +00:00
_order = ' id desc '
2008-07-22 15:11:28 +00:00
2010-06-11 09:54:14 +00:00
def get_wizard ( self , type ) :
2008-07-22 15:11:28 +00:00
logger = netsvc . Logger ( )
logger . notifyChannel ( " warning " , netsvc . LOG_WARNING ,
" No wizard found for the payment type ' %s ' . " % type )
return None
def _total ( self , cursor , user , ids , name , args , context = None ) :
if not ids :
return { }
res = { }
for order in self . browse ( cursor , user , ids , context = context ) :
if order . line_ids :
res [ order . id ] = reduce ( lambda x , y : x + y . amount , order . line_ids , 0.0 )
else :
res [ order . id ] = 0.0
return res
_columns = {
2010-10-22 12:08:56 +00:00
' date_scheduled ' : fields . date ( ' Scheduled date if fixed ' , states = { ' done ' : [ ( ' readonly ' , True ) ] } , help = ' Select a date if you have chosen Preferred Date to be fixed. ' ) ,
' reference ' : fields . char ( ' Reference ' , size = 128 , required = 1 , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
' mode ' : fields . many2one ( ' payment.mode ' , ' Payment mode ' , select = True , required = 1 , states = { ' done ' : [ ( ' readonly ' , True ) ] } , help = ' Select the Payment Mode to be applied. ' ) ,
2008-07-22 15:11:28 +00:00
' state ' : fields . selection ( [
( ' draft ' , ' Draft ' ) ,
2010-10-22 12:08:56 +00:00
( ' open ' , ' Confirmed ' ) ,
( ' cancel ' , ' Cancelled ' ) ,
( ' done ' , ' Done ' ) ] , ' State ' , select = True ,
2009-11-25 10:03:47 +00:00
help = ' When an order is placed the state is \' Draft \' . \n Once the bank is confirmed the state is set to \' Confirmed \' . \n Then the order is paid the state is \' Done \' . ' ) ,
2010-10-22 12:08:56 +00:00
' line_ids ' : fields . one2many ( ' payment.line ' , ' order_id ' , ' Payment lines ' , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2011-07-01 23:41:24 +00:00
' total ' : fields . function ( _total , string = " Total " , type = ' float ' ) ,
2010-10-22 12:08:56 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' User ' , required = True , states = { ' done ' : [ ( ' readonly ' , True ) ] } ) ,
2008-07-22 15:11:28 +00:00
' date_prefered ' : fields . selection ( [
( ' now ' , ' Directly ' ) ,
( ' due ' , ' Due date ' ) ,
( ' fixed ' , ' Fixed date ' )
2010-10-22 12:08:56 +00:00
] , " Preferred date " , change_default = True , required = True , states = { ' done ' : [ ( ' readonly ' , True ) ] } , help = " Choose an option for the Payment Order: ' Fixed ' stands for a date specified by you. ' Directly ' stands for the direct execution. ' Due date ' stands for the scheduled date of execution. " ) ,
2008-07-22 15:11:28 +00:00
' date_created ' : fields . date ( ' Creation date ' , readonly = True ) ,
' date_done ' : fields . date ( ' Execution date ' , readonly = True ) ,
2011-02-28 06:09:22 +00:00
' company_id ' : fields . related ( ' mode ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = True , readonly = True ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
' user_id ' : lambda self , cr , uid , context : uid ,
2010-10-22 12:08:56 +00:00
' state ' : ' draft ' ,
' date_prefered ' : ' due ' ,
2010-11-04 12:42:42 +00:00
' date_created ' : lambda * a : time . strftime ( ' % Y- % m- %d ' ) ,
2008-07-22 15:11:28 +00:00
' reference ' : lambda self , cr , uid , context : self . pool . get ( ' ir.sequence ' ) . get ( cr , uid , ' payment.order ' ) ,
}
def set_to_draft ( self , cr , uid , ids , * args ) :
2010-10-22 12:08:56 +00:00
self . write ( cr , uid , ids , { ' state ' : ' draft ' } )
2008-07-22 15:11:28 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for id in ids :
wf_service . trg_create ( uid , ' payment.order ' , id , cr )
return True
def action_open ( self , cr , uid , ids , * args ) :
2010-10-20 13:36:16 +00:00
ir_seq_obj = self . pool . get ( ' ir.sequence ' )
2010-06-16 11:51:39 +00:00
for order in self . read ( cr , uid , ids , [ ' reference ' ] ) :
2008-07-22 15:11:28 +00:00
if not order [ ' reference ' ] :
2010-10-20 13:36:16 +00:00
reference = ir_seq_obj . get ( cr , uid , ' payment.order ' )
2010-10-22 12:08:56 +00:00
self . write ( cr , uid , order [ ' id ' ] , { ' reference ' : reference } )
2008-07-22 15:11:28 +00:00
return True
2010-10-11 12:38:01 +00:00
def set_done ( self , cr , uid , ids , * args ) :
2008-07-22 15:11:28 +00:00
wf_service = netsvc . LocalService ( " workflow " )
2010-10-11 12:38:01 +00:00
self . write ( cr , uid , ids , { ' date_done ' : time . strftime ( ' % Y- % m- %d ' ) } )
wf_service . trg_validate ( uid , ' payment.order ' , ids [ 0 ] , ' done ' , cr )
2008-07-22 15:11:28 +00:00
return True
2007-08-06 09:45:52 +00:00
2010-09-01 06:02:18 +00:00
def copy ( self , cr , uid , id , default = { } , context = None ) :
default . update ( {
2010-10-22 12:08:56 +00:00
' state ' : ' draft ' ,
2010-09-01 06:02:18 +00:00
' line_ids ' : [ ] ,
' reference ' : self . pool . get ( ' ir.sequence ' ) . get ( cr , uid , ' payment.order ' )
} )
return super ( payment_order , self ) . copy ( cr , uid , id , default , context = context )
2010-09-13 12:13:41 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
if context is None :
context = { }
payment_line_obj = self . pool . get ( ' payment.line ' )
payment_line_ids = [ ]
2010-10-20 13:36:16 +00:00
2010-09-13 12:13:41 +00:00
if ( vals . get ( ' date_prefered ' , False ) == ' fixed ' and not vals . get ( ' date_scheduled ' , False ) ) or vals . get ( ' date_scheduled ' , False ) :
for order in self . browse ( cr , uid , ids , context = context ) :
for line in order . line_ids :
payment_line_ids . append ( line . id )
2010-10-22 12:08:56 +00:00
payment_line_obj . write ( cr , uid , payment_line_ids , { ' date ' : vals . get ( ' date_scheduled ' , False ) } , context = context )
2010-09-13 12:13:41 +00:00
elif vals . get ( ' date_prefered ' , False ) == ' due ' :
2010-10-22 12:08:56 +00:00
vals . update ( { ' date_scheduled ' : False } )
2010-09-13 12:13:41 +00:00
for order in self . browse ( cr , uid , ids , context = context ) :
for line in order . line_ids :
2010-10-22 12:08:56 +00:00
payment_line_obj . write ( cr , uid , [ line . id ] , { ' date ' : line . ml_maturity_date } , context = context )
2010-09-13 12:13:41 +00:00
elif vals . get ( ' date_prefered ' , False ) == ' now ' :
2010-10-22 12:08:56 +00:00
vals . update ( { ' date_scheduled ' : False } )
2010-09-13 12:13:41 +00:00
for order in self . browse ( cr , uid , ids , context = context ) :
for line in order . line_ids :
payment_line_ids . append ( line . id )
payment_line_obj . write ( cr , uid , payment_line_ids , { ' date ' : False } , context = context )
return super ( payment_order , self ) . write ( cr , uid , ids , vals , context = context )
2007-08-06 09:45:52 +00:00
payment_order ( )
class payment_line ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = ' payment.line '
_description = ' Payment Line '
def translate ( self , orig ) :
return {
" due_date " : " date_maturity " ,
" reference " : " ref " } . get ( orig , orig )
def info_owner ( self , cr , uid , ids , name = None , args = None , context = None ) :
if not ids : return { }
2010-10-20 13:36:16 +00:00
partner_zip_obj = self . pool . get ( ' res.partner.zip ' )
2008-07-22 15:11:28 +00:00
result = { }
info = ' '
for line in self . browse ( cr , uid , ids , context = context ) :
2010-10-22 12:08:56 +00:00
owner = line . order_id . mode . bank_id . partner_id
result [ line . id ] = False
2008-07-22 15:11:28 +00:00
if owner . address :
for ads in owner . address :
2010-10-22 12:08:56 +00:00
if ads . type == ' default ' :
st = ads . street and ads . street or ' '
st1 = ads . street2 and ads . street2 or ' '
2008-07-22 15:11:28 +00:00
if ' zip_id ' in ads :
2010-10-22 12:08:56 +00:00
zip_city = ads . zip_id and partner_zip_obj . name_get ( cr , uid , [ ads . zip_id . id ] ) [ 0 ] [ 1 ] or ' '
2008-07-22 15:11:28 +00:00
else :
2010-10-22 12:08:56 +00:00
zip = ads . zip and ads . zip or ' '
city = ads . city and ads . city or ' '
zip_city = zip + ' ' + city
cntry = ads . country_id and ads . country_id . name or ' '
info = owner . name + " \n " + st + " " + st1 + " \n " + zip_city + " \n " + cntry
result [ line . id ] = info
2008-07-22 15:11:28 +00:00
break
return result
def info_partner ( self , cr , uid , ids , name = None , args = None , context = None ) :
if not ids : return { }
2010-10-20 13:36:16 +00:00
partner_zip_obj = self . pool . get ( ' res.partner.zip ' )
2008-07-22 15:11:28 +00:00
result = { }
2010-10-22 12:08:56 +00:00
info = ' '
2010-10-20 13:36:16 +00:00
2008-07-22 15:11:28 +00:00
for line in self . browse ( cr , uid , ids , context = context ) :
2010-10-22 12:08:56 +00:00
result [ line . id ] = False
2008-07-22 15:11:28 +00:00
if not line . partner_id :
break
partner = line . partner_id . name or ' '
if line . partner_id . address :
for ads in line . partner_id . address :
2010-10-22 12:08:56 +00:00
if ads . type == ' default ' :
st = ads . street and ads . street or ' '
st1 = ads . street2 and ads . street2 or ' '
2008-07-22 15:11:28 +00:00
if ' zip_id ' in ads :
2010-10-22 12:08:56 +00:00
zip_city = ads . zip_id and partner_zip_obj . name_get ( cr , uid , [ ads . zip_id . id ] ) [ 0 ] [ 1 ] or ' '
2008-07-22 15:11:28 +00:00
else :
2010-10-22 12:08:56 +00:00
zip = ads . zip and ads . zip or ' '
city = ads . city and ads . city or ' '
zip_city = zip + ' ' + city
cntry = ads . country_id and ads . country_id . name or ' '
info = partner + " \n " + st + " " + st1 + " \n " + zip_city + " \n " + cntry
result [ line . id ] = info
2008-07-22 15:11:28 +00:00
break
return result
def select_by_name ( self , cr , uid , ids , name , args , context = None ) :
if not ids : return { }
partner_obj = self . pool . get ( ' res.partner ' )
2010-10-20 13:36:16 +00:00
2008-07-22 15:11:28 +00:00
cr . execute ( """ SELECT pl.id, ml. %s
2010-10-22 12:08:56 +00:00
FROM account_move_line ml
INNER JOIN payment_line pl
ON ( ml . id = pl . move_line_id )
WHERE pl . id IN % % s """ % s elf.translate(name),
2010-06-10 13:34:19 +00:00
( tuple ( ids ) , ) )
2008-07-22 15:11:28 +00:00
res = dict ( cr . fetchall ( ) )
if name == ' partner_id ' :
partner_name = { }
2010-06-16 11:51:39 +00:00
for p_id , p_name in partner_obj . name_get ( cr , uid ,
filter ( lambda x : x and x != 0 , res . values ( ) ) , context = context ) :
2008-07-22 15:11:28 +00:00
partner_name [ p_id ] = p_name
for id in ids :
if id in res and partner_name :
res [ id ] = ( res [ id ] , partner_name [ res [ id ] ] )
else :
res [ id ] = ( False , False )
else :
for id in ids :
res . setdefault ( id , ( False , " " ) )
return res
def _amount ( self , cursor , user , ids , name , args , context = None ) :
if not ids :
return { }
currency_obj = self . pool . get ( ' res.currency ' )
if context is None :
context = { }
res = { }
2010-10-20 13:36:16 +00:00
2008-07-22 15:11:28 +00:00
for line in self . browse ( cursor , user , ids , context = context ) :
ctx = context . copy ( )
ctx [ ' date ' ] = line . order_id . date_done or time . strftime ( ' % Y- % m- %d ' )
res [ line . id ] = currency_obj . compute ( cursor , user , line . currency . id ,
line . company_currency . id ,
line . amount_currency , context = ctx )
return res
2010-11-19 13:48:01 +00:00
def _get_currency ( self , cr , uid , context = None ) :
2010-10-20 13:36:16 +00:00
user_obj = self . pool . get ( ' res.users ' )
currency_obj = self . pool . get ( ' res.currency ' )
2010-11-19 13:48:01 +00:00
user = user_obj . browse ( cr , uid , uid , context = context )
2010-10-20 13:36:16 +00:00
2008-07-22 15:11:28 +00:00
if user . company_id :
return user . company_id . currency_id . id
else :
2010-10-22 12:08:56 +00:00
return currency_obj . search ( cr , uid , [ ( ' rate ' , ' = ' , 1.0 ) ] ) [ 0 ]
2008-07-22 15:11:28 +00:00
2010-09-22 10:12:38 +00:00
def _get_date ( self , cr , uid , context = None ) :
if context is None :
context = { }
2010-10-20 13:36:16 +00:00
payment_order_obj = self . pool . get ( ' payment.order ' )
2010-09-22 10:12:38 +00:00
date = False
2010-10-20 13:36:16 +00:00
2010-09-22 10:12:38 +00:00
if context . get ( ' order_id ' ) and context [ ' order_id ' ] :
2010-11-19 13:48:01 +00:00
order = payment_order_obj . browse ( cr , uid , context [ ' order_id ' ] , context = context )
2010-09-22 10:12:38 +00:00
if order . date_prefered == ' fixed ' :
date = order . date_scheduled
else :
date = time . strftime ( ' % Y- % m- %d ' )
return date
2008-07-22 15:11:28 +00:00
def _get_ml_inv_ref ( self , cr , uid , ids , * a ) :
2010-10-22 12:08:56 +00:00
res = { }
2008-07-22 15:11:28 +00:00
for id in self . browse ( cr , uid , ids ) :
2008-09-04 13:06:03 +00:00
res [ id . id ] = False
2008-07-22 15:11:28 +00:00
if id . move_line_id :
if id . move_line_id . invoice :
2008-09-04 13:06:03 +00:00
res [ id . id ] = id . move_line_id . invoice . id
2008-07-22 15:11:28 +00:00
return res
def _get_ml_maturity_date ( self , cr , uid , ids , * a ) :
2010-10-22 12:08:56 +00:00
res = { }
2008-07-22 15:11:28 +00:00
for id in self . browse ( cr , uid , ids ) :
if id . move_line_id :
res [ id . id ] = id . move_line_id . date_maturity
else :
2011-02-01 11:04:30 +00:00
res [ id . id ] = False
2008-07-22 15:11:28 +00:00
return res
def _get_ml_created_date ( self , cr , uid , ids , * a ) :
2010-10-22 12:08:56 +00:00
res = { }
2008-07-22 15:11:28 +00:00
for id in self . browse ( cr , uid , ids ) :
if id . move_line_id :
res [ id . id ] = id . move_line_id . date_created
else :
2011-02-01 11:04:30 +00:00
res [ id . id ] = False
2008-07-22 15:11:28 +00:00
return res
_columns = {
' name ' : fields . char ( ' Your Reference ' , size = 64 , required = True ) ,
2010-06-16 11:51:39 +00:00
' communication ' : fields . char ( ' Communication ' , size = 64 , required = True , help = " Used as the message between ordering customer and current company. Depicts ' What do you want to say to the recipient about this order ? ' " ) ,
' communication2 ' : fields . char ( ' Communication 2 ' , size = 64 , help = ' The successor message of Communication. ' ) ,
2010-10-22 12:08:56 +00:00
' move_line_id ' : fields . many2one ( ' account.move.line ' , ' Entry line ' , domain = [ ( ' reconcile_id ' , ' = ' , False ) , ( ' account_id.type ' , ' = ' , ' payable ' ) ] , help = ' This Entry Line will be referred for the information of the ordering customer. ' ) ,
' amount_currency ' : fields . float ( ' Amount in Partner Currency ' , digits = ( 16 , 2 ) ,
2008-07-22 15:11:28 +00:00
required = True , help = ' Payment amount in the partner currency ' ) ,
2010-06-16 11:51:39 +00:00
' currency ' : fields . many2one ( ' res.currency ' , ' Partner Currency ' , required = True ) ,
' company_currency ' : fields . many2one ( ' res.currency ' , ' Company Currency ' , readonly = True ) ,
2011-04-14 09:40:54 +00:00
' bank_id ' : fields . many2one ( ' res.partner.bank ' , ' Destination Bank Account ' ) ,
2008-07-22 15:11:28 +00:00
' order_id ' : fields . many2one ( ' payment.order ' , ' Order ' , required = True ,
ondelete = ' cascade ' , select = True ) ,
2010-06-16 11:51:39 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , string = " Partner " , required = True , help = ' The Ordering Customer ' ) ,
2008-08-29 15:20:06 +00:00
' amount ' : fields . function ( _amount , string = ' Amount in Company Currency ' ,
2011-07-05 12:28:57 +00:00
type = ' float ' ,
2008-07-22 15:11:28 +00:00
help = ' Payment amount in the company currency ' ) ,
' ml_date_created ' : fields . function ( _get_ml_created_date , string = " Effective Date " ,
2011-07-05 12:28:57 +00:00
type = ' date ' , help = " Invoice Effective Date " ) ,
2011-07-01 23:41:24 +00:00
' ml_maturity_date ' : fields . function ( _get_ml_maturity_date , type = ' date ' , string = ' Due Date ' ) ,
' ml_inv_ref ' : fields . function ( _get_ml_inv_ref , type = ' many2one ' , relation = ' account.invoice ' , string = ' Invoice Ref. ' ) ,
' info_owner ' : fields . function ( info_owner , string = " Owner Account " , type = " text " , help = ' Address of the Main Partner ' ) ,
' info_partner ' : fields . function ( info_partner , string = " Destination Account " , type = " text " , help = ' Address of the Ordering Customer. ' ) ,
2010-06-16 11:51:39 +00:00
' date ' : fields . date ( ' Payment Date ' , help = " If no payment date is specified, the bank will treat this payment line directly " ) ,
2010-10-11 10:16:12 +00:00
' create_date ' : fields . datetime ( ' Created ' , readonly = True ) ,
2010-09-01 10:38:47 +00:00
' state ' : fields . selection ( [ ( ' normal ' , ' Free ' ) , ( ' structured ' , ' Structured ' ) ] , ' Communication Type ' , required = True ) ,
2011-02-24 11:45:37 +00:00
' bank_statement_line_id ' : fields . many2one ( ' account.bank.statement.line ' , ' Bank statement line ' ) ,
2011-02-28 06:09:22 +00:00
' company_id ' : fields . related ( ' order_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = True , readonly = True ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
' name ' : lambda obj , cursor , user , context : obj . pool . get ( ' ir.sequence '
) . get ( cursor , user , ' payment.line ' ) ,
2010-10-11 12:38:01 +00:00
' state ' : ' normal ' ,
2008-07-22 15:11:28 +00:00
' currency ' : _get_currency ,
' company_currency ' : _get_currency ,
2010-09-22 10:12:38 +00:00
' date ' : _get_date ,
2008-07-22 15:11:28 +00:00
}
_sql_constraints = [
( ' name_uniq ' , ' UNIQUE(name) ' , ' The payment line name must be unique! ' ) ,
]
2010-09-01 05:11:44 +00:00
def onchange_move_line ( self , cr , uid , ids , move_line_id , payment_type , date_prefered , date_scheduled , currency = False , company_currency = False , context = None ) :
2010-10-22 12:08:56 +00:00
data = { }
2010-10-20 13:36:16 +00:00
move_line_obj = self . pool . get ( ' account.move.line ' )
2008-09-17 14:57:37 +00:00
2010-10-22 12:08:56 +00:00
data [ ' amount_currency ' ] = data [ ' communication ' ] = data [ ' partner_id ' ] = data [ ' reference ' ] = data [ ' date_created ' ] = data [ ' bank_id ' ] = data [ ' amount ' ] = False
2008-09-17 14:57:37 +00:00
2008-07-22 15:11:28 +00:00
if move_line_id :
2010-12-13 06:43:09 +00:00
line = move_line_obj . browse ( cr , uid , move_line_id , context = context )
2010-10-20 13:36:16 +00:00
data [ ' amount_currency ' ] = line . amount_to_pay
2008-12-29 07:01:21 +00:00
2008-09-17 21:18:37 +00:00
res = self . onchange_amount ( cr , uid , ids , data [ ' amount_currency ' ] , currency ,
company_currency , context )
2008-09-17 11:54:22 +00:00
if res :
data [ ' amount ' ] = res [ ' value ' ] [ ' amount ' ]
2010-10-22 12:08:56 +00:00
data [ ' partner_id ' ] = line . partner_id . id
2008-09-17 14:57:37 +00:00
temp = line . currency_id and line . currency_id . id or False
if not temp :
if line . invoice :
data [ ' currency ' ] = line . invoice . currency_id . id
else :
data [ ' currency ' ] = temp
2008-07-22 15:11:28 +00:00
# calling onchange of partner and updating data dictionary
2010-10-22 12:08:56 +00:00
temp_dict = self . onchange_partner ( cr , uid , ids , line . partner_id . id , payment_type )
2008-07-22 15:11:28 +00:00
data . update ( temp_dict [ ' value ' ] )
2010-10-22 12:08:56 +00:00
data [ ' reference ' ] = line . ref
2008-08-29 15:20:06 +00:00
data [ ' date_created ' ] = line . date_created
2010-10-22 12:08:56 +00:00
data [ ' communication ' ] = line . ref
2008-07-22 15:11:28 +00:00
2008-08-29 15:20:06 +00:00
if date_prefered == ' now ' :
#no payment date => immediate payment
data [ ' date ' ] = False
elif date_prefered == ' due ' :
data [ ' date ' ] = line . date_maturity
elif date_prefered == ' fixed ' :
2010-09-01 05:11:44 +00:00
data [ ' date ' ] = date_scheduled
2008-07-22 15:11:28 +00:00
return { ' value ' : data }
2008-12-29 07:01:21 +00:00
def onchange_amount ( self , cr , uid , ids , amount , currency , cmpny_currency , context = None ) :
if ( not amount ) or ( not cmpny_currency ) :
2010-10-22 12:08:56 +00:00
return { ' value ' : { ' amount ' : False } }
2008-09-17 11:54:22 +00:00
res = { }
currency_obj = self . pool . get ( ' res.currency ' )
2008-12-29 07:01:21 +00:00
company_amount = currency_obj . compute ( cr , uid , currency , cmpny_currency , amount )
2008-09-17 11:54:22 +00:00
res [ ' amount ' ] = company_amount
return { ' value ' : res }
2010-06-16 11:51:39 +00:00
def onchange_partner ( self , cr , uid , ids , partner_id , payment_type , context = None ) :
2010-10-22 12:08:56 +00:00
data = { }
2010-10-20 13:36:16 +00:00
partner_zip_obj = self . pool . get ( ' res.partner.zip ' )
partner_obj = self . pool . get ( ' res.partner ' )
payment_mode_obj = self . pool . get ( ' payment.mode ' )
2010-10-22 12:08:56 +00:00
data [ ' info_partner ' ] = data [ ' bank_id ' ] = False
2008-07-22 15:11:28 +00:00
if partner_id :
2010-11-19 13:48:01 +00:00
part_obj = partner_obj . browse ( cr , uid , partner_id , context = context )
2010-10-22 12:08:56 +00:00
partner = part_obj . name or ' '
2008-07-22 15:11:28 +00:00
if part_obj . address :
for ads in part_obj . address :
2010-10-22 12:08:56 +00:00
if ads . type == ' default ' :
st = ads . street and ads . street or ' '
st1 = ads . street2 and ads . street2 or ' '
2008-07-22 15:11:28 +00:00
if ' zip_id ' in ads :
2010-10-22 12:08:56 +00:00
zip_city = ads . zip_id and partner_zip_obj . name_get ( cr , uid , [ ads . zip_id . id ] ) [ 0 ] [ 1 ] or ' '
2008-07-22 15:11:28 +00:00
else :
2010-10-22 12:08:56 +00:00
zip = ads . zip and ads . zip or ' '
city = ads . city and ads . city or ' '
zip_city = zip + ' ' + city
2008-07-22 15:11:28 +00:00
2010-10-22 12:08:56 +00:00
cntry = ads . country_id and ads . country_id . name or ' '
info = partner + " \n " + st + " " + st1 + " \n " + zip_city + " \n " + cntry
2008-07-22 15:11:28 +00:00
2010-10-22 12:08:56 +00:00
data [ ' info_partner ' ] = info
2008-07-22 15:11:28 +00:00
2008-08-29 15:20:06 +00:00
if part_obj . bank_ids and payment_type :
2010-10-20 13:36:16 +00:00
bank_type = payment_mode_obj . suitable_bank_types ( cr , uid , payment_type , context = context )
2008-08-29 15:20:06 +00:00
for bank in part_obj . bank_ids :
if bank . state in bank_type :
data [ ' bank_id ' ] = bank . id
break
2008-07-22 15:11:28 +00:00
return { ' value ' : data }
def fields_get ( self , cr , uid , fields = None , context = None ) :
res = super ( payment_line , self ) . fields_get ( cr , uid , fields , context )
if ' communication2 ' in res :
res [ ' communication2 ' ] . setdefault ( ' states ' , { } )
res [ ' communication2 ' ] [ ' states ' ] [ ' structured ' ] = [ ( ' readonly ' , True ) ]
res [ ' communication2 ' ] [ ' states ' ] [ ' normal ' ] = [ ( ' readonly ' , False ) ]
return res
2007-08-06 09:45:52 +00:00
payment_line ( )
2008-07-23 14:41:47 +00:00
2010-11-03 10:49:56 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: