def rows(self): rows = [] if self.location_id: if self.location.location_type.name == 'country': supply_points = self.reporting_supply_points(self.all_reporting_locations()) else: supply_points = self.reporting_supply_points() for location in SQLLocation.objects.filter(supply_point_id__in=supply_points): st = StockTransaction.objects.filter( case_id=location.supply_point_id, report__date__range=[self.config['startdate'], self.config['enddate']] ).order_by('-report__date') products_per_location = {product.product_id for product in location.products} if products_per_location - set(st.values_list('product_id', flat=True)): if st: date = st[0].report.date.strftime("%m-%d-%Y") else: date = '---' url = make_url( StockLevelsReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (location.location_id, self.config['startdate'], self.config['enddate'])) rows.append([link_format(location.name, url), date]) return rows
def rows(self): rows = [] if self.location_id: for location in self.get_locations: supply_points = self.get_supply_points(location.location_id) sites = supply_points.count() reported = StockTransaction.objects.filter( case_id__in=supply_points.values_list('supply_point_id', flat=True), report__date__range=[self.config['startdate'], self.config['enddate']] ).distinct('case_id').count() reporting_rates = '%.2f%%' % (reported * 100 / (float(sites) or 1.0)) completed = 0 for supply_point in supply_points: reported_products = StockTransaction.objects.filter( case_id=supply_point.supply_point_id, report__date__range=[self.config['startdate'], self.config['enddate']] ).distinct('product_id').values_list('product_id', flat=True) if not (set(supply_point.products) - set(reported_products)): completed += 1 completed_rates = '%.2f%%' % (completed * 100 / (float(sites) or 1.0)) url = make_url( ReportingRatesReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (location.location_id, self.config['startdate'], self.config['enddate'])) rows.append([link_format(location.name, url), sites, reported, reporting_rates, completed_rates]) 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 rows(self): rows = [] if self.config['location_id']: product_id_to_name = { product_id: product_name for (product_id, product_name) in self.config['unique_products'].values_list('product_id', 'name') } for supply_point in self.config['stockout_table_supply_points']: products_set = self.config['stockouts'].get(supply_point.supply_point_id) url = link_format(supply_point.name, make_url( StockStatus, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (supply_point.location_id, self.config['startdate'], self.config['enddate']) )) if products_set: rows.append( [url if not self.config.get('is_rendered_as_email') else supply_point.name, ', '.join( product_id_to_name[product_id] for product_id in products_set )] ) else: rows.append( [url if not self.config.get('is_rendered_as_email') else supply_point.name, '-'] ) return rows
def rows(self): rows = [] unique_products = self.unique_products( get_supply_points(self.config['domain'], self.config['location_id']), all=(not self.config['export']) ) if self.config['location_id']: for case_id, products in self.config['months_of_stock'].iteritems(): sp = SQLLocation.objects.get(supply_point_id=case_id) url = make_url( StockStatus, self.config['domain'], '?location_id=%s&filter_by_program=%s&startdate=%s&enddate=%s&report_type=%s', (sp.location_id, self.config['program'] or ALL_OPTION, self.config['startdate'].date(), self.config['enddate'].date(), self.config['report_type']) ) row = [ link_format(sp.name, url) if not self.config.get('is_rendered_as_email', False) else sp.name ] for p in unique_products: product_data = products.get(p.product_id) if product_data: value = '%.1f' % product_data else: value = '-' row.append(value) rows.append(row) 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']: supply_points = get_supply_points(self.config['location_id'], self.config['domain']).values_list( 'supply_point_id', flat=True ) last_period_st, last_period_end = calculate_last_period(self.config['enddate']) reported = StockTransaction.objects.filter(case_id__in=supply_points, report__date__range=[last_period_st, last_period_end] ).values_list('case_id', flat=True) not_reported = SQLLocation.objects.filter(location_type__in=self.location_types, parent__location_id=self.config['location_id'])\ .exclude(supply_point_id__in=reported) for loc in not_reported: url = make_url( StockLevelsReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (loc.location_id, self.config['startdate'], self.config['enddate'])) st = StockTransaction.objects.filter(case_id=loc.supply_point_id).order_by('-report__date') if st: date = st[0].report.date else: date = _('---') rows.append([link_format(loc.name, url), date]) return rows
def rows(self): rows = [] if self.config['location_id']: product_id_to_name = { product_id: product_name for (product_id, product_name) in self.config['unique_products'].values_list('product_id', 'name') } for supply_point in self.config['stockout_table_supply_points']: products_set = self.config['stockouts'].get(supply_point.supply_point_id) url = link_format(supply_point.name, make_url( StockLevelsReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (supply_point.location_id, self.config['startdate'], self.config['enddate']) )) if products_set: rows.append( [url if not self.config.get('is_rendered_as_email') else supply_point.name, ', '.join( product_id_to_name[product_id] for product_id in products_set )] ) else: rows.append( [url if not self.config.get('is_rendered_as_email') else supply_point.name, '-'] ) return rows
def rows(self): rows = [] if self.config['location_id']: last_period_st, last_period_end = calculate_last_period(self.config['enddate']) locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id'], location_type__in=self.location_types) for loc in locations: st = StockTransaction.objects.filter(case_id=loc.supply_point_id, report__date__range=[last_period_st, last_period_end] ).order_by('-report__date') st_count = st.distinct('product_id').count() if len(loc.products) != st_count: if st: date = st[0].report.date else: date = '---' url = make_url( StockLevelsReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (loc.location_id, self.config['startdate'], self.config['enddate'])) rows.append([link_format(loc.name, url), date]) 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 ) 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): 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 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 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 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 = [] if self.location_id: for name, location_id, date in self.config['incomplete_table']: url = make_url( StockLevelsReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (location_id, self.config['startdate'], self.config['enddate']) ) rows.append([link_format(name, url), ews_date_format(date)]) 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 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['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 = [] if self.location_id: for name, location_id, date in self.config['incomplete_table']: url = make_url(ReportingRatesReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (location_id, self.config['startdate'], self.config['enddate'])) rows.append([ link_format(name, url) if not self.config['is_rendered_as_email'] else name, ews_date_format(date) ]) return rows
def rows(self): rows = [] if self.location_id: for name, location_id, date in self.config["incomplete_table"]: url = make_url( ReportingRatesReport, self.config["domain"], "?location_id=%s&startdate=%s&enddate=%s", (location_id, self.config["startdate"], self.config["enddate"]), ) rows.append( [link_format(name, url) if not self.config["is_rendered_as_email"] else name, ews_date_format(date)] ) 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 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 = [] if self.location_id: for name, location_id, date in self.config['incomplete_table']: url = make_url( ReportingRatesReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (location_id, self.config['startdate'], self.config['enddate']) ) rows.append( [ link_format(name, url) if not self.config['is_rendered_as_email'] else name, ews_date_format(date) ] ) 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 _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 = [] if self.config['location_id']: for sp in self.get_supply_points: if sp.location_type.administrative: cls = StockLevelsReport else: cls = StockStatus url = make_url( cls, self.config['domain'], '?location_id=%s&filter_by_program=%s&startdate=%s&enddate=%s&report_type=%s', (sp.location_id, self.config['program'] or ALL_OPTION, self.config['startdate'].date(), self.config['enddate'].date(), self.config['report_type'])) row = [link_format(sp.name, url)] products = self.unique_products(self.get_supply_points, all=(not self.config['export'])) transactions = list(StockTransaction.objects.filter( type='stockonhand', product_id__in=products.values_list('product_id', flat=True), case_id=sp.supply_point_id, report__date__lte=self.config['enddate'] ).order_by('-report__date')) states = list(StockState.objects.filter( case_id=sp.supply_point_id, product_id__in=products.values_list('product_id', flat=True), section_id='stock' )) for p in products: transaction = first_item(transactions, lambda x: x.product_id == p.product_id) state = first_item(states, lambda x: x.product_id == p.product_id) if transaction and state: if state.daily_consumption: row.append("%.1f" % (transaction.stock_on_hand / state.get_monthly_consumption())) else: row.append('-') else: row.append('-') rows.append(row) 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']: for sp in self.get_supply_points: location_types = [loc_type.name for loc_type in filter( lambda loc_type: not loc_type.administrative, Domain.get_by_name(self.config['domain']).location_types )] if sp.location_type in location_types: cls = StockLevelsReport else: cls = StockStatus url = make_url( cls, self.config['domain'], '?location_id=%s&filter_by_program=%s&startdate=%s' '&enddate=%s&report_type=%s&filter_by_product=%s', (sp.location_id, self.config['program'] or ALL_OPTION, self.config['startdate'], self.config['enddate'], self.config['report_type'], '&filter_by_product='.join(self.config['products']))) row = [link_format(sp.name, url)] for p in self.unique_products(self.get_supply_points, all=True): transaction = StockTransaction.objects.filter( type='stockonhand', product_id=p.product_id, case_id=sp.supply_point_id, report__date__lte=self.config['enddate'], report__date__gte=self.config['startdate'] ).order_by('-report__date') state = StockState.objects.filter(sql_product=p, case_id=sp.supply_point_id)\ .order_by('-last_modified_date') if transaction and state: monthly = state[0].get_monthly_consumption() if monthly: row.append(int(transaction[0].stock_on_hand / monthly)) else: row.append(0) else: row.append('-') rows.append(row) 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 self.location_id: for name, location_id, date, supply_point_id in self.config['non_reporting_table']: url = make_url( ReportingRatesReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (location_id, self.config['startdate'], self.config['enddate']) ) st = StockTransaction.objects.filter( case_id=supply_point_id, report__date__lte=self.config['startdate'] ).select_related('report').order_by('-report__date') if st: date = ews_date_format(st[0].report.date) else: date = '---' rows.append([link_format(name, url) if not self.config['is_rendered_as_email'] else name, date]) return rows
def rows(self): rows = [] if self.location_id and self.locations: for location_name, values in self.config["summary_reporting_rates"].iteritems(): url = make_url( ReportingRatesReport, self.config["domain"], "?location_id=%s&startdate=%s&enddate=%s", (values["location_id"], self.config["startdate"], self.config["enddate"]), ) is_rendered_as_email = self.config["is_rendered_as_email"] rows.append( [ link_format(location_name, url) if not is_rendered_as_email else location_name, values["all"], values["complete"] + values["incomplete"], "%d%%" % (100 * (values["complete"] + values["incomplete"]) / (values["all"] or 1)), "%d%%" % (100 * values["complete"] / ((values["complete"] + values["incomplete"]) or 1)), ] ) return rows
def rows(self): rows = [] if self.location_id: for location_name, values in self.config[ 'summary_reporting_rates'].iteritems(): url = make_url( ReportingRatesReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (values['location_id'], self.config['startdate'], self.config['enddate'])) is_rendered_as_email = self.config['is_rendered_as_email'] rows.append([ link_format(location_name, url) if not is_rendered_as_email else location_name, values['all'], values['complete'] + values['incomplete'], '%d%%' % (100 * (values['complete'] + values['incomplete']) / (values['all'] or 1)), '%d%%' % (100 * values['complete'] / (values['all'] or 1)) ]) return rows
def rows(self): rows = [] if self.location_id: for name, location_id, date, supply_point_id in self.config['non_reporting_table']: url = make_url( StockLevelsReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (location_id, self.config['startdate'], self.config['enddate']) ) st = StockTransaction.objects.filter( case_id=supply_point_id, report__date__lte=self.config['startdate'] ).select_related('report__date').order_by('-report__date') if st: date = ews_date_format(st[0].report.date) else: date = '---' rows.append([link_format(name, url), date]) return rows
def rows(self): rows = [] if self.location_id: for location_name, values in self.config['summary_reporting_rates'].iteritems(): url = make_url( ReportingRatesReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (values['location_id'], self.config['startdate'], self.config['enddate']) ) rows.append( [ link_format(location_name, url), values['all'], values['complete'] + values['incomplete'], '%d%%' % (100 * (values['complete'] + values['incomplete']) / (values['all'] or 1)), '%d%%' % (100 * values['complete'] / (values['all'] or 1)) ] ) return rows
def rows(self): rows = [] if self.location_id and self.locations: for location_name, values in self.config['summary_reporting_rates'].iteritems(): url = make_url( ReportingRatesReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (values['location_id'], self.config['startdate'].strftime('%Y-%m-%d'), self.config['enddate'].strftime('%Y-%m-%d')) ) is_rendered_as_email = self.config['is_rendered_as_email'] rows.append( [ link_format(location_name, url) if not is_rendered_as_email else location_name, values['all'], values['complete'] + values['incomplete'], '%d%%' % (100 * (values['complete'] + values['incomplete']) / (values['all'] or 1)), '%d%%' % (100 * values['complete'] / ((values['complete'] + values['incomplete']) or 1)) ] ) 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