Project

General

Profile

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

calebasse / calebasse / facturation / tests.py @ 8b60637d

1
# -*- coding: utf-8 -*-
2
import time
3
from datetime import datetime, date
4
from dateutil import rrule
5
from dateutil.relativedelta import relativedelta
6

    
7
from django.test import TestCase
8
from django.contrib.auth.models import User
9

    
10
from calebasse.actes.validation import automated_validation, \
11
    are_all_acts_of_the_day_locked, \
12
    get_days_with_acts_not_locked
13
from calebasse.actes.models import Act
14
from calebasse.agenda.models import EventWithAct
15
from calebasse.dossiers.models import create_patient, PatientRecord, \
16
    SessadHealthCareNotification, CmppHealthCareTreatment, CmppHealthCareDiagnostic
17
from calebasse.dossiers.models import Status
18
from calebasse.ressources.models import ActType, Service, WorkerType
19
from calebasse.personnes.models import Worker
20

    
21
from list_acts import (list_acts_for_billing_CAMSP,
22
    list_acts_for_billing_SESSAD, list_acts_for_billing_CMPP,
23
    list_acts_for_billing_CMPP)
24

    
25
from models import PricePerAct, Invoicing
26

    
27

    
28
class FacturationTest(TestCase):
29
    fixtures = ['services', 'filestates']
30
    def setUp(self):
31
        self.creator = User.objects.create(username='John')
32

    
33
        self.wtype = WorkerType.objects.create(name='ElDoctor', intervene=True)
34
        self.therapist1 = Worker.objects.create(first_name='Bob', last_name='Leponge', type=self.wtype)
35
        self.therapist2 = Worker.objects.create(first_name='Jean', last_name='Valjean', type=self.wtype)
36
        self.therapist3 = Worker.objects.create(first_name='Pierre', last_name='PaulJacques', type=self.wtype)
37
        self.act_type = ActType.objects.create(name='trepanation')
38

    
39
    def test_facturation_camsp(self):
40
        service_camsp = Service.objects.get(name='CAMSP')
41

    
42
        patient_a = create_patient('a', 'A', service_camsp, self.creator, date_selected=datetime(2020, 10, 5))
43
        act0 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
44
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 6, 10, 15),
45
                end_datetime=datetime(2020, 10, 6, 12, 20))
46
        status_suivi = Status.objects.filter(services__name='CAMSP').filter(type='SUIVI')[0]
47
        patient_a.set_state(status_suivi, self.creator, date_selected=datetime(2020, 10, 7))
48
        act1 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
49
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 7, 10, 15),
50
                end_datetime=datetime(2020, 10, 7, 12, 20))
51
        act2 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
52
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 7, 14, 15),
53
                end_datetime=datetime(2020, 10, 7, 16, 20))
54
        act3 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
55
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 7, 16, 20),
56
                end_datetime=datetime(2020, 10, 7, 17, 20))
57
        status_clos = Status.objects.filter(services__name='CAMSP').filter(type='CLOS')[0]
58
        patient_a.set_state(status_clos, self.creator, date_selected=datetime(2020, 10, 8))
59
        act4 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
60
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 8, 10, 15),
61
                end_datetime=datetime(2020, 10, 8, 12, 20))
62

    
63
        patient_b = create_patient('b', 'B', service_camsp, self.creator, date_selected=datetime(2020, 10, 4))
64
        act5 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_b, [self.therapist3],
65
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 4, 10, 15),
66
                end_datetime=datetime(2020, 10, 4, 12, 20))
67
        act6 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_b, [self.therapist3],
68
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 5, 10, 15),
69
                end_datetime=datetime(2020, 10, 5, 12, 20))
70
        act6.act.set_state('ABS_EXC', self.creator)
71
        act7 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_b, [self.therapist3],
72
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 5, 10, 15),
73
                end_datetime=datetime(2020, 10, 5, 12, 20))
74
        act7.switch_billable = True
75
        act7.save()
76
        patient_b.set_state(status_suivi, self.creator, date_selected=datetime(2020, 10, 6))
77
        act8 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_b, [self.therapist3],
78
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 7, 10, 15),
79
                end_datetime=datetime(2020, 10, 7, 12, 20))
80
        act9 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_b, [self.therapist3],
81
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 7, 14, 15),
82
                end_datetime=datetime(2020, 10, 7, 16, 20))
83
        act10 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_b, [self.therapist3],
84
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 7, 16, 20),
85
                end_datetime=datetime(2020, 10, 7, 17, 20))
86
        act11 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_b, [self.therapist3],
87
                self.act_type, service_camsp, start_datetime=datetime(2020, 10, 8, 10, 15),
88
                end_datetime=datetime(2020, 10, 8, 12, 20))
