Project

General

Profile

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

calebasse / calebasse / ressources / models.py @ d3eeac81

1
# -*- coding: utf-8 -*-
2

    
3
from datetime import date, datetime
4
import bisect
5

    
6
from django.db import models
7
from django.db.models import query
8
from model_utils import Choices
9

    
10
from calebasse.models import PhoneNumberField, ZipCodeField
11

    
12
from model_utils.managers import PassThroughManager
13

    
14

    
15
class ServiceLinkedQuerySet(query.QuerySet):
16
    def for_service(self, service, allow_global=True):
17
        '''Allows service local and service global objects'''
18
        return self.filter(models.Q(service=service)
19
                |models.Q(service__isnull=allow_global))
20

    
21
ServiceLinkedManager = PassThroughManager.for_queryset_class(ServiceLinkedQuerySet)
22

    
23
class NamedAbstractModel(models.Model):
24
    name = models.CharField(max_length=150, verbose_name=u'Nom')
25

    
26
    def __unicode__(self):
27
        return self.name
28

    
29
    def __repr__(self):
30
        return '<%s %r>' % (self.__class__.__name__, unicode(self))
31

    
32
    class Meta:
33
        abstract = True
34
        ordering = ['name']
35

    
36
class ServiceLinkedAbstractModel(models.Model):
37
    objects = ServiceLinkedManager()
38
    service = models.ForeignKey('Service', null=True, blank=True)
39

    
40
    class Meta:
41
        abstract = True
42

    
43
DEST_TYPE = {
44
    # code grand regime: type destinataire
45
    '01': 'CT', # regime general
46
    '02': 'MA', # agricole
47
    '03': 'SR', # independant
48
    '04': 'CF', # sncf
49
    '05': 'RP', # ratp
50
    '06': 'EN', # invalide marine
51
    '07': 'RM', # minier
52
    '08': 'CM', # militaire
53
    '10': 'CE', # notaires
54
    '12': 'CI', # cci paris
55
    '14': 'AN', # assemblee nationale
56
    '15': 'SE', # senat
57
    '16': 'PB', # port autonome bordeaux
58
    '90': 'CC', # cultes
59
    '91': 'SM', # ensuite toutes les mutuelles nationales
60
    '92': 'SM',
61
    '93': 'SM',
62
    '94': 'SM',
63
    '95': 'SM',
64
    '96': 'SM',
65
    '99': 'SM',
66
}
67

    
68

    
69
class HealthCenter(NamedAbstractModel):
70
    class Meta:
71
        verbose_name = u'Centre d\'assurance maladie'
72
        verbose_name_plural = u'Centres d\'assurances maladie'
73

    
74
    def __unicode__(self):
75
        return self.large_regime.code + ' ' + self.health_fund + ' ' + self.code + ' ' + self.name
76

    
77
    code = models.CharField(verbose_name=u"Code du centre",
78
            max_length=4,
79
            null=True, blank=True)
80
    large_regime = models.ForeignKey('LargeRegime',
81
            verbose_name=u"Grand régime")
82
    dest_organism = models.CharField(max_length=8,
83
            verbose_name=u"Organisme destinataire")
84
    computer_center_code = models.CharField(max_length=8,
85
            verbose_name=u"Code centre informatique",
86
            null=True, default=True)
87
    abbreviation = models.CharField(verbose_name=u'Abbrévation',
88
            max_length=8,
89
            null=True, default=True)
90
    health_fund = models.CharField(verbose_name=u"Numéro de la caisse",
91
            max_length=3)
92
    active = models.BooleanField(default=True, verbose_name=u"Active")
93
    address = models.CharField(max_length=120, verbose_name=u"Adresse")
94
    address_complement = models.CharField(max_length=120, blank=True,
95
            null=True, default=None, verbose_name=u"Adresse complémentaire")
96
    zip_code = models.CharField(max_length=8, verbose_name=u"Code postal")
97
    city = models.CharField(max_length=80, verbose_name=u"Ville")
98
    phone = models.CharField(max_length=30, verbose_name=u"Téléphone")
99
    fax = models.CharField(max_length=30,
100
            null=True, blank=True, verbose_name=u"Fax")
