Skip to content

Commit

Permalink
Add new view bill run presenter (#541)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4223

This presenter has been added to format the bill run data used in a new bill run page that will replace the existing one in the legacy service.

The page displays top-level details about the bill run (date created type, region etc) and then a summary for each bill linked to the bill run. This presenter handles the top-level information returned by `FetchBillRunService`.
  • Loading branch information
Cruikshanks authored Nov 26, 2023
1 parent cb726d6 commit 30a225f
Show file tree
Hide file tree
Showing 2 changed files with 441 additions and 0 deletions.
135 changes: 135 additions & 0 deletions app/presenters/bill-runs/view-bill-run.presenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
'use strict'

/**
* Formats bill run data ready for presenting in the view bill run page
* @module ViewBillRunPresenter
*/

const {
capitalize,
formatLongDate,
formatMoney
} = require('../base.presenter.js')

function go (billRun) {
const {
batchType,
billingBatchId,
billRunNumber,
createdAt,
creditNoteCount,
creditNoteValue,
invoiceCount,
invoiceValue,
isSummer,
netTotal,
region,
scheme,
status,
transactionFileReference,
toFinancialYearEnding
} = billRun

const billRunType = _billRunType(batchType, isSummer, scheme)
const regionName = capitalize(region.displayName)

return {
billsCount: _billsCount(creditNoteCount, invoiceCount, billRunType),
billRunId: billingBatchId,
billRunNumber,
billRunStatus: status,
billRunTotal: _billRunTotal(netTotal),
billRunType,
chargeScheme: _chargeScheme(scheme),
creditsCount: _creditsCount(creditNoteCount),
creditsTotal: formatMoney(creditNoteValue),
dateCreated: formatLongDate(createdAt),
debitsCount: _debitsCount(invoiceCount),
debitsTotal: formatMoney(invoiceValue),
displayCreditDebitTotals: _displayCreditDebitTotals(billRun),
financialYear: _financialYear(toFinancialYearEnding),
pageTitle: _pageTitle(regionName, billRunType),
region: regionName,
transactionFile: transactionFileReference
}
}

function _billsCount (creditsCount, debitsCount, billRunType) {
const total = creditsCount + debitsCount

if (total === 1) {
return `1 ${billRunType} bill`
}

return `${total} ${billRunType} bills`
}

function _billRunTotal (valueInPence) {
const valueAsMoney = formatMoney(valueInPence)

if (valueInPence < 0) {
return `${valueAsMoney} credit`
}

return valueAsMoney
}

function _billRunType (batchType, isSummer, scheme) {
if (batchType !== 'two_part_tariff') {
return capitalize(batchType)
}

if (scheme === 'sroc') {
return 'Two-part tariff'
}

if (isSummer) {
return 'Two-part tariff summer'
}

return 'Two-part tariff winter and all year'
}

function _chargeScheme (scheme) {
if (scheme === 'sroc') {
return 'Current'
}

return 'Old'
}

function _creditsCount (count) {
if (count === 1) {
return '1 credit note'
}

return `${count} credit notes`
}

function _debitsCount (count) {
if (count === 1) {
return '1 invoice'
}

return `${count} invoices`
}

function _displayCreditDebitTotals (billRun) {
const { batchType } = billRun

return batchType === 'supplementary'
}

function _financialYear (financialYearEnding) {
return `${financialYearEnding - 1} to ${financialYearEnding}`
}

function _pageTitle (regionName, billRunType) {
const lowercaseBillRunType = billRunType.toLowerCase()

return `${regionName} ${lowercaseBillRunType}`
}

module.exports = {
go
}
Loading

0 comments on commit 30a225f

Please sign in to comment.