Skip to content

Commit

Permalink
feat: implement EDITED flag on records
Browse files Browse the repository at this point in the history
This commit implements the EDITED flag on records of transactions that
have been modified via the posting journal edit tool.  This signals the
user that the transaction has been edited and the data might be out of
date.

Closes Third-Culture-Software#2047.
  • Loading branch information
jniles committed Nov 17, 2017
1 parent 77eb63f commit d95170f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 42 deletions.
18 changes: 10 additions & 8 deletions server/controllers/finance/cash.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function lookup(uuid) {
SELECT BUID(cash.uuid) as uuid, cash.project_id,
CONCAT_WS('.', '${CASH_KEY}', project.abbr, cash.reference) AS reference,
cash.date, BUID(cash.debtor_uuid) AS debtor_uuid, cash.currency_id, cash.amount,
cash.description, cash.cashbox_id, cash.is_caution, cash.user_id
cash.description, cash.cashbox_id, cash.is_caution, cash.user_id, cash.edited
FROM cash JOIN project ON cash.project_id = project.id
WHERE cash.uuid = ?;
`;
Expand Down Expand Up @@ -131,7 +131,8 @@ function find(options) {
CONCAT_WS('.', '${CASH_KEY}', project.abbr, cash.reference) AS reference,
cash.date, BUID(cash.debtor_uuid) AS debtor_uuid, cash.currency_id, cash.amount,
cash.description, cash.cashbox_id, cash.is_caution, cash.user_id, cash.reversed,
d.text AS debtor_name, cb.label AS cashbox_label, u.display_name, p.display_name AS patientName
d.text AS debtor_name, cb.label AS cashbox_label, u.display_name,
p.display_name AS patientName, cash.edited
FROM cash
JOIN project ON cash.project_id = project.id
JOIN debtor d ON d.uuid = cash.debtor_uuid
Expand All @@ -140,17 +141,18 @@ function find(options) {
JOIN user u ON u.id = cash.user_id
`;

filters.period('period', 'date');
filters.dateFrom('custom_period_start', 'date');
filters.dateTo('custom_period_end', 'date');
filters.fullText('description');
filters.equals('user_id');
filters.equals('is_caution');
filters.equals('cashbox_id');
filters.equals('debtor_uuid');
filters.equals('currency_id');
filters.equals('reversed');
filters.equals('debtor_group_uuid', 'group_uuid', 'd');
filters.equals('debtor_uuid');
filters.equals('edited');
filters.equals('is_caution');
filters.equals('reversed');
filters.equals('user_id');
filters.fullText('description');
filters.period('period', 'date');

const referenceStatement = `CONCAT_WS('.', '${CASH_KEY}', project.abbr, cash.reference) = ?`;
filters.custom('reference', referenceStatement);
Expand Down
16 changes: 15 additions & 1 deletion server/controllers/finance/journal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ const FilterParser = require('../../../lib/filter');
const NotFound = require('../../../lib/errors/NotFound');
const BadRequest = require('../../../lib/errors/BadRequest');

const identifiers = require('../../../config/identifiers');

const hrRecordToTableMap = {};
_.forEach(identifiers, v => {
hrRecordToTableMap[v.key] = hrRecordToTableMap[v.table];
});

// Fiscal Service
const FiscalService = require('../../finance/fiscal');

Expand Down Expand Up @@ -239,6 +246,7 @@ function editTransaction(req, res, next) {
const UPDATE_JOURNAL_ROW = 'UPDATE posting_journal SET ? WHERE uuid = ?;';
const INSERT_JOURNAL_ROW = 'INSERT INTO posting_journal SET ?;';
const UPDATE_TRANSACTION_HISTORY = 'INSERT INTO transaction_history SET ?;';
const UPDATE_RECORD_EDITED_FLAG = 'UPDATE ?? SET edited = 1 WHERE uuid = ?;'

const transaction = db.transaction();
const recordUuid = req.params.record_uuid;
Expand All @@ -249,6 +257,7 @@ function editTransaction(req, res, next) {

let _transactionToEdit;
let _fiscalYear;
let _recordTableToEdit;

rowsRemoved.forEach(row => transaction.addQuery(REMOVE_JOURNAL_ROW, [db.bid(row.uuid)]));

Expand All @@ -261,6 +270,9 @@ function editTransaction(req, res, next) {
// bind the current transaction under edit as "transactionToEdit"
_transactionToEdit = transactionToEdit;

// _recordTableToEdit is now either voucher, invoice, or cash
_recordTableToEdit = hrRecordToTableMap[transactionToEdit.hrRecord];

// check the source (posted vs. non-posted) of the first transaction row
if (posted) {
throw new BadRequest(
Expand Down Expand Up @@ -319,7 +331,9 @@ function editTransaction(req, res, next) {
user_id : req.session.user.id,
};

transaction.addQuery(UPDATE_TRANSACTION_HISTORY, [transactionHistory]);
transaction
.addQuery(UPDATE_RECORD_EDITED_FLAG, [_recordTableToEdit, db.bid(row.recordUuid)])
.addQuery(UPDATE_TRANSACTION_HISTORY, [transactionHistory]);

return transaction.execute();
})
Expand Down
25 changes: 14 additions & 11 deletions server/controllers/finance/patientInvoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
*/

const uuid = require('uuid/v4');
const identifiers = require('../../config/identifiers');
const db = require('../../lib/db');
const barcode = require('../../lib/barcode');
const FilterParser = require('../../lib/filter');

const BadRequest = require('../../lib/errors/BadRequest');
const createInvoice = require('./invoice/patientInvoice.create');
const Debtors = require('./debtors');
const FilterParser = require('../../lib/filter');
const barcode = require('../../lib/barcode');
const createInvoice = require('./invoice/patientInvoice.create');
const db = require('../../lib/db');
const identifiers = require('../../config/identifiers');

const shared = require('./shared');

Expand Down Expand Up @@ -211,7 +212,8 @@ function find(options) {
patient.display_name as patientName, invoice.cost, BUID(invoice.debtor_uuid) as debtor_uuid,
CONCAT_WS('.', '${identifiers.INVOICE.key}', project.abbr, invoice.reference) AS reference,
CONCAT_WS('.', '${identifiers.PATIENT.key}', project.abbr, patient.reference) AS patientReference,
service.name as serviceName, user.display_name, invoice.user_id, invoice.reversed
service.name as serviceName, user.display_name, invoice.user_id,
invoice.reversed, invoice.edited
FROM invoice
LEFT JOIN patient ON invoice.debtor_uuid = patient.debtor_uuid
JOIN debtor AS d ON invoice.debtor_uuid = d.uuid
Expand All @@ -220,14 +222,15 @@ function find(options) {
JOIN project ON project.id = invoice.project_id
`;

filters.equals('patientUuid', 'uuid', 'patient');
filters.equals('user_id');
filters.equals('cost');
filters.equals('debtor_group_uuid', 'group_uuid', 'd');
filters.equals('debtor_uuid');
filters.equals('edited');
filters.equals('patientUuid', 'uuid', 'patient');
filters.equals('project_id');
filters.equals('reversed');
filters.equals('cost');
filters.equals('service_id');
filters.equals('project_id');
filters.equals('debtor_group_uuid', 'group_uuid', 'd');
filters.equals('user_id');

filters.custom(
'cash_uuid',
Expand Down
1 change: 0 additions & 1 deletion server/controllers/finance/reports/invoices/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* @overview
* Invoice Reports
Expand Down
3 changes: 2 additions & 1 deletion server/controllers/finance/vouchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function find(options) {
BUID(v.uuid) as uuid, v.date, v.project_id, v.currency_id, v.amount,
v.description, v.user_id, v.type_id, u.display_name, transaction_type.text,
CONCAT_WS('.', '${entityIdentifier}', p.abbr, v.reference) AS reference,
BUID(v.reference_uuid) AS reference_uuid
v.edited, BUID(v.reference_uuid) AS reference_uuid
FROM voucher v
JOIN project p ON p.id = v.project_id
JOIN user u ON u.id = v.user_id
Expand All @@ -147,6 +147,7 @@ function find(options) {
filters.dateFrom('custom_period_start', 'date');
filters.dateTo('custom_period_end', 'date');
filters.equals('user_id');
filters.equals('edited');

filters.custom('reference', referenceStatement);

Expand Down
5 changes: 4 additions & 1 deletion server/models/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ CREATE TABLE `cash` (
`is_caution` BOOLEAN NOT NULL DEFAULT 0,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`reversed` TINYINT NOT NULL DEFAULT 0,
`edited` TINYINT NOT NULL DEFAULT 0,
PRIMARY KEY (`uuid`),
UNIQUE KEY `cash_1` (`reference`, `project_id`),
KEY `project_id` (`project_id`),
Expand Down Expand Up @@ -1405,7 +1406,7 @@ CREATE TABLE `purchase` (
`user_id` SMALLINT(5) UNSIGNED NOT NULL,
`payment_method` TEXT,
`note` TEXT,
`status_id` TINYINT(3) UNSIGNED NOT NULL DEFAULT 1,
`status_id` TINYINT(3) UNSIGNED NOT NULL DEFAULT 1,
PRIMARY KEY (`uuid`),
UNIQUE KEY `purchase_1` (`project_id`, `reference`),
KEY `project_id` (`project_id`),
Expand Down Expand Up @@ -1541,6 +1542,7 @@ CREATE TABLE `invoice` (
`date` DATETIME NOT NULL,
`description` TEXT NOT NULL,
`reversed` TINYINT NOT NULL DEFAULT 0,
`edited` TINYINT NOT NULL DEFAULT 0,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`uuid`),
UNIQUE KEY `invoice_1` (`project_id`, `reference`),
Expand Down Expand Up @@ -1817,6 +1819,7 @@ CREATE TABLE IF NOT EXISTS `voucher` (
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`type_id` SMALLINT(3) UNSIGNED NULL,
`reference_uuid` BINARY(16),
`edited` TINYINT NOT NULL DEFAULT 0,
KEY `project_id` (`project_id`),
KEY `currency_id` (`currency_id`),
KEY `user_id` (`user_id`),
Expand Down
8 changes: 3 additions & 5 deletions test/integration/cash.invoices.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* global expect, agent */
'use strict';

/**
* @overview CashInvoicePayments
Expand All @@ -13,7 +12,7 @@ const _ = require('lodash');
module.exports = PatientInvoicePayments;

function PatientInvoicePayments() {
const DEBTOR_UUID = // Test Patient
const DEBTOR_UUID =
'3be232f9-a4b9-4af6-984c-5d3f87d5c107';

const defaults = {
Expand All @@ -24,7 +23,7 @@ function PatientInvoicePayments() {
user_id : 1, // Test User
};

const INVOICES = [ // invoices defined in the database
const INVOICES = [ // invoices defined in the database
{ invoice_uuid : '957e4e79-a6bb-4b4d-a8f7-c42152b2c2f6' },
{ invoice_uuid : 'c44619e0-3a88-4754-a750-a414fc9567bf' },
];
Expand Down Expand Up @@ -88,7 +87,6 @@ function PatientInvoicePayments() {
return agent.post('/cash')
.send({ payment : INVALID_INVOICE_PAYMENT })
.then((res) => {

// anticipate a 400 error from the API.
helpers.api.errored(res, 400);
})
Expand Down Expand Up @@ -136,7 +134,7 @@ function PatientInvoicePayments() {
expect(res.body).to.not.be.empty;
expect(res.body).to.have.keys(
'uuid', 'barcode', 'reference', 'date', 'debtor_uuid', 'project_id', 'currency_id', 'items',
'cashbox_id', 'amount', 'user_id', 'description', 'is_caution'
'cashbox_id', 'amount', 'user_id', 'description', 'is_caution', 'edited'
);
})
.catch(helpers.handler);
Expand Down
28 changes: 14 additions & 14 deletions test/integration/cash.search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* global expect, chai, agent */
'use strict';
/* global agent */

/**
* @overview CashPaymentsSearch
Expand All @@ -13,12 +12,13 @@ const helpers = require('./helpers');

module.exports = CashPaymentsSearch;

function CashPaymentsSearch() { const NUM_CASH_RECORDS = 3;
function CashPaymentsSearch() {
const NUM_CASH_RECORDS = 3;
const DEBTOR_UUID = '3be232f9-a4b9-4af6-984c-5d3f87d5c107';

let TOMORROW = new Date();
TOMORROW.setDate(TOMORROW.getDate() + 1);
TOMORROW = TOMORROW.toISOString().split('T')[0];
[TOMORROW] = TOMORROW.toISOString().split('T');

// this is a quick querying function to reduce LOC
const SendHTTPQuery = (parameters, numResults) => {
Expand All @@ -40,52 +40,52 @@ function CashPaymentsSearch() { const NUM_CASH_RECORDS = 3;

// test limit functionality alone
it('GET /cash?limit=1 returns a single record', () => {
const params = { limit: 1 };
const params = { limit : 1 };
return SendHTTPQuery(params, 1);
});

it('GET /cash?is_caution=1 returns two records', () => {
const params = { is_caution: 1 };
const params = { is_caution : 1 };
return SendHTTPQuery(params, 2);
});

it('GET /cash?debtor_uuid=? returns 3 records', () => {
const params = { debtor_uuid: DEBTOR_UUID };
const params = { debtor_uuid : DEBTOR_UUID };
return SendHTTPQuery(params, 3);
});

it('GET /cash?is_caution=1&limit=1 should combine to return a single record', () => {
const params = { is_caution: 1, limit: 1 };
const params = { is_caution : 1, limit : 1 };
return SendHTTPQuery(params, 1);
});

it('GET /cash?cashbox_id=2 should return one record', () => {
const params = { cashbox_id: 2 };
const params = { cashbox_id : 2 };
return SendHTTPQuery(params, 1);
});

it('GET /cash?reference=CP.TPA.1 should return a single record', () => {
const params = { reference: 'CP.TPA.1' };
const params = { reference : 'CP.TPA.1' };
return SendHTTPQuery(params, 1);
});

it('GET /cash?dateFrom=2016-01-01 should return all records', () => {
const params = { dateFrom: '2016-01-01' };
const params = { dateFrom : '2016-01-01' };
return SendHTTPQuery(params, NUM_CASH_RECORDS);
});

it(`GET /cash?dateFrom=2016-01-01&dateTo=${TOMORROW} should return all records`, () => {
const params = { dateFrom: '2016-01-01', dateTo : TOMORROW };
const params = { dateFrom : '2016-01-01', dateTo : TOMORROW };
return SendHTTPQuery(params, NUM_CASH_RECORDS);
});

it('GET /cash?user_id=1 should return all records', () => {
const params = { user_id: 1 };
const params = { user_id : 1 };
return SendHTTPQuery(params, NUM_CASH_RECORDS);
});

it('GET /cash?currency_id=1 should return all records', () => {
const params = { currency_id: 1 };
const params = { currency_id : 1 };
return SendHTTPQuery(params, NUM_CASH_RECORDS);
});
}

0 comments on commit d95170f

Please sign in to comment.