Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix supp. billing not crediting old accounts #280

Merged
merged 6 commits into from
Jun 26, 2023

Conversation

Cruikshanks
Copy link
Member

https://eaflood.atlassian.net/browse/WATER-4044

Assume the following scenario. A charge version is added to a licence starting on 1 April 2022 against invoice account INVAC001. The licence is flagged for supplementary billing which is run during the 2023-2024 billing period.

This will will generate 2 bills for INVAC001; one in 2024 and one in 2023 (multi-year billing!)

Then another charge version is added starting on 1 June 2022 against invoice account INVAC002. Again the licence is flagged for supplementary billing which is run during the 2023-2024 billing period.

What should happen is

  • 2024
    • INVAC001 receives a whole year credit
    • INVAC002 receives a whole year debit
  • 2023
    • INVAC001 receives a part-year credit and debit
    • INVAC002 receives a part-year debit

So, 4 bills in total which result in a £0 bill run.

What is happening is no credit is generated for INVAC001 in 2024. The reason is FetchChargeVersionsService was written to exclude charge versions which start and finish outside the billing period being processed. But that exclusion means we never check them for previous transactions, which in this scenario would result in INVAC001's debit being found so a credit is generated.

This change resolves the issue by removing the clause in our query which excludes charge versions that end before the billing period start date. To support this we also need to amend the _periodIsInvalid() in DetermineChargePeriodService.

Note - removal of this clause means we might pick up charge versions that don't need to be processed at all. We did manage to implement an Objection.js query that used a UNION to prevent this. But it made FetchChargeVersionsService vastly more complex purely for a negligible performance bump.

https://eaflood.atlassian.net/browse/WATER-4044

Assume the following scenario. A charge version is added to a licence starting on 1 April 2022 against invoice account **INVAC001**. The licence is flagged for supplementary billing which is run during the 2023-2024 billing period.

