Project

General

Profile

Download (42 KB) Statistics
| Branch: | Tag: | Revision:
d11b45e3 Benjamin Dauvergne
# -*- coding: utf-8 -*-
#!/usr/bin/env python

import os
import sys
import csv

from datetime import datetime, time
8c87136a Mikaël Ates
from dateutil.relativedelta import relativedelta
d11b45e3 Benjamin Dauvergne
import calebasse.settings
import django.core.management

django.core.management.setup_environ(calebasse.settings)

from django.contrib.auth.models import User

from calebasse.agenda.models import Event, EventType
5ca2d204 Mikaël Ates
from calebasse.dossiers.models import PatientRecord, Status, FileState, PatientAddress, PatientContact
d11b45e3 Benjamin Dauvergne
from calebasse.ressources.models import Service
from calebasse.personnes.models import Worker, Holiday, ExternalWorker, ExternalTherapist
from calebasse.ressources.models import (WorkerType, ParentalAuthorityType, ParentalCustodyType,
FamilySituationType, TransportType, TransportCompany, Provenance, AnalyseMotive, FamilyMotive,
5ca2d204 Mikaël Ates
CodeCFTMEA, SocialisationDuration, School, SchoolLevel, OutMotive, OutTo, AdviceGiver,
fa39865b Mikaël Ates
MaritalStatusType, Job, PatientRelatedLink, HealthCenter)
d11b45e3 Benjamin Dauvergne
# Configuration
7fd275a7 Mikaël Ates
db_path = "./scripts/20130104-213225"
d11b45e3 Benjamin Dauvergne
4d879841 Benjamin Dauvergne
#dbs = ["F_ST_ETIENNE_SESSAD_TED", "F_ST_ETIENNE_CMPP", "F_ST_ETIENNE_CAMSP", "F_ST_ETIENNE_SESSAD"]
dbs = ["F_ST_ETIENNE_SESSAD_TED"]
d11b45e3 Benjamin Dauvergne

1cb58085 Mikaël Ates
map_cs = {}
map_cs['CAMSP'] = {
'1': 'ACT_DOUBLE',
'2': 'ABS_NON_EXC',
'3': 'ABS_EXC',
'4': 'ABS_INTER',
'5': 'ACT_LOST',
'6': 'ANNUL_NOUS',
'7': 'ANNUL_FAMILLE',
'8': 'ENF_HOSP',
'9': 'ACT_LOST',
'10': 'ACT_LOST',
'11': 'ACT_LOST',
'12': 'REPORTE'
}

map_cs['CMPP'] = {
'1': 'ACT_DOUBLE',
'2': 'ABS_NON_EXC',
'3': 'ABS_EXC',
'4': 'ABS_INTER',
'5': 'ACT_LOST',
'6': 'ANNUL_NOUS',
'7': 'ANNUL_FAMILLE',
'8': 'ABS_ESS_PPS',
'9': 'ACT_LOST',
'10': 'ACT_LOST',
'11': 'ACT_LOST',
'12': 'REPORTE'
}

map_cs['SESSAD DYS'] = {
'1': 'ACT_DOUBLE',
'2': 'ABS_NON_EXC',
'3': 'ABS_EXC',
'4': 'ABS_INTER',
'5': 'ACT_LOST',
'6': 'ANNUL_NOUS',
'7': 'ANNUL_FAMILLE',
'8': 'ABS_ESS_PPS',
'9': 'ACT_LOST',
'10': 'ACT_LOST',
'11': 'REPORTE'
}


map_cs['SESSAD TED'] = {
'1': 'ACT_DOUBLE',
'2': 'ABS_NON_EXC',
'3': 'ABS_EXC',
'4': 'ACT_LOST',
'5': 'ABS_INTER',
'6': 'ANNUL_NOUS',
'7': 'ANNUL_FAMILLE',
'8': 'REPORTE'
}

d11b45e3 Benjamin Dauvergne
def _exist(str):
if str and str != "" and str != '0':
return True
return False

def treat_name(name):
res = ''
for p in name.split():
res += p[0].upper()+p[1:].lower()
res += ' '
return res[:-1]

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

def _to_int(str_int):
if not str_int:
return None
return int(str_int)

def _get_dict(cols, line):
""""""
res = {}
for i, data in enumerate(line):
res[cols[i]] = data.decode('utf-8')
return res

tables_data = {}

4ecbfa8e Mikaël Ates
map_rm_cmpp = [1, 3, 2, 8, 6, 4]
5ca2d204 Mikaël Ates
def get_rm(service, val):
old_id_rm = _to_int(val)
if old_id_rm < 1 or 'SESSAD' in service.name:
return None
4ecbfa8e Mikaël Ates
if service.name == 'CMPP':
old_id_rm = map_rm_cmpp[old_id_rm - 1]
5ca2d204 Mikaël Ates
try:
return MaritalStatusType.objects.get(id=old_id_rm)
except:
return None

