1
|
# -*- coding: utf-8 -*-
|
2
|
#!/usr/bin/env python
|
3
|
|
4
|
import os
|
5
|
import csv
|
6
|
|
7
|
from datetime import datetime, time, date
|
8
|
|
9
|
import calebasse.settings
|
10
|
import django.core.management
|
11
|
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
|
12
|
|
13
|
django.core.management.setup_environ(calebasse.settings)
|
14
|
|
15
|
from django.contrib.auth.models import User
|
16
|
|
17
|
from calebasse.actes.models import EventAct
|
18
|
from calebasse.agenda.models import Event, EventType
|
19
|
from calebasse.dossiers.models import PatientRecord, Status, FileState
|
20
|
from calebasse.ressources.models import Service
|
21
|
from calebasse.personnes.models import Worker, Holiday, TimeTable, PERIODICITIES
|
22
|
from calebasse.personnes.forms import PERIOD_LIST_TO_FIELDS
|
23
|
from calebasse.ressources.models import WorkerType, HolidayType
|
24
|
|
25
|
# Configuration
|
26
|
db_path = "./scripts/20121221-192258"
|
27
|
|
28
|
dbs = ["F_ST_ETIENNE_SESSAD_TED", "F_ST_ETIENNE_CMPP", "F_ST_ETIENNE_CAMSP", "F_ST_ETIENNE_SESSAD"]
|
29
|
|
30
|
|
31
|
def _to_date(str_date):
|
32
|
if not str_date:
|
33
|
return None
|
34
|
return datetime.strptime(str_date[:-13], "%Y-%m-%d")
|
35
|
|
36
|
def _to_int(str_int):
|
37
|
if not str_int:
|
38
|
return None
|
39
|
return int(str_int)
|
40
|
|
41
|
def discipline_mapper(tables_data, service):
|
42
|
for line in tables_data['discipline']:
|
43
|
# Insert workertype
|
44
|
if not WorkerType.objects.filter(name=line['libelle']):
|
45
|
WorkerType.objects.create(name=line['libelle'])
|
46
|
|
47
|
|
48
|
def intervenants_mapper(tables_data, service):
|
49
|
for line in tables_data['intervenants']:
|
50
|
# Insert workers
|
51
|
for disp in tables_data['discipline']:
|
52
|
if disp['id'] == line['discipline']:
|
53
|
type = WorkerType.objects.get(name=disp['libelle'])
|
54
|
# TODO : import actif or not
|
55
|
worker, created = Worker.objects.get_or_create(
|
56
|
type=type,
|
57
|
last_name=line['nom'],
|
58
|
first_name=line['prenom'],
|
59
|
email=line['email'],
|
60
|
phone=line['tel'],
|
61
|
gender=int(line['titre']),
|
62
|
)
|
63
|
worker.services.add(service)
|
64
|
|
65
|
def dossiers_mapper(tables_data, service):
|
66
|
global dossiers
|
67
|
for line in tables_data['dossiers']:
|
68
|
status = Status.objects.filter(type="ACCUEIL").filter(services=service)
|
69
|
creator = User.objects.get(id=1)
|
70
|
gender = _to_int(line['nais_sexe'])
|
71
|
if gender == 0:
|
72
|
gender = None
|
73
|
# TODO: add more fields
|
74
|
patient, created = PatientRecord.objects.get_or_create(first_name=line['prenom'],
|
75
|
last_name=line['nom'], birthdate=_to_date(line['nais_date']),
|
76
|
twinning_rank=_to_int(line['nais_rang']),
|
77
|
gender=gender, service=service, creator=creator)
|
78
|
dossiers[line['id']] = patient
|
79
|
|
80
|
if not created:
|
81
|
if not line['ins_date']:
|
82
|
# Hack when there is no inscription date put 01/01/1970
|
83
|
line['ins_date'] = "1970-01-01 00:00:00.000"
|
84
|
fs = FileState.objects.create(status=status[0], author=creator,
|
85
|
date_selected=_to_date(line['ins_date']),
|
86
|
previous_state=None, patient=patient)
|
87
|
patient.last_state = fs
|
88
|
patient.save()
|
89
|
if line['sor_date']:
|
90
|
status = Status.objects.filter(type="CLOS").filter(services=service)
|
91
|
fs = FileState.objects.create(status=status[0], author=creator,
|
92
|
date_selected=_to_date(line['sor_date']),
|
93
|
previous_state=None, patient=patient)
|
94
|
patient.last_state = fs
|
95
|
patient.save()
|
96
|
|
97
|
def rs_mapper(tables_data, service):
|
98
|
global dossiers
|
99
|
|
100
|
event_type = EventType.objects.get(
|
101
|
label=u"Rendez-vous patient"
|
102
|
)
|
103
|
|
104
|
for line in tables_data['rs']:
|
105
|
if dossiers.has_key(line['enfant_id']):
|
106
|
patient = dossiers[line['enfant_id']]
|
107
|
strdate = line['date_rdv'][:-13] + ' ' + line['heure'][11:-4]
|
108
|
date = datetime.strptime(strdate, "%Y-%m-%d %H:%M:%S")
|
109
|
|
110
|
# TODO: add act_type
|
111
|
# act_event = EventAct.objects.get_or_create(
|
112
|
# title=line['libelle'],
|
113
|
# event_type=event_type,
|
114
|
# patient=patient,
|
115
|
# act_type=act_type,
|
116
|
# date=date
|
117
|
# )
|
118
|
else:
|
119
|
# TODO: if no patient add event
|
120
|
pass
|
121
|
|
122
|
|
123
|
def conge_mapper(tables_data, service):
|
124
|
""" """
|
125
|
for line in tables_data['conge']:
|
126
|
pass
|
127
|
|
128
|
def ev_mapper(tables_data, service):
|
129
|
""" """
|
130
|
pass
|
131
|
|
132
|
def notes_mapper(tables_data, service):
|
133
|
""" """
|
134
|
pass
|
135
|
|
136
|
def _get_dict(cols, line):
|
137
|
""""""
|
138
|
res = {}
|
139
|
for i, data in enumerate(line):
|
140
|
res[cols[i]] = data.decode('utf-8')
|
141
|
return res
|
142
|
|
143
|
tables_data = {}
|
144
|
|
145
|
PERIOD_FAURE_NOUS = {1 : 1,
|
146
|
2 : 2,
|
147
|
3 : 3,
|
148
|
4 : 4,
|
149
|
5: 6,
|
150
|
6: 7,
|
151
|
7: 8,
|
152
|
8: 9,
|
153
|
9: None,
|
154
|
10: 10,
|
155
|
12: 11,
|
156
|
13: 12,
|
157
|
}
|
158
|
|
159
|
JOURS = {1: 'lundi',
|
160
|
2: 'mardi',
|
161
|
3: 'mercredi',
|
162
|
4: 'jeudi',
|
163
|
5: 'vendredi'
|
164
|
}
|
165
|
|
166
|
dic_worker = {}
|
167
|
|
168
|
|
169
|
def main():
|
170
|
""" """
|
171
|
|
172
|
thera_evt = {}
|
173
|
cf = open('./scripts/horaires_manuel.csv', 'wb')
|
174
|
writer = csv.writer(cf, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
175
|
writer.writerow(['Jour', 'Nom', 'Prénom', 'Libellé',
|
176
|
'Date début', 'Date fin', 'Horaire', 'Périodicité'])
|
177
|
|
178
|
|
179
|
for db in dbs:
|
180
|
if "F_ST_ETIENNE_CMPP" == db:
|
181
|
service = Service.objects.get(name="CMPP")
|
182
|
elif "F_ST_ETIENNE_CAMSP" == db:
|
183
|
service = Service.objects.get(name="CAMSP")
|
184
|
elif "F_ST_ETIENNE_SESSAD_TED" == db:
|
185
|
service = Service.objects.get(name="SESSAD TED")
|
186
|
elif "F_ST_ETIENNE_SESSAD" == db:
|
187
|
service = Service.objects.get(name="SESSAD DYS")
|
188
|
|
189
|
tables_data[service.name] = []
|
190
|
csvfile = open(os.path.join(db_path, db, 'ev.csv'), 'rb')
|
191
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
192
|
cols = csvlines.next()
|
193
|
i = 0
|
194
|
for line in csvlines:
|
195
|
if line[8].upper() == 'ARRIVEE' or line[8].upper() == 'DEPART':
|
196
|
data = _get_dict(cols, line)
|
197
|
tables_data[service.name].append(data)
|
198
|
i += 1
|
199
|
csvfile.close()
|
200
|
|
201
|
print "%s - Nombre d'evt horaires : %d" % (service.name, i)
|
202
|
|
203
|
thera_evt[service.name] = {}
|
204
|
csvfile = open(os.path.join(db_path, db, 'details_ev.csv'), 'rb')
|
205
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
206
|
cols = csvlines.next()
|
207
|
not_found = []
|
208
|
for line in csvlines:
|
209
|
worker = None
|
210
|
try:
|
211
|
if service.name == 'CMPP':
|
212
|
worker = Worker.objects.get(old_cmpp_id=line[2])
|
213
|
elif service.name == 'CAMSP':
|
214
|
worker = Worker.objects.get(old_camsp_id=line[2])
|
215
|
elif service.name == 'SESSAD DYS':
|
216
|
worker = Worker.objects.get(old_sessad_dys_id=line[2])
|
217
|
elif service.name == 'SESSAD TED':
|
218
|
worker = Worker.objects.get(old_sessad_ted_id=line[2])
|
219
|
else:
|
220
|
print "service inconnu!!!"
|
221
|
exit(0)
|
222
|
except Exception, e:
|
223
|
not_found.append(line[2])
|
224
|
if worker:
|
225
|
workers = thera_evt.setdefault(line[1], [])
|
226
|
workers.append(worker)
|
227
|
csvfile.close()
|
228
|
|
229
|
print "%s - Liste des worker not found : %s" % (service.name, str(set(not_found)))
|
230
|
# 7 8 17 n'existe pas au SESSAD TED
|
231
|
# ['91', '89', '17', '77', '76', '75', '74', '73', '72', '82', '90', '85'] n'existe pas au CMPP
|
232
|
|
233
|
i = 0
|
234
|
j = 0
|
235
|
dic_worker[service.name] = {}
|
236
|
for horaire in tables_data[service.name]:
|
237
|
if not horaire['id'] in thera_evt[service.name]:
|
238
|
j += 1
|
239
|
elif not thera_evt[service.name][horaire['id']]:
|
240
|
i += 1
|
241
|
else:
|
242
|
if len(thera_evt[service.name][horaire['id']]) > 1:
|
243
|
print "%s - Horaire %s avec plus d'un worker %s!" % (service.name, horaire['id'], str(len(thera_evt[service.name][horaire['id']])))
|
244
|
exit(0)
|
245
|
worker = thera_evt[service.name][horaire['id']][0]
|
246
|
if not worker in dic_worker[service.name].keys():
|
247
|
dic_worker[service.name][worker] = {}
|
248
|
if 'horaires' in dic_worker[service.name][worker].keys():
|
249
|
dic_worker[service.name][worker]['horaires'].append(horaire)
|
250
|
else:
|
251
|
dic_worker[service.name][worker]['horaires'] = [horaire]
|
252
|
|
253
|
for worker, details in dic_worker[service.name].items():
|
254
|
for horaire in details['horaires']:
|
255
|
date_fin = None
|
256
|
if 'date_fin' in horaire and horaire['date_fin'] != '':
|
257
|
date_fin = datetime.strptime(horaire['date_fin'][:-13], "%Y-%m-%d")
|
258
|
if not date_fin or date_fin > datetime.today():
|
259
|
start_date = datetime.strptime(horaire['date_debut'][:-13], "%Y-%m-%d").date()
|
260
|
weekday = int(start_date.strftime('%w'))
|
261
|
if weekday in dic_worker[service.name][worker].keys():
|
262
|
dic_worker[service.name][worker][weekday].append(horaire)
|
263
|
else:
|
264
|
dic_worker[service.name][worker][weekday] = [horaire]
|
265
|
for i in range(0,8):
|
266
|
if i in dic_worker[service.name][worker].keys():
|
267
|
horaires = sorted(dic_worker[service.name][worker][weekday], key=lambda tup: tup['date_debut'])
|
268
|
dic_worker[service.name][worker][weekday] = horaires
|
269
|
|
270
|
i = 0
|
271
|
j = 0
|
272
|
for worker in dic_worker[service.name].keys():
|
273
|
for day, horaires in dic_worker[service.name][worker].items():
|
274
|
if day != 'horaires':
|
275
|
i += 1
|
276
|
if len(horaires) > 2 or len(horaires)%2:
|
277
|
print 'A'
|
278
|
for horaire in horaires:
|
279
|
print [day, worker.last_name, horaire['libelle'], horaire['date_debut']]
|
280
|
for k, v in PERIODICITIES:
|
281
|
if PERIOD_FAURE_NOUS[int(horaire['rythme'])] == k:
|
282
|
p = v
|
283
|
break
|
284
|
d = JOURS[day]
|
285
|
writer.writerow([d, worker.last_name.encode('utf-8'), worker.first_name.encode('utf-8'), horaire['libelle'],
|
286
|
horaire['date_debut'], horaire['date_fin'], horaire['heure'], p])
|
287
|
else:
|
288
|
arrivee = None
|
289
|
depart = None
|
290
|
for horaire in horaires:
|
291
|
if horaire['libelle'] == 'ARRIVEE':
|
292
|
arrivee = horaire
|
293
|
else:
|
294
|
depart = horaire
|
295
|
if not (arrivee and depart):
|
296
|
print 'B'
|
297
|
for horaire in horaires:
|
298
|
print [day, worker.last_name, horaire['libelle'], horaire['date_debut']]
|
299
|
for k, v in PERIODICITIES:
|
300
|
if PERIOD_FAURE_NOUS[int(horaire['rythme'])] == k:
|
301
|
p = v
|
302
|
break
|
303
|
d = JOURS[day]
|
304
|
writer.writerow([d, worker.last_name.encode('utf-8'), worker.first_name.encode('utf-8'), horaire['libelle'],
|
305
|
horaire['date_debut'], horaire['date_fin'], horaire['heure'], p])
|
306
|
else:
|
307
|
j += 1
|
308
|
# Créer les horaires
|
309
|
start_date = datetime.strptime(arrivee['date_debut'][:-13], "%Y-%m-%d").date()
|
310
|
weekday = day - 1
|
311
|
periodicity = PERIOD_FAURE_NOUS[int(arrivee['rythme'])]
|
312
|
if not periodicity:
|
313
|
periodicity = 1
|
314
|
week_period, week_parity, week_rank = PERIOD_LIST_TO_FIELDS[periodicity - 1]
|
315
|
end_date = None
|
316
|
skip = False
|
317
|
if arrivee['date_fin']:
|
318
|
end_date = datetime.strptime(arrivee['date_fin'][:-13], "%Y-%m-%d").date()
|
319
|
tt = TimeTable(worker=worker,
|
320
|
weekday=weekday,
|
321
|
periodicity=periodicity,
|
322
|
week_period=week_period, week_parity=week_parity, week_rank=week_rank,
|
323
|
start_time = datetime.strptime(arrivee['heure'][11:16], "%H:%M").time(),
|
324
|
end_time = datetime.strptime(depart['heure'][11:16], "%H:%M").time(),
|
325
|
start_date = start_date,
|
326
|
end_date = end_date)
|
327
|
tt.save()
|
328
|
tt.services.add(service)
|
329
|
print '====> %s %s' % (worker.last_name.encode('utf-8'), worker.first_name.encode('utf-8'))
|
330
|
|
331
|
|
332
|
print i
|
333
|
print j
|
334
|
|
335
|
csvfile.close()
|
336
|
|
337
|
if __name__ == "__main__":
|
338
|
main()
|