1
|
# -*- coding: utf-8 -*-
|
2
|
#!/usr/bin/env python
|
3
|
|
4
|
import os
|
5
|
import sys
|
6
|
import csv
|
7
|
|
8
|
from datetime import datetime, time
|
9
|
from dateutil.relativedelta import relativedelta
|
10
|
|
11
|
import calebasse.settings
|
12
|
import django.core.management
|
13
|
|
14
|
django.core.management.setup_environ(calebasse.settings)
|
15
|
|
16
|
from django.contrib.auth.models import User
|
17
|
|
18
|
from calebasse.agenda.models import Event, EventType
|
19
|
from calebasse.dossiers.models import PatientRecord, Status, FileState, PatientAddress, PatientContact
|
20
|
from calebasse.ressources.models import Service
|
21
|
from calebasse.personnes.models import Worker, Holiday, ExternalWorker, ExternalTherapist
|
22
|
from calebasse.ressources.models import (WorkerType, ParentalAuthorityType, ParentalCustodyType,
|
23
|
FamilySituationType, TransportType, TransportCompany, Provenance, AnalyseMotive, FamilyMotive,
|
24
|
CodeCFTMEA, SocialisationDuration, School, SchoolLevel, OutMotive, OutTo, AdviceGiver,
|
25
|
MaritalStatusType, Job, PatientRelatedLink, HealthCenter)
|
26
|
|
27
|
# Configuration
|
28
|
db_path = "./scripts/20130104-213225"
|
29
|
|
30
|
#dbs = ["F_ST_ETIENNE_SESSAD_TED", "F_ST_ETIENNE_CMPP", "F_ST_ETIENNE_CAMSP", "F_ST_ETIENNE_SESSAD"]
|
31
|
dbs = ["F_ST_ETIENNE_SESSAD_TED"]
|
32
|
|
33
|
|
34
|
map_cs = {}
|
35
|
map_cs['CAMSP'] = {
|
36
|
'1': 'ACT_DOUBLE',
|
37
|
'2': 'ABS_NON_EXC',
|
38
|
'3': 'ABS_EXC',
|
39
|
'4': 'ABS_INTER',
|
40
|
'5': 'ACT_LOST',
|
41
|
'6': 'ANNUL_NOUS',
|
42
|
'7': 'ANNUL_FAMILLE',
|
43
|
'8': 'ENF_HOSP',
|
44
|
'9': 'ACT_LOST',
|
45
|
'10': 'ACT_LOST',
|
46
|
'11': 'ACT_LOST',
|
47
|
'12': 'REPORTE'
|
48
|
}
|
49
|
|
50
|
map_cs['CMPP'] = {
|
51
|
'1': 'ACT_DOUBLE',
|
52
|
'2': 'ABS_NON_EXC',
|
53
|
'3': 'ABS_EXC',
|
54
|
'4': 'ABS_INTER',
|
55
|
'5': 'ACT_LOST',
|
56
|
'6': 'ANNUL_NOUS',
|
57
|
'7': 'ANNUL_FAMILLE',
|
58
|
'8': 'ABS_ESS_PPS',
|
59
|
'9': 'ACT_LOST',
|
60
|
'10': 'ACT_LOST',
|
61
|
'11': 'ACT_LOST',
|
62
|
'12': 'REPORTE'
|
63
|
}
|
64
|
|
65
|
map_cs['SESSAD DYS'] = {
|
66
|
'1': 'ACT_DOUBLE',
|
67
|
'2': 'ABS_NON_EXC',
|
68
|
'3': 'ABS_EXC',
|
69
|
'4': 'ABS_INTER',
|
70
|
'5': 'ACT_LOST',
|
71
|
'6': 'ANNUL_NOUS',
|
72
|
'7': 'ANNUL_FAMILLE',
|
73
|
'8': 'ABS_ESS_PPS',
|
74
|
'9': 'ACT_LOST',
|
75
|
'10': 'ACT_LOST',
|
76
|
'11': 'REPORTE'
|
77
|
}
|
78
|
|
79
|
|
80
|
map_cs['SESSAD TED'] = {
|
81
|
'1': 'ACT_DOUBLE',
|
82
|
'2': 'ABS_NON_EXC',
|
83
|
'3': 'ABS_EXC',
|
84
|
'4': 'ACT_LOST',
|
85
|
'5': 'ABS_INTER',
|
86
|
'6': 'ANNUL_NOUS',
|
87
|
'7': 'ANNUL_FAMILLE',
|
88
|
'8': 'REPORTE'
|
89
|
}
|
90
|
|
91
|
def _exist(str):
|
92
|
if str and str != "" and str != '0':
|
93
|
return True
|
94
|
return False
|
95
|
|
96
|
def treat_name(name):
|
97
|
res = ''
|
98
|
for p in name.split():
|
99
|
res += p[0].upper()+p[1:].lower()
|
100
|
res += ' '
|
101
|
return res[:-1]
|
102
|
|
103
|
def _to_date(str_date):
|
104
|
if not str_date:
|
105
|
return None
|
106
|
return datetime.strptime(str_date[:-13], "%Y-%m-%d")
|
107
|
|
108
|
def _to_int(str_int):
|
109
|
if not str_int:
|
110
|
return None
|
111
|
return int(str_int)
|
112
|
|
113
|
def _get_dict(cols, line):
|
114
|
""""""
|
115
|
res = {}
|
116
|
for i, data in enumerate(line):
|
117
|
res[cols[i]] = data.decode('utf-8')
|
118
|
return res
|
119
|
|
120
|
tables_data = {}
|
121
|
|
122
|
map_rm_cmpp = [1, 3, 2, 8, 6, 4]
|
123
|
|
124
|
def get_rm(service, val):
|
125
|
old_id_rm = _to_int(val)
|
126
|
if old_id_rm < 1 or 'SESSAD' in service.name:
|
127
|
return None
|
128
|
if service.name == 'CMPP':
|
129
|
old_id_rm = map_rm_cmpp[old_id_rm - 1]
|
130
|
try:
|
131
|
return MaritalStatusType.objects.get(id=old_id_rm)
|
132
|
except:
|
133
|
return None
|
134
|
|
135
|
map_job_camsp = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 25, 21, 23, 20, 17, 15, 18, 16, 26, 27, 28]
|
136
|
|
137
|
# CMPP à 25 = Rien
|
138
|
def get_job(service, val):
|
139
|
old_id_job = _to_int(val)
|
140
|
if old_id_job < 1:
|
141
|
return None
|
142
|
if service.name == 'CAMSP' and old_id_job == 26:
|
143
|
return None
|
144
|
if service.name == 'CAMSP':
|
145
|
try:
|
146
|
old_id_job = map_job_camsp[old_id_job - 1]
|
147
|
except:
|
148
|
print 'Old id job out of range: %d' % old_id_job
|
149
|
try:
|
150
|
return Job.objects.get(id=old_id_job)
|
151
|
except:
|
152
|
return None
|
153
|
|
154
|
def extract_phone(val):
|
155
|
if not val or val == '' or val == '0':
|
156
|
return None
|
157
|
s = ''.join([c for c in val if c.isdigit()])
|
158
|
return s[:11]
|
159
|
|
160
|
def get_nir(nir, key, writer, line, service):
|
161
|
if not nir:
|
162
|
return None
|
163
|
if len(nir) != 13:
|
164
|
return -1
|
165
|
if key:
|
166
|
minus = 0
|
167
|
# Corsica dept 2A et 2B
|
168
|
if nir[6] in ('A', 'a'):
|
169
|
nir = [c for c in nir]
|
170
|
nir[6] = '0'
|
171
|
nir = ''.join(nir)
|
172
|
minus = 1000000
|
173
|
elif nir[6] in ('B', 'b'):
|
174
|
nir = [c for c in nir]
|
175
|
nir[6] = '0'
|
176
|
nir = ''.join(nir)
|
177
|
minus = 2000000
|
178
|
try:
|
179
|
nir = int(nir) - minus
|
180
|
good_key = 97 - (nir % 97)
|
181
|
key = int(key)
|
182
|
if key != good_key:
|
183
|
msg = 'Clé incorrect %s pour %s' % (str(key), str(nir))
|
184
|
writer.writerow(line + [service.name, msg])
|
185
|
except:
|
186
|
pass
|
187
|
return nir
|
188
|
|
189
|
|
190
|
def import_dossiers_phase_1():
|
191
|
""" """
|
192
|
print "====== Début à %s ======" % str(datetime.today())
|
193
|
|
194
|
f1 = open('./scripts/dossiers_ecoles_manuel.csv', 'wb')
|
195
|
writer1 = csv.writer(f1, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
196
|
|
197
|
f2 = open('./scripts/dossiers_manuel.csv', 'wb')
|
198
|
writer2 = csv.writer(f2, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
199
|
|
200
|
f3 = open('./scripts/contacts_manuel.csv', 'wb')
|
201
|
writer3 = csv.writer(f3, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
202
|
|
203
|
f4 = open('./scripts/pc_manuel.csv', 'wb')
|
204
|
writer4 = csv.writer(f4, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
|
205
|
|
206
|
status_accueil = Status.objects.filter(type="ACCUEIL")[0]
|
207
|
status_diagnostic = Status.objects.filter(type="DIAGNOSTIC")[0]
|
208
|
status_traitement = Status.objects.filter(type="TRAITEMENT")[0]
|
209
|
status_clos = Status.objects.filter(type="CLOS")[0]
|
210
|
status_bilan = Status.objects.filter(type="BILAN")[0]
|
211
|
status_suivi = Status.objects.filter(type="SUIVI")[0]
|
212
|
status_surveillance = Status.objects.filter(type="SURVEILLANCE")[0]
|
213
|
creator = User.objects.get(id=1)
|
214
|
|
215
|
for db in dbs:
|
216
|
if "F_ST_ETIENNE_CMPP" == db:
|
217
|
service = Service.objects.get(name="CMPP")
|
218
|
elif "F_ST_ETIENNE_CAMSP" == db:
|
219
|
service = Service.objects.get(name="CAMSP")
|
220
|
elif "F_ST_ETIENNE_SESSAD_TED" == db:
|
221
|
service = Service.objects.get(name="SESSAD TED")
|
222
|
elif "F_ST_ETIENNE_SESSAD" == db:
|
223
|
service = Service.objects.get(name="SESSAD DYS")
|
224
|
|
225
|
print "====== %s ======" % service.name
|
226
|
print datetime.today()
|
227
|
|
228
|
print "--> Lecture table des dossiers..."
|
229
|
csvfile = open(os.path.join(db_path, db, 'dossiers.csv'), 'rb')
|
230
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
231
|
d_cols = csvlines.next()
|
232
|
writer2.writerow(d_cols + ['service', 'creation', 'commentaire'])
|
233
|
tables_data['dossiers'] = []
|
234
|
for line in csvlines:
|
235
|
#Au moins nom et prénom
|
236
|
if _exist(line[1]) and _exist(line[2]):
|
237
|
data = _get_dict(d_cols, line)
|
238
|
tables_data['dossiers'].append(data)
|
239
|
else:
|
240
|
writer2.writerow(line + [service.name, 'Non', 'Ni Nom, ni prénom'])
|
241
|
csvfile.close()
|
242
|
print "<-- Terminé"
|
243
|
|
244
|
print "--> Lecture table des caisses..."
|
245
|
csvfile = open(os.path.join(db_path, db, 'caisses.csv'), 'rb')
|
246
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
247
|
caisses_cols = csvlines.next()
|
248
|
tables_data['caisses'] = {}
|
249
|
for line in csvlines:
|
250
|
data = _get_dict(caisses_cols, line)
|
251
|
tables_data['caisses'][data['id']] = data
|
252
|
csvfile.close()
|
253
|
print "<-- Terminé"
|
254
|
|
255
|
print "--> Chargement mises..."
|
256
|
mises = {}
|
257
|
csvfile = open(os.path.join(db_path, db, 'mises.csv'), 'rb')
|
258
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
259
|
cols = csvlines.next()
|
260
|
for line in csvlines:
|
261
|
mises[line[0]] = (line[1], line[2])
|
262
|
csvfile.close()
|
263
|
print "<-- Terminé"
|
264
|
|
265
|
print "--> Chargement quotations..."
|
266
|
mises_per_patient = {}
|
267
|
csvfile = open(os.path.join(db_path, db, 'dossiers_mises.csv'), 'rb')
|
268
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
269
|
cols = csvlines.next()
|
270
|
for line in csvlines:
|
271
|
code, axe = mises[line[2]]
|
272
|
quotation = CodeCFTMEA.objects.get(code=int(code), axe=int(axe))
|
273
|
if line[1] in mises_per_patient.keys():
|
274
|
mises_per_patient[line[1]].append(quotation)
|
275
|
else:
|
276
|
mises_per_patient[line[1]] = [quotation]
|
277
|
csvfile.close()
|
278
|
print "<-- Terminé"
|
279
|
|
280
|
print "--> Ajout périodes de socialisation..."
|
281
|
social_duration_per_patient = {}
|
282
|
csvfile = open(os.path.join(db_path, db, 'dossiers_ecoles.csv'), 'rb')
|
283
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
284
|
cols = csvlines.next()
|
285
|
writer1.writerow(cols + ['service'])
|
286
|
i = 0
|
287
|
for line in csvlines:
|
288
|
i += 1
|
289
|
print "Nombre à traiter : %d" % i
|
290
|
csvfile.close()
|
291
|
csvfile = open(os.path.join(db_path, db, 'dossiers_ecoles.csv'), 'rb')
|
292
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
293
|
csvlines.next()
|
294
|
for line in csvlines:
|
295
|
school = None
|
296
|
if _exist(line[2]):
|
297
|
school = School.objects.get(old_id=line[2], old_service=service.name)
|
298
|
level = None
|
299
|
try:
|
300
|
level = SchoolLevel.objects.get(old_id=line[4], old_service=service.name)
|
301
|
except:
|
302
|
pass
|
303
|
contact = line[3]
|
304
|
if contact != "":
|
305
|
contact += ' ' + line[7]
|
306
|
else:
|
307
|
contact = line[7]
|
308
|
start_date = _to_date(line[5])
|
309
|
social_duration = SocialisationDuration(school=school, level=level, start_date=start_date, contact=contact)
|
310
|
social_duration.save()
|
311
|
if line[1] in social_duration_per_patient.keys():
|
312
|
social_duration_per_patient[line[1]].append(social_duration)
|
313
|
else:
|
314
|
social_duration_per_patient[line[1]] = [social_duration]
|
315
|
#Produce file for manuel treatment:
|
316
|
for j in range(6, 16):
|
317
|
if j != 7 and _exist(line[j]):
|
318
|
writer1.writerow(line + [service.name])
|
319
|
i -= 1
|
320
|
if not (i % 10):
|
321
|
sys.stdout.write('%d' %i)
|
322
|
else:
|
323
|
sys.stdout.write('.')
|
324
|
sys.stdout.flush()
|
325
|
csvfile.close()
|
326
|
print "<-- Terminé"
|
327
|
|
328
|
print "--> Ajout des adresses..."
|
329
|
adresses_per_patient = {}
|
330
|
adresses_per_old_id = {}
|
331
|
csvfile = open(os.path.join(db_path, db, 'adresses.csv'), 'rb')
|
332
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
333
|
cols = csvlines.next()
|
334
|
i = 0
|
335
|
for line in csvlines:
|
336
|
i += 1
|
337
|
print "Nombre à traiter : %d" % i
|
338
|
csvfile.close()
|
339
|
csvfile = open(os.path.join(db_path, db, 'adresses.csv'), 'rb')
|
340
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
341
|
csvlines.next()
|
342
|
for line in csvlines:
|
343
|
phone = extract_phone(line[5])
|
344
|
comment = ''
|
345
|
if _exist(line[10]):
|
346
|
comment += line[10] + ' - '
|
347
|
if _exist(line[5]):
|
348
|
comment += "Numéro 1 : " + line[5] + ' - '
|
349
|
if _exist(line[8]):
|
350
|
comment += "Numéro 2 : " + line[8] + ' - '
|
351
|
fax = None
|
352
|
place_of_life = False
|
353
|
if _exist(line[9]) and line[9] == '-1':
|
354
|
place_of_life = True
|
355
|
number = None
|
356
|
street = line[11]
|
357
|
address_complement = line[12]
|
358
|
zip_code = None
|
359
|
if _exist(line[3]):
|
360
|
if len(line[3]) > 5:
|
361
|
zip_code = line[3][-5:]
|
362
|
else:
|
363
|
zip_code = line[3]
|
364
|
city = line[4]
|
365
|
|
366
|
address = PatientAddress(phone=phone, comment=comment,
|
367
|
fax=fax, place_of_life=place_of_life, number=number,
|
368
|
street=street, address_complement=address_complement,
|
369
|
zip_code=zip_code, city=city)
|
370
|
|
371
|
address.save()
|
372
|
if line[1] in adresses_per_patient.keys():
|
373
|
adresses_per_patient[line[1]].append(address)
|
374
|
else:
|
375
|
adresses_per_patient[line[1]] = [address]
|
376
|
adresses_per_old_id[line[0]] = (address, line[1])
|
377
|
i -= 1
|
378
|
if not (i % 10):
|
379
|
sys.stdout.write('%d' %i)
|
380
|
else:
|
381
|
sys.stdout.write('.')
|
382
|
sys.stdout.flush()
|
383
|
csvfile.close()
|
384
|
print "<-- Terminé"
|
385
|
|
386
|
print "--> Chargement des prise en charge..."
|
387
|
csvfile = open(os.path.join(db_path, db, 'pc.csv'), 'rb')
|
388
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
389
|
pc_cols = csvlines.next()
|
390
|
writer4.writerow(pc_cols + ['service', 'creation', 'commentaire'])
|
391
|
tables_data['pcs'] = {}
|
392
|
for line in csvlines:
|
393
|
data = _get_dict(pc_cols, line)
|
394
|
if line[1] in tables_data['pcs'].keys():
|
395
|
tables_data['pcs'][line[1]].append(data)
|
396
|
else:
|
397
|
tables_data['pcs'][line[1]] = [data]
|
398
|
csvfile.close()
|
399
|
print "<-- Terminé"
|
400
|
|
401
|
print "--> Ajout des contacts..."
|
402
|
contacts_per_patient = {}
|
403
|
csvfile = open(os.path.join(db_path, db, 'contacts.csv'), 'rb')
|
404
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
405
|
cols = csvlines.next()
|
406
|
writer3.writerow(cols + ['service', 'commentaire'])
|
407
|
i = 0
|
408
|
for line in csvlines:
|
409
|
i += 1
|
410
|
print "Nombre à traiter : %d" % i
|
411
|
csvfile.close()
|
412
|
csvfile = open(os.path.join(db_path, db, 'contacts.csv'), 'rb')
|
413
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
414
|
csvlines.next()
|
415
|
for line in csvlines:
|
416
|
phone = extract_phone(line[13])
|
417
|
mobile = extract_phone(line[14])
|
418
|
contact_comment = ''
|
419
|
if _exist(line[13]):
|
420
|
contact_comment += "Travail : " + line[13] + ' - '
|
421
|
if _exist(line[14]):
|
422
|
contact_comment += "Mobile : " + line[14] + ' - '
|
423
|
if _exist(line[17]):
|
424
|
contact_comment += "Divers : " + line[17] + ' - '
|
425
|
last_name = treat_name(line[3])
|
426
|
first_name = treat_name(line[4])
|
427
|
gender = None
|
428
|
if line[2] == "1":
|
429
|
gender = 1
|
430
|
if line[2] == "2":
|
431
|
gender = 2
|
432
|
email = None
|
433
|
if _exist(line[15]):
|
434
|
email = line[15]
|
435
|
birthplace = None
|
436
|
if _exist(line[6]):
|
437
|
birthplace = line[6]
|
438
|
social_security_id = get_nir(line[7], line[8], writer3, line, service)
|
439
|
if social_security_id == -1:
|
440
|
msg = 'Numéro %s de longueur diff de 13.' % line[7]
|
441
|
writer3.writerow(line + [service.name, msg])
|
442
|
contact_comment += "Numéro NIR invalide : " + line[7] + ' - '
|
443
|
social_security_id = None
|
444
|
birthdate = _to_date(line[5])
|
445
|
job = get_job(service, line[9])
|
446
|
parente = None
|
447
|
try:
|
448
|
if service.name == 'CAMSP':
|
449
|
parente = PatientRelatedLink.objects.get(old_camsp_id=_to_int(line[11]))
|
450
|
elif service.name == 'CMPP':
|
451
|
parente = PatientRelatedLink.objects.get(old_cmpp_id=_to_int(line[11]))
|
452
|
elif service.name == 'SESSAD DYS':
|
453
|
parente = PatientRelatedLink.objects.get(old_sessad_dys_id=_to_int(line[11]))
|
454
|
elif service.name == 'SESSAD TED':
|
455
|
parente = PatientRelatedLink.objects.get(old_sessad_ted_id=_to_int(line[11]))
|
456
|
except:
|
457
|
pass
|
458
|
twinning_rank = None
|
459
|
thirdparty_payer = False
|
460
|
begin_rights = None
|
461
|
end_rights = None
|
462
|
health_center = None
|
463
|
old_contact_id = line[0]
|
464
|
|
465
|
contact = PatientContact(phone=phone, mobile= mobile,
|
466
|
contact_comment=contact_comment,
|
467
|
last_name = last_name, first_name = first_name,
|
468
|
gender = gender, email = email, parente = parente,
|
469
|
social_security_id = social_security_id,
|
470
|
birthdate = birthdate, job = job, birthplace=birthplace,
|
471
|
twinning_rank = twinning_rank,
|
472
|
thirdparty_payer = thirdparty_payer,
|
473
|
begin_rights = begin_rights,
|
474
|
end_rights = end_rights,
|
475
|
health_center = health_center,
|
476
|
old_contact_id=old_contact_id)
|
477
|
contact.save()
|
478
|
|
479
|
if not line[1] in adresses_per_old_id:
|
480
|
msg = 'Contact sans adresse'
|
481
|
writer3.writerow(line + [service.name, msg])
|
482
|
contact.delete()
|
483
|
else:
|
484
|
adresse, old_patient_id = adresses_per_old_id[line[1]]
|
485
|
contact.addresses.add(adresse)
|
486
|
# Ajouter l'adresse au contact
|
487
|
# Faire une liste des contacts par patient pour ajouter ensuite
|
488
|
if old_patient_id in contacts_per_patient.keys():
|
489
|
contacts_per_patient[old_patient_id].append(contact)
|
490
|
else:
|
491
|
contacts_per_patient[old_patient_id] = [contact]
|
492
|
i -= 1
|
493
|
if not (i % 10):
|
494
|
sys.stdout.write('%d' %i)
|
495
|
else:
|
496
|
sys.stdout.write('.')
|
497
|
sys.stdout.flush()
|
498
|
csvfile.close()
|
499
|
print "<-- Terminé"
|
500
|
|
501
|
print "--> Ajout dossiers..."
|
502
|
print "Nombre à traiter : %d" % len(tables_data['dossiers'])
|
503
|
i = len(tables_data['dossiers'])
|
504
|
for dossier in tables_data['dossiers']:
|
505
|
|
506
|
date_accueil = None
|
507
|
date_diagnostic = None
|
508
|
date_traitement = None
|
509
|
date_clos = None
|
510
|
date_bilan = None
|
511
|
date_suivi = None
|
512
|
date_surveillance = None
|
513
|
date_retour = None
|
514
|
fss = []
|
515
|
date_accueil = _to_date(dossier['con_date'])
|
516
|
date_traitement = _to_date(dossier['ins_date'])
|
517
|
date_clos = _to_date(dossier['sor_date'])
|
518
|
if not (date_accueil or date_traitement or date_clos):
|
519
|
# no state date, the record is inconsistent
|
520
|
writer2.writerow([dossier[c].encode('utf-8') for c in d_cols] + [service.name, 'Non', "Aucune date d'état existante"])
|
521
|
continue
|
522
|
# Manage states
|
523
|
if date_accueil and date_traitement and date_accueil >= date_traitement:
|
524
|
date_accueil = None
|
525
|
if date_traitement and date_clos and date_traitement >= date_clos:
|
526
|
date_traitement = None
|
527
|
if date_accueil and date_clos and date_accueil >= date_clos:
|
528
|
date_accueil = None
|
529
|
if "SESSAD" in service.name:
|
530
|
# Il n'y a jamais eu de retour au SESSADs
|
531
|
if date_accueil:
|
532
|
fss.append((status_accueil, date_accueil, None))
|
533
|
if date_traitement:
|
534
|
fss.append((status_traitement, date_traitement, None))
|
535
|
if date_clos:
|
536
|
fss.append((status_clos, date_clos, None))
|
537
|
# Jamais de motif et provenance au retour
|
538
|
elif service.name == 'CAMSP':
|
539
|
date_retour = _to_date(dossier['ret_date'])
|
540
|
if date_accueil:
|
541
|
fss.append((status_accueil, date_accueil, None))
|
542
|
if not date_retour:
|
543
|
if date_traitement:
|
544
|
s = status_bilan
|
545
|
if dossier['suivi'] == '3':
|
546
|
s = status_suivi
|
547
|
elif dossier['suivi'] == '4':
|
548
|
s = status_surveillance
|
549
|
# fss.append((s, date_traitement, "Il peut y avoir plusieurs états de suivi durant cette période de suivi mais ils ne peuvent être déterminés."))
|
550
|
fss.append((s, date_traitement, ""))
|
551
|
else:
|
552
|
# Le retour supprime la précédente de clôture, on choisit le retour à j-1
|
553
|
if date_traitement:
|
554
|
# c'est l'inscription
|
555
|
if date_traitement < date_retour:
|
556
|
fss.append((status_suivi, date_traitement, "Etat de traitement indéterminé (Suivi par défaut)."))
|
557
|
old_clos_date = date_retour + relativedelta(days=-1)
|
558
|
fss.append((status_clos, old_clos_date, "La date de clôture est indéterminée (par défaut positionnée à 1 jour avant le retour)."))
|
559
|
s = status_bilan
|
560
|
if dossier['suivi'] == '3':
|
561
|
s = status_suivi
|
562
|
elif dossier['suivi'] == '4':
|
563
|
s = status_surveillance
|
564
|
# fss.append((s, date_retour, "Il peut y avoir plusieurs états de suivi durant cette période de suivi mais ils ne peuvent être déterminés."))
|
565
|
fss.append((s, date_retour, ""))
|
566
|
if date_clos:
|
567
|
if date_retour and date_clos < date_retour:
|
568
|
print 'La date de clôture ne peut être antérieure à la date de retour!'
|
569
|
else:
|
570
|
fss.append((status_clos, date_clos, None))
|
571
|
else:
|
572
|
date_retour = _to_date(dossier['ret_date'])
|
573
|
if date_accueil:
|
574
|
fss.append((status_accueil, date_accueil, None))
|
575
|
if date_traitement:
|
576
|
fss.append((status_diagnostic, date_traitement, "Inscription en diag mais pourrait etre en trait. A préciser"))
|
577
|
if (date_retour and date_clos and date_retour > date_clos) or not date_clos:
|
578
|
if old_id in tables_data['pcs'].keys():
|
579
|
pcs = tables_data['pcs'][old_id]
|
580
|
last_pc = pcs[len(pcs)-1]
|
581
|
if last_pc['genre_pc'] == '2':
|
582
|
status, date, msg = fss[len(fss)-1]
|
583
|
last_date = date + relativedelta(days=1)
|
584
|
fss.append((status_traitement, last_date, "Date à préciser."))
|
585
|
else:
|
586
|
fss.append((status_clos, date_clos, None))
|
587
|
|
588
|
# Il faut l'historique des actes du patient
|
589
|
# Il faut pour chaque acte assigner à la pc
|
590
|
# on sait les derniers actes s'ils sont affecté ou non
|
591
|
# Il faudrait conserver avec les actes les num de facture ?
|
592
|
# rEvori el fonctionnement de l'assignation d'un acte a une pc
|
593
|
|
594
|
for col in ('mdph', 'code_archive', 'aeeh', 'mdph_departement', 'pps', 'pps_deb', 'pps_fin', 'mdph_Debut', 'mdph_Fin'):
|
595
|
if _exist(dossier[col]):
|
596
|
writer2.writerow([dossier[c].encode('utf-8') for c in d_cols] + [service.name, 'Oui', "Données présentes non traitées"])
|
597
|
break
|
598
|
|
599
|
#People
|
600
|
first_name = treat_name(dossier['prenom'])
|
601
|
last_name = treat_name(dossier['nom'])
|
602
|
display_name = None
|
603
|
gender = _to_int(dossier['nais_sexe'])
|
604
|
if not gender in (1,2):
|
605
|
gender = None
|
606
|
email = None
|
607
|
phone = None
|
608
|
|
609
|
#PatientContact
|
610
|
mobile = None
|
611
|
social_security_id = None
|
612
|
birthdate = _to_date(dossier['nais_date'])
|
613
|
twinning_rank = _to_int(dossier['nais_rang'])
|
614
|
# Pourra etre init à l'import des contacts après création du dossier
|
615
|
thirdparty_payer = False
|
616
|
begin_rights = None
|
617
|
end_rights = None
|
618
|
health_center = None
|
619
|
addresses = None
|
620
|
contact_comment = None
|
621
|
|
622
|
#PatientRecord
|
623
|
creator = creator
|
624
|
# Pourra etre init à l'import des contacts après création du dossier
|
625
|
nationality = None
|
626
|
paper_id = None
|
627
|
comment = dossier['infos']
|
628
|
pause = False
|
629
|
if _exist(dossier['blocage']):
|
630
|
pause = True
|
631
|
confidential = False
|
632
|
if _exist(dossier['non_communication_ecole']):
|
633
|
confidential = True
|
634
|
|
635
|
# Physiology and health data
|
636
|
size = _to_int(dossier['taille'])
|
637
|
weight = _to_int(dossier['poids'])
|
638
|
pregnancy_term = _to_int(dossier['terme'])
|
639
|
cranium_perimeter = None
|
640
|
chest_perimeter = None
|
641
|
apgar_score_one = _to_int(dossier['apgar_1'])
|
642
|
apgar_score_two = _to_int(dossier['apgar_5'])
|
643
|
|
644
|
|
645
|
# Inscription motive
|
646
|
# Up to now only used at the CAMSP
|
647
|
analysemotive = None
|
648
|
try:
|
649
|
analysemotive = AnalyseMotive.objects.get(id=_to_int(dossier['ins_motif']))
|
650
|
except:
|
651
|
pass
|
652
|
# Up to now only used at the CAMSP
|
653
|
familymotive = None
|
654
|
try:
|
655
|
familymotive = FamilyMotive.objects.get(id=_to_int(dossier['ins_motif_exprim']))
|
656
|
except:
|
657
|
pass
|
658
|
provenance = None
|
659
|
try:
|
660
|
provenance = Provenance.objects.get(old_id=_to_int(dossier['ins_provenance']), old_service=service.name)
|
661
|
except:
|
662
|
pass
|
663
|
# Up to now only used at the CAMSP
|
664
|
advicegiver = None
|
665
|
try:
|
666
|
advicegiver = AdviceGiver.objects.get(id=_to_int(dossier['con_qui']))
|
667
|
except:
|
668
|
pass
|
669
|
|
670
|
# Inscription motive
|
671
|
# Up to now only used at the CAMSP
|
672
|
outmotive = None
|
673
|
try:
|
674
|
outmotive = OutMotive.objects.get(id=_to_int(dossier['sor_motif']))
|
675
|
except:
|
676
|
pass
|
677
|
# Up to now only used at the CAMSP
|
678
|
outto = None
|
679
|
try:
|
680
|
outto = OutTo.objects.get(id=_to_int(dossier['sor_orientation']))
|
681
|
except:
|
682
|
pass
|
683
|
|
684
|
# Family
|
685
|
sibship_place = _to_int(dossier['nais_fratrie'])
|
686
|
nb_children_family = _to_int(dossier['nbr_enfants'])
|
687
|
parental_authority = None
|
688
|
try:
|
689
|
parental_authority = ParentalAuthorityType.objects.get(id=_to_int(dossier['autorite_parentale']))
|
690
|
except:
|
691
|
pass
|
692
|
# Up to now only used at the CAMSP
|
693
|
family_situation = None
|
694
|
try:
|
695
|
family_situation = FamilySituationType.objects.get(id=_to_int(dossier['situation_familiale']))
|
696
|
except:
|
697
|
pass
|
698
|
# Up to now only used at the CAMSP
|
699
|
child_custody = None
|
700
|
try:
|
701
|
child_custody = ParentalCustodyType.objects.get(id=_to_int(dossier['garde']))
|
702
|
except:
|
703
|
pass
|
704
|
|
705
|
rm_mother = get_rm(service, dossier['rm_mere'])
|
706
|
rm_father = get_rm(service, dossier['rm_pere'])
|
707
|
job_mother = get_job(service, dossier['prof_mere'])
|
708
|
job_father = get_job(service, dossier['prof_pere'])
|
709
|
family_comment = None
|
710
|
|
711
|
# Transport
|
712
|
transportcompany = None
|
713
|
try:
|
714
|
if service.name == 'CAMSP':
|
715
|
transportcompany = TransportCompany.objects.get(old_camsp_id=_to_int(dossier['transport']))
|
716
|
elif service.name == 'CMPP':
|
717
|
transportcompany = TransportCompany.objects.get(old_cmpp_id=_to_int(dossier['transport']))
|
718
|
elif service.name == 'SESSAD DYS':
|
719
|
transportcompany = TransportCompany.objects.get(old_sessad_dys_id=_to_int(dossier['transport']))
|
720
|
elif service.name == 'SESSAD TED':
|
721
|
transportcompany = TransportCompany.objects.get(old_sessad_ted_id=_to_int(dossier['transport']))
|
722
|
except:
|
723
|
pass
|
724
|
transporttype = None
|
725
|
try:
|
726
|
transporttype = TransportType.objects.get(id=_to_int(dossier['type_transport']))
|
727
|
except:
|
728
|
pass
|
729
|
|
730
|
# FollowUp
|
731
|
externaldoctor = None
|
732
|
try:
|
733
|
externaldoctor = ExternalTherapist.objects.get(old_id=_to_int(dossier['medecin_exterieur']), old_service=service.name)
|
734
|
except:
|
735
|
pass
|
736
|
externalintervener = None
|
737
|
try:
|
738
|
externalintervener = ExternalWorker.objects.get(old_id=_to_int(dossier['intervenant_exterieur']), old_service=service.name)
|
739
|
except:
|
740
|
pass
|
741
|
|
742
|
old_id = dossier['id']
|
743
|
old_old_id = dossier['ancien_numero']
|
744
|
|
745
|
|
746
|
patient, created = PatientRecord.objects.get_or_create(first_name = first_name,
|
747
|
last_name = last_name,
|
748
|
birthdate = birthdate,
|
749
|
twinning_rank = twinning_rank,
|
750
|
gender = gender,
|
751
|
display_name = display_name,
|
752
|
email = email,
|
753
|
phone = phone,
|
754
|
mobile = mobile,
|
755
|
contact_comment = contact_comment,
|
756
|
nationality = nationality,
|
757
|
paper_id = paper_id,
|
758
|
comment = comment,
|
759
|
pause = pause,
|
760
|
confidential = confidential,
|
761
|
size = size,
|
762
|
weight = weight,
|
763
|
pregnancy_term = pregnancy_term,
|
764
|
cranium_perimeter = cranium_perimeter,
|
765
|
chest_perimeter = chest_perimeter,
|
766
|
apgar_score_one = apgar_score_one,
|
767
|
apgar_score_two = apgar_score_two,
|
768
|
analysemotive = analysemotive,
|
769
|
familymotive = familymotive,
|
770
|
provenance = provenance,
|
771
|
advicegiver = advicegiver,
|
772
|
outmotive = outmotive,
|
773
|
outto = outto,
|
774
|
sibship_place = sibship_place,
|
775
|
nb_children_family = nb_children_family,
|
776
|
parental_authority = parental_authority,
|
777
|
family_situation = family_situation,
|
778
|
rm_mother = rm_mother,
|
779
|
rm_father = rm_father,
|
780
|
job_mother = job_mother,
|
781
|
job_father = job_father,
|
782
|
family_comment = family_comment,
|
783
|
child_custody = child_custody,
|
784
|
transportcompany = transportcompany,
|
785
|
transporttype = transporttype,
|
786
|
externaldoctor = externaldoctor,
|
787
|
externalintervener = externalintervener,
|
788
|
service=service,
|
789
|
creator=creator,
|
790
|
old_id = old_id,
|
791
|
old_old_id = old_old_id)
|
792
|
|
793
|
# if created:
|
794
|
# print 'Creation de%s' % patient
|
795
|
# else:
|
796
|
# print 'Patient %s existe' % patient
|
797
|
|
798
|
# Init states
|
799
|
if not fss:
|
800
|
print "Pas d'etat et le dossier patient %s (old_id) a ete cree!" % old_id
|
801
|
else:
|
802
|
fs = FileState(status=fss[0][0], author=creator, previous_state=None)
|
803
|
date_selected = fss[0][1]
|
804
|
fs.patient = patient
|
805
|
fs.date_selected = date_selected
|
806
|
fs.comment = fss[0][2]
|
807
|
fs.save()
|
808
|
patient.last_state = fs
|
809
|
patient.save()
|
810
|
if len(fss) > 1:
|
811
|
for status, date, comment in fss[1:]:
|
812
|
patient.set_state(status=status, author=creator, date_selected=date, comment=comment)
|
813
|
|
814
|
|
815
|
if old_id in mises_per_patient.keys():
|
816
|
for quotation in mises_per_patient[old_id]:
|
817
|
if quotation.axe == 1:
|
818
|
patient.mises_1.add(quotation)
|
819
|
elif quotation.axe == 2:
|
820
|
patient.mises_2.add(quotation)
|
821
|
elif quotation.axe == 3:
|
822
|
patient.mises_3.add(quotation)
|
823
|
else:
|
824
|
raise
|
825
|
|
826
|
if old_id in social_duration_per_patient.keys():
|
827
|
for social_duration in social_duration_per_patient[old_id]:
|
828
|
patient.socialisation_durations.add(social_duration)
|
829
|
|
830
|
for t_the in ('the_medecin', 'the_referent', 'the_therapeute'):
|
831
|
try:
|
832
|
therapist = None
|
833
|
if service.name == 'CAMSP':
|
834
|
therapist = Worker.objects.get(old_camsp_id=_to_int(dossier[t_the]))
|
835
|
elif service.name == 'CMPP':
|
836
|
therapist = Worker.objects.get(old_cmpp_id=_to_int(dossier[t_the]))
|
837
|
elif service.name == 'SESSAD DYS':
|
838
|
therapist = Worker.objects.get(old_sessad_dys_id=_to_int(dossier[t_the]))
|
839
|
elif service.name == 'SESSAD TED':
|
840
|
therapist = Worker.objects.get(old_sessad_ted_id=_to_int(dossier[t_the]))
|
841
|
patient.coordinators.add(therapist)
|
842
|
except:
|
843
|
pass
|
844
|
|
845
|
# Initialisation adresses et contacts
|
846
|
if old_id in adresses_per_patient.keys():
|
847
|
for adresse in adresses_per_patient[old_id]:
|
848
|
patient.addresses.add(adresse)
|
849
|
if old_id in contacts_per_patient.keys():
|
850
|
for contact in contacts_per_patient[old_id]:
|
851
|
if contact.last_name == patient.last_name \
|
852
|
and contact.first_name == patient.first_name:
|
853
|
# print "Le contact %s %s est le patient" % (contact.last_name, contact.first_name)
|
854
|
if not patient.birthdate:
|
855
|
patient.birthdate = contact.birthdate
|
856
|
patient.birthplace = contact.birthplace
|
857
|
patient.email = contact.email
|
858
|
patient.phone = contact.phone
|
859
|
patient.mobile = contact.mobile
|
860
|
patient.social_security_id = contact.social_security_id
|
861
|
patient.thirdparty_payer = contact.thirdparty_payer
|
862
|
patient.begin_rights = contact.begin_rights
|
863
|
patient.end_rights = contact.end_rights
|
864
|
patient.health_center = contact.health_center
|
865
|
patient.contact_comment = contact.contact_comment
|
866
|
patient.old_contact_id = contact.old_contact_id
|
867
|
patient.save()
|
868
|
contact.delete()
|
869
|
else:
|
870
|
patient.contacts.add(contact)
|
871
|
|
872
|
policyholder = None
|
873
|
health_center = None
|
874
|
other_health_center = None
|
875
|
if old_id in tables_data['pcs'].keys():
|
876
|
pcs = tables_data['pcs'][old_id]
|
877
|
j = len(pcs)-1
|
878
|
found = False
|
879
|
last_pc = None
|
880
|
while not found and j >= 0:
|
881
|
last_pc = pcs[j]
|
882
|
if 'contact_id' in last_pc.keys():
|
883
|
found = True
|
884
|
j -= 1
|
885
|
if not found:
|
886
|
writer2.writerow([dossier[c].encode('utf-8') for c in d_cols] + [service.name, 'Oui', "Pas de pc, le patient est l'assure sans caisse"])
|
887
|
# print "Pas de d'assure pour le patient %s" % old_id
|
888
|
# print "Le patient sera l'assure"
|
889
|
else:
|
890
|
try:
|
891
|
caisse = None
|
892
|
centre = None
|
893
|
lg = None
|
894
|
policyholder = patient.contacts.get(old_contact_id=_to_int(last_pc['contact_id']))
|
895
|
if last_pc['caisse_id'] in tables_data['caisses'].keys():
|
896
|
lg = tables_data['caisses'][last_pc['caisse_id']]['tp']
|
897
|
if len(lg) < 2:
|
898
|
lg = ['0', lg]
|
899
|
lg = ''.join(lg)
|
900
|
caisse = tables_data['caisses'][last_pc['caisse_id']]['caisse']
|
901
|
while len(caisse) < 3:
|
902
|
caisse = ['0', caisse]
|
903
|
caisse = ''.join(caisse)
|
904
|
centre = tables_data['caisses'][last_pc['caisse_id']]['centre']
|
905
|
while len(centre) < 4:
|
906
|
centre = ['0', centre]
|
907
|
centre = ''.join(centre)
|
908
|
if lg and caisse:
|
909
|
health_centers = HealthCenter.objects.filter(large_regime__code=lg, health_fund=caisse)
|
910
|
if health_centers and len(health_centers.all()) == 1:
|
911
|
health_center = health_centers[0]
|
912
|
if last_pc['centre']:
|
913
|
while len(last_pc['centre']) < 4:
|
914
|
last_pc['centre'] = ['0', last_pc['centre']]
|
915
|
last_pc['centre'] = ''.join(last_pc['centre'])
|
916
|
other_health_center = last_pc['centre']
|
917
|
elif health_centers and len(health_centers.all()) > 1:
|
918
|
health_centers = None
|
919
|
if last_pc['centre']:
|
920
|
while len(last_pc['centre']) < 4:
|
921
|
last_pc['centre'] = ['0', last_pc['centre']]
|
922
|
last_pc['centre'] = ''.join(last_pc['centre'])
|
923
|
# print "centre 1 %s" % last_pc['centre']
|
924
|
health_centers = HealthCenter.objects.filter(large_regime__code=lg, health_fund=caisse,
|
925
|
code = last_pc['centre'])
|
926
|
elif centre:
|
927
|
# print "centre 2 %s" % centre
|
928
|
health_centers = HealthCenter.objects.filter(large_regime__code=lg, health_fund=caisse,
|
929
|
code = centre)
|
930
|
if health_centers and len(health_centers.all()) == 1:
|
931
|
health_center = health_centers[0]
|
932
|
elif health_centers:
|
933
|
# print "Plusieurs caisses avec le meme centre, patient %s" % old_id
|
934
|
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
|
935
|
[service.name, '', "Plusieurs caisses avec le meme centre, patient %s" % old_id])
|
936
|
else:
|
937
|
# print "Caisse non determinee par code centre, patient %s" % old_id
|
938
|
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
|
939
|
[service.name, '', "Caisse non determinee par code centre, patient %s" % old_id])
|
940
|
else:
|
941
|
# print 'Caisse non trouvee avec ce numero de caisse et grand regime, patient %s' % old_id
|
942
|
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
|
943
|
[service.name, '', 'Caisse non trouvee avec ce numero de caisse et grand regime, patient %s' % old_id])
|
944
|
else:
|
945
|
# print 'Infos manquantes dans fichiers des caisses, patient %s' % old_id
|
946
|
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
|
947
|
[service.name, '', 'Infos manquantes dans fichiers des caisses, patient %s' % old_id])
|
948
|
else:
|
949
|
# print 'Pas de caisse dans le fichiers caisse avec l id %s, patient %s' % (last_pc['caisse_id'], old_id)
|
950
|
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
|
951
|
[service.name, '', 'Pas de caisse dans le fichier caisse avec l id %s, patient %s' % (last_pc['caisse_id'], old_id)])
|
952
|
except:
|
953
|
# print "Pas de contact avec id %s, patient %s" % (last_pc['contact_id'], old_id)
|
954
|
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
|
955
|
[service.name, '', "Pas d'assuré existant, le patient est choisi."])
|
956
|
|
957
|
else:
|
958
|
writer2.writerow([dossier[c].encode('utf-8') for c in d_cols] + \
|
959
|
[service.name, 'Oui', "Pas de pc, le patient est l'assure sans caisse"])
|
960
|
# print "Pas de pc pour le patient %s" % old_id
|
961
|
# print "Le patient sera l'assure"
|
962
|
if not policyholder:
|
963
|
policyholder = patient.patientcontact
|
964
|
policyholder.health_center = health_center
|
965
|
policyholder.other_health_center = other_health_center
|
966
|
policyholder.save()
|
967
|
patient.policyholder = policyholder
|
968
|
patient.save()
|
969
|
|
970
|
|
971
|
# On ne pass plus par le clean du contact form, du coup, pas moyen de modifier le health center!
|
972
|
|
973
|
# Faut-il gére un code de gestion ?
|
974
|
|
975
|
|
976
|
#Etat des dossiers
|
977
|
|
978
|
# patient.policyholder soit le contact, d'il n'y en a qu'un
|
979
|
# au cmmp, cf la pc
|
980
|
|
981
|
# Dossier en pause facturation! champs pause sur le dossier OK
|
982
|
# si aucun contact, ou aucun contact avec un Nir valide!
|
983
|
|
984
|
#Tiers-payant ? healthcenter ?
|
985
|
|
986
|
# Notifications au sessad, il n'y en a pas!
|
987
|
|
988
|
# i += 1
|
989
|
# print 'Fin de traitement pour le dossier %s' % patient
|
990
|
# if i >= 10:
|
991
|
# break
|
992
|
i -= 1
|
993
|
if not (i % 10):
|
994
|
sys.stdout.write('%d' %i)
|
995
|
else:
|
996
|
sys.stdout.write('.')
|
997
|
sys.stdout.flush()
|
998
|
|
999
|
print "<-- Terminé"
|
1000
|
print "====== Fin à %s ======" % str(datetime.today())
|
1001
|
|
1002
|
#Travail manuel pour secreatires
|
1003
|
# mdph_requests = models.ManyToManyField('ressources.MDPHRequest',
|
1004
|
# mdph_responses = models.ManyToManyField('ressources.MDPHResponse',
|
1005
|
|
1006
|
|
1007
|
# policyholder = None
|
1008
|
# contacts = None
|
1009
|
|
1010
|
|
1011
|
if __name__ == "__main__":
|
1012
|
import_dossiers_phase_1()
|