89
        patient_b.set_state(status_clos, self.creator, date_selected=datetime(2020, 10, 9))
90

    
91
        automated_validation(datetime(2020, 10, 5), service_camsp, self.creator)
92
        automated_validation(datetime(2020, 10, 6), service_camsp, self.creator)
93
        automated_validation(datetime(2020, 10, 7), service_camsp, self.creator)
94
        automated_validation(datetime(2020, 10, 8), service_camsp, self.creator)
95

    
96
        not_locked, days_not_locked, not_valide, not_billable, acts_pause, rejected, selected = \
97
            list_acts_for_billing_CAMSP(datetime(2020, 10, 4), datetime(2020, 10, 8), service_camsp)
98

    
99
        self.assertEqual(len(days_not_locked), 1)
100

    
101
        self.assertTrue(act1.act in selected[patient_a])
102
        self.assertTrue(act2.act in selected[patient_a])
103
        self.assertTrue(act3.act in selected[patient_a])
104
        acts_rejected = [x[0] for x in rejected[patient_a]]
105
        self.assertTrue(act0.act in acts_rejected)
106
        self.assertTrue(act4.act in acts_rejected)
107

    
108
        acts_not_locked = [x[0] for x in not_locked[patient_b]]
109
        self.assertTrue(act5.act in acts_not_locked)
110
        acts_not_valide = [x[0] for x in not_valide[patient_b]]
111
        self.assertTrue(act6.act in acts_not_valide)
112
        print not_billable
113
        self.assertTrue(act7.act in not_billable[patient_b])
114
        self.assertTrue(act8.act in selected[patient_b])
115
        self.assertTrue(act9.act in selected[patient_b])
116
        self.assertTrue(act10.act in selected[patient_b])
117
        self.assertTrue(act11.act in selected[patient_b])
118

    
119
        states = patient_b.get_states_history()
120
        patient_b.change_day_selected_of_state(states[2], datetime(2020, 10, 7))
121
        not_locked, days_not_locked, not_valide, not_billable, acts_pause, rejected, selected = \
122
            list_acts_for_billing_CAMSP(datetime(2020, 10, 4), datetime(2020, 10, 8), service_camsp)
123
        acts_not_locked = [x[0] for x in not_locked[patient_b]]
124
        self.assertTrue(act5.act in acts_not_locked)
125
        acts_not_valide = [x[0] for x in not_valide[patient_b]]
126
        self.assertTrue(act6.act in acts_not_valide)
127
        self.assertTrue(act7.act in not_billable[patient_b])
128
        acts_rejected = [x[0] for x in rejected[patient_b]]
129
        self.assertTrue(act8.act in acts_rejected)
130
        self.assertTrue(act9.act in acts_rejected)
131
        self.assertTrue(act10.act in acts_rejected)
132
        self.assertTrue(act11.act in acts_rejected)
133

    
134
        states = patient_b.get_states_history()
135
        patient_b.change_day_selected_of_state(states[2], datetime(2020, 10, 9))
136
        patient_b.change_day_selected_of_state(states[1], datetime(2020, 10, 8))
137
        not_locked, days_not_locked, not_valide, not_billable, acts_pause, rejected, selected = \
138
            list_acts_for_billing_CAMSP(datetime(2020, 10, 4), datetime(2020, 10, 8), service_camsp)
139
        acts_not_locked = [x[0] for x in not_locked[patient_b]]
140
        self.assertTrue(act5.act in acts_not_locked)
141
        acts_not_valide = [x[0] for x in not_valide[patient_b]]
142
        self.assertTrue(act6.act in acts_not_valide)
143
        self.assertTrue(act7.act in not_billable[patient_b])
144
        acts_rejected = [x[0] for x in rejected[patient_b]]
145
        self.assertTrue(act8.act in acts_rejected)
146
        self.assertTrue(act9.act in acts_rejected)
147
        self.assertTrue(act10.act in acts_rejected)
148
        self.assertTrue(act11.act in selected[patient_b])
149

    
150
    def test_facturation_sessad(self):
151
        service_sessad = Service.objects.get(name='SESSAD DYS')
152

    
153
        patient_a = create_patient('a', 'A', service_sessad, self.creator, date_selected=datetime(2020, 10, 5))
154
        act0 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
155
                self.act_type, service_sessad, start_datetime=datetime(2020, 10, 6, 10, 15),
156
                end_datetime=datetime(2020, 10, 6, 12, 20))
157
        status_traitement = Status.objects.filter(services__name='SESSAD DYS').filter(type='TRAITEMENT')[0]
158
        patient_a.set_state(status_traitement, self.creator, date_selected=datetime(2020, 10, 7))
159
        act1 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
160
                self.act_type, service_sessad, start_datetime=datetime(2020, 10, 7, 10, 15),
