1+ from datetime import datetime
2+ import sentry_sdk
13from logistics .models import Product
24from logistics .reports import calc_percentage , format_percentage
35from logistics_project .apps .malawi .warehouse import warehouse_view
46from logistics_project .apps .malawi .warehouse .models import CalculatedConsumption
7+ from logistics_project .utils .dates import months_between
58
69CONDITION_DIARRHEA = "Diarrhea"
710CONDITION_UNCOMPLICATED_MALARIA_YOUNG = "Uncomplicated Malaria (5 - 35 months)"
@@ -77,14 +80,18 @@ def _get_product_display_name(product):
7780 return "MRDTs"
7881 else :
7982 return product .name
80-
8183 product = _get_product_for_condition (condition )
82- consumption = CalculatedConsumption .objects .get (
83- supply_point = supply_point ,
84- product = product ,
85- date = month ,
86- )
87- cases , consumption_display = _get_cases_for_consumption_amount (condition , consumption .calculated_consumption )
84+ try :
85+ consumption = CalculatedConsumption .objects .get (
86+ supply_point = supply_point ,
87+ product = product ,
88+ date = month ,
89+ )
90+ except CalculatedConsumption .DoesNotExist as e :
91+ sentry_sdk .capture_exception (e )
92+ cases , consumption_display = 0 , 0
93+ else :
94+ cases , consumption_display = _get_cases_for_consumption_amount (condition , consumption .calculated_consumption )
8895 return [
8996 condition ,
9097 _get_product_display_name (product ),
@@ -124,14 +131,13 @@ class View(warehouse_view.MalawiWarehouseView):
124131 def custom_context (self , request ):
125132 # get consumption data
126133 reporting_sp = self .get_reporting_supply_point (request )
127- month = request .datespan .enddate
128134 main_table_headers = [
129135 'Condition' ,
130136 'Product' ,
131137 'Products Dispensed' ,
132138 '# Cases' ,
133139 ]
134- main_table_rows = self ._get_main_table_rows (reporting_sp , month )
140+ main_table_rows = self ._get_main_table_rows (reporting_sp , request . datespan )
135141 malaria_total_row = _get_total_malaria_row (main_table_rows )
136142 pneumonia_total_row = _get_total_pneumonia_row (main_table_rows )
137143 main_table_rows .insert (3 , malaria_total_row )
@@ -156,7 +162,6 @@ def custom_context(self, request):
156162 total_uncomplicated = uncomplicated_malaria_young_cases + uncomplicated_malaria_old_cases
157163 total_malaria = total_uncomplicated + severe_malaria_cases
158164 return {
159- "show_single_date" : True ,
160165 "main_table" : main_table ,
161166 'uncomplicated_malaria_breakdown_table' : {
162167 "is_datatable" : False ,
@@ -179,8 +184,20 @@ def custom_context(self, request):
179184 }
180185 }
181186
182- def _get_main_table_rows (self , supply_point , month ):
183- return [
184- _build_condition_row (condition , supply_point , month )
185- for condition in CONDITIONS
186- ]
187+ def _get_main_table_rows (self , supply_point , datespan ):
188+ rows = []
189+ for condition in CONDITIONS :
190+ row = None
191+ for year , month in months_between (datespan .startdate , datespan .enddate ):
192+ month_datetime = datetime (year , month , 1 )
193+ current_row = _build_condition_row (condition , supply_point , month_datetime )
194+ print (current_row )
195+ if row is None :
196+ row = current_row
197+ else :
198+ row [2 ] += current_row [2 ]
199+ row [3 ] += current_row [3 ]
200+
201+ rows .append (row )
202+
203+ return rows
0 commit comments