Example #1
0
    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
Example #2
0
    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
Example #3
0
    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
Example #4
0
    def rows(self):
        rows = []
        products_ids = self.get_products_ids()

        if not self.config['location_id']:
            return rows

        location = SQLLocation.objects.get(
            location_id=self.config['location_id'])
        sql_locations = SQLLocation.objects.filter(
            parent__location_id=self.config['location_id'])
        is_mohsw = False
        stockouts_map = {}
        product_availabilities = {
            (pa['location_id'], pa['product']):
            (pa['without_stock'], pa['total'])
            for pa in ProductAvailabilityData.objects.filter(
                location_id__in=list(
                    sql_locations.values_list('location_id', flat=True)),
                date__range=(
                    self.config['startdate'],
                    self.config['enddate'])).values('location_id', 'product').
            annotate(without_stock=Avg('without_stock'), total=Max('total'))
        }
        if location.location_type.name == 'MOHSW':
            is_mohsw = True
            stockouts_map = self.get_stockouts_map(self.config['enddate'],
                                                   location)

        for sql_location in sql_locations.exclude(is_archived=True):
            facilities = get_facilities(sql_location, self.config['domain'])
            facilities_count = facilities.count()

            soh_late, soh_not_responding, soh_on_time = self.get_soh_data(
                sql_location, facilities_count)
            if not is_mohsw:
                percent_stockouts = self.get_stockouts(facilities)
            else:
                if facilities_count > 0:
                    stockouts = stockouts_map.get(sql_location.location_id, 0)
                    percent_stockouts = stockouts * 100 / float(
                        facilities_count)
                else:
                    percent_stockouts = 0

            row_data = self._format_row(percent_stockouts, soh_late,
                                        soh_not_responding, soh_on_time,
                                        sql_location)
            for product_id in products_ids:
                product_availability = product_availabilities.get(
                    (sql_location.location_id, product_id))
                if product_availability and product_availability[1] != 0:
                    row_data.append(
                        format_percent(product_availability[0] * 100 /
                                       float(product_availability[1])))
                else:
                    row_data.append("<span class='no_data'>No Data</span>")
            rows.append(row_data)
        return rows
Example #5
0
 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
Example #6
0
 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
Example #7
0
    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
Example #8
0
    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
Example #9
0
    def rows(self):
        rows = []
        products_ids = self.get_products_ids()

        if not self.config['location_id']:
            return rows

        location = SQLLocation.objects.get(location_id=self.config['location_id'])
        sql_locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id'])
        is_mohsw = False
        stockouts_map = {}
        product_availabilities = {
            (pa['location_id'], pa['product']): (pa['without_stock'], pa['total'])
            for pa in ProductAvailabilityData.objects.filter(
                location_id__in=list(sql_locations.values_list('location_id', flat=True)),
                date__range=(self.config['startdate'], self.config['enddate'])
            ).values('location_id', 'product').annotate(without_stock=Avg('without_stock'), total=Max('total'))
        }
        if location.location_type.name == 'MOHSW':
            is_mohsw = True
            stockouts_map = self.get_stockouts_map(self.config['enddate'], location)

        for sql_location in sql_locations.exclude(is_archived=True):
            facilities = get_facilities(sql_location, self.config['domain'])
            facilities_count = facilities.count()

            soh_late, soh_not_responding, soh_on_time = self.get_soh_data(sql_location, facilities_count)
            if not is_mohsw:
                percent_stockouts = self.get_stockouts(facilities)
            else:
                if facilities_count > 0:
                    stockouts = stockouts_map.get(sql_location.location_id, 0)
                    percent_stockouts = stockouts * 100 / float(facilities_count)
                else:
                    percent_stockouts = 0

            row_data = self._format_row(
                percent_stockouts, soh_late, soh_not_responding, soh_on_time, sql_location
            )
            for product_id in products_ids:
                product_availability = product_availabilities.get((sql_location.location_id, product_id))
                if product_availability:
                    row_data.append(
                        format_percent(
                            product_availability[0] * 100 / float(product_availability[1])
                        )
                    )
                else:
                    row_data.append("<span class='no_data'>No Data</span>")
            rows.append(row_data)
        return rows
Example #10
0
    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