161
                end_datetime=datetime(2020, 10, 7, 12, 20))
162
        act2 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
163
                self.act_type, service_sessad, start_datetime=datetime(2020, 10, 7, 14, 15),
164
                end_datetime=datetime(2020, 10, 7, 16, 20))
165
        act3 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
166
                self.act_type, service_sessad, start_datetime=datetime(2020, 10, 7, 16, 20),
167
                end_datetime=datetime(2020, 10, 7, 17, 20))
168
        status_clos = Status.objects.filter(services__name='SESSAD DYS').filter(type='CLOS')[0]
169
        patient_a.set_state(status_clos, self.creator, date_selected=datetime(2020, 10, 8))
170
        act4 = EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
171
                self.act_type, service_sessad, start_datetime=datetime(2020, 10, 8, 10, 15),
172
                end_datetime=datetime(2020, 10, 8, 12, 20))
173

    
174
        automated_validation(datetime(2020, 10, 6), service_sessad, self.creator)
175
        automated_validation(datetime(2020, 10, 7), service_sessad, self.creator)
176
        automated_validation(datetime(2020, 10, 8), service_sessad, self.creator)
177

    
178
        not_locked, days_not_locked, not_valide, not_billable, acts_pause, rejected, missing_valid_notification, selected = \
179
            list_acts_for_billing_SESSAD(datetime(2020, 10, 4), datetime(2020, 10, 8), service_sessad)
180
        self.assertEqual(not_locked, {})
181
        self.assertEqual(not_valide, {})
182
        self.assertEqual(not_billable, {})
183
        self.assertEqual(len(rejected[patient_a]), 2)
184
        self.assertEqual(len(missing_valid_notification[patient_a]), 3)
185
        self.assertEqual(selected, {})
186

    
187
        SessadHealthCareNotification(patient=patient_a, author=self.creator, start_date=datetime(2020,10,7), end_date=datetime(2021,10,6)).save()
188
        not_locked, days_not_locked, not_valide, not_billable, acts_pause, rejected, missing_valid_notification, selected = \
189
            list_acts_for_billing_SESSAD(datetime(2020, 10, 4), datetime(2020, 10, 8), service_sessad)
190
        self.assertEqual(not_locked, {})
191
        self.assertEqual(not_valide, {})
192
        self.assertEqual(not_billable, {})
193
        self.assertEqual(len(rejected[patient_a]), 2)
194
        self.assertEqual(missing_valid_notification, {})
195
        self.assertEqual(len(selected[patient_a]), 3)
196

    
197
    def test_facturation_cmpp(self):
198
        service_cmpp = Service.objects.get(name='CMPP')
199

    
200
        patient_a = create_patient('a', 'A', service_cmpp, self.creator, date_selected=datetime(2020, 10, 1))
201
        acts = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
202
                self.act_type, service_cmpp, start_datetime=datetime(2020, 10, i, 10, 15),
203
                end_datetime=datetime(2020, 10, i, 12, 20)) for i in range (1, 32)]
204
        status_accueil = Status.objects.filter(services__name='CMPP').filter(type='ACCUEIL')[0]
205
        status_diagnostic = Status.objects.filter(services__name='CMPP').filter(type='DIAGNOSTIC')[0]
206
        status_traitement = Status.objects.filter(services__name='CMPP').filter(type='TRAITEMENT')[0]
207

    
208
        self.assertEqual(patient_a.get_state().status, status_accueil)
209
        automated_validation(datetime(2020, 10, 1), service_cmpp, self.creator)
210
        patient_a = PatientRecord.objects.get(id=patient_a.id)
211
        self.assertEqual(patient_a.get_state().status, status_diagnostic)
212
        automated_validation(datetime(2020, 10, 2), service_cmpp, self.creator)
213
        patient_a = PatientRecord.objects.get(id=patient_a.id)
214
        self.assertEqual(patient_a.get_state().status, status_diagnostic)
215
        automated_validation(datetime(2020, 10, 3), service_cmpp, self.creator)
216
        patient_a = PatientRecord.objects.get(id=patient_a.id)
217
        self.assertEqual(patient_a.get_state().status, status_diagnostic)
218
        automated_validation(datetime(2020, 10, 4), service_cmpp, self.creator)
219
        patient_a = PatientRecord.objects.get(id=patient_a.id)
220
        self.assertEqual(patient_a.get_state().status, status_diagnostic)
221
        automated_validation(datetime(2020, 10, 5), service_cmpp, self.creator)
222
        patient_a = PatientRecord.objects.get(id=patient_a.id)
223
        self.assertEqual(patient_a.get_state().status, status_diagnostic)
224
        automated_validation(datetime(2020, 10, 6), service_cmpp, self.creator)
225
        patient_a = PatientRecord.objects.get(id=patient_a.id)
