~vauxoo/addons-vauxoo/company_description_dev_luis

« back to all changes in this revision

Viewing changes to account_bank_statement_vauxoo/model/account_bank_statement.py

  • Committer: Nhomar Hernandez
  • Date: 2012-07-28 06:27:13 UTC
  • mfrom: (355.2.2 addons-vauxoo)
  • Revision ID: nhomar@gmail.com-20120728062713-rfk2bse8guqt9sjh
[MERGE] From Working directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution    
 
5
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    d$
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from osv import osv
 
24
from osv import fields
 
25
import decimal_precision as dp
 
26
import netsvc
 
27
from tools.translate import _
 
28
#Excel Stuff
 
29
import xlrd
 
30
import xlwt
 
31
import base64
 
32
from tempfile import NamedTemporaryFile
 
33
from datetime import date, datetime, timedelta
 
34
 
 
35
 
 
36
class account_bank_statement(osv.osv):
 
37
    _inherit = 'account.bank.statement'
 
38
    _columns = {
 
39
        'bs_line_ids':fields.one2many('bank.statement.imported.lines', 'bank_statement_id', 'Statement', required=False),
 
40
    }
 
41
 
 
42
    def file_verify_cr(self, cr, uid, ids, context={}):
 
43
        '''
 
44
        Verification of format Files.
 
45
        For CR Banco Nacional
 
46
        #Oficina        FechaMovimiento NumDocumento    Debito  Credito Descripcion
 
47
        '''
 
48
        sheet=context.get('xls_sheet')
 
49
        if 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':
 
56
                return True
 
57
        return False
 
58
 
 
59
    def xlrd_to_date(self,cv):
 
60
        from1900to1970 = datetime(1970,1,1) - datetime(1900,1,1) + timedelta(days=2)
 
61
        print cv
 
62
        value = date.fromtimestamp( int(cv) * 86400) - from1900to1970
 
63
        print value
 
64
        return value
 
65
 
 
66
    def write_file(self, cr, uid, ids, context={}):
 
67
        sheet=context.get('xls_sheet')
 
68
        for i in range(sheet.nrows - 1):
 
69
            if i:
 
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),
 
76
                }
 
77
                
 
78
                self.write(cr,uid,ids,{'bs_line_ids':[(0,0,l)]}, context=context)
 
79
        return True
 
80
 
 
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]
 
84
        return True
 
85
 
 
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')
 
95
            if checkfilename:
 
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)
 
103
            else:
 
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)
 
107
        return True
 
108
 
 
109
account_bank_statement()
 
110
 
 
111
class bank_statement_imported_lines(osv.osv):
 
112
    """
 
113
    OpenERP Model : ClassName
 
114
    """
 
115
    
 
116
    _name = 'bank.statement.imported.lines'
 
117
 
 
118
    _description = 'Imported lines for banks files'
 
119
    
 
120
    _columns = {
 
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),
 
128
    }
 
129
 
 
130
    _defaults = {
 
131
        'name': lambda *a: None,
 
132
    }
 
133
bank_statement_imported_lines()