This will will generate 2 bills for **INVAC001**; one in 2024 and one in 2023 ([multi-year billing!](#228))

Then another charge version is added starting on 1 June 2022 against invoice account **INVAC002**. Again the licence is flagged for supplementary billing which is run during the 2023-2024 billing period.

What should happen is

- 2024
  - **INVAC001** receives a whole year credit
  - **INVAC002** receives a whole year debit
- 2023
  - **INVAC001** receives a part year credit and debit
  - **INVAC002** receives a part year debit

So, 4 bills in total which result in a £0 bill run.

What is happening is no credit is generated for **INVAC001** in 2024. The reason is `FetchChargeVersionsService` was written to exclude charge versions which start and finish outside the billing period being processed. But that exclusion means we never check them for previous transactions, which in this scenario would result in **INVAC001's** debit being found so a credit generated.

This change resolves the issue by removing the clause in our query which excludes charge versions that end before the billing period start date. To support this we also need to amend the `_periodIsInvalid()` in `DetermineChargePeriodService`.

> Note - removal of this clause means we might pick up charge versions that don't need to be processed at all. We did manage to implement an Objection.js query that used a [UNION](https://www.w3schools.com/sql/sql_union.asp) to prevent this. But it made `FetchChargeVersionsService` vastly more complex purely for a negligible performance bump.
@Cruikshanks Cruikshanks self-assigned this Jun 23, 2023
We thought we were helping things by making the references in the query explicit. But we then started getting 'missing FROM clause' errors.

It is due to how Objection.js is generating the SQL which we could fix by adding yet more code. Better to just put things back how they were and leave alone!
We remove the test that checks charge versions with end dates before the period start date are not returned.

We enhance the existing tests that check the results to cover our new scenario.
@Cruikshanks Cruikshanks marked this pull request as ready for review June 24, 2023 09:16
@Cruikshanks Cruikshanks requested review from Jozzey and StuAA78 June 24, 2023 09:16
Copy link
Contributor

@Jozzey Jozzey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cruikshanks Cruikshanks merged commit d1a4869 into main Jun 26, 2023
@Cruikshanks Cruikshanks deleted the not-crediting-old-account-v2 branch June 26, 2023 10:44
Cruikshanks added a commit that referenced this pull request Jun 27, 2023
https://eaflood.atlassian.net/browse/WATER-4044

In [Fix supp. billing not crediting old accounts](#280) as sought to resolve a problem in multi-year billing that saw debits raised in a period not being credited.

Imagine a charge version that starts 2022-04-01 with no end date. The first bill run would generate a debit for both 2023-24 and 2022-23. Should we add a new charge version that starts 2022-06-01 the service will automatically add an end date to the first one (2022-05-31).

Our engine on the second run, when fetching the charge versions for 2023-24 would now skip the first charge version because it's outside the billing period. But by skipping it we overlook checking for any previous transactions to credit, which we need to do in this case.

We fixed it by simply removing the end date clause in `FetchChargeVersionsService`. Now to be included a charge version just needs to start before the billing period end date (2024-03-31).

We got lucky when we ran the fix against the scenario that identified this problem. A lot of complexity comes from working out just when a charge version is 'chargeable' and whether their abstraction period overlaps this. In the original scenario, all things aligned to calculate zero billable days.

Further testing by our QA found a scenario where the bill run always errored. Essentially, we were calculating a negative billable days and trying to send it to the charging module API, which was rejecting it.

As part of the original fix, we'd removed a check in our `DetermineChargePeriodService` that said the charge version's end date couldn't be before the billing period's start date. We had to as we were now fetching charge versions where this was the case.

In the new scenario there was a charge version for 2022-09-07 to 2022-11-30 which when processed in 2023-204 was determining a charge period of

- **start date** 2023-04-01
- **end date** 2022-11-30

End date before the start date was causing a problem further down the process hence us trying to send a negative billable days value to the charging module.

This change puts back the check we removed in `DetermineChargePeriodService`, and now use it to tell us if the charge version and billing period are incompatible. And if they are, instead of throwing an error we return a result that says, “Don't bother calculating this one”.

We skip unnecessary processing and avoid trying to send an invalid transaction to the Charging Module API.
Cruikshanks added a commit that referenced this pull request Jun 28, 2023
https://eaflood.atlassian.net/browse/WATER-4044

In [Fix supp. billing not crediting old accounts](#280) as sought to resolve a problem in multi-year billing that saw debits raised in a period not being credited.

Imagine a charge version that starts 2022-04-01 with no end date. The first bill run would generate a debit for both 2023-24 and 2022-23. Should we add a new charge version that starts 2022-06-01 the service will automatically add an end date to the first one (2022-05-31).

Our engine on the second run, when fetching the charge versions for 2023-24 would now skip the first charge version because it's outside the billing period. But by skipping it we overlook checking for any previous transactions to credit, which we need to do in this case.

We fixed it by simply removing the end date clause in `FetchChargeVersionsService`. Now to be included a charge version just needs to start before the billing period end date (2024-03-31).

We got lucky when we ran the fix against the scenario that identified this problem. A lot of complexity comes from working out just when a charge version is 'chargeable' and whether their abstraction period overlaps this. In the original scenario, all things aligned to calculate zero billable days.

Further testing by our QA found a scenario where the bill run always errored. Essentially, we were calculating negative billable days and trying to send it to the charging module API, which was rejecting it.

As part of the original fix, we'd removed a check in our `DetermineChargePeriodService` that said the charge version's end date couldn't be before the billing period's start date. We had to as we were now fetching charge versions where this was the case.

In the new scenario, there was a charge version for 2022-09-07 to 2022-11-30 which when processed in 2023-204 was determining a charge period of

- **start date** 2023-04-01
- **end date** 2022-11-30

End date before the start date was causing a problem further down the process hence us trying to send a negative billable days value to the charging module.

This change puts back the check we removed in `DetermineChargePeriodService`, and now use it to tell us if the charge version and billing period are incompatible. And if they are, instead of throwing an error we return a result that says, “Don't bother calculating this one”.

We skip unnecessary processing and avoid trying to send an invalid transaction to the Charging Module API.
@StuAA78 StuAA78 added the bug Something isn't working label Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants