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-09-23 07:13:12 +00:00
from lxml import etree
2010-08-16 20:14:32 +00:00
import netsvc
2010-12-13 20:25:12 +00:00
from osv import osv , fields
import decimal_precision as dp
2010-07-22 08:43:54 +00:00
from tools . translate import _
2010-07-07 13:05:41 +00:00
2010-08-18 20:13:20 +00:00
2008-10-23 05:53:55 +00:00
class account_voucher ( osv . osv ) :
2010-10-11 07:34:19 +00:00
2010-12-29 15:07:46 +00:00
def _get_type ( self , cr , uid , context = None ) :
if context is None :
context = { }
2010-09-18 12:43:30 +00:00
return context . get ( ' type ' , False )
2010-09-29 05:29:52 +00:00
2010-11-19 13:48:01 +00:00
def _get_period ( self , cr , uid , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-05-27 19:55:40 +00:00
if context . get ( ' period_id ' , False ) :
return context . get ( ' period_id ' )
2011-08-04 09:55:50 +00:00
if context . get ( ' invoice_id ' , False ) :
company_id = self . pool . get ( ' account.invoice ' ) . browse ( cr , uid , context [ ' invoice_id ' ] , context = context ) . company_id . id
context . update ( { ' company_id ' : company_id } )
periods = self . pool . get ( ' account.period ' ) . find ( cr , uid , context = context )
2010-08-26 00:14:59 +00:00
return periods and periods [ 0 ] or False
2009-12-24 09:39:00 +00:00
2010-11-19 13:48:01 +00:00
def _get_journal ( self , cr , uid , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-05-26 19:30:21 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
2011-01-13 08:40:40 +00:00
invoice_pool = self . pool . get ( ' account.invoice ' )
if context . get ( ' invoice_id ' , False ) :
2011-01-13 10:33:56 +00:00
currency_id = invoice_pool . browse ( cr , uid , context [ ' invoice_id ' ] , context = context ) . currency_id . id
2011-01-13 08:40:40 +00:00
journal_id = journal_pool . search ( cr , uid , [ ( ' currency ' , ' = ' , currency_id ) ] , limit = 1 )
return journal_id and journal_id [ 0 ] or False
2010-05-26 19:30:21 +00:00
if context . get ( ' journal_id ' , False ) :
return context . get ( ' journal_id ' )
2010-09-11 05:51:47 +00:00
if not context . get ( ' journal_id ' , False ) and context . get ( ' search_default_journal_id ' , False ) :
return context . get ( ' search_default_journal_id ' )
2010-09-29 05:29:52 +00:00
2010-08-13 21:02:45 +00:00
ttype = context . get ( ' type ' , ' bank ' )
2010-09-23 07:35:58 +00:00
if ttype in ( ' payment ' , ' receipt ' ) :
ttype = ' bank '
2010-05-26 19:30:21 +00:00
res = journal_pool . search ( cr , uid , [ ( ' type ' , ' = ' , ttype ) ] , limit = 1 )
2010-08-26 00:14:59 +00:00
return res and res [ 0 ] or False
2009-12-24 09:39:00 +00:00
2010-11-19 13:48:01 +00:00
def _get_tax ( self , cr , uid , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-09-15 14:33:40 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
2010-08-25 16:42:19 +00:00
journal_id = context . get ( ' journal_id ' , False )
2010-08-26 00:14:59 +00:00
if not journal_id :
2010-09-15 14:33:40 +00:00
ttype = context . get ( ' type ' , ' bank ' )
res = journal_pool . search ( cr , uid , [ ( ' type ' , ' = ' , ttype ) ] , limit = 1 )
if not res :
return False
journal_id = res [ 0 ]
2010-09-29 05:29:52 +00:00
2010-09-15 15:22:43 +00:00
if not journal_id :
return False
2010-11-19 13:48:01 +00:00
journal = journal_pool . browse ( cr , uid , journal_id , context = context )
2010-08-26 00:14:59 +00:00
account_id = journal . default_credit_account_id or journal . default_debit_account_id
2010-08-25 16:42:19 +00:00
if account_id and account_id . tax_ids :
tax_id = account_id . tax_ids [ 0 ] . id
2010-08-26 00:14:59 +00:00
return tax_id
return False
2010-08-17 13:24:18 +00:00
2010-11-19 13:48:01 +00:00
def _get_currency ( self , cr , uid , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-09-15 14:33:40 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
journal_id = context . get ( ' journal_id ' , False )
2010-09-16 14:42:32 +00:00
if journal_id :
2010-11-19 13:48:01 +00:00
journal = journal_pool . browse ( cr , uid , journal_id , context = context )
2010-10-11 07:34:19 +00:00
# currency_id = journal.company_id.currency_id.id
2010-09-16 14:42:32 +00:00
if journal . currency :
2010-10-08 09:56:14 +00:00
return journal . currency . id
2010-09-16 14:42:32 +00:00
return False
2010-09-23 07:13:12 +00:00
2010-11-19 13:48:01 +00:00
def _get_partner ( self , cr , uid , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-09-01 08:06:53 +00:00
return context . get ( ' partner_id ' , False )
2010-09-23 07:13:12 +00:00
2010-11-19 13:48:01 +00:00
def _get_reference ( self , cr , uid , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-09-15 14:33:40 +00:00
return context . get ( ' reference ' , False )
2010-09-23 07:13:12 +00:00
2010-11-19 13:48:01 +00:00
def _get_narration ( self , cr , uid , context = None ) :
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-09-15 14:33:40 +00:00
return context . get ( ' narration ' , False )
2010-09-23 07:13:12 +00:00
2010-12-06 11:50:27 +00:00
def _get_amount ( self , cr , uid , context = None ) :
if context is None :
context = { }
return context . get ( ' amount ' , 0.0 )
2010-09-23 07:26:57 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2010-10-13 13:46:45 +00:00
if not ids :
2010-09-23 07:26:57 +00:00
return [ ]
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-09-29 07:58:36 +00:00
return [ ( r [ ' id ' ] , ( str ( " %.2f " % r [ ' amount ' ] ) or ' ' ) ) for r in self . read ( cr , uid , ids , [ ' amount ' ] , context , load = ' _classic_write ' ) ]
2010-09-23 07:26:57 +00:00
2010-09-23 07:13:12 +00:00
def fields_view_get ( self , cr , uid , view_id = None , view_type = False , context = None , toolbar = False , submenu = False ) :
2010-11-01 10:55:16 +00:00
mod_obj = self . pool . get ( ' ir.model.data ' )
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-11-01 10:55:16 +00:00
if not view_id and context . get ( ' invoice_type ' , False ) :
if context . get ( ' invoice_type ' , False ) in ( ' out_invoice ' , ' out_refund ' ) :
2010-10-28 08:35:00 +00:00
result = mod_obj . get_object_reference ( cr , uid , ' account_voucher ' , ' view_vendor_receipt_form ' )
2010-10-06 08:00:25 +00:00
else :
2010-10-28 08:35:00 +00:00
result = mod_obj . get_object_reference ( cr , uid , ' account_voucher ' , ' view_vendor_payment_form ' )
result = result and result [ 1 ] or False
2010-10-06 08:00:25 +00:00
view_id = result
2011-03-01 11:04:40 +00:00
if not view_id and view_type == ' form ' and context . get ( ' line_type ' , False ) :
2010-11-01 10:55:16 +00:00
if context . get ( ' line_type ' , False ) == ' customer ' :
result = mod_obj . get_object_reference ( cr , uid , ' account_voucher ' , ' view_vendor_receipt_form ' )
else :
result = mod_obj . get_object_reference ( cr , uid , ' account_voucher ' , ' view_vendor_payment_form ' )
result = result and result [ 1 ] or False
view_id = result
2010-10-11 12:49:25 +00:00
res = super ( account_voucher , self ) . fields_view_get ( cr , uid , view_id = view_id , view_type = view_type , context = context , toolbar = toolbar , submenu = submenu )
2010-09-23 07:13:12 +00:00
doc = etree . XML ( res [ ' arch ' ] )
nodes = doc . xpath ( " //field[@name= ' partner_id ' ] " )
if context . get ( ' type ' , ' sale ' ) in ( ' purchase ' , ' payment ' ) :
for node in nodes :
node . set ( ' domain ' , " [( ' supplier ' , ' = ' , True)] " )
res [ ' arch ' ] = etree . tostring ( doc )
return res
2011-07-15 08:26:43 +00:00
def _compute_writeoff_amount ( self , cr , uid , line_dr_ids , line_cr_ids , amount , voucher_date , context = None ) :
2011-07-14 13:44:21 +00:00
if context is None :
context = { }
2011-07-14 15:48:22 +00:00
currency_pool = self . pool . get ( ' res.currency ' )
2011-07-14 13:44:21 +00:00
ctx = context . copy ( )
2011-07-14 15:48:22 +00:00
counter_for_writeoff = counter_for_currency_diff = real_amount = 0.0
2011-07-15 08:26:43 +00:00
ctx . update ( { ' date ' : voucher_date } )
2011-01-28 13:32:54 +00:00
for l in line_dr_ids :
2011-07-14 15:48:22 +00:00
real_amount - = l . get ( ' amount_in_company_currency ' , 0.0 )
2011-07-15 08:26:43 +00:00
counter_for_writeoff - = currency_pool . compute ( cr , uid , l [ ' company_currency_id ' ] , l [ ' voucher_currency_id ' ] , l . get ( ' amount_in_company_currency ' , 0.0 ) , context = ctx )
2011-08-04 10:04:57 +00:00
counter_for_currency_diff - = currency_pool . compute ( cr , uid , l [ ' currency_id ' ] , l [ ' company_currency_id ' ] , l [ ' amount ' ] , context = ctx )
2011-01-28 13:32:54 +00:00
for l in line_cr_ids :
2011-07-14 15:48:22 +00:00
real_amount + = l . get ( ' amount_in_company_currency ' , 0.0 )
2011-07-15 08:26:43 +00:00
counter_for_writeoff + = currency_pool . compute ( cr , uid , l [ ' company_currency_id ' ] , l [ ' voucher_currency_id ' ] , l . get ( ' amount_in_company_currency ' , 0.0 ) , context = ctx )
2011-08-04 10:04:57 +00:00
counter_for_currency_diff + = currency_pool . compute ( cr , uid , l [ ' currency_id ' ] , l [ ' company_currency_id ' ] , l [ ' amount ' ] , context = ctx )
2011-07-14 15:48:22 +00:00
writeoff_amount = amount - counter_for_writeoff
currency_rate_difference = real_amount - counter_for_currency_diff
return writeoff_amount , currency_rate_difference
2011-07-15 08:26:43 +00:00
def onchange_line_ids ( self , cr , uid , ids , line_dr_ids , line_cr_ids , amount , voucher_date , context = None ) :
2011-01-05 13:55:31 +00:00
if not line_dr_ids and not line_cr_ids :
return { ' value ' : { } }
2011-06-30 10:19:04 +00:00
line_osv = self . pool . get ( " account.voucher.line " )
line_dr_ids = resolve_o2m_operations ( cr , uid , line_osv , line_dr_ids , [ ' amount ' ] , context )
line_cr_ids = resolve_o2m_operations ( cr , uid , line_osv , line_cr_ids , [ ' amount ' ] , context )
2011-08-04 10:04:57 +00:00
writeoff_amount , currency_rate_diff = self . _compute_writeoff_amount ( cr , uid , line_dr_ids , line_cr_ids , amount , voucher_date , context = context )
2011-07-14 13:44:21 +00:00
return { ' value ' : { ' writeoff_amount ' : writeoff_amount , } } # 'currency_rate_difference': currency_rate_diff}}
2010-12-13 20:25:12 +00:00
2010-11-24 10:18:29 +00:00
def _get_writeoff_amount ( self , cr , uid , ids , name , args , context = None ) :
if not ids : return { }
2011-07-14 13:44:21 +00:00
if context is None :
context = { }
res = { } . fromkeys ( ids , { } )
2011-07-14 15:48:22 +00:00
counter_for_writeoff = counter_for_currency_diff = real_amount = 0.0
2011-07-14 13:44:21 +00:00
currency_pool = self . pool . get ( ' res.currency ' )
2011-01-28 13:32:54 +00:00
for voucher in self . browse ( cr , uid , ids , context = context ) :
2011-07-14 13:44:21 +00:00
ctx = context . copy ( )
2011-07-15 08:26:43 +00:00
ctx . update ( { ' date ' : voucher . date } )
2011-01-28 13:32:54 +00:00
for l in voucher . line_dr_ids :
2011-07-14 13:44:21 +00:00
real_amount - = l . amount_in_company_currency
2011-07-15 12:47:38 +00:00
counter_for_writeoff - = ( l . voucher_currency_id . id == l . company_currency_id . id ) and l . amount_in_company_currency or l . amount_in_voucher_currency
2011-07-15 08:26:43 +00:00
#ctx.update({'date': l.date_original})
#counter_for_writeoff -= currency_pool.compute(cr, uid, voucher.company_id.currency_id.id, voucher.currency_id.id, l.amount_in_company_currency, context=ctx)
2011-08-04 10:04:57 +00:00
counter_for_currency_diff - = currency_pool . compute ( cr , uid , l . currency_id . id , voucher . company_id . currency_id . id , l . amount , context = ctx )
2011-01-28 13:32:54 +00:00
for l in voucher . line_cr_ids :
2011-07-14 13:44:21 +00:00
real_amount + = l . amount_in_company_currency
2011-07-15 12:47:38 +00:00
counter_for_writeoff + = ( l . voucher_currency_id . id == l . company_currency_id . id ) and l . amount_in_company_currency or l . amount_in_voucher_currency
2011-07-15 08:26:43 +00:00
#ctx.update({'date': l.date_original})
#counter_for_writeoff += currency_pool.compute(cr, uid, voucher.company_id.currency_id.id, voucher.currency_id.id, l.amount_in_company_currency, context=ctx)
2011-08-04 10:04:57 +00:00
counter_for_currency_diff + = currency_pool . compute ( cr , uid , l . currency_id . id , voucher . company_id . currency_id . id , l . amount , context = ctx )
2011-07-14 13:44:21 +00:00
writeoff_amount = voucher . amount - counter_for_writeoff
2011-08-04 10:04:57 +00:00
res [ voucher . id ] [ ' writeoff_amount ' ] = writeoff_amount
2011-07-14 13:44:21 +00:00
res [ voucher . id ] [ ' currency_rate_difference ' ] = real_amount - counter_for_currency_diff
2010-11-24 10:18:29 +00:00
return res
2011-07-15 08:26:43 +00:00
def _currency_id ( self , cr , uid , ids , name , args , context = None ) :
res = { }
for voucher in self . browse ( cr , uid , ids , context = context ) :
currency = voucher . journal_id . currency and voucher . journal_id . currency . id or voucher . company_id . currency_id . id
res [ voucher . id ] = { ' currency_id ' : currency , ' currency_id2 ' : currency }
return res
2008-10-23 05:53:55 +00:00
_name = ' account.voucher '
_description = ' Accounting Voucher '
2010-08-26 00:14:59 +00:00
_order = " date desc, id desc "
2008-10-23 05:53:55 +00:00
_columns = {
2010-08-26 15:04:35 +00:00
' type ' : fields . selection ( [
( ' sale ' , ' Sale ' ) ,
( ' purchase ' , ' Purchase ' ) ,
( ' payment ' , ' Payment ' ) ,
( ' receipt ' , ' Receipt ' ) ,
2010-08-31 14:42:33 +00:00
] , ' Default Type ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-20 18:57:46 +00:00
' name ' : fields . char ( ' Memo ' , size = 256 , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-01-17 11:20:56 +00:00
' date ' : fields . date ( ' Date ' , readonly = True , select = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , help = " Effective date for accounting entries " ) ,
2011-07-15 12:47:38 +00:00
' journal_id ' : fields . many2one ( ' account.journal ' , ' Journal ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , change_default = 1 ) ,
2010-08-19 14:01:41 +00:00
' account_id ' : fields . many2one ( ' account.account ' , ' Account ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-26 00:14:59 +00:00
' line_ids ' : fields . one2many ( ' account.voucher.line ' , ' voucher_id ' , ' Voucher Lines ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' line_cr_ids ' : fields . one2many ( ' account.voucher.line ' , ' voucher_id ' , ' Credits ' ,
2010-08-29 15:01:56 +00:00
domain = [ ( ' type ' , ' = ' , ' cr ' ) ] , context = { ' default_type ' : ' cr ' } , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-26 00:14:59 +00:00
' line_dr_ids ' : fields . one2many ( ' account.voucher.line ' , ' voucher_id ' , ' Debits ' ,
2010-08-29 15:01:56 +00:00
domain = [ ( ' type ' , ' = ' , ' dr ' ) ] , context = { ' default_type ' : ' dr ' } , 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-08-30 13:23:02 +00:00
' narration ' : fields . text ( ' Notes ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-07-15 08:26:43 +00:00
' currency_id ' : fields . function ( _currency_id , type = ' many2one ' , relation = ' res.currency ' , string = ' Currency ' , store = True , readonly = True , multi = " currency " ) ,
#duplicated field for display purposes
' currency_id2 ' : fields . function ( _currency_id , type = ' many2one ' , relation = ' res.currency ' , string = ' Currency ' , store = True , readonly = True , multi = " currency " ) ,
2011-08-04 09:55:50 +00:00
' company_id ' : fields . related ( ' journal_id ' , ' company_id ' , type = ' many2one ' , relation = ' res.company ' , string = ' Company ' , store = True , readonly = True ) ,
2011-07-15 12:47:38 +00:00
' company_currency ' : fields . related ( ' company_id ' , ' currency_id ' , type = ' many2one ' , relation = ' res.currency ' , string = ' Currency ' , readonly = True ) ,
2008-10-23 05:53:55 +00:00
' state ' : fields . selection (
2010-05-26 18:00:17 +00:00
[ ( ' draft ' , ' Draft ' ) ,
( ' proforma ' , ' Pro-forma ' ) ,
( ' posted ' , ' Posted ' ) ,
2010-08-26 00:14:59 +00:00
( ' cancel ' , ' Cancelled ' )
2010-05-26 18:00:17 +00:00
] , ' 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-12-13 20:25:12 +00:00
' amount ' : fields . float ( ' Total ' , digits_compute = dp . get_precision ( ' Account ' ) , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' tax_amount ' : fields . float ( ' Tax Amount ' , digits_compute = dp . get_precision ( ' Account ' ) , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-09-14 22:47:00 +00:00
' reference ' : fields . char ( ' Ref # ' , size = 64 , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , help = " Transaction reference number. " ) ,
2010-09-23 17:39:28 +00:00
' number ' : fields . char ( ' Number ' , size = 32 , readonly = True , ) ,
2008-10-23 05:53:55 +00:00
' move_id ' : fields . many2one ( ' account.move ' , ' Account Entry ' ) ,
2010-09-21 10:13:33 +00:00
' move_ids ' : fields . related ( ' move_id ' , ' line_id ' , type = ' one2many ' , relation = ' account.move.line ' , string = ' Journal Items ' , readonly = True ) ,
2010-08-30 13:23:02 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' , change_default = 1 , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-03-19 09:35:42 +00:00
' audit ' : fields . related ( ' move_id ' , ' to_check ' , type = ' boolean ' , help = ' Check this box if you are unsure of that journal entry and if you want to note it as \' to be reviewed \' by an accounting expert. ' , relation = ' account.move ' , string = ' To Review ' ) ,
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 ) ] } ) ,
2010-08-25 16:42:19 +00:00
' pre_line ' : fields . boolean ( ' Previous Payments ? ' , required = False ) ,
2011-01-17 11:20:56 +00:00
' date_due ' : fields . date ( ' Due Date ' , readonly = True , select = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-11-24 06:56:40 +00:00
' payment_option ' : fields . selection ( [
2010-12-13 20:25:12 +00:00
( ' without_writeoff ' , ' Keep Open ' ) ,
2011-07-15 12:47:38 +00:00
( ' with_writeoff ' , ' Reconcile ' ) ,
2010-12-13 20:25:12 +00:00
] , ' Payment Difference ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-11-24 06:56:40 +00:00
' writeoff_acc_id ' : fields . many2one ( ' account.account ' , ' Write-Off account ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-12-13 20:25:12 +00:00
' comment ' : fields . char ( ' Write-Off Comment ' , size = 64 , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' analytic_id ' : fields . many2one ( ' account.analytic.account ' , ' Write-Off Analytic Account ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-07-14 13:44:21 +00:00
' writeoff_amount ' : fields . function ( _get_writeoff_amount , string = ' Write-Off Amount ' , type = ' float ' , readonly = True , multi = " writeoff " ) ,
' currency_rate_difference ' : fields . function ( _get_writeoff_amount , string = " Currency Rate Difference " , type = ' float ' , multi = " writeoff " ) ,
2008-10-23 05:53:55 +00:00
}
_defaults = {
' period_id ' : _get_period ,
2010-09-01 08:06:53 +00:00
' partner_id ' : _get_partner ,
2008-10-23 05:53:55 +00:00
' journal_id ' : _get_journal ,
' currency_id ' : _get_currency ,
2010-09-15 14:33:40 +00:00
' reference ' : _get_reference ,
' narration ' : _get_narration ,
2010-12-06 11:50:27 +00:00
' amount ' : _get_amount ,
2010-08-26 15:04:35 +00:00
' type ' : _get_type ,
2010-09-23 17:39:28 +00:00
' state ' : ' draft ' ,
' pay_now ' : ' pay_later ' ,
' name ' : ' ' ,
2010-11-04 12:42:42 +00:00
' date ' : lambda * a : time . strftime ( ' % Y- % m- %d ' ) ,
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 ) ,
2010-08-25 16:42:19 +00:00
' tax_id ' : _get_tax ,
2010-11-24 10:18:29 +00:00
' payment_option ' : ' without_writeoff ' ,
2010-12-13 20:25:12 +00:00
' comment ' : _ ( ' Write-Off ' ) ,
2008-10-23 05:53:55 +00:00
}
2010-09-29 05:29:52 +00:00
2010-11-19 13:48:01 +00:00
def compute_tax ( self , cr , uid , ids , context = None ) :
2010-08-30 13:23:02 +00:00
tax_pool = self . pool . get ( ' account.tax ' )
partner_pool = self . pool . get ( ' res.partner ' )
position_pool = self . pool . get ( ' account.fiscal.position ' )
voucher_line_pool = self . pool . get ( ' account.voucher.line ' )
voucher_pool = self . pool . get ( ' account.voucher ' )
2010-11-23 07:05:05 +00:00
if context is None : context = { }
2010-09-29 05:29:52 +00:00
2010-11-19 13:48:01 +00:00
for voucher in voucher_pool . browse ( cr , uid , ids , context = context ) :
2010-08-30 13:23:02 +00:00
voucher_amount = 0.0
for line in voucher . line_ids :
voucher_amount + = line . untax_amount or line . amount
2010-08-31 14:42:33 +00:00
line . amount = line . untax_amount or line . amount
voucher_line_pool . write ( cr , uid , [ line . id ] , { ' amount ' : line . amount , ' untax_amount ' : line . untax_amount } )
2010-09-29 05:29:52 +00:00
2010-08-30 13:23:02 +00:00
if not voucher . tax_id :
2010-09-14 08:09:17 +00:00
self . write ( cr , uid , [ voucher . id ] , { ' amount ' : voucher_amount , ' tax_amount ' : 0.0 } )
2010-08-30 13:23:02 +00:00
continue
2010-09-29 05:29:52 +00:00
2010-11-19 13:48:01 +00:00
tax = [ tax_pool . browse ( cr , uid , voucher . tax_id . id , context = context ) ]
partner = partner_pool . browse ( cr , uid , voucher . partner_id . id , context = context ) or False
2010-08-30 13:23:02 +00:00
taxes = position_pool . map_tax ( cr , uid , partner and partner . property_account_position or False , tax )
2010-11-19 13:48:01 +00:00
tax = tax_pool . browse ( cr , uid , taxes , context = context )
2010-09-29 05:29:52 +00:00
2010-08-30 13:23:02 +00:00
total = voucher_amount
total_tax = 0.0
2010-09-29 05:29:52 +00:00
2010-08-30 13:23:02 +00:00
if not tax [ 0 ] . price_include :
2010-10-11 12:49:25 +00:00
for tax_line in tax_pool . compute_all ( cr , uid , tax , voucher_amount , 1 ) . get ( ' taxes ' , [ ] ) :
2010-10-08 06:13:09 +00:00
total_tax + = tax_line . get ( ' amount ' , 0.0 )
2010-08-30 13:23:02 +00:00
total + = total_tax
else :
for line in voucher . line_ids :
line_total = 0.0
line_tax = 0.0
2010-09-29 05:29:52 +00:00
2010-10-11 12:49:25 +00:00
for tax_line in tax_pool . compute_all ( cr , uid , tax , line . untax_amount or line . amount , 1 ) . get ( ' taxes ' , [ ] ) :
2010-10-08 06:13:09 +00:00
line_tax + = tax_line . get ( ' amount ' , 0.0 )
2010-08-30 13:23:02 +00:00
line_total + = tax_line . get ( ' price_unit ' )
total_tax + = line_tax
untax_amount = line . untax_amount or line . amount
voucher_line_pool . write ( cr , uid , [ line . id ] , { ' amount ' : line_total , ' untax_amount ' : untax_amount } )
2010-09-29 05:29:52 +00:00
2010-08-30 13:23:02 +00:00
self . write ( cr , uid , [ voucher . id ] , { ' amount ' : total , ' tax_amount ' : total_tax } )
return True
2010-09-29 05:29:52 +00:00
2010-11-19 13:48:01 +00:00
def onchange_price ( self , cr , uid , ids , line_ids , tax_id , partner_id = False , context = None ) :
2011-06-30 09:20:57 +00:00
context = context or { }
2010-08-23 11:09:08 +00:00
tax_pool = self . pool . get ( ' account.tax ' )
partner_pool = self . pool . get ( ' res.partner ' )
position_pool = self . pool . get ( ' account.fiscal.position ' )
2011-06-30 09:20:57 +00:00
line_pool = self . pool . get ( ' account.voucher.line ' )
2010-08-19 14:01:41 +00:00
res = {
2010-10-11 12:49:25 +00:00
' tax_amount ' : False ,
' amount ' : False ,
2010-08-19 14:01:41 +00:00
}
2010-08-25 16:42:19 +00:00
voucher_total = 0.0
2011-06-30 09:20:57 +00:00
line_ids = resolve_o2m_operations ( cr , uid , line_pool , line_ids , [ " amount " ] , context )
2010-09-29 05:29:52 +00:00
2010-08-20 11:40:55 +00:00
total = 0.0
2010-08-25 16:42:19 +00:00
total_tax = 0.0
2010-08-26 00:14:59 +00:00
for line in line_ids :
2010-08-30 13:23:02 +00:00
line_amount = 0.0
2011-06-30 09:20:57 +00:00
line_amount = line . get ( ' amount ' , 0.0 )
2010-08-30 13:23:02 +00:00
voucher_total + = line_amount
2010-09-29 05:29:52 +00:00
2010-08-25 16:42:19 +00:00
total = voucher_total
2010-09-14 08:09:17 +00:00
total_tax = 0.0
if tax_id :
2010-11-19 13:48:01 +00:00
tax = [ tax_pool . browse ( cr , uid , tax_id , context = context ) ]
2010-09-14 08:09:17 +00:00
if partner_id :
2010-11-19 13:48:01 +00:00
partner = partner_pool . browse ( cr , uid , partner_id , context = context ) or False
2010-09-14 08:09:17 +00:00
taxes = position_pool . map_tax ( cr , uid , partner and partner . property_account_position or False , tax )
2010-11-19 13:48:01 +00:00
tax = tax_pool . browse ( cr , uid , taxes , context = context )
2010-09-29 05:29:52 +00:00
2010-09-14 08:09:17 +00:00
if not tax [ 0 ] . price_include :
2010-10-11 12:49:25 +00:00
for tax_line in tax_pool . compute_all ( cr , uid , tax , voucher_total , 1 ) . get ( ' taxes ' , [ ] ) :
2010-09-14 08:09:17 +00:00
total_tax + = tax_line . get ( ' amount ' )
total + = total_tax
2010-09-29 05:29:52 +00:00
2010-08-20 11:40:55 +00:00
res . update ( {
2010-09-14 11:19:20 +00:00
' amount ' : total or voucher_total ,
2010-08-25 16:42:19 +00:00
' tax_amount ' : total_tax
2010-08-20 11:40:55 +00:00
} )
2010-08-19 14:01:41 +00:00
return {
' value ' : res
}
2010-09-29 05:29:52 +00:00
2010-08-26 15:04:35 +00:00
def onchange_term_id ( self , cr , uid , ids , term_id , amount ) :
term_pool = self . pool . get ( ' account.payment.term ' )
terms = False
due_date = False
default = { ' date_due ' : False }
if term_id and amount :
terms = term_pool . compute ( cr , uid , term_id , amount )
if terms :
due_date = terms [ - 1 ] [ 0 ]
default . update ( {
' date_due ' : due_date
} )
return { ' value ' : default }
2010-09-29 05:29:52 +00:00
2010-11-19 13:48:01 +00:00
def onchange_journal_voucher ( self , cr , uid , ids , line_ids = False , tax_id = False , price = 0.0 , partner_id = False , journal_id = False , ttype = False , context = None ) :
2010-09-14 13:13:29 +00:00
""" price
Returns a dict that contains new values and context
2010-09-29 05:29:52 +00:00
2010-09-14 13:13:29 +00:00
@param partner_id : latest value from user input for field partner_id
@param args : other arguments
@param context : context arguments , like lang , time zone
2010-09-29 05:29:52 +00:00
2010-09-14 13:13:29 +00:00
@return : Returns a dict which contains new values , and context
"""
default = {
' value ' : { } ,
}
2010-09-29 05:29:52 +00:00
2010-09-14 13:58:56 +00:00
if not partner_id or not journal_id :
2010-09-14 13:13:29 +00:00
return default
2010-09-29 05:29:52 +00:00
2010-09-14 13:13:29 +00:00
partner_pool = self . pool . get ( ' res.partner ' )
journal_pool = self . pool . get ( ' account.journal ' )
2010-11-19 13:48:01 +00:00
journal = journal_pool . browse ( cr , uid , journal_id , context = context )
partner = partner_pool . browse ( cr , uid , partner_id , context = context )
2010-09-14 13:13:29 +00:00
account_id = False
tr_type = False
if journal . type in ( ' sale ' , ' sale_refund ' ) :
account_id = partner . property_account_receivable . id
tr_type = ' sale '
elif journal . type in ( ' purchase ' , ' purchase_refund ' , ' expense ' ) :
account_id = partner . property_account_payable . id
tr_type = ' purchase '
else :
2011-04-20 06:07:17 +00:00
if not journal . default_credit_account_id or not journal . default_debit_account_id :
raise osv . except_osv ( _ ( ' Error ! ' ) , _ ( ' Please define default credit/debit account on the %s ! ' ) % ( journal . name ) )
2010-09-14 13:13:29 +00:00
account_id = journal . default_credit_account_id . id or journal . default_debit_account_id . id
tr_type = ' receipt '
default [ ' value ' ] [ ' account_id ' ] = account_id
2010-09-18 12:43:30 +00:00
default [ ' value ' ] [ ' type ' ] = ttype or tr_type
2010-09-29 05:29:52 +00:00
2010-09-15 09:45:09 +00:00
vals = self . onchange_journal ( cr , uid , ids , journal_id , line_ids , tax_id , partner_id , context )
default [ ' value ' ] . update ( vals . get ( ' value ' ) )
2010-09-29 05:29:52 +00:00
2010-09-14 13:13:29 +00:00
return default
2010-09-29 05:29:52 +00:00
2011-07-15 08:26:43 +00:00
def onchange_partner_id ( self , cr , uid , ids , partner_id , journal_id , price , voucher_currency_id , ttype , date , context = None ) :
2010-08-26 15:04:35 +00:00
""" price
2010-08-18 20:13:20 +00:00
Returns a dict that contains new values and context
2010-09-29 05:29:52 +00:00
2010-08-18 20:13:20 +00:00
@param partner_id : latest value from user input for field partner_id
@param args : other arguments
@param context : context arguments , like lang , time zone
2010-09-29 05:29:52 +00:00
2010-08-18 20:13:20 +00:00
@return : Returns a dict which contains new values , and context
"""
2010-08-26 00:14:59 +00:00
if not journal_id :
return { }
2011-07-15 12:47:38 +00:00
if context is None :
context = { }
2010-12-22 13:35:10 +00:00
context_multi_currency = context . copy ( )
if date :
context_multi_currency . update ( { ' date ' : date } )
2010-09-29 05:29:52 +00:00
2010-12-13 20:25:12 +00:00
currency_pool = self . pool . get ( ' res.currency ' )
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 ' )
2011-07-15 12:47:38 +00:00
line_pool = self . pool . get ( ' account.voucher.line ' )
#get default values
vals = self . onchange_journal ( cr , uid , ids , journal_id , [ ] , False , partner_id , context = context )
vals = vals . get ( ' value ' , { } )
2011-07-15 08:26:43 +00:00
voucher_currency_id = vals . get ( ' currency_id ' , voucher_currency_id )
2010-08-18 20:13:20 +00:00
default = {
2011-07-15 08:26:43 +00:00
' value ' : { ' line_ids ' : [ ] , ' line_dr_ids ' : [ ] , ' line_cr_ids ' : [ ] , ' pre_line ' : False , ' currency_id ' : voucher_currency_id } ,
2010-08-18 20:13:20 +00:00
}
2010-08-26 00:14:59 +00:00
2011-07-15 12:47:38 +00:00
#drop existing lines
line_ids = ids and line_pool . search ( cr , uid , [ ( ' voucher_id ' , ' in ' , ids ) ] ) or [ ]
if line_ids :
line_pool . unlink ( cr , uid , line_ids , context = context )
2010-08-26 15:04:35 +00:00
2011-07-15 12:47:38 +00:00
if not partner_id :
2010-08-18 20:13:20 +00:00
return default
2010-08-26 00:14:59 +00:00
2010-11-19 13:48:01 +00:00
journal = journal_pool . browse ( cr , uid , journal_id , context = context )
partner = partner_pool . browse ( cr , uid , partner_id , context = context )
2010-08-26 00:14:59 +00:00
account_id = False
if journal . type in ( ' sale ' , ' sale_refund ' ) :
2010-08-19 14:01:41 +00:00
account_id = partner . property_account_receivable . id
2010-08-26 00:14:59 +00:00
elif journal . type in ( ' purchase ' , ' purchase_refund ' , ' expense ' ) :
2010-08-19 14:01:41 +00:00
account_id = partner . property_account_payable . id
2010-08-26 00:14:59 +00:00
else :
account_id = journal . default_credit_account_id . id or journal . default_debit_account_id . id
2010-09-13 19:05:26 +00:00
default [ ' value ' ] [ ' account_id ' ] = account_id
2010-10-06 08:00:25 +00:00
2010-08-26 00:14:59 +00:00
if journal . type not in ( ' cash ' , ' bank ' ) :
2010-08-18 20:13:20 +00:00
return default
2010-09-29 05:29:52 +00:00
2010-09-01 16:18:05 +00:00
total_credit = 0.0
total_debit = 0.0
2010-08-26 19:09:33 +00:00
account_type = ' receivable '
if ttype == ' payment ' :
account_type = ' payable '
2010-09-01 16:18:05 +00:00
total_debit = price or 0.0
2010-08-26 19:09:33 +00:00
else :
2010-09-01 16:18:05 +00:00
total_credit = price or 0.0
2010-08-26 19:09:33 +00:00
account_type = ' receivable '
2010-09-29 11:25:46 +00:00
if not context . get ( ' move_line_ids ' , False ) :
2010-10-30 18:54:38 +00:00
ids = move_line_pool . search ( cr , uid , [ ( ' state ' , ' = ' , ' valid ' ) , ( ' account_id.type ' , ' = ' , account_type ) , ( ' reconcile_id ' , ' = ' , False ) , ( ' partner_id ' , ' = ' , partner_id ) ] , context = context )
2010-09-29 11:25:46 +00:00
else :
ids = context [ ' move_line_ids ' ]
2011-07-15 14:41:26 +00:00
#order the lines by most old first
2010-09-01 07:18:02 +00:00
ids . reverse ( )
2010-09-29 05:29:52 +00:00
2011-07-15 14:41:26 +00:00
moves = move_line_pool . browse ( cr , uid , ids , context = context )
2010-09-15 14:33:40 +00:00
company_currency = journal . company_id . currency_id . id
2011-07-15 08:26:43 +00:00
if company_currency != voucher_currency_id and ttype == ' payment ' :
total_debit = currency_pool . compute ( cr , uid , voucher_currency_id , company_currency , total_debit , context = context_multi_currency )
elif company_currency != voucher_currency_id and ttype == ' receipt ' :
total_credit = currency_pool . compute ( cr , uid , voucher_currency_id , company_currency , total_credit , context = context_multi_currency )
2010-09-29 05:29:52 +00:00
2011-07-15 14:41:26 +00:00
invoice_id = context . get ( ' invoice_id ' , False )
move_line_found = False
2010-08-26 00:14:59 +00:00
for line in moves :
2010-09-14 12:14:31 +00:00
if line . credit and line . reconcile_partial_id and ttype == ' receipt ' :
continue
if line . debit and line . reconcile_partial_id and ttype == ' payment ' :
2010-09-29 05:29:52 +00:00
continue
2011-07-15 14:41:26 +00:00
if invoice_id and line . move_id . invoice_id . id == invoice_id :
move_line_found = line . id
break
if voucher_currency_id == company_currency :
if line . amount_residual == price :
move_line_found = line . id
break
total_credit + = line . credit or 0.0
total_debit + = line . debit or 0.0
elif voucher_currency_id == line . currency_id . id :
if line . amount_residual_currency == price :
move_line_found = line . id
break
total_credit + = line . amount_currency or 0.0
total_debit + = line . amount_currency or 0.0
2010-08-26 00:14:59 +00:00
for line in moves :
2010-09-14 12:14:31 +00:00
if line . credit and line . reconcile_partial_id and ttype == ' receipt ' :
2010-08-26 00:14:59 +00:00
continue
2010-09-14 12:14:31 +00:00
if line . debit and line . reconcile_partial_id and ttype == ' payment ' :
continue
2010-12-02 13:02:17 +00:00
original_amount = line . credit or line . debit or 0.0
2011-07-14 13:44:21 +00:00
amount_original , amount_unreconciled = line_pool . _get_amounts ( cr , uid , line , context = context )
currency_id = line . currency_id and line . currency_id . id or line . company_id . currency_id . id
2010-08-26 00:14:59 +00:00
rs = {
2010-08-20 13:47:29 +00:00
' name ' : line . move_id . name ,
2010-08-26 00:14:59 +00:00
' type ' : line . credit and ' dr ' or ' cr ' ,
2010-08-20 11:40:55 +00:00
' move_line_id ' : line . id ,
2010-08-26 00:14:59 +00:00
' account_id ' : line . account_id . id ,
2011-07-14 13:44:21 +00:00
' amount_original ' : amount_original ,
2011-08-04 10:04:57 +00:00
' amount ' : ( move_line_found == line . id ) and min ( price , amount_unreconciled ) or 0.0 ,
2011-07-14 13:44:21 +00:00
' currency_id ' : currency_id ,
2011-08-04 10:04:57 +00:00
' voucher_currency_id ' : voucher_currency_id ,
2010-08-26 00:14:59 +00:00
' date_original ' : line . date ,
2011-07-14 13:44:21 +00:00
' company_currency_id ' : line . company_id . currency_id . id ,
2010-08-26 00:14:59 +00:00
' date_due ' : line . date_maturity ,
2011-01-05 13:55:31 +00:00
' amount_unreconciled ' : amount_unreconciled ,
2011-07-15 08:26:43 +00:00
' amount_in_company_currency ' : 0.0 ,
2010-08-26 00:14:59 +00:00
}
2011-01-05 13:55:31 +00:00
2011-07-15 14:41:26 +00:00
if not move_line_found :
#TODO: this part isn't working really: we should assign the voucher amount on only lines that are or the same currency
if company_currency == voucher_currency_id :
if line . credit :
amount = min ( amount_unreconciled , abs ( total_debit ) )
rs [ ' amount ' ] = amount
total_debit - = amount
else :
amount = min ( amount_unreconciled , abs ( total_credit ) )
rs [ ' amount ' ] = amount
total_credit - = amount
elif voucher_currency_id == line . currency_id . id :
if line . credit :
amount = min ( amount_unreconciled , abs ( total_debit ) )
rs [ ' amount ' ] = amount
total_debit - = amount
else :
amount = min ( amount_unreconciled , abs ( total_credit ) )
rs [ ' amount ' ] = amount
total_credit - = amount
2010-08-26 00:14:59 +00:00
default [ ' value ' ] [ ' line_ids ' ] . append ( rs )
if rs [ ' type ' ] == ' cr ' :
default [ ' value ' ] [ ' line_cr_ids ' ] . append ( rs )
else :
default [ ' value ' ] [ ' line_dr_ids ' ] . append ( rs )
2010-09-29 05:29:52 +00:00
2010-08-26 19:09:33 +00:00
if ttype == ' payment ' and len ( default [ ' value ' ] [ ' line_cr_ids ' ] ) > 0 :
2010-08-26 00:14:59 +00:00
default [ ' value ' ] [ ' pre_line ' ] = 1
2010-08-26 19:09:33 +00:00
elif ttype == ' receipt ' and len ( default [ ' value ' ] [ ' line_dr_ids ' ] ) > 0 :
2010-09-29 05:29:52 +00:00
default [ ' value ' ] [ ' pre_line ' ] = 1
2011-07-15 08:26:43 +00:00
default [ ' value ' ] [ ' writeoff_amount ' ] , default [ ' value ' ] [ ' currency_rate_difference ' ] = self . _compute_writeoff_amount ( cr , uid , default [ ' value ' ] [ ' line_dr_ids ' ] , default [ ' value ' ] [ ' line_cr_ids ' ] , price , date , context = context )
2010-08-26 00:14:59 +00:00
return default
2010-08-20 13:47:29 +00:00
2010-12-22 13:35:10 +00:00
def onchange_date ( self , cr , uid , ids , partner_id , journal_id , price , currency_id , ttype , date , context = None ) :
2010-08-13 21:08:42 +00:00
"""
@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
"""
2011-08-04 09:55:50 +00:00
if context is None : context = { }
2010-08-13 21:08:42 +00:00
period_pool = self . pool . get ( ' account.period ' )
2010-12-22 13:35:10 +00:00
res = self . onchange_partner_id ( cr , uid , ids , partner_id , journal_id , price , currency_id , ttype , date , context = context )
2011-08-04 09:55:50 +00:00
if context . get ( ' invoice_id ' , False ) :
company_id = self . pool . get ( ' account.invoice ' ) . browse ( cr , uid , context [ ' invoice_id ' ] , context = context ) . company_id . id
context . update ( { ' company_id ' : company_id } )
pids = period_pool . find ( cr , uid , date , context = context )
2010-12-22 13:35:10 +00:00
if pids :
2010-12-22 22:47:22 +00:00
if not ' value ' in res :
res [ ' value ' ] = { }
2010-12-22 13:35:10 +00:00
res [ ' value ' ] . update ( { ' period_id ' : pids [ 0 ] } )
return res
2010-08-13 21:02:45 +00:00
2010-11-19 13:48:01 +00:00
def onchange_journal ( self , cr , uid , ids , journal_id , line_ids , tax_id , partner_id , context = None ) :
2011-08-04 09:55:50 +00:00
if context is None : context = { }
2010-09-01 13:54:07 +00:00
if not journal_id :
2011-07-15 12:47:38 +00:00
return { }
2010-09-01 13:54:07 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
2010-11-19 13:48:01 +00:00
journal = journal_pool . browse ( cr , uid , journal_id , context = context )
2010-09-01 13:54:07 +00:00
account_id = journal . default_credit_account_id or journal . default_debit_account_id
tax_id = False
if account_id and account_id . tax_ids :
tax_id = account_id . tax_ids [ 0 ] . id
2010-09-29 05:29:52 +00:00
2010-09-14 08:09:17 +00:00
vals = self . onchange_price ( cr , uid , ids , line_ids , tax_id , partner_id , context )
vals [ ' value ' ] . update ( { ' tax_id ' : tax_id } )
2010-09-15 14:33:40 +00:00
currency_id = journal . company_id . currency_id . id
if journal . currency :
currency_id = journal . currency . id
vals [ ' value ' ] . update ( { ' currency_id ' : currency_id } )
2011-08-04 09:55:50 +00:00
context . update ( { ' company_id ' : journal . company_id . id } )
periods = self . pool . get ( ' account.period ' ) . find ( cr , uid , context = context )
vals [ ' value ' ] . update ( { ' period_id ' : periods [ 0 ] } )
2010-09-14 08:09:17 +00:00
return vals
2010-08-14 12:20:54 +00:00
2010-09-21 10:13:33 +00:00
def proforma_voucher ( self , cr , uid , ids , context = None ) :
self . action_move_line_create ( cr , uid , ids , context = context )
2008-10-23 05:53:55 +00:00
return True
2010-08-14 12:20:54 +00:00
2010-11-19 13:48:01 +00:00
def action_cancel_draft ( self , cr , uid , ids , context = None ) :
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-11-19 13:48:01 +00:00
def cancel_voucher ( self , cr , uid , ids , context = None ) :
2010-09-01 16:18:05 +00:00
reconcile_pool = self . pool . get ( ' account.move.reconcile ' )
2010-07-07 13:05:41 +00:00
move_pool = self . pool . get ( ' account.move ' )
2010-09-29 05:29:52 +00:00
2010-11-19 13:48:01 +00:00
for voucher in self . browse ( cr , uid , ids , context = context ) :
2010-09-01 16:18:05 +00:00
recs = [ ]
for line in voucher . move_ids :
if line . reconcile_id :
recs + = [ line . reconcile_id . id ]
if line . reconcile_partial_id :
recs + = [ line . reconcile_partial_id . id ]
reconcile_pool . unlink ( cr , uid , recs )
2010-09-29 05:29:52 +00:00
2010-07-07 13:05:41 +00:00
if voucher . move_id :
move_pool . button_cancel ( cr , uid , [ voucher . move_id . id ] )
move_pool . unlink ( cr , uid , [ voucher . move_id . id ] )
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 ) :
2010-08-26 00:14:59 +00:00
for t in self . read ( cr , uid , ids , [ ' state ' ] , context = context ) :
if t [ ' state ' ] not in ( ' draft ' , ' cancel ' ) :
raise osv . except_osv ( _ ( ' Invalid action ! ' ) , _ ( ' Cannot delete Voucher(s) which are already opened or paid ! ' ) )
return super ( account_voucher , self ) . unlink ( cr , uid , ids , context = context )
2009-12-24 09:39:00 +00:00
2010-08-26 00:14:59 +00:00
def onchange_payment ( self , cr , uid , ids , pay_now , journal_id , partner_id , ttype = ' sale ' ) :
2010-09-07 11:54:14 +00:00
res = { }
2010-08-26 00:14:59 +00:00
if not partner_id :
2010-09-07 11:54:14 +00:00
return res
2010-08-19 14:01:41 +00:00
res = { ' account_id ' : False }
2010-09-07 11:54:14 +00:00
partner_pool = self . pool . get ( ' res.partner ' )
journal_pool = self . pool . get ( ' account.journal ' )
2010-08-26 00:14:59 +00:00
if pay_now == ' pay_later ' :
2010-08-19 14:01:41 +00:00
partner = partner_pool . browse ( cr , uid , partner_id )
2010-09-07 11:54:14 +00:00
journal = journal_pool . browse ( cr , uid , journal_id )
if journal . type in ( ' sale ' , ' sale_refund ' ) :
account_id = partner . property_account_receivable . id
elif journal . type in ( ' purchase ' , ' purchase_refund ' , ' expense ' ) :
account_id = partner . property_account_payable . id
else :
account_id = journal . default_credit_account_id . id or journal . default_debit_account_id . id
res [ ' account_id ' ] = account_id
return { ' value ' : res }
2010-08-26 00:14:59 +00:00
2010-09-21 10:13:33 +00:00
def action_move_line_create ( self , cr , uid , ids , context = None ) :
2010-09-29 05:29:52 +00:00
2010-08-26 19:09:33 +00:00
def _get_payment_term_lines ( term_id , amount ) :
term_pool = self . pool . get ( ' account.payment.term ' )
if term_id and amount :
terms = term_pool . compute ( cr , uid , term_id , amount )
return terms
return False
2010-11-23 07:05:05 +00:00
if context is None :
2010-09-21 10:13:33 +00:00
context = { }
2010-07-07 13:05:41 +00:00
move_pool = self . pool . get ( ' account.move ' )
move_line_pool = self . pool . get ( ' account.move.line ' )
currency_pool = self . pool . get ( ' res.currency ' )
2010-10-14 06:09:17 +00:00
tax_obj = self . pool . get ( ' account.tax ' )
2010-10-26 13:33:52 +00:00
seq_obj = self . pool . get ( ' ir.sequence ' )
2011-07-14 13:44:21 +00:00
for voucher in self . browse ( cr , uid , ids , context = context ) :
if voucher . move_id :
2008-10-23 05:53:55 +00:00
continue
2010-12-22 13:35:10 +00:00
context_multi_currency = context . copy ( )
2011-07-14 13:44:21 +00:00
context_multi_currency . update ( { ' date ' : voucher . date } )
2010-12-22 13:35:10 +00:00
2011-07-14 13:44:21 +00:00
if voucher . number :
name = voucher . number
2010-07-28 18:35:57 +00:00
else :
2011-07-14 13:44:21 +00:00
name = seq_obj . get_id ( cr , uid , voucher . journal_id . sequence_id . id )
if not voucher . reference :
2010-10-21 11:02:12 +00:00
ref = name . replace ( ' / ' , ' ' )
else :
2011-07-14 13:44:21 +00:00
ref = voucher . reference
2010-08-13 21:02:45 +00:00
2009-05-14 07:06:09 +00:00
move = {
2010-10-13 13:46:45 +00:00
' name ' : name ,
2011-07-14 13:44:21 +00:00
' journal_id ' : voucher . journal_id . id ,
' narration ' : voucher . narration ,
' date ' : voucher . date ,
2010-10-21 11:02:12 +00:00
' ref ' : ref ,
2011-07-14 13:44:21 +00:00
' period_id ' : voucher . period_id and voucher . period_id . id or False
2009-05-14 07:06:09 +00:00
}
2010-07-07 13:05:41 +00:00
move_id = move_pool . create ( cr , uid , move )
2010-09-29 05:29:52 +00:00
2010-07-07 13:05:41 +00:00
#create the first line manually
2011-07-14 13:44:21 +00:00
company_currency = voucher . journal_id . company_id . currency_id . id
2011-07-15 08:26:43 +00:00
voucher_currency = voucher . currency_id . id
2010-08-26 00:14:59 +00:00
debit = 0.0
credit = 0.0
2011-07-14 13:44:21 +00:00
if voucher . type in ( ' purchase ' , ' payment ' ) :
2011-07-15 08:26:43 +00:00
credit = currency_pool . compute ( cr , uid , voucher_currency , company_currency , voucher . amount , context = context_multi_currency )
2011-07-14 13:44:21 +00:00
elif voucher . type in ( ' sale ' , ' receipt ' ) :
2011-07-15 08:26:43 +00:00
debit = currency_pool . compute ( cr , uid , voucher_currency , company_currency , voucher . amount , context = context_multi_currency )
2010-09-01 12:08:08 +00:00
if debit < 0 :
credit = - debit
debit = 0.0
if credit < 0 :
debit = - credit
credit = 0.0
2010-11-08 10:07:40 +00:00
sign = debit - credit < 0 and - 1 or 1
2011-01-05 13:55:31 +00:00
#create the first line of the voucher
2010-09-01 16:18:05 +00:00
move_line = {
2011-07-14 13:44:21 +00:00
' name ' : voucher . name or ' / ' ,
2010-10-18 08:44:34 +00:00
' debit ' : debit ,
' credit ' : credit ,
2011-07-14 13:44:21 +00:00
' account_id ' : voucher . account_id . id ,
2010-10-18 08:44:34 +00:00
' move_id ' : move_id ,
2011-07-14 13:44:21 +00:00
' journal_id ' : voucher . journal_id . id ,
' period_id ' : voucher . period_id . id ,
' partner_id ' : voucher . partner_id . id ,
2011-07-15 08:26:43 +00:00
' currency_id ' : company_currency < > voucher_currency and voucher_currency or False ,
' amount_currency ' : company_currency < > voucher_currency and sign * voucher . amount or 0.0 ,
2011-07-14 13:44:21 +00:00
' date ' : voucher . date ,
' date_maturity ' : voucher . date_due
2010-09-01 16:18:05 +00:00
}
2011-01-05 15:16:59 +00:00
move_line_pool . create ( cr , uid , move_line )
2011-07-14 15:48:22 +00:00
#create the move line for the currency difference
if voucher . currency_rate_difference :
if voucher . currency_rate_difference > 0 :
2011-07-15 12:47:38 +00:00
account_id = voucher . company_id . property_expense_currency_exchange
2011-07-14 15:48:22 +00:00
if not account_id :
raise osv . except_osv ( _ ( ' Warning ' ) , _ ( " Unable to create accounting entry for currency rate difference. You have to configure the field ' Income Currency Rate ' on the company! " ) )
else :
2011-07-15 12:47:38 +00:00
account_id = voucher . company_id . property_income_currency_exchange
2011-07-14 15:48:22 +00:00
if not account_id :
raise osv . except_osv ( _ ( ' Warning ' ) , _ ( " Unable to create accounting entry for currency rate difference. You have to configure the field ' Expense Currency Rate ' on the company! " ) )
2011-08-04 10:04:57 +00:00
2011-07-14 15:48:22 +00:00
currency_diff_line = {
' name ' : _ ( ' Currency Difference ' ) ,
' debit ' : voucher . currency_rate_difference > 0 and voucher . currency_rate_difference or 0.0 ,
' credit ' : voucher . currency_rate_difference < 0 and - voucher . currency_rate_difference or 0.0 ,
' account_id ' : account_id . id ,
' move_id ' : move_id ,
' journal_id ' : voucher . journal_id . id ,
' period_id ' : voucher . period_id . id ,
' partner_id ' : voucher . partner_id . id ,
' date ' : voucher . date ,
' date_maturity ' : voucher . date_due
}
move_line_pool . create ( cr , uid , currency_diff_line , context = context )
2010-09-01 16:18:05 +00:00
2011-07-15 12:47:38 +00:00
rec_list_ids = [ ]
2011-07-14 13:44:21 +00:00
for line in voucher . line_ids :
2011-01-05 13:55:31 +00:00
#create one move line per voucher line where amount is not 0.0
2011-01-28 13:32:54 +00:00
if not line . amount :
2010-08-26 00:14:59 +00:00
continue
2011-01-05 13:55:31 +00:00
#we check if the voucher line is fully paid or not and create a move line to balance the payment and initial invoice if needed
if line . amount == line . amount_unreconciled :
2011-01-06 04:56:09 +00:00
amount = line . move_line_id . amount_residual #residual amount in company currency
2011-01-05 13:55:31 +00:00
else :
2011-07-15 08:26:43 +00:00
amount = line . amount_in_company_currency
2010-08-19 14:01:41 +00:00
move_line = {
2011-07-14 13:44:21 +00:00
' journal_id ' : voucher . journal_id . id ,
' period_id ' : voucher . period_id . id ,
2011-02-15 06:45:10 +00:00
' name ' : line . name or ' / ' ,
2010-10-18 08:44:34 +00:00
' account_id ' : line . account_id . id ,
' move_id ' : move_id ,
2011-07-14 13:44:21 +00:00
' partner_id ' : voucher . partner_id . id ,
2011-07-15 08:26:43 +00:00
' currency_id ' : company_currency < > line . currency_id . id and line . currency_id . id or False ,
2010-10-18 08:44:34 +00:00
' analytic_account_id ' : line . account_analytic_id and line . account_analytic_id . id or False ,
' quantity ' : 1 ,
' credit ' : 0.0 ,
' debit ' : 0.0 ,
2011-07-14 13:44:21 +00:00
' date ' : voucher . date
2010-08-19 14:01:41 +00:00
}
2010-09-01 12:08:08 +00:00
if amount < 0 :
amount = - amount
if line . type == ' dr ' :
line . type = ' cr '
else :
line . type = ' dr '
2010-08-26 00:14:59 +00:00
if ( line . type == ' dr ' ) :
move_line [ ' debit ' ] = amount
else :
move_line [ ' credit ' ] = amount
2011-07-14 13:44:21 +00:00
if voucher . tax_id and voucher . type in ( ' sale ' , ' purchase ' ) :
2010-09-01 16:18:05 +00:00
move_line . update ( {
2011-07-14 13:44:21 +00:00
' account_tax_id ' : voucher . tax_id . id ,
2010-09-01 16:18:05 +00:00
} )
2010-10-14 06:09:17 +00:00
if move_line . get ( ' account_tax_id ' , False ) :
2010-10-14 08:39:10 +00:00
tax_data = tax_obj . browse ( cr , uid , [ move_line [ ' account_tax_id ' ] ] , context = context ) [ 0 ]
if not ( tax_data . base_code_id and tax_data . tax_code_id ) :
raise osv . except_osv ( _ ( ' No Account Base Code and Account Tax Code! ' ) , _ ( " You have to configure account base code and account tax code on the ' %s ' tax! " ) % ( tax_data . name ) )
2010-11-08 10:07:40 +00:00
sign = ( move_line [ ' debit ' ] - move_line [ ' credit ' ] ) < 0 and - 1 or 1
2011-07-15 08:26:43 +00:00
move_line [ ' amount_currency ' ] = company_currency < > line . currency_id . id and sign * line . amount or 0.0
2011-01-05 13:55:31 +00:00
voucher_line = move_line_pool . create ( cr , uid , move_line )
2010-08-26 00:14:59 +00:00
if line . move_line_id . id :
2011-01-05 13:55:31 +00:00
rec_ids = [ voucher_line , line . move_line_id . id ]
2010-08-26 00:14:59 +00:00
rec_list_ids . append ( rec_ids )
2010-09-22 12:28:48 +00:00
2011-07-14 15:48:22 +00:00
if not currency_pool . is_zero ( cr , uid , voucher . currency_id , voucher . writeoff_amount ) :
2011-07-15 12:47:38 +00:00
#create one line for the write off if needed
2011-08-04 10:04:57 +00:00
diff = currency_pool . compute ( cr , uid , voucher_currency , company_currency , voucher . writeoff_amount , context = context_multi_currency )
2010-12-13 20:25:12 +00:00
account_id = False
2011-02-15 07:10:01 +00:00
write_off_name = ' '
2011-07-14 13:44:21 +00:00
if voucher . payment_option == ' with_writeoff ' :
account_id = voucher . writeoff_acc_id . id
write_off_name = voucher . comment
elif voucher . type in ( ' sale ' , ' receipt ' ) :
account_id = voucher . partner_id . property_account_receivable . id
2010-12-13 20:25:12 +00:00
else :
2011-07-14 13:44:21 +00:00
account_id = voucher . partner_id . property_account_payable . id
2010-08-23 11:09:08 +00:00
move_line = {
2011-02-15 07:10:01 +00:00
' name ' : write_off_name or name ,
2010-12-13 20:25:12 +00:00
' account_id ' : account_id ,
2010-10-13 13:46:45 +00:00
' move_id ' : move_id ,
2011-07-14 13:44:21 +00:00
' partner_id ' : voucher . partner_id . id ,
' date ' : voucher . date ,
2010-10-11 12:49:25 +00:00
' debit ' : diff < 0 and - diff or 0.0 ,
2011-07-15 12:47:38 +00:00
' credit ' : diff > 0 and diff or 0.0 ,
' amount_currency ' : company_currency < > voucher_currency and - voucher . writeoff_amount or 0.0 ,
2011-07-15 08:26:43 +00:00
' currency_id ' : company_currency < > voucher_currency and voucher_currency or False ,
2010-08-23 11:09:08 +00:00
}
2010-10-08 09:56:14 +00:00
move_line_pool . create ( cr , uid , move_line )
2011-07-14 13:44:21 +00:00
self . write ( cr , uid , [ voucher . id ] , {
2010-08-23 11:09:08 +00:00
' move_id ' : move_id ,
2010-09-23 17:39:28 +00:00
' state ' : ' posted ' ,
' number ' : name ,
2010-08-26 00:14:59 +00:00
} )
2011-07-14 13:44:21 +00:00
if voucher . journal_id . entry_posted :
2011-03-19 08:37:45 +00:00
move_pool . post ( cr , uid , [ move_id ] , context = { } )
2010-12-13 20:25:12 +00:00
for rec_ids in rec_list_ids :
if len ( rec_ids ) > = 2 :
move_line_pool . reconcile_partial ( cr , uid , rec_ids )
2010-11-12 13:15:46 +00:00
return True
2010-08-14 12:20:54 +00:00
def copy ( self , cr , uid , id , default = { } , context = None ) :
2010-08-26 00:14:59 +00:00
default . update ( {
2010-10-18 08:44:34 +00:00
' state ' : ' draft ' ,
' number ' : False ,
' move_id ' : False ,
' line_cr_ids ' : False ,
' line_dr_ids ' : False ,
' reference ' : False
2010-08-26 00:14:59 +00:00
} )
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-26 00:14:59 +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 '
2010-08-26 00:14:59 +00:00
_description = ' Voucher Lines '
2010-08-30 13:23:02 +00:00
_order = " move_line_id "
2010-09-29 05:29:52 +00:00
2011-07-14 13:44:21 +00:00
def _currency_id ( self , cr , uid , ids , name , args , context = None ) :
res = { }
for line in self . browse ( cr , uid , ids , context = context ) :
move_line = line . move_line_id
res [ line . id ] = move_line . currency_id and move_line . currency_id . id or move_line . company_id . currency_id . id
return res
def _get_amounts ( self , cr , uid , line_browse_rec , context = None ) :
if line_browse_rec . currency_id :
amount_original = line_browse_rec . amount_currency or 0.0
amount_unreconciled = line_browse_rec . amount_residual_currency
elif line_browse_rec . credit > 0 :
amount_original = line_browse_rec . credit or 0.0
amount_unreconciled = line_browse_rec . amount_residual
else :
amount_original = line_browse_rec . debit or 0.0
amount_unreconciled = line_browse_rec . amount_residual
return amount_original , amount_unreconciled
2010-08-25 16:42:19 +00:00
def _compute_balance ( self , cr , uid , ids , name , args , context = None ) :
2010-09-15 15:22:43 +00:00
currency_pool = self . pool . get ( ' res.currency ' )
2011-07-14 13:44:21 +00:00
res = { }
2010-11-19 13:48:01 +00:00
for line in self . browse ( cr , uid , ids , context = context ) :
2011-07-14 13:44:21 +00:00
amount_original , amount_unreconciled = self . _get_amounts ( cr , uid , line . move_line_id , context )
res [ line . id ] = {
' amount_original ' : amount_original ,
' amount_unreconciled ' : amount_unreconciled ,
}
return res
2010-09-29 05:29:52 +00:00
2011-07-14 13:44:21 +00:00
def __company_currency_amount ( self , cr , uid , line , amount , context = None ) :
if context is None :
context = { }
ctx = context . copy ( )
ctx . update ( { ' date ' : line . date_original } )
currency_pool = self . pool . get ( ' res.currency ' )
2011-07-15 08:26:43 +00:00
amount_company_cur = currency_pool . compute ( cr , uid , line . currency_id . id , line . company_currency_id . id , amount , context = ctx )
ctx . update ( { ' date ' : line . voucher_id . date } )
amount_voucher_cur = currency_pool . compute ( cr , uid , line . currency_id . id , line . voucher_currency_id . id , amount , context = ctx )
return amount_company_cur , amount_voucher_cur
2011-07-14 13:44:21 +00:00
def onchange_amount ( self , cr , uid , ids , amount , context = None ) :
2011-07-15 08:26:43 +00:00
if not amount or not ids :
return { ' value ' : { ' amount_in_company_currency ' : 0.0 , ' amount_in_voucher_currency ' : 0.0 } }
2011-07-14 13:44:21 +00:00
for line in self . browse ( cr , uid , ids , context = context ) :
2011-07-15 08:26:43 +00:00
amount_company_cur , amount_voucher_cur = self . __company_currency_amount ( cr , uid , line , amount , context = context )
return { ' value ' : { ' amount_in_company_currency ' : amount_company_cur , ' amount_in_voucher_currency ' : amount_voucher_cur } }
2011-07-14 13:44:21 +00:00
def _get_amount_in_company_currency ( self , cr , uid , ids , name , args , context = None ) :
res = { }
for line in self . browse ( cr , uid , ids , context = context ) :
2011-08-04 10:04:57 +00:00
amount_in_company_currency , amount_in_voucher_currency = self . __company_currency_amount ( cr , uid , line , line . amount , context = context )
2011-07-15 08:26:43 +00:00
res [ line . id ] = {
' amount_in_company_currency ' : amount_in_company_currency ,
' amount_in_voucher_currency ' : amount_in_voucher_currency ,
}
2011-07-14 13:44:21 +00:00
return res
2010-08-17 13:24:18 +00:00
2008-10-23 05:53:55 +00:00
_columns = {
2010-08-26 19:09:33 +00:00
' voucher_id ' : fields . many2one ( ' account.voucher ' , ' Voucher ' , required = 1 , ondelete = ' cascade ' ) ,
2011-08-04 10:04:57 +00:00
' move_line_id ' : fields . many2one ( ' account.move.line ' , ' Journal Item ' ) ,
2011-07-15 08:26:43 +00:00
' type ' : fields . selection ( [ ( ' dr ' , ' Debit ' ) , ( ' cr ' , ' Credit ' ) ] , ' Dr/Cr ' ) ,
2010-08-23 11:09:08 +00:00
' name ' : fields . char ( ' Description ' , size = 256 ) ,
2010-08-26 15:04:35 +00:00
' account_id ' : fields . many2one ( ' account.account ' , ' Account ' , required = True ) ,
2011-07-15 08:26:43 +00:00
' account_analytic_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' ) ,
2010-08-17 07:32:51 +00:00
' partner_id ' : fields . related ( ' voucher_id ' , ' partner_id ' , type = ' many2one ' , relation = ' res.partner ' , string = ' Partner ' ) ,
2010-08-30 13:23:02 +00:00
' untax_amount ' : fields . float ( ' Untax Amount ' ) ,
2011-07-15 08:26:43 +00:00
' currency_id ' : fields . function ( _currency_id , string = ' Currency ' , type = ' many2one ' , relation = ' res.currency ' ) ,
2011-07-14 13:44:21 +00:00
' company_currency_id ' : fields . related ( ' move_line_id ' , ' company_id ' , ' currency_id ' , type = ' many2one ' , relation = ' res.currency ' , string = " Company Currency " ) ,
2011-07-15 08:26:43 +00:00
' voucher_currency_id ' : fields . related ( ' voucher_id ' , ' currency_id ' , type = ' many2one ' , relation = ' res.currency ' , string = " Voucher Currency " ) ,
' amount ' : fields . float ( ' Amount ' , digits_compute = dp . get_precision ( ' Account ' ) ) ,
2011-08-04 10:04:57 +00:00
' amount_in_company_currency ' : fields . function ( _get_amount_in_company_currency , string = ' Amount in Company Currency ' , type = ' float ' , digits_compute = dp . get_precision ( ' Account ' ) , multi = " voucher_line_amount " ) ,
' amount_in_voucher_currency ' : fields . function ( _get_amount_in_company_currency , string = ' Amount in Voucher Currency ' , type = ' float ' , digits_compute = dp . get_precision ( ' Account ' ) , multi = " voucher_line_amount " ) ,
2011-07-15 08:26:43 +00:00
2010-08-26 00:14:59 +00:00
' date_original ' : fields . related ( ' move_line_id ' , ' date ' , type = ' date ' , relation = ' account.move.line ' , string = ' Date ' , readonly = 1 ) ,
' date_due ' : fields . related ( ' move_line_id ' , ' date_maturity ' , type = ' date ' , relation = ' account.move.line ' , string = ' Due Date ' , readonly = 1 ) ,
2011-07-01 23:41:24 +00:00
' amount_original ' : fields . function ( _compute_balance , multi = ' dc ' , type = ' float ' , string = ' Original Amount ' , store = True ) ,
' amount_unreconciled ' : fields . function ( _compute_balance , multi = ' dc ' , type = ' float ' , string = ' Open Balance ' , store = True ) ,
2011-01-06 11:32:21 +00:00
' company_id ' : fields . related ( ' voucher_id ' , ' company_id ' , relation = ' res.company ' , type = ' many2one ' , string = ' Company ' , store = True , readonly = True ) ,
2008-10-23 05:53:55 +00:00
}
_defaults = {
2010-10-18 08:44:34 +00:00
' name ' : ' '
2008-10-23 05:53:55 +00:00
}
2009-12-24 09:39:00 +00:00
2010-11-19 13:48:01 +00:00
def onchange_move_line_id ( self , cr , user , ids , move_line_id , context = None ) :
2010-08-20 18:57:46 +00:00
"""
Returns a dict that contains new values and context
@param move_line_id : latest value from user input for field move_line_id
@param args : other arguments
@param context : context arguments , like lang , time zone
2010-09-29 05:29:52 +00:00
2010-08-20 18:57:46 +00:00
@return : Returns a dict which contains new values , and context
"""
res = { }
move_line_pool = self . pool . get ( ' account.move.line ' )
if move_line_id :
2010-08-26 00:14:59 +00:00
move_line = move_line_pool . browse ( cr , user , move_line_id , context = context )
if move_line . credit :
2010-10-11 12:49:25 +00:00
ttype = ' dr '
2010-08-26 00:14:59 +00:00
else :
2010-10-11 12:49:25 +00:00
ttype = ' cr '
2010-08-20 18:57:46 +00:00
account_id = move_line . account_id . id
res . update ( {
2010-08-26 00:14:59 +00:00
' account_id ' : account_id ,
' type ' : ttype
2010-08-20 18:57:46 +00:00
} )
return {
' value ' : res ,
}
2010-08-26 00:14:59 +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
2010-09-29 05:29:52 +00:00
@param fields_list : list of fields , for which default values are required to be read
2010-08-20 11:40:55 +00:00
@param context : context arguments , like lang , time zone
2010-09-29 05:29:52 +00:00
2010-08-20 11:40:55 +00:00
@return : Returns a dict that contains default values for fields
"""
2010-12-17 04:37:32 +00:00
if context is None :
2010-12-13 06:43:09 +00:00
context = { }
2010-08-26 00:14:59 +00:00
journal_id = context . get ( ' journal_id ' , False )
partner_id = context . get ( ' partner_id ' , False )
2010-08-20 11:40:55 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
partner_pool = self . pool . get ( ' res.partner ' )
values = super ( account_voucher_line , self ) . default_get ( cr , user , fields_list , context = context )
2010-08-26 00:14:59 +00:00
if ( not journal_id ) or ( ' account_id ' not in fields_list ) :
return values
2010-11-19 13:48:01 +00:00
journal = journal_pool . browse ( cr , user , journal_id , context = context )
2010-08-26 00:14:59 +00:00
account_id = False
2010-08-31 14:42:33 +00:00
ttype = ' cr '
2010-09-14 13:46:07 +00:00
if journal . type in ( ' sale ' , ' sale_refund ' ) :
2010-08-26 00:14:59 +00:00
account_id = journal . default_credit_account_id and journal . default_credit_account_id . id or False
2010-09-14 13:13:29 +00:00
ttype = ' cr '
2010-09-14 13:46:07 +00:00
elif journal . type in ( ' purchase ' , ' expense ' , ' purchase_refund ' ) :
2010-08-26 00:14:59 +00:00
account_id = journal . default_debit_account_id and journal . default_debit_account_id . id or False
2010-08-31 14:42:33 +00:00
ttype = ' dr '
2010-08-26 00:14:59 +00:00
elif partner_id :
2010-08-26 19:09:33 +00:00
partner = partner_pool . browse ( cr , user , partner_id , context = context )
if context . get ( ' type ' ) == ' payment ' :
2010-08-31 14:42:33 +00:00
ttype = ' dr '
2010-08-26 19:09:33 +00:00
account_id = partner . property_account_payable . id
elif context . get ( ' type ' ) == ' receipt ' :
account_id = partner . property_account_receivable . id
2010-08-26 00:14:59 +00:00
2010-08-20 11:40:55 +00:00
values . update ( {
2010-08-25 16:42:19 +00:00
' account_id ' : account_id ,
2010-08-31 14:42:33 +00:00
' type ' : ttype
2010-08-20 11:40:55 +00:00
} )
return values
2010-02-16 07:06:49 +00:00
account_voucher_line ( )
2010-09-18 12:43:30 +00:00
class account_bank_statement ( osv . osv ) :
_inherit = ' account.bank.statement '
2010-10-01 03:51:39 +00:00
def button_cancel ( self , cr , uid , ids , context = None ) :
2010-10-26 13:33:52 +00:00
voucher_obj = self . pool . get ( ' account.voucher ' )
2010-11-19 13:48:01 +00:00
for st in self . browse ( cr , uid , ids , context = context ) :
2010-10-01 03:51:39 +00:00
voucher_ids = [ ]
for line in st . line_ids :
if line . voucher_id :
voucher_ids . append ( line . voucher_id . id )
2010-10-26 13:33:52 +00:00
voucher_obj . cancel_voucher ( cr , uid , voucher_ids , context )
2010-10-01 03:51:39 +00:00
return super ( account_bank_statement , self ) . button_cancel ( cr , uid , ids , context = context )
2010-09-21 10:13:33 +00:00
def create_move_from_st_line ( self , cr , uid , st_line_id , company_currency_id , next_number , context = None ) :
2010-09-29 05:29:52 +00:00
voucher_obj = self . pool . get ( ' account.voucher ' )
wf_service = netsvc . LocalService ( " workflow " )
2010-10-26 13:33:52 +00:00
move_line_obj = self . pool . get ( ' account.move.line ' )
2010-10-17 20:31:19 +00:00
bank_st_line_obj = self . pool . get ( ' account.bank.statement.line ' )
st_line = bank_st_line_obj . browse ( cr , uid , st_line_id , context = context )
2010-09-18 12:43:30 +00:00
if st_line . voucher_id :
2010-09-29 05:29:52 +00:00
voucher_obj . write ( cr , uid , [ st_line . voucher_id . id ] , { ' number ' : next_number } , context = context )
2010-10-06 12:48:23 +00:00
if st_line . voucher_id . state == ' cancel ' :
voucher_obj . action_cancel_draft ( cr , uid , [ st_line . voucher_id . id ] , context = context )
2010-09-22 12:28:48 +00:00
wf_service . trg_validate ( uid , ' account.voucher ' , st_line . voucher_id . id , ' proforma_voucher ' , cr )
2010-10-17 20:31:19 +00:00
v = voucher_obj . browse ( cr , uid , st_line . voucher_id . id , context = context )
bank_st_line_obj . write ( cr , uid , [ st_line_id ] , {
' move_ids ' : [ ( 4 , v . move_id . id , False ) ]
} )
2010-10-26 13:33:52 +00:00
return move_line_obj . write ( cr , uid , [ x . id for x in v . move_ids ] , { ' statement_id ' : st_line . statement_id . id } , context = context )
2010-10-01 03:51:39 +00:00
return super ( account_bank_statement , self ) . create_move_from_st_line ( cr , uid , st_line . id , company_currency_id , next_number , context = context )
2010-09-18 12:43:30 +00:00
account_bank_statement ( )
class account_bank_statement_line ( osv . osv ) :
_inherit = ' account.bank.statement.line '
def _amount_reconciled ( self , cursor , user , ids , name , args , context = None ) :
if not ids :
return { }
2010-09-29 09:41:19 +00:00
2010-09-18 12:43:30 +00:00
res = { }
2010-10-08 09:56:14 +00:00
# company_currency_id = False
2010-09-18 12:43:30 +00:00
for line in self . browse ( cursor , user , ids , context = context ) :
2010-09-29 09:41:19 +00:00
# if not company_currency_id:
# company_currency_id = line.company_id.id
2010-09-18 12:43:30 +00:00
if line . voucher_id :
2010-09-29 09:41:19 +00:00
res [ line . id ] = line . voucher_id . amount #
# res_currency_obj.compute(cursor, user,
# company_currency_id, line.statement_id.currency.id,
# line.voucher_id.amount, context=context)
2010-09-18 12:43:30 +00:00
else :
res [ line . id ] = 0.0
return res
2010-12-04 05:24:20 +00:00
def _check_amount ( self , cr , uid , ids , context = None ) :
2010-12-10 05:27:57 +00:00
for obj in self . browse ( cr , uid , ids , context = context ) :
2010-12-09 12:53:12 +00:00
if obj . voucher_id :
2011-03-17 09:38:22 +00:00
diff = abs ( obj . amount ) - obj . voucher_id . amount
if not self . pool . get ( ' res.currency ' ) . is_zero ( cr , uid , obj . voucher_id . currency_id , diff ) :
2010-12-09 12:53:12 +00:00
return False
return True
2010-12-04 05:24:20 +00:00
_constraints = [
2010-12-10 05:27:57 +00:00
( _check_amount , ' The amount of the voucher must be the same amount as the one on the statement line ' , [ ' amount ' ] ) ,
2010-12-04 05:24:20 +00:00
]
2010-09-18 12:43:30 +00:00
_columns = {
' amount_reconciled ' : fields . function ( _amount_reconciled ,
2011-07-01 23:41:24 +00:00
string = ' Amount reconciled ' , type = ' float ' ) ,
2010-09-18 12:43:30 +00:00
' voucher_id ' : fields . many2one ( ' account.voucher ' , ' Payment ' ) ,
}
2010-09-22 12:28:48 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
2010-10-26 13:33:52 +00:00
voucher_obj = self . pool . get ( ' account.voucher ' )
2010-11-19 13:48:01 +00:00
statement_line = self . browse ( cr , uid , ids , context = context )
2010-09-22 12:28:48 +00:00
unlink_ids = [ ]
for st_line in statement_line :
2010-10-01 03:51:39 +00:00
if st_line . voucher_id :
unlink_ids . append ( st_line . voucher_id . id )
2010-10-26 13:33:52 +00:00
voucher_obj . unlink ( cr , uid , unlink_ids , context = context )
2010-09-22 12:28:48 +00:00
return super ( account_bank_statement_line , self ) . unlink ( cr , uid , ids , context = context )
2010-10-17 20:31:19 +00:00
account_bank_statement_line ( )
2010-10-15 06:01:40 +00:00
2011-06-30 09:20:57 +00:00
def resolve_o2m_operations ( cr , uid , target_osv , operations , fields , context ) :
results = [ ]
for operation in operations :
result = None
if not isinstance ( operation , ( list , tuple ) ) :
result = target_osv . read ( cr , uid , operation , fields , context = context )
2011-06-30 09:42:13 +00:00
elif operation [ 0 ] == 0 :
2011-06-30 09:20:57 +00:00
# may be necessary to check if all the fields are here and get the default values?
result = operation [ 2 ]
elif operation [ 0 ] == 1 :
result = target_osv . read ( cr , uid , operation [ 1 ] , fields , context = context )
result . update ( operation [ 2 ] )
elif operation [ 0 ] == 4 :
result = target_osv . read ( cr , uid , operation [ 1 ] , fields , context = context )
2011-06-30 10:19:04 +00:00
if result != None :
results . append ( result )
2011-06-30 09:20:57 +00:00
return results
2010-12-22 13:35:10 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: