Project

General

Profile

Download (44 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / scripts / import_dossiers.py @ 5fce3dc9

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
# Mettre tous les actes avant le 3 janvier à validation_locked = True
92
# Mettre tous les actes avec dossier['marque'] = 1 à is_billed = True
93

    
94

    
95

    
96

    
97
#tarifs: prix_journee.csv
98

    
99
#Contacts et données secu, assuré
100
#Adresses
101
#prise en charges (cmpp)
102
#Quel contact est l'assuré ? voir pc Idem, la caisse est pas sur le contact mais sur la pc!
103
#Les états des dossiers!
104
#diag id = 1 trait id = 2
105
#notes ?
106

    
107
#Ajouter au contact lien avec l'enfant mère, grand mèere, etc.
108
#table parente champs du dossier: parente.csv associé à la table contact ?
109
#Ajouter au contact
110
#Attention caisse il faut les ancien id pour retourber? on peut rechercher sur le numéro de la caisse!
111

    
112

    
113
#tables = ["dossiers", "adresses", "contacts", "convocations", "dossiers_ecole", "dossiers_mises", "pc", "periodes_pc","pmt", "rm", , "sor_motifs", "sor_orientation", "suivi"]
114
#tables = ["dossiers_test"]
115

    
116
def _exist(str):
117
    if str and str != "" and str != '0':
118
        return True
119
    return False
120

    
121
def treat_name(name):
122
    res = ''
123
    for p in name.split():
124
        res += p[0].upper()+p[1:].lower()
125
        res += ' '
126
    return res[:-1]
127

    
128
def _to_date(str_date):
129
    if not str_date:
130
        return None
131
    return datetime.strptime(str_date[:-13], "%Y-%m-%d")
132

    
133
def _to_int(str_int):
134
    if not str_int:
135
        return None
136
    return int(str_int)
137

    
138
def _get_dict(cols, line):
139
    """"""
140
    res = {}
141
    for i, data in enumerate(line):
142
        res[cols[i]] = data.decode('utf-8')
143
    return res
144

    
145
tables_data = {}
146

    
147
map_rm_cmpp = [1, 3, 2, 8, 6, 4]
148

    
149
def get_rm(service, val):
150
    old_id_rm = _to_int(val)
151
    if old_id_rm < 1 or 'SESSAD' in service.name:
152
        return None
153
    if service.name == 'CMPP':
154
        old_id_rm = map_rm_cmpp[old_id_rm - 1]
155
    try:
156
        return MaritalStatusType.objects.get(id=old_id_rm)
157
    except:
158
        return None
159

    
160
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]
161

    
162
# CMPP à 25 = Rien
163
def get_job(service, val):
164
    old_id_job = _to_int(val)
165
    if old_id_job < 1:
166
        return None
167
    if service.name == 'CAMSP' and old_id_job == 26:
168
        return None
169
    if service.name == 'CAMSP':
170
        try:
171
            old_id_job = map_job_camsp[old_id_job - 1]
172
        except:
173
            print 'Old id job out of range: %d' % old_id_job
174
    try:
175
        return Job.objects.get(id=old_id_job)
176
    except:
177
        return None
178

    
179
def extract_phone(val):
180
    if not val or val == '' or val == '0':
181
        return None
182
    s = ''.join([c for c in val if c.isdigit()])
183
    return s[:11]
184

    
185
def get_nir(nir, key, writer, line, service):
186
    if not nir:
187
        return None
188
    if len(nir) != 13:
189
        return -1
190
    if key:
191
        minus = 0
192
        # Corsica dept 2A et 2B
193
        if nir[6] in ('A', 'a'):
194
            nir = [c for c in nir]
195
            nir[6] = '0'
196
            nir = ''.join(nir)
197
            minus = 1000000
198
        elif nir[6] in ('B', 'b'):
199
            nir = [c for c in nir]
200
            nir[6] = '0'
201
            nir = ''.join(nir)
202
            minus = 2000000
203
        try:
204
            nir = int(nir) - minus
205
            good_key = 97 - (nir % 97)
206
            key = int(key)
207
            if key != good_key:
208
                msg = 'Clé incorrect %s pour %s' % (str(key), str(nir))
209
                writer.writerow(line + [service.name, msg])
210
        except:
211
            pass
212
    return nir
213

    
214

    
215
def import_dossiers_phase_1():
216
    """ """
217
    print "====== Début à %s ======" % str(datetime.today())
218

    
219
    f1 = open('./scripts/dossiers_ecoles_manuel.csv', 'wb')
