-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
utils.py
95 lines (81 loc) · 2.6 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from decimal import Decimal
from casexml.apps.stock.models import StockTransaction
def months_of_stock_remaining(stock, daily_consumption):
if daily_consumption:
return stock / Decimal((daily_consumption * 30))
else:
return None
def stock_category(stock, daily_consumption, understock, overstock):
if stock is None:
return 'nodata'
elif stock <= 0:
return 'stockout'
elif daily_consumption is None:
return 'nodata'
elif daily_consumption == 0:
return 'overstock'
months_left = months_of_stock_remaining(stock, daily_consumption)
if months_left is None:
return 'nodata'
elif months_left < understock:
return 'understock'
elif months_left > overstock:
return 'overstock'
else:
return 'adequate'
def state_stock_category(state):
if not state.sql_location:
return 'nodata'
location_type = state.sql_location.location_type
return stock_category(
state.stock_on_hand,
state.get_daily_consumption(),
location_type.understock_threshold,
location_type.overstock_threshold,
)
def get_current_ledger_transactions(case_id):
"""
Given a case returns a dict of all current ledger data.
{
"section_id": {
"product_id": StockTransaction,
"product_id": StockTransaction,
...
},
...
}
"""
from corehq.apps.commtrack.models import StockState
results = StockState.objects.filter(case_id=case_id).values_list('case_id', 'section_id', 'product_id')
ret = {}
for case_id, section_id, product_id in results:
sections = ret.setdefault(section_id, {})
sections[product_id] = StockTransaction.latest(case_id, section_id, product_id)
return ret
def get_current_ledger_state(case_ids):
"""
Given a list of cases returns a dict of all current ledger data of the following format:
{
"case_id": {
"section_id": {
"product_id": StockState,
"product_id": StockState,
...
},
...
},
...
}
Where you get one stock transaction per product/section which is the last one seen.
"""
from corehq.apps.commtrack.models import StockState
if not case_ids:
return {}
states = StockState.objects.filter(
case_id__in=case_ids
)
ret = {case_id: {} for case_id in case_ids}
for state in states:
sections = ret[state.case_id].setdefault(state.section_id, {})
sections[state.product_id] = state
return ret