6571bfae Mikaël Ates
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]
5ca2d204 Mikaël Ates
# CMPP à 25 = Rien
def get_job(service, val):
old_id_job = _to_int(val)
if old_id_job < 1:
return None
4ecbfa8e Mikaël Ates
if service.name == 'CAMSP' and old_id_job == 26:
return None
5ca2d204 Mikaël Ates
if service.name == 'CAMSP':
9f2fcd0c Mikaël Ates
try:
old_id_job = map_job_camsp[old_id_job - 1]
except:
print 'Old id job out of range: %d' % old_id_job
5ca2d204 Mikaël Ates
try:
return Job.objects.get(id=old_id_job)
except:
return None

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

6571bfae Mikaël Ates
def get_nir(nir, key, writer, line, service):
5ca2d204 Mikaël Ates
if not nir:
return None
if len(nir) != 13:
return -1
if key:
9f2fcd0c Mikaël Ates
minus = 0
# Corsica dept 2A et 2B
if nir[6] in ('A', 'a'):
nir = [c for c in nir]
nir[6] = '0'
nir = ''.join(nir)
minus = 1000000
elif nir[6] in ('B', 'b'):
nir = [c for c in nir]
nir[6] = '0'
nir = ''.join(nir)
minus = 2000000
5ca2d204 Mikaël Ates
try:
9f2fcd0c Mikaël Ates
nir = int(nir) - minus
5ca2d204 Mikaël Ates
good_key = 97 - (nir % 97)
key = int(key)
if key != good_key:
6571bfae Mikaël Ates
msg = 'Clé incorrect %s pour %s' % (str(key), str(nir))
writer.writerow(line + [service.name, msg])
5ca2d204 Mikaël Ates
except:
pass
return nir


fa39865b Mikaël Ates
def import_dossiers_phase_1():
d11b45e3 Benjamin Dauvergne
""" """
print "====== Début à %s ======" % str(datetime.today())

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

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

6571bfae Mikaël Ates
f3 = open('./scripts/contacts_manuel.csv', 'wb')
writer3 = csv.writer(f3, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)

fa39865b Mikaël Ates
f4 = open('./scripts/pc_manuel.csv', 'wb')
writer4 = csv.writer(f4, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)

8c87136a Mikaël Ates
status_accueil = Status.objects.filter(type="ACCUEIL")[0]
status_diagnostic = Status.objects.filter(type="DIAGNOSTIC")[0]
status_traitement = Status.objects.filter(type="TRAITEMENT")[0]
status_clos = Status.objects.filter(type="CLOS")[0]
status_bilan = Status.objects.filter(type="BILAN")[0]
status_suivi = Status.objects.filter(type="SUIVI")[0]
status_surveillance = Status.objects.filter(type="SURVEILLANCE")[0]
creator = User.objects.get(id=1)

d11b45e3 Benjamin Dauvergne
for db in dbs:
if "F_ST_ETIENNE_CMPP" == db:
service = Service.objects.get(name="CMPP")
elif "F_ST_ETIENNE_CAMSP" == db:
service = Service.objects.get(name="CAMSP")
elif "F_ST_ETIENNE_SESSAD_TED" == db:
service = Service.objects.get(name="SESSAD TED")
elif "F_ST_ETIENNE_SESSAD" == db:
service = Service.objects.get(name="SESSAD DYS")

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

