~mga/openobject-addons/account_india

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# -*- encoding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
import pooler
import wizard
import base64 
from osv import osv
import time
import pooler
import mx.DateTime
from mx.DateTime import RelativeDateTime, DateTime

def _bank_get(self, cr, uid, context={}):
	pool = pooler.get_pool(cr.dbname)
	partner_id = pool.get('res.users').browse(cr,uid,[uid])[0].company_id.partner_id

	obj = pool.get('res.partner.bank')
	ids = obj.search(cr, uid, [('partner_id','=',partner_id.id)])
	res = obj.read(cr, uid, ids, ['active', 'name'], context)
	res = [(r['id'], r['name']) for r in res]
	return res 


check_form = """<?xml version="1.0"?>
<form string="DTA file creation">
<separator colspan="4" string="DTA Details :" />
	<field name="bank"/>
	<field name="journal"/>
	<field name="dta_line_ids" nolabel="1"  colspan="4" />
</form>
"""

check_fields = {
	'dta_line_ids' : {
		'string':'DTA lines',
		'type':'one2many',
		'relation':'account.dta.line',
		},
	'bank' : {
		'string':'Bank Account',
		'type':'selection',
		'selection':_bank_get,
		'required': True,
	},

	'journal' : {
		'string':'Journal',
		'type':'many2one',
		'relation':'account.journal',
		'domain':"[('type','=','cash')]",
		'required': True,
		'help':'The journal used for the bank statement',
	},
}


res_form = """<?xml version="1.0"?>
<form string="DTA file creation - Results">
<separator colspan="4" string="Clic on 'Save as' to save the DTA file :" />
	<field name="dta"/>
	<separator string="Logs" colspan="4"/>
	<field name="note" colspan="4" nolabel="1"/>
</form>
"""

res_fields = {
	'dta' : {
		'string':'DTA File',
		'type':'binary',
		'required':True,
		'readonly':True,
	},
	'note' : {'string':'Log','type':'text'}
}


def _get_bank(self,cr,uid,data,context):
	pool = pooler.get_pool(cr.dbname)
	user = pool.get('res.users').browse(cr,uid,[uid])[0]
	company= user.company_id
	
	if company.partner_id.bank_ids:
		bank = company.partner_id.bank_ids[0]
		return {'bank':bank.bank_name,'bank_code':bank.bank_code or ''} # 'city':'',

	return {}


def _get_dta_lines(self,cr,uid,data,context):
	pool = pooler.get_pool(cr.dbname)

	res= {}

	user = pool.get('res.users').browse(cr,uid,[uid])[0]
	company= user.company_id
	
	if company.partner_id.bank_ids:
		bank = company.partner_id.bank_ids[0]
		res.update({'bank':bank.bank_name,'bank_code':bank.bank_code or ''}) # 'city':'',


	dta_line_obj = pool.get('account.dta.line')
	lines=[]

	id_dta= pool.get('account.dta').create(cr,uid,{
		'date':time.strftime('%Y-%m-%d'),
		'user_id':uid,
		})


	for i in pool.get('account.invoice').browse(cr,uid,data['ids']):
		if i.dta_state in ['none','paid'] or i.state in ['draft','cancel','paid']:
			continue

		cash_disc_date=""
		discount=""
		if i.payment_term :
			disc_list= pool.get('account.payment.term').get_discounts(cr,uid,i.payment_term.id, i.date_invoice)

			for (cash_disc_date,discount) in disc_list:
				if cash_disc_date >= time.strftime('%Y-%m-%d'):
					break

		
		lines.append(dta_line_obj.create(cr,uid,{
			'name': i.id,
			'partner_id': i.partner_id.id,
			'due_date': i.date_due,
			'invoice_date': i.date_invoice,
			'cashdisc_date': cash_disc_date and cash_disc_date or None,
			'amount_to_pay': discount and i.amount_total*(1-discount) or i.amount_total,
			'amount_invoice': i.amount_total,
			'amount_cashdisc': discount and i.amount_total*(1-discount),
			'dta_id': id_dta,
			}))
		
	res.update({'dta_line_ids': lines,'dta_id': id_dta})
	return res



trans=[(u'é','e'),
	   (u'è','e'),
	   (u'à','a'),
	   (u'ê','e'),
	   (u'î','i'),
	   (u'ï','i'),
	   (u'â','a'),
	   (u'ä','a')]
def tr(s):
	s= s.decode('utf-8')

	for k in trans:
		s = s.replace(k[0],k[1])

	try:
		res= s.encode('ascii','replace')
	except:
		res = s
	return res
		
