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
import netsvc
2010-05-27 19:55:40 +00:00
from osv import fields
from osv import osv
2008-10-23 05:53:55 +00:00
import ir
import pooler
import mx . DateTime
from mx . DateTime import RelativeDateTime
from tools import config
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-05-26 19:30:21 +00:00
' max_amount ' : fields . float ( ' Verify Transection ' , digits = ( 16 , int ( config [ ' price_accuracy ' ] ) ) , help = " Validate voucher entry twice before posting it, if transection 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 ' )
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-05-26 19:30:21 +00:00
type = context . get ( ' type ' , ' bank_rec_voucher ' )
2008-10-23 05:53:55 +00:00
return 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 ' )
if context . get ( ' journal_id ' , False ) :
return context . get ( ' journal_id ' )
type_inv = context . get ( ' type ' , ' rec_voucher ' )
ttype = type2journal . get ( type_inv , type_inv )
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-05-26 19:30:21 +00:00
user = pooler . get_pool ( cr . dbname ) . 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 :
return pooler . get_pool ( cr . dbname ) . 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 '
_order = " number "
_columns = {
' name ' : fields . char ( ' Name ' , size = 256 , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' type ' : fields . selection ( [
2010-05-26 18:17:02 +00:00
( ' pay_voucher ' , ' Cash Payment ' ) ,
( ' bank_pay_voucher ' , ' Bank Payment ' ) ,
( ' rec_voucher ' , ' Cash Receipt ' ) ,
( ' bank_rec_voucher ' , ' Bank Receipt ' ) ,
( ' cont_voucher ' , ' Contra ' ) ,
( ' journal_sale_vou ' , ' Journal Sale ' ) ,
( ' journal_pur_voucher ' , ' Journal Purchase ' ) ,
2009-05-14 07:06:09 +00:00
( ' journal_voucher ' , ' Journal Voucher ' ) ,
2010-05-27 19:55:40 +00:00
] , ' Entry Type ' , select = True , size = 128 , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2008-10-23 05:53:55 +00:00
' date ' : fields . date ( ' Date ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' 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 ' ) ] ) ,
2008-10-23 05:53:55 +00:00
' payment_ids ' : fields . one2many ( ' account.voucher.line ' , ' voucher_id ' , ' Voucher Lines ' , readonly = False , states = { ' proforma ' : [ ( ' readonly ' , True ) ] } ) ,
' period_id ' : fields . many2one ( ' account.period ' , ' Period ' , required = True , states = { ' posted ' : [ ( ' readonly ' , True ) ] } ) ,
2009-05-14 07:06:09 +00:00
' narration ' : fields . text ( ' Narration ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , required = True ) ,
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-05-26 18:00:17 +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 ) ,
2008-10-23 05:53:55 +00:00
' number ' : fields . char ( ' Number ' , size = 32 , readonly = True ) ,
' reference ' : fields . char ( ' Voucher Reference ' , size = 64 ) ,
' reference_type ' : fields . selection ( _get_reference_type , ' Reference Type ' ,
required = True ) ,
' move_id ' : fields . many2one ( ' account.move ' , ' Account Entry ' ) ,
' move_ids ' : fields . many2many ( ' account.move.line ' , ' voucher_id ' , ' account_id ' , ' rel_account_move ' , ' Real Entry ' ) ,
2009-05-14 07:06:09 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } )
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 " ,
' 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-05-27 19:55:40 +00:00
# def _get_analityc_lines(self, cr, uid, id):
# inv = self.browse(cr, uid, [id])[0]
# cur_obj = self.pool.get('res.currency')
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def onchange_account ( self , cr , uid , ids , account_id ) :
2010-05-27 19:55:40 +00:00
2008-10-23 05:53:55 +00:00
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-02-16 07:06:49 +00:00
def onchange_journal ( self , cr , uid , ids , journal_id , type ) :
2008-10-23 05:53:55 +00:00
if not journal_id :
2010-05-27 19:55:40 +00:00
return {
' value ' : { ' account_id ' : False }
}
2008-10-23 05:53:55 +00:00
journal = self . pool . get ( ' account.journal ' )
2010-05-27 19:55:40 +00:00
2009-05-14 07:06:09 +00:00
if journal_id and ( type in ( ' rec_voucher ' , ' bank_rec_voucher ' , ' journal_pur_voucher ' , ' journal_voucher ' ) ) :
2008-10-23 05:53:55 +00:00
account_id = journal . browse ( cr , uid , journal_id ) . default_debit_account_id
2010-05-27 19:55:40 +00:00
return {
' value ' : { ' account_id ' : account_id . id }
}
2009-05-14 07:06:09 +00:00
elif journal_id and ( type in ( ' pay_voucher ' , ' bank_pay_voucher ' , ' journal_sale_vou ' ) ) :
2008-10-23 05:53:55 +00:00
account_id = journal . browse ( cr , uid , journal_id ) . default_credit_account_id
2010-05-27 19:55:40 +00:00
return {
' value ' : { ' account_id ' : account_id . id }
}
2008-10-23 05:53:55 +00:00
else :
account_id = journal . browse ( cr , uid , journal_id ) . default_credit_account_id
2010-05-27 19:55:40 +00:00
return {
' value ' : { ' account_id ' : account_id . id }
}
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def open_voucher ( self , cr , uid , ids , context = { } ) :
2010-05-27 19:55:40 +00:00
obj = self . pool . get ( ' account.voucher ' ) . browse ( cr , uid , ids )
2010-05-17 13:17:13 +00:00
total = 0
2008-10-23 05:53:55 +00:00
for i in obj [ 0 ] . payment_ids :
2010-05-17 13:17:13 +00:00
total + = i . amount
if total != 0 :
self . write ( cr , uid , ids , { ' amount ' : total , ' state ' : ' proforma ' } )
2010-02-15 06:28:47 +00:00
else :
2010-05-17 13:17:13 +00:00
raise osv . except_osv ( ' Invalid action ! ' , ' You cannot post to Pro-Forma a voucher with Total amount = 0 ! ' )
2008-10-23 05:53:55 +00:00
return True
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def proforma_voucher ( self , cr , uid , ids , context = { } ) :
self . action_number ( cr , uid , ids )
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
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def cancel_voucher ( self , cr , uid , ids , context = { } ) :
self . action_cancel ( cr , uid , ids )
self . write ( cr , uid , ids , { ' state ' : ' cancel ' } )
return True
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def action_cancel_draft ( self , cr , uid , ids , * args ) :
self . write ( cr , uid , ids , { ' state ' : ' draft ' } )
return True
2009-05-14 07:06:09 +00:00
def unlink ( self , cr , uid , ids , context = { } ) :
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-05-27 19:55:40 +00:00
return super ( account_voucher , self ) . unlink ( self , cr , uid , unlink_ids )
2009-12-24 09:39:00 +00:00
2008-12-02 18:08:12 +00:00
def _get_analytic_lines ( self , cr , uid , id ) :
2008-10-23 05:53:55 +00:00
inv = self . browse ( cr , uid , [ id ] ) [ 0 ]
cur_obj = self . pool . get ( ' res.currency ' )
company_currency = inv . company_id . currency_id . id
if inv . type in ( ' rec_voucher ' ) :
sign = 1
else :
sign = - 1
iml = self . pool . get ( ' account.voucher.line ' ) . move_line_get ( cr , uid , inv . id )
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
for il in iml :
if il [ ' account_analytic_id ' ] :
2009-10-26 13:50:06 +00:00
if inv . type in ( ' pay_voucher ' , ' rec_voucher ' , ' cont_voucher ' , ' bank_pay_voucher ' , ' bank_rec_voucher ' , ' journal_sale_vou ' , ' journal_pur_voucher ' ) :
2008-10-23 05:53:55 +00:00
ref = inv . reference
else :
ref = self . _convert_ref ( cr , uid , inv . number )
2009-12-24 09:39:00 +00:00
2008-12-15 11:48:52 +00:00
il [ ' analytic_lines ' ] = [ ( 0 , 0 , {
2008-10-23 05:53:55 +00:00
' name ' : il [ ' name ' ] ,
' date ' : inv [ ' date ' ] ,
' account_id ' : il [ ' account_analytic_id ' ] ,
' amount ' : inv [ ' amount ' ] * sign ,
' general_account_id ' : il [ ' account_id ' ] or False ,
2008-12-15 08:44:24 +00:00
' journal_id ' : self . pool . get ( ' account.voucher ' ) . browse ( cr , uid , id ) . journal_id . analytic_journal_id . id or False ,
2008-10-23 05:53:55 +00:00
' ref ' : ref ,
} ) ]
return iml
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-05-27 19:55:40 +00:00
2008-10-23 05:53:55 +00:00
for inv in self . browse ( cr , uid , ids ) :
2010-05-27 19:55:40 +00:00
2008-10-23 05:53:55 +00:00
if inv . move_id :
continue
2010-05-27 19:55:40 +00:00
2008-10-23 05:53:55 +00:00
company_currency = inv . company_id . currency_id . id
2008-12-15 11:48:52 +00:00
2008-10-23 05:53:55 +00:00
line_ids = self . read ( cr , uid , [ inv . id ] , [ ' payment_ids ' ] ) [ 0 ] [ ' payment_ids ' ]
ils = self . pool . get ( ' account.voucher.line ' ) . read ( cr , uid , line_ids )
2008-12-15 11:48:52 +00:00
2008-12-02 18:08:12 +00:00
iml = self . _get_analytic_lines ( cr , uid , inv . id )
2008-12-15 11:48:52 +00:00
2008-10-23 05:53:55 +00:00
diff_currency_p = inv . currency_id . id < > company_currency
2008-12-15 11:48:52 +00:00
2008-10-23 05:53:55 +00:00
total = 0
2009-10-26 13:50:06 +00:00
if inv . type in ( ' pay_voucher ' , ' journal_voucher ' , ' rec_voucher ' , ' cont_voucher ' , ' bank_pay_voucher ' , ' bank_rec_voucher ' , ' journal_sale_vou ' , ' journal_pur_voucher ' ) :
2008-10-23 05:53:55 +00:00
ref = inv . reference
else :
ref = self . _convert_ref ( cr , uid , inv . number )
2009-09-24 10:46:21 +00:00
2009-12-24 09:39:00 +00:00
acc_id = None
2008-10-23 05:53:55 +00:00
date = inv . date
total_currency = 0
2009-05-15 11:35:22 +00:00
acc_id = None
2008-10-23 05:53:55 +00:00
for i in iml :
2008-12-15 11:48:52 +00:00
partner_id = i [ ' partner_id ' ]
2009-12-24 09:39:00 +00:00
acc_id = i [ ' account_id ' ]
2008-10-23 05:53:55 +00:00
if inv . currency_id . id != company_currency :
i [ ' currency_id ' ] = inv . currency_id . id
i [ ' amount_currency ' ] = i [ ' amount ' ]
else :
i [ ' amount_currency ' ] = False
i [ ' currency_id ' ] = False
2008-12-15 11:48:52 +00:00
if inv . type in ( ' rec_voucher ' , ' bank_rec_voucher ' , ' journal_pur_voucher ' , ' journal_voucher ' ) :
2008-10-23 05:53:55 +00:00
total + = i [ ' amount ' ]
total_currency + = i [ ' amount_currency ' ] or i [ ' amount ' ]
i [ ' amount ' ] = - i [ ' amount ' ]
else :
total - = i [ ' amount ' ]
total_currency - = i [ ' amount_currency ' ] or i [ ' amount ' ]
name = inv [ ' name ' ] or ' / '
totlines = False
iml . append ( {
' type ' : ' dest ' ,
' name ' : name ,
2008-12-15 11:48:52 +00:00
' amount ' : total or False ,
2008-10-23 05:53:55 +00:00
' account_id ' : acc_id ,
' amount_currency ' : diff_currency_p \
and total_currency or False ,
' currency_id ' : diff_currency_p \
and inv . currency_id . id or False ,
2008-12-15 11:48:52 +00:00
' ref ' : ref ,
' partner_id ' : partner_id or False ,
2008-10-24 11:57:48 +00:00
} )
2008-10-23 05:53:55 +00:00
date = inv . date
inv . amount = total
2008-12-15 11:48:52 +00:00
line = map ( lambda x : ( 0 , 0 , self . line_get_convert ( cr , uid , x , date , context = { } ) ) , iml )
an_journal_id = inv . journal_id . analytic_journal_id . id
journal_id = inv . journal_id . id
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
journal = self . pool . get ( ' account.journal ' ) . browse ( cr , uid , journal_id )
if journal . sequence_id :
name = self . pool . get ( ' ir.sequence ' ) . get_id ( cr , uid , journal . sequence_id . id )
2009-05-14 07:06:09 +00:00
move = {
2010-05-10 11:12:52 +00:00
' name ' : name ,
2009-12-24 09:39:00 +00:00
' journal_id ' : journal_id ,
2010-05-10 11:12:52 +00:00
' type ' : inv . type ,
2010-05-27 19:55:40 +00:00
' narration ' : inv . narration ,
' date ' : inv . date
2009-05-14 07:06:09 +00:00
}
2008-10-23 05:53:55 +00:00
if inv . period_id :
move [ ' period_id ' ] = inv . period_id . id
for i in line :
i [ 2 ] [ ' period_id ' ] = inv . period_id . id
move_id = self . pool . get ( ' account.move ' ) . create ( cr , uid , move )
2009-05-14 07:06:09 +00:00
ref = move [ ' name ' ]
2008-12-15 11:48:52 +00:00
amount = 0.0
2009-12-24 09:39:00 +00:00
2008-12-15 11:48:52 +00:00
#create the first line our self
move_line = {
' name ' : inv . name ,
' debit ' : False ,
' credit ' : False ,
' account_id ' : inv . account_id . id or False ,
' move_id ' : move_id ,
' journal_id ' : journal_id ,
' period_id ' : inv . period_id . id ,
' partner_id ' : False ,
2009-12-24 09:39:00 +00:00
' ref ' : ref ,
2008-12-15 11:48:52 +00:00
' date ' : inv . date
}
if inv . type in ( ' rec_voucher ' , ' bank_rec_voucher ' , ' journal_pur_voucher ' , ' journal_voucher ' ) :
move_line [ ' debit ' ] = inv . amount
else :
move_line [ ' credit ' ] = inv . amount * ( - 1 )
self . pool . get ( ' account.move.line ' ) . create ( cr , uid , move_line )
2009-12-24 09:39:00 +00:00
2008-12-15 11:48:52 +00:00
for line in inv . payment_ids :
2009-12-24 09:39:00 +00:00
2008-12-15 11:48:52 +00:00
move_line = {
' name ' : line . name ,
' debit ' : False ,
' credit ' : False ,
' account_id ' : line . account_id . id or False ,
' move_id ' : move_id ,
' journal_id ' : journal_id ,
' period_id ' : inv . period_id . id ,
' partner_id ' : line . partner_id . id or False ,
2009-12-24 09:39:00 +00:00
' ref ' : ref ,
2008-12-15 11:48:52 +00:00
' date ' : inv . date
}
2009-12-24 09:39:00 +00:00
2008-12-15 11:48:52 +00:00
if line . type == ' dr ' :
move_line [ ' debit ' ] = line . amount or False
amount = line . amount
elif line . type == ' cr ' :
move_line [ ' credit ' ] = line . amount or False
amount = line . amount * ( - 1 )
2010-05-27 19:55:40 +00:00
move_line [ ' analytic_account_id ' ] = line . account_analytic_id . id or False
2008-12-15 11:48:52 +00:00
ml_id = self . pool . get ( ' account.move.line ' ) . create ( cr , uid , move_line )
2009-12-24 09:39:00 +00:00
2008-12-15 11:48:52 +00:00
if inv . narration :
2010-05-27 19:55:40 +00:00
line . name = inv . narration
2008-12-15 11:48:52 +00:00
else :
2010-05-27 19:55:40 +00:00
line . name = line . name
2009-12-24 09:39:00 +00:00
2008-12-15 12:09:18 +00:00
if line . account_analytic_id :
an_line = {
' name ' : line . name ,
' date ' : inv . date ,
' amount ' : amount ,
' account_id ' : line . account_analytic_id . id or False ,
' move_id ' : ml_id ,
' journal_id ' : an_journal_id ,
' general_account_id ' : line . account_id . id ,
' ref ' : ref
}
self . pool . get ( ' account.analytic.line ' ) . create ( cr , uid , an_line )
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
self . write ( cr , uid , [ inv . id ] , { ' move_id ' : move_id } )
2008-12-15 11:48:52 +00:00
obj = self . pool . get ( ' account.move ' ) . browse ( cr , uid , move_id )
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
for line in obj . line_id :
2008-12-15 11:48:52 +00:00
cr . execute ( ' insert into voucher_id (account_id,rel_account_move) values ( %d , %d ) ' , ( int ( ids [ 0 ] ) , int ( line . id ) ) )
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
return True
2009-05-14 07:06:09 +00:00
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def line_get_convert ( self , cr , uid , x , date , context = { } ) :
return {
' date ' : date ,
' date_maturity ' : x . get ( ' date_maturity ' , False ) ,
' partner_id ' : x . get ( ' partner_id ' , False ) ,
' name ' : x [ ' name ' ] [ : 64 ] ,
' debit ' : x [ ' amount ' ] > 0 and x [ ' amount ' ] ,
' credit ' : x [ ' amount ' ] < 0 and - x [ ' amount ' ] ,
' account_id ' : x [ ' account_id ' ] ,
' analytic_lines ' : x . get ( ' analytic_lines ' , [ ] ) ,
' amount_currency ' : x . get ( ' amount_currency ' , False ) ,
' currency_id ' : x . get ( ' currency_id ' , False ) ,
' tax_code_id ' : x . get ( ' tax_code_id ' , False ) ,
' tax_amount ' : x . get ( ' tax_amount ' , False ) ,
' ref ' : x . get ( ' ref ' , False )
}
2010-05-27 19:55:40 +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
2008-10-23 05:53:55 +00:00
def action_number ( self , cr , uid , ids , * args ) :
cr . execute ( ' SELECT id, type, number, move_id, reference ' \
' FROM account_voucher ' \
2010-06-10 13:34:19 +00:00
' WHERE id IN %s ' , ( tuple ( ids ) , ) )
2008-10-23 05:53:55 +00:00
for ( id , invtype , number , move_id , reference ) in cr . fetchall ( ) :
if not number :
2009-05-14 07:06:09 +00:00
number = self . pool . get ( ' ir.sequence ' ) . get ( cr , uid , invtype )
2008-10-23 05:53:55 +00:00
2009-05-14 07:06:09 +00:00
if type in ( ' pay_voucher ' , ' journal_voucher ' , ' rec_voucher ' , ' cont_voucher ' , ' bank_pay_voucher ' , ' bank_rec_voucher ' , ' journal_sale_vou ' , ' journal_pur_voucher ' ) :
2008-10-23 05:53:55 +00:00
ref = reference
else :
ref = self . _convert_ref ( cr , uid , number )
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
cr . execute ( ' UPDATE account_voucher SET number= %s ' \
2009-11-27 10:51:31 +00:00
' WHERE id= %s ' , ( number , id ) )
2008-10-23 05:53:55 +00:00
cr . execute ( ' UPDATE account_move_line SET ref= %s ' \
2009-11-27 10:51:31 +00:00
' WHERE move_id= %s AND (ref is null OR ref = \' \' ) ' ,
2008-10-23 05:53:55 +00:00
( ref , move_id ) )
cr . execute ( ' UPDATE account_analytic_line SET ref= %s ' \
' FROM account_move_line ' \
2009-11-27 10:51:31 +00:00
' WHERE account_move_line.move_id = %s ' \
2008-10-23 05:53:55 +00:00
' AND account_analytic_line.move_id = account_move_line.id ' ,
( ref , move_id ) )
return True
def name_get ( self , cr , uid , ids , context = { } ) :
if not len ( ids ) :
return [ ]
types = {
2010-05-26 19:30:21 +00:00
' 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 '
2008-12-15 11:48:52 +00:00
}
2008-10-23 05:53:55 +00:00
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 ' ) ]
2009-12-09 11:43:34 +00:00
def name_search ( self , cr , user , name , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
2008-10-23 05:53:55 +00:00
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 )
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
if default is None :
default = { }
default = default . copy ( )
2009-05-14 07:06:09 +00:00
default . update ( { ' state ' : ' draft ' , ' number ' : False , ' move_id ' : False , ' move_ids ' : False , ' payment_ids ' : False } )
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
def action_cancel ( self , cr , uid , ids , * args ) :
account_move_obj = self . pool . get ( ' account.move ' )
voucher = self . read ( cr , uid , ids , [ ' move_id ' ] )
for i in voucher :
if i [ ' move_id ' ] :
account_move_obj . button_cancel ( cr , uid , [ i [ ' move_id ' ] [ 0 ] ] )
# delete the move this invoice was pointing to
# Note that the corresponding move_lines and move_reconciles
# will be automatically deleted too
account_move_obj . unlink ( cr , uid , [ i [ ' move_id ' ] [ 0 ] ] )
self . write ( cr , uid , ids , { ' state ' : ' cancel ' , ' move_id ' : False } )
return True
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 ' ) ,
' 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 ' ) ] ) ,
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 ) ,
2008-10-23 05:53:55 +00:00
' account_analytic_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' )
}
_defaults = {
' type ' : lambda * a : ' cr '
}
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def move_line_get ( self , cr , uid , voucher_id , context = { } ) :
res = [ ]
cur_obj = self . pool . get ( ' res.currency ' )
inv = self . pool . get ( ' account.voucher ' ) . browse ( cr , uid , voucher_id )
company_currency = inv . company_id . currency_id . id
cur = inv . currency_id
for line in inv . payment_ids :
res . append ( self . move_line_get_item ( cr , uid , line , context ) )
return res
2009-12-24 09:39:00 +00:00
2009-10-26 13:50:06 +00:00
def onchange_partner ( self , cr , uid , ids , partner_id , ttype , type1 ) :
2008-10-23 05:53:55 +00:00
if not partner_id :
return { ' value ' : { ' account_id ' : False , ' type ' : False , ' amount ' : False } }
obj = self . pool . get ( ' res.partner ' )
account_id = False
2009-05-14 07:06:09 +00:00
if type1 in ( ' rec_voucher ' , ' bank_rec_voucher ' , ' journal_voucher ' ) :
2008-10-23 05:53:55 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
2009-05-14 07:06:09 +00:00
balance = obj . browse ( cr , uid , partner_id ) . credit
ttype = ' cr '
2009-12-24 09:39:00 +00:00
elif type1 in ( ' pay_voucher ' , ' bank_pay_voucher ' , ' cont_voucher ' ) :
2008-10-23 05:53:55 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
2009-05-14 07:06:09 +00:00
balance = obj . browse ( cr , uid , partner_id ) . debit
ttype = ' dr '
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_sale_vou ' ) :
2008-10-23 05:53:55 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
2009-05-14 07:06:09 +00:00
balance = obj . browse ( cr , uid , partner_id ) . credit
ttype = ' dr '
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_pur_voucher ' ) :
2008-10-23 05:53:55 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
2009-05-14 07:06:09 +00:00
balance = obj . browse ( cr , uid , partner_id ) . debit
2009-12-24 09:39:00 +00:00
ttype = ' cr '
2008-10-23 05:53:55 +00:00
return {
2009-05-14 07:06:09 +00:00
' value ' : { ' account_id ' : account_id . id , ' type ' : ttype , ' amount ' : balance }
2008-10-23 05:53:55 +00:00
}
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def onchange_amount ( self , cr , uid , ids , partner_id , amount , type , type1 ) :
if not amount :
2009-05-14 07:06:09 +00:00
return { ' value ' : { } }
if partner_id :
obj = self . pool . get ( ' res.partner ' )
if type1 in ( ' rec_voucher ' , ' bank_rec_voucher ' , ' journal_voucher ' ) :
if amount < 0 :
2009-12-24 09:39:00 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
2009-05-14 07:06:09 +00:00
type = ' dr '
else :
2009-12-24 09:39:00 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
2009-05-14 07:06:09 +00:00
type = ' cr '
2009-12-24 09:39:00 +00:00
elif type1 in ( ' pay_voucher ' , ' bank_pay_voucher ' , ' cont_voucher ' ) :
2009-05-14 07:06:09 +00:00
if amount < 0 :
2009-12-24 09:39:00 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
2009-05-14 07:06:09 +00:00
type = ' cr '
else :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
type = ' dr '
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_sale_vou ' ) :
2009-05-14 07:06:09 +00:00
if amount < 0 :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
type = ' cr '
else :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
type = ' dr '
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_pur_voucher ' ) :
2009-05-14 07:06:09 +00:00
if amount < 0 :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
type = ' dr '
else :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
type = ' cr '
else :
if type1 in ( ' rec_voucher ' , ' bank_rec_voucher ' , ' journal_voucher ' ) :
if amount < 0 :
type = ' dr '
else :
type = ' cr '
2009-12-24 09:39:00 +00:00
elif type1 in ( ' pay_voucher ' , ' bank_pay_voucher ' , ' cont_voucher ' ) :
2009-05-14 07:06:09 +00:00
if amount < 0 :
type = ' cr '
else :
type = ' dr '
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_sale_vou ' ) :
2009-05-14 07:06:09 +00:00
if amount < 0 :
type = ' cr '
else :
type = ' dr '
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_pur_voucher ' ) :
2009-05-14 07:06:09 +00:00
if amount < 0 :
type = ' dr '
else :
type = ' cr '
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
return {
' value ' : { ' type ' : type , ' amount ' : amount }
}
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def onchange_type ( self , cr , uid , ids , partner_id , amount , type , type1 ) :
2009-05-14 07:06:09 +00:00
if partner_id :
obj = self . pool . get ( ' res.partner ' )
if type1 in ( ' rec_voucher ' , ' bank_rec_voucher ' , ' journal_voucher ' ) :
if type == ' dr ' :
2009-12-24 09:39:00 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
2009-05-14 07:06:09 +00:00
total = amount * ( - 1 )
else :
2009-12-24 09:39:00 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
2009-05-14 07:06:09 +00:00
total = amount * ( - 1 )
2009-12-24 09:39:00 +00:00
elif type1 in ( ' pay_voucher ' , ' bank_pay_voucher ' , ' cont_voucher ' ) :
2009-05-14 07:06:09 +00:00
if type == ' cr ' :
2009-12-24 09:39:00 +00:00
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
2009-05-14 07:06:09 +00:00
total = amount * ( - 1 )
else :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
total = amount * ( - 1 )
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_sale_vou ' ) :
2009-05-14 07:06:09 +00:00
if type == ' cr ' :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
total = amount * ( - 1 )
else :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
total = amount * ( - 1 )
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_pur_voucher ' ) :
2009-05-14 07:06:09 +00:00
if type == ' dr ' :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_receivable
total = amount * ( - 1 )
else :
account_id = obj . browse ( cr , uid , partner_id ) . property_account_payable
total = amount * ( - 1 )
else :
if type1 in ( ' rec_voucher ' , ' bank_rec_voucher ' , ' journal_voucher ' ) :
if type == ' dr ' :
total = amount * ( - 1 )
else :
total = amount * ( - 1 )
2009-12-24 09:39:00 +00:00
elif type1 in ( ' pay_voucher ' , ' bank_pay_voucher ' , ' cont_voucher ' ) :
2009-05-14 07:06:09 +00:00
if type == ' cr ' :
total = amount * ( - 1 )
else :
total = amount * ( - 1 )
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_sale_vou ' ) :
2009-05-14 07:06:09 +00:00
if type == ' cr ' :
total = amount * ( - 1 )
else :
total = amount * ( - 1 )
2009-12-24 09:39:00 +00:00
elif type1 in ( ' journal_pur_voucher ' ) :
2009-05-14 07:06:09 +00:00
if type == ' dr ' :
total = amount * ( - 1 )
else :
total = amount * ( - 1 )
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
return {
' value ' : { ' type ' : type , ' amount ' : total }
}
2009-12-24 09:39:00 +00:00
2008-10-23 05:53:55 +00:00
def move_line_get_item ( self , cr , uid , line , context = { } ) :
return {
2008-12-15 11:48:52 +00:00
' type ' : ' src ' ,
' name ' : line . name [ : 64 ] ,
' amount ' : line . amount ,
' account_id ' : line . account_id . id ,
' partner_id ' : line . partner_id . id or False ,
' account_analytic_id ' : line . account_analytic_id . id or False ,
2009-05-14 07:06:09 +00:00
' ref ' : line . ref
2008-12-15 11:48:52 +00:00
}
2009-12-24 09:39:00 +00:00
2010-02-16 07:06:49 +00:00
account_voucher_line ( )