示例#1
0
    def _execute(self):
        config: SmdPartialBillingScraperConfiguration = self._configuration
        meter = config.meter

        usage_points = relevant_usage_points(meter)
        log.info("Identified %s relevant usage point(s): %s",
                 len(usage_points), usage_points)
        query = db.session.query(SmdBill).filter(
            SmdBill.usage_point.in_(usage_points))

        if self.start_date:
            start = self.start_date
            end = max(start, self.end_date or date.today())
            if end - self.start_date <= timedelta(days=60):
                start = start - timedelta(days=60)
                log.info("Adjusting start date to %s.", start)
            query = query.filter(start <= SmdBill.start)

        if self.end_date:
            query = query.filter(SmdBill.start <= self.end_date)

        query = query.order_by(SmdBill.published)

        log.info("Identified %d raw SMD bills relevant to this meter.",
                 query.count())
        # It often happens that we receive several versions of the same bill across multiple files.
        # The first thing we need to do is order the bills by publication date, so we can decide
        # which SmdBill record is the correct one for our chosen date.
        unified_bills: List[SmdBill] = SmdBill.unify_bills(query)
        adjusted_bills: List[SmdBill] = SmdBill.adjust_single_day_bills(
            unified_bills)
        partial_bills = [
            b.to_billing_datum(self.service) for b in adjusted_bills
        ]

        if partial_bills:
            log.debug(
                "Identified %s partial bills in Share My Data for meter %s (%s).",
                len(partial_bills),
                meter.name,
                meter.oid,
            )
            if datafeeds_config.enabled("S3_BILL_UPLOAD"):
                partial_bills = self.attach_corresponding_urja_pdfs(
                    partial_bills)

        return Results(tnd_bills=partial_bills)
示例#2
0
    def test_one_day_bills(self):
        """
        Test that one-day bills from SMD are adjusted by adding one day to the end date,
        and adjusting the subsequent bill if necessary.
        """
        raw_bills = from_fixture(FIXTURE_03)
        actual = SmdBill.unify_bills(raw_bills)
        adjusted = SmdBill.adjust_single_day_bills(actual)
        self.assertEqual(len(adjusted), 3)
        self.assertEqual(adjusted[0].initial, date(2018, 4, 18))
        self.assertEqual(adjusted[0].closing, date(2018, 4, 19),
                         "Was previously 4/18/18")

        self.assertEqual(
            adjusted[1].initial,
            date(2018, 4, 20),
            "Previously 4/19/18, had to be adjusted w/ 4/18 bill.",
        )
        self.assertEqual(adjusted[1].closing, date(2018, 5, 17))

        self.assertEqual(adjusted[2].initial, date(2018, 5, 18))
        self.assertEqual(adjusted[2].closing, date(2018, 6, 18))