1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
24
from osv import fields
25
import decimal_precision as dp
27
from tools.translate import _
32
from tempfile import NamedTemporaryFile
33
from datetime import date, datetime, timedelta
36
class account_bank_statement(osv.osv):
37
_inherit = 'account.bank.statement'
39
'bs_line_ids':fields.one2many('bank.statement.imported.lines', 'bank_statement_id', 'Statement', required=False),
42
def file_verify_cr(self, cr, uid, ids, context={}):
44
Verification of format Files.
46
#Oficina FechaMovimiento NumDocumento Debito Credito Descripcion
48
sheet=context.get('xls_sheet')
50
if sheet.cell(0,0).value=='Oficina' and \
51
sheet.cell(0,1).value=='FechaMovimiento' and \
52
sheet.cell(0,2).value=='NumDocumento' and \
53
sheet.cell(0,3).value=='Debito' and \
54
sheet.cell(0,4).value=='Credito' and \
55
sheet.cell(0,5).value=='Descripcion':
59
def xlrd_to_date(self,cv):
60
from1900to1970 = datetime(1970,1,1) - datetime(1900,1,1) + timedelta(days=2)
62
value = date.fromtimestamp( int(cv) * 86400) - from1900to1970
66
def write_file(self, cr, uid, ids, context={}):
67
sheet=context.get('xls_sheet')
68
for i in range(sheet.nrows - 1):
70
l = {'office':int(sheet.cell_value(i,0)),
71
'date':self.xlrd_to_date(sheet.cell_value(i,1)),
72
'numdocument':sheet.cell_value(i,2),
73
'debit':sheet.cell_value(i,3) and float(sheet.cell_value(i,3).replace(',','')),
74
'credit':sheet.cell_value(i,4) and float(sheet.cell_value(i,4).replace(',','')),
75
'name':sheet.cell_value(i,5),
78
self.write(cr,uid,ids,{'bs_line_ids':[(0,0,l)]}, context=context)
81
def delete_lines_file(self, cr, uid, ids, context = None):
82
bs=self.browse(cr,uid,ids,context=context)[0]
83
bs.bs_line_ids and [self.write(cr, uid, ids, {'bs_line_ids':[(2,i.id)]}) for i in bs.bs_line_ids]
86
def read_file(self, cr, uid, ids, context=None):
87
att_obj=self.pool.get('ir.attachment')
88
file_xls_ids=att_obj.search(cr,uid,[('res_model','=','account.bank.statement'),('res_id','in',ids)])
89
if len(file_xls_ids)<>1:
90
raise osv.except_osv(_('Warning'), _('I found quatity of attachments <> 1 ! \
91
Please Attach JUST One XLS file to this bank statement.'))
92
file_xls_brw=att_obj.browse(cr, uid, file_xls_ids, context=context)
93
if len(file_xls_ids)==1:
94
checkfilename=file_xls_brw[0].datas_fname and file_xls_brw[0].datas_fname.endswith('.xls')
96
f=NamedTemporaryFile(delete=False)
97
f.write(base64.b64decode(file_xls_brw[0].datas))
98
doc=xlrd.open_workbook(f.name)
99
sheet = doc.sheet_by_index(0)
100
context.update({'xls_sheet':sheet})
101
if self.file_verify_cr(cr, uid, ids, context=context):
102
self.write_file(cr, uid, ids, context=context)
104
raise osv.except_osv(_('Warning'), _('File Must be an XLS file ! \
105
Please verify save as correctly in excel your exported file from bank statement'))
106
file_xls_brw=att_obj.browse(cr,uid,file_xls_ids, context=context)
109
account_bank_statement()
111
class bank_statement_imported_lines(osv.osv):
113
OpenERP Model : ClassName
116
_name = 'bank.statement.imported.lines'
118
_description = 'Imported lines for banks files'
121
'name':fields.char('Description', size=255, required=True, readonly=False),
122
'date': fields.date('Date', required=True),
123
'numdocument':fields.char('Num Document', size=64, required=True, readonly=False),
124
'debit': fields.float('Debit', digits_compute=dp.get_precision('Account'), required=True),
125
'credit': fields.float('Credit', digits_compute=dp.get_precision('Account'), required=True),
126
'office':fields.char('Office', size=16, required=False, readonly=False),
127
'bank_statement_id':fields.many2one('account.bank.statement', 'Bank Statement', required=True),
131
'name': lambda *a: None,
133
bank_statement_imported_lines()