226
        self.assertEqual(patient_a.get_state().status, status_diagnostic)
227
        automated_validation(datetime(2020, 10, 7), service_cmpp, self.creator)
228
        patient_a = PatientRecord.objects.get(id=patient_a.id)
229
        self.assertEqual(patient_a.get_state().status, status_traitement)
230
        automated_validation(datetime(2020, 10, 8), service_cmpp, self.creator)
231
        patient_a = PatientRecord.objects.get(id=patient_a.id)
232
        self.assertEqual(patient_a.get_state().status, status_traitement)
233
        automated_validation(datetime(2020, 10, 9), service_cmpp, self.creator)
234
        patient_a = PatientRecord.objects.get(id=patient_a.id)
235
        self.assertEqual(patient_a.get_state().status, status_traitement)
236
        for i in range(10, 32):
237
            automated_validation(datetime(2020, 10, i), service_cmpp, self.creator)
238
            patient_a = PatientRecord.objects.get(id=patient_a.id)
239
            self.assertEqual(patient_a.get_state().status, status_traitement)
240

    
241
        acts_2 = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient_a, [self.therapist3],
242
                self.act_type, service_cmpp, start_datetime=datetime(2020, 11, i, 10, 15),
243
                end_datetime=datetime(2020, 11, i, 12, 20)) for i in range (1, 31)]
244
        for i in range(1, 31):
245
            automated_validation(datetime(2020, 11, i), service_cmpp, self.creator)
246
            patient_a = PatientRecord.objects.get(id=patient_a.id)
247
            self.assertEqual(patient_a.get_state().status, status_traitement)
248

    
249
        self.assertEqual(get_days_with_acts_not_locked(datetime(2020, 10, 1), datetime(2020, 11, 30)), [])
250

    
251
        acts = acts + acts_2
252

    
253
        hct = CmppHealthCareTreatment(patient=patient_a, start_date=datetime(2020, 10, 7), author=self.creator)
254
        hct.save()
255
        hct.add_prolongation()
256

    
257
        billing_cmpp = list_acts_for_billing_CMPP(datetime(2020, 11, 30), service_cmpp)
258

    
259
        self.assertEqual(len(billing_cmpp[5][patient_a]), 6)
260
        self.assertEqual(len(billing_cmpp[6][patient_a]), 40)
261
        self.assertEqual(len(billing_cmpp[7][patient_a]), 15)
262

    
263

    
264
    def test_prices(self):
265
        price_o = add_price(120, date(2012, 10, 1))
266
        self.assertEqual(PricePerAct.get_price(), 120)
267
        price_o = add_price(130, date.today())
268
        self.assertEqual(PricePerAct.get_price(date.today() + relativedelta(days=-1)), 120)
269
        self.assertEqual(PricePerAct.get_price(date.today()), 130)
270
        self.assertEqual(PricePerAct.get_price(date.today() + relativedelta(days=1)), 130)
271
#        price_o = add_price(140, date(day=27, month=11, year=2012))
272
        price_o = add_price(140, date.today() + relativedelta(days=1))
273
        self.assertEqual(PricePerAct.get_price(date.today() + relativedelta(days=-1)), 120)
274
        self.assertEqual(PricePerAct.get_price(date.today()), 130)
275
        self.assertEqual(PricePerAct.get_price(date.today() + relativedelta(days=1)), 140)
276
        self.assertEqual(PricePerAct.get_price(date.today() + relativedelta(days=2)), 140)
277

    
278
    def test_invoicing(self):
279
        service_cmpp = Service.objects.get(name='CMPP')
280
        price_o = add_price(120, date(2012, 10, 1))
281

    
282

    
283
        patients = []
284
        for j in range(2):
285
            patients.append(create_patient(str(j), str(j), service_cmpp, self.creator, date_selected=datetime(2012, 10, 1)))
286
            acts = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patients[j], [self.therapist3],
287
                    self.act_type, service_cmpp, start_datetime=datetime(2012, 10, i, 10, 15),
288
                    end_datetime=datetime(2012, 10, i, 12, 20)) for i in range (1, 32)]
289
            acts_2 = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patients[j], [self.therapist3],
290
                    self.act_type, service_cmpp, start_datetime=datetime(2012, 11, i, 10, 15),
291
                    end_datetime=datetime(2012, 11, i, 12, 20)) for i in range (1, 31)]
292
            hct = CmppHealthCareTreatment(patient=patients[j], start_date=datetime(2012, 10, 7), author=self.creator)
293
            hct.save()
294
            hct.add_prolongation()
295

    
296
        for i in range(1, 32):
297
            automated_validation(datetime(2012, 10, i), service_cmpp, self.creator)
298
        for i in range(1, 31):
299
            automated_validation(datetime(2012, 11, i), service_cmpp, self.creator)
