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

New billing batch record for SROC supplementary bill run #37

Merged
merged 21 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
31 changes: 31 additions & 0 deletions app/models/billing-batch.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* Model for water.billing_batches
* @module BillingBatchModel
*/

const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class BillingBatchModel extends BaseModel {
static get tableName () {
return 'water.billing_batches'
}

static get relationMappings () {
return {
region: {
relation: Model.BelongsToOneRelation,
modelClass: 'region.model',
join: {
from: 'water.billing_batches.region_id',
to: 'water.regions.region_id'
}
}
}
}
}

module.exports = BillingBatchModel
8 changes: 8 additions & 0 deletions app/models/region.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class RegionModel extends BaseModel {
from: 'water.regions.region_id',
to: 'water.licences.region_id'
}
},
billingBatches: {
relation: Model.HasManyRelation,
modelClass: 'billing_batch.model',
join: {
from: 'water.regions.region_id',
to: 'water.billing_batches.region_id'
}
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions app/services/supplementary-billing/create-billing-batch.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

/**
* Creates a billing batch based on the regionId & billing period provided
* @module CreateBillingBatchService
*/

const BillingBatchModel = require('../../models/billing-batch.model.js')

class CreateBillingBatchService {
/**
* Create a new billing batch
*
* @param {Object} [regionId] The region_id for the selected region
* @param {Object} [billingPeriod] The billing period in the format { startDate: 2022-04-01, endDate: 2023-03-31 }
*
* @returns {Object} The newly created billing batch record
*/
static async go (regionId, billingPeriod) {
const billingBatch = await BillingBatchModel.query()
.insert({
region_id: regionId,
batch_type: 'supplementary',
from_financial_year_ending: billingPeriod.endDate.getFullYear(),
to_financial_year_ending: billingPeriod.endDate.getFullYear(),
status: 'processing',
scheme: 'sroc'
})
.returning('*')

return billingBatch
}
}

module.exports = CreateBillingBatchService
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class FetchChargeVersionsService {
'lic.include_in_supplementary_billing': 'yes',
'lic.region_id': regionId
})
.andWhere('start_date', '>=', billingPeriod.startDate)
.andWhere('start_date', '<=', billingPeriod.endDate)
.andWhere('chv.start_date', '>=', billingPeriod.startDate)
.andWhere('chv.start_date', '<=', billingPeriod.endDate)

return chargeVersions
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FetchLicencesService {
* This is a temporary service to help us confirm we are selecting the correct data to use when creating a
* supplementary bill run. Its primary aim is to meet the acceptance criteria defined in WATER-3787.
*
* @param {Objecy} region Instance of `RegionModel` for the selected region
* @param {Object} region Instance of `RegionModel` for the selected region
*
* @returns {Object[]} Array of matching `LicenceModel`
*/
Expand Down
47 changes: 47 additions & 0 deletions db/migrations/20221125164859_create_billing_batches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

const tableName = 'billing_batches'

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

// Data
table.uuid('region_id')
table.string('batch_type')
table.integer('from_financial_year_ending')
table.integer('to_financial_year_ending')
table.string('status')
table.integer('invoice_count')
table.integer('credit_note_count')
table.integer('net_total')
table.integer('bill_run_number')
table.string('source')
table.decimal('invoice_value')
table.decimal('credit_note_value')
table.string('transaction_file_reference')
table.string('scheme')

// Automatic timestamps
table.timestamps(false, true)
})

await knex.raw(`
CREATE TRIGGER update_timestamp
BEFORE UPDATE
ON water.${tableName}
FOR EACH ROW
EXECUTE PROCEDURE update_timestamp();
`)
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
30 changes: 30 additions & 0 deletions test/models/billing-batch.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const BillingBatch = require('../../app/models/billing-batch.model.js')

describe('Billing Batch model', () => {
it('can successfully run a query', async () => {
const query = await BillingBatch.query()

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

describe('Relationships', () => {
describe('when linking to region', () => {
it('can successfully run a query', async () => {
const query = await BillingBatch.query()
.innerJoinRelated('region')

expect(query).to.exist()
})
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const DatabaseHelper = require('../../support/helpers/database.helper.js')

// Thing under test
const CreateBillingBatchService = require('../../../app/services/supplementary-billing/create-billing-batch.service.js')

describe('Create Billing Batch service', () => {
const regionId = '6ac6cd43-af79-492a-9b82-15a70411c906'
const billingPeriod = { startDate: new Date('2022-04-01'), endDate: new Date('2023-03-31') }

beforeEach(async () => {
await DatabaseHelper.clean()
})

it('creates a billing batch record', async () => {
const result = await CreateBillingBatchService.go(regionId, billingPeriod)

expect(result.regionId).to.equal(regionId)
expect(result.fromFinancialYearEnding).to.equal(2023)
expect(result.toFinancialYearEnding).to.equal(2023)
})
})
61 changes: 61 additions & 0 deletions test/support/helpers/billing-batch.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

/**
* @module BillingBatchHelper
*/

const { db } = require('../../../db/db.js')

class BillingBatchHelper {
/**
* Add a new billing batch
*
* If no `data` is provided, default values will be used. These are
*
* - `region_id` - bd114474-790f-4470-8ba4-7b0cc9c225d7
* - `batch_type` - supplementary
* - `from_financial_year_ending` - 2023
* - `to_financial_year_ending` - 2023
* - `status` - processing
* - `scheme` - sroc
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
* @returns {string} The ID of the newly created record
*/
static async add (data = {}) {
const insertData = this.defaults(data)

const result = await db.table('water.billing_batches')
.insert(insertData)
.returning('billing_batch_id')

return result
}

/**
* Returns the defaults used when creating a new billing batch
*
* It will override or append to them any data provided. Mainly used by the `add()` method, we make it available
* for use in tests to avoid having to duplicate values.
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*/
static defaults (data = {}) {
const defaults = {
region_id: 'bd114474-790f-4470-8ba4-7b0cc9c225d7',
batch_type: 'supplementary',
from_financial_year_ending: 2023,
to_financial_year_ending: 2023,
status: 'processing',
scheme: 'sroc'
}

return {
...defaults,
...data
}
}
}

module.exports = BillingBatchHelper