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
Exemple #2
0
    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