class record:
	def __init__(self,global_context_dict):

		for i in global_context_dict:
			global_context_dict[i]= global_context_dict[i] and tr(global_context_dict[i])
		self.fields = []
		self.global_values = global_context_dict
		self.pre={'padding':'','seg_num1':'01','seg_num2':'02',
				  'seg_num3':'03','seg_num4':'04','seg_num5':'05',
				   'flag':'0', 'zero5':'00000'
						   }
		self.post={'date_value_hdr':'000000','type_paiement':'0'}
		self.init_local_context()

	def init_local_context(self):
		"""
		Must instanciate a fields list, field = (name,size)
		and update a local_values dict.
		"""
		raise Exception, "not implemented"

	def generate(self):
		res=''
		for field in self.fields :
			if self.pre.has_key(field[0]):
				value = self.pre[field[0]]
			elif self.global_values.has_key(field[0]):
				value = self.global_values[field[0]]
			elif self.post.has_key(field[0]):
				value = self.post[field[0]]
			else :
				pass
				#raise Exception(field[0]+' not found !')

			try:
				res = res + c_ljust(value, field[1])
			except :
				pass
			
		return res

class record_gt826(record):
	# -> bvr
	def init_local_context(self):
		self.fields=[
			('seg_num1',2),
			#header
			('date_value_hdr',6),('partner_bank_clearing',12),('zero5',5),('creation_date',6),
			('comp_bank_clearing',7), ('uid',5), 
			('sequence',5),
			('genre_trans',3),
			('type_paiement',1),('flag',1),
			#seg1
			('comp_dta',5),('invoice_number',11),('comp_bank_iban',24),('date_value',6),
			('invoice_currency',3),('amount_to_pay',12),('padding',14),
			#seg2
			('seg_num2',2),('comp_name',20),('comp_street',20),('comp_zip',10),
			('comp_city',10),('comp_country',20),('padding',46),
			#seg3
			('seg_num3',2),('partner_bvr',12),#numero d'adherent bvr
			('partner_name',20),('partner_street',20),('partner_zip',10),
			('partner_city',10),('partner_country',20),
			('invoice_bvr_num',27),#communication structuree
			('padding',2),#cle de controle
			('padding',5)
			]

		self.pre.update({'date_value_hdr': self.global_values['date_value'],
						 'date_value':'',
						 'partner_bank_clearing':'','partner_cpt_benef':'',
						 'genre_trans':'826',
						 'conv_cours':'', 'option_id_bank':'D',
						 'partner_bvr' : '/C/'+ self.global_values['partner_bvr'],
						 'ref2':'','ref3':'', 
						 'format':'0'})

class record_gt827(record):
	# -> interne suisse
	def init_local_context(self):
		self.fields=[
			('seg_num1',2),
			#header
			('date_value_hdr',6),('partner_bank_clearing',12),('zero5',5),('creation_date',6),
			('comp_bank_clearing',7), ('uid',5), 
			('sequence',5),
			('genre_trans',3),
			('type_paiement',1),('flag',1),
			#seg1
			('comp_dta',5),('invoice_number',11),('comp_bank_iban',24),('date_value',6),
			('invoice_currency',3),('amount_to_pay',12),('padding',14),
			#seg2
			('seg_num2',2),('comp_name',20),('comp_street',20),('comp_zip',10), 
			('comp_city',10),('comp_country',20),('padding',46),
			#seg3
			('seg_num3',2),('partner_bank_number',30),
			('partner_name',24),('partner_street',24),('partner_zip',12),
			('partner_city',12),('partner_country',24),
			#seg4
			('seg_num4',2),('partner_comment',112),('padding',14),
			#seg5
			#('padding',128)
			]

		self.pre.update({'date_value_hdr': self.global_values['date_value'],
						 'date_value':'',						 
						 'partner_cpt_benef':'',
						 'type_paiement':'0', 'genre_trans':'827',
						 'conv_cours':'', 'option_id_bank':'D',
						 'ref2':'','ref3':'', 
						 'format':'0'})




