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, date
|
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
|
import logging
|
17
|
logger = logging.getLogger('import_pcs')
|
18
|
log_handler = logging.FileHandler("./scripts/import_pcs.log")
|
19
|
log_handler.setLevel(logging.DEBUG)
|
20
|
log_handler.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)-8s %(name)s.%(message)s'))
|
21
|
logger.addHandler(log_handler)
|
22
|
|
23
|
#log_file = "./scripts/import_pcs.log"
|
24
|
#FORMAT = '[%(asctime)s] %(levelname)-8s %(name)s.%(message)s'
|
25
|
#logger.basicConfig(filename=log_file,level=logger.DEBUG, format=FORMAT, filemode = 'a')
|
26
|
|
27
|
from django.contrib.auth.models import User
|
28
|
from django.db import transaction
|
29
|
|
30
|
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
|
31
|
|
32
|
from calebasse.agenda.models import Event, EventType
|
33
|
from calebasse.dossiers.models import PatientRecord, Status, FileState, PatientAddress, PatientContact, \
|
34
|
CmppHealthCareDiagnostic, CmppHealthCareTreatment
|
35
|
|
36
|
from calebasse.ressources.models import Service
|
37
|
from calebasse.personnes.models import Worker, Holiday, ExternalWorker, ExternalTherapist
|
38
|
from calebasse.ressources.models import (WorkerType, ParentalAuthorityType, ParentalCustodyType,
|
39
|
FamilySituationType, TransportType, TransportCompany, Provenance, AnalyseMotive, FamilyMotive,
|
40
|
CodeCFTMEA, SocialisationDuration, School, SchoolLevel, OutMotive, OutTo, AdviceGiver,
|
41
|
MaritalStatusType, Job, PatientRelatedLink, HealthCenter)
|
42
|
from calebasse.actes.models import Act
|
43
|
|
44
|
# Configuration
|
45
|
db_path = "./scripts/20130104-213225"
|
46
|
|
47
|
map_cs = {}
|
48
|
map_cs['CAMSP'] = {
|
49
|
'1': 'ACT_DOUBLE',
|
50
|
'2': 'ABS_NON_EXC',
|
51
|
'3': 'ABS_EXC',
|
52
|
'4': 'ABS_INTER',
|
53
|
'5': 'ACT_LOST',
|
54
|
'6': 'ANNUL_NOUS',
|
55
|
'7': 'ANNUL_FAMILLE',
|
56
|
'8': 'ENF_HOSP',
|
57
|
'9': 'ACT_LOST',
|
58
|
'10': 'ACT_LOST',
|
59
|
'11': 'ACT_LOST',
|
60
|
'12': 'REPORTE'
|
61
|
}
|
62
|
|
63
|
map_cs['CMPP'] = {
|
64
|
'1': 'ACT_DOUBLE',
|
65
|
'2': 'ABS_NON_EXC',
|
66
|
'3': 'ABS_EXC',
|
67
|
'4': 'ABS_INTER',
|
68
|
'5': 'ACT_LOST',
|
69
|
'6': 'ANNUL_NOUS',
|
70
|
'7': 'ANNUL_FAMILLE',
|
71
|
'8': 'ABS_ESS_PPS',
|
72
|
'9': 'ACT_LOST',
|
73
|
'10': 'ACT_LOST',
|
74
|
'11': 'ACT_LOST',
|
75
|
'12': 'REPORTE'
|
76
|
}
|
77
|
|
78
|
map_cs['SESSAD DYS'] = {
|
79
|
'1': 'ACT_DOUBLE',
|
80
|
'2': 'ABS_NON_EXC',
|
81
|
'3': 'ABS_EXC',
|
82
|
'4': 'ABS_INTER',
|
83
|
'5': 'ACT_LOST',
|
84
|
'6': 'ANNUL_NOUS',
|
85
|
'7': 'ANNUL_FAMILLE',
|
86
|
'8': 'ABS_ESS_PPS',
|
87
|
'9': 'ACT_LOST',
|
88
|
'10': 'ACT_LOST',
|
89
|
'11': 'REPORTE'
|
90
|
}
|
91
|
|
92
|
|
93
|
map_cs['SESSAD TED'] = {
|
94
|
'1': 'ACT_DOUBLE',
|
95
|
'2': 'ABS_NON_EXC',
|
96
|
'3': 'ABS_EXC',
|
97
|
'4': 'ACT_LOST',
|
98
|
'5': 'ABS_INTER',
|
99
|
'6': 'ANNUL_NOUS',
|
100
|
'7': 'ANNUL_FAMILLE',
|
101
|
'8': 'REPORTE'
|
102
|
}
|
103
|
|
104
|
def _exist(str):
|
105
|
if str and str != "" and str != '0':
|
106
|
return True
|
107
|
return False
|
108
|
|
109
|
def treat_name(name):
|
110
|
res = ''
|
111
|
for p in name.split():
|
112
|
res += p[0].upper()+p[1:].lower()
|
113
|
res += ' '
|
114
|
return res[:-1]
|
115
|
|
116
|
def _to_date(str_date):
|
117
|
if not str_date:
|
118
|
return None
|
119
|
return datetime.strptime(str_date[:-13], "%Y-%m-%d")
|
120
|
|
121
|
def _to_int(str_int):
|
122
|
if not str_int:
|
123
|
return None
|
124
|
return int(str_int)
|
125
|
|
126
|
def _get_dict(cols, line):
|
127
|
""""""
|
128
|
res = {}
|
129
|
for i, data in enumerate(line):
|
130
|
res[cols[i]] = data.decode('utf-8')
|
131
|
return res
|
132
|
|
133
|
tables_data = {}
|
134
|
|
135
|
map_rm_cmpp = [1, 3, 2, 8, 6, 4]
|
136
|
|
137
|
def get_rm(service, val):
|
138
|
old_id_rm = _to_int(val)
|
139
|
if old_id_rm < 1 or 'SESSAD' in service.name:
|
140
|
return None
|
141
|
if service.name == 'CMPP':
|
142
|
old_id_rm = map_rm_cmpp[old_id_rm - 1]
|
143
|
try:
|
144
|
return MaritalStatusType.objects.get(id=old_id_rm)
|
145
|
except:
|
146
|
return None
|
147
|
|
148
|
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]
|
149
|
|
150
|
# CMPP à 25 = Rien
|
151
|
def get_job(service, val):
|
152
|
old_id_job = _to_int(val)
|
153
|
if old_id_job < 1:
|
154
|
return None
|
155
|
if service.name == 'CAMSP' and old_id_job == 26:
|
156
|
return None
|
157
|
if service.name == 'CAMSP':
|
158
|
try:
|
159
|
old_id_job = map_job_camsp[old_id_job - 1]
|
160
|
except:
|
161
|
print 'Old id job out of range: %d' % old_id_job
|
162
|
try:
|
163
|
return Job.objects.get(id=old_id_job)
|
164
|
except:
|
165
|
return None
|
166
|
|
167
|
def extract_phone(val):
|
168
|
if not val or val == '' or val == '0':
|
169
|
return None
|
170
|
s = ''.join([c for c in val if c.isdigit()])
|
171
|
return s[:11]
|
172
|
|
173
|
def get_nir(nir, key, writer, line, service):
|
174
|
if not nir:
|
175
|
return None
|
176
|
if len(nir) != 13:
|
177
|
return -1
|
178
|
if key:
|
179
|
minus = 0
|
180
|
# Corsica dept 2A et 2B
|
181
|
if nir[6] in ('A', 'a'):
|
182
|
nir = [c for c in nir]
|
183
|
nir[6] = '0'
|
184
|
nir = ''.join(nir)
|
185
|
minus = 1000000
|
186
|
elif nir[6] in ('B', 'b'):
|
187
|
nir = [c for c in nir]
|
188
|
nir[6] = '0'
|
189
|
nir = ''.join(nir)
|
190
|
minus = 2000000
|
191
|
try:
|
192
|
nir = int(nir) - minus
|
193
|
good_key = 97 - (nir % 97)
|
194
|
key = int(key)
|
195
|
if key != good_key:
|
196
|
msg = 'Clé incorrect %s pour %s' % (str(key), str(nir))
|
197
|
writer.writerow(line + [service.name, msg])
|
198
|
except:
|
199
|
pass
|
200
|
return nir
|
201
|
|
202
|
|
203
|
#@transaction.commit_manually
|
204
|
def import_dossiers_phase_1():
|
205
|
status_accueil = Status.objects.filter(type="ACCUEIL")[0]
|
206
|
status_fin_accueil = Status.objects.filter(type="FIN_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
|
db = "F_ST_ETIENNE_CMPP"
|
216
|
service = Service.objects.get(name="CMPP")
|
217
|
|
218
|
pcs_d = {}
|
219
|
|
220
|
msg = "Chargement des actes..."
|
221
|
logger.info("%s" % msg)
|
222
|
csvfile = open(os.path.join(db_path, db, 'actes.csv'), 'rb')
|
223
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
224
|
pc_cols = csvlines.next()
|
225
|
tables_data['actes'] = {}
|
226
|
tables_data['actes']['nf'] = []
|
227
|
for line in csvlines:
|
228
|
data = _get_dict(pc_cols, line)
|
229
|
if _exist(line[6]):
|
230
|
if line[6] in tables_data['actes'].keys():
|
231
|
tables_data['actes'][line[6]].append(data)
|
232
|
else:
|
233
|
tables_data['actes'][line[6]] = [data]
|
234
|
else:
|
235
|
tables_data['actes']['nf'].append(data)
|
236
|
csvfile.close()
|
237
|
msg = "Terminé : dictionnaire avec clé facture prêt"
|
238
|
logger.info("%s" % msg)
|
239
|
|
240
|
msg = "Chargement des factures..."
|
241
|
logger.info("%s" % msg)
|
242
|
csvfile = open(os.path.join(db_path, db, 'factures.csv'), 'rb')
|
243
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
244
|
pc_cols = csvlines.next()
|
245
|
tables_data['factures'] = {}
|
246
|
for line in csvlines:
|
247
|
data = _get_dict(pc_cols, line)
|
248
|
if line[7] in tables_data['factures'].keys():
|
249
|
tables_data['factures'][line[7]].append(data)
|
250
|
else:
|
251
|
tables_data['factures'][line[7]] = [data]
|
252
|
csvfile.close()
|
253
|
msg = "Terminé : dictionnaire avec clé période de pc prêt"
|
254
|
logger.info("%s" % msg)
|
255
|
|
256
|
msg = "Lecture de la table des dossiers..."
|
257
|
logger.info("%s" % msg)
|
258
|
csvfile = open(os.path.join(db_path, db, 'dossiers.csv'), 'rb')
|
259
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
260
|
d_cols = csvlines.next()
|
261
|
tables_data['dossiers'] = {}
|
262
|
for line in csvlines:
|
263
|
#Au moins nom et prénom
|
264
|
data = _get_dict(d_cols, line)
|
265
|
tables_data['dossiers'][line[0]] = data
|
266
|
csvfile.close()
|
267
|
msg = "Terminé"
|
268
|
logger.info("%s" % msg)
|
269
|
|
270
|
msg = "Chargement des prise en charge..."
|
271
|
logger.info("%s" % msg)
|
272
|
csvfile = open(os.path.join(db_path, db, 'pc.csv'), 'rb')
|
273
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
274
|
pc_cols = csvlines.next()
|
275
|
tables_data['pcs'] = {}
|
276
|
i = 0
|
277
|
for line in csvlines:
|
278
|
data = _get_dict(pc_cols, line)
|
279
|
pcs_d[line[0]] = data
|
280
|
if line[1] in tables_data['pcs'].keys():
|
281
|
tables_data['pcs'][line[1]].append(data)
|
282
|
else:
|
283
|
tables_data['pcs'][line[1]] = [data]
|
284
|
i += 1
|
285
|
csvfile.close()
|
286
|
msg = "Terminé : dictionnaire avec clé patient prêt"
|
287
|
logger.info("%s" % msg)
|
288
|
|
289
|
msg = "Chargement des periodes prise en charge..."
|
290
|
logger.info("%s" % msg)
|
291
|
csvfile = open(os.path.join(db_path, db, 'periodes_pc.csv'), 'rb')
|
292
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
293
|
pc_cols = csvlines.next()
|
294
|
tables_data['periodes_pcs'] = {}
|
295
|
j = 0
|
296
|
for line in csvlines:
|
297
|
data = _get_dict(pc_cols, line)
|
298
|
if line[1] in tables_data['periodes_pcs'].keys():
|
299
|
tables_data['periodes_pcs'][line[1]].append(data)
|
300
|
else:
|
301
|
tables_data['periodes_pcs'][line[1]] = [data]
|
302
|
j += 1
|
303
|
csvfile.close()
|
304
|
msg = "Terminé : dictionnaire avec clé prise en charge prêt"
|
305
|
logger.info("%s" % msg)
|
306
|
|
307
|
msg = "Nombre de patients concernés par une prise en charge: %d" % len(tables_data['pcs'].keys())
|
308
|
logger.info("%s" % msg)
|
309
|
msg = "Nombre de prises en charges à traiter : %d" % i
|
310
|
logger.info("%s" % msg)
|
311
|
k = 0
|
312
|
l = 0
|
313
|
for dossier_id, pcs in tables_data['pcs'].items():
|
314
|
if len(pcs) > 1:
|
315
|
k += 1
|
316
|
else:
|
317
|
if pcs[0]['genre_pc'] != '1':
|
318
|
l += 1
|
319
|
msg = "Nombre de patients qui ont plus d'une prise en charge : %d" % k
|
320
|
logger.info("%s" % msg)
|
321
|
msg = "Nombre de patients qui n'ont qu'une prise en charge mais qui n'est pas de diagnostic diag : %d" % l
|
322
|
logger.info("%s" % msg)
|
323
|
msg = "Nombre de periodes pour toutes les prises en charge : %d" % j
|
324
|
logger.info("%s" % msg)
|
325
|
k = 0
|
326
|
l = 0
|
327
|
m = 0
|
328
|
for pc, periodes in tables_data['periodes_pcs'].items():
|
329
|
if len(periodes) > 1:
|
330
|
k += 1
|
331
|
if pcs_d[pc]['genre_pc'] != '1':
|
332
|
l += 1
|
333
|
msg = "Nombre de prises en charge qui on plus d'une periode : %d" % k
|
334
|
logger.info("%s" % msg)
|
335
|
msg = "Nombre de prises en charge diagnostic qui on plus d'une periode : %d" % (k - l)
|
336
|
logger.info("%s" % msg)
|
337
|
msg = "Nombre de prises en charge traitement qui on plus d'une periode : %d" % l
|
338
|
logger.info("%s" % msg)
|
339
|
|
340
|
j = 0
|
341
|
k = 0
|
342
|
nb_actes_diag = [0 for i in range(20)]
|
343
|
nb_actes_trait = [0 for i in range(100)]
|
344
|
periode_ss_fact = []
|
345
|
facture_ss_actes = []
|
346
|
histo = {}
|
347
|
facturations = {}
|
348
|
total_factures = []
|
349
|
for dossier_id, pcs in tables_data['pcs'].items():
|
350
|
histo[dossier_id] = []
|
351
|
for pc in pcs:
|
352
|
t = pc['genre_pc']
|
353
|
for periode in tables_data['periodes_pcs'][pc['id']]:
|
354
|
if not _exist(periode['date_debut']):# or _to_date(periode['date_debut']) < datetime(year=2002, month=1, day=1):
|
355
|
continue
|
356
|
my_pc = {}
|
357
|
my_pc['type'] = t
|
358
|
my_pc['periode'] = periode
|
359
|
# Il n'y qu'au cmpp où il y a des factures
|
360
|
# pour le sessad dy, on ajoute juste les periodes pour indiquer les notifications
|
361
|
# Dans les autres services, rien ?
|
362
|
factures = tables_data['factures'].get(periode['ppc_id'], None)
|
363
|
my_pc['factures'] = []
|
364
|
my_pc['actes'] = []
|
365
|
if factures:
|
366
|
total_factures += [f['id'] for f in factures]
|
367
|
for facture in factures:
|
368
|
if facture['pc_id'] != pc['id']:
|
369
|
print "%s != %s" % (facture['pc_id'], pc['id'])
|
370
|
num = facture['numero']
|
371
|
actes = tables_data['actes'].get(num, None)
|
372
|
if actes:
|
373
|
my_pc['factures'].append((facture, actes))
|
374
|
my_pc['actes'] += actes
|
375
|
fact_num = facture['numero'][0:3]
|
376
|
if not fact_num in facturations:
|
377
|
facturations[fact_num] = {}
|
378
|
facturations[fact_num]['factures'] = []
|
379
|
facturations[fact_num]['actes'] = []
|
380
|
facturations[fact_num]['factures'].append(facture)
|
381
|
facturations[fact_num]['actes'] += actes
|
382
|
else:
|
383
|
facture_ss_actes.append(facture['id'])
|
384
|
else:
|
385
|
periode_ss_fact.append(periode)
|
386
|
if t == '1':
|
387
|
k += 1
|
388
|
if t == '1':
|
389
|
nb_actes_diag[len(my_pc['actes'])] += 1
|
390
|
else:
|
391
|
nb_actes_trait[len(my_pc['actes'])] += 1
|
392
|
histo[dossier_id].append(my_pc)
|
393
|
j += len(my_pc['actes'])
|
394
|
msg = "Nombre de factures : %d" % len(total_factures)
|
395
|
logger.info("%s" % msg)
|
396
|
diff = len(total_factures) - len(set(total_factures))
|
397
|
if diff > 0:
|
398
|
msg = "Il y a des factures en doubles : %d" % diff
|
399
|
logger.warn("%s" % msg)
|
400
|
msg = "Nombre d'actes : %d" % j
|
401
|
logger.info("%s" % msg)
|
402
|
# Ca arrive surtout avant car ajout periode auto sans qu'il y ait de facturation derriere
|
403
|
msg = "Periodes sans factures, donc sans actes : %d" % len(periode_ss_fact)
|
404
|
logger.info("%s" % msg)
|
405
|
msg = "Periodes sans factures de type diagnostic : %d" % k
|
406
|
logger.info("%s" % msg)
|
407
|
msg = "Periodes sans factures de type traitraitement : %d" % (len(periode_ss_fact) - k)
|
408
|
logger.info("%s" % msg)
|
409
|
# Ca arrive aussi
|
410
|
msg = "Factures sans actes : %d %s" % (len(facture_ss_actes), str(facture_ss_actes))
|
411
|
logger.warn("%s" % msg)
|
412
|
|
413
|
msg = "Nombre d'actes par prises en charge de diagnostique :"
|
414
|
logger.info("%s" % msg)
|
415
|
i = 0
|
416
|
for val in nb_actes_diag:
|
417
|
msg = "%d : %d" % (i, val)
|
418
|
logger.info("%s" % msg)
|
419
|
i += 1
|
420
|
msg = "Nombre d'actes par prises en charge de traitement :"
|
421
|
logger.info("%s" % msg)
|
422
|
i = 0
|
423
|
for val in nb_actes_trait:
|
424
|
msg = "%d : %d" % (i, val)
|
425
|
logger.info("%s" % msg)
|
426
|
i += 1
|
427
|
|
428
|
for num, values in facturations.items():
|
429
|
msg = "Nombre de facturations : %s" % num
|
430
|
logger.info("%s" % msg)
|
431
|
msg = "Nombre de factures (hors factures sans actes) : %d" % len(values['factures'])
|
432
|
logger.info("%s" % msg)
|
433
|
msg = "Nombre d'actes : %d" % len(values['actes'])
|
434
|
logger.info("%s" % msg)
|
435
|
|
436
|
author = User.objects.get(pk=1)
|
437
|
|
438
|
msg = "Suppression de toutes les prises en charge existante dans calebasse..."
|
439
|
logger.info("%s" % msg)
|
440
|
CmppHealthCareDiagnostic.objects.all().delete()
|
441
|
CmppHealthCareTreatment.objects.all().delete()
|
442
|
msg = "Terminé"
|
443
|
logger.info("%s" % msg)
|
444
|
# Creation des Healthcare
|
445
|
HcDiags = []
|
446
|
HcTraits = []
|
447
|
msg = "Création des prises en charge..."
|
448
|
logger.info("%s" % msg)
|
449
|
for patient_id, pcs in histo.items():
|
450
|
patient = None
|
451
|
try:
|
452
|
patient = PatientRecord.objects.get(old_id=patient_id, service=service)
|
453
|
except:
|
454
|
msg = "Patient présent dans la table des prises en charge mais pas dans calebasse"
|
455
|
logger.error("%s" % msg)
|
456
|
msg = "Anciens ID : %s - Nom : %s - Prénom : %s" % (patient_id, str(tables_data['dossiers'][patient_id]['nom']), str(tables_data['dossiers'][patient_id]['prenom']))
|
457
|
logger.error("%s" % msg)
|
458
|
continue
|
459
|
for pc in pcs:
|
460
|
start_date = _to_date(pc['periode']['date_debut'])
|
461
|
request_date = _to_date(pc['periode']['date_demande'])
|
462
|
agree_date = _to_date(pc['periode']['date_accord'])
|
463
|
insist_date = _to_date(pc['periode']['date_relance'])
|
464
|
act_number = _to_int(pc['periode']['nbr_seances']) or 0
|
465
|
if pc['type'] == '1':
|
466
|
# if act_number != 6:
|
467
|
# print "PC diag pour %d avec nombre d'acte pris en charge de %d" % (patient.id, act_number)
|
468
|
hc = CmppHealthCareDiagnostic(start_date=start_date,
|
469
|
request_date=request_date,
|
470
|
agree_date=agree_date,
|
471
|
insist_date=insist_date,
|
472
|
patient=patient,
|
473
|
act_number=act_number,
|
474
|
author=author)
|
475
|
HcDiags.append(hc)
|
476
|
else:
|
477
|
# if act_number != 30:
|
478
|
# print "PC diag pour %d avec nombre d'acte pris en charge de %d" % (patient.id, act_number)
|
479
|
hc = CmppHealthCareTreatment(start_date=start_date,
|
480
|
request_date=request_date,
|
481
|
agree_date=agree_date,
|
482
|
insist_date=insist_date,
|
483
|
patient=patient,
|
484
|
act_number=act_number,
|
485
|
author=author)
|
486
|
HcTraits.append(hc)
|
487
|
hc.save()
|
488
|
pc['hc'] = hc
|
489
|
msg = "Création des prises en charge terminé"
|
490
|
logger.info("%s" % msg)
|
491
|
#CmppHealthCareDiagnostic.objects.bulk_create(HcDiags)
|
492
|
#CmppHealthCareTreatment.objects.bulk_create(HcTraits)
|
493
|
# Association des actes au healthcare
|
494
|
|
495
|
msg = "Association des actes dans calebasse aux prises en charge..."
|
496
|
logger.info("%s" % msg)
|
497
|
i = 0
|
498
|
j = 0
|
499
|
for patient_id, pcs in histo.items():
|
500
|
patient = None
|
501
|
try:
|
502
|
patient = PatientRecord.objects.get(old_id=patient_id, service=service)
|
503
|
except:
|
504
|
# print 'Patient %s non trouve (2)' % patient_id
|
505
|
continue
|
506
|
for pc in pcs:
|
507
|
hc = pc['hc']
|
508
|
for act in pc['actes']:
|
509
|
a = None
|
510
|
try:
|
511
|
a = Act.objects.get(old_id=act['id'], patient__service=service)
|
512
|
except ObjectDoesNotExist:
|
513
|
msg = "Acte pointé par une facture avec ancien ID %s non trouvé" % str(act['id'])
|
514
|
logger.error("%s" % msg)
|
515
|
i += 1
|
516
|
continue
|
517
|
except MultipleObjectsReturned:
|
518
|
msg = "Acte pointé par une facture avec ancien ID %s existe plusieurs fois" % str(act['id'])
|
519
|
logger.error("%s" % msg)
|
520
|
i += 1
|
521
|
continue
|
522
|
except Exception, e:
|
523
|
msg = "Acte pointé par une facture avec ancien ID %s lève %s" % (str(act['id']), str(e))
|
524
|
logger.error("%s" % msg)
|
525
|
i += 1
|
526
|
continue
|
527
|
if not a.is_billed:
|
528
|
msg = "Acte trouvé et pris en charge mais non marqué facturé dans calebasse, marquage facturé (ID acte calebasse : %d)" % a.id
|
529
|
logger.warn("%s" % msg)
|
530
|
a.is_billed = True
|
531
|
a.healthcare = hc
|
532
|
a.save()
|
533
|
j += 1
|
534
|
msg = "Actes non trouvés : %d" % i
|
535
|
logger.info("%s" % msg)
|
536
|
msg = "Actes facturés chez Faure et réimputés dans calebasse aux prises en charge : %d" % j
|
537
|
logger.info("%s" % msg)
|
538
|
# Historique des dossiers, Automatic switch state ? Automated hc creation ?
|
539
|
|
540
|
csvfile = open(os.path.join(db_path, db, 'dossiers.csv'), 'rb')
|
541
|
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
|
542
|
d_cols = csvlines.next()
|
543
|
tables_data['dossiers'] = []
|
544
|
for line in csvlines:
|
545
|
data = _get_dict(d_cols, line)
|
546
|
tables_data['dossiers'].append(data)
|
547
|
csvfile.close()
|
548
|
|
549
|
date_accueil = None
|
550
|
date_diagnostic = None
|
551
|
date_inscription = None
|
552
|
date_clos = None
|
553
|
date_retour = None
|
554
|
|
555
|
msg = "Suppresion des états des dossiers dans calebasse qui ont été importés."
|
556
|
logger.info("%s" % msg)
|
557
|
FileState.objects.filter(patient__service=service, patient__old_id__isnull=False).delete()
|
558
|
|
559
|
#transaction.commit()
|
560
|
|
561
|
msg = "Création de l'historique d'état des dossiers patients"
|
562
|
logger.info("%s" % msg)
|
563
|
for dossier in tables_data['dossiers']:
|
564
|
fss = []
|
565
|
patient = None
|
566
|
try:
|
567
|
patient = PatientRecord.objects.get(old_id=dossier['id'], service=service)
|
568
|
except:
|
569
|
msg = "Patient présent dans la table des dossiers mais pas dans calebasse"
|
570
|
logger.error("%s" % msg)
|
571
|
msg = "Anciens ID : %s - Nom : %s - Prénom : %s" % (str(dossier['id']), str(dossier['nom'].encode('utf-8')), str(dossier['prenom'].encode('utf-8')))
|
572
|
logger.error("%s" % msg)
|
573
|
continue
|
574
|
date_accueil = _to_date(dossier['con_date'])
|
575
|
date_inscription = _to_date(dossier['ins_date'])
|
576
|
date_clos = _to_date(dossier['sor_date'])
|
577
|
date_retour = _to_date(dossier['ret_date'])
|
578
|
|
579
|
# La vrai date d'inscription c'est le premier acte facturé
|
580
|
# donc date_inscription devrait être égale
|
581
|
# verification
|
582
|
try:
|
583
|
real_date_inscription = patient.act_set.filter(is_billed=True).order_by('date')[0].date
|
584
|
real_date_inscription = datetime(year=real_date_inscription.year,
|
585
|
month=real_date_inscription.month, day=real_date_inscription.day)
|
586
|
except Exception, e:
|
587
|
pass
|
588
|
# print "Patient %s jamais facture, exception %s" % (dossier['id'], str(e))
|
589
|
else:
|
590
|
if date_inscription and real_date_inscription != date_inscription:
|
591
|
pass
|
592
|
# print "La date d'inscription est differente du premier acte facture pour %s" % dossier['id']
|
593
|
elif not date_inscription:
|
594
|
msg = "Pas de date d'inscription, on prend le premier acte pour %s - %s %s " % (str(dossier['id'].encode('utf-8')), str(dossier['nom'].encode('utf-8')), str(dossier['prenom'].encode('utf-8')))
|
595
|
logger.warn("%s" % msg)
|
596
|
date_inscription = real_date_inscription
|
597
|
|
598
|
if (date_accueil and not date_inscription) or (date_accueil and date_inscription and date_accueil < date_inscription):
|
599
|
fss.append((status_accueil, date_accueil, None))
|
600
|
|
601
|
if date_clos :
|
602
|
if not date_inscription:
|
603
|
msg = "Dossier clos sans avoir été inscrit : %s - %s %s " % (str(dossier['id']), str(dossier['nom'].encode('utf-8')), str(dossier['prenom'].encode('utf-8')))
|
604
|
logger.info("%s" % msg)
|
605
|
if date_inscription and date_clos < date_inscription:
|
606
|
msg = "Dossier %s - %s %s avec une date de clôture antérieure à la date d'inscription, on le clos sans inscription." % (str(dossier['id']), str(dossier['nom'].encode('utf-8')), str(dossier['prenom'].encode('utf-8')))
|
607
|
logger.error("%s" % msg)
|
608
|
date_inscription = None
|
609
|
|
610
|
# Historique par les actes
|
611
|
history = []
|
612
|
d = True
|
613
|
for act in patient.act_set.filter(is_billed=True).order_by('date'):
|
614
|
tag = act.get_hc_tag()
|
615
|
if tag and 'D' in tag:
|
616
|
# print tag
|
617
|
if not history or not d:
|
618
|
history.append(('D', act.date))
|
619
|
d = True
|
620
|
else:
|
621
|
# if not tag:
|
622
|
# print 'Patient %d: Act facture %d sans pc associee, traitement.' % (patient.id, act.id)
|
623
|
# else:
|
624
|
# print tag
|
625
|
if d:
|
626
|
history.append(('T', act.date))
|
627
|
d = False
|
628
|
|
629
|
if not history:
|
630
|
if date_inscription:
|
631
|
fss.append((status_diagnostic, date_inscription, None))
|
632
|
if date_retour:
|
633
|
if not date_clos or date_clos < date_retour:
|
634
|
fss.append((status_diagnostic, date_retour, None))
|
635
|
else:
|
636
|
fss.append((status_clos, date_clos, None))
|
637
|
elif date_clos:
|
638
|
fss.append((status_clos, date_clos, None))
|
639
|
else:
|
640
|
clos = False
|
641
|
inscrit = False
|
642
|
tt = None
|
643
|
for i in range(len(history)):
|
644
|
t, act_date = history[i]
|
645
|
if isinstance(act_date, date):
|
646
|
act_date = datetime(year=act_date.year,
|
647
|
month=act_date.month, day=act_date.day)
|
648
|
if not inscrit:
|
649
|
inscrit = True
|
650
|
if date_inscription:
|
651
|
if t == 'D':
|
652
|
fss.append((status_diagnostic, date_inscription, None))
|
653
|
else:
|
654
|
fss.append((status_traitement, date_inscription, None))
|
655
|
if len(history) == 1 and date_clos:
|
656
|
fss.append((status_clos, date_clos, None))
|
657
|
else:
|
658
|
if not date_clos:
|
659
|
if t == 'D':
|
660
|
fss.append((status_diagnostic, act_date, None))
|
661
|
else:
|
662
|
fss.append((status_traitement, act_date, None))
|
663
|
elif not clos:
|
664
|
if t == 'D':
|
665
|
fss.append((status_diagnostic, act_date, None))
|
666
|
else:
|
667
|
fss.append((status_traitement, act_date, None))
|
668
|
next_date = None
|
669
|
if i < len(history) - 1:
|
670
|
_, next_date = history[i+1]
|
671
|
if isinstance(next_date, date):
|
672
|
next_date = datetime(year=next_date.year,
|
673
|
month=next_date.month, day=next_date.day)
|
674
|
if not next_date or date_clos < next_date:
|
675
|
fss.append((status_clos, date_clos, None))
|
676
|
clos = True
|
677
|
else:
|
678
|
if date_retour and date_retour > date_clos:
|
679
|
if act_date >= date_retour:
|
680
|
if t == 'D':
|
681
|
fss.append((status_diagnostic, act_date, None))
|
682
|
else:
|
683
|
fss.append((status_traitement, act_date, None))
|
684
|
|
685
|
if not fss:
|
686
|
msg = "Dossier %s - %s %s sans aucune date ni acte facturé, on le met en accueil aujourd'hui." % (str(dossier['id']), str(dossier['nom'].encode('utf-8')), str(dossier['prenom'].encode('utf-8')))
|
687
|
logger.error("%s" % msg)
|
688
|
fss.append((status_accueil, datetime.today(), None))
|
689
|
else:
|
690
|
fs = FileState(status=fss[0][0], author=creator, previous_state=None)
|
691
|
date_selected = fss[0][1]
|
692
|
fs.patient = patient
|
693
|
fs.date_selected = date_selected
|
694
|
fs.comment = fss[0][2]
|
695
|
fs.save()
|
696
|
patient.last_state = fs
|
697
|
patient.save()
|
698
|
if len(fss) > 1:
|
699
|
for status, date_selected, comment in fss[1:]:
|
700
|
try:
|
701
|
patient.set_state(status=status, author=creator, date_selected=date_selected, comment=comment)
|
702
|
except Exception, e:
|
703
|
msg = "Dossier %s - %s %s, exception %s lors de l'ajout d'un état %s en date du %s" % (str(dossier['id']), str(dossier['nom'].encode('utf-8')), str(dossier['prenom'].encode('utf-8')), str(e), str(status), str(date_selected))
|
704
|
logger.error("%s" % msg)
|
705
|
|
706
|
|
707
|
# i = 0
|
708
|
# for d in PatientRecord.objects.all():
|
709
|
# hcds = CmppHealthCareDiagnostic.objects.filter(patient=d)
|
710
|
# if not hcds:
|
711
|
# i += 1
|
712
|
# print 'Dossier %s sans PC de diag' % d
|
713
|
# print 'Il y a %d dossiers sans PC de diag' % i
|
714
|
|
715
|
# for d in PatientRecord.objects.all():
|
716
|
# d.create_diag_healthcare(creator)
|
717
|
|
718
|
# Si reouverture apres date de cloture, passer à cette date en d ou en t
|
719
|
# Combinaisons possibles
|
720
|
# Mais au final la reouverture n'a d'intérêt que si on a
|
721
|
# une date de cloture antérieure
|
722
|
# if date_retour and date_clos and date_retour < date_clos:
|
723
|
# # accueil, d/t, cloture (date inconnue), d/t, cloture
|
724
|
# elif date_retour and date_clos and date_retour > date_clos:
|
725
|
# # accueil, d/t, cloture, d/t
|
726
|
# elif date_retour and date_clos and date_retour == date_clos:
|
727
|
# print "Date de retour et date de clotûre égale pour %s" % dossier['id']
|
728
|
# elif date_retour:
|
729
|
# # accueil, d/t, cloture (date inconnue), d/t
|
730
|
# elif date_clos:
|
731
|
# # accueil, d/t, cloture
|
732
|
# else:
|
733
|
# # accueil, d/t
|
734
|
|
735
|
if __name__ == "__main__":
|
736
|
msg = "Lancement du script"
|
737
|
logger.info("%s" % msg)
|
738
|
|
739
|
import_dossiers_phase_1()
|
740
|
|
741
|
msg = "Fin du script"
|
742
|
logger.info("%s" % msg)
|