101
    email = models.EmailField(
102
            null=True, blank=True, verbose_name=u"Courriel")
103
    accounting_number = models.CharField(max_length=30,
104
             null=True, blank=True, verbose_name=u"Numéro de compte")
105
    correspondant = models.CharField(max_length=80, verbose_name=u"Correspondant")
106
    hc_invoice = models.ForeignKey('HealthCenter',
107
            verbose_name=u"Centre pour facturation (optionnel)",
108
            null=True, blank=True, default=None)
109

    
110
    def b2_000(self):
111
        '''
112
        renvoie le numéro destinataire selon la norme B2, type 000
113
        '''
114
        return DEST_TYPE[self.large_regime.code] + '000000' + self.large_regime.code[0:2] + self.computer_center_code[0:3] + self.dest_organism[0:3]
115

    
116

    
117
class LargeRegime(NamedAbstractModel):
118
    class Meta:
119
        verbose_name = u'Grand régime'
120
        verbose_name_plural = u'Grands régimes'
121

    
122
    def __unicode__(self):
123
        return self.code + ' ' + self.name
124

    
125
    code = models.CharField(verbose_name=u"Code grand régime",
126
            max_length=2)
127

    
128

    
129
class TransportCompany(NamedAbstractModel):
130
    description = models.TextField(blank=True, null=True, default=None)
131
    address = models.CharField(max_length=120,
132
            verbose_name=u"Adresse", blank=True, null=True, default=None)
133
    address_complement = models.CharField(max_length=120,
134
            blank=True,
135
            null=True,
136
            default=None,
137
            verbose_name=u"Complément d'adresse")
138
    zip_code = ZipCodeField(verbose_name=u"Code postal",
139
        blank=True, null=True, default=None)
140
    city = models.CharField(max_length=80, verbose_name=u"Ville",
141
        blank=True, null=True, default=None)
142
    phone = PhoneNumberField(verbose_name=u"Téléphone",
143
        blank=True, null=True, default=None)
144
    fax = models.CharField(max_length=30,
145
            blank=True, null=True, default=None)
146
    email = models.EmailField(blank=True, null=True)
147
    correspondant = models.CharField(max_length=80, blank=True, null=True)
148
    old_camsp_id = models.CharField(max_length=256,
149
            verbose_name=u'Ancien ID au CAMSP', blank=True, null=True)
150
    old_cmpp_id = models.CharField(max_length=256,
151
            verbose_name=u'Ancien ID au CMPP', blank=True, null=True)
152
    old_sessad_dys_id = models.CharField(max_length=256,
153
            verbose_name=u'Ancien ID au SESSAD TED', blank=True, null=True)
154
    old_sessad_ted_id = models.CharField(max_length=256,
155
            verbose_name=u'Ancien ID au SESSAD DYS', blank=True, null=True)
156

    
157
    class Meta:
158
        verbose_name = u'Compagnie de transport'
159
        verbose_name_plural = u'Compagnies de transport'
160

    
161

    
162
class ManagementCode(NamedAbstractModel):
163
    class Meta:
164
        verbose_name = u'Code de gestion'
165
        verbose_name_plural = u'Codes de gestion'
166

    
167
    def __unicode__(self):
168
        return self.code + ' ' + self.name
169

    
170
    code = models.CharField(verbose_name=u"Code",
171
            max_length=10)
172
    old_id = models.CharField(max_length=256,
173
            verbose_name=u'Ancien ID', blank=True, null=True)
174

    
175

    
176
class UninvoicableCode(models.Model):
177
    class Meta:
178
        verbose_name = u'Code de non-facturation'
179
        verbose_name_plural = u'Codes de non-facturation'
180

    
181

    
182
class Office(NamedAbstractModel):
183
    class Meta:
184
        verbose_name = u'Établissement'
185
        verbose_name_plural = u'Établissements'
186

    
187
    def __unicode__(self):
188
        return self.name
189

    
190
    description = models.TextField(blank=True, null=True)
191

    
192
    # Contact
193
    phone = PhoneNumberField(verbose_name=u"Téléphone", blank=True, null=True)
194
    fax = PhoneNumberField(verbose_name=u"Fax", blank=True, null=True)
195
    email = models.EmailField(blank=True, null=True)
