Project

General

Profile

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

calebasse / calebasse / facturation / list_acts.py @ 8ef57f1f

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

    
97

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

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

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

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

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

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

    
146

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

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

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

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

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

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

    
206

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

    
210
    For the CMPP, acts are billable if
211

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

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

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

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