def hsas_no_supervision(request): """ HSAs working out of facilities that don't have any registered in charges or managers. """ base_facilitities = facility_supply_points_below(request.location) hsas = SupplyPoint.objects.filter(active=True, type=SupplyPointType.objects.get(code="hsa")) facilities_with_hsas = hsas.values_list("supplied_by", flat=True).distinct() orphaned_facilities = base_facilitities.exclude\ (contact__role__code__in=config.Roles.SUPERVISOR_ROLES) orphaned_facilities_with_hsas = orphaned_facilities.filter(pk__in=facilities_with_hsas) return [Alert(config.Alerts.FACILITY_NO_SUPERVISOR % {"facility": fac}, _facility_url(fac)) \ for fac in orphaned_facilities_with_hsas]
def hsas_no_supervision(request): """ HSAs working out of facilities that don't have any registered in charges or managers. """ base_facilitities = facility_supply_points_below(request.location) hsas = SupplyPoint.objects.filter( active=True, type=SupplyPointType.objects.get(code="hsa")) facilities_with_hsas = hsas.values_list("supplied_by", flat=True).distinct() orphaned_facilities = base_facilitities.exclude\ (contact__role__code__in=config.Roles.SUPERVISOR_ROLES) orphaned_facilities_with_hsas = orphaned_facilities.filter( pk__in=facilities_with_hsas) return [Alert(config.Alerts.FACILITY_NO_SUPERVISOR % {"facility": fac}, _facility_url(fac)) \ for fac in orphaned_facilities_with_hsas]
def _district_breakdown(datespan, facility=False): """ Breakdown of reporting information, by group and district """ em = get_em_districts() ept = get_ept_districts() em_reports = SortedDict() ept_reports = SortedDict() em_totals = defaultdict(lambda: 0) ept_totals = defaultdict(lambda: 0) em_totals.update({'no_stockouts_pct_p':{}, 'no_stockouts_p':{}, 'stockouts_duration_p':{}, 'stockouts_avg_duration_p':{}, 'totals_p':{}, 'discrepancies_p': {}, 'discrepancies_tot_p': {}, 'discrepancies_pct_p': {}, 'discrepancies_avg_p': {}, 'filled_orders_p': {}, 'req_times':[]}) ept_totals.update({'no_stockouts_pct_p':{}, 'no_stockouts_p':{}, 'stockouts_duration_p':{}, 'stockouts_avg_duration_p':{}, 'totals_p':{}, 'discrepancies_p': {}, 'discrepancies_pct_p': {}, 'discrepancies_tot_p': {}, 'discrepancies_avg_p': {}, 'filled_orders_p': {}, 'req_times':[]}) for d in em: if facility: bd = ReportingBreakdown(facility_supply_points_below(d), datespan, MNE=True) else: bd = ReportingBreakdown(hsa_supply_points_below(d), datespan, MNE=True) em_reports[d] = _to_totals(bd) _update_dict(em_totals, em_reports[d]) em_totals['req_times'] += bd.req_times em_reports[d]['no_stockouts_pct_p'] = bd.no_stockouts_pct_p em_reports[d]['no_stockouts_p'] = bd.no_stockouts_p em_reports[d]['totals_p'] = bd.totals_p em_reports[d]['avg_req_time'] = bd.avg_req_time em_reports[d]['discrepancies_pct_p'] = bd.discrepancies_pct_p em_reports[d]['discrepancies_avg_p'] = bd.discrepancies_avg_p em_reports[d]['discrepancies_tot_p'] = bd.discrepancies_tot_p em_reports[d]['discrepancies_p'] = bd.discrepancies_p em_reports[d]['stockouts_duration_p'] = bd.stockouts_duration_p em_reports[d]['stockouts_avg_duration_p'] = bd.stockouts_avg_duration_p _update_dict(em_totals['stockouts_duration_p'], bd.stockouts_duration_p) _update_dict(em_totals['no_stockouts_p'], bd.no_stockouts_p) _update_dict(em_totals['discrepancies_p'], bd.discrepancies_p) _update_dict(em_totals['discrepancies_tot_p'], bd.discrepancies_tot_p) _update_dict(em_totals['filled_orders_p'], bd.filled_orders_p) _update_dict(em_totals['totals_p'], bd.totals_p) for d in ept: if facility: bd = ReportingBreakdown(facility_supply_points_below(d), datespan, MNE=True) else: bd = ReportingBreakdown(hsa_supply_points_below(d), datespan, MNE=True) ept_reports[d] = _to_totals(bd) _update_dict(ept_totals, ept_reports[d]) ept_totals['req_times'] += bd.req_times ept_reports[d]['no_stockouts_pct_p'] = bd.no_stockouts_pct_p ept_reports[d]['no_stockouts_p'] = bd.no_stockouts_p ept_reports[d]['totals_p'] = bd.totals_p ept_reports[d]['discrepancies_pct_p'] = bd.discrepancies_pct_p ept_reports[d]['discrepancies_avg_p'] = bd.discrepancies_avg_p ept_reports[d]['discrepancies_tot_p'] = bd.discrepancies_tot_p ept_reports[d]['discrepancies_p'] = bd.discrepancies_p ept_reports[d]['avg_req_time'] = bd.avg_req_time ept_reports[d]['stockouts_duration_p'] = bd.stockouts_duration_p ept_reports[d]['stockouts_avg_duration_p'] = bd.stockouts_avg_duration_p _update_dict(ept_totals['no_stockouts_p'], bd.no_stockouts_p) _update_dict(ept_totals['stockouts_duration_p'], bd.stockouts_duration_p) _update_dict(ept_totals['discrepancies_p'], bd.discrepancies_p) _update_dict(ept_totals['discrepancies_tot_p'], bd.discrepancies_tot_p) _update_dict(ept_totals['filled_orders_p'], bd.filled_orders_p) _update_dict(ept_totals['totals_p'], bd.totals_p) for p in ept_totals['stockouts_duration_p']: ept_totals['stockouts_avg_duration_p'][p] = timedelta(seconds=sum(ept_totals['stockouts_duration_p'][p])/len(ept_totals['stockouts_duration_p'][p])) for p in em_totals['stockouts_duration_p']: em_totals['stockouts_avg_duration_p'][p] = timedelta(seconds=sum(em_totals['stockouts_duration_p'][p])/len(em_totals['stockouts_duration_p'][p])) for p in ept_totals['no_stockouts_p']: ept_totals['no_stockouts_pct_p'][p] = calc_percentage(ept_totals['no_stockouts_p'][p], ept_totals['totals_p'][p]) for p in em_totals['no_stockouts_p']: em_totals['no_stockouts_pct_p'][p] = calc_percentage(em_totals['no_stockouts_p'][p], em_totals['totals_p'][p]) for p in ept_totals['discrepancies_p']: ept_totals['discrepancies_pct_p'][p] = calc_percentage(ept_totals['discrepancies_p'][p], ept_totals['filled_orders_p'][p]) if ept_totals['discrepancies_p'][p]: ept_totals['discrepancies_avg_p'][p] = float(ept_totals['discrepancies_tot_p'][p]) / ept_totals['discrepancies_p'][p] for p in em_totals['discrepancies_p']: em_totals['discrepancies_pct_p'][p] = calc_percentage(em_totals['discrepancies_p'][p], em_totals['filled_orders_p'][p]) if em_totals['discrepancies_p'][p]: em_totals['discrepancies_avg_p'][p] = float(em_totals['discrepancies_tot_p'][p]) / em_totals['discrepancies_p'][p] if len(em_totals['req_times']): em_totals['req_times'] = timedelta(seconds=sum(em_totals['req_times'])/len(em_totals['req_times'])) else: em_totals['req_times'] = None if len(ept_totals['req_times']): ept_totals['req_times'] = timedelta(seconds=sum(ept_totals['req_times'])/len(ept_totals['req_times'])) else: ept_totals['req_times'] = None return {"em_reports": em_reports, "ept_reports": ept_reports, "em_totals": em_totals, "ept_totals": ept_totals}
def _district_breakdown(datespan, facility=False): """ Breakdown of reporting information, by group and district """ em = get_em_districts() ept = get_ept_districts() em_reports = SortedDict() ept_reports = SortedDict() em_totals = defaultdict(lambda: 0) ept_totals = defaultdict(lambda: 0) em_totals.update({ 'no_stockouts_pct_p': {}, 'no_stockouts_p': {}, 'stockouts_duration_p': {}, 'stockouts_avg_duration_p': {}, 'totals_p': {}, 'discrepancies_p': {}, 'discrepancies_tot_p': {}, 'discrepancies_pct_p': {}, 'discrepancies_avg_p': {}, 'filled_orders_p': {}, 'req_times': [] }) ept_totals.update({ 'no_stockouts_pct_p': {}, 'no_stockouts_p': {}, 'stockouts_duration_p': {}, 'stockouts_avg_duration_p': {}, 'totals_p': {}, 'discrepancies_p': {}, 'discrepancies_pct_p': {}, 'discrepancies_tot_p': {}, 'discrepancies_avg_p': {}, 'filled_orders_p': {}, 'req_times': [] }) for d in em: if facility: bd = ReportingBreakdown(facility_supply_points_below(d), datespan, MNE=True) else: bd = ReportingBreakdown(hsa_supply_points_below(d), datespan, MNE=True) em_reports[d] = _to_totals(bd) _update_dict(em_totals, em_reports[d]) em_totals['req_times'] += bd.req_times em_reports[d]['no_stockouts_pct_p'] = bd.no_stockouts_pct_p em_reports[d]['no_stockouts_p'] = bd.no_stockouts_p em_reports[d]['totals_p'] = bd.totals_p em_reports[d]['avg_req_time'] = bd.avg_req_time em_reports[d]['discrepancies_pct_p'] = bd.discrepancies_pct_p em_reports[d]['discrepancies_avg_p'] = bd.discrepancies_avg_p em_reports[d]['discrepancies_tot_p'] = bd.discrepancies_tot_p em_reports[d]['discrepancies_p'] = bd.discrepancies_p em_reports[d]['stockouts_duration_p'] = bd.stockouts_duration_p em_reports[d]['stockouts_avg_duration_p'] = bd.stockouts_avg_duration_p _update_dict(em_totals['stockouts_duration_p'], bd.stockouts_duration_p) _update_dict(em_totals['no_stockouts_p'], bd.no_stockouts_p) _update_dict(em_totals['discrepancies_p'], bd.discrepancies_p) _update_dict(em_totals['discrepancies_tot_p'], bd.discrepancies_tot_p) _update_dict(em_totals['filled_orders_p'], bd.filled_orders_p) _update_dict(em_totals['totals_p'], bd.totals_p) for d in ept: if facility: bd = ReportingBreakdown(facility_supply_points_below(d), datespan, MNE=True) else: bd = ReportingBreakdown(hsa_supply_points_below(d), datespan, MNE=True) ept_reports[d] = _to_totals(bd) _update_dict(ept_totals, ept_reports[d]) ept_totals['req_times'] += bd.req_times ept_reports[d]['no_stockouts_pct_p'] = bd.no_stockouts_pct_p ept_reports[d]['no_stockouts_p'] = bd.no_stockouts_p ept_reports[d]['totals_p'] = bd.totals_p ept_reports[d]['discrepancies_pct_p'] = bd.discrepancies_pct_p ept_reports[d]['discrepancies_avg_p'] = bd.discrepancies_avg_p ept_reports[d]['discrepancies_tot_p'] = bd.discrepancies_tot_p ept_reports[d]['discrepancies_p'] = bd.discrepancies_p ept_reports[d]['avg_req_time'] = bd.avg_req_time ept_reports[d]['stockouts_duration_p'] = bd.stockouts_duration_p ept_reports[d][ 'stockouts_avg_duration_p'] = bd.stockouts_avg_duration_p _update_dict(ept_totals['no_stockouts_p'], bd.no_stockouts_p) _update_dict(ept_totals['stockouts_duration_p'], bd.stockouts_duration_p) _update_dict(ept_totals['discrepancies_p'], bd.discrepancies_p) _update_dict(ept_totals['discrepancies_tot_p'], bd.discrepancies_tot_p) _update_dict(ept_totals['filled_orders_p'], bd.filled_orders_p) _update_dict(ept_totals['totals_p'], bd.totals_p) for p in ept_totals['stockouts_duration_p']: ept_totals['stockouts_avg_duration_p'][p] = timedelta( seconds=sum(ept_totals['stockouts_duration_p'][p]) / len(ept_totals['stockouts_duration_p'][p])) for p in em_totals['stockouts_duration_p']: em_totals['stockouts_avg_duration_p'][p] = timedelta( seconds=sum(em_totals['stockouts_duration_p'][p]) / len(em_totals['stockouts_duration_p'][p])) for p in ept_totals['no_stockouts_p']: ept_totals['no_stockouts_pct_p'][p] = calc_percentage( ept_totals['no_stockouts_p'][p], ept_totals['totals_p'][p]) for p in em_totals['no_stockouts_p']: em_totals['no_stockouts_pct_p'][p] = calc_percentage( em_totals['no_stockouts_p'][p], em_totals['totals_p'][p]) for p in ept_totals['discrepancies_p']: ept_totals['discrepancies_pct_p'][p] = calc_percentage( ept_totals['discrepancies_p'][p], ept_totals['filled_orders_p'][p]) if ept_totals['discrepancies_p'][p]: ept_totals['discrepancies_avg_p'][p] = float( ept_totals['discrepancies_tot_p'] [p]) / ept_totals['discrepancies_p'][p] for p in em_totals['discrepancies_p']: em_totals['discrepancies_pct_p'][p] = calc_percentage( em_totals['discrepancies_p'][p], em_totals['filled_orders_p'][p]) if em_totals['discrepancies_p'][p]: em_totals['discrepancies_avg_p'][p] = float( em_totals['discrepancies_tot_p'] [p]) / em_totals['discrepancies_p'][p] if len(em_totals['req_times']): em_totals['req_times'] = timedelta( seconds=sum(em_totals['req_times']) / len(em_totals['req_times'])) else: em_totals['req_times'] = None if len(ept_totals['req_times']): ept_totals['req_times'] = timedelta( seconds=sum(ept_totals['req_times']) / len(ept_totals['req_times'])) else: ept_totals['req_times'] = None return { "em_reports": em_reports, "ept_reports": ept_reports, "em_totals": em_totals, "ept_totals": ept_totals }
def health_center_stockout(request): sps = facility_supply_points_below(request.location) return [ProductStockAlert(stock.supply_point, stock.product) for stock in \ ProductStock.objects.filter(is_active=True, supply_point__in=sps).filter(quantity=0)]