300

    
301
        self.assertEqual(get_days_with_acts_not_locked(datetime(2012, 10, 1), datetime(2012, 11, 30)), [])
302
        invoicing = Invoicing.objects.create(service=service_cmpp, seq_id=666, start_date=date.today())
303
        (len_patients, len_invoices, len_invoices_hors_pause,
304
                len_acts_invoiced, len_acts_invoiced_hors_pause,
305
                len_patient_invoiced, len_patient_invoiced_hors_pause,
306
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
307
                days_not_locked, len_patient_acts_paused,
308
                len_acts_paused) = invoicing.get_stats_or_validate()
309

    
310
        self.assertEqual(len_patients, 2)
311
        self.assertEqual(len_invoices, 4)
312
        self.assertEqual(len_invoices_hors_pause, 4)
313
        self.assertEqual(len_acts_invoiced, 92) # tous les actes billed
314
        self.assertEqual(len_acts_invoiced_hors_pause, 92) # tous les actes billed - les acts qui ne seront pas billed à cause de la pause facturation
315
        self.assertEqual(len_patient_invoiced, 2)
316
        self.assertEqual(len_patient_invoiced_hors_pause, 2)
317
        self.assertEqual(len_acts_lost, 30)
318
        self.assertEqual(len_patient_with_lost_acts, 2)
319

    
320
        patients[1].pause = True
321
        patients[1].save()
322

    
323
        (len_patients, len_invoices, len_invoices_hors_pause,
324
                len_acts_invoiced, len_acts_invoiced_hors_pause,
325
                len_patient_invoiced, len_patient_invoiced_hors_pause,
326
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
327
                days_not_locked, len_patient_acts_paused,
328
                len_acts_paused) = invoicing.get_stats_or_validate()
329

    
330
        self.assertEqual(len_patients, 2)
331
        self.assertEqual(len_invoices, 4)
332
        self.assertEqual(len_invoices_hors_pause, 2)
333
        self.assertEqual(len_acts_invoiced, 92) # tous les actes billed
334
        self.assertEqual(len_acts_invoiced_hors_pause, 46) # tous les actes billed - les acts qui ne seront pas billed à cause de la pause facturation
335
        self.assertEqual(len_patient_invoiced, 2)
336
        self.assertEqual(len_patient_invoiced_hors_pause, 1)
337
        self.assertEqual(len_acts_lost, 30)
338
        self.assertEqual(len_patient_with_lost_acts, 2)
339

    
340
        invoicing_next = invoicing.close(date(day=30, month=11, year=2012))
341

    
342
        (len_patients, len_invoices, len_invoices_hors_pause,
343
                len_acts_invoiced, len_acts_invoiced_hors_pause,
344
                len_patient_invoiced, len_patient_invoiced_hors_pause,
345
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
346
                days_not_locked, len_patient_acts_paused,
347
                len_acts_paused) = invoicing.get_stats_or_validate(commit=True)
348

    
349
        (len_patients, len_invoices, len_invoices_hors_pause,
350
                len_acts_invoiced, len_acts_invoiced_hors_pause,
351
                len_patient_invoiced, len_patient_invoiced_hors_pause,
352
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
353
                days_not_locked, len_patient_acts_paused,
354
                len_acts_paused) = invoicing.get_stats_or_validate()
355

    
356
        self.assertEqual(len_patients, 1)
357
        self.assertEqual(len_invoices, 2)
358
        self.assertEqual(len_acts_invoiced, 46)
359

    
360
        patients[1].pause = False
361
        patients[1].save()
362
        invoicing_next.close(date(day=2, month=12, year=2012))
363

    
364
        (len_patients, len_invoices, len_invoices_hors_pause,
365
                len_acts_invoiced, len_acts_invoiced_hors_pause,
366
                len_patient_invoiced, len_patient_invoiced_hors_pause,
367
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
368
                days_not_locked, len_patient_acts_paused,
369
                len_acts_paused) = invoicing_next.get_stats_or_validate()
370

    
371
        self.assertEqual(len_patients, 2)
372
        self.assertEqual(len_invoices, 2)
373
        self.assertEqual(len_acts_invoiced, 46)
374

    
375
    def test_invoicing2(self):
376
        service_cmpp = Service.objects.get(name='CMPP')
377
        price_o = add_price(120, date(2012, 10, 1))
378

    
379

    
380
        patient = create_patient('A', 'a', service_cmpp, self.creator, date_selected=datetime(2012, 10, 1))
381
        acts = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient, [self.therapist3],
382
                self.act_type, service_cmpp, start_datetime=datetime(2012, 10, i, 10, 15),
383
                end_datetime=datetime(2012, 10, i, 12, 20)) for i in range (1, 32)]