196

    
197
    # Address
198
    address = models.CharField(max_length=120,
199
            verbose_name=u"Adresse", blank=True, null=True, default=None)
200
    address_complement = models.CharField(max_length=120,
201
            blank=True,
202
            null=True,
203
            default=None,
204
            verbose_name=u"Complément d'adresse")
205
    zip_code = ZipCodeField(verbose_name=u"Code postal",
206
            blank=True, null=True, default=None)
207
    city = models.CharField(max_length=80, verbose_name=u"Ville",
208
            blank=True, null=True, default=None)
209

    
210
    # TODO: add this fields : finess, suite, dm, dpa, genre, categorie, statut_juridique, mft, mt, dmt
211

    
212
class SchoolType(NamedAbstractModel):
213
    class Meta:
214
        verbose_name = u'Type du lieu de socialisation'
215
        verbose_name_plural = u'Types du lieu de socialisation'
216

    
217
    services = models.ManyToManyField('ressources.Service')
218

    
219
class School(NamedAbstractModel):
220
    class Meta:
221
        verbose_name = u'Lieu de socialisation'
222
        verbose_name_plural = u'Lieux de socialisation'
223

    
224
    def __unicode__(self):
225
        if self.school_type.name != 'Inconnu':
226
            return unicode(self.school_type) + ' ' + self.name
227
        return self.name
228

    
229
    school_type = models.ForeignKey('ressources.SchoolType',
230
        verbose_name=u"Type d'établissement")
231
    description = models.TextField(blank=True, null=True, default=None)
232
    address = models.CharField(max_length=120,
233
            verbose_name=u"Adresse", blank=True, null=True, default=None)
234
    address_complement = models.CharField(max_length=120,
235
            blank=True,
236
            null=True,
237
            default=None,
238
            verbose_name=u"Complément d'adresse")
239
    zip_code = ZipCodeField(verbose_name=u"Code postal",
240
        blank=True, null=True, default=None)
241
    city = models.CharField(max_length=80, verbose_name=u"Ville",
242
        blank=True, null=True, default=None)
243
    phone = PhoneNumberField(verbose_name=u"Téléphone",
244
        blank=True, null=True, default=None)
245
    fax = models.CharField(max_length=30,
246
            blank=True, null=True, default=None)
247
    email = models.EmailField(blank=True, null=True, default=None)
248
    director_name = models.CharField(max_length=70,
249
            blank=True, null=True, default=None,
250
            verbose_name=u"Nom du directeur")
251
    old_id = models.CharField(max_length=256,
252
            verbose_name=u'Ancien ID', blank=True, null=True)
253
    old_service = models.CharField(max_length=256,
254
            verbose_name=u'Ancien Service', blank=True, null=True)
255

    
256

    
257
class SchoolTeacherRole(NamedAbstractModel):
258
    class Meta:
259
        verbose_name = u'Type de rôle des professeurs'
260
        verbose_name_plural = u'Types de rôle des professeurs'
261

    
262

    
263
class SchoolLevel(NamedAbstractModel):
264
    old_id = models.CharField(max_length=256,
265
            verbose_name=u'Ancien ID', blank=True, null=True)
266
    old_service = models.CharField(max_length=256,
267
            verbose_name=u'Ancien Service', blank=True, null=True)
268

    
269
    class Meta:
270
        verbose_name = u'Classe'
271
        verbose_name_plural = u'Classes'
272

    
273

    
274
class SocialisationDuration(models.Model):
275
    class Meta:
276
        verbose_name = u'Période de socialisation'
277
        verbose_name_plural = u'Périodes de socialisation'
278

    
279
    school = models.ForeignKey('ressources.School',
280
        verbose_name=u'Lieu de socialisation',
281
        blank=True, null=True)
282
    level = models.ForeignKey('ressources.SchoolLevel',
283
        verbose_name=u'Classe',
284
        blank=True, null=True)
285
    redoublement = models.BooleanField(verbose_name=u"Redoublement",
286
            default=False)
287
    start_date = models.DateField(verbose_name=u"Date d'arrivée")
288
    contact = models.CharField(verbose_name=u"Contact", max_length=200, blank=True, null=True, default=None)
289
    end_date = models.DateField(verbose_name=u"Date de départ",
290
        blank=True, null=True)
291
    created = models.DateTimeField(u'Création', auto_now_add=True)
292
    comment = models.TextField(max_length=3000,
293
        blank=True, null=True, verbose_name=u"Commentaire")
294

    
295

    
296
class InscriptionMotive(NamedAbstractModel):
297
    class Meta:
298
        verbose_name = u'Motif d\'inscription'
299
        verbose_name_plural = u'Motifs d\'inscription'
300

    
301
class Provenance(NamedAbstractModel):
302
    old_id = models.CharField(max_length=256,
303
            verbose_name=u'Ancien ID', blank=True, null=True)
304
    old_service = models.CharField(max_length=256,
305
            verbose_name=u'Ancien Service', blank=True, null=True)
306
    class Meta:
307
        verbose_name = u'Provenance'
308
        verbose_name_plural = u'Provenances'
309

    
310

    
311
class Nationality(NamedAbstractModel):
312
    class Meta:
313
        verbose_name = u'Nationalité'
314
        verbose_name_plural = u'Nationalités'
315

    
316

    
317
class Job(NamedAbstractModel):
318
    class Meta:
319
        verbose_name = u'Profession'
320
        verbose_name_plural = u'Professions'
321

    
322

    
323
class RoomQuerySet(query.QuerySet):
324
    def for_etablissement(self, etablissement):
325
        return self.filter(etablissement=etablissement)
326

    
327
    def for_service(self, service):
328
        return self.filter(etablissement__service=service)
329

    
330

    
331
class Room(NamedAbstractModel):
332
    objects = PassThroughManager.for_queryset_class(RoomQuerySet)()
333
    etablissement = models.ForeignKey('Office')
334

    
335
    class Meta:
336
        verbose_name = u'Salle'
337
        verbose_name_plural = u'Salles'
338

    
339
class AnalyseMotive(NamedAbstractModel):
340
    class Meta:
341
        verbose_name = u"Motif analysé"
342
        verbose_name_plural = u"Motifs analysés"
343

    
344
class FamilyMotive(NamedAbstractModel):
345
    class Meta:
346
        verbose_name = u"Motif familiale"
347
        verbose_name_plural = u"Motifs familiaux"
348

    
349
class OutMotive(NamedAbstractModel):
350
    class Meta:
351
        verbose_name = u"Motif de sortie"
352
        verbose_name_plural = u"Motifs de sortie"
353

    
354
class OutTo(NamedAbstractModel):
355
    class Meta:
356
        verbose_name = u"Orientation de sortie"
357
        verbose_name_plural = u"Orientations de sortie"
358

    
359
class AdviceGiver(NamedAbstractModel):
360
    class Meta:
361
        verbose_name = u"Conseilleur"
362
        verbose_name_plural = u"Conseilleurs"
363

    
364
class Service(NamedAbstractModel):
365
    admin_only = True
366

    
367
    slug = models.SlugField()
368
    description = models.TextField()
369

    
370
    # Contact
371
    phone = PhoneNumberField(verbose_name=u"Téléphone")
372
    fax = PhoneNumberField(verbose_name=u"Fax")
373
    email = models.EmailField()
374

    
375
    class Meta:
376
        verbose_name = u'Service'
377
        verbose_name_plural = u'Services'
378

    
379
class ActTypeQuerySet(query.QuerySet):
380
    def for_service(self, service):
381
        return self.filter(service=service)
382

    
383
class ActType(NamedAbstractModel, ServiceLinkedAbstractModel):
384
    objects = PassThroughManager.for_queryset_class(ActTypeQuerySet)()
385
    billable = models.BooleanField(default=True, verbose_name=u"Facturable")
386
    old_id = models.CharField(max_length=256,
387
            verbose_name=u'Ancien ID', blank=True, null=True)
388
    display_first = models.BooleanField(default=False, verbose_name=u"Acte principalement utilisé")
389

    
390
    class Meta(NamedAbstractModel.Meta):
391
        verbose_name = u'Type d\'actes'
392
        verbose_name_plural = u'Types d\'actes'
393
        ordering = ('-display_first','name')
394

    
395
class ParentalAuthorityType(NamedAbstractModel):
396
    class Meta:
397
        verbose_name = u'Type d\'autorité parentale'
398
        verbose_name_plural = u'Types d\'autorités parentales'
399

    
400

    
401
class ParentalCustodyType(NamedAbstractModel):
402
    class Meta:
403
        verbose_name = u'Type de gardes parentales'
404
        verbose_name_plural = u'Types de gardes parentales'
405

    
406

    
407
class SessionType(NamedAbstractModel):
408
    class Meta:
409
        verbose_name = u'Type de séance'
410
        verbose_name_plural = u'Types de séances'
411

    
412

    
413
class FamilySituationType(NamedAbstractModel):
414
    class Meta:
415
        verbose_name = u'Type de situation familiale'
416
        verbose_name_plural = u'Types de situations familiales'
417

    
418

    
419
class MaritalStatusType(NamedAbstractModel):
420
    class Meta:
421
        verbose_name = u'Régime matrimonial'
422
        verbose_name_plural = u'Régimes matrimoniaux'
423

    
424

    
425
class TransportType(NamedAbstractModel):
426
    class Meta:
427
        verbose_name = u'Type de transport'
428
        verbose_name_plural = u'Types de transports'
429

    
430

    
431
class WorkerType(NamedAbstractModel):
432
    intervene = models.BooleanField(
433
            verbose_name=u'Intervenant',
434
            blank=True)
435

    
436
    class Meta:
437
        verbose_name = u'Type de personnel'
438
        verbose_name_plural = u'Types de personnel'
439

    
440

    
441
AXIS =  Choices(
442
        (1, 'Axe I : catégories cliniques'),
443
        (2, 'Axe II : facteurs organiques'),
444
        (3, 'Axe II : facteurs environnementaux'),
445
)
446

    
447
class CodeCFTMEA(NamedAbstractModel):
448
    code = models.IntegerField(verbose_name=u"Code")
449
    axe = models.IntegerField(verbose_name=u"Axe", choices=AXIS,
450
            max_length=1)
451
    ordering_code = models.CharField(max_length=20, blank=True, null=True,
452
            verbose_name=u"Classification")
453

    
454
    def __unicode__(self):
455
        return "%s %s" % (self.ordering_code, self.name)
456

    
457
    class Meta:
458
        ordering = ['ordering_code']
459
        verbose_name = u'Code CFTMEA'
460
        verbose_name_plural = u'Codes CFTMEA'
461

    
462
class MDPH(models.Model):
463
    class Meta:
464
        verbose_name = u'Etablissement MDPH'
465
        verbose_name_plural = u'Etablissements MDPH'
466

    
467
    def __unicode__(self):
468
        return self.department
469

    
470
    department = models.CharField(max_length=200,
471
            verbose_name=u"Département")
472

    
473
    description = models.TextField(blank=True, null=True)
474

    
475
    # Contact
476
    phone = PhoneNumberField(verbose_name=u"Téléphone", blank=True, null=True)
477
    fax = PhoneNumberField(verbose_name=u"Fax", blank=True, null=True)
478
    email = models.EmailField(blank=True, null=True)
479
    website = models.CharField(max_length=200,
480
            verbose_name=u"Site Web", blank=True, null=True)
481

    
482
    # Address
483
    address = models.CharField(max_length=120,
484
            verbose_name=u"Adresse", blank=True, null=True)
485
    address_complement = models.CharField(max_length=120,
486
            blank=True,
487
            null=True,
488
            default=None,
489
            verbose_name=u"Complément d'addresse")
490
    zip_code = ZipCodeField(verbose_name=u"Code postal",
491
        blank=True, null=True)
492
    city = models.CharField(max_length=80,
493
            verbose_name=u"Ville", blank=True, null=True)
494

    
495
class MDPHRequest(models.Model):
496
    class Meta:
497
        verbose_name = u'Demande MDPH'
498
        verbose_name_plural = u'Demandes MDPH'
499

    
500
    start_date = models.DateField(verbose_name=u"Date de la demande")
501
    mdph = models.ForeignKey('ressources.MDPH',
502
            verbose_name=u"MDPH")