220
    writer1 = csv.writer(f1, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
221

    
222
    f2 = open('./scripts/dossiers_manuel.csv', 'wb')
223
    writer2 = csv.writer(f2, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
224

    
225
    f3 = open('./scripts/contacts_manuel.csv', 'wb')
226
    writer3 = csv.writer(f3, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
227

    
228
    f4 = open('./scripts/pc_manuel.csv', 'wb')
229
    writer4 = csv.writer(f4, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
230

    
231
    status_accueil = Status.objects.filter(type="ACCUEIL")[0]
232
    status_diagnostic = Status.objects.filter(type="DIAGNOSTIC")[0]
233
    status_traitement = Status.objects.filter(type="TRAITEMENT")[0]
234
    status_clos = Status.objects.filter(type="CLOS")[0]
235
    status_bilan = Status.objects.filter(type="BILAN")[0]
236
    status_suivi = Status.objects.filter(type="SUIVI")[0]
237
    status_surveillance = Status.objects.filter(type="SURVEILLANCE")[0]
238
    creator = User.objects.get(id=1)
239

    
240
    for db in dbs:
241
        if "F_ST_ETIENNE_CMPP" == db:
242
            service = Service.objects.get(name="CMPP")
243
        elif "F_ST_ETIENNE_CAMSP" == db:
244
            service = Service.objects.get(name="CAMSP")
245
        elif "F_ST_ETIENNE_SESSAD_TED" == db:
246
            service = Service.objects.get(name="SESSAD TED")
247
        elif "F_ST_ETIENNE_SESSAD" == db:
248
            service = Service.objects.get(name="SESSAD DYS")
249

    
250
        print "====== %s ======" % service.name
251
        print datetime.today()
252

    
253
        print "--> Lecture table des dossiers..."
254
        csvfile = open(os.path.join(db_path, db, 'dossiers.csv'), 'rb')
255
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
256
        d_cols = csvlines.next()
257
        writer2.writerow(d_cols + ['service', 'creation', 'commentaire'])
258
        tables_data['dossiers'] = []
259
        for line in csvlines:
260
            #Au moins nom et prénom
261
            if _exist(line[1]) and _exist(line[2]):
262
                data = _get_dict(d_cols, line)
263
                tables_data['dossiers'].append(data)
264
            else:
265
                writer2.writerow(line + [service.name, 'Non', 'Ni Nom, ni prénom'])
266
        csvfile.close()
267
        print "<-- Terminé"
268

    
269
        print "--> Lecture table des caisses..."
270
        csvfile = open(os.path.join(db_path, db, 'caisses.csv'), 'rb')
271
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
272
        caisses_cols = csvlines.next()
273
        tables_data['caisses'] = {}
274
        for line in csvlines:
275
            data = _get_dict(caisses_cols, line)
276
            tables_data['caisses'][data['id']] = data
277
        csvfile.close()
278
        print "<-- Terminé"
279

    
280
        print "--> Chargement mises..."
281
        mises = {}
282
        csvfile = open(os.path.join(db_path, db, 'mises.csv'), 'rb')
283
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
284
        cols = csvlines.next()
285
        for line in csvlines:
286
            mises[line[0]] = (line[1], line[2])
287
        csvfile.close()
288
        print "<-- Terminé"
289

    
290
        print "--> Chargement quotations..."
291
        mises_per_patient = {}
292
        csvfile = open(os.path.join(db_path, db, 'dossiers_mises.csv'), 'rb')
293
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
294
        cols = csvlines.next()
295
        for line in csvlines:
296
            code, axe = mises[line[2]]
297
            quotation = CodeCFTMEA.objects.get(code=int(code), axe=int(axe))
298
            if line[1] in mises_per_patient.keys():
299
                mises_per_patient[line[1]].append(quotation)
300
            else:
301
                mises_per_patient[line[1]] = [quotation]
302
        csvfile.close()
303
        print "<-- Terminé"
304

    
305
        print "--> Ajout périodes de socialisation..."
306
        social_duration_per_patient = {}
307
        csvfile = open(os.path.join(db_path, db, 'dossiers_ecoles.csv'), 'rb')
308
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
309
        cols = csvlines.next()
310
        writer1.writerow(cols + ['service'])
311
        i = 0
312
        for line in csvlines:
313
            i += 1
314
        print "Nombre à traiter : %d" % i
315
        csvfile.close()
316
        csvfile = open(os.path.join(db_path, db, 'dossiers_ecoles.csv'), 'rb')
317
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
318
        csvlines.next()
319
        for line in csvlines:
320
            school = None
321
            if _exist(line[2]):
322
                school = School.objects.get(old_id=line[2], old_service=service.name)
323
            level = None
324
            try:
325
                level = SchoolLevel.objects.get(old_id=line[4], old_service=service.name)
326
            except:
327
                pass
328
            contact = line[3]
329
            if contact != "":
330
                contact += ' ' + line[7]
331
            else:
332
                contact = line[7]
333
            start_date = _to_date(line[5])
334
            social_duration = SocialisationDuration(school=school, level=level, start_date=start_date, contact=contact)
335
            social_duration.save()
336
            if line[1] in social_duration_per_patient.keys():
337
                social_duration_per_patient[line[1]].append(social_duration)
338
            else:
339
                social_duration_per_patient[line[1]] = [social_duration]
340
            #Produce file for manuel treatment:
341
            for j in range(6, 16):
342
                if j != 7 and _exist(line[j]):
343
                    writer1.writerow(line + [service.name])
344
            i -= 1
345
            if not (i % 10):
346
                sys.stdout.write('%d' %i)
347
            else:
348
                sys.stdout.write('.')
349
            sys.stdout.flush()
350
        csvfile.close()
351
        print "<-- Terminé"
352

    
353
        print "--> Ajout des adresses..."
354
        adresses_per_patient = {}
355
        adresses_per_old_id = {}
356
        csvfile = open(os.path.join(db_path, db, 'adresses.csv'), 'rb')
357
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
358
        cols = csvlines.next()
359
        i = 0
360
        for line in csvlines:
361
            i += 1
362
        print "Nombre à traiter : %d" % i
363
        csvfile.close()
364
        csvfile = open(os.path.join(db_path, db, 'adresses.csv'), 'rb')
365
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
366
        csvlines.next()
367
        for line in csvlines:
368
            phone = extract_phone(line[5])
369
            comment = ''
370
            if _exist(line[10]):
371
                comment += line[10] + ' - '
372
            if _exist(line[5]):
373
                comment += "Numéro 1 : " + line[5] + ' - '
374
            if _exist(line[8]):
375
                comment += "Numéro 2 : " + line[8] + ' - '
376
            fax = None
377
            place_of_life = False
378
            if _exist(line[9]) and line[9] == '-1':
379
                place_of_life = True
380
            number = None
381
            street = line[11]
382
            address_complement = line[12]
383
            zip_code = None
384
            if _exist(line[3]):
385
                if len(line[3]) > 5:
386
                    zip_code = line[3][-5:]
387
                else:
388
                    zip_code = line[3]
389
            city = line[4]
390

    
391
            address = PatientAddress(phone=phone, comment=comment,
392
                fax=fax, place_of_life=place_of_life, number=number,
393
                street=street, address_complement=address_complement,
394
                zip_code=zip_code, city=city)
395

    
396
            address.save()
397
            if line[1] in adresses_per_patient.keys():
398
                adresses_per_patient[line[1]].append(address)
399
            else:
400
                adresses_per_patient[line[1]] = [address]
401
            adresses_per_old_id[line[0]] = (address, line[1])
402
            i -= 1
403
            if not (i % 10):
404
                sys.stdout.write('%d' %i)
405
            else:
406
                sys.stdout.write('.')
407
            sys.stdout.flush()
408
        csvfile.close()
409
        print "<-- Terminé"
410

    
411
        print "--> Chargement des prise en charge..."
412
        csvfile = open(os.path.join(db_path, db, 'pc.csv'), 'rb')
413
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
414
        pc_cols = csvlines.next()
415
        writer4.writerow(pc_cols + ['service', 'creation', 'commentaire'])
416
        tables_data['pcs'] = {}
417
        for line in csvlines:
418
            data = _get_dict(pc_cols, line)
419
            if line[1] in tables_data['pcs'].keys():
420
                tables_data['pcs'][line[1]].append(data)
421
            else:
422
                tables_data['pcs'][line[1]] = [data]
423
        csvfile.close()
424
        print "<-- Terminé"
425

    
426
        print "--> Ajout des contacts..."
427
        contacts_per_patient = {}
428
        csvfile = open(os.path.join(db_path, db, 'contacts.csv'), 'rb')
429
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
430
        cols = csvlines.next()
431
        writer3.writerow(cols + ['service', 'commentaire'])
432
        i = 0
433
        for line in csvlines:
434
            i += 1
435
        print "Nombre à traiter : %d" % i
436
        csvfile.close()
437
        csvfile = open(os.path.join(db_path, db, 'contacts.csv'), 'rb')
438
        csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
439
        csvlines.next()
440
        for line in csvlines:
441
            phone = extract_phone(line[13])
442
            mobile = extract_phone(line[14])
443
            contact_comment = ''
444
            if _exist(line[13]):
445
                contact_comment += "Travail : " + line[13] + ' - '
446
            if _exist(line[14]):
447
                contact_comment += "Mobile : " + line[14] + ' - '
448
            if _exist(line[17]):
449
                contact_comment += "Divers : " + line[17] + ' - '
450
            last_name = treat_name(line[3])
451
            first_name = treat_name(line[4])
452
            gender = None
453
            if line[2] == "1":
454
                gender = 1
455
            if line[2] == "2":
456
                gender = 2
457
            email = None
458
            if _exist(line[15]):
459
                email = line[15]
460
            birthplace = None
461
            if _exist(line[6]):
462
                birthplace = line[6]
463
            social_security_id = get_nir(line[7], line[8], writer3, line, service)
464
            if social_security_id == -1:
465
                msg = 'Numéro %s de longueur diff de 13.' % line[7]
466
                writer3.writerow(line + [service.name, msg])
467
                contact_comment += "Numéro NIR invalide : " + line[7] + ' - '
468
                social_security_id = None
469
            birthdate = _to_date(line[5])
470
            job = get_job(service, line[9])
471
            parente = None
472
            try:
473
                if service.name == 'CAMSP':
474
                    parente = PatientRelatedLink.objects.get(old_camsp_id=_to_int(line[11]))
475
                elif service.name == 'CMPP':
476
                    parente = PatientRelatedLink.objects.get(old_cmpp_id=_to_int(line[11]))
477
                elif service.name == 'SESSAD DYS':
478
                    parente = PatientRelatedLink.objects.get(old_sessad_dys_id=_to_int(line[11]))
479
                elif service.name == 'SESSAD TED':
480
                    parente = PatientRelatedLink.objects.get(old_sessad_ted_id=_to_int(line[11]))
481
            except:
482
                pass
483
            twinning_rank = None
484
            thirdparty_payer = False
485
            begin_rights = None
486
            end_rights = None
487
            health_center = None
488
            old_contact_id = line[0]
489

    
490
            contact = PatientContact(phone=phone, mobile= mobile,
491
                contact_comment=contact_comment,
492
                last_name = last_name, first_name = first_name,
493
                gender = gender, email = email, parente = parente,
494
                social_security_id = social_security_id,
495
                birthdate = birthdate, job = job, birthplace=birthplace,
496
                twinning_rank = twinning_rank,
497
                thirdparty_payer = thirdparty_payer,
498
                begin_rights = begin_rights,
499
                end_rights = end_rights,
500
                health_center = health_center,
501
                old_contact_id=old_contact_id)
502
            contact.save()
503

    
504
            if not line[1] in adresses_per_old_id:
505
                msg = 'Contact sans adresse'
506
                writer3.writerow(line + [service.name, msg])
507
                contact.delete()
508
            else:
509
                adresse, old_patient_id = adresses_per_old_id[line[1]]
510
                contact.addresses.add(adresse)
511
                # Ajouter l'adresse au contact
512
                # Faire une liste des contacts par patient pour ajouter ensuite
513
                if old_patient_id in contacts_per_patient.keys():
514
                    contacts_per_patient[old_patient_id].append(contact)
515
                else:
516
                    contacts_per_patient[old_patient_id] = [contact]
517
            i -= 1
518
            if not (i % 10):
519
                sys.stdout.write('%d' %i)
520
            else:
521
                sys.stdout.write('.')
522
            sys.stdout.flush()
523
        csvfile.close()
524
        print "<-- Terminé"
525

    
526
        print "--> Ajout dossiers..."
527
        print "Nombre à traiter : %d" % len(tables_data['dossiers'])
528
        i = len(tables_data['dossiers'])
529
        for dossier in tables_data['dossiers']:
530

    
531
            date_accueil = None
532
            date_diagnostic = None
533
            date_traitement = None
534
            date_clos = None
535
            date_bilan = None
536
            date_suivi = None
537
            date_surveillance = None
538
            date_retour = None
539
            fss = []
540
            date_accueil = _to_date(dossier['con_date'])
541
            date_traitement = _to_date(dossier['ins_date'])
542
            date_clos = _to_date(dossier['sor_date'])
543
            if not (date_accueil or date_traitement or date_clos):
544
                # no state date, the record is inconsistent
545
                writer2.writerow([dossier[c].encode('utf-8') for c in d_cols] + [service.name, 'Non', "Aucune date d'état existante"])
546
                continue
547
            # Manage states
548
            if date_accueil and date_traitement and date_accueil > date_traitement:
549
                date_accueil = None
550
            if date_traitement and date_clos and date_traitement > date_clos:
551
                date_traitement = None
552
            if date_accueil and date_clos and date_accueil > date_clos:
553
                date_accueil = None
554
            if "SESSAD" in service.name:
555
                # Il n'y a jamais eu de retour au SESSADs
556
                if date_accueil:
557
                    fss.append((status_accueil, date_accueil, None))
558
                if date_traitement:
559
                    fss.append((status_traitement, date_traitement, None))
560
                if date_clos:
561
                    fss.append((status_clos, date_clos, None))
562
            # Jamais de motif et provenance au retour
563
            elif service.name == 'CAMSP':
564
                date_retour = _to_date(dossier['ret_date'])
565
                if date_accueil:
566
                    fss.append((status_accueil, date_accueil, None))
567
                if not date_retour:
568
                    if date_traitement:
569
                        s = status_bilan
570
                        if dossier['suivi'] == '3':
571
                            s = status_suivi
572
                        elif dossier['suivi'] == '4':
573
                            s = status_surveillance
574
#                        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."))
575
                        fss.append((s, date_traitement, ""))
576
                else:
577
                    # Le retour supprime la précédente de clôture, on choisit le retour à j-1
578
                    if date_traitement:
579
                        # c'est l'inscription
580
                        if date_traitement < date_retour:
581
                            fss.append((status_suivi, date_traitement, "Etat de traitement indéterminé (Suivi par défaut)."))
582
                        old_clos_date = date_retour + relativedelta(days=-1)
583
                        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)."))
584
                        s = status_bilan
585
                        if dossier['suivi'] == '3':
586
                            s = status_suivi
587
                        elif dossier['suivi'] == '4':
588
                            s = status_surveillance
589
#                        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."))
590
                        fss.append((s, date_retour,  ""))
591
                if date_clos:
592
                    if date_retour and date_clos < date_retour:
593
                        print 'La date de clôture ne peut être antérieure à la date de retour!'
594
                    else:
595
                        fss.append((status_clos, date_clos, None))
596
            else:
597
                date_retour = _to_date(dossier['ret_date'])
598
                if date_accueil:
599
                    fss.append((status_accueil, date_accueil, None))
600
                if not date_retour:
601
                    if date_traitement:
602
                        fss.append((status_diagnostic, date_traitement, "Inscription en diag mais pourrait etre en trait. A préciser"))
603
                else:
604
                    # Le retour supprime la précédente de clôture, on choisit le retour à j-1
605
                    if date_traitement:
606
                        # c'est l'inscription
607
                        if date_traitement < date_retour:
608
                            fss.append((status_diagnostic, date_traitement, "A préciser."))
609
                            fss.append((status_traitement, date_traitement + relativedelta(days=1), "A préciser."))
610
                        old_clos_date = date_retour + relativedelta(days=-1)
611
                        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)."))
612
                        fss.append((status_diagnostic, date_retour,  "Retour en diag mais pourrait etre en trait. A préciser"))
613
                if date_clos:
614
                    if date_retour and date_clos < date_retour:
615
                        print 'La date de clôture ne peut être antérieure à la date de retour!'
616
                    else:
617
                        fss.append((status_clos, date_clos, None))
618
                else:
619
                    if old_id in tables_data['pcs'].keys():
620
                        pcs = tables_data['pcs'][old_id]
621
                        last_pc = pcs[len(pcs)-1]
622
                        if last_pc['genre_pc'] == '2':
623
                            status, date, msg = fss[len(fss)-1]
624
                            last_date = date + relativedelta(days=1)
625
                            fss.append((status_traitement, last_date, "Date à préciser."))
626
                        else:
627
                            pass
628
    #                        pc idag, reste en diag.
629
                    else:
630
                        pass
631
#                        pas de pc, par defaut en diag.
632

    
633
                # Il faut l'historique des actes du patient
634
                # Il faut pour chaque acte assigner à la pc
635
                # on sait les derniers actes s'ils sont affecté ou non
636
                # Il faudrait conserver avec les actes les num de facture ?
637
                # rEvori el fonctionnement de l'assignation d'un acte a une pc
638

    
639
            for col in ('mdph', 'code_archive', 'aeeh', 'mdph_departement', 'pps', 'pps_deb', 'pps_fin', 'mdph_Debut', 'mdph_Fin'):
640
                if _exist(dossier[col]):
641
                    writer2.writerow([dossier[c].encode('utf-8') for c in d_cols] + [service.name, 'Oui', "Données présentes non traitées"])
642
                    break
643

    
644
            #People
645
            first_name = treat_name(dossier['prenom'])
646
            last_name = treat_name(dossier['nom'])
647
            display_name = None
648
            gender = _to_int(dossier['nais_sexe'])
649
            if not gender in (1,2):
650
                gender = None
651
            email = None
652
            phone = None
653

    
654
            #PatientContact
655
            mobile = None
656
            social_security_id = None
657
            birthdate = _to_date(dossier['nais_date'])
658
            twinning_rank = _to_int(dossier['nais_rang'])
659
            # Pourra etre init à l'import des contacts après création du dossier
660
            thirdparty_payer = False
661
            begin_rights = None
662
            end_rights = None
663
            health_center = None
664
            addresses = None
665
            contact_comment = None
666

    
667
            #PatientRecord
668
            creator = creator
669
            # Pourra etre init à l'import des contacts après création du dossier
670
            nationality = None
671
            paper_id = None
672
            comment = dossier['infos']
673
            pause = False
674
            if _exist(dossier['blocage']):
675
                pause = True
676
            confidential = False
677
            if _exist(dossier['non_communication_ecole']):
678
                confidential = True
679

    
680
            # Physiology and health data
681
            size = _to_int(dossier['taille'])
682
            weight = _to_int(dossier['poids'])
683
            pregnancy_term = _to_int(dossier['terme'])
684
            cranium_perimeter = None
685
            chest_perimeter = None
686
            apgar_score_one = _to_int(dossier['apgar_1'])
687
            apgar_score_two = _to_int(dossier['apgar_5'])
688

    
689

    
690
            # Inscription motive
691
            # Up to now only used at the CAMSP
692
            analysemotive = None
693
            try:
694
                analysemotive = AnalyseMotive.objects.get(id=_to_int(dossier['ins_motif']))
695
            except:
696
                pass
697
            # Up to now only used at the CAMSP
698
            familymotive = None
699
            try:
700
                familymotive = FamilyMotive.objects.get(id=_to_int(dossier['ins_motif_exprim']))
701
            except:
702
                pass
703
            provenance = None
704
            try:
705
                provenance = Provenance.objects.get(old_id=_to_int(dossier['ins_provenance']), old_service=service.name)
706
            except:
707
                pass
708
            # Up to now only used at the CAMSP
709
            advicegiver = None
710
            try:
711
                advicegiver = AdviceGiver.objects.get(id=_to_int(dossier['con_qui']))
712
            except:
713
                pass
714

    
715
            # Inscription motive
716
            # Up to now only used at the CAMSP
717
            outmotive = None
718
            try:
719
                outmotive = OutMotive.objects.get(id=_to_int(dossier['sor_motif']))
720
            except:
721
                pass
722
            # Up to now only used at the CAMSP
723
            outto = None
724
            try:
725
                outto = OutTo.objects.get(id=_to_int(dossier['sor_orientation']))
726
            except:
727
                pass
728

    
729
            # Family
730
            sibship_place = _to_int(dossier['nais_fratrie'])
731
            nb_children_family = _to_int(dossier['nbr_enfants'])
732
            parental_authority = None
733
            try:
734
                parental_authority = ParentalAuthorityType.objects.get(id=_to_int(dossier['autorite_parentale']))
735
            except:
736
                pass
737
            # Up to now only used at the CAMSP
738
            family_situation = None
739
            try:
740
                family_situation = FamilySituationType.objects.get(id=_to_int(dossier['situation_familiale']))
741
            except:
742
                pass
743
            # Up to now only used at the CAMSP
744
            child_custody = None
745
            try:
746
                child_custody = ParentalCustodyType.objects.get(id=_to_int(dossier['garde']))
747
            except:
748
                pass
749

    
750
            rm_mother = get_rm(service, dossier['rm_mere'])
751
            rm_father = get_rm(service, dossier['rm_pere'])
752
            job_mother = get_job(service, dossier['prof_mere'])
753
            job_father = get_job(service, dossier['prof_pere'])
754
            family_comment = None
755

    
756
            # Transport
757
            transportcompany = None
758
            try:
759
                if service.name == 'CAMSP':
760
                    transportcompany = TransportCompany.objects.get(old_camsp_id=_to_int(dossier['transport']))
761
                elif service.name == 'CMPP':
762
                    transportcompany = TransportCompany.objects.get(old_cmpp_id=_to_int(dossier['transport']))
763
                elif service.name == 'SESSAD DYS':
764
                    transportcompany = TransportCompany.objects.get(old_sessad_dys_id=_to_int(dossier['transport']))
765
                elif service.name == 'SESSAD TED':
766
                    transportcompany = TransportCompany.objects.get(old_sessad_ted_id=_to_int(dossier['transport']))
767
            except:
768
                pass
769
            transporttype = None
770
            try:
771
                transporttype = TransportType.objects.get(id=_to_int(dossier['type_transport']))
772
            except:
773
                pass
774

    
775
            # FollowUp
776
            externaldoctor = None
777
            try:
778
                externaldoctor = ExternalTherapist.objects.get(old_id=_to_int(dossier['medecin_exterieur']), old_service=service.name)
779
            except:
780
                pass
781
            externalintervener = None
782
            try:
783
                externalintervener = ExternalWorker.objects.get(old_id=_to_int(dossier['intervenant_exterieur']), old_service=service.name)
784
            except:
785
                pass
786

    
787
            old_id = dossier['id']
788
            old_old_id = dossier['ancien_numero']
789

    
790

    
791
            patient, created = PatientRecord.objects.get_or_create(first_name = first_name,
792
                    last_name = last_name,
793
                    birthdate = birthdate,
794
                    twinning_rank = twinning_rank,
795
                    gender = gender,
796
                    display_name = display_name,
797
                    email = email,
798
                    phone = phone,
799
                    mobile = mobile,
800
                    contact_comment = contact_comment,
801
                    nationality = nationality,
802
                    paper_id = paper_id,
803
                    comment = comment,
804
                    pause = pause,
805
                    confidential = confidential,
806
                    size = size,
807
                    weight = weight,
808
                    pregnancy_term = pregnancy_term,
809
                    cranium_perimeter = cranium_perimeter,
810
                    chest_perimeter = chest_perimeter,
811
                    apgar_score_one = apgar_score_one,
812
                    apgar_score_two = apgar_score_two,
813
                    analysemotive = analysemotive,
814
                    familymotive = familymotive,
815
                    provenance = provenance,
816
                    advicegiver = advicegiver,
817
                    outmotive = outmotive,
818
                    outto = outto,
819
                    sibship_place = sibship_place,
820
                    nb_children_family = nb_children_family,
821
                    parental_authority = parental_authority,
822
                    family_situation = family_situation,
823
                    rm_mother = rm_mother,
824
                    rm_father = rm_father,
825
                    job_mother = job_mother,
826
                    job_father = job_father,
827
                    family_comment = family_comment,
828
                    child_custody = child_custody,
829
                    transportcompany = transportcompany,
830
                    transporttype = transporttype,
831
                    externaldoctor = externaldoctor,
832
                    externalintervener = externalintervener,
833
                    service=service,
834
                    creator=creator,
835
                    old_id = old_id,
836
                    old_old_id = old_old_id)
837

    
838
#            if created:
839
#                print 'Creation de%s' % patient
840
#            else:
841
#                print 'Patient %s existe' % patient
842

    
843
            # Init states
844
            if not fss:
845
                print "Pas d'etat et le dossier patient %s (old_id) a ete cree!" % old_id
846
            else:
847
                fs = FileState(status=fss[0][0], author=creator, previous_state=None)
848
                date_selected = fss[0][1]
849
                fs.patient = patient
850
                fs.date_selected = date_selected
851
                fs.comment = fss[0][2]
852
                fs.save()
853
                patient.last_state = fs
854
                patient.save()
855
                if len(fss) > 1:
856
                    for status, date, comment in fss[1:]:
857
                        patient.set_state(status=status, author=creator, date_selected=date, comment=comment)
858

    
859

    
860
            if old_id in mises_per_patient.keys():
861
                for quotation in mises_per_patient[old_id]:
862
                    if quotation.axe == 1:
863
                        patient.mises_1.add(quotation)
864
                    elif quotation.axe == 2:
865
                        patient.mises_2.add(quotation)
866
                    elif quotation.axe == 3:
867
                        patient.mises_3.add(quotation)
868
                    else:
869
                        raise
870

    
871
            if old_id in social_duration_per_patient.keys():
872
                for social_duration in social_duration_per_patient[old_id]:
873
                    patient.socialisation_durations.add(social_duration)
874

    
875
            for t_the in ('the_medecin', 'the_referent', 'the_therapeute'):
876
                try:
877
                    therapist = None
878
                    if service.name == 'CAMSP':
879
                        therapist = Worker.objects.get(old_camsp_id=_to_int(dossier[t_the]))
880
                    elif service.name == 'CMPP':
881
                        therapist = Worker.objects.get(old_cmpp_id=_to_int(dossier[t_the]))
882
                    elif service.name == 'SESSAD DYS':
883
                        therapist = Worker.objects.get(old_sessad_dys_id=_to_int(dossier[t_the]))
884
                    elif service.name == 'SESSAD TED':
885
                        therapist = Worker.objects.get(old_sessad_ted_id=_to_int(dossier[t_the]))
886
                    patient.coordinators.add(therapist)
887
                except:
888
                    pass
889

    
890
            # Initialisation adresses et contacts
891
            if old_id in adresses_per_patient.keys():
892
                for adresse in adresses_per_patient[old_id]:
893
                    patient.addresses.add(adresse)
894
            if old_id in contacts_per_patient.keys():
895
                for contact in contacts_per_patient[old_id]:
896
                    if contact.last_name == patient.last_name \
897
                            and contact.first_name == patient.first_name:
898
#                        print "Le contact %s %s est le patient" % (contact.last_name, contact.first_name)
899
                        if not patient.birthdate:
900
                             patient.birthdate = contact.birthdate
901
                        patient.birthplace = contact.birthplace
902
                        patient.email = contact.email
903
                        patient.phone = contact.phone
904
                        patient.mobile = contact.mobile
905
                        patient.social_security_id = contact.social_security_id
906
                        patient.thirdparty_payer = contact.thirdparty_payer
907
                        patient.begin_rights = contact.begin_rights
908
                        patient.end_rights = contact.end_rights
909
                        patient.health_center = contact.health_center
910
                        patient.contact_comment = contact.contact_comment
911
                        patient.old_contact_id = contact.old_contact_id
912
                        patient.save()
913
                        contact.delete()
914
                    else:
915
                        patient.contacts.add(contact)
916

    
917
            policyholder = None
918
            health_center = None
919
            other_health_center = None
920
            if old_id in tables_data['pcs'].keys():
921
                pcs = tables_data['pcs'][old_id]
922
                j = len(pcs)-1
923
                found = False
924
                last_pc = None
925
                while not found and j >= 0:
926
                    last_pc = pcs[j]
927
                    if 'contact_id' in last_pc.keys():
928
                        found = True
929
                    j -= 1
930
                if not found:
931
                    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"])
932
#                    print "Pas de d'assure pour le patient %s" % old_id
933
#                    print "Le patient sera l'assure"
934
                else:
935
                    try:
936
                        caisse = None
937
                        centre = None
938
                        lg = None
939
                        policyholder = patient.contacts.get(old_contact_id=_to_int(last_pc['contact_id']))
940
                        if last_pc['caisse_id'] in tables_data['caisses'].keys():
941
                            lg = tables_data['caisses'][last_pc['caisse_id']]['tp']
942
                            if len(lg) < 2:
943
                                lg = ['0', lg]
944
                                lg = ''.join(lg)
945
                            caisse = tables_data['caisses'][last_pc['caisse_id']]['caisse']
946
                            while len(caisse) < 3:
947
                                caisse = ['0', caisse]
948
                                caisse = ''.join(caisse)
949
                            centre = tables_data['caisses'][last_pc['caisse_id']]['centre']
950
                            while len(centre) < 4:
951
                                centre = ['0', centre]
952
                                centre = ''.join(centre)
953
                            if lg and caisse:
954
                                health_centers = HealthCenter.objects.filter(large_regime__code=lg, health_fund=caisse)
955
                                if health_centers and len(health_centers.all()) == 1:
956
                                    health_center = health_centers[0]
957
                                    if last_pc['centre']:
958
                                        while len(last_pc['centre']) < 4:
959
                                            last_pc['centre'] = ['0', last_pc['centre']]
960
                                            last_pc['centre'] = ''.join(last_pc['centre'])
961
                                        other_health_center = last_pc['centre']
962
                                elif health_centers and len(health_centers.all()) > 1:
963
                                    health_centers = None
964
                                    if last_pc['centre']:
965
                                        while len(last_pc['centre']) < 4:
966
                                            last_pc['centre'] = ['0', last_pc['centre']]
967
                                            last_pc['centre'] = ''.join(last_pc['centre'])
968
#                                        print "centre 1 %s" % last_pc['centre']
969
                                        health_centers = HealthCenter.objects.filter(large_regime__code=lg, health_fund=caisse,
970
                                            code = last_pc['centre'])
971
                                    elif centre:
972
#                                        print "centre 2 %s" % centre
973
                                        health_centers = HealthCenter.objects.filter(large_regime__code=lg, health_fund=caisse,
974
                                            code = centre)
975
                                    if health_centers and len(health_centers.all()) == 1:
976
                                        health_center = health_centers[0]
977
                                    elif health_centers:
978
#                                        print "Plusieurs caisses avec le meme centre, patient %s" % old_id
979
                                        writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
980
                                            [service.name, '', "Plusieurs caisses avec le meme centre, patient %s" % old_id])
981
                                    else:
982
#                                        print "Caisse non determinee par code centre, patient %s" % old_id
983
                                        writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
984
                                            [service.name, '', "Caisse non determinee par code centre, patient %s" % old_id])
