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

Handle REPLACED charge v's with different inv. acc #179

Merged
merged 17 commits into from
Apr 13, 2023

Conversation

Cruikshanks
Copy link
Member

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

During the supplementary billing process as we iterate through the charge versions we do so in invoice account and then licence order. This is so we can use 'iterating to the next one' as a trigger to know when to persist the current billing invoice and billing licence records we're dealing with.

Because we have the invoice account and licence ID's during this process it made it simple to look at the last bill run and extract any matching debit billing transactions that might now need crediting.

But during test, a scenario came up that we're not managing. A charge version can be created that supersedes the previous one. It supersedes the previous because the start date of the new charge version is the same as the previous i.e. the previous charge version should be completely ignored.

This most often happens because the billing account was wrong. So, the new charge version will be linked to a different billing account. The problem is when the previous charge version has already been billed. We need to include a credit in the new bill run we're generating for it. But our 'prudent' inclusion of the invoice account ID in our query means we're overlooking the previous debit to the old billing invoice account.

The end result is 2 invoices to 2 billing accounts on different bill runs, for the same licence and abstraction period.

This change resolves the problem, though goodness knows how!

@Cruikshanks Cruikshanks added the bug Something isn't working label Mar 27, 2023
@Cruikshanks Cruikshanks self-assigned this Mar 27, 2023
https://eaflood.atlassian.net/browse/WATER-3950

During the supplementary billing process as we iterate through the charge versions we do so in invoice account then licence order. This is so we can use 'iterating to the next one' as a trigger to know when to persist the current billing invoice and billing licence records we're dealing with.

Because we have the invoice account and licence ID's during this process it made it simple to look at the last bill run and extract any matching debit billing transactions that might now need crediting.

But during test a scenario came up that we're not managing. A charge version can be created that supercedes the previous one. It supercedes the previous, because the the start date of the new charge version is the same as the previous i.e. the previous charge version should be completely ignored.

This most often happens because the billing account was wrong. So, the new charge version will be linked to a different billing account. The problem is when the previous charge version has already been billed. We need to include a credit in the new bill run we're generating for it. But our 'prudent' inclusion of the invoice account ID in our query means we're overlooking the previous debit to the old billing invoice account.

The end result is 2 invoices to 2 billing accounts on different bill runs, for the same licence and abstraction period.

This change resolves the problem, though goodness knows how!
Modelled on `FetchChargeVersionsService` this returns those charge versions that display as REPLACED in the UI (superseded) in the DB.

We don't return the same amount of data and linked records. Unlike the `FetchChargeVersionsService`, the results we return won't be used as the basis for calculating the transaction lines. Instead, they'll be used to identify existing billing transaction lines.
@Cruikshanks Cruikshanks force-pushed the fetch-all-debits-from-previous branch from 168f23f to 8dda923 Compare April 10, 2023 07:47
Just to make it distinct and consistent with `FetchReplacedChargeVersionsService`.
We now need to fetch and reverse previous billing transactions in 2 places. We were already doing this work in `ProcessBillingTransactionsService`. So, we move that work into a new service as a first step. Then we'll refactor `ProcessBillingTransactionsService` to use our new service.
We had dropped data we thought we didn't need. But in testing we found that the CreateTransactionPresenter for the charging module was not getting the properties is was expecting.
In testing we hit on a scenario where the `FetchReplacedChargeVersionsService` might pull through charge versions with invoice account ID's that have already been processed as part of the main `ProcessBillingBatchService`.

When this happened an error was thrown because it tried to insert into `billing_invoices` a record with the same invoice account ID and billing batch ID as already exists.

If a replaced charge version is linked to an invoice account that is also linked to an approved invoice account it will already have been processed.
Alongside the other services we have added, this change provides a working solution to the replaced charge versions problem.

I have tested multiple replacements of a charge version where the billing account has been changed, within a single licence, and for partial parts of the year.

It seems to be working!

This started as an exercise in getting the sudo-code PR into something that more closely resembled what is needed. But it's ended up with a working solution.

There is probably some more refactoring we can do to improve things. We could certainly in more documentation and missing unit tests.

But hopefully what is here will provide a base on which to make those changes.
@Cruikshanks
Copy link
Member Author

@Jozzey @StuAA78 I meant to just push the notes we had but instead started to have a play 😁. With my changes, this is the kind of scenario I have seen it working locally for

Screenshot 2023-04-10 at 17 20 26

It was correctly adding the credits which it wasn't doing before. There is still work to be done to complete the PR. But hopefully a working PR makes more sense than some rough notes 🤷

@Cruikshanks Cruikshanks changed the title Fetch all debit trans. from previous bill runs Handle REPLACED charge v's with different inv. acc Apr 10, 2023
Jozzey and others added 9 commits April 11, 2023 13:55
We decided that it made more sense for `ProcessReplacedChargeVersionsService` to return `true` if any transactions were generated and `false` if they weren't -- this avoids a double negative situation in `ProcessBillingBatchService` where we were essentially saying "if nothing was not generated then...". We therefore flip the service's return value.
@StuAA78 StuAA78 marked this pull request as ready for review April 13, 2023 14:22
@StuAA78 StuAA78 requested review from Jozzey and StuAA78 April 13, 2023 14:22
@StuAA78 StuAA78 merged commit bf46224 into main Apr 13, 2023
@StuAA78 StuAA78 deleted the fetch-all-debits-from-previous branch April 13, 2023 14:24
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