class record_gt836(record):
	# -> iban
	def init_local_context(self):
		self.fields=[
			('seg_num1',2),
			#header
			('date_value_hdr',6),('partner_bank_clearing',12),('zero5',5),('creation_date',6),
			('comp_bank_clearing',7), ('uid',5), 
			('sequence',5),
			('genre_trans',3),
			('type_paiement',1),('flag',1),
			#seg1
			('comp_dta',5),('invoice_number',11),('comp_bank_iban',24),('date_value',6),
			('invoice_currency',3),('amount_to_pay',15),('padding',11),
			#seg2
			('seg_num2',2),('conv_cours',12),('comp_name',35),('comp_street',35),('comp_country',3),('comp_zip',10),
			('comp_city',22),('padding',9),
			#seg3
			('seg_num3',2),('option_id_bank',1),('partner_bank_ident',70),
			('partner_bank_iban',34),('padding',21),
			#seg4
			('seg_num4',2),('partner_name',35),('partner_street',35),('partner_country',3),('partner_zip',10),('partner_city',22),
			('padding',21),
			#seg5
			('seg_num5',2),('option_motif',1),('invoice_reference',105),('format',1),('padding',19)
		]

		self.pre.update( {
			'partner_bank_clearing':'','partner_cpt_benef':'',
			'type_paiement':'0','genre_trans':'836',
			'conv_cours':'',
			'invoice_reference': self.global_values['invoice_reference'] or self.global_values['partner_comment'],
			'ref2':'','ref3':'',
			'format':'0'
		})
		self.post.update({'comp_dta':'','option_motif':'U'})


class record_gt890(record):
	# -> total
	def init_local_context(self):
		self.fields=[
			('seg_num1',2),
			#header
			('date_value_hdr',6),('partner_bank_clearing',12),('zero5',5),('creation_date',6),
			('comp_bank_clearing',7), ('uid',5), 
			('sequence',5),
			('genre_trans',3),
			('type_paiement',1),('flag',1),
			#total
			('amount_total',16),('padding',59)]

		self.pre.update({'partner_bank_clearing':'','partner_cpt_benef':'',
							  'company_bank_clearing':'','genre_trans':'890'})
			
def c_ljust(s, size):
	"""
	check before calling ljust
	"""
	s= s or ''
	if len(s) > size:
		s= s[:size]
	s = s.decode('utf-8').encode('latin1','replace').ljust(size)
	return s



def _create_dta(self,cr,uid,data,context):

	# cree des gt836
	v={}
	v['uid'] = str(uid)
	v['creation_date']= time.strftime('%y%m%d')
	log=''
	dta=''

	pool = pooler.get_pool(cr.dbname)
	bank= pool.get('res.partner.bank').browse(cr,uid,[data['form']['bank']])[0]

	if not bank:
		return {'note':'No bank account for the company.'}
	

 	v['comp_bank_name']= bank.name or False
 	v['comp_bank_clearing'] = bank.bank_clearing or False # clearing 
	v['comp_bank_code'] = bank.bank_code or False # swift or BIC

 	if not v['comp_bank_clearing']:
 		return {'note':'You must provide a Clearing Number for your bank account.'}
	
	user = pool.get('res.users').browse(cr,uid,[uid])[0]
	company= user.company_id
	co_addr= company.partner_id.address[0]
	v['comp_country'] = co_addr.country_id and co_addr.country_id.name or ''
	v['comp_street'] = co_addr.street or ''
	v['comp_zip'] = co_addr.zip
	v['comp_city'] = co_addr.city
	v['comp_name'] = co_addr.name
	
	v['comp_dta'] = ''#FIXME


	v['comp_bank_number'] = bank.number or ''

	v['comp_bank_iban'] = bank.iban or ''
	if not v['comp_bank_iban'] : 
		return {'note':'No iban number for the company bank account.'}

	
	inv_obj = pool.get('account.invoice')
	dta_line_obj = pool.get('account.dta.line')

	seq= 1
	amount_tot = 0
	th_amount_tot= 0
	dta_id=data['form']['dta_id']

	if not dta_id :
		return {'note':'No dta line'}

	# write the bank account for the dta object
	pool.get('account.dta').write(cr,uid,[dta_id],{'bank':data['form']['bank']})

	dta_line_ids= []

	for line in data['form']['dta_line_ids']:
		if  line[1]!=0 and line[2] and line[2]['partner_id']:
			dta_line_ids.append(line[1])
			th_amount_tot += line[2]['amount_to_pay']
			dta_line_obj.write(cr, uid, [line[1]] , line[2] )


	# creation of a bank statement : TODO ajouter le partner 
 	bk_st_id = pool.get('account.bank.statement').create(cr,uid,{
 		'journal_id':data['form']['journal'],
 		'balance_start':0,
 		'balance_end_real':0, 
 		'state':'draft',
 		})

	for dtal in dta_line_obj.browse(cr,uid,dta_line_ids):

		i = dtal.name #dta_line.name is the invoice id
		invoice_number = i.number or '??'
		if not i.partner_bank_id:
			log= log +'\nNo partner bank defined. (invoice '+ invoice_number +')' 
			continue
				
		
		v['sequence'] = str(seq).rjust(5).replace(' ','0')
		v['amount_to_pay']= str(dtal.amount_to_pay).replace('.',',')
		v['invoice_number'] = invoice_number or ''
		v['invoice_currency'] = i.currency_id.code or ''
		if not v['invoice_currency'] :
			log= log +'\nInvoice currency code undefined. (invoice '+ invoice_number +')' 
			continue

		
		v['partner_bank_name'] =  i.partner_bank_id.bank_name or False
		v['partner_bank_clearing'] =  i.partner_bank_id.bank_clearing or False
		if not v['partner_bank_name'] :
			log= log +'\nPartner bank account not well defined. (invoice '+ invoice_number +')' 
			continue

		v['partner_bank_iban']=  i.partner_bank_id.iban or False
		v['partner_bank_number']=  i.partner_bank_id.number  and i.partner_bank_id.number.replace('.','').replace('-','') or  False
		
		v['partner_bvr']= i.partner_bank_id.bvr_number or ''
		
		if v['partner_bvr']:
			v['partner_bvr']= v['partner_bvr'].replace('-','')
			if len(v['partner_bvr']) < 9:
				v['partner_bvr']= v['partner_bvr'][:2]+ '0'*(9-len(v['partner_bvr'])) +v['partner_bvr'][2:]



		v['partner_bank_city']= i.partner_bank_id.city or False
		v['partner_bank_street']= i.partner_bank_id.street or ''
		v['partner_bank_zip']= i.partner_bank_id.zip or ''
		v['partner_bank_country']= i.partner_bank_id.country_id  and i.partner_bank_id.country_id.name or ''
		v['partner_bank_code']= i.partner_bank_id.bank_code or False
		v['invoice_reference']= i.reference
		v['invoice_bvr_num']= i.bvr_ref_num
		if v['invoice_bvr_num']:
			v['invoice_bvr_num'] = v['invoice_bvr_num'].replace(' ', '').rjust(27).replace(' ','0')
			
		v['partner_comment']= i.partner_comment
		
		v['partner_name'] = i.partner_id and i.partner_id.name or ''
		if i.partner_id and i.partner_id.address and i.partner_id.address[0]:
			v['partner_street'] = i.partner_id.address[0].street
			v['partner_city']= i.partner_id.address[0].city
			v['partner_zip']= i.partner_id.address[0].zip
			# If iban => country=country code for space reason
			elec_pay = i.partner_bank_id.type_id.elec_pay
			if elec_pay == 'iban':
				v['partner_country']= i.partner_id.address[0].country_id and i.partner_id.address[0].country_id.code+'-' or ''
			else:
				v['partner_country']= i.partner_id.address[0].country_id and i.partner_id.address[0].country_id.name or ''
		else:
			v['partner_street'] =''
			v['partner_city']= ''
			v['partner_zip']= ''
			v['partner_country']= ''
			log= log +'\nNo address for the invoice partner. (invoice '+ invoice_number+')' 

		
		date_value = dtal.cashdisc_date or dtal.due_date
		if date_value :
			date_value = mx.DateTime.strptime( date_value,'%Y-%m-%d') or  mx.DateTime.now()
			v['date_value'] = date_value.strftime("%y%m%d")
		else:
			v['date_value'] = "000000"

		# si compte iban -> iban (836)
		# si payment structure  -> bvr (826)
		# si non -> (827) 

		elec_pay = i.partner_bank_id.type_id.elec_pay
		if not elec_pay :
			log= log +'\nBank type does not support DTA. (invoice '+ invoice_number +')' 
			continue

		if elec_pay == 'iban':
			# If iban => country=country code for space reason
			v['comp_country'] = co_addr.country_id and co_addr.country_id.code+'-' or ''
			record_type = record_gt836
			if not v['partner_bank_iban']:
				log= log +'\nNo iban number for the partner bank. (invoice '+ invoice_number +')' 
				continue

			if v['partner_bank_code'] :
				v['option_id_bank']= 'A'
				v['partner_bank_ident']= v['partner_bank_code']
			elif v['partner_bank_city']:
				#
				# added by fabien
				#
				log= log +'\nCode IBAN or Swift code doesn t exist. (invoice '+ invoice_number +')' 
				continue

				v['option_id_bank']= 'D'
				v['partner_bank_ident']= v['partner_bank_name'] +' '+v['partner_bank_street']\
					+' '+v['partner_bank_zip']+' '+v['partner_bank_city']\
					+' '+v['partner_bank_country']
			else:
				log= log +'\nYou must provide the bank city or the bank code. (invoice '+ invoice_number +')' 
				continue

			
		elif elec_pay == 'bvrbank' or elec_pay == 'bvrpost':
			if not v['invoice_bvr_num']:
				log= log +'\nYou must provide an Bvr reference number. (invoice '+ invoice_number +')' 
				continue
			if not v['partner_bvr']:
				log= log +'\nYou must provide a BVR reference number in the partner bank. (invoice '+ invoice_number +')' 
				continue
			record_type = record_gt826
			
			
			
		elif elec_pay == 'bvbank':
			if not v['partner_bank_number'] :
				if  v['partner_bank_iban'] :
					v['partner_bank_number']= v['partner_bank_iban'] 
				else:
					log= log +'\nYou must provide a bank number in the partner bank. (invoice '+ invoice_number +')' 
					continue

			if not  v['partner_bank_clearing']:
				log= log +'\nPartner bank must have a Clearing Number for a BV Bank operation. (invoice '+ invoice_number +')' 
				continue
			v['partner_bank_number'] = '/C/'+v['partner_bank_number']
 			record_type = record_gt827

			
 		elif elec_pay == 'bvpost':
			if not v['partner_bank_number']:
				log= log +'\nYou must provide a post number in the partner bank. (invoice '+ invoice_number +')' 
				continue
			v['partner_bank_clearing']= ''
			v['partner_bank_number'] = '/C/'+v['partner_bank_number']
 			record_type = record_gt827
			
		else:
			log= log +'\nBank type not supported. (invoice '+ invoice_number +')' 
			continue


		try:
			dta_line = record_type(v).generate()
		except Exception,e :
			log= log +'\nERROR:'+ str(e)+'(invoice '+ invoice_number+')' 
			dta_line_obj.write(cr,uid,[dtal.id],{'state':'cancel'})			
			raise
			continue

		#logging
		log = log + "Invoice : %s, Amount paid : %d %s, Value date : %s, State : Paid."%\
			  (invoice_number,dtal.amount_to_pay,v['invoice_currency'],date_value and date_value.strftime("%Y-%m-%d") or 'Empty date')

		reconcile_id = pool.get('account.bank.statement.reconcile').create(cr, uid, {
			'name': time.strftime('%Y-%m-%d'),
			'line_ids': [(6, 0, i.move_line_id_payment_get(cr, uid, [i.id]))],
			})

		pool.get('account.bank.statement.line').create(cr,uid,{
			'name':i.number,
			'date': time.strftime('%Y-%m-%d'),
			'amount': -dtal.amount_to_pay,
			'type':{'out_invoice':'customer','in_invoice':'supplier','out_refund':'customer','in_refund':'supplier'}[i.type],
			'partner_id':i.partner_id.id,
			'account_id':i.account_id.id,
			'statement_id': bk_st_id,
			'reconcile_id': reconcile_id,
			})

		dta = dta + dta_line
		amount_tot += dtal.amount_to_pay
		inv_obj.write(cr,uid,[i.id],{'dta_state':'paid'})
		dta_line_obj.write(cr,uid,[dtal.id],{'state':'done'})
		seq += 1
		
	# bank statement updated with the total amount :
	pool.get('account.bank.statement').write(cr,uid,[bk_st_id],{'balance_end_real': -amount_tot})
	
	# segment total
	v['amount_total'] = str(amount_tot).replace('.',',')
	v['sequence'] = str(seq).rjust(5).replace(' ','0')	
	try:
		if dta :
			dta = dta + record_gt890(v).generate()
	except Exception,e :
		log= log +'\n'+ str(e) + 'CORRUPTED FILE !\n'
		raise
		

	log = log + "\n--\nSummary :\nTotal amount paid : %.2f\nTotal amount expected : %.2f"%(amount_tot,th_amount_tot) 
	pool.get('account.dta').write(cr,uid,[dta_id],{'note':log,'name':base64.encodestring(dta or "")})
	
	return {'note':log, 'dta': base64.encodestring(dta)}



class wizard_dta_create(wizard.interface):
	states = {
		
		'init':{
		'actions' : [_get_dta_lines],
		'result' : {'type' : 'form',
				    'arch' : check_form,
				    'fields' : check_fields,
				    'state' : [('end', 'Cancel'),('creation', 'Yes') ]}
		},

		'creation' : {
			'actions' : [_create_dta],
			'result' : {'type' : 'form',
						'arch' : res_form,
						'fields' : res_fields,
						'state' : [('end', 'Quit') ]}
		},

	}

wizard_dta_create('account.dta_create')