985
                                else:
986
#                                    print 'Caisse non trouvee avec ce numero de caisse et grand regime, patient %s' % old_id
987
                                    writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
988
                                        [service.name, '', 'Caisse non trouvee avec ce numero de caisse et grand regime, patient %s' % old_id])
989
                            else:
990
#                                print 'Infos manquantes dans fichiers des caisses, patient %s' % old_id
991
                                writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
992
                                    [service.name, '', 'Infos manquantes dans fichiers des caisses, patient %s' % old_id])
993
                        else:
994
#                            print 'Pas de caisse dans le fichiers caisse avec l id %s, patient %s' % (last_pc['caisse_id'], old_id)
995
                            writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
996
                                [service.name, '', 'Pas de caisse dans le fichier caisse avec l id %s, patient %s' % (last_pc['caisse_id'], old_id)])
997
                    except:
998
#                        print "Pas de contact avec id %s, patient %s" % (last_pc['contact_id'], old_id)
999
                        writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
1000
                            [service.name, '', "Pas d'assuré existant, le patient est choisi."])
1001

    
1002
            else:
1003
                writer2.writerow([dossier[c].encode('utf-8') for c in d_cols] + \
1004
                    [service.name, 'Oui', "Pas de pc, le patient est l'assure sans caisse"])
