Project

General

Profile

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

calebasse / calebasse / facturation / list_acts.py @ f9afb714

1
# -*- coding: utf-8 -*-
2
from datetime import datetime, timedelta
3

    
4
from calebasse.actes.validation import are_all_acts_of_the_day_locked
5

    
6

    
7
def list_acts_for_billing_first_round(end_day, service, start_day=None):
8
    """Used to sort acts and extract acts billable before specific service
9
        requirements.
10

    
11
    At first, all acts not billed are listed per patient.
12
    Then acts are sorted.
13
    Each sorting split the acts in two sets, one set for rejected acts,
14
        on set for selected sets.
15
    First sort rejects acts days where all acts are not locked:
16
        acts_not_locked
17
    Second sort rejects acts not in state 'VALIDE':
18
        acts_not_valide
19
    Third sort rejects acts not billable:
20
        acts_not_billable
21
    Acts billable in :
22
        acts_billable
23

    
24
    acts = acts_not_locked + \
25
        acts_not_valide + \
26
            acts_not_billable + \
27
                acts_billable
28

    
29
    :param end_day: formatted date that gives the last day when acts are taken
30
        in account.
31
    :type end_day: datetime
32
    :param service: service in which acts are dealt with.
33
    :type service: calebasse.ressources.Service
34

    
35
    :returns: a list of dictionnaries where patients are the keys and values
36
        are lists of acts. The second element of this list in not a dict but
37
        a list of the days where are all days are not locked.
38
    :rtype: list
39
    """
40

    
41
    from calebasse.actes.models import EventAct
42
    acts = EventAct.objects.filter(is_billed=False,
43
        patient__service=service).order_by('-date')
44
    # Filter acts according to the date
45
    i = 0
46
    for act in acts:
47
        # On enlève tous les acts sup au jour de l'end_date
48
        if datetime(act.date.year, act.date.month, act.date.day) <= \
49
                datetime(end_day.year, end_day.month, end_day.day):
50
            acts = acts[i:]
51
            break
52
        i = i + 1
53
    if start_day:
54
        i = 0
55
        for act in acts:
56
            # On enlève tous les acts inf au jour de la start_date
57
            if datetime(act.date.year, act.date.month, act.date.day) < \
58
                    datetime(start_day.year, start_day.month, start_day.day):
59
                break
60
            i = i + 1
61
        acts = acts[:i]
62
    acts_not_locked = {}
63
    days_not_locked = []
64
    acts_not_valide = {}
65
    acts_not_billable = {}
66
    acts_billable = {}
67
    locked = False
68
    for act in acts:
69
        current_day = datetime(act.date.year, act.date.month, act.date.day)
70
        locked = are_all_acts_of_the_day_locked(current_day)
71
        if not locked:
72
            if not current_day in days_not_locked:
73
                days_not_locked.append(current_day)
74
            if act.patient in acts_not_locked:
75
                acts_not_locked[act.patient].append((act, act.date))
76
            else:
77
                acts_not_locked[act.patient] = [(act, act.date)]
78
        elif not act.is_state('VALIDE'):
79
            if act.patient in acts_not_valide:
80
                acts_not_valide[act.patient].append((act, act.get_state()))
81
            else:
82
                acts_not_valide[act.patient] = [(act, act.get_state())]
83
        elif not act.is_billable():
84
            if act.patient in acts_not_billable:
85
                acts_not_billable[act.patient].append(act)
86
            else:
87
                acts_not_billable[act.patient] = [act]
88
        else:
89
            if act.patient in acts_billable:
90
                acts_billable[act.patient].append(act)
91
            else:
92
                acts_billable[act.patient] = [act]
93
    return (acts_not_locked, days_not_locked, acts_not_valide,
94
        acts_not_billable, acts_billable)
95

    
96

    
97
def list_acts_for_billing_CAMSP(start_day, end_day, service):
98
    """Used to sort acts billable by specific service requirements.
99

    
100
    For the CAMSP, only the state of the patient record 'CAMSP_STATE_SUIVI'
101
        at the date of the act determine if the acte is billable.
102

    
103
    acts = acts_not_locked + \
104
        acts_not_valide + \
105
            acts_not_billable + \
106
                acts_bad_state + \
107
                    acts_accepted
108

    
109
    :param end_day: formatted date that gives the last day when acts are taken
110
        in account.
111
    :type end_day: datetime
112
    :param service: service in which acts are dealt with.
113
    :type service: calebasse.ressources.Service
114

    
115
    :returns: a list of dictionnaries where patients are the keys and values
116
        are lists of acts. The second element of this list in not a dict but
117
        a list of the days where are all days are not locked.
118
    :rtype: list
119
    """
120

    
121
    acts_not_locked, days_not_locked, acts_not_valide, \
122
        acts_not_billable, acts_billable = \
123
            list_acts_for_billing_first_round(end_day, service,
124
                start_day=start_day)
125
    acts_bad_state = {}
126
    acts_accepted = {}
127
    for patient, acts in acts_billable.items():
128
        for act in acts:
129
            if patient.was_in_state_at_day(act.date, 'CAMSP_STATE_SUIVI'):
130
                if act.patient in acts_accepted:
131
                    acts_accepted[act.patient].append(act)
132
                else:
