~openerp-accountedge/openerp-accountedge/github-7.0

« back to all changes in this revision

Viewing changes to hr_expense_accountedge/tools/import_expenses.py

  • Committer: Maxime Chambreuil
  • Date: 2013-08-08 14:43:27 UTC
  • Revision ID: git-v1:e0553b9603db9f77540864025849829feaaab7c1
[IMP] Cleanup and copy the header once

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) 2010 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
import xmlrpclib
 
23
import base64
 
24
import getpass
 
25
from datetime import datetime
 
26
 
 
27
def main():
 
28
    username    = 'openerp_user'
 
29
    dbname      = ''
 
30
    server_url  = 'http://localhost:8069'
 
31
    csv_path    = 'hr_expense_accountedge.tsv'
 
32
 
 
33
    pwd = getpass.getpass(prompt="Please enter the password of %s: " % username)
 
34
    
 
35
    # Get the uid
 
36
    sock_common = xmlrpclib.ServerProxy('%s/xmlrpc/common' % server_url)
 
37
    uid = sock_common.login(dbname, username, pwd)
 
38
 
 
39
    if not uid:
 
40
        print "Connection error. Please check the username, password and server url."
 
41
        goodbye = raw_input("Type 'enter' to quit...")
 
42
        return 1
 
43
 
 
44
    # Replace localhost with the address of the server 
 
45
    sock = xmlrpclib.ServerProxy('%s/xmlrpc/object' % server_url)
 
46
 
 
47
    # Search for exported expense notes
 
48
    args = [('state', '=', 'exported')]
 
49
    expense_ids = sock.execute(dbname, uid, pwd, 'hr.expense.expense', 'search', args)
 
50
 
 
51
    print "There are %d expense sheets to import" % len(expense_ids)
 
52
 
 
53
    if not expense_ids:
 
54
        goodbye = raw_input("Type 'enter' to quit...")
 
55
        return 1
 
56
   
 
57
    # Outpout file for AccountEdge
 
58
    final_csv = open(csv_path, 'w')
 
59
    
 
60
    num_expense = 0
 
61
 
 
62
    # For each exported expense note, search for the tsv attachment
 
63
    for expense_id in expense_ids:
 
64
 
 
65
        args    = [('res_model','=','hr.expense.expense'),('res_id', '=', expense_id)]
 
66
        csv_ids = sock.execute(dbname, uid, pwd, 'ir.attachment', 'search', args)
 
67
        
 
68
        fields  = ['name', 'datas']
 
69
        csv_obj = sock.execute(dbname, uid, pwd, 'ir.attachment', 'read', csv_ids, fields)
 
70
        
 
71
        latest_csv = None
 
72
        latest_date = datetime(2000, 1, 1, 0, 0, 0)
 
73
 
 
74
        # Find the latest csv
 
75
        for csv in csv_obj:
 
76
 
 
77
            format = 'export_%Y%m%d_%H%M%S.tsv'
 
78
            date_created = datetime.strptime(csv["name"], format)
 
79
            
 
80
            if date_created > latest_date:
 
81
                latest_date = date_created
 
82
                latest_csv = csv
 
83
 
 
84
        # Copy the lines to the new summary file
 
85
        if latest_csv:
 
86
            content     = base64.b64decode(csv['datas'])
 
87
            content     = content.split("\r\n")
 
88
 
 
89
            
 
90
            for num_line in range(len(content)):
 
91
                if (num_line == 1 and num_expense == 0) or num_line > 1:
 
92
                    final_csv.write(content[num_line])
 
93
                    final_csv.write("\r\n")
 
94
 
 
95
 
 
96
            num_expense = num_expense + 1
 
97
 
 
98
 
 
99
        # Mark the expenses as imported
 
100
        values = {'state': 'exported'}
 
101
        result = sock.execute(dbname, uid, pwd, 'hr.expense.expense', 'write', expense_ids, values)
 
102
 
 
103
    final_csv.close()
 
104
 
 
105
    goodbye = raw_input("Type 'enter' to quit...")
 
106
 
 
107
if __name__ == "__main__":
 
108
    main()
 
109