diff --git a/app/services/supplementary-billing/generate-billing-invoice-licence.service.js b/app/services/supplementary-billing/generate-billing-invoice-licence.service.js new file mode 100644 index 0000000000..f4476fe8ee --- /dev/null +++ b/app/services/supplementary-billing/generate-billing-invoice-licence.service.js @@ -0,0 +1,50 @@ +'use strict' + +/** + * Generates a billing invoice licence record ready for persisting + * @module CreateBillingInvoiceLicenceService + */ + +const { randomUUID } = require('crypto') + +/** + * Create a billing invoice licence record for the provided billing invoice and licence + * + * @param {module:BillingInvoiceModel} billingInvoice An instance of `BillingInvoiceModel` + * @param {module:licenceModel} licence An instance of `LicenceModel` + * + * @returns {Object} The newly-created billing invoice licence record + */ +async function go (generatedBillingInvoiceLicences, billingInvoiceId, licence) { + let billingInvoiceLicence = _existing(generatedBillingInvoiceLicences, billingInvoiceId) + + if (billingInvoiceLicence) { + return { + billingInvoiceLicence, + billingInvoiceLicences: generatedBillingInvoiceLicences + } + } + + billingInvoiceLicence = { + billingInvoiceId, + billingInvoiceLicenceId: randomUUID({ disableEntropyCache: true }), + licenceRef: licence.licenceRef, + licenceId: licence.licenceId + } + const updatedBillingInvoiceLicences = [...generatedBillingInvoiceLicences, billingInvoiceLicence] + + return { + billingInvoiceLicence, + billingInvoiceLicences: updatedBillingInvoiceLicences + } +} + +function _existing (generatedBillingInvoiceLicences, billingInvoiceId) { + return generatedBillingInvoiceLicences.filter((invoiceLicence) => { + return billingInvoiceId === invoiceLicence.billingInvoiceId + })[0] +} + +module.exports = { + go +} diff --git a/app/services/supplementary-billing/generate-billing-invoice.service.js b/app/services/supplementary-billing/generate-billing-invoice.service.js index c8626b5fc7..25a0cab7f3 100644 --- a/app/services/supplementary-billing/generate-billing-invoice.service.js +++ b/app/services/supplementary-billing/generate-billing-invoice.service.js @@ -10,11 +10,14 @@ const { randomUUID } = require('crypto') const InvoiceAccountModel = require('../../models/crm-v2/invoice-account.model.js') -async function go (invoiceAccountId, billingBatchId, financialYearEnding, generatedBillingInvoices = []) { - let billingInvoice = _existingBillingInvoice(invoiceAccountId, generatedBillingInvoices) +async function go (generatedBillingInvoices, invoiceAccountId, billingBatchId, financialYearEnding) { + let billingInvoice = _existing(generatedBillingInvoices, invoiceAccountId) if (billingInvoice) { - return billingInvoice + return { + billingInvoice, + billingInvoices: generatedBillingInvoices + } } const invoiceAccount = await InvoiceAccountModel.query().findById(invoiceAccountId) @@ -28,15 +31,18 @@ async function go (invoiceAccountId, billingBatchId, financialYearEnding, genera financialYearEnding, isCredit: false } - generatedBillingInvoices.push(billingInvoice) + const updatedBillingInvoices = [...generatedBillingInvoices, billingInvoice] - return billingInvoice + return { + billingInvoice, + billingInvoices: updatedBillingInvoices + } } -function _existingBillingInvoice (invoiceAccountId, generatedBillingInvoices) { +function _existing (generatedBillingInvoices, invoiceAccountId) { return generatedBillingInvoices.filter((invoice) => { return invoiceAccountId === invoice.invoiceAccountId - }) + })[0] } module.exports = { diff --git a/app/services/supplementary-billing/process-billing-batch.service.js b/app/services/supplementary-billing/process-billing-batch.service.js index 27959d7eb3..8b51869277 100644 --- a/app/services/supplementary-billing/process-billing-batch.service.js +++ b/app/services/supplementary-billing/process-billing-batch.service.js @@ -39,16 +39,18 @@ async function go (billingBatch, billingPeriod) { // This is why we are only passing through the first billing period; we know there is only one! const chargeVersions = await FetchChargeVersionsService.go(billingBatch.regionId, billingPeriod) - const generatedBillingInvoices = [] + let generatedBillingInvoices = [] for (const chargeVersion of chargeVersions) { - const { chargeElements, licence } = chargeVersion + // const { chargeElements, licence } = chargeVersion - const billingInvoice = await GenerateBillingInvoiceService.go( + const result = await GenerateBillingInvoiceService.go( + generatedBillingInvoices, chargeVersion.invoiceAccountId, billingBatchId, - financialYearEnding, - generatedBillingInvoices + financialYearEnding ) + generatedBillingInvoices = result.billingInvoices + const billingInvoiceLicence = { billingInvoiceLicenceId: randomUUID({ disableEntropyCache: true }), billingInvoiceId: billingInvoice.billingInvoiceId, @@ -56,22 +58,25 @@ async function go (billingBatch, billingPeriod) { licenceId: licence.licenceId } + let transactionLines = [] if (chargeVersion.chargeElements) { - const transactionLines = _generateTransactionLines(billingPeriod, chargeVersion) + transactionLines = _generateTransactionLines(billingPeriod, chargeVersion) if (transactionLines.length > 0) { await _createTransactionLines( transactionLines, billingPeriod, - billingInvoice.invoiceAccountNumber, + result.billingInvoice.invoiceAccountNumber, billingInvoiceLicence.billingInvoiceLicenceId, chargeVersion, billingBatch.externalId ) } } + billingInvoiceLicence.transactionLines = transactionLines } + await ChargingModuleGenerateService.go(billingBatch.externalId) // NOTE: Retaining this as a candidate for updating the bill run status if the process errors or the bill run is empty