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-05-27 19:55:40 +00:00
from osv import fields
from osv import osv
2010-07-22 08:43:54 +00:00
from tools . translate import _
2010-07-07 13:05:41 +00:00
2010-08-26 00:14:59 +00:00
class account_move_line ( osv . osv ) :
_inherit = ' account.move.line '
2010-10-11 07:34:19 +00:00
2010-08-26 00:14:59 +00:00
def _unreconciled ( self , cr , uid , ids , prop , unknow_none , context ) :
2010-10-11 07:34:19 +00:00
res = { }
2010-08-26 00:14:59 +00:00
for line in self . browse ( cr , uid , ids , context = context ) :
res [ line . id ] = line . debit - line . credit
if line . reconcile_partial_id :
res [ line . id ] = 0
for partial in line . reconcile_partial_id . line_partial_ids :
res [ line . id ] + = partial . debit - partial . credit
res [ line . id ] = abs ( res [ line . id ] )
2010-08-18 20:13:20 +00:00
return res
2010-08-26 00:14:59 +00:00
2010-08-18 20:13:20 +00:00
_columns = {
2010-08-26 00:14:59 +00:00
' amount_unreconciled ' : fields . function ( _unreconciled , method = True , string = ' Unreconciled Amount ' ) ,
2010-08-18 20:13:20 +00:00
}
2010-10-11 07:34:19 +00:00
2010-08-26 00:14:59 +00:00
account_move_line ( )
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-08-26 15:04:35 +00:00
def _get_type ( self , cr , uid , ids , context = { } ) :
2010-09-18 12:43:30 +00:00
return context . get ( ' type ' , False )
2010-09-29 05:29:52 +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 )
2010-08-26 00:14:59 +00:00
return periods and periods [ 0 ] or False
2009-12-24 09:39:00 +00:00
2010-05-26 19:30:21 +00:00
def _get_journal ( self , cr , uid , context = { } ) :
journal_pool = self . pool . get ( ' account.journal ' )
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-08-25 16:42:19 +00:00
def _get_tax ( self , cr , uid , 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-08-25 16:42:19 +00:00
journal = journal_pool . browse ( cr , uid , journal_id )
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
2008-10-23 05:53:55 +00:00
def _get_currency ( self , cr , uid , 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 :
journal = journal_pool . browse ( cr , uid , journal_id )
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-09-01 08:06:53 +00:00
def _get_partner ( self , cr , uid , context = { } ) :
return context . get ( ' partner_id ' , False )
2010-09-23 07:13:12 +00:00
2010-09-15 14:33:40 +00:00
def _get_reference ( self , cr , uid , context = { } ) :
return context . get ( ' reference ' , False )
2010-09-23 07:13:12 +00:00
2010-09-15 14:33:40 +00:00
def _get_narration ( self , cr , uid , context = { } ) :
return context . get ( ' narration ' , False )
2010-09-23 07:13:12 +00:00
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-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-10-11 12:49:25 +00:00
if not view_id and context . get ( ' invoice_type ' , False ) :
2010-10-06 08:00:25 +00:00
mod_obj = self . pool . get ( ' ir.model.data ' )
2010-10-11 07:34:19 +00:00
if context . get ( ' invoice_type ' ) in ( ' out_invoice ' , ' out_refund ' ) :
2010-10-06 08:00:25 +00:00
result = mod_obj . _get_id ( cr , uid , ' account_voucher ' , ' view_vendor_receipt_form ' )
else :
result = mod_obj . _get_id ( cr , uid , ' account_voucher ' , ' view_vendor_payment_form ' )
result = mod_obj . read ( cr , uid , [ result ] , [ ' res_id ' ] , context = context ) [ 0 ] [ ' res_id ' ]
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
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 "
2010-09-23 07:26:57 +00:00
# _rec_name = 'number'
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 ) ] } ) ,
2010-07-14 05:51:50 +00:00
' date ' : fields . date ( ' Date ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , help = " Effective date for accounting entries " ) ,
2008-10-23 05:53:55 +00:00
' journal_id ' : fields . many2one ( ' account.journal ' , ' Journal ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-19 14:01:41 +00:00
' account_id ' : fields . many2one ( ' account.account ' , ' Account ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-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 ) ] } ) ,
2010-09-15 14:33:40 +00:00
' currency_id ' : fields . many2one ( ' res.currency ' , ' Currency ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
# 'currency_id': fields.related('journal_id','currency', type='many2one', relation='res.currency', string='Currency', store=True, readonly=True, states={'draft':[('readonly',False)]}),
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
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-08-25 16:42:19 +00:00
' amount ' : fields . float ( ' Total ' , digits = ( 16 , 2 ) , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-23 11:09:08 +00:00
' tax_amount ' : fields . float ( ' Tax Amount ' , digits = ( 14 , 2 ) , 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 ) ] } ) ,
2010-08-14 07:53:03 +00:00
' audit ' : fields . related ( ' move_id ' , ' to_check ' , type = ' boolean ' , relation = ' account.move ' , string = ' Audit Complete ? ' ) ,
2010-08-18 20:13:20 +00:00
' pay_now ' : fields . selection ( [
2010-08-19 07:57:59 +00:00
( ' pay_now ' , ' Pay Directly ' ) ,
( ' pay_later ' , ' Pay Later or Group Funds ' ) ,
2010-08-18 20:13:20 +00:00
] , ' Payment ' , select = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-19 14:01:41 +00:00
' tax_id ' : fields . many2one ( ' account.tax ' , ' Tax ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2010-08-25 16:42:19 +00:00
' pre_line ' : fields . boolean ( ' Previous Payments ? ' , required = False ) ,
2010-09-14 11:19:20 +00:00
' date_due ' : fields . date ( ' Due Date ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
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-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-10-13 13:46:45 +00:00
' date ' : 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 ,
2008-10-23 05:53:55 +00:00
}
2010-09-29 05:29:52 +00:00
2010-08-30 13:23:02 +00:00
def compute_tax ( self , cr , uid , ids , context = { } ) :
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-09-29 05:29:52 +00:00
2010-08-30 13:23:02 +00:00
for voucher in voucher_pool . browse ( cr , uid , ids , context ) :
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-08-30 13:23:02 +00:00
tax = [ tax_pool . browse ( cr , uid , voucher . tax_id . id ) ]
partner = partner_pool . browse ( cr , uid , voucher . partner_id . id ) or False
taxes = position_pool . map_tax ( cr , uid , partner and partner . property_account_position or False , tax )
tax = tax_pool . browse ( cr , uid , taxes )
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-08-26 00:14:59 +00:00
def onchange_price ( self , cr , uid , ids , line_ids , tax_id , partner_id = False , context = { } ) :
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 ' )
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
2010-08-26 00:14:59 +00:00
voucher_line_ids = [ ]
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
2010-10-15 21:47:18 +00:00
line_amount = line [ 2 ] and line [ 2 ] . get ( ' amount ' , 0.0 ) or 0.0
2010-08-26 00:14:59 +00:00
voucher_line_ids + = [ line [ 1 ] ]
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 :
tax = [ tax_pool . browse ( cr , uid , tax_id ) ]
if partner_id :
partner = partner_pool . browse ( cr , uid , partner_id ) or False
taxes = position_pool . map_tax ( cr , uid , partner and partner . property_account_position or False , tax )
tax = tax_pool . browse ( cr , uid , taxes )
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-09-15 09:45:09 +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 = { } ) :
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 ' )
journal = journal_pool . browse ( cr , uid , journal_id )
partner = partner_pool . browse ( cr , uid , partner_id )
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 :
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
2010-09-30 05:40:40 +00:00
def onchange_partner_id ( self , cr , uid , ids , partner_id , journal_id , price , currency_id , ttype , 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 { }
2010-09-29 05:29:52 +00:00
2010-09-30 05:40:40 +00:00
if context is None :
context = { }
2010-09-06 09:35:34 +00:00
currency_pool = self . pool . get ( ' res.currency ' )
2010-08-18 20:13:20 +00:00
line_pool = self . pool . get ( ' account.voucher.line ' )
2010-08-20 11:40:55 +00:00
move_line_pool = self . pool . get ( ' account.move.line ' )
2010-08-19 14:01:41 +00:00
partner_pool = self . pool . get ( ' res.partner ' )
2010-08-20 11:40:55 +00:00
journal_pool = self . pool . get ( ' account.journal ' )
2010-09-29 05:29:52 +00:00
2010-09-15 14:33:40 +00:00
vals = self . onchange_journal ( cr , uid , ids , journal_id , [ ] , False , partner_id , context )
vals = vals . get ( ' value ' )
currency_id = vals . get ( ' currency_id ' , currency_id )
2010-08-18 20:13:20 +00:00
default = {
2010-09-15 14:33:40 +00:00
' value ' : { ' line_ids ' : [ ] , ' line_dr_ids ' : [ ] , ' line_cr_ids ' : [ ] , ' pre_line ' : False , ' currency_id ' : currency_id } ,
2010-08-18 20:13:20 +00:00
}
2010-08-26 00:14:59 +00:00
2010-08-23 11:09:08 +00:00
if not partner_id :
2010-08-26 15:04:35 +00:00
return default
if not partner_id and ids :
2010-10-11 12:49:25 +00:00
line_ids = line_pool . search ( cr , uid , [ ( ' voucher_id ' , ' = ' , ids [ 0 ] ) ] )
2010-08-26 15:04:35 +00:00
if line_ids :
line_pool . unlink ( cr , uid , line_ids )
2010-08-18 20:13:20 +00:00
return default
2010-08-26 00:14:59 +00:00
journal = journal_pool . browse ( cr , uid , journal_id )
2010-08-19 14:01:41 +00:00
partner = partner_pool . browse ( cr , uid , partner_id )
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-11 12:49:25 +00:00
ids = move_line_pool . search ( cr , uid , [ ( ' 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 ' ]
2010-09-01 07:18:02 +00:00
ids . reverse ( )
2010-08-26 00:14:59 +00:00
moves = move_line_pool . browse ( cr , uid , ids )
2010-09-29 05:29:52 +00:00
2010-09-15 14:33:40 +00:00
company_currency = journal . company_id . currency_id . id
2010-09-06 09:35:34 +00:00
if company_currency != currency_id and ttype == ' payment ' :
total_debit = currency_pool . compute ( cr , uid , currency_id , company_currency , total_debit )
elif company_currency != currency_id and ttype == ' receipt ' :
total_credit = currency_pool . compute ( cr , uid , currency_id , company_currency , total_credit )
2010-09-29 05:29:52 +00:00
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
2010-08-26 00:14:59 +00:00
total_credit + = line . credit or 0.0
total_debit + = line . debit or 0.0
2010-09-06 09:35:34 +00:00
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-09-29 05:29:52 +00:00
2010-09-15 14:33:40 +00:00
orignal_amount = line . credit or line . debit or 0.0
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 ,
2010-09-15 14:33:40 +00:00
' amount_original ' : currency_pool . compute ( cr , uid , company_currency , currency_id , orignal_amount ) ,
2010-08-26 00:14:59 +00:00
' date_original ' : line . date ,
' date_due ' : line . date_maturity ,
2010-09-15 14:33:40 +00:00
' amount_unreconciled ' : currency_pool . compute ( cr , uid , company_currency , currency_id , line . amount_unreconciled )
2010-08-26 00:14:59 +00:00
}
if line . credit :
2010-09-06 09:35:34 +00:00
amount = min ( line . amount_unreconciled , total_debit )
rs [ ' amount ' ] = currency_pool . compute ( cr , uid , company_currency , currency_id , amount )
total_debit - = amount
2010-08-26 00:14:59 +00:00
else :
2010-09-06 09:35:34 +00:00
amount = min ( line . amount_unreconciled , total_credit )
rs [ ' amount ' ] = currency_pool . compute ( cr , uid , company_currency , currency_id , 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
2010-10-06 05:20:59 +00:00
2010-08-26 00:14:59 +00:00
return default
2010-08-20 13:47:29 +00:00
2010-08-13 21:08:42 +00:00
def onchange_date ( self , cr , user , ids , date , context = { } ) :
"""
@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
"""
period_pool = self . pool . get ( ' account.period ' )
2010-10-11 12:49:25 +00:00
pids = period_pool . search ( cr , user , [ ( ' date_start ' , ' <= ' , date ) , ( ' date_stop ' , ' >= ' , date ) ] )
2010-08-26 00:14:59 +00:00
if not pids :
return { }
2010-08-13 21:08:42 +00:00
return {
2010-08-26 00:14:59 +00:00
' value ' : {
' period_id ' : pids [ 0 ]
2010-05-27 19:55:40 +00:00
}
2010-08-13 21:02:45 +00:00
}
2010-09-14 08:09:17 +00:00
def onchange_journal ( self , cr , uid , ids , journal_id , line_ids , tax_id , partner_id , context = { } ) :
2010-09-01 13:54:07 +00:00
if not journal_id :
return False
journal_pool = self . pool . get ( ' account.journal ' )
journal = journal_pool . browse ( cr , uid , journal_id )
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 } )
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-07-09 04:54:54 +00:00
def action_cancel_draft ( self , cr , uid , ids , context = { } ) :
2010-08-16 20:14:32 +00:00
wf_service = netsvc . LocalService ( " workflow " )
for voucher_id in ids :
wf_service . trg_create ( uid , ' account.voucher ' , voucher_id , cr )
2010-07-07 15:04:16 +00:00
self . write ( cr , uid , ids , { ' state ' : ' draft ' } )
return True
2010-08-14 12:20:54 +00:00
2010-06-16 11:51:39 +00:00
def cancel_voucher ( self , cr , uid , ids , context = { } ) :
2010-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-07-07 13:05:41 +00:00
for voucher in self . browse ( cr , uid , ids ) :
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-09-14 13:13:29 +00:00
# TODO: may be we can remove this method if not used anyware
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-09-21 10:13:33 +00:00
if not context :
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-09-30 07:06:01 +00:00
bank_st_line_obj = self . pool . get ( ' account.bank.statement.line ' )
2010-10-14 06:09:17 +00:00
tax_obj = self . pool . get ( ' account.tax ' )
2008-10-23 05:53:55 +00:00
for inv in self . browse ( cr , uid , ids ) :
if inv . move_id :
continue
2010-09-29 05:29:52 +00:00
if inv . number :
2010-09-23 17:39:28 +00:00
name = inv . number
2010-09-21 10:13:33 +00:00
elif inv . journal_id . sequence_id :
2010-08-26 00:14:59 +00:00
name = self . pool . get ( ' ir.sequence ' ) . get_id ( cr , uid , inv . journal_id . sequence_id . id )
2010-07-28 18:35:57 +00:00
else :
2010-08-26 00:14:59 +00:00
raise osv . except_osv ( _ ( ' Error ! ' ) , _ ( ' Please define a sequence on the journal ! ' ) )
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 ,
2010-08-26 00:14:59 +00:00
' journal_id ' : inv . journal_id . id ,
2010-10-13 13:46:45 +00:00
' narration ' : inv . narration ,
2010-07-14 05:51:50 +00:00
' date ' : inv . date ,
2010-08-26 00:14:59 +00:00
' ref ' : inv . reference ,
' period_id ' : inv . period_id and inv . 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-30 07:06:01 +00:00
line_bank_ids = bank_st_line_obj . search ( cr , uid , [ ( ' voucher_id ' , ' = ' , inv . id ) ] , context = context )
if line_bank_ids :
bank_st_line_obj . write ( cr , uid , line_bank_ids , {
' move_ids ' : [ ( 4 , move_id , False ) ]
} )
2010-09-29 05:29:52 +00:00
2010-07-07 13:05:41 +00:00
#create the first line manually
2010-09-15 14:33:40 +00:00
company_currency = inv . journal_id . company_id . currency_id . id
2010-08-26 00:14:59 +00:00
debit = 0.0
credit = 0.0
2010-09-29 05:29:52 +00:00
# TODO: is there any other alternative then the voucher type ??
2010-09-15 14:33:40 +00:00
# -for sale, purchase we have but for the payment and receipt we do not have as based on the bank/cash journal we can not know its payment or receipt
2010-08-26 15:04:35 +00:00
if inv . type in ( ' purchase ' , ' payment ' ) :
credit = currency_pool . compute ( cr , uid , inv . currency_id . id , company_currency , inv . amount )
elif inv . type in ( ' sale ' , ' receipt ' ) :
2010-08-26 00:14:59 +00:00
debit = currency_pool . compute ( cr , uid , inv . currency_id . id , company_currency , inv . amount )
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-09-29 05:29:52 +00:00
2010-09-01 16:18:05 +00:00
move_line = {
' name ' : inv . name or ' / ' ,
' debit ' : debit ,
' credit ' : credit ,
' account_id ' : inv . account_id . id ,
2010-10-13 13:46:45 +00:00
' move_id ' : move_id ,
2010-09-01 16:18:05 +00:00
' journal_id ' : inv . journal_id . id ,
' period_id ' : inv . period_id . id ,
' partner_id ' : inv . partner_id . id ,
' currency_id ' : inv . currency_id . id ,
2010-09-15 14:33:40 +00:00
' amount_currency ' : inv . amount ,
2010-09-01 16:18:05 +00:00
' date ' : inv . date ,
' date_maturity ' : inv . date_due
}
2010-09-14 11:19:20 +00:00
if ( debit == 0.0 or credit == 0.0 or debit + credit > 0 ) and ( debit > 0.0 or credit > 0.0 ) :
master_line = move_line_pool . create ( cr , uid , move_line )
2010-08-19 14:01:41 +00:00
2010-08-26 00:14:59 +00:00
rec_list_ids = [ ]
line_total = debit - credit
2010-09-01 16:18:05 +00:00
if inv . type == ' sale ' :
2010-09-15 14:33:40 +00:00
line_total = line_total - currency_pool . compute ( cr , uid , inv . currency_id . id , company_currency , inv . tax_amount )
2010-09-01 16:18:05 +00:00
elif inv . type == ' purchase ' :
2010-09-15 14:33:40 +00:00
line_total = line_total + currency_pool . compute ( cr , uid , inv . currency_id . id , company_currency , inv . tax_amount )
2010-09-01 16:18:05 +00:00
2010-08-26 00:14:59 +00:00
for line in inv . line_ids :
if not line . amount :
continue
amount = currency_pool . compute ( cr , uid , inv . currency_id . id , company_currency , line . amount )
2010-09-29 05:29:52 +00:00
2010-08-19 14:01:41 +00:00
move_line = {
2010-09-01 16:18:05 +00:00
' journal_id ' : inv . journal_id . id ,
' period_id ' : inv . period_id . id ,
2010-08-26 00:14:59 +00:00
' name ' : line . name and line . name or ' / ' ,
' account_id ' : line . account_id . id ,
' move_id ' : move_id ,
' partner_id ' : inv . partner_id . id ,
' currency_id ' : inv . currency_id . id ,
2010-09-14 11:19:20 +00:00
' amount_currency ' : line . amount ,
2010-08-30 13:23:02 +00:00
' analytic_account_id ' : line . account_analytic_id and line . account_analytic_id . id or False ,
2010-09-01 16:18:05 +00:00
' quantity ' : 1 ,
' credit ' : 0.0 ,
' debit ' : 0.0 ,
' date ' : inv . 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-09-29 05:29:52 +00:00
2010-08-26 00:14:59 +00:00
if ( line . type == ' dr ' ) :
line_total + = amount
move_line [ ' debit ' ] = amount
else :
line_total - = amount
move_line [ ' credit ' ] = amount
2010-09-14 11:19:20 +00:00
if inv . tax_id and inv . type in ( ' sale ' , ' purchase ' ) :
2010-09-01 16:18:05 +00:00
move_line . update ( {
' account_tax_id ' : inv . tax_id . id ,
} )
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-08-26 00:14:59 +00:00
master_line = move_line_pool . create ( cr , uid , move_line )
if line . move_line_id . id :
rec_ids = [ master_line , line . move_line_id . id ]
rec_list_ids . append ( rec_ids )
2010-09-22 12:28:48 +00:00
2010-08-26 00:14:59 +00:00
if not self . pool . get ( ' res.currency ' ) . is_zero ( cr , uid , inv . currency_id , line_total ) :
diff = line_total
2010-08-23 11:09:08 +00:00
move_line = {
2010-10-11 12:49:25 +00:00
' name ' : name ,
' account_id ' : False ,
2010-10-13 13:46:45 +00:00
' move_id ' : move_id ,
2010-10-11 12:49:25 +00:00
' partner_id ' : inv . partner_id . id ,
' date ' : inv . date ,
' credit ' : diff > 0 and diff or 0.0 ,
' debit ' : diff < 0 and - diff or 0.0 ,
2010-08-23 11:09:08 +00:00
}
account_id = False
2010-09-14 12:14:31 +00:00
if inv . type in ( ' sale ' , ' receipt ' ) :
# if inv.journal_id.type in ('sale','sale_refund', 'cash', 'bank'):
2010-08-26 00:14:59 +00:00
account_id = inv . partner_id . property_account_receivable . id
else :
account_id = inv . partner_id . property_account_payable . id
move_line [ ' account_id ' ] = account_id
2010-10-06 05:20:59 +00:00
2010-10-08 09:56:14 +00:00
move_line_pool . create ( cr , uid , move_line )
2010-08-26 00:14:59 +00:00
self . write ( cr , uid , [ inv . 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
} )
2010-09-15 14:33:40 +00:00
move_pool . post ( cr , uid , [ move_id ] , context = { } )
2010-08-23 11:09:08 +00:00
for rec_ids in rec_list_ids :
2010-08-25 16:42:19 +00:00
if len ( rec_ids ) > = 2 :
move_line_pool . reconcile_partial ( cr , uid , rec_ids )
2008-10-23 05:53:55 +00:00
return True
2009-05-14 07:06:09 +00:00
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-08-13 21:02:45 +00:00
' state ' : ' draft ' ,
' number ' : False ,
2010-08-30 13:23:02 +00:00
' 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
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 ' )
rs_data = { }
2010-08-25 16:42:19 +00:00
for line in self . browse ( cr , uid , ids ) :
2010-09-15 15:22:43 +00:00
res = { }
company_currency = line . voucher_id . journal_id . company_id . currency_id . id
voucher_currency = line . voucher_id . currency_id . id
2010-08-26 00:14:59 +00:00
move_line = line . move_line_id or False
2010-09-29 05:29:52 +00:00
2010-08-26 00:14:59 +00:00
if not move_line :
2010-09-15 15:22:43 +00:00
res [ ' amount_original ' ] = 0.0
res [ ' amount_unreconciled ' ] = 0.0
2010-09-29 05:29:52 +00:00
2010-08-26 00:14:59 +00:00
elif move_line and move_line . credit > 0 :
2010-09-15 15:22:43 +00:00
res [ ' amount_original ' ] = currency_pool . compute ( cr , uid , company_currency , voucher_currency , move_line . credit )
2010-08-26 00:14:59 +00:00
else :
2010-09-15 15:22:43 +00:00
res [ ' amount_original ' ] = currency_pool . compute ( cr , uid , company_currency , voucher_currency , move_line . debit )
2010-09-29 05:29:52 +00:00
2010-09-15 15:22:43 +00:00
if move_line :
res [ ' amount_unreconciled ' ] = currency_pool . compute ( cr , uid , company_currency , voucher_currency , move_line . amount_unreconciled )
rs_data [ line . id ] = res
return rs_data
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 ' ) ,
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 ) ,
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 ' ) ,
2010-09-15 14:33:40 +00:00
' amount ' : fields . float ( ' Amount ' , digits = ( 14 , 2 ) ) ,
2010-08-25 16:42:19 +00:00
' type ' : fields . selection ( [ ( ' dr ' , ' Debit ' ) , ( ' cr ' , ' Credit ' ) ] , ' Cr/Dr ' ) ,
2010-08-13 21:02:45 +00:00
' account_analytic_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic Account ' ) ,
2010-08-19 07:57:59 +00:00
' move_line_id ' : fields . many2one ( ' account.move.line ' , ' Journal Item ' ) ,
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 ) ,
2010-09-15 15:22:43 +00:00
' amount_original ' : fields . function ( _compute_balance , method = True , multi = ' dc ' , type = ' float ' , string = ' Originial Amount ' , store = True ) ,
' amount_unreconciled ' : fields . function ( _compute_balance , method = True , multi = ' dc ' , type = ' float ' , string = ' Open Balance ' , store = True ) ,
2010-09-16 14:42:32 +00:00
' company_id ' : fields . related ( ' voucher_id ' , ' company_id ' , relation = ' res.company ' , string = ' Company ' , store = True ) ,
2008-10-23 05:53:55 +00:00
}
_defaults = {
2010-08-26 00:14:59 +00:00
' name ' : lambda * a : ' '
2008-10-23 05:53:55 +00:00
}
2009-12-24 09:39:00 +00:00
2010-08-20 18:57:46 +00:00
def onchange_move_line_id ( self , cr , user , ids , move_line_id , context = { } ) :
"""
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
amount = move_line . credit
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-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
journal = journal_pool . browse ( cr , user , journal_id )
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
if ( not account_id ) and ' account_id ' in fields_list :
2010-08-20 18:57:46 +00:00
raise osv . except_osv ( _ ( ' Invalid Error ! ' ) , _ ( ' Please change partner and try again ! ' ) )
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 ) :
for st in self . browse ( cr , uid , ids , context ) :
voucher_ids = [ ]
for line in st . line_ids :
if line . voucher_id :
voucher_ids . append ( line . voucher_id . id )
self . pool . get ( ' account.voucher ' ) . cancel_voucher ( cr , uid , voucher_ids , context )
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-09-21 10:13:33 +00:00
st_line = self . pool . get ( ' account.bank.statement.line ' ) . 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-09-29 05:29:52 +00:00
return self . pool . get ( ' account.move.line ' ) . write ( cr , uid , [ x . id for x in st_line . voucher_id . 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
_columns = {
' amount_reconciled ' : fields . function ( _amount_reconciled ,
string = ' Amount reconciled ' , method = True , type = ' float ' ) ,
' voucher_id ' : fields . many2one ( ' account.voucher ' , ' Payment ' ) ,
}
2010-09-22 12:28:48 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
statement_line = self . browse ( cr , uid , ids , context )
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-09-22 12:28:48 +00:00
self . pool . get ( ' account.voucher ' ) . unlink ( cr , uid , unlink_ids , context = context )
return super ( account_bank_statement_line , self ) . unlink ( cr , uid , ids , context = context )
2010-10-11 12:49:25 +00:00
account_bank_statement_line ( )