384
        hct = CmppHealthCareTreatment(patient=patient, start_date=datetime(2011, 11, 7), author=self.creator)
385
        hct.save()
386
        hct.add_prolongation()
387

    
388
        for i in range(1, 32):
389
            automated_validation(datetime(2012, 10, i), service_cmpp, self.creator)
390

    
391
        self.assertEqual(get_days_with_acts_not_locked(datetime(2012, 10, 1), datetime(2012, 11, 30)), [])
392
        invoicing = Invoicing.objects.create(service=service_cmpp, seq_id=666, start_date=date.today())
393
        invoicing_next = invoicing.close(date(day=1, month=11, year=2012))
394
        (len_patients, len_invoices, len_invoices_hors_pause,
395
                len_acts_invoiced, len_acts_invoiced_hors_pause,
396
                len_patient_invoiced, len_patient_invoiced_hors_pause,
397
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
398
                days_not_locked, len_patient_acts_paused,
399
                len_acts_paused) = invoicing.get_stats_or_validate(commit=True)
400

    
401
        self.assertEqual(len_patients, 1)
402
        self.assertEqual(len_invoices, 2)
403
        self.assertEqual(len_invoices_hors_pause, 2)
404
        self.assertEqual(len_acts_invoiced, 31) # tous les actes billed
405
        self.assertEqual(len_acts_invoiced_hors_pause, 31) # tous les actes billed - les acts qui ne seront pas billed à cause de la pause facturation
406
        self.assertEqual(len_patient_invoiced, 1)
407
        self.assertEqual(len_patient_invoiced_hors_pause, 1)
408
        self.assertEqual(len_acts_lost, 0)
409
        self.assertEqual(len_patient_with_lost_acts, 0)
410

    
411

    
412
        acts_2 = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient, [self.therapist3],
413
                self.act_type, service_cmpp, start_datetime=datetime(2012, 11, i, 10, 15),
414
                end_datetime=datetime(2012, 11, i, 12, 20)) for i in range (1, 31)]
415

    
416
        for i in range(1, 31):
417
            automated_validation(datetime(2012, 11, i), service_cmpp, self.creator)
418

    
419
        hct = CmppHealthCareTreatment(patient=patient, start_date=datetime(2012, 11, 7), author=self.creator)
420
        hct.save()
421

    
422
        # 6 sur hct 1
423
        # 24 sur hct 2
424

    
425
        (len_patients, len_invoices, len_invoices_hors_pause,
426
                len_acts_invoiced, len_acts_invoiced_hors_pause,
427
                len_patient_invoiced, len_patient_invoiced_hors_pause,
428
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
429
                days_not_locked, len_patient_acts_paused,
430
                len_acts_paused) = invoicing_next.get_stats_or_validate(commit=True)
431

    
432
        self.assertEqual(len_patients, 1)
433
        self.assertEqual(len_invoices, 1)
434
        self.assertEqual(len_invoices_hors_pause, 1)
435
        self.assertEqual(len_acts_invoiced, 30) # tous les actes billed
436
        self.assertEqual(len_acts_invoiced_hors_pause, 30) # tous les actes billed - les acts qui ne seront pas billed à cause de la pause facturation
437
        self.assertEqual(len_patient_invoiced, 1)
438
        self.assertEqual(len_patient_invoiced_hors_pause, 1)
439
        self.assertEqual(len_acts_lost, 0)
440
        self.assertEqual(len_patient_with_lost_acts, 0)
441

    
442

    
443
    def test_change_state(self):
444
        service_cmpp = Service.objects.get(name='CMPP')
445
        price_o = add_price(120, date(2012, 10, 1))
446

    
447

    
448
        patient = create_patient('A', 'a', service_cmpp, self.creator, date_selected=datetime(2012, 10, 1))
449

    
450
        self.assertEqual(patient.last_state.status.type, "ACCUEIL")
451

    
452
        EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient, [self.therapist3],
453
                self.act_type, service_cmpp, start_datetime=datetime(2012, 10, 1, 10, 15),
454
                end_datetime=datetime(2012, 10, 1, 12, 20))
455

    
456
        automated_validation(datetime(2012, 10, 1), service_cmpp, self.creator)
457
        patient =  PatientRecord.objects.get(id=patient.id)
458

    
459
        self.assertEqual(patient.last_state.status.type, "DIAGNOSTIC")
460
        self.assertEqual(patient.last_state.date_selected, datetime(2012, 10, 1, 0, 0))
461

    
462
        acts = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient, [self.therapist3],
463
                self.act_type, service_cmpp, start_datetime=datetime(2012, 10, i, 10, 15),
464
                end_datetime=datetime(2012, 10, i, 12, 20)) for i in range (2, 32)]
465

    
466
        for i in range(2, 32):
467
            automated_validation(datetime(2012, 10, i), service_cmpp, self.creator)
