himport 4.84 KB
Newer Older
plb's avatar
plb committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import getopt
import locale
import logging
import os
import shutil
import sys
import csv
import datetime
import codecs
import getpass

from himports import settings
from himports.hledger import *
18
from himports.dolibarrAlchemyHledger import *
plb's avatar
plb committed
19
20
21
22
23
24

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('hreport')

sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)

25

plb's avatar
plb committed
26
27
def process_args(argv):
    options = {}
28
    usage = u'himport -a -p -d -v'
plb's avatar
plb committed
29
30
    try:
        opts, args = getopt.getopt(
31
32
33
            argv, "hp:P:adv",
            ["mysql-password=", "mysql-port"]
        )
plb's avatar
plb committed
34
    except getopt.GetoptError:
35
        print usage
plb's avatar
plb committed
36
37
        sys.exit(2)

38
39
40
41
    options['alchemy'] = False
    options['pdb'] = False
    options['verbose'] = 0

plb's avatar
plb committed
42
43
44
45
46
47
48
49
    for opt, arg in opts:
        if opt == '-h':
            print usage
            sys.exit()
        elif opt in ("-p", "--mysql-password"):
            options['mysql_password'] = arg
        elif opt in ("-P", "--mysql-port"):
            options['mysql_port'] = arg
50
51
52
53
54
55
        elif opt in ("-a", "--alchemy"):
            options['alchemy'] = True
        elif opt in ("-v", "--verbose"):
            options['verbose'] += 1
        elif opt in ("-d", "--pdb"):
            options['pdb'] = True
plb's avatar
plb committed
56
57
58
59

    return options


60
61
def do_mysql(options):
    # On recupere les donnees via la base de donnees de dolibarr
plb's avatar
plb committed
62
63
64
65
66
67
68
69
70
71
72
73
    s = settings.get('MYSQL_SETTINGS')

    password = s['password']
    if 'mysql_password' in options:
        password = options['mysql_password']
    if password is None or password == "":
        password = getpass.getpass("password for mysql user '%s': " % (s['user']))

    port = s['port']
    if 'mysql_port' in options:
        port = options['mysql_port']

74
75
76
77
78
79
    if options['alchemy']:
        dolibarr = HledgerDolibarrSQLAlchemy(s['host'], port, s['database'], s['user'], password)
        dolibarr.connect()
    else:
        dolibarr = DolibarrSQL(s['host'], port, s['database'], s['user'], password)
        dolibarr.connect()
plb's avatar
plb committed
80
81
82
83
    
    bank_entries = dolibarr.get_bank_entries()
    sell_entries = dolibarr.get_sell_entries()
    supplier_entries = dolibarr.get_supplier_entries()
plb's avatar
plb committed
84
    social_entries = dolibarr.get_social_entries()
plb's avatar
plb committed
85
86

    dolibarr.disconnect()
87
88
    
    # On verifie s'il manque des postes comptables dans les ecritures
plb's avatar
plb committed
89
90
91
92
    pc_missing = set()
    pc_missing.update(bank_entries.check_pc())
    pc_missing.update(sell_entries.check_pc())
    pc_missing.update(supplier_entries.check_pc())
plb's avatar
plb committed
93
    pc_missing.update(social_entries.check_pc())
plb's avatar
plb committed
94
95
96
97
98
    if len(pc_missing) > 0:
        print "WARNING: poste comptable manquant"
        for pc in pc_missing:
            sys.stdout.write("%s\n" % (pc))
    
99
    # On ecrie les fichiers hledger
plb's avatar
plb committed
100
101
102
    Writer.write("bank",bank_entries)            
    Writer.write("sells",sell_entries)            
    Writer.write("suppliers",supplier_entries)            
plb's avatar
plb committed
103
    Writer.write("social",social_entries)            
plb's avatar
plb committed
104
105
    Writer.write_hreport_plan()

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

def do_sqlalchemy(options):
    # On recupere les donnees via la base de donnees de dolibarr
    s = settings.get('MYSQL_SETTINGS')

    password = s['password']
    if 'mysql_password' in options:
        password = options['mysql_password']
    if password is None or password == "":
        password = getpass.getpass("password for mysql user '%s': " % (s['user']))

    port = s['port']
    if 'mysql_port' in options:
        port = options['mysql_port']

    dolibarr = HledgerDolibarrSQLAlchemy(s['host'], port, s['database'], s['user'], password, options['verbose'] >= 2)
    dolibarr.connect()

    if options['pdb']:
        s = dolibarr.session
        b = s.query(Bank).all()[10]
        set_trace()
        pass

    bank_journal = dolibarr.get_bank_journal()
    sell_journal = dolibarr.get_sell_journal()
    supplier_journal = dolibarr.get_supplier_journal()
    social_journal = dolibarr.get_social_journal()

    
    # On verifie s'il manque des postes comptables dans les ecritures
    pc_missing = set()
    pc_missing.update(bank_journal.check_pc())
    pc_missing.update(sell_journal.check_pc())
    pc_missing.update(supplier_journal.check_pc())
    pc_missing.update(social_journal.check_pc())
    if len(pc_missing) > 0:
        print "WARNING: poste comptable manquant"
        for pc in pc_missing:
            sys.stdout.write("%s\n" % (pc))
146
 
147
148
149
150
151
152
153
154
155
156
157
    # On ecrie les fichiers hledger
    Writer.write("bank", bank_journal)
    Writer.write("sells", sell_journal)
    Writer.write("suppliers", supplier_journal)
    Writer.write("social", social_journal)
    Writer.write_hreport_plan()

    dolibarr.disconnect()



plb's avatar
plb committed
158
159
160
def main(argv):
    locale.setlocale(locale.LC_ALL, b'fr_FR.utf-8')
    options = process_args(argv)
161
162
163
164
165
   
    if options['alchemy']:
        do_sqlalchemy(options)
    else:
        do_mysql(options)
plb's avatar
plb committed
166
167
168
169


if __name__ == "__main__":
    main(sys.argv[1:])