503
    comment = models.TextField(max_length=3000,
504
        blank=True, null=True, verbose_name=u"Commentaire")
505
    created = models.DateTimeField(u'Création', auto_now_add=True)
506

    
507
MDPH_HELP =  Choices(
508
        (0, "Non défini"),
509
        (1, "AEEH (Allocation d'éducation de l'enfant handicapé)"),
510
        (2, 'AVS (Assistant de vie scolaire)'),
511
        (3, 'EVS (Emplois de vie scolaire)'),
512
)
513

    
514
class MDPHResponse(models.Model):
515
    class Meta:
516
        verbose_name = u'Réponse MDPH'
517
        verbose_name_plural = u'Réponses MDPH'
518

    
519
    start_date = models.DateField(verbose_name=u"Date de début")
520
    end_date = models.DateField(verbose_name=u"Date de fin")
521
    mdph = models.ForeignKey('ressources.MDPH',
522
            verbose_name=u"MDPH")
523
    comment = models.TextField(max_length=3000,
524
        blank=True, null=True, verbose_name=u"Commentaire")
525
    created = models.DateTimeField(u'Création', auto_now_add=True)
526
    type_aide = models.IntegerField(verbose_name=u"Type d'aide", choices=MDPH_HELP,
527
            max_length=1, default=0)
528
    name =  models.CharField(max_length=200,
529
            verbose_name=u"Nom", blank=True, null=True)
530
    rate =  models.CharField(max_length=10,
531
            verbose_name=u"Taux", blank=True, null=True)
532

    
533

    
534
class HolidayType(NamedAbstractModel):
535
    for_group = models.BooleanField(
536
            verbose_name=u'Absence de groupe',
537
            blank=True)
538

    
539
    class Meta:
540
        verbose_name = u"Type d'absence"
541
        verbose_name_plural = u"Types d'absence"
542

    
543
class PatientRelatedLink(NamedAbstractModel):
544
    old_camsp_id = models.CharField(max_length=256,
545
            verbose_name=u'Ancien ID au CAMSP', blank=True, null=True)
546
    old_cmpp_id = models.CharField(max_length=256,
547
            verbose_name=u'Ancien ID au CMPP', blank=True, null=True)
548
    old_sessad_dys_id = models.CharField(max_length=256,
549
            verbose_name=u'Ancien ID au SESSAD TED', blank=True, null=True)
550
    old_sessad_ted_id = models.CharField(max_length=256,
551
            verbose_name=u'Ancien ID au SESSAD DYS', blank=True, null=True)
552
    class Meta:
553
        verbose_name = u'Type de lien avec le patient (parenté)'
554
        verbose_name_plural = u'Types de lien avec le patient (parenté)'
555

    
556
class PricePerAct(models.Model):
557
    price = models.DecimalField(verbose_name=u"Tarif", max_digits=5, decimal_places=2)
558
    start_date = models.DateField(verbose_name=u"Prise d'effet")
559

    
560
    class Pricing(object):
561
        def __init__(self, ppas):
562
            ppas = ppas.order_by('start_date')
563
            self.dates = [ppa.start_date for ppa in ppas]
564
            self.ppas = list(ppas)
565

    
566
        def price_at_date(self, date):
567
            i = bisect.bisect(self.dates, date)
568
            if i == 0:
569
                raise RuntimeError('No existing price at date %s' % date)
570
            return self.ppas[i-1].price
571

    
572
    class Meta:
573
        verbose_name = u"Tarif horaire de l'acte"
574
        verbose_name_plural = u"Tarifs horaires de l'acte"
575

    
576
    @classmethod
577
    def pricing(cls):
578
        return cls.Pricing(cls.objects.all())
579

    
580
    @classmethod
581
    def get_price(cls, at_date=None):
582
        if not at_date:
583
            at_date = date.today()
584
        if isinstance(at_date, datetime):
585
            at_date = date(day=at_date.day, month=at_date.month,
586
                year=at_date.year)
587
        found = cls.objects.filter(start_date__lte = at_date).latest('start_date')
588
        if not found:
589
            raise Exception('No price to apply')
590
        return found.price
591

    
592
    def __unicode__(self):
593
        return str(self.price) + ' (' + str(self.start_date) + ')'
(4-4/7)