Project

General

Profile

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

calebasse / calebasse / facturation / tests.py @ 76974b6f

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_2)
24

    
25
from models import add_price, 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.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 in selected[patient_a])
102
        self.assertTrue(act2 in selected[patient_a])
103
        self.assertTrue(act3 in selected[patient_a])
104
        acts_rejected = [x[0] for x in rejected[patient_a]]
105
        self.assertTrue(act0 in acts_rejected)
106
        self.assertTrue(act4 in acts_rejected)
107

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

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

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

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

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

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

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

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

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

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

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

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

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

    
250
        acts = acts + acts_2
251

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

    
256
        billing_cmpp = list_acts_for_billing_CMPP_2(datetime(2020, 11, 30), service_cmpp)
257

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

    
262

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

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

    
281

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
378

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

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

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

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

    
410

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

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

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

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

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

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

    
441

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

    
446

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
504
    def test_change_hc_number(self):
505

    
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)
(6-6/8)