print "--> Lecture table des dossiers..."
csvfile = open(os.path.join(db_path, db, 'dossiers.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
d_cols = csvlines.next()
fa39865b Mikaël Ates
writer2.writerow(d_cols + ['service', 'creation', 'commentaire'])
d11b45e3 Benjamin Dauvergne
tables_data['dossiers'] = []
for line in csvlines:
#Au moins nom et prénom
if _exist(line[1]) and _exist(line[2]):
data = _get_dict(d_cols, line)
tables_data['dossiers'].append(data)
else:
8c87136a Mikaël Ates
writer2.writerow(line + [service.name, 'Non', 'Ni Nom, ni prénom'])
d11b45e3 Benjamin Dauvergne
csvfile.close()
print "<-- Terminé"

fa39865b Mikaël Ates
print "--> Lecture table des caisses..."
csvfile = open(os.path.join(db_path, db, 'caisses.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
caisses_cols = csvlines.next()
tables_data['caisses'] = {}
for line in csvlines:
data = _get_dict(caisses_cols, line)
tables_data['caisses'][data['id']] = data
csvfile.close()
print "<-- Terminé"

d11b45e3 Benjamin Dauvergne
print "--> Chargement mises..."
mises = {}
csvfile = open(os.path.join(db_path, db, 'mises.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
cols = csvlines.next()
for line in csvlines:
mises[line[0]] = (line[1], line[2])
csvfile.close()
print "<-- Terminé"

print "--> Chargement quotations..."
mises_per_patient = {}
csvfile = open(os.path.join(db_path, db, 'dossiers_mises.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
cols = csvlines.next()
for line in csvlines:
code, axe = mises[line[2]]
quotation = CodeCFTMEA.objects.get(code=int(code), axe=int(axe))
if line[1] in mises_per_patient.keys():
mises_per_patient[line[1]].append(quotation)
else:
mises_per_patient[line[1]] = [quotation]
csvfile.close()
print "<-- Terminé"

print "--> Ajout périodes de socialisation..."
social_duration_per_patient = {}
csvfile = open(os.path.join(db_path, db, 'dossiers_ecoles.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
cols = csvlines.next()
writer1.writerow(cols + ['service'])
i = 0
for line in csvlines:
i += 1
print "Nombre à traiter : %d" % i
csvfile.close()
csvfile = open(os.path.join(db_path, db, 'dossiers_ecoles.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
csvlines.next()
for line in csvlines:
school = None
if _exist(line[2]):
school = School.objects.get(old_id=line[2], old_service=service.name)
level = None
try:
level = SchoolLevel.objects.get(old_id=line[4], old_service=service.name)
except:
pass
contact = line[3]
if contact != "":
contact += ' ' + line[7]
else:
contact = line[7]
start_date = _to_date(line[5])
social_duration = SocialisationDuration(school=school, level=level, start_date=start_date, contact=contact)
social_duration.save()
if line[1] in social_duration_per_patient.keys():
social_duration_per_patient[line[1]].append(social_duration)
else:
social_duration_per_patient[line[1]] = [social_duration]
#Produce file for manuel treatment:
for j in range(6, 16):
if j != 7 and _exist(line[j]):
writer1.writerow(line + [service.name])
i -= 1
if not (i % 10):
sys.stdout.write('%d' %i)
else:
sys.stdout.write('.')
sys.stdout.flush()
csvfile.close()
print "<-- Terminé"

5ca2d204 Mikaël Ates
print "--> Ajout des adresses..."
adresses_per_patient = {}
adresses_per_old_id = {}
csvfile = open(os.path.join(db_path, db, 'adresses.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
cols = csvlines.next()
i = 0
for line in csvlines:
i += 1
print "Nombre à traiter : %d" % i
csvfile.close()
csvfile = open(os.path.join(db_path, db, 'adresses.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
csvlines.next()
for line in csvlines:
phone = extract_phone(line[5])
comment = ''
if _exist(line[10]):
comment += line[10] + ' - '
if _exist(line[5]):
comment += "Numéro 1 : " + line[5] + ' - '
if _exist(line[8]):
comment += "Numéro 2 : " + line[8] + ' - '
fax = None
place_of_life = False
8c87136a Mikaël Ates
if _exist(line[9]) and line[9] == '-1':
5ca2d204 Mikaël Ates
place_of_life = True
number = None
street = line[11]
address_complement = line[12]
6571bfae Mikaël Ates
zip_code = None
if _exist(line[3]):
if len(line[3]) > 5:
zip_code = line[3][-5:]
else:
zip_code = line[3]
5ca2d204 Mikaël Ates
city = line[4]

address = PatientAddress(phone=phone, comment=comment,
fax=fax, place_of_life=place_of_life, number=number,
street=street, address_complement=address_complement,
zip_code=zip_code, city=city)

address.save()
if line[1] in adresses_per_patient.keys():
adresses_per_patient[line[1]].append(address)
else:
adresses_per_patient[line[1]] = [address]
adresses_per_old_id[line[0]] = (address, line[1])
i -= 1
if not (i % 10):
sys.stdout.write('%d' %i)
else:
sys.stdout.write('.')
sys.stdout.flush()
csvfile.close()
print "<-- Terminé"

fa39865b Mikaël Ates
print "--> Chargement des prise en charge..."
csvfile = open(os.path.join(db_path, db, 'pc.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
pc_cols = csvlines.next()
writer4.writerow(pc_cols + ['service', 'creation', 'commentaire'])
tables_data['pcs'] = {}
for line in csvlines:
data = _get_dict(pc_cols, line)
if line[1] in tables_data['pcs'].keys():
tables_data['pcs'][line[1]].append(data)
else:
tables_data['pcs'][line[1]] = [data]
csvfile.close()
5ca2d204 Mikaël Ates
print "<-- Terminé"

print "--> Ajout des contacts..."
contacts_per_patient = {}
csvfile = open(os.path.join(db_path, db, 'contacts.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
cols = csvlines.next()
6571bfae Mikaël Ates
writer3.writerow(cols + ['service', 'commentaire'])
5ca2d204 Mikaël Ates
i = 0
for line in csvlines:
i += 1
print "Nombre à traiter : %d" % i
csvfile.close()
csvfile = open(os.path.join(db_path, db, 'contacts.csv'), 'rb')
csvlines = csv.reader(csvfile, delimiter=';', quotechar='|')
csvlines.next()
for line in csvlines:
phone = extract_phone(line[13])
mobile = extract_phone(line[14])
contact_comment = ''
if _exist(line[13]):
contact_comment += "Travail : " + line[13] + ' - '
if _exist(line[14]):
contact_comment += "Mobile : " + line[14] + ' - '
if _exist(line[17]):
contact_comment += "Divers : " + line[17] + ' - '
last_name = treat_name(line[3])
first_name = treat_name(line[4])
gender = None
if line[2] == "1":
gender = 1
if line[2] == "2":
gender = 2
email = None
if _exist(line[15]):
email = line[15]
4ecbfa8e Mikaël Ates
birthplace = None
if _exist(line[6]):
birthplace = line[6]
6571bfae Mikaël Ates
social_security_id = get_nir(line[7], line[8], writer3, line, service)
5ca2d204 Mikaël Ates
if social_security_id == -1:
6571bfae Mikaël Ates
msg = 'Numéro %s de longueur diff de 13.' % line[7]
writer3.writerow(line + [service.name, msg])
contact_comment += "Numéro NIR invalide : " + line[7] + ' - '
5ca2d204 Mikaël Ates
social_security_id = None
birthdate = _to_date(line[5])
job = get_job(service, line[9])
parente = None
try:
if service.name == 'CAMSP':
parente = PatientRelatedLink.objects.get(old_camsp_id=_to_int(line[11]))
elif service.name == 'CMPP':
parente = PatientRelatedLink.objects.get(old_cmpp_id=_to_int(line[11]))
elif service.name == 'SESSAD DYS':
parente = PatientRelatedLink.objects.get(old_sessad_dys_id=_to_int(line[11]))
elif service.name == 'SESSAD TED':
parente = PatientRelatedLink.objects.get(old_sessad_ted_id=_to_int(line[11]))
except:
pass
twinning_rank = None
thirdparty_payer = False
begin_rights = None
end_rights = None
health_center = None
fa39865b Mikaël Ates
old_contact_id = line[0]
5ca2d204 Mikaël Ates
contact = PatientContact(phone=phone, mobile= mobile,
contact_comment=contact_comment,
last_name = last_name, first_name = first_name,
gender = gender, email = email, parente = parente,
social_security_id = social_security_id,
4ecbfa8e Mikaël Ates
birthdate = birthdate, job = job, birthplace=birthplace,
5ca2d204 Mikaël Ates
twinning_rank = twinning_rank,
thirdparty_payer = thirdparty_payer,
begin_rights = begin_rights,
end_rights = end_rights,
fa39865b Mikaël Ates
health_center = health_center,
old_contact_id=old_contact_id)
5ca2d204 Mikaël Ates
contact.save()

if not line[1] in adresses_per_old_id:
6571bfae Mikaël Ates
msg = 'Contact sans adresse'
writer3.writerow(line + [service.name, msg])
5ca2d204 Mikaël Ates
contact.delete()
else:
adresse, old_patient_id = adresses_per_old_id[line[1]]
contact.addresses.add(adresse)
# Ajouter l'adresse au contact
# Faire une liste des contacts par patient pour ajouter ensuite
if old_patient_id in contacts_per_patient.keys():
contacts_per_patient[old_patient_id].append(contact)
else:
contacts_per_patient[old_patient_id] = [contact]
i -= 1
if not (i % 10):
sys.stdout.write('%d' %i)
else:
sys.stdout.write('.')
sys.stdout.flush()
csvfile.close()
print "<-- Terminé"

d11b45e3 Benjamin Dauvergne
print "--> Ajout dossiers..."
print "Nombre à traiter : %d" % len(tables_data['dossiers'])
i = len(tables_data['dossiers'])
for dossier in tables_data['dossiers']:
8c87136a Mikaël Ates
date_accueil = None
date_diagnostic = None
date_traitement = None
date_clos = None
date_bilan = None
date_suivi = None
date_surveillance = None
1a0ef10d Mikaël Ates
date_retour = None
8c87136a Mikaël Ates
fss = []
date_accueil = _to_date(dossier['con_date'])
date_traitement = _to_date(dossier['ins_date'])
date_clos = _to_date(dossier['sor_date'])
if not (date_accueil or date_traitement or date_clos):
# no state date, the record is inconsistent
writer2.writerow([dossier[c].encode('utf-8') for c in d_cols] + [service.name, 'Non', "Aucune date d'état existante"])
continue
# Manage states
649e38fc Mikaël Ates
if date_accueil and date_traitement and date_accueil >= date_traitement:
8c87136a Mikaël Ates
date_accueil = None
649e38fc Mikaël Ates
if date_traitement and date_clos and date_traitement >= date_clos:
8c87136a Mikaël Ates
date_traitement = None
649e38fc Mikaël Ates
if date_accueil and date_clos and date_accueil >= date_clos:
1a0ef10d Mikaël Ates
date_accueil = None
8c87136a Mikaël Ates
if "SESSAD" in service.name:
# Il n'y a jamais eu de retour au SESSADs
if date_accueil:
fss.append((status_accueil, date_accueil, None))
if date_traitement:
fss.append((status_traitement, date_traitement, None))
if date_clos:
fss.append((status_clos, date_clos, None))
# Jamais de motif et provenance au retour
elif service.name == 'CAMSP':
date_retour = _to_date(dossier['ret_date'])
if date_accueil:
fss.append((status_accueil, date_accueil, None))
if not date_retour:
if date_traitement:
s = status_bilan
if dossier['suivi'] == '3':
s = status_suivi
elif dossier['suivi'] == '4':
s = status_surveillance
fa39865b Mikaël Ates
# 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."))
fss.append((s, date_traitement, ""))
8c87136a Mikaël Ates
else:
# Le retour supprime la précédente de clôture, on choisit le retour à j-1
if date_traitement:
# c'est l'inscription
1a0ef10d Mikaël Ates
if date_traitement < date_retour:
fss.append((status_suivi, date_traitement, "Etat de traitement indéterminé (Suivi par défaut)."))
old_clos_date = date_retour + relativedelta(days=-1)
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)."))
8c87136a Mikaël Ates
s = status_bilan
if dossier['suivi'] == '3':
s = status_suivi
elif dossier['suivi'] == '4':
s = status_surveillance
fa39865b Mikaël Ates
# 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."))
fss.append((s, date_retour, ""))
8c87136a Mikaël Ates
if date_clos:
if date_retour and date_clos < date_retour:
fa39865b Mikaël Ates
print 'La date de clôture ne peut être antérieure à la date de retour!'
8c87136a Mikaël Ates
else:
fss.append((status_clos, date_clos, None))
else:
fa39865b Mikaël Ates
date_retour = _to_date(dossier['ret_date'])
if date_accueil:
fss.append((status_accueil, date_accueil, None))
649e38fc Mikaël Ates
if date_traitement:
fss.append((status_diagnostic, date_traitement, "Inscription en diag mais pourrait etre en trait. A préciser"))
if (date_retour and date_clos and date_retour > date_clos) or not date_clos:
fa39865b Mikaël Ates
if old_id in tables_data['pcs'].keys():
pcs = tables_data['pcs'][old_id]
5fce3dc9 Mikaël Ates
last_pc = pcs[len(pcs)-1]
if last_pc['genre_pc'] == '2':
fa39865b Mikaël Ates
status, date, msg = fss[len(fss)-1]
last_date = date + relativedelta(days=1)
fss.append((status_traitement, last_date, "Date à préciser."))
649e38fc Mikaël Ates
else:
fss.append((status_clos, date_clos, None))
fa39865b Mikaël Ates
# Il faut l'historique des actes du patient
# Il faut pour chaque acte assigner à la pc
# on sait les derniers actes s'ils sont affecté ou non
# Il faudrait conserver avec les actes les num de facture ?
# rEvori el fonctionnement de l'assignation d'un acte a une pc
8c87136a Mikaël Ates
d11b45e3 Benjamin Dauvergne
for col in ('mdph', 'code_archive', 'aeeh', 'mdph_departement', 'pps', 'pps_deb', 'pps_fin', 'mdph_Debut', 'mdph_Fin'):
if _exist(dossier[col]):
8c87136a Mikaël Ates
writer2.writerow([dossier[c].encode('utf-8') for c in d_cols] + [service.name, 'Oui', "Données présentes non traitées"])
break
d11b45e3 Benjamin Dauvergne
#People
first_name = treat_name(dossier['prenom'])
last_name = treat_name(dossier['nom'])
display_name = None
gender = _to_int(dossier['nais_sexe'])
if not gender in (1,2):
gender = None
email = None
phone = None

#PatientContact
mobile = None
social_security_id = None
birthdate = _to_date(dossier['nais_date'])
twinning_rank = _to_int(dossier['nais_rang'])
# Pourra etre init à l'import des contacts après création du dossier
5ca2d204 Mikaël Ates
thirdparty_payer = False
d11b45e3 Benjamin Dauvergne
begin_rights = None
end_rights = None
health_center = None
addresses = None
contact_comment = None

#PatientRecord
creator = creator
# Pourra etre init à l'import des contacts après création du dossier
nationality = None
paper_id = None
comment = dossier['infos']
pause = False
5ca2d204 Mikaël Ates
if _exist(dossier['blocage']):
pause = True
confidential = False
if _exist(dossier['non_communication_ecole']):
confidential = True
d11b45e3 Benjamin Dauvergne
# Physiology and health data
size = _to_int(dossier['taille'])
weight = _to_int(dossier['poids'])
pregnancy_term = _to_int(dossier['terme'])
cranium_perimeter = None
chest_perimeter = None
apgar_score_one = _to_int(dossier['apgar_1'])
apgar_score_two = _to_int(dossier['apgar_5'])


# Inscription motive
# Up to now only used at the CAMSP
analysemotive = None
try:
analysemotive = AnalyseMotive.objects.get(id=_to_int(dossier['ins_motif']))
except:
pass
# Up to now only used at the CAMSP
familymotive = None
try:
familymotive = FamilyMotive.objects.get(id=_to_int(dossier['ins_motif_exprim']))
except:
pass
provenance = None
try:
provenance = Provenance.objects.get(old_id=_to_int(dossier['ins_provenance']), old_service=service.name)
except:
pass
5ca2d204 Mikaël Ates
# Up to now only used at the CAMSP
d11b45e3 Benjamin Dauvergne
advicegiver = None
5ca2d204 Mikaël Ates
try:
advicegiver = AdviceGiver.objects.get(id=_to_int(dossier['con_qui']))
except:
pass
d11b45e3 Benjamin Dauvergne
# Inscription motive
# Up to now only used at the CAMSP
outmotive = None
try:
outmotive = OutMotive.objects.get(id=_to_int(dossier['sor_motif']))
except:
pass
# Up to now only used at the CAMSP
outto = None
try:
outto = OutTo.objects.get(id=_to_int(dossier['sor_orientation']))
except:
pass

# Family
sibship_place = _to_int(dossier['nais_fratrie'])
nb_children_family = _to_int(dossier['nbr_enfants'])
parental_authority = None
try:
parental_authority = ParentalAuthorityType.objects.get(id=_to_int(dossier['autorite_parentale']))
except:
pass
# Up to now only used at the CAMSP
family_situation = None
try:
family_situation = FamilySituationType.objects.get(id=_to_int(dossier['situation_familiale']))
except:
pass
# Up to now only used at the CAMSP
child_custody = None
try:
child_custody = ParentalCustodyType.objects.get(id=_to_int(dossier['garde']))
except:
pass

5ca2d204 Mikaël Ates
rm_mother = get_rm(service, dossier['rm_mere'])
rm_father = get_rm(service, dossier['rm_pere'])
job_mother = get_job(service, dossier['prof_mere'])
job_father = get_job(service, dossier['prof_pere'])
family_comment = None

d11b45e3 Benjamin Dauvergne
# Transport
transportcompany = None
try:
if service.name == 'CAMSP':
transportcompany = TransportCompany.objects.get(old_camsp_id=_to_int(dossier['transport']))
elif service.name == 'CMPP':
transportcompany = TransportCompany.objects.get(old_cmpp_id=_to_int(dossier['transport']))
elif service.name == 'SESSAD DYS':
transportcompany = TransportCompany.objects.get(old_sessad_dys_id=_to_int(dossier['transport']))
elif service.name == 'SESSAD TED':
transportcompany = TransportCompany.objects.get(old_sessad_ted_id=_to_int(dossier['transport']))
except:
pass
transporttype = None
try:
transporttype = TransportType.objects.get(id=_to_int(dossier['type_transport']))
except:
pass

# FollowUp
externaldoctor = None
try:
externaldoctor = ExternalTherapist.objects.get(old_id=_to_int(dossier['medecin_exterieur']), old_service=service.name)
except:
pass
externalintervener = None
try:
externalintervener = ExternalWorker.objects.get(old_id=_to_int(dossier['intervenant_exterieur']), old_service=service.name)
except:
pass

old_id = dossier['id']
old_old_id = dossier['ancien_numero']

8c87136a Mikaël Ates
d11b45e3 Benjamin Dauvergne
patient, created = PatientRecord.objects.get_or_create(first_name = first_name,
last_name = last_name,
birthdate = birthdate,
twinning_rank = twinning_rank,
gender = gender,
display_name = display_name,
email = email,
phone = phone,
mobile = mobile,
contact_comment = contact_comment,
nationality = nationality,
paper_id = paper_id,
comment = comment,
pause = pause,
5ca2d204 Mikaël Ates
confidential = confidential,
d11b45e3 Benjamin Dauvergne
size = size,
weight = weight,
pregnancy_term = pregnancy_term,
cranium_perimeter = cranium_perimeter,
chest_perimeter = chest_perimeter,
apgar_score_one = apgar_score_one,
apgar_score_two = apgar_score_two,
analysemotive = analysemotive,
familymotive = familymotive,
provenance = provenance,
advicegiver = advicegiver,
outmotive = outmotive,
outto = outto,
sibship_place = sibship_place,
nb_children_family = nb_children_family,
parental_authority = parental_authority,
family_situation = family_situation,
5ca2d204 Mikaël Ates
rm_mother = rm_mother,
rm_father = rm_father,
job_mother = job_mother,
job_father = job_father,
family_comment = family_comment,
d11b45e3 Benjamin Dauvergne
child_custody = child_custody,
transportcompany = transportcompany,
transporttype = transporttype,
externaldoctor = externaldoctor,
externalintervener = externalintervener,
service=service,
creator=creator,
old_id = old_id,
old_old_id = old_old_id)

# if created:
# print 'Creation de%s' % patient
# else:
# print 'Patient %s existe' % patient

8c87136a Mikaël Ates
# Init states
fa39865b Mikaël Ates
if not fss:
print "Pas d'etat et le dossier patient %s (old_id) a ete cree!" % old_id
else:
fs = FileState(status=fss[0][0], author=creator, previous_state=None)
date_selected = fss[0][1]
fs.patient = patient
fs.date_selected = date_selected
fs.comment = fss[0][2]
fs.save()
patient.last_state = fs
patient.save()
if len(fss) > 1:
for status, date, comment in fss[1:]:
patient.set_state(status=status, author=creator, date_selected=date, comment=comment)

d11b45e3 Benjamin Dauvergne
if old_id in mises_per_patient.keys():
for quotation in mises_per_patient[old_id]:
if quotation.axe == 1:
patient.mises_1.add(quotation)
elif quotation.axe == 2:
patient.mises_2.add(quotation)
elif quotation.axe == 3:
patient.mises_3.add(quotation)
else:
raise

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

for t_the in ('the_medecin', 'the_referent', 'the_therapeute'):
try:
therapist = None
if service.name == 'CAMSP':
therapist = Worker.objects.get(old_camsp_id=_to_int(dossier[t_the]))
elif service.name == 'CMPP':
therapist = Worker.objects.get(old_cmpp_id=_to_int(dossier[t_the]))
elif service.name == 'SESSAD DYS':
therapist = Worker.objects.get(old_sessad_dys_id=_to_int(dossier[t_the]))
elif service.name == 'SESSAD TED':
therapist = Worker.objects.get(old_sessad_ted_id=_to_int(dossier[t_the]))
patient.coordinators.add(therapist)
except:
pass

5ca2d204 Mikaël Ates
# Initialisation adresses et contacts
if old_id in adresses_per_patient.keys():
for adresse in adresses_per_patient[old_id]:
patient.addresses.add(adresse)
if old_id in contacts_per_patient.keys():
for contact in contacts_per_patient[old_id]:
if contact.last_name == patient.last_name \
and contact.first_name == patient.first_name:
4ecbfa8e Mikaël Ates
# print "Le contact %s %s est le patient" % (contact.last_name, contact.first_name)
if not patient.birthdate:
patient.birthdate = contact.birthdate
patient.birthplace = contact.birthplace
5ca2d204 Mikaël Ates
patient.email = contact.email
patient.phone = contact.phone
patient.mobile = contact.mobile
patient.social_security_id = contact.social_security_id
patient.thirdparty_payer = contact.thirdparty_payer
patient.begin_rights = contact.begin_rights
patient.end_rights = contact.end_rights
patient.health_center = contact.health_center
patient.contact_comment = contact.contact_comment
fa39865b Mikaël Ates
patient.old_contact_id = contact.old_contact_id
5ca2d204 Mikaël Ates
patient.save()
contact.delete()
else:
patient.contacts.add(contact)

fa39865b Mikaël Ates
policyholder = None
health_center = None
other_health_center = None
if old_id in tables_data['pcs'].keys():
pcs = tables_data['pcs'][old_id]
5fce3dc9 Mikaël Ates
j = len(pcs)-1
fa39865b Mikaël Ates
found = False
last_pc = None
5fce3dc9 Mikaël Ates
while not found and j >= 0:
last_pc = pcs[j]
fa39865b Mikaël Ates
if 'contact_id' in last_pc.keys():
found = True
5fce3dc9 Mikaël Ates
j -= 1
fa39865b Mikaël Ates
if not found:
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"])
# print "Pas de d'assure pour le patient %s" % old_id
# print "Le patient sera l'assure"
else:
try:
caisse = None
centre = None
lg = None
policyholder = patient.contacts.get(old_contact_id=_to_int(last_pc['contact_id']))
if last_pc['caisse_id'] in tables_data['caisses'].keys():
lg = tables_data['caisses'][last_pc['caisse_id']]['tp']
if len(lg) < 2:
lg = ['0', lg]
lg = ''.join(lg)
caisse = tables_data['caisses'][last_pc['caisse_id']]['caisse']
while len(caisse) < 3:
caisse = ['0', caisse]
caisse = ''.join(caisse)
centre = tables_data['caisses'][last_pc['caisse_id']]['centre']
while len(centre) < 4:
centre = ['0', centre]
centre = ''.join(centre)
if lg and caisse:
health_centers = HealthCenter.objects.filter(large_regime__code=lg, health_fund=caisse)
if health_centers and len(health_centers.all()) == 1:
health_center = health_centers[0]
if last_pc['centre']:
while len(last_pc['centre']) < 4:
last_pc['centre'] = ['0', last_pc['centre']]
last_pc['centre'] = ''.join(last_pc['centre'])
other_health_center = last_pc['centre']
elif health_centers and len(health_centers.all()) > 1:
health_centers = None
if last_pc['centre']:
while len(last_pc['centre']) < 4:
last_pc['centre'] = ['0', last_pc['centre']]
last_pc['centre'] = ''.join(last_pc['centre'])
5fce3dc9 Mikaël Ates
# print "centre 1 %s" % last_pc['centre']
fa39865b Mikaël Ates
health_centers = HealthCenter.objects.filter(large_regime__code=lg, health_fund=caisse,
code = last_pc['centre'])
elif centre:
5fce3dc9 Mikaël Ates
# print "centre 2 %s" % centre
fa39865b Mikaël Ates
health_centers = HealthCenter.objects.filter(large_regime__code=lg, health_fund=caisse,
code = centre)
if health_centers and len(health_centers.all()) == 1:
health_center = health_centers[0]
elif health_centers:
# print "Plusieurs caisses avec le meme centre, patient %s" % old_id
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
[service.name, '', "Plusieurs caisses avec le meme centre, patient %s" % old_id])
else:
# print "Caisse non determinee par code centre, patient %s" % old_id
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
[service.name, '', "Caisse non determinee par code centre, patient %s" % old_id])
else:
# print 'Caisse non trouvee avec ce numero de caisse et grand regime, patient %s' % old_id
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
[service.name, '', 'Caisse non trouvee avec ce numero de caisse et grand regime, patient %s' % old_id])
else:
# print 'Infos manquantes dans fichiers des caisses, patient %s' % old_id
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
[service.name, '', 'Infos manquantes dans fichiers des caisses, patient %s' % old_id])
else:
# print 'Pas de caisse dans le fichiers caisse avec l id %s, patient %s' % (last_pc['caisse_id'], old_id)
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
[service.name, '', 'Pas de caisse dans le fichier caisse avec l id %s, patient %s' % (last_pc['caisse_id'], old_id)])
except:
# print "Pas de contact avec id %s, patient %s" % (last_pc['contact_id'], old_id)
writer4.writerow([last_pc[c].encode('utf-8') for c in pc_cols] + \
[service.name, '', "Pas d'assuré existant, le patient est choisi."])

else:
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"])
# print "Pas de pc pour le patient %s" % old_id
# print "Le patient sera l'assure"
if not policyholder:
policyholder = patient.patientcontact
policyholder.health_center = health_center
policyholder.other_health_center = other_health_center
policyholder.save()
patient.policyholder = policyholder
patient.save()


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

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


4ecbfa8e Mikaël Ates
#Etat des dossiers
6571bfae Mikaël Ates
4ecbfa8e Mikaël Ates
# patient.policyholder soit le contact, d'il n'y en a qu'un
# au cmmp, cf la pc
5ca2d204 Mikaël Ates
# Dossier en pause facturation! champs pause sur le dossier OK
# si aucun contact, ou aucun contact avec un Nir valide!

#Tiers-payant ? healthcenter ?

8c87136a Mikaël Ates
# Notifications au sessad, il n'y en a pas!
6571bfae Mikaël Ates
d11b45e3 Benjamin Dauvergne
# i += 1
# print 'Fin de traitement pour le dossier %s' % patient
# if i >= 10:
# break
i -= 1
if not (i % 10):
5ca2d204 Mikaël Ates
sys.stdout.write('%d' %i)
d11b45e3 Benjamin Dauvergne
else:
sys.stdout.write('.')
sys.stdout.flush()

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

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


# policyholder = None
# contacts = None


if __name__ == "__main__":
fa39865b Mikaël Ates
import_dossiers_phase_1()