468

    
469
        patient =  PatientRecord.objects.get(id=patient.id)
470
        self.assertEqual(patient.last_state.status.type, "TRAITEMENT")
471
        self.assertEqual(patient.last_state.date_selected, datetime(2012, 10, 7, 0, 0))
472

    
473
        patient = create_patient('B', 'b', service_cmpp, self.creator, date_selected=datetime(2012, 10, 1))
474

    
475
        self.assertEqual(patient.last_state.status.type, "ACCUEIL")
476

    
477
        EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient, [self.therapist3],
478
                self.act_type, service_cmpp, start_datetime=datetime(2012, 10, 1, 10, 15),
479
                end_datetime=datetime(2012, 10, 1, 12, 20))
480

    
481
        automated_validation(datetime(2012, 10, 1), service_cmpp, self.creator)
482
        patient =  PatientRecord.objects.get(id=patient.id)
483

    
484
        self.assertEqual(patient.last_state.status.type, "DIAGNOSTIC")
485
        self.assertEqual(patient.last_state.date_selected, datetime(2012, 10, 1, 0, 0))
486

    
487
        status = Status.objects.filter(type="CLOS").\
488
                filter(services__name='CMPP')[0]
489
        patient.set_state(status, self.creator, date_selected=datetime(2012, 12, 9, 0, 0))
490

    
491
        self.assertEqual(patient.last_state.status.type, "CLOS")
492
        self.assertEqual(patient.last_state.date_selected, datetime(2012, 12, 9, 0, 0))
493

    
494
        acts = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient, [self.therapist3],
495
                self.act_type, service_cmpp, start_datetime=datetime(2012, 10, i, 10, 15),
496
                end_datetime=datetime(2012, 10, i, 12, 20)) for i in range (2, 32)]
497

    
498
        for i in range(2, 32):
499
            automated_validation(datetime(2012, 10, i), service_cmpp, self.creator)
500

    
501
        patient =  PatientRecord.objects.get(id=patient.id)
502
        self.assertEqual(patient.last_state.status.type, "CLOS")
503
        self.assertEqual(patient.last_state.date_selected, datetime(2012, 12, 9, 0, 0))
504

    
505
    def test_change_hc_number(self):
506
        service_cmpp = Service.objects.get(name='CMPP')
507
        price_o = add_price(120, date(2012, 10, 1))
508

    
509
        patient = create_patient('A', 'a', service_cmpp, self.creator, date_selected=datetime(2012, 10, 1))
510
        acts = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient, [self.therapist3],
511
                self.act_type, service_cmpp, start_datetime=datetime(2012, 10, i, 10, 15),
512
                end_datetime=datetime(2012, 10, i, 12, 20)) for i in range (1, 4)]
513

    
514
        for i in range(1, 4):
515
            automated_validation(datetime(2012, 10, i), service_cmpp, self.creator)
516

    
517
        invoicing = Invoicing.objects.create(service=service_cmpp, seq_id=666, start_date=date.today())
518
        invoicing_next = invoicing.close(date(day=4, month=10, year=2012))
519
        (len_patients, len_invoices, len_invoices_hors_pause,
520
                len_acts_invoiced, len_acts_invoiced_hors_pause,
521
                len_patient_invoiced, len_patient_invoiced_hors_pause,
522
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
523
                days_not_locked, len_patient_acts_paused,
524
                len_acts_paused) = invoicing.get_stats_or_validate(commit=True)
525

    
526
        self.assertEqual(len_patients, 1)
527
        self.assertEqual(len_invoices, 1)
528
        self.assertEqual(len_invoices_hors_pause, 1)
529
        self.assertEqual(len_acts_invoiced, 3) # tous les actes billed
530
        self.assertEqual(len_acts_invoiced_hors_pause, 3) # tous les actes billed - les acts qui ne seront pas billed à cause de la pause facturation
531
        self.assertEqual(len_patient_invoiced, 1)
532
        self.assertEqual(len_patient_invoiced_hors_pause, 1)
533
        self.assertEqual(len_acts_lost, 0)
534
        self.assertEqual(len_patient_with_lost_acts, 0)
535

    
536
        hcd = CmppHealthCareDiagnostic.objects.\
537
            filter(patient=patient).latest('start_date')
538

    
539
        self.assertEqual(hcd.get_nb_acts_cared(), 3)
540

    
541
        try:
542
            hcd.set_act_number(2)
543
            raise
544
        except:
545
            pass
546

    
547
        hcd.set_act_number(5)
548

    
549
        acts = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient, [self.therapist3],
550
                self.act_type, service_cmpp, start_datetime=datetime(2012, 10, i, 10, 15),
551
                end_datetime=datetime(2012, 10, i, 12, 20)) for i in range (4, 32)]
