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-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
journal2type = {
2010-08-13 21:02:45 +00:00
' bank ' : ' receipt ' ,
' cash ' : ' receipt ' ,
' sale ' : ' sale ' ,
' purchase ' : ' purchase '
2010-07-07 13:05:41 +00:00
}
2008-10-23 05:53:55 +00:00
2010-05-26 19:30:21 +00:00
type2journal = {
' rec_voucher ' : ' cash ' ,
2010-05-27 19:55:40 +00:00
' bank_rec_voucher ' : ' bank ' ,
2010-05-26 19:30:21 +00:00
' pay_voucher ' : ' cash ' ,
2010-05-27 19:55:40 +00:00
' bank_pay_voucher ' : ' bank ' ,
2010-05-26 19:30:21 +00:00
' cont_voucher ' : ' cash ' ,
' journal_sale_vou ' : ' sale ' ,
' journal_pur_voucher ' : ' purchase ' ,
2010-05-27 19:55:40 +00:00
' journal_voucher ' : ' general '
2010-05-26 19:30:21 +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 ( )
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
2008-10-23 05:53:55 +00:00
def _get_type ( self , cr , uid , context = { } ) :
2010-07-07 13:05:41 +00:00
vtype = context . get ( ' type ' , ' bank ' )
voucher_type = journal2type . get ( vtype )
return voucher_type
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def _get_reference_type ( self , cursor , user , context = None ) :
return [ ( ' none ' , ' Free Reference ' ) ]
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
2009-12-24 09:39:00 +00:00
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 "
2008-10-23 05:53:55 +00:00
_columns = {
' name ' : fields . char ( ' Name ' , size = 256 , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' type ' : fields . selection ( [
2010-08-13 21:02:45 +00:00
( ' payment ' , ' Payment ' ) ,
( ' receipt ' , ' Receipt ' ) ,
( ' sale ' , ' Sales ' ) ,
( ' purchase ' , ' Purchase ' ) ] ,
' Default Type ' , select = True , size = 128 , 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-05-12 09:05:03 +00:00
' account_id ' : fields . many2one ( ' account.account ' , ' Account ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , domain = [ ( ' type ' , ' <> ' , ' view ' ) ] ) ,
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. ' ) ,
2009-05-14 07:06:09 +00:00
' amount ' : fields . float ( ' Amount ' , readonly = True ) ,
2010-08-11 14:49:18 +00:00
' reference ' : fields . char ( ' Reference ' , 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-14 05:51:50 +00:00
' reference_type ' : fields . selection ( _get_reference_type , ' Reference Type ' , required = True ) ,
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:02:45 +00:00
#'move_ids':fields.many2many('account.move.line', 'voucher_id', 'account_id', 'rel_account_move', 'Real Entry', 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 ? ' ) ,
2008-10-23 05:53:55 +00:00
}
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
_defaults = {
' period_id ' : _get_period ,
' type ' : _get_type ,
' journal_id ' : _get_journal ,
' currency_id ' : _get_currency ,
2010-05-27 19:55:40 +00:00
' state ' : lambda * a : ' draft ' ,
' date ' : lambda * a : time . strftime ( ' % Y- % m- %d ' ) ,
' reference_type ' : lambda * a : " none " ,
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
}
2009-12-24 09:39:00 +00:00
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 ,
}
2008-10-23 05:53:55 +00:00
def onchange_account ( self , cr , uid , ids , account_id ) :
if not account_id :
2010-05-27 19:55:40 +00:00
return {
' value ' : { ' amount ' : False }
}
account = self . pool . get ( ' account.account ' ) . browse ( cr , uid , account_id )
2008-10-23 05:53:55 +00:00
balance = account . balance
2010-05-27 19:55:40 +00:00
return {
' value ' : { ' amount ' : balance }
}
2008-10-23 05:53:55 +00:00
2010-08-13 21:02:45 +00:00
def onchange_journal ( self , cr , uid , ids , journal_id , ttype ) :
res = { ' account_id ' : False }
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_id and ( ttype in ( ' receipt ' , ' purchase ' ) ) :
account_id = journal . default_debit_account_id
res . update ( {
' account_id ' : account_id . id
} )
elif journal_id and ( ttype in ( ' payment ' , ' sale ' ) ) :
account_id = journal . default_credit_account_id
res . update ( {
' account_id ' : account_id . id
} )
2008-10-23 05:53:55 +00:00
else :
2010-08-13 21:02:45 +00:00
account_id = journal . default_credit_account_id
res . update ( {
' account_id ' : account_id . id
} )
2009-12-24 09:39:00 +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
}
def open_voucher ( self , dbcr , uid , ids , context = { } ) :
cr = 0.0
dr = 0.0
total = 0.0
new_line = [ ]
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
position_pool = self . pool . get ( ' account.fiscal.position ' )
voucher_pool = self . pool . get ( ' account.voucher ' )
voucher_line_pool = self . pool . get ( ' account.voucher.line ' )
partner_pool = self . pool . get ( ' res.partner ' )
tax_pool = self . pool . get ( ' account.tax ' )
line_ids = voucher_line_pool . search ( dbcr , uid , [ ( ' voucher_id ' , ' = ' , ids [ 0 ] ) , ( ' is_tax ' , ' = ' , True ) ] )
if line_ids :
voucher_line_pool . unlink ( dbcr , uid , line_ids )
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
voucher = voucher_pool . browse ( dbcr , uid , ids [ 0 ] )
if voucher . account_id . tax_ids :
if voucher . account_id . tax_ids :
for line in voucher . payment_ids :
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
if line . amount < = 0 :
raise osv . except_osv ( _ ( ' Invalid amount ! ' ) , _ ( ' You can not create Pro-Forma voucher with Total amount <= 0 ! ' ) )
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
part = line . partner_id and partner_pool . browse ( dbcr , uid , line . partner_id . id ) or False
taxes = position_pool . map_tax ( dbcr , uid , part and part . property_account_position or False , voucher . account_id . tax_ids )
taxes = tax_pool . browse ( dbcr , uid , taxes )
new_price = line . amount
for tax in tax_pool . compute_all ( dbcr , uid , taxes , line . amount , 1 ) . get ( ' taxes ' ) :
tax_line = {
' name ' : " %s / %s " % ( line . name , tax . get ( ' name ' ) ) ,
' amount ' : tax . get ( ' amount ' ) ,
' voucher_id ' : voucher . id ,
' is_tax ' : True
}
crdr = False
account = False
if voucher . type == ' purchase ' :
crdr = ' dr '
account = tax . get ( ' account_paid_id ' )
elif voucher . type == ' sale ' :
crdr = ' cr '
account = tax . get ( ' account_collected_id ' , account )
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
tax_line . update ( {
' account_id ' : account or voucher . account_id . id ,
' type ' : crdr
} )
new_line + = [ tax_line ]
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
if new_line :
for line in new_line :
voucher_line_pool . create ( dbcr , uid , line )
voucher = voucher_pool . browse ( dbcr , uid , ids [ 0 ] )
2010-07-07 13:05:41 +00:00
for line in voucher . payment_ids :
2010-08-13 21:02:45 +00:00
if line . type == ' cr ' :
cr + = line . amount
else :
dr + = line . amount
if cr > dr :
total = cr - dr
else :
total = dr - cr
2010-08-14 12:20:54 +00:00
2010-05-17 13:17:13 +00:00
if total != 0 :
2010-07-07 13:05:41 +00:00
res = {
2010-08-15 12:48:12 +00:00
' amount ' : total ,
' state ' : voucher . state
2010-07-07 13:05:41 +00:00
}
2010-08-13 21:02:45 +00:00
self . write ( dbcr , uid , ids , res )
2010-02-15 06:28:47 +00:00
else :
2010-08-13 21:02:45 +00:00
raise osv . except_osv ( _ ( ' Invalid amount ! ' ) , _ ( ' You can not create Pro-Forma voucher with Total amount <= 0 ! ' ) )
2008-10-23 05:53:55 +00:00
return True
2010-08-15 12:48:12 +00:00
def write ( self , cr , uid , ids , vals , context = { } ) :
res = super ( account_voucher , self ) . write ( cr , uid , ids , vals , context )
2010-08-15 12:55:29 +00:00
#If there is state says that method called from the work flow signals
2010-08-15 12:48:12 +00:00
if not ' state ' in vals . keys ( ) :
self . open_voucher ( cr , uid , ids , context )
return res
2010-08-14 07:53:03 +00:00
def voucher_recheck ( self , cr , uid , ids , context = { } ) :
2010-08-15 12:48:12 +00:00
#self.open_voucher(cr, uid, ids, context)
2010-08-14 07:53:03 +00:00
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 = { } ) :
2010-08-15 12:48:12 +00:00
#self.open_voucher(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-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-07-09 04:54:54 +00:00
def audit_pass ( self , cr , uid , ids , context = { } ) :
move_pool = self . pool . get ( ' account.move ' )
result = True
audit_pass = [ ]
for voucher in self . browse ( cr , uid , ids ) :
if voucher . move_id and voucher . move_id . state == ' draft ' :
result = result and move_pool . button_validate ( cr , uid , [ voucher . move_id . id ] )
audit_pass + = [ voucher . id ]
2010-08-14 12:20:54 +00:00
2010-07-09 04:54:54 +00:00
self . write ( cr , uid , audit_pass , { ' state ' : ' audit ' } )
return result
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
2008-10-23 05:53:55 +00:00
def action_move_line_create ( self , cr , uid , ids , * args ) :
2010-06-18 13:39:57 +00:00
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-14 12:20:54 +00:00
2008-10-23 05:53:55 +00:00
for inv in self . browse ( cr , uid , ids ) :
2010-08-14 12:20:54 +00:00
2008-10-23 05:53:55 +00:00
if inv . move_id :
continue
2010-08-14 12:20:54 +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-14 12:20:54 +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-14 12:20:54 +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-14 12:20:54 +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-14 12:20:54 +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-13 21:02:45 +00:00
' account_id ' : inv . account_id . id or False ,
' move_id ' : move_id ,
' journal_id ' : inv . journal_id . id ,
' period_id ' : inv . period_id . id ,
' partner_id ' : False ,
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-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-13 21:02:45 +00:00
if inv . type in ( ' receipt ' , ' purchase ' ) :
2008-12-15 11:48:52 +00:00
move_line [ ' debit ' ] = inv . amount
else :
2010-07-07 13:05:41 +00:00
move_line [ ' credit ' ] = inv . amount
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-14 12:20:54 +00:00
2008-12-15 11:48:52 +00:00
for line in inv . payment_ids :
2010-07-07 13:05:41 +00:00
amount = 0.0
2010-08-13 21:02:45 +00:00
if inv . type in ( ' payment ' ) :
2010-07-28 18:35:57 +00:00
ref = line . ref
2010-08-14 12:20:54 +00:00
2008-12-15 11:48:52 +00:00
move_line = {
2010-07-28 18:35:57 +00:00
' 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
2010-07-07 13:05:41 +00:00
}
2010-08-14 12:20:54 +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 , line . amount )
line . 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-07-07 13:05:41 +00:00
if line . account_analytic_id :
move_line . update ( {
' analytic_account_id ' : line . account_analytic_id . id
} )
2010-08-14 12:20:54 +00:00
2008-12-15 11:48:52 +00:00
if line . type == ' dr ' :
2010-07-07 13:05:41 +00:00
move_line . update ( {
' debit ' : line . amount or False
} )
amount = line . amount
2010-08-14 12:20:54 +00:00
2008-12-15 11:48:52 +00:00
elif line . type == ' cr ' :
2010-07-07 13:05:41 +00:00
move_line . update ( {
' credit ' : line . amount or False
} )
2010-08-13 21:02:45 +00:00
amount = line . amount
2010-08-14 12:20:54 +00:00
2010-07-07 13:05:41 +00:00
move_line_id = move_line_pool . create ( cr , uid , move_line )
line_ids + = [ move_line_id ]
2010-08-14 12:20:54 +00:00
2010-07-07 13:05:41 +00:00
rec = {
' move_id ' : move_id ,
' move_ids ' : [ ( 6 , 0 , line_ids ) ]
}
2010-08-14 12:20:54 +00:00
2010-07-22 08:43:54 +00:00
message = _ ( ' Voucher ' ) + " ' " + inv . name + " ' " + _ ( " is confirmed " )
2010-07-14 13:45:23 +00:00
self . log ( cr , uid , inv . id , message )
2010-08-14 12:20:54 +00:00
2010-07-07 13:05:41 +00:00
self . write ( cr , uid , [ inv . id ] , rec )
2010-08-14 12:20:54 +00:00
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-13 21:02:45 +00:00
# def name_get(self, cr, uid, ids, context={}):
# if not len(ids):
# return []
# types = {
# 'pay_voucher': 'CPV: ',
# 'rec_voucher': 'CRV: ',
# 'cont_voucher': 'CV: ',
# 'bank_pay_voucher': 'BPV: ',
# 'bank_rec_voucher': 'BRV: ',
# 'journal_sale_vou': 'JSV: ',
# 'journal_pur_voucher': 'JPV: ',
# 'journal_voucher':'JV'
# }
# return [(r['id'], types[r['type']]+(r['number'] or '')+' '+(r['name'] or '')) for r in self.read(cr, uid, ids, ['type', 'number', 'name'], context, load='_classic_write')]
# def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
# if not args:
# args=[]
# if not context:
# context={}
# ids = []
# if name:
# ids = self.search(cr, user, [('number','=',name)]+args, limit=limit, context=context)
# if not ids:
# ids = self.search(cr, user, [('name',operator,name)]+args, limit=limit, context=context)
# return self.name_get(cr, user, ids, context)
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 ,
' move_id ' : False ,
' payment_ids ' : 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 )
2009-12-24 09:39:00 +00:00
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 '
_columns = {
' voucher_id ' : fields . many2one ( ' account.voucher ' , ' Voucher ' ) ,
2010-08-16 14:56:27 +00:00
' name ' : fields . char ( ' Memo ' , size = 256 , required = True ) ,
2010-05-12 09:05:03 +00:00
' account_id ' : fields . many2one ( ' account.account ' , ' Account ' , required = True , domain = [ ( ' type ' , ' <> ' , ' view ' ) ] ) ,
2009-05-14 07:06:09 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' , change_default = True ) ,
2008-10-23 05:53:55 +00:00
' amount ' : fields . float ( ' Amount ' ) ,
' type ' : fields . selection ( [ ( ' dr ' , ' Debit ' ) , ( ' cr ' , ' Credit ' ) ] , ' Type ' ) ,
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 ) ,
2008-10-23 05:53:55 +00:00
}
_defaults = {
' type ' : lambda * a : ' cr '
}
2009-12-24 09:39:00 +00:00
2010-08-13 21:02:45 +00:00
def onchange_type ( self , cr , uid , ids , partner_id , ttype , type1 , currency ) :
currency_pool = self . pool . get ( ' res.currency ' )
company = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
vals = {
2010-08-14 12:20:54 +00:00
' account_id ' : False ,
2010-08-13 21:02:45 +00:00
' type ' : False ,
' amount ' : False
}
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
if not partner_id :
return {
' value ' : vals
}
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
partner_pool = self . pool . get ( ' res.partner ' )
account_id = False
partner = partner_pool . browse ( cr , uid , partner_id )
balance = 0.0
if ttype == ' cr ' and type1 in ( ' receipt ' ) :
account_id = partner . property_account_receivable . id
balance = partner . credit
elif ttype == ' dr ' and type1 in ( ' payment ' ) :
account_id = partner . property_account_payable . id
balance = partner . debit
elif ttype == ' dr ' and type1 in ( ' sale ' ) :
account_id = partner . property_account_receivable . id
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
elif ttype == ' cr ' and type1 in ( ' purchase ' ) :
account_id = partner . property_account_payable . id
else :
raise osv . except_osv ( _ ( ' Invalid Configuration ! ' ) , _ ( ' You can not encode unbalanced entry ! ' ) )
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
if company . currency_id != currency :
balance = currency_pool . compute ( cr , uid , company . currency_id . id , currency , balance )
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
vals . update ( {
2010-08-14 12:20:54 +00:00
' account_id ' : account_id ,
' type ' : ttype ,
2010-08-13 21:02:45 +00:00
' amount ' : balance
} )
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
return {
' value ' : vals
}
2010-08-14 12:20:54 +00:00
2010-07-13 09:01:30 +00:00
def onchange_partner ( self , cr , uid , ids , partner_id , ttype , type1 , currency ) :
currency_pool = self . pool . get ( ' res.currency ' )
company = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id
2010-08-14 12:20:54 +00:00
2010-07-13 09:01:30 +00:00
vals = {
2010-08-14 12:20:54 +00:00
' account_id ' : False ,
2010-07-13 09:01:30 +00:00
' type ' : False ,
' amount ' : False
}
2010-08-14 12:20:54 +00:00
2008-10-23 05:53:55 +00:00
if not partner_id :
2010-07-07 15:04:16 +00:00
return {
2010-08-13 21:02:45 +00:00
' value ' : vals
2010-07-07 15:04:16 +00:00
}
2010-08-14 12:20:54 +00:00
2010-07-07 15:04:16 +00:00
partner_pool = self . pool . get ( ' res.partner ' )
2008-10-23 05:53:55 +00:00
account_id = False
2010-07-07 15:04:16 +00:00
partner = partner_pool . browse ( cr , uid , partner_id )
2010-07-30 18:48:45 +00:00
balance = 0.0
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
if type1 in ( ' receipt ' ) :
2010-07-07 15:04:16 +00:00
account_id = partner . property_account_receivable . id
balance = partner . credit
2009-05-14 07:06:09 +00:00
ttype = ' cr '
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
elif type1 in ( ' payment ' ) :
2010-07-07 15:04:16 +00:00
account_id = partner . property_account_payable . id
balance = partner . debit
2009-05-14 07:06:09 +00:00
ttype = ' dr '
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
elif type1 in ( ' sale ' ) :
2010-07-07 15:04:16 +00:00
account_id = partner . property_account_receivable . id
2009-05-14 07:06:09 +00:00
ttype = ' dr '
2010-08-14 12:20:54 +00:00
2010-08-13 21:02:45 +00:00
elif type1 in ( ' purchase ' ) :
2010-07-07 15:04:16 +00:00
account_id = partner . property_account_payable . id
2009-12-24 09:39:00 +00:00
ttype = ' cr '
2010-08-13 21:02:45 +00:00
2010-07-13 09:01:30 +00:00
if company . currency_id != currency :
balance = currency_pool . compute ( cr , uid , company . currency_id . id , currency , balance )
2010-08-14 12:20:54 +00:00
2010-07-07 15:04:16 +00:00
vals . update ( {
2010-08-14 12:20:54 +00:00
' account_id ' : account_id ,
' type ' : ttype ,
2010-07-07 15:04:16 +00:00
' amount ' : balance
} )
2010-08-14 12:20:54 +00:00
2008-10-23 05:53:55 +00:00
return {
2010-08-13 21:02:45 +00:00
' value ' : vals
2008-12-15 11:48:52 +00:00
}
2010-08-14 12:20:54 +00:00
2010-02-16 07:06:49 +00:00
account_voucher_line ( )