Skip to content

Commit

Permalink
Alan run away
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruikshanks committed Mar 9, 2023
1 parent 15665e2 commit a390d06
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,44 @@ 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,
licenceRef: licence.licenceRef,
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
Expand Down

0 comments on commit a390d06

Please sign in to comment.