552

    
553
        for i in range(4, 32):
554
            automated_validation(datetime(2012, 10, i), service_cmpp, self.creator)
555

    
556
        invoicing = Invoicing.objects.create(service=service_cmpp, start_date=date.today())
557
        invoicing_next = invoicing.close(date(day=1, month=11, year=2012))
558
        (len_patients, len_invoices, len_invoices_hors_pause,
559
                len_acts_invoiced, len_acts_invoiced_hors_pause,
560
                len_patient_invoiced, len_patient_invoiced_hors_pause,
561
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
562
                days_not_locked, len_patient_acts_paused,
563
                len_acts_paused) = invoicing.get_stats_or_validate(commit=True)
564

    
565
        self.assertEqual(len_patients, 1)
566
        self.assertEqual(len_invoices, 1)
567
        self.assertEqual(len_invoices_hors_pause, 1)
568
        self.assertEqual(len_acts_invoiced, 2) # tous les actes billed
569
        self.assertEqual(len_acts_invoiced_hors_pause, 2) # tous les actes billed - les acts qui ne seront pas billed à cause de la pause facturation
570
        self.assertEqual(len_patient_invoiced, 1)
571
        self.assertEqual(len_patient_invoiced_hors_pause, 1)
572
        self.assertEqual(len_acts_lost, 26)
573
        self.assertEqual(len_patient_with_lost_acts, 1)
574

    
575
        hct = CmppHealthCareTreatment(patient=patient, start_date=datetime(2012, 10, 1), author=self.creator)
576
        hct.save()
577

    
578
        invoicing = Invoicing.objects.create(service=service_cmpp, start_date=date.today())
579
        invoicing_next = invoicing.close(date(day=2, month=11, year=2012))
580
        (len_patients, len_invoices, len_invoices_hors_pause,
581
                len_acts_invoiced, len_acts_invoiced_hors_pause,
582
                len_patient_invoiced, len_patient_invoiced_hors_pause,
583
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
584
                days_not_locked, len_patient_acts_paused,
585
                len_acts_paused) = invoicing.get_stats_or_validate(commit=True)
586

    
587
        self.assertEqual(len_patients, 1)
588
        self.assertEqual(len_invoices, 1)
589
        self.assertEqual(len_invoices_hors_pause, 1)
590
        self.assertEqual(len_acts_invoiced, 26) # tous les actes billed
591
        self.assertEqual(len_acts_invoiced_hors_pause, 26) # tous les actes billed - les acts qui ne seront pas billed à cause de la pause facturation
592
        self.assertEqual(len_patient_invoiced, 1)
593
        self.assertEqual(len_patient_invoiced_hors_pause, 1)
594
        self.assertEqual(len_acts_lost, 0)
595
        self.assertEqual(len_patient_with_lost_acts, 0)
596

    
597
        try:
598
            hct.set_act_number(24)
599
            raise
600
        except:
601
            pass
602

    
603
        hct.set_act_number(28)
604

    
605
        acts = [ EventWithAct.objects.create_patient_appointment(self.creator, 'RDV', patient, [self.therapist3],
606
                self.act_type, service_cmpp, start_datetime=datetime(2012, 11, i, 10, 15),
607
                end_datetime=datetime(2012, 11, i, 12, 20)) for i in range (1, 4)]
608

    
609
        for i in range(1, 4):
610
            automated_validation(datetime(2012, 11, i), service_cmpp, self.creator)
611

    
612
        invoicing = Invoicing.objects.create(service=service_cmpp, start_date=date.today())
613
        invoicing_next = invoicing.close(date(day=3, month=11, year=2012))
614
        (len_patients, len_invoices, len_invoices_hors_pause,
615
                len_acts_invoiced, len_acts_invoiced_hors_pause,
616
                len_patient_invoiced, len_patient_invoiced_hors_pause,
617
                len_acts_lost, len_patient_with_lost_acts, patients_stats,
618
                days_not_locked, len_patient_acts_paused,
619
                len_acts_paused) = invoicing.get_stats_or_validate(commit=True)
620

    
621
        self.assertEqual(len_patients, 1)
622
        self.assertEqual(len_invoices, 1)
623
        self.assertEqual(len_invoices_hors_pause, 1)
624
        self.assertEqual(len_acts_invoiced, 2) # tous les actes billed
625
        self.assertEqual(len_acts_invoiced_hors_pause, 2) # tous les actes billed - les acts qui ne seront pas billed à cause de la pause facturation
626
        self.assertEqual(len_patient_invoiced, 1)
627
        self.assertEqual(len_patient_invoiced_hors_pause, 1)
628
        self.assertEqual(len_acts_lost, 1)
629
        self.assertEqual(len_patient_with_lost_acts, 1)
(13-13/16)