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

Determine if licence is 'billed' #82

Merged
merged 22 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a7b6f96
Determine if charge version is 'billed'
Jozzey Jan 10, 2023
9484699
created migrations
Jozzey Jan 11, 2023
5eeb9e5
created new models and updated existing
Jozzey Jan 12, 2023
4b92e92
built test helpers for new models
Jozzey Jan 12, 2023
738f49c
updated tests/models/helpers with new relationships
Jozzey Jan 13, 2023
c44f43c
removed .only
Jozzey Jan 13, 2023
24db45c
Merge branch 'main' into determine-if-charge-version-is-billed
Jozzey Jan 13, 2023
cbe6943
Merge branch 'main' into determine-if-charge-version-is-billed
Jozzey Jan 13, 2023
9529028
created tests for billing invoice model
Jozzey Jan 13, 2023
b1e3007
created billing invoice licence tests
Jozzey Jan 13, 2023
dc5e9ea
Merge branch 'main' into determine-if-charge-version-is-billed
Jozzey Jan 16, 2023
7e1f24a
add date created translations to new models
Jozzey Jan 16, 2023
2bff260
Merge branch 'main' into determine-if-charge-version-is-billed
Jozzey Jan 16, 2023
f8e31a2
added flag to licence to indicate if included in billing
Jozzey Jan 16, 2023
a640311
fixed tests and added test for new flag
Jozzey Jan 16, 2023
c920fc1
teeny tiny little tweak
Jozzey Jan 16, 2023
d8ee0fb
Merge branch 'main' into determine-if-charge-version-is-billed
Jozzey Jan 17, 2023
cafa128
updated tests and added some new ones
Jozzey Jan 17, 2023
552b608
removed .only DOH!
Jozzey Jan 17, 2023
e61fa11
updated test
Jozzey Jan 17, 2023
e0f0b45
Merge branch 'main' into determine-if-charge-version-is-billed
Jozzey Jan 18, 2023
b331493
updated as per Alan's PR comment
Jozzey Jan 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/models/water/billing-batch.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class BillingBatchModel extends WaterBaseModel {
from: 'billingBatches.regionId',
to: 'regions.regionId'
}
},
billingInvoices: {
relation: Model.HasManyRelation,
modelClass: 'billing-invoice.model',
join: {
from: 'billingBatches.billingBatchId',
to: 'billingInvoices.billingBatchId'
}
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions app/models/water/billing-invoice-licence.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

/**
* Model for billingInvoiceLicences
* @module BillingInvoiceLicenceModel
*/

const { Model } = require('objection')

const WaterBaseModel = require('./water-base.model.js')

class BillingInvoiceLicenceModel extends WaterBaseModel {
static get tableName () {
return 'billingInvoiceLicences'
}

static get idColumn () {
return 'billingInvoiceLicenceId'
}

static get translations () {
return [
{ database: 'dateCreated', model: 'createdAt' },
{ database: 'dateUpdated', model: 'updatedAt' }
]
}

static get relationMappings () {
return {
billingInvoice: {
relation: Model.BelongsToOneRelation,
modelClass: 'billing-invoice.model',
join: {
from: 'billingInvoiceLicences.billingInvoiceId',
to: 'billingInvoices.billingInvoiceId'
}
},
licence: {
relation: Model.BelongsToOneRelation,
modelClass: 'licence.model',
join: {
from: 'billingInvoiceLicences.licenceId',
to: 'licences.licenceId'
}
}
}
}
}

module.exports = BillingInvoiceLicenceModel
50 changes: 50 additions & 0 deletions app/models/water/billing-invoice.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

/**
* Model for billingInvoices
* @module BillingInvoiceModel
*/

const { Model } = require('objection')

const WaterBaseModel = require('./water-base.model.js')

class BillingInvoiceModel extends WaterBaseModel {
static get tableName () {
return 'billingInvoices'
}

static get idColumn () {
return 'billingInvoiceId'
}

static get translations () {
return [
{ database: 'dateCreated', model: 'createdAt' },
{ database: 'dateUpdated', model: 'updatedAt' }
]
}

static get relationMappings () {
return {
billingBatch: {
relation: Model.BelongsToOneRelation,
modelClass: 'billing-batch.model',
join: {
from: 'billingInvoices.billingBatchId',
to: 'billingBatches.billingBatchId'
}
},
billingInvoiceLicences: {
relation: Model.HasManyRelation,
modelClass: 'billing-invoice-licence.model',
join: {
from: 'billingInvoices.billingInvoiceId',
to: 'billingInvoiceLicences.billingInvoiceId'
}
}
}
}
}

module.exports = BillingInvoiceModel
8 changes: 8 additions & 0 deletions app/models/water/licence.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class LicenceModel extends WaterBaseModel {
from: 'licences.regionId',
to: 'regions.regionId'
}
},
billingInvoiceLicences: {
relation: Model.HasManyRelation,
modelClass: 'billing-invoice-licence.model',
join: {
from: 'licences.licenceId',
to: 'billingInvoiceLicences.licenceId'
}
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion app/presenters/check/supplementary-data.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

function go (data) {
const licences = data.licences.map((licence) => {
const licenceExistsInBilling = _licenceExistsInBilling(licence.billingInvoiceLicences)

return {
licenceId: licence.licenceId,
licenceRef: licence.licenceRef
licenceRef: licence.licenceRef,
licenceExistsInBilling
}
})
const chargeVersions = data.chargeVersions
Expand All @@ -21,6 +24,18 @@ function go (data) {
}
}

function _licenceExistsInBilling (billingInvoiceLicences) {
if (billingInvoiceLicences) {
for (let i = 0; i < billingInvoiceLicences.length; i++) {
if (billingInvoiceLicences[i].billingInvoice) {
return true
}
}
Jozzey marked this conversation as resolved.
Show resolved Hide resolved
}

return false
}

module.exports = {
go
}
3 changes: 2 additions & 1 deletion app/services/check/supplementary-data.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const SupplementaryDataPresenter = require('../../presenters/check/supplementary
async function go (naldRegionId) {
const region = await FetchRegionService.go(naldRegionId)
const billingPeriods = BillingPeriodService.go()
const licences = await FetchLicencesService.go(region)
const billingPeriodFinancialYearEnding = billingPeriods[0].endDate.getFullYear()
const licences = await FetchLicencesService.go(region, billingPeriodFinancialYearEnding)

// We know in the future we will be calculating multiple billing periods and so will have to iterate through each,
// generating bill runs and reviewing if there is anything to bill. For now, whilst our knowledge of the process
Expand Down
18 changes: 15 additions & 3 deletions app/services/supplementary-billing/fetch-licences.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,31 @@ const LicenceModel = require('../../models/water/licence.model.js')
*
* @returns {Object[]} Array of matching `LicenceModel`
*/
async function go (region) {
const licences = await _fetch(region)
async function go (region, billingPeriodFinancialYearEnding) {
const licences = await _fetch(region, billingPeriodFinancialYearEnding)

return licences
}

async function _fetch (region) {
async function _fetch (region, billingPeriodFinancialYearEnding) {
const result = await LicenceModel.query()
.distinctOn('licenceId')
.innerJoinRelated('chargeVersions')
.where('regionId', region.regionId)
.where('includeInSupplementaryBilling', 'yes')
.where('chargeVersions.scheme', 'sroc')
.withGraphFetched('billingInvoiceLicences.billingInvoice')
.modifyGraph('billingInvoiceLicences', builder => {
builder.select(
'billingInvoiceLicenceId'
)
})
.modifyGraph('billingInvoiceLicences.billingInvoice', builder => {
builder.select(
'financialYearEnding'
)
.where('financialYearEnding', billingPeriodFinancialYearEnding)
})

return result
}
Expand Down
28 changes: 28 additions & 0 deletions db/migrations/20230111160155_create-water-billing-invoices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const tableName = 'billing_invoices'

exports.up = async function (knex) {
await knex
.schema
.withSchema('water')
.createTable(tableName, table => {
// Primary Key
table.uuid('billing_invoice_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.uuid('billing_batch_id')
table.integer('financial_year_ending')

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now())
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const tableName = 'billing_invoice_licences'

exports.up = async function (knex) {
await knex
.schema
.withSchema('water')
.createTable(tableName, table => {
// Primary Key
table.uuid('billing_invoice_licence_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.uuid('billing_invoice_id')
table.string('licence_ref')
table.uuid('licence_id')

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now())
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
38 changes: 38 additions & 0 deletions test/models/water/billing-batch.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const { expect } = Code

// Test helpers
const BillingBatchHelper = require('../../support/helpers/water/billing-batch.helper.js')
const BillingInvoiceHelper = require('../../support/helpers/water/billing-invoice.helper.js')
const BillingInvoiceModel = require('../../../app/models/water/billing-invoice.model.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const RegionHelper = require('../../support/helpers/water/region.helper.js')
const RegionModel = require('../../../app/models/water/region.model.js')
Expand Down Expand Up @@ -64,5 +66,41 @@ describe('Billing Batch model', () => {
expect(result.region).to.equal(testRegion)
})
})

describe('when linking to billing invoices', () => {
let testBillingInvoices

beforeEach(async () => {
testRecord = await BillingBatchHelper.add()
const { billingBatchId } = testRecord

testBillingInvoices = []
for (let i = 0; i < 2; i++) {
const billingInvoice = await BillingInvoiceHelper.add({ financialYearEnding: 2023 }, { billingBatchId })
testBillingInvoices.push(billingInvoice)
}
})

it('can successfully run a related query', async () => {
const query = await BillingBatchModel.query()
.innerJoinRelated('billingInvoices')

expect(query).to.exist()
})

it('can eager load the billing invoices', async () => {
const result = await BillingBatchModel.query()
.findById(testRecord.billingBatchId)
.withGraphFetched('billingInvoices')

expect(result).to.be.instanceOf(BillingBatchModel)
expect(result.billingBatchId).to.equal(testRecord.billingBatchId)

expect(result.billingInvoices).to.be.an.array()
expect(result.billingInvoices[0]).to.be.an.instanceOf(BillingInvoiceModel)
expect(result.billingInvoices).to.include(testBillingInvoices[0])
expect(result.billingInvoices).to.include(testBillingInvoices[1])
})
})
})
})
Loading