2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2008-11-03 19:18:56 +00:00
##############################################################################
2009-12-24 09:39:00 +00:00
#
2009-10-14 11:15:34 +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-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.
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.
2008-11-03 19:18:56 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-12-24 09:39:00 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-11-03 19:18:56 +00:00
#
##############################################################################
2009-05-14 07:06:09 +00:00
2008-10-23 05:53:55 +00:00
import time
2010-08-16 20:14:32 +00:00
import netsvc
2010-05-27 19:55:40 +00:00
from osv import fields
from osv import osv
2010-07-22 08:43:54 +00:00
from tools . translate import _
2010-07-07 13:05:41 +00:00
2009-05-14 07:06:09 +00:00
class ir_sequence_type ( osv . osv ) :
_inherit = " ir.sequence.type "
_columns = {
' name ' : fields . char ( ' Sequence Name ' , size = 128 , required = True ) ,
' code ' : fields . char ( ' Sequence Code ' , size = 128 , required = True ) ,
}
ir_sequence_type ( )
2008-12-06 13:18:05 +00:00
2010-05-26 18:00:17 +00:00
class account_journal ( osv . osv ) :
_inherit = " account.journal "
_columns = {
2010-08-14 12:20:54 +00:00
' max_amount ' : fields . float ( ' Verify Transaction ' , digits = ( 16 , 2 ) , help = " Validate voucher entry twice before posting it, if transaction amount more then entered here " ) ,
2010-05-26 18:00:17 +00:00
}
account_journal ( )
2010-08-20 11:40:55 +00:00
class account_account ( osv . osv ) :
"""
account_account
"""
_inherit = ' account.account '
_columns = {
' user_type_type ' : fields . related ( ' user_type ' , ' report_type ' , type = ' char ' , size = 64 , relation = ' account.account.type ' , string = ' User Type Code ' , readonly = True , store = True )
}
account_account ( )
2010-08-18 20:13:20 +00:00
class account_move ( osv . osv ) :
_inherit = ' account.move '
def _get_line_ids ( self , cr , uid , ids , context = None ) :
result = { }
for line in self . pool . get ( ' account.move.line ' ) . browse ( cr , uid , ids , context = context ) :
result [ line . move_id . id ] = True
return result . keys ( )
def _amount_all ( self , cr , uid , ids , name , args , context = None ) :
res = { }
for move in self . browse ( cr , uid , ids , context = context ) :
rs = {
' reconcile_id ' : False ,
}
for line in move . line_id :
if line . reconcile_id :
rs . update ( {
' reconcile_id ' : line . reconcile_id . id
} )
2010-08-18 23:43:52 +00:00
break
2010-08-18 20:13:20 +00:00
res [ move . id ] = rs
return res
_columns = {
2010-08-18 23:43:52 +00:00
' reconcile_id ' : fields . function ( _amount_all , method = True , type = " many2one " , relation = " account.move.reconcile " , select = True , string = ' Reconcile ' ,
store = True ,
2010-08-18 20:13:20 +00:00
multi = ' all ' ) ,
}
def search ( self , cr , user , args , offset = 0 , limit = None , order = None , context = None , count = False ) :
"""
Returns a list of ids based on search domain { args }
@param cr : A database cursor
@param user : ID of the user currently logged in
@param args : list of conditions to be applied in search opertion
@param offset : default from first record , you can start from nth record
@param limit : number of records to be obtained as a result of search opertion
@param order : ordering on any field ( s )
@param context : context arguments , like lang , time zone
@param count :
@return : Returns a list of ids based on search domain
"""
if not context :
context = { }
2010-08-18 20:39:09 +00:00
ttype = context . get ( ' type ' , False )
2010-08-18 20:13:20 +00:00
partner = context . get ( ' partner_id ' , False )
voucher = context . get ( ' voucher ' , False )
if voucher and not partner :
raise osv . except_osv ( _ ( ' Invalid Partner ! ' ) , _ ( ' Please select the partner ! ' ) )
if ttype and ttype in ( ' receipt ' ) :
args + = [ ( ' journal_id.type ' , ' in ' , [ ' sale ' , ' purchase_refund ' ] ) ]
elif ttype and ttype in ( ' payment ' ) :
args + = [ ( ' journal_id.type ' , ' in ' , [ ' purchase ' , ' sale_refund ' ] ) ]
elif ttype and ttype in ( ' sale ' , ' purchase ' ) :
raise osv . except_osv ( _ ( ' Invalid action ! ' ) , _ ( ' You can not reconcile sales, purchase, or journal voucher with invoice ! ' ) )
args + = [ ( ' journal_id.type ' , ' = ' , ' do_not_allow_search ' ) ]
2010-08-18 23:43:52 +00:00
res = super ( account_move , self ) . search ( cr , user , args , offset , limit , order , { } , count )
2010-08-18 20:13:20 +00:00
return res
account_move ( )
2008-10-23 05:53:55 +00:00
class account_voucher ( osv . osv ) :
2010-05-26 19:30:21 +00:00
2010-05-27 19:55:40 +00:00
def _get_period ( self , cr , uid , context = { } ) :
if context . get ( ' period_id ' , False ) :
return context . get ( ' period_id ' )
2010-06-18 13:39:57 +00:00
2008-10-23 05:53:55 +00:00
periods = self . pool . get ( ' account.period ' ) . find ( cr , uid )
if periods :
return periods [ 0 ]
else :
return False
2009-12-24 09:39:00 +00:00
2010-05-26 19:30:21 +00:00
def _get_journal ( self , cr , uid , context = { } ) :
journal_pool = self . pool . get ( ' account.journal ' )
2010-06-18 13:39:57 +00:00
2010-05-26 19:30:21 +00:00
if context . get ( ' journal_id ' , False ) :
return context . get ( ' journal_id ' )
2010-06-18 13:39:57 +00:00
2010-08-13 21:02:45 +00:00
ttype = context . get ( ' type ' , ' bank ' )
2010-05-26 19:30:21 +00:00
res = journal_pool . search ( cr , uid , [ ( ' type ' , ' = ' , ttype ) ] , limit = 1 )
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
if res :
return res [ 0 ]
else :
return False
2010-08-19 14:01:41 +00:00
def _get_type ( self , cr , uid , context = { } ) :
return context . get ( ' type ' )
2010-08-17 13:24:18 +00:00
def _get_pay_journal ( self , cr , uid , context = { } ) :
journal_pool = self . pool . get ( ' account.journal ' )
res = journal_pool . search ( cr , uid , [ ( ' type ' , ' = ' , ' bank ' ) ] , limit = 1 )
if res :
return res [ 0 ]
else :
return False
2008-10-23 05:53:55 +00:00
def _get_currency ( self , cr , uid , context ) :
2010-07-07 13:05:41 +00:00
user = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid )
2008-10-23 05:53:55 +00:00
if user . company_id :
return user . company_id . currency_id . id
else :
2010-07-07 13:05:41 +00:00
return self . pool . get ( ' res.currency ' ) . search ( cr , uid , [ ( ' rate ' , ' = ' , 1.0 ) ] ) [ 0 ]
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
_name = ' account.voucher '
_description = ' Accounting Voucher '
2010-07-07 15:04:16 +00:00
_order = " id desc "
2010-08-16 20:14:32 +00:00
_rec_name = ' number '
2008-10-23 05:53:55 +00:00
_columns = {
2010-08-19 14:01:41 +00:00
' type ' : fields . selection ( [
( ' payment ' , ' Payment ' ) ,
( ' receipt ' , ' Receipt ' ) ,
( ' sale ' , ' Sale ' ) ,
( ' purchase ' , ' Purchase ' ) ,
] , ' Type ' , select = True , readonly = True ) ,
2010-08-17 07:32:51 +00:00
' name ' : fields . char ( ' Memo ' , size = 256 , required = False , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-07-14 05:51:50 +00:00
' date ' : fields . date ( ' Date ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , help = " Effective date for accounting entries " ) ,
2008-10-23 05:53:55 +00:00
' journal_id ' : fields . many2one ( ' account.journal ' , ' Journal ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-19 14:01:41 +00:00
' account_id ' : fields . many2one ( ' account.account ' , ' Account ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-07-28 18:35:57 +00:00
' payment_ids ' : fields . one2many ( ' account.voucher.line ' , ' voucher_id ' , ' Voucher Lines ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-13 21:02:45 +00:00
' period_id ' : fields . many2one ( ' account.period ' , ' Period ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-07-28 18:35:57 +00:00
' narration ' : fields . text ( ' Narration ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2008-10-23 05:53:55 +00:00
' currency_id ' : fields . many2one ( ' res.currency ' , ' Currency ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True ) ,
' state ' : fields . selection (
2010-05-26 18:00:17 +00:00
[ ( ' draft ' , ' Draft ' ) ,
( ' proforma ' , ' Pro-forma ' ) ,
( ' posted ' , ' Posted ' ) ,
( ' recheck ' , ' Waiting for Re-checking ' ) ,
( ' cancel ' , ' Cancel ' ) ,
( ' audit ' , ' Audit Complete ' )
] , ' State ' , readonly = True , size = 32 ,
help = ' * The \' Draft \' state is used when a user is encoding a new and unconfirmed Voucher. \
2009-12-24 09:39:00 +00:00
\n * The \' Pro-forma \' when voucher is in Pro-forma state,voucher does not have an voucher number. \
\n * The \' Posted \' state is used when user create voucher,a voucher number is generated and voucher entries are created in account \
2010-06-18 13:39:57 +00:00
\n * The \' Cancelled \' state is used when user cancel voucher. ' ) ,
2010-08-20 11:40:55 +00:00
#'amount': fields.function(_compute_total, method=True, type='float', digits=(14,2), string='Total', store=True),
' amount ' : fields . float ( ' Total ' , digits = ( 16 , 2 ) , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' tax_amount ' : fields . float ( ' Tax Amount ' , digits = ( 14 , 4 ) , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-17 07:32:51 +00:00
' reference ' : fields . char ( ' Ref # ' , size = 64 , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , help = " Payment or Receipt transaction number, i.e. Bank cheque number or payorder number or Wire transfer number or Acknowledge number. " ) ,
2010-07-07 13:05:41 +00:00
' number ' : fields . related ( ' move_id ' , ' name ' , type = " char " , readonly = True , string = ' Number ' ) ,
2008-10-23 05:53:55 +00:00
' move_id ' : fields . many2one ( ' account.move ' , ' Account Entry ' ) ,
2010-08-15 12:55:29 +00:00
' move_ids ' : fields . related ( ' move_id ' , ' line_id ' , type = ' many2many ' , relation = ' account.move.line ' , string = ' Journal Items ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-13 21:12:34 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-14 07:53:03 +00:00
' audit ' : fields . related ( ' move_id ' , ' to_check ' , type = ' boolean ' , relation = ' account.move ' , string = ' Audit Complete ? ' ) ,
2010-08-18 20:13:20 +00:00
' pay_now ' : fields . selection ( [
2010-08-19 07:57:59 +00:00
( ' pay_now ' , ' Pay Directly ' ) ,
( ' pay_later ' , ' Pay Later or Group Funds ' ) ,
2010-08-18 20:13:20 +00:00
] , ' Payment ' , select = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-19 14:01:41 +00:00
' tax_id ' : fields . many2one ( ' account.tax ' , ' Tax ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2008-10-23 05:53:55 +00:00
}
2010-08-17 13:24:18 +00:00
2008-10-23 05:53:55 +00:00
_defaults = {
2010-08-19 14:01:41 +00:00
' type ' : _get_type ,
2008-10-23 05:53:55 +00:00
' period_id ' : _get_period ,
' journal_id ' : _get_journal ,
' currency_id ' : _get_currency ,
2010-05-27 19:55:40 +00:00
' state ' : lambda * a : ' draft ' ,
2010-08-19 07:57:59 +00:00
' pay_now ' : lambda * a : ' pay_later ' ,
2010-08-20 11:40:55 +00:00
' name ' : lambda * a : ' ' ,
2010-05-27 19:55:40 +00:00
' date ' : lambda * a : time . strftime ( ' % Y- % m- %d ' ) ,
2010-08-13 21:12:34 +00:00
' audit ' : lambda * a : False ,
2010-05-27 19:55:40 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' account.voucher ' , context = c ) ,
2008-10-23 05:53:55 +00:00
}
2010-08-19 14:01:41 +00:00
2010-08-20 11:40:55 +00:00
def onchange_price ( self , cr , uid , ids , payment_ids , tax_amount , tax_id , context = { } ) :
2010-08-19 14:01:41 +00:00
res = {
2010-08-20 11:40:55 +00:00
' tax_amount ' : False ,
' amount ' : False
2010-08-19 14:01:41 +00:00
}
tax_pool = self . pool . get ( ' account.tax ' )
2010-08-20 11:40:55 +00:00
total = 0.0
for line in payment_ids :
total + = line [ 2 ] . get ( ' amount ' )
2010-08-19 14:01:41 +00:00
if tax_id :
tax = tax_pool . browse ( cr , uid , tax_id )
if tax . type == ' percent ' :
2010-08-20 11:40:55 +00:00
tax_amount = total * tax_amount and tax_amount or tax . amount
2010-08-19 14:01:41 +00:00
if tax . type == ' fixed ' :
2010-08-20 11:40:55 +00:00
tax_amount = tax_amount and tax_amount or tax . amount
res . update ( {
' amount ' : total + tax_amount ,
' tax_amount ' : tax_amount
} )
2010-08-19 14:01:41 +00:00
return {
' value ' : res
}
2010-08-20 11:40:55 +00:00
2010-08-19 09:25:59 +00:00
def create ( self , cr , uid , vals , context = { } ) :
"""
Create a new record for a model account_voucher
@param cr : A database cursor
@param user : ID of the user currently logged in
@param vals : provides data for new record
@param context : context arguments , like lang , time zone
@return : Returns an id of the new record
"""
old_line = [ ]
new_lines = [ ]
payment_ids = vals . get ( ' payment_ids ' )
2010-08-20 11:40:55 +00:00
2010-08-19 14:01:41 +00:00
if not payment_ids :
payment_ids = [ ]
2010-08-20 13:47:29 +00:00
2010-08-19 09:25:59 +00:00
vals . update ( {
' payment_ids ' : False
} )
for line in payment_ids :
id1 = line [ 0 ]
id2 = line [ 1 ]
res = line [ 2 ]
if id1 == 0 and id2 == 0 :
new_lines + = [ ( id1 , id2 , res ) ]
else :
old_line + = [ ( id1 , id2 , res ) ]
if new_lines :
vals . update ( {
' payment_ids ' : new_lines
} )
2010-08-20 11:40:55 +00:00
2010-08-19 09:25:59 +00:00
res_id = super ( account_voucher , self ) . create ( cr , uid , vals , context )
if old_line :
new_payment_ids = [ ]
for line in old_line :
id1 = line [ 0 ]
id2 = line [ 1 ]
res = line [ 2 ]
2010-08-20 11:40:55 +00:00
if res :
res . update ( {
' voucher_id ' : res_id
} )
new_payment_ids + = [ ( id1 , id2 , res ) ]
2010-08-19 09:25:59 +00:00
self . write ( cr , uid , [ res_id ] , { ' payment_ids ' : new_payment_ids } )
return res_id
2010-08-18 20:13:20 +00:00
2010-08-20 11:40:55 +00:00
def onchange_partner_id ( self , cr , uid , ids , partner_id , ttype , journal_id = False , context = { } ) :
2010-08-18 20:13:20 +00:00
"""
Returns a dict that contains new values and context
@param cr : A database cursor
@param user : ID of the user currently logged in
@param partner_id : latest value from user input for field partner_id
@param args : other arguments
@param context : context arguments , like lang , time zone
@return : Returns a dict which contains new values , and context
"""
move_pool = self . pool . get ( ' account.move ' )
line_pool = self . pool . get ( ' account.voucher.line ' )
2010-08-20 11:40:55 +00:00
move_line_pool = self . pool . get ( ' account.move.line ' )
2010-08-19 14:01:41 +00:00
partner_pool = self . pool . get ( ' res.partner ' )
2010-08-20 11:40:55 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
2010-08-18 20:13:20 +00:00
res = [ ]
context . update ( {
2010-08-18 23:43:52 +00:00
' type ' : ttype ,
2010-08-18 20:13:20 +00:00
' partner_id ' : partner_id ,
' voucher ' : True ,
} )
2010-08-20 11:40:55 +00:00
if journal_id :
context . update ( {
' journal_id ' : journal_id ,
} )
2010-08-18 20:13:20 +00:00
default = {
' value ' : { } ,
' context ' : context ,
}
if not partner_id or not ttype :
2010-08-18 23:43:52 +00:00
if ids :
line_ids = line_pool . search ( cr , uid , [ ( ' voucher_id ' , ' = ' , ids [ 0 ] ) ] )
if line_ids :
line_pool . unlink ( cr , uid , line_ids )
2010-08-18 20:13:20 +00:00
return default
2010-08-20 13:47:29 +00:00
2010-08-19 14:01:41 +00:00
account_id = False
partner = partner_pool . browse ( cr , uid , partner_id )
2010-08-20 11:40:55 +00:00
if ttype in ( ' sale ' ) :
2010-08-19 14:01:41 +00:00
account_id = partner . property_account_receivable . id
2010-08-20 11:40:55 +00:00
elif ttype in ( ' purchase ' ) :
2010-08-19 14:01:41 +00:00
account_id = partner . property_account_payable . id
2010-08-20 11:40:55 +00:00
elif ttype in ( ' payment ' , ' receipt ' ) :
journal = journal_pool . browse ( cr , uid , journal_id )
if ttype == ' payment ' :
account_id = journal . default_credit_account_id . id
elif ttype == ' receipt ' :
account_id = journal . default_debit_account_id . id
2010-08-19 14:01:41 +00:00
default [ ' value ' ] . update ( {
' account_id ' : account_id
} )
2010-08-18 20:13:20 +00:00
if ttype not in ( ' payment ' , ' receipt ' ) :
return default
2010-08-19 09:25:59 +00:00
voucher_id = ids and ids [ 0 ] or False
2010-08-20 13:47:29 +00:00
search_type = ' credit '
account_type = False
if ttype == ' receipt ' :
search_type = ' debit '
account_type = ' receivable '
elif ttype == ' payment ' :
search_type = ' credit '
account_type = ' payable '
ids = move_line_pool . search ( cr , uid , [ ( ' account_id.type ' , ' = ' , account_type ) , ( ' reconcile_id ' , ' = ' , False ) , ( ' partner_id ' , ' = ' , partner_id ) ] , context = context )
total = 0.0
2010-08-20 11:40:55 +00:00
for line in move_line_pool . browse ( cr , uid , ids ) :
rs = move_line_pool . default_get ( cr , uid , move_line_pool . _columns . keys ( ) , context = context )
rs . update ( {
2010-08-20 13:47:29 +00:00
' name ' : line . move_id . name ,
2010-08-20 11:40:55 +00:00
' ref ' : line . ref or ' / ' ,
' move_id ' : line . move_id . id ,
' move_line_id ' : line . id ,
2010-08-18 20:13:20 +00:00
' voucher_id ' : voucher_id ,
2010-08-20 11:40:55 +00:00
} )
2010-08-20 13:47:29 +00:00
amount = 0.0
2010-08-18 20:13:20 +00:00
if ttype == ' payment ' :
rs . update ( {
2010-08-20 11:40:55 +00:00
' account_id ' : line . move_id . partner_id . property_account_payable . id ,
' amount ' : line . credit
2010-08-18 20:13:20 +00:00
} )
2010-08-20 13:47:29 +00:00
amount = line . credit
2010-08-18 20:13:20 +00:00
elif ttype == ' receipt ' :
rs . update ( {
2010-08-20 11:40:55 +00:00
' account_id ' : line . move_id . partner_id . property_account_receivable . id ,
' amount ' : line . debit
2010-08-18 20:13:20 +00:00
} )
2010-08-20 13:47:29 +00:00
amount = line . debit
total + = amount
2010-08-20 11:40:55 +00:00
line_id = line_pool . create ( cr , uid , rs , context = context )
2010-08-18 20:13:20 +00:00
res + = [ line_id ]
2010-08-19 14:01:41 +00:00
2010-08-20 13:47:29 +00:00
res = {
' payment_ids ' : res ,
' account_id ' : account_id ,
' amount ' : total
}
2010-08-18 20:13:20 +00:00
return {
2010-08-19 14:01:41 +00:00
' value ' : res ,
2010-08-18 20:13:20 +00:00
' context ' : context ,
}
2010-08-13 21:08:42 +00:00
def onchange_date ( self , cr , user , ids , date , context = { } ) :
"""
Returns a dict that contains new values and context
@param cr : A database cursor
@param user : ID of the user currently logged in
@param date : latest value from user input for field date
@param args : other arguments
@param context : context arguments , like lang , time zone
@return : Returns a dict which contains new values , and context
"""
res = { }
period_pool = self . pool . get ( ' account.period ' )
pids = period_pool . search ( cr , user , [ ( ' date_start ' , ' <= ' , date ) , ( ' date_stop ' , ' >= ' , date ) ] )
if pids :
res . update ( {
' period_id ' : pids [ 0 ]
} )
context . update ( {
' period_id ' : pids [ 0 ]
} )
return {
' value ' : res ,
' context ' : context ,
}
2010-08-17 13:24:18 +00:00
2010-08-13 21:02:45 +00:00
def onchange_journal ( self , cr , uid , ids , journal_id , ttype ) :
2010-08-19 14:01:41 +00:00
res = { }
2010-08-14 12:20:54 +00:00
2008-10-23 05:53:55 +00:00
if not journal_id :
2010-05-27 19:55:40 +00:00
return {
2010-08-13 21:02:45 +00:00
' value ' : res
2010-05-27 19:55:40 +00:00
}
2010-08-13 21:02:45 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
journal = journal_pool . browse ( cr , uid , journal_id )
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
if journal . type in ( ' sale ' , ' purchase ' ) and not ttype :
res . update ( {
' type ' : journal . type
} )
return {
' value ' : res
}
2010-08-14 07:53:03 +00:00
def voucher_recheck ( self , cr , uid , ids , context = { } ) :
self . write ( cr , uid , ids , { ' state ' : ' recheck ' } , context )
return True
2010-08-14 12:20:54 +00:00
2008-10-23 05:53:55 +00:00
def proforma_voucher ( self , cr , uid , ids , context = { } ) :
2009-05-14 07:06:09 +00:00
self . action_move_line_create ( cr , uid , ids )
2008-10-23 05:53:55 +00:00
self . write ( cr , uid , ids , { ' state ' : ' posted ' } )
return True
2010-08-14 12:20:54 +00:00
2010-07-09 04:54:54 +00:00
def action_cancel_draft ( self , cr , uid , ids , context = { } ) :
2010-08-16 20:14:32 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for voucher_id in ids :
wf_service . trg_create ( uid , ' account.voucher ' , voucher_id , cr )
2010-07-07 15:04:16 +00:00
self . write ( cr , uid , ids , { ' state ' : ' draft ' } )
return True
2010-08-14 12:20:54 +00:00
2010-06-16 11:51:39 +00:00
def cancel_voucher ( self , cr , uid , ids , context = { } ) :
2010-07-07 13:05:41 +00:00
move_pool = self . pool . get ( ' account.move ' )
2010-08-13 21:02:45 +00:00
voucher_line_pool = self . pool . get ( ' account.voucher.line ' )
2010-08-14 12:20:54 +00:00
2010-07-07 13:05:41 +00:00
for voucher in self . browse ( cr , uid , ids ) :
if voucher . move_id :
move_pool . button_cancel ( cr , uid , [ voucher . move_id . id ] )
move_pool . unlink ( cr , uid , [ voucher . move_id . id ] )
2010-08-13 21:02:45 +00:00
line_ids = voucher_line_pool . search ( cr , uid , [ ( ' voucher_id ' , ' = ' , voucher . id ) , ( ' is_tax ' , ' = ' , True ) ] )
if line_ids :
voucher_line_pool . unlink ( cr , uid , line_ids )
2010-08-14 12:20:54 +00:00
2010-07-07 13:05:41 +00:00
res = {
2010-08-14 12:20:54 +00:00
' state ' : ' cancel ' ,
2010-07-07 13:05:41 +00:00
' move_id ' : False ,
}
self . write ( cr , uid , ids , res )
2008-10-23 05:53:55 +00:00
return True
2009-12-24 09:39:00 +00:00
2010-06-21 13:19:12 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
2008-10-23 05:53:55 +00:00
vouchers = self . read ( cr , uid , ids , [ ' state ' ] )
unlink_ids = [ ]
for t in vouchers :
if t [ ' state ' ] in ( ' draft ' , ' cancel ' ) :
unlink_ids . append ( t [ ' id ' ] )
else :
2010-02-15 06:28:47 +00:00
raise osv . except_osv ( ' Invalid action ! ' , ' Cannot delete Voucher(s) which are already opened or paid ! ' )
2010-06-21 13:19:12 +00:00
return super ( account_voucher , self ) . unlink ( cr , uid , unlink_ids , context = context )
2009-12-24 09:39:00 +00:00
2010-08-19 14:01:41 +00:00
def onchange_payment ( self , cr , uid , ids , pay_now , journal_id , partner_id , ttype ) :
partner_pool = self . pool . get ( ' res.partner ' )
2010-06-18 13:39:57 +00:00
2010-08-19 14:01:41 +00:00
res = { ' account_id ' : False }
if pay_now == ' pay_later ' and ttype in ( ' sale ' , ' purchase ' ) :
partner = partner_pool . browse ( cr , uid , partner_id )
if ttype == ' sale ' :
res . update ( {
' account_id ' : partner . property_account_receivable . id ,
} )
elif ttype == ' purchase ' :
res . update ( {
' account_id ' : partner . property_account_payable . id ,
} )
return {
' value ' : res
}
def action_move_line_create ( self , cr , uid , ids , * args ) :
2010-07-07 13:05:41 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
sequence_pool = self . pool . get ( ' ir.sequence ' )
move_pool = self . pool . get ( ' account.move ' )
move_line_pool = self . pool . get ( ' account.move.line ' )
analytic_pool = self . pool . get ( ' account.analytic.line ' )
currency_pool = self . pool . get ( ' res.currency ' )
2010-07-28 18:35:57 +00:00
invoice_pool = self . pool . get ( ' account.invoice ' )
2010-08-19 14:01:41 +00:00
2008-10-23 05:53:55 +00:00
for inv in self . browse ( cr , uid , ids ) :
2010-08-19 14:01:41 +00:00
2008-10-23 05:53:55 +00:00
if inv . move_id :
continue
2010-08-14 12:20:54 +00:00
2010-08-16 20:14:32 +00:00
if not inv . payment_ids :
raise osv . except_osv ( _ ( ' Error ! ' ) , _ ( ' Please define lines on voucher ! ' ) )
2010-08-19 14:01:41 +00:00
2010-07-07 13:05:41 +00:00
journal = journal_pool . browse ( cr , uid , inv . journal_id . id )
2010-08-13 21:02:45 +00:00
if journal . sequence_id :
name = sequence_pool . get_id ( cr , uid , journal . sequence_id . id )
2010-07-30 18:48:45 +00:00
else :
2010-08-13 21:02:45 +00:00
raise osv . except_osv ( _ ( ' Error ! ' ) , _ ( ' Please define sequence on journal ! ' ) )
2010-08-19 14:01:41 +00:00
2010-07-28 18:35:57 +00:00
ref = False
2010-08-13 21:02:45 +00:00
if inv . type in ( ' purchase ' , ' receipt ' ) :
2010-07-28 18:35:57 +00:00
ref = inv . reference
else :
ref = invoice_pool . _convert_ref ( cr , uid , name )
2010-08-19 14:01:41 +00:00
2010-07-28 18:35:57 +00:00
company_currency = inv . company_id . currency_id . id
diff_currency_p = inv . currency_id . id < > company_currency
2010-08-13 21:02:45 +00:00
2009-05-14 07:06:09 +00:00
move = {
2010-08-13 21:02:45 +00:00
' name ' : name ,
' journal_id ' : journal . id ,
' type ' : inv . type ,
' narration ' : inv . narration and inv . narration or inv . name ,
2010-07-14 05:51:50 +00:00
' date ' : inv . date ,
2010-07-28 18:35:57 +00:00
' ref ' : ref
2009-05-14 07:06:09 +00:00
}
2010-08-19 14:01:41 +00:00
2008-10-23 05:53:55 +00:00
if inv . period_id :
2010-07-07 13:05:41 +00:00
move . update ( {
' period_id ' : inv . period_id . id
} )
2010-08-19 14:01:41 +00:00
2010-07-07 13:05:41 +00:00
move_id = move_pool . create ( cr , uid , move )
2010-08-14 12:20:54 +00:00
2010-07-07 13:05:41 +00:00
#create the first line manually
2008-12-15 11:48:52 +00:00
move_line = {
2010-08-13 21:02:45 +00:00
' name ' : inv . name ,
' debit ' : False ,
2008-12-15 11:48:52 +00:00
' credit ' : False ,
2010-08-20 11:40:55 +00:00
' account_id ' : inv . account_id . id ,
2010-08-13 21:02:45 +00:00
' move_id ' : move_id ,
' journal_id ' : inv . journal_id . id ,
' period_id ' : inv . period_id . id ,
2010-08-19 14:01:41 +00:00
' partner_id ' : inv . partner_id . id ,
2010-07-28 18:35:57 +00:00
' ref ' : ref ,
2010-08-13 21:02:45 +00:00
' date ' : inv . date
2008-12-15 11:48:52 +00:00
}
2010-08-19 14:01:41 +00:00
2010-07-07 13:05:41 +00:00
if diff_currency_p :
amount_currency = currency_pool . compute ( cr , uid , inv . currency_id . id , company_currency , inv . amount )
inv . amount = amount_currency
move_line . update ( {
' amount_currency ' : amount_currency ,
' currency_id ' : inv . currency_id . id
} )
2010-08-14 12:20:54 +00:00
2010-08-20 13:47:29 +00:00
if inv . type in ( ' sale ' , ' receipt ' ) :
2010-08-19 14:01:41 +00:00
move_line . update ( {
' debit ' : inv . amount
} )
elif inv . type == ' purchase ' :
move_line . update ( {
' credit ' : inv . amount
2010-08-20 11:40:55 +00:00
} )
2010-08-14 12:20:54 +00:00
2010-07-07 13:05:41 +00:00
line_ids = [ ]
line_ids + = [ move_line_pool . create ( cr , uid , move_line ) ]
2010-08-13 21:02:45 +00:00
rec_ids = [ ]
2010-08-19 14:01:41 +00:00
for line in inv . payment_ids :
amount = 0.0
if inv . type in ( ' payment ' ) :
ref = line . ref
move_line = {
' name ' : line . name ,
' debit ' : False ,
' credit ' : False ,
' account_id ' : line . account_id . id or False ,
' move_id ' : move_id ,
' journal_id ' : inv . journal_id . id ,
' period_id ' : inv . period_id . id ,
' partner_id ' : line . partner_id . id or False ,
' ref ' : ref ,
' date ' : inv . date ,
' analytic_account_id ' : False
}
if diff_currency_p :
amount_currency = currency_pool . compute ( cr , uid , inv . currency_id . id , company_currency , line . amount )
line . amount = amount_currency
move_line . update ( {
' amount_currency ' : amount_currency ,
' currency_id ' : inv . currency_id . id
} )
if line . account_analytic_id :
move_line . update ( {
' analytic_account_id ' : line . account_analytic_id . id
} )
2010-08-20 13:47:29 +00:00
if inv . type in ( ' sale ' , ' receipt ' ) :
2010-08-19 14:01:41 +00:00
move_line . update ( {
' credit ' : line . amount or False
} )
if inv . type in ( ' purchase ' ) :
move_line . update ( {
' debit ' : line . amount or False
} )
move_line_id = move_line_pool . create ( cr , uid , move_line )
line_ids + = [ move_line_id ]
if inv . type in ( ' payment ' , ' receipt ' ) and line . move_id :
rec_ids + = [ move_line_id ]
for move_line in line . move_id . line_id :
if line . account_id . id == move_line . account_id . id :
rec_ids + = [ move_line . id ]
if inv . type in ( ' sale ' , ' purchase ' ) and inv . tax_amount > 0 :
2008-12-15 11:48:52 +00:00
move_line = {
2010-08-19 14:01:41 +00:00
' name ' : inv . tax_id . name ,
' account_id ' : False ,
2010-08-17 13:24:18 +00:00
' move_id ' : move_id ,
2010-08-19 14:01:41 +00:00
' journal_id ' : inv . journal_id . id ,
2010-08-17 13:24:18 +00:00
' period_id ' : inv . period_id . id ,
' partner_id ' : inv . partner_id . id ,
' ref ' : ref ,
' date ' : inv . date
2010-07-07 13:05:41 +00:00
}
2010-08-19 14:01:41 +00:00
account_id = False
if inv . tax_id and inv . tax_id . account_collected_id :
account_id = inv . tax_id . account_collected_id . id
if inv . type == ' sale ' :
move_line . update ( {
' credit ' : inv . tax_amount ,
' account_id ' : account_id and account_id or inv . journal_id . default_credit_account_id . id
} )
2010-08-14 12:20:54 +00:00
2010-08-19 14:01:41 +00:00
elif inv . type == ' purchase ' :
move_line . update ( {
' debit ' : inv . tax_amount ,
' account_id ' : account_id and account_id or inv . journal_id . default_debit_account_id . id
} )
move_line_id = move_line_pool . create ( cr , uid , move_line )
line_ids + = [ move_line_id ]
if rec_ids :
move_line_pool . reconcile_partial ( cr , uid , rec_ids )
2010-07-07 13:05:41 +00:00
rec = {
2010-08-19 14:01:41 +00:00
' move_id ' : move_id
2010-07-07 13:05:41 +00:00
}
2010-08-16 20:14:32 +00:00
2010-08-19 14:01:41 +00:00
message = _ ( ' Voucher ' ) + " ' " + inv . name + " ' " + _ ( " is confirm " )
2010-07-14 13:45:23 +00:00
self . log ( cr , uid , inv . id , message )
2010-08-19 14:01:41 +00:00
2010-07-07 13:05:41 +00:00
self . write ( cr , uid , [ inv . id ] , rec )
2010-08-16 20:14:32 +00:00
move_pool . post ( cr , uid , [ move_id ] , context = { } )
2008-10-23 05:53:55 +00:00
return True
2009-05-14 07:06:09 +00:00
2008-10-23 05:53:55 +00:00
def _convert_ref ( self , cr , uid , ref ) :
return ( ref or ' ' ) . replace ( ' / ' , ' ' )
2009-12-24 09:39:00 +00:00
2010-08-14 12:20:54 +00:00
def copy ( self , cr , uid , id , default = { } , context = None ) :
2010-08-13 21:02:45 +00:00
res = {
' state ' : ' draft ' ,
' number ' : False ,
2010-08-19 14:01:41 +00:00
' move_id ' : False
2008-12-15 11:48:52 +00:00
}
2010-08-13 21:02:45 +00:00
default . update ( res )
2008-10-23 05:53:55 +00:00
if ' date ' not in default :
default [ ' date ' ] = time . strftime ( ' % Y- % m- %d ' )
return super ( account_voucher , self ) . copy ( cr , uid , id , default , context )
2010-08-18 20:13:20 +00:00
def fields_view_get ( self , cr , uid , view_id = None , view_type = ' form ' , context = None , toolbar = False , submenu = False ) :
"""
Returns views and fields for current model where view will depend on { view_type } .
@param cr : A database cursor
@param user : ID of the user currently logged in
@param view_id : list of fields , which required to read signatures
@param view_type : defines a view type . it can be one of ( form , tree , graph , calender , gantt , search , mdx )
@param context : context arguments , like lang , time zone
@param toolbar : contains a list of reports , wizards , and links related to current model
@return : Returns a dict that contains definition for fields , views , and toolbars
"""
data_pool = self . pool . get ( ' ir.model.data ' )
journal_pool = self . pool . get ( ' account.journal ' )
voucher_type = {
' sale ' : ' view_sale_receipt_form ' ,
' purchase ' : ' view_purchase_receipt_form ' ,
' payment ' : ' view_vendor_payment_form ' ,
' receipt ' : ' view_vendor_receipt_form '
}
2009-12-24 09:39:00 +00:00
2010-08-18 20:13:20 +00:00
if view_type == ' form ' :
tview = voucher_type . get ( context . get ( ' type ' ) )
result = data_pool . _get_id ( cr , uid , ' account_voucher ' , tview )
view_id = data_pool . browse ( cr , uid , result , context = context ) . res_id
res = super ( account_voucher , self ) . fields_view_get ( cr , uid , view_id , view_type , context , toolbar , submenu )
#Restrict the list of journal view in search view
if view_type == ' search ' :
journal_list = journal_pool . name_search ( cr , uid , ' ' , [ ] , context = context )
2010-08-20 11:40:55 +00:00
res [ ' fields ' ] [ ' journal_id ' ] [ ' selection ' ] = journal_list
2010-08-18 20:13:20 +00:00
return res
2008-10-23 05:53:55 +00:00
account_voucher ( )
2010-02-16 07:06:49 +00:00
class account_voucher_line ( osv . osv ) :
2008-10-23 05:53:55 +00:00
_name = ' account.voucher.line '
_description = ' Voucher Line '
2010-08-17 13:24:18 +00:00
2008-10-23 05:53:55 +00:00
_columns = {
' voucher_id ' : fields . many2one ( ' account.voucher ' , ' Voucher ' ) ,
2010-08-20 11:40:55 +00:00
' name ' : fields . char ( ' Description ' , size = 256 , required = True ) ,
2010-05-12 09:05:03 +00:00
' account_id ' : fields . many2one ( ' account.account ' , ' Account ' , required = True , domain = [ ( ' type ' , ' <> ' , ' view ' ) ] ) ,
2010-08-17 07:32:51 +00:00
' partner_id ' : fields . related ( ' voucher_id ' , ' partner_id ' , type = ' many2one ' , relation = ' res.partner ' , string = ' Partner ' ) ,
2008-10-23 05:53:55 +00:00
' amount ' : fields . float ( ' Amount ' ) ,
2010-08-18 20:13:20 +00:00
' type ' : fields . selection ( [ ( ' dr ' , ' Debit ' ) , ( ' cr ' , ' Credit ' ) ] , ' Cr/Dr ' ) ,
2009-12-22 09:38:41 +00:00
' ref ' : fields . char ( ' Reference ' , size = 32 ) ,
2010-08-13 21:02:45 +00:00
' account_analytic_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' ) ,
' is_tax ' : fields . boolean ( ' Tax ? ' , required = False ) ,
2010-08-18 20:13:20 +00:00
' stype ' : fields . selection ( [ ( ' service ' , ' Service ' ) , ( ' other ' , ' Other ' ) ] , ' Product Type ' ) ,
2010-08-19 07:57:59 +00:00
' move_line_id ' : fields . many2one ( ' account.move.line ' , ' Journal Item ' ) ,
2010-08-20 13:47:29 +00:00
' date_original ' : fields . related ( ' move_line_id ' , ' date ' , type = ' date ' , relation = ' account.move.line ' , string = ' Date ' , readonly = " 1 " ) ,
# 'date_original': fields.date('Date', readonly="1"), #fields.related account.move.line
' date_due ' : fields . related ( ' move_line_id ' , ' date_maturity ' , type = ' date ' , relation = ' account.move.line ' , string = ' Due Date ' , readonly = " 1 " ) ,
# 'date_due': fields.date('Due Date', readonly="1"), #fields.related account.move.line
2010-08-19 07:57:59 +00:00
' amount_original ' : fields . float ( ' Originial Amount ' , readonly = " 1 " ) , #fields.related account.move.line
2010-08-20 13:47:29 +00:00
' amount_unreconciled ' : fields . related ( ' move_line_id ' , ' balance ' , type = ' float ' , relation = ' account.move.line ' , string = ' Open Balance ' , readonly = " 1 " ) ,
# 'amount_unreconciled': fields.float('Open Balance', readonly="1"), #fields.related account.move.line
2010-08-19 14:01:41 +00:00
' move_id ' : fields . many2one ( ' account.move ' , ' Bill / Invoice ' ) ,
2008-10-23 05:53:55 +00:00
}
_defaults = {
2010-08-20 13:47:29 +00:00
' type ' : lambda * a : ' cr ' ,
' name ' : lambda * a : ' Payment '
2008-10-23 05:53:55 +00:00
}
2009-12-24 09:39:00 +00:00
2010-08-20 11:40:55 +00:00
def default_get ( self , cr , user , fields_list , context = None ) :
"""
Returns default values for fields
@param cr : A database cursor
@param user : ID of the user currently logged in
@param fields_list : list of fields , for which default values are required to be read
@param context : context arguments , like lang , time zone
@return : Returns a dict that contains default values for fields
"""
journal_pool = self . pool . get ( ' account.journal ' )
partner_pool = self . pool . get ( ' res.partner ' )
account_id = False
values = super ( account_voucher_line , self ) . default_get ( cr , user , fields_list , context = context )
journal_id = context . get ( ' journal_id ' , False )
ttype = context . get ( ' type ' , False )
partner_id = context . get ( ' partner_id ' , False )
if ttype and journal_id and ttype in ( ' sale ' , ' purchase ' ) :
journal = journal_pool . browse ( cr , user , journal_id )
if ttype == ' sale ' and journal :
account_id = journal . default_credit_account_id and journal . default_credit_account_id . id or False
elif ttype == ' purchase ' and journal :
account_id = journal . default_credit_account_id and journal . default_debit_account_id . id or False
elif ttype and partner_id and ttype in ( ' payment ' , ' receipt ' ) :
partner = partner_pool . browse ( cr , user , partner_id )
if ttype == ' receipt ' and partner :
account_id = partner . property_account_receivable and partner . property_account_receivable . id or False
elif ttype == ' payment ' and partner :
account_id = partner . property_account_receivable and partner . property_account_payable . id or False
else :
account_id = False
values . update ( {
' account_id ' : account_id
} )
return values
2010-02-16 07:06:49 +00:00
account_voucher_line ( )