def reporting_rates(self):
        complete = 0
        incomplete = 0
        all_locations_count = self.location.get_descendants().filter(
            location_type__administrative=False, is_archived=False
        ).count()
        transactions = self.get_stock_transactions().values_list('case_id', 'product_id', 'report__date')
        grouped_by_case = {}
        parent_sum_rates = {}
        locations_ids = set()
        supply_points = list(self.get_supply_points(
            self.report_config['location_id']
        ))
        report_status = {
            supply_point.supply_point_id: {
                'status': 'non_reporting',
                'name': supply_point.name,
                'location_id': supply_point.location_id,
                'date': None
            }
            for supply_point in supply_points
        }
        aggregate_type = None
        if self.location.location_type.name == 'country':
            aggregate_type = 'region'
        elif self.location.location_type.name == 'region':
            aggregate_type = 'district'
        if aggregate_type:
            for location in self.location.get_children().filter(
                location_type__administrative=True, is_archived=False
            ):
                parent_sum_rates[location.name] = {
                    'complete': 0,
                    'incomplete': 0,
                    'location_id': location.location_id,
                    'all': location.get_descendants().filter(
                        location_type__administrative=False, is_archived=False
                    ).count()
                }

        for (case_id, product_id, date) in transactions:
            if case_id in report_status:
                report_status[case_id]['date'] = date

            if case_id in grouped_by_case:
                grouped_by_case[case_id].add(product_id)
            else:
                grouped_by_case[case_id] = {product_id}

        for case_id, products in grouped_by_case.iteritems():
            location = SQLLocation.objects.get(
                supply_point_id=case_id
            )
            locations_ids.add(location.location_id)

            aggregate_location = None

            if aggregate_type:
                aggregate_location = location.get_ancestors(ascending=True).filter(
                    location_type__name=aggregate_type
                )

            if aggregate_location:
                aggregate_location = aggregate_location[0]

            if not (set(location.products.values_list('product_id', flat=True)) - products):
                complete += 1
                if case_id in report_status:
                    report_status[case_id]['status'] = 'complete'
                if aggregate_type and aggregate_location:
                    parent_sum_rates[aggregate_location.name]['complete'] += 1
            else:
                incomplete += 1
                if case_id in report_status:
                    report_status[case_id]['status'] = 'incomplete'
                if aggregate_location:
                    parent_sum_rates[aggregate_location.name]['incomplete'] += 1

        return {
            'all': all_locations_count,
            'complete': complete,
            'incomplete': incomplete,
            'reporting_supply_points': locations_ids,
            'summary_reporting_rates': parent_sum_rates,
            'non_reporting': all_locations_count - (complete + incomplete),
            'non_reporting_table': [
                [status['name'], status['location_id'], status['date'], key]
                for key, status in report_status.iteritems()
                if not status['date']
            ],
            'incomplete_table': [
                [status['name'], status['location_id'], status['date']]
                for key, status in report_status.iteritems()
                if status['status'] == 'incomplete'
            ]
        }
dataset/ETHPy150Open dimagi/commcare-hq/custom/ewsghana/reports/specific_reports/reporting_rates.py/ReportingRatesReport.reporting_rates/original