def rows(self): rows = [] locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id']) dg = [] for date in list(rrule.rrule(rrule.MONTHLY, dtstart=self.config['startdate'], until=self.config['enddate'])): dg.extend(DeliveryGroups().submitting(locations, date.month)) for child in dg: total_responses = 0 total_possible = 0 submitted, rr_value = randr_value(child.location_id, self.config['startdate'], self.config['enddate']) if child.is_archived and not rr_value: continue group_summaries = GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=child.location_id, title=SupplyPointStatusTypes.R_AND_R_FACILITY ) for group_summary in group_summaries: if group_summary: total_responses += group_summary.responded total_possible += group_summary.total hist_resp_rate = rr_format_percent(total_responses, total_possible) url = make_url(FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (self.config['location_id'], self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) contact = CommCareUser.get_db().view( 'locations/users_by_location_id', startkey=[child.location_id], endkey=[child.location_id, {}], include_docs=True ).first() if contact and contact['doc']: contact = CommCareUser.wrap(contact['doc']) role = contact.user_data.get('role') or "" args = (contact.first_name, contact.last_name, role, contact.default_phone_number) contact_string = "%s %s (%s) %s" % args else: contact_string = "" rows.append( [ child.site_code, link_format(child.name, url), get_span(submitted) % (rr_value.strftime("%d %b %Y") if rr_value else "Not reported"), contact_string, hist_resp_rate ] ) return rows
def rows(self): locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id']) rows = [] for loc in locations: try: org_summary = OrganizationSummary.objects.filter( location_id=loc.location_id, date__range=(self.config['startdate'], self.config['enddate']) ).aggregate(average_lead_time_in_days=Avg('average_lead_time_in_days')) except OrganizationSummary.DoesNotExist: continue avg_lead_time = org_summary['average_lead_time_in_days'] if avg_lead_time: avg_lead_time = "%.1f" % avg_lead_time else: avg_lead_time = "None" url = make_url(DeliveryReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (loc.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) rows.append([link_format(loc.name, url), avg_lead_time]) return rows
def rows(self): rows = [] if self.config['location_id']: locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id']) for loc in locations: total_responses = 0 total_possible = 0 for g in GroupSummary.objects.filter(org_summary__date__lte=datetime(int(self.config['year']), int(self.config['month']), 1), org_summary__supply_point=loc.location_id, title=SupplyPointStatusTypes.SUPERVISION_FACILITY): if g: total_responses += g.responded total_possible += g.total if total_possible: response_rate = "%.1f%%" % (100.0 * total_responses / total_possible) else: response_rate = "<span class='no_data'>None</span>" url = make_url(FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s%s', (loc.location_id, self.config['program'], self.config['prd_part_url'])) latest = latest_status_or_none(loc.location_id, SupplyPointStatusTypes.SUPERVISION_FACILITY, int(self.config['month']), int(self.config['year'])) rows.append([ loc.site_code, link_format(loc.name, url), latest.name if latest else None, latest.status_date if latest else None, response_rate ]) return rows
def rows(self): locations = SQLLocation.objects.filter(parent__location_id=self.config["location_id"]) rows = [] for loc in locations: try: org_summary = OrganizationSummary.objects.filter( location_id=loc.location_id, date__range=(self.config["startdate"], self.config["enddate"]) ).aggregate(average_lead_time_in_days=Avg("average_lead_time_in_days")) except OrganizationSummary.DoesNotExist: continue avg_lead_time = org_summary["average_lead_time_in_days"] if avg_lead_time: avg_lead_time = "%.1f" % avg_lead_time else: avg_lead_time = "None" url = make_url( DeliveryReport, self.config["domain"], "?location_id=%s&filter_by_program=%s&" "datespan_type=%s&datespan_first=%s&datespan_second=%s", ( loc.location_id, self.config["program"], self.config["datespan_type"], self.config["datespan_first"], self.config["datespan_second"], ), ) rows.append([link_format(loc.name, url), avg_lead_time]) return rows
def report_facilities_url(self): config = self.report_config return make_url( StockOnHandReport, self.domain, '?location_id=%s&filter_by_program=%s&datespan_type=%s&datespan_first=%s&datespan_second=%s', (config['location_id'], config['program'], self.type, self.first, self.second))
def rows(self): rows = [] enddate = self.config['enddate'] products = self.get_products() if self.config['location_id']: locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id']) for loc in locations: supply_point = loc.supply_point_id status, last_reported = get_last_reported(supply_point, self.config['domain'], enddate) hisp = get_hisp_resp_rate(loc) url = make_url(FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (loc.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) row_data = [ loc.site_code, link_format(loc.name, url), loc.metadata.get('group', None), icon_format(status, last_reported), "<span title='%d of %d'>%s%%</span>" % (hisp[1], hisp[2], floatformat(hisp[0] * 100.0)) if hisp else "No data" ] for product in products: last_of_the_month = get_day_of_month(enddate.year, enddate.month, -1) first_of_the_next_month = last_of_the_month + timedelta(days=1) try: srs = StockTransaction.objects.filter( report__domain=self.config['domain'], report__date__lt=first_of_the_next_month, case_id=supply_point, product_id=product.product_id ).order_by("-report__date")[0] except IndexError: srs = None if srs: try: ss = StockState.objects.get(case_id=supply_point, product_id=product.product_id) val = calculate_months_remaining(ss, srs.stock_on_hand) ret = _months_or_default(val, -1) except StockState.DoesNotExist: ret = -1 else: ret = -1 row_data.append(product_format(ret, srs, self.config['soh_month'])) rows.append(row_data) return rows
def rows(self): rows = [] enddate = self.config['enddate'] products = self.get_products() if self.config['location_id']: locations = SQLLocation.active_objects.filter(parent__location_id=self.config['location_id']) for loc in locations: supply_point = loc.supply_point_id status, last_reported = get_last_reported(supply_point, self.config['domain'], enddate) hisp = get_hisp_resp_rate(loc) url = make_url(FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (loc.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) row_data = [ loc.site_code, link_format(loc.name, url), loc.metadata.get('group', None), icon_format(status, last_reported), "<span title='%d of %d'>%s%%</span>" % (hisp[1], hisp[2], floatformat(hisp[0] * 100.0)) if hisp else "No data" ] for product in products: last_of_the_month = get_day_of_month(enddate.year, enddate.month, -1) first_of_the_next_month = last_of_the_month + timedelta(days=1) try: srs = StockTransaction.objects.filter( report__domain=self.config['domain'], report__date__lt=first_of_the_next_month, case_id=supply_point, product_id=product.product_id ).order_by("-report__date")[0] except IndexError: srs = None if srs: try: ss = StockState.objects.get(case_id=supply_point, product_id=product.product_id) val = calculate_months_remaining(ss, srs.stock_on_hand) ret = _months_or_default(val, -1) except StockState.DoesNotExist: ret = -1 else: ret = -1 row_data.append(product_format(ret, srs, self.config['soh_month'])) rows.append(row_data) return rows
def report_stockonhand_url(self): from custom.ilsgateway.tanzania.reports.stock_on_hand import StockOnHandReport return html.escape(make_url(StockOnHandReport, self.domain, '?location_id=%s&month=%s&year=%s', (self.request_params['location_id'], self.request_params['month'], self.request_params['year'])))
def report_delivery_url(self): from custom.ilsgateway.tanzania.reports.delivery import DeliveryReport return html.escape(make_url(DeliveryReport, self.domain, '?location_id=%s&month=%s&year=%s', (self.request_params['location_id'], self.request_params['month'], self.request_params['year'])))
def report_supervision_url(self): from custom.ilsgateway.tanzania.reports.supervision import SupervisionReport return html.escape(make_url(SupervisionReport, self.domain, '?location_id=%s&month=%s&year=%s', (self.request_params['location_id'], self.request_params['month'], self.request_params['year'])))
def report_rand_url(self): from custom.ilsgateway.tanzania.reports.randr import RRreport return html.escape(make_url(RRreport, self.domain, '?location_id=%s&month=%s&year=%s', (self.request_params['location_id'], self.request_params['month'], self.request_params['year'])))
def report_facilities_url(self): config = self.report_config return make_url( StockOnHandReport, self.domain, '?location_id=%s&filter_by_program=%s&datespan_type=%s&datespan_first=%s&datespan_second=%s', (config['location_id'], config['program'], self.type, self.first, self.second) )
def report_facilities_url(self): config = self.report_config return html.escape(make_url( StockOnHandReport, self.domain, '?location_id=%s&month=%s&year=%s&filter_by_program=%s%s', (config['location_id'], config['month'], config['year'], config['program'], config['prd_part_url']) ))
def rows(self): rows = [] if not self.config['products']: prd_id = SQLProduct.objects.filter(domain=self.config['domain'])\ .order_by('code').values_list('product_id', flat=True) else: prd_id = self.config['products'] if self.config['location_id']: locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id'], site_code__icontains=self.config['msd_code']) for loc in locations: org_summary = OrganizationSummary.objects.filter(date__range=(self.config['startdate'], self.config['enddate']), supply_point=loc.location_id) self.config['org_summary'] = org_summary soh_data = SohSubmissionData(config=self.config).rows[0] facs = get_facilities(loc, self.config['domain']) facs_count = facs.count() soh_on_time = soh_data.on_time * 100 / facs_count soh_late = soh_data.late * 100 / facs_count soh_not_responding = soh_data.not_responding * 100 / facs_count fac_ids = facs.exclude(supply_point_id__isnull=True).values_list(*['supply_point_id'], flat=True) stockouts = (StockTransaction.objects.filter( case_id__in=fac_ids, quantity__lte=0, report__date__month=self.config['enddate'].month, report__date__year=self.config['enddate'].year).count() or 0) / facs_count url = make_url( StockOnHandReport, self.config['domain'], '?location_id=%s&month=%s&year=%s', (loc.location_id, self.config['month'], self.config['year'])) row_data = [ link_format(loc.name, url), format_percent(soh_on_time), format_percent(soh_late), format_percent(soh_not_responding), format_percent(stockouts) ] for product in prd_id: ps = ProductAvailabilityData.objects.filter( supply_point=loc.location_id, product=product, date__range=(self.config['startdate'], self.config['enddate']))\ .aggregate(without_stock=Avg('without_stock'), total=Max('total')) if ps['without_stock'] and ps['total']: row_data.append(format_percent(ps['without_stock'] * 100 / float(ps['total']))) else: row_data.append("<span class='no_data'>None</span>") rows.append(row_data) return rows
def ils_make_url(self, cls): params = '?location_id=%s&filter_by_program=%s&datespan_type=%s&datespan_first=%s&datespan_second=%s' return make_url(cls, self.domain, params, ( self.request.GET.get('location_id'), self.request.GET.get('filter_by_program'), self.request.GET.get('datespan_type', ''), self.request.GET.get('datespan_first', ''), self.request.GET.get('datespan_second', ''), ))
def rows(self): rows = [] if not self.config['products']: prd_id = SQLProduct.objects.filter(domain=self.config['domain'])\ .order_by('code').values_list('product_id') else: prd_id = self.config['products'] if self.config['location_id']: locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id'], site_code__icontains=self.config['msd_code']) for loc in locations: org_summary = OrganizationSummary.objects.filter(date__range=(self.config['startdate'], self.config['enddate']), supply_point=loc.location_id)[0] soh_data = GroupSummary.objects.get(title=SupplyPointStatusTypes.SOH_FACILITY, org_summary=org_summary) facs = get_facilities(loc, self.config['domain']) facs_count = facs.count() soh_on_time = soh_data.on_time * 100 / facs_count soh_late = soh_data.late * 100 / facs_count soh_not_responding = soh_data.not_responding * 100 / facs_count fac_ids = facs.exclude(supply_point_id__isnull=True).values_list(*['supply_point_id'], flat=True) stockouts = (StockTransaction.objects.filter( case_id__in=fac_ids, quantity__lte=0, report__date__month=int(self.config['month']), report__date__year=int(self.config['year'])).count() or 0) / facs_count url = make_url( StockOnHandReport, self.config['domain'], '?location_id=%s&month=%s&year=%s', (loc.location_id, self.config['month'], self.config['year'])) row_data = [ link_format(loc.name, url), format_percent(soh_on_time), format_percent(soh_late), format_percent(soh_not_responding), format_percent(stockouts) ] for product in prd_id: ps = ProductAvailabilityData.objects.filter( supply_point=loc.external_id, product=product, date=self.config['startdate']) if ps: row_data.append(format_percent(ps[0].without_stock * 100 / float(ps[0].total))) else: row_data.append("<span class='no_data'>None</span>") rows.append(row_data) return rows
def title_url(self): soh_month = True if self.config['soh_month']: soh_month = False return html.escape(make_url(StockOnHandReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s&soh_month=%s', (self.config['location_id'], self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'], soh_month)))
def rows(self): rows = [] if self.config['location_id'] and self.config['org_summary']: locations = SQLLocation.active_objects.filter( parent__location_id=self.config['location_id']) for loc in locations: facilities = loc.get_descendants().filter( location_type__administrative=False, is_archived=False).count() org_summary = OrganizationSummary.objects.filter( date__range=(self.config['startdate'], self.config['enddate']), location_id=loc.location_id) self.config['org_summary'] = org_summary soh_data = SupervisionSummaryData(config=self.config).rows[0] total_responses = 0 total_possible = 0 for group_summary in GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=loc.location_id, title=SupplyPointStatusTypes.SUPERVISION_FACILITY): if group_summary: total_responses += group_summary.responded total_possible += group_summary.total if total_possible: response_rate = "%.1f%%" % (100.0 * total_responses / total_possible) else: response_rate = "<span class='no_data'>None</span>" url = make_url( SupervisionReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (loc.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) rows.append([ link_format(loc.name, url), format_percent( float(soh_data.received) * 100 / float(facilities or 1)), format_percent( float(soh_data.not_received) * 100 / float(facilities or 1)), format_percent( float(soh_data.not_responding) * 100 / float(facilities or 1)), response_rate ]) return rows
def rows(self): rows = [] locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id']) dg = [] for date in list(rrule.rrule(rrule.MONTHLY, dtstart=self.config['startdate'], until=self.config['enddate'])): dg.extend(DeliveryGroups().delivering(locations, date.month)) for child in dg: group_summary = GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=child.location_id, title=SupplyPointStatusTypes.DELIVERY_FACILITY, total=1 ).exists() if not group_summary: continue latest = latest_status_or_none( child.location_id, SupplyPointStatusTypes.DELIVERY_FACILITY, self.config['startdate'], self.config['enddate'] ) status_name = latest.name if latest else "" status_date = latest.status_date.strftime("%d-%m-%Y") if latest else "None" url = make_url(FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (child.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) cycle_lead_time = get_this_lead_time( child.location_id, self.config['startdate'], self.config['enddate'] ) avg_lead_time = get_avg_lead_time(child.location_id, self.config['startdate'], self.config['enddate']) rows.append( [ child.site_code, link_format(child.name, url), status_name, status_date, cycle_lead_time, avg_lead_time ] ) return rows
def rows(self): rows = [] if self.config['org_summary']: locations = SQLLocation.objects.filter( parent__location_id=self.config['location_id']) for child in locations: try: org_summary = OrganizationSummary.objects.filter( date__range=(self.config['startdate'], self.config['enddate']), location_id=child.location_id) except OrganizationSummary.DoesNotExist: return [] self.config['org_summary'] = org_summary rr_data = RandRSubmissionData(config=self.config).rows[0] fp_partial = partial(rr_format_percent, denominator=rr_data.total) total_responses = 0 total_possible = 0 group_summaries = GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=child.location_id, title=SupplyPointStatusTypes.R_AND_R_FACILITY) for group_summary in group_summaries: if group_summary: total_responses += group_summary.responded total_possible += group_summary.total hist_resp_rate = rr_format_percent(total_responses, total_possible) url = make_url( RRreport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (child.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) rows.append([ link_format(child.name, url), fp_partial(rr_data.on_time), fp_partial(rr_data.late), fp_partial(rr_data.not_submitted), fp_partial(rr_data.not_responding), hist_resp_rate ]) return rows
def title_url(self): soh_month = True if self.config['soh_month']: soh_month = False return html.escape(make_url(StockOnHandReport, self.config['domain'], '?location_id=%s&month=%s&year=%s&soh_month=%s&filter_by_program=%s%s', (self.config['location_id'], self.config['month'], self.config['year'], soh_month, self.config['program'], self.config['prd_part_url'])))
def rows(self): rows = [] if self.config['org_summary']: locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id']) for child in locations: try: org_summary = OrganizationSummary.objects.filter( date__range=(self.config['startdate'], self.config['enddate']), location_id=child.location_id ) except OrganizationSummary.DoesNotExist: return [] self.config['org_summary'] = org_summary rr_data = RandRSubmissionData(config=self.config).rows[0] fp_partial = partial(rr_format_percent, denominator=rr_data.total) total_responses = 0 total_possible = 0 group_summaries = GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=child.location_id, title=SupplyPointStatusTypes.R_AND_R_FACILITY ) for group_summary in group_summaries: if group_summary: total_responses += group_summary.responded total_possible += group_summary.total hist_resp_rate = rr_format_percent(total_responses, total_possible) url = make_url(RRreport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (child.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) rows.append( [ link_format(child.name, url), fp_partial(rr_data.on_time), fp_partial(rr_data.late), fp_partial(rr_data.not_submitted), fp_partial(rr_data.not_responding), hist_resp_rate ] ) return rows
def rows(self): rows = [] locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id'], site_code__icontains=self.config['msd_code']) for child in locations: try: org_summary = OrganizationSummary.objects.get( date__range=(self.config['startdate'], self.config['enddate']), supply_point=child.location_id ) except OrganizationSummary.DoesNotExist: return [] rr_data = GroupSummary.objects.get( title=SupplyPointStatusTypes.R_AND_R_FACILITY, org_summary=org_summary ) fp_partial = partial(rr_format_percent, denominator=rr_data.total) total_responses = 0 total_possible = 0 group_summaries = GroupSummary.objects.filter( org_summary__date__lte=datetime(int(self.config['year']), int(self.config['month']), 1), org_summary__supply_point=child.location_id, title='rr_fac' ) for g in group_summaries: if g: total_responses += g.responded total_possible += g.total hist_resp_rate = rr_format_percent(total_responses, total_possible) args = (child.location_id, self.config['month'], self.config['year']) url = make_url(RRreport, self.config['domain'], '?location_id=%s&month=%s&year=%s', args) rows.append( [ link_format(child.name, url), fp_partial(rr_data.on_time), fp_partial(rr_data.late), fp_partial(rr_data.not_submitted), fp_partial(rr_data.not_responding), hist_resp_rate ] ) return rows
def rows(self): rows = [] if self.config['location_id']: locations = SQLLocation.active_objects.filter( parent__location_id=self.config['location_id']) for loc in locations: total_responses = 0 total_possible = 0 group_summaries = GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=loc.location_id, title=SupplyPointStatusTypes.SUPERVISION_FACILITY, total=1) if not group_summaries: continue for group_summary in group_summaries: if group_summary: total_responses += group_summary.responded total_possible += group_summary.total if total_possible: response_rate = "%.1f%%" % (100.0 * total_responses / total_possible) else: response_rate = "<span class='no_data'>None</span>" url = make_url( FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (loc.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) latest = latest_status_or_none( loc.location_id, SupplyPointStatusTypes.SUPERVISION_FACILITY, self.config['startdate'], self.config['enddate']) rows.append([ loc.site_code, link_format(loc.name, url), latest.name if latest else None, latest.status_date.strftime('%b. %d, %Y') if latest else '', response_rate ]) return rows
def _format_row(self, percent_stockouts, soh_late, soh_not_responding, soh_on_time, sql_location): url = make_url( StockOnHandReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (sql_location.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second']) ) row_data = [ link_format(sql_location.name, url), format_percent(soh_on_time), format_percent(soh_late), format_percent(soh_not_responding), format_percent(percent_stockouts) ] return row_data
def rows(self): rows = [] if self.config['location_id'] and self.config['org_summary']: locations = SQLLocation.active_objects.filter(parent__location_id=self.config['location_id']) for loc in locations: facilities = loc.get_descendants().filter( location_type__administrative=False, is_archived=False ).count() org_summary = OrganizationSummary.objects.filter( date__range=(self.config['startdate'], self.config['enddate']), location_id=loc.location_id ) self.config['org_summary'] = org_summary soh_data = SupervisionSummaryData(config=self.config).rows[0] total_responses = 0 total_possible = 0 for group_summary in GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=loc.location_id, title=SupplyPointStatusTypes.SUPERVISION_FACILITY ): if group_summary: total_responses += group_summary.responded total_possible += group_summary.total if total_possible: response_rate = "%.1f%%" % (100.0 * total_responses / total_possible) else: response_rate = "<span class='no_data'>None</span>" url = make_url(SupervisionReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (loc.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) rows.append([ link_format(loc.name, url), format_percent(float(soh_data.received) * 100 / float(facilities or 1)), format_percent(float(soh_data.not_received) * 100 / float(facilities or 1)), format_percent(float(soh_data.not_responding) * 100 / float(facilities or 1)), response_rate ]) return rows
def _format_row(self, percent_stockouts, soh_late, soh_not_responding, soh_on_time, sql_location): url = make_url( StockOnHandReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (sql_location.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) row_data = [ link_format(sql_location.name, url), format_percent(soh_on_time), format_percent(soh_late), format_percent(soh_not_responding), format_percent(percent_stockouts) ] return row_data
def rows(self): rows = [] if self.config['location_id']: locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id']) for loc in locations: total_responses = 0 total_possible = 0 group_summaries = GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=loc.location_id, title=SupplyPointStatusTypes.SUPERVISION_FACILITY, total=1 ) if not group_summaries: continue for group_summary in group_summaries: if group_summary: total_responses += group_summary.responded total_possible += group_summary.total if total_possible: response_rate = "%.1f%%" % (100.0 * total_responses / total_possible) else: response_rate = "<span class='no_data'>None</span>" url = make_url(FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (loc.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) latest = latest_status_or_none(loc.location_id, SupplyPointStatusTypes.SUPERVISION_FACILITY, self.config['startdate'], self.config['enddate']) rows.append([ loc.site_code, link_format(loc.name, url), latest.name if latest else None, latest.status_date.strftime('%b. %d, %Y') if latest else '', response_rate ]) return rows
def rows(self): rows = [] locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id'], site_code__icontains=self.config['msd_code']) dg = DeliveryGroups().submitting(locations, int(self.config['month'])) for child in dg: total_responses = 0 total_possible = 0 group_summaries = GroupSummary.objects.filter( org_summary__date__lte=datetime(int(self.config['year']), int(self.config['month']), 1), org_summary__supply_point=child.location_id, title='rr_fac' ) for g in group_summaries: if g: total_responses += g.responded total_possible += g.total hist_resp_rate = rr_format_percent(total_responses, total_possible) url = make_url(FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s%s', (child.location_id, self.config['program'], self.config['prd_part_url'])) rr_value = randr_value(child.location_id, int(self.config['month']), int(self.config['year'])) contact = get_default_contact_for_location(self.config['domain'], child.location_id) if contact: role = contact.user_data.get('role') or "" args = (contact.first_name, contact.last_name, role, contact.default_phone_number) contact_string = "%s %s (%s) %s" % args else: contact_string = "" rows.append( [ child.site_code, link_format(child.name, url), get_span(rr_value) % (format(rr_value, "d M Y") if rr_value else "Not reported"), contact_string, hist_resp_rate ] ) return rows
def rows(self): rows = [] if self.config['location_id']: locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id'], site_code__icontains=self.config['msd_code']) for loc in locations: facilities = SQLLocation.objects.filter(parent=loc).count() org_summary = OrganizationSummary.objects.filter(date__range=(self.config['startdate'], self.config['enddate']), supply_point=loc.location_id)[0] soh_data = GroupSummary.objects.get(title=SupplyPointStatusTypes.SUPERVISION_FACILITY, org_summary=org_summary) total_responses = 0 total_possible = 0 for g in GroupSummary.objects.filter(org_summary__date__lte=datetime(int(self.config['year']), int(self.config['month']), 1), org_summary__supply_point=loc.location_id, title=SupplyPointStatusTypes.SUPERVISION_FACILITY): if g: total_responses += g.responded total_possible += g.total if total_possible: response_rate = "%.1f%%" % (100.0 * total_responses / total_possible) else: response_rate = "<span class='no_data'>None</span>" url = make_url(SupervisionReport, self.config['domain'], '?location_id=%s&month=%s&year=%s', (loc.location_id, self.config['month'], self.config['year'])) rows.append([ link_format(loc.name, url), format_percent(float(soh_data.received) * 100 / float(facilities)), format_percent(float(soh_data.not_received) * 100 / float(facilities)), format_percent(float(soh_data.not_responding) * 100 / float(facilities)), response_rate ]) return rows
def rows(self): rows = [] if not self.config['products']: prd_id = SQLProduct.objects.filter( domain=self.config['domain'] ).order_by('code').values_list('product_id', flat=True) else: prd_id = self.config['products'] if self.config['location_id']: locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id']) for loc in locations: org_summary = OrganizationSummary.objects.filter( date__range=(self.config['startdate'], self.config['enddate']), location_id=loc.location_id ) self.config['org_summary'] = org_summary soh_rows = SohSubmissionData(config=self.config).rows soh_data = soh_rows[0] if soh_rows else None facs = get_facilities(loc, self.config['domain']) facs_count = facs.count() if soh_data and facs_count > 0: soh_on_time = soh_data.on_time * 100 / facs_count soh_late = soh_data.late * 100 / facs_count soh_not_responding = soh_data.not_responding * 100 / facs_count else: soh_on_time = None soh_late = None soh_not_responding = None if facs_count > 0: fac_ids = facs.exclude(supply_point_id__isnull=True).values_list('supply_point_id', flat=True) enddate = self.config['enddate'] month = enddate.month - 1 if enddate.month != 1 else 12 year = enddate.year - 1 if enddate.month == 1 else enddate.year stockouts = StockTransaction.objects.filter( case_id__in=list(fac_ids), stock_on_hand__lte=0, report__domain=self.config['domain'], report__date__range=[ datetime(year, month, 1), datetime(enddate.year, enddate.month, 1) ] ).order_by('case_id').distinct('case_id').count() percent_stockouts = (stockouts or 0) * 100 / float(facs_count) else: percent_stockouts = 0 url = make_url(StockOnHandReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (loc.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) row_data = [ link_format(loc.name, url), format_percent(soh_on_time), format_percent(soh_late), format_percent(soh_not_responding), format_percent(percent_stockouts) ] for product in prd_id: ps = ProductAvailabilityData.objects.filter( location_id=loc.location_id, product=product, date__range=(self.config['startdate'], self.config['enddate']) ).aggregate(without_stock=Avg('without_stock'), total=Max('total')) if ps['without_stock'] and ps['total']: row_data.append(format_percent(ps['without_stock'] * 100 / float(ps['total']))) else: row_data.append("<span class='no_data'>No Data</span>") rows.append(row_data) return rows
def rows(self): rows = [] enddate = self.config['enddate'] def get_last_reported(supplypoint): last_bd_of_the_month = get_business_day_of_month(enddate.year, enddate.month, -1) st = StockTransaction.objects.filter( case_id=supplypoint, type='stockonhand', report__date__lte=last_bd_of_the_month ).order_by('-report__date') last_bd_of_last_month = datetime.combine(get_business_day_of_month(enddate.year, enddate.month, -1), time()) if st: sts = _reported_on_time(last_bd_of_last_month, st[0].report.date) return sts, st[0].report.date.date() else: sts = OnTimeStates.NO_DATA return sts, None def get_hisp_resp_rate(location): statuses = SupplyPointStatus.objects.filter(supply_point=location.location_id, status_type=SupplyPointStatusTypes.SOH_FACILITY) if not statuses: return None status_month_years = set([(x.status_date.month, x.status_date.year) for x in statuses]) denom = len(status_month_years) num = 0 for s in status_month_years: f = statuses.filter(status_date__month=s[0], status_date__year=s[1]).filter( Q(status_value=SupplyPointStatusValues.SUBMITTED) | Q(status_value=SupplyPointStatusValues.NOT_SUBMITTED) | Q(status_value=SupplyPointStatusValues.RECEIVED) | Q(status_value=SupplyPointStatusValues.NOT_RECEIVED)).order_by("-status_date") if f.count(): num += 1 return float(num) / float(denom), num, denom if not self.config['products']: products = SQLProduct.objects.filter(domain=self.config['domain']).order_by('code') else: products = SQLProduct.objects.filter(product_id__in=self.config['products'], domain=self.config['domain']).order_by('code') if self.config['location_id']: locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id'], site_code__icontains=self.config['msd_code']) for loc in locations: supply_point = loc.supply_point_id status, last_reported = get_last_reported(supply_point) hisp = get_hisp_resp_rate(loc) url = make_url(FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s%s', (loc.location_id, self.config['program'], self.config['prd_part_url'])) row_data = [ loc.site_code, link_format(loc.name, url), loc.metadata.get('group', None), icon_format(status, last_reported), "<span title='%d of %d'>%s%%</span>" % (hisp[1], hisp[2], floatformat(hisp[0] * 100.0)) if hisp else "No data" ] for product in products: last_of_the_month = get_day_of_month(enddate.year, enddate.month, -1) first_of_the_next_month = last_of_the_month + timedelta(days=1) try: srs = StockTransaction.objects.filter( report__date__lt=first_of_the_next_month, case_id=supply_point, product_id=product.product_id).order_by("-report__date")[0] except IndexError: srs = None if srs: def calculate_months_remaining(stock_state, quantity): consumption = stock_state.get_monthly_consumption() if consumption is not None and consumption > 0 and quantity is not None: return float(quantity) / float(consumption) elif quantity == 0: return 0 return None try: ss = StockState.objects.get(case_id=supply_point, product_id=product.product_id) val = calculate_months_remaining(ss, srs.stock_on_hand) ret = _months_or_default(val, -1) except StockState.DoesNotExist: ret = -1 else: ret = -1 row_data.append(product_format(ret, srs, self.config['soh_month'])) rows.append(row_data) return rows
def rows(self): rows = [] locations = SQLLocation.objects.filter( parent__location_id=self.config['location_id']) dg = [] for date in list( rrule.rrule(rrule.MONTHLY, dtstart=self.config['startdate'], until=self.config['enddate'])): dg.extend(DeliveryGroups().submitting(locations, date.month)) for child in dg: total_responses = 0 total_possible = 0 submitted, rr_value = randr_value(child.location_id, self.config['startdate'], self.config['enddate']) if child.is_archived and not rr_value: continue group_summaries = GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=child.location_id, title=SupplyPointStatusTypes.R_AND_R_FACILITY, total=1) if not group_summaries: continue for group_summary in group_summaries: if group_summary: total_responses += group_summary.responded total_possible += group_summary.total hist_resp_rate = rr_format_percent(total_responses, total_possible) url = make_url( FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (child.location_id, self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) contact = get_one_user_at_location(self.config['domain'], child.location_id) if contact: role = contact.user_data.get('role') or "" args = (contact.first_name, contact.last_name, role, contact.default_phone_number) contact_string = "%s %s (%s) %s" % args else: contact_string = "" rows.append([ child.site_code, link_format(child.name, url), get_span(submitted) % (rr_value.strftime("%d %b %Y") if rr_value else "Not reported"), contact_string, hist_resp_rate ]) return rows