133
                    acts_accepted[act.patient] = [act]
134
            else:
135
                if act.patient in acts_bad_state:
136
                    acts_bad_state[act.patient]. \
137
                        append((act, 'NOT_ACCOUNTABLE_STATE'))
138
                else:
139
                    acts_bad_state[act.patient] = \
140
                        [(act, 'NOT_ACCOUNTABLE_STATE')]
141
    return (acts_not_locked, days_not_locked, acts_not_valide,
142
        acts_not_billable, acts_bad_state,
143
        acts_accepted)
144

    
145

    
146
def list_acts_for_billing_SESSAD(start_day, end_day, service):
147
    """Used to sort acts billable by specific service requirements.
148

    
149
    For the SESSAD, acts are billable if the state of the patient record at
150
        the date of the act is 'SESSAD_STATE_TRAITEMENT' and there was also a
151
        valid notification at that date.
152

    
153
    acts = acts_not_locked + \
154
        acts_not_valide + \
155
            acts_not_billable + \
156
                acts_bad_state + \
157
                    acts_missing_valid_notification + \
158
                        acts_accepted
159

    
160
    :param end_day: formatted date that gives the last day when acts are taken
161
        in account.
162
    :type end_day: datetime
163
    :param service: service in which acts are dealt with.
164
    :type service: calebasse.ressources.Service
165

    
166
    :returns: a list of dictionnaries where patients are the keys and values
167
        are lists of acts. The second element of this list in not a dict but
168
        a list of the days where are all days are not locked.
169
    :rtype: list
170
    """
171

    
172
    acts_not_locked, days_not_locked, acts_not_valide, \
173
        acts_not_billable, acts_billable = \
174
            list_acts_for_billing_first_round(end_day, service,
175
                start_day=start_day)
176
    acts_bad_state = {}
177
    acts_missing_valid_notification = {}
178
    acts_accepted = {}
179
    for patient, acts in acts_billable.items():
180
        for act in acts:
181
            if patient.was_in_state_at_day(act.date,
182
                    'SESSAD_STATE_TRAITEMENT'):
183
                if not act.was_covered_by_notification():
184
                    if act.patient in acts_missing_valid_notification:
185
                        acts_missing_valid_notification[act.patient]. \
186
                            append(act)
187
                    else:
188
                        acts_missing_valid_notification[act.patient] = [act]
189
                else:
190
                    if act.patient in acts_accepted:
191
                        acts_accepted[act.patient].append(act)
192
                    else:
193
                        acts_accepted[act.patient] = [act]
194
            else:
195
                if act.patient in acts_bad_state:
196
                    acts_bad_state[act.patient]. \
197
                        append((act, 'NOT_ACCOUNTABLE_STATE'))
198
                else:
199
                    acts_bad_state[act.patient] = \
200
                        [(act, 'NOT_ACCOUNTABLE_STATE')]
201
    return (acts_not_locked, days_not_locked, acts_not_valide,
202
        acts_not_billable, acts_bad_state, acts_missing_valid_notification,
203
        acts_accepted)
204

    
205

    
206
def list_acts_for_billing_CMPP(end_day, service):
207
    """Used to sort acts billable by specific service requirements.
208

    
209
    For the CMPP, acts are billable if
210

    
211
    acts = acts_not_locked + \
212
        acts_not_valide + \
213
            acts_not_billable + \
214
                acts_diagnostic + \
215
                    acts_treatment + \
216
                        acts_losts
217

    
218
    :param end_day: formatted date that gives the last day when acts are taken
219
        in account.
220
    :type end_day: datetime
221
    :param service: service in which acts are dealt with.
222
    :type service: calebasse.ressources.Service
223

    
224
    :returns: a list of dictionnaries where patients are the keys and values
225
        are lists of acts. The second element of this list in not a dict but
226
        a list of the days where are all days are not locked.
227
    :rtype: list
228
    """
229

    
230
    acts_not_locked, days_not_locked, acts_not_valide, \
231
        acts_not_billable, acts_billable = \
232
            list_acts_for_billing_first_round(end_day, service)
233
    acts_diagnostic = {}
234
    acts_treatment = {}
235
    acts_losts = {}
236
    for patient, acts in acts_billable.items():
237
        for act in acts:
238
            cared, hc = act.is_act_covered_by_diagnostic_healthcare()
239
            if cared:
240
                if act.patient in acts_diagnostic:
241
                    acts_diagnostic[act.patient]. \
242
                        append((act, hc))
243
                else:
244
                    acts_diagnostic[act.patient] = [(act, hc)]
245
            else:
246
                cared, hc = act.is_act_covered_by_treatment_healthcare()
247
                if cared:
248
                    if act.patient in acts_treatment:
249
                        acts_treatment[act.patient]. \
250
                            append((act, hc))
251
                    else:
252
                        acts_treatment[act.patient] = [(act, hc)]
253
                else:
254
                    if act.patient in acts_losts:
255
                        acts_losts[act.patient]. \
256
                            append(act)
257
                    else:
258
                        acts_losts[act.patient] = [act]
259
    return (acts_not_locked, days_not_locked, acts_not_valide,
260
        acts_not_billable, acts_diagnostic, acts_treatment,
261
        acts_losts)
(3-3/7)