1005
#                print "Pas de pc pour le patient %s" % old_id
1006
#                print "Le patient sera l'assure"
1007
            if not policyholder:
1008
                policyholder = patient.patientcontact
1009
            policyholder.health_center = health_center
1010
            policyholder.other_health_center = other_health_center
1011
            policyholder.save()
1012
            patient.policyholder = policyholder
1013
            patient.save()
1014

    
1015

    
1016
            # On ne pass plus par le clean du contact form, du coup, pas moyen de modifier le health center!
1017

    
1018
            # Faut-il gére un code de gestion ?
1019

    
1020

    
1021
            #Etat des dossiers
1022

    
1023
            # patient.policyholder soit le contact, d'il n'y en a qu'un
1024
            # au cmmp, cf la pc
1025

    
1026
            # Dossier en pause facturation! champs pause sur le dossier OK
1027
            # si aucun contact, ou aucun contact avec un Nir valide!
1028

    
1029
            #Tiers-payant ? healthcenter ?
1030

    
1031
            # Notifications au sessad, il n'y en a pas!
1032

    
1033
#            i += 1
1034
#            print 'Fin de traitement pour le dossier %s' % patient
1035
#            if i >= 10:
1036
#                break
1037
            i -= 1
1038
            if not (i % 10):
1039
                sys.stdout.write('%d' %i)
1040
            else:
1041
                sys.stdout.write('.')
1042
            sys.stdout.flush()
1043

    
1044
        print "<-- Terminé"
1045
    print "====== Fin à %s ======" % str(datetime.today())
1046

    
1047
            #Travail manuel pour secreatires
1048
#            mdph_requests = models.ManyToManyField('ressources.MDPHRequest',
1049
#            mdph_responses = models.ManyToManyField('ressources.MDPHResponse',
1050

    
1051

    
1052
#            policyholder = None
1053
#            contacts = None
1054

    
1055

    
1056
if __name__ == "__main__":
1057
    import_dossiers_phase_1()
(10-10/28)