Skip to content

Commit

Permalink
fix(vouchers): undo reversals on delete
Browse files Browse the repository at this point in the history
This commit updates the voucher delete method to undo the "reversal"
flag on the reversed record if the voucher has been removed.  It is
currently a naive method - simply attempt to remove the value if it is
present.  This can be improved in the future by checking the transaction
types first.

Closes #2207.
  • Loading branch information
jniles committed Oct 19, 2017
1 parent 0a678ad commit 7d46062
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
30 changes: 27 additions & 3 deletions server/controllers/finance/vouchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,11 @@ function create(req, res, next) {
* @function safelyDeleteVoucher
*
* @description
* This function deletes a voucher from the system. It assumes that
* checks have already been made for referencing transactions.
* This function deletes a voucher from the system. The method first checks
* that a transaction can be deleted using the shared transaction library.
* After removing the voucher, it also updates and "reversal" flags if necessary
* to ensure that cash payments and invoices do not maintain broken links to
* vouchers that have been deleted.
*/
function safelyDeleteVoucher(guid) {
const DELETE_TRANSACTION = `
Expand All @@ -266,6 +269,25 @@ function safelyDeleteVoucher(guid) {
DELETE FROM document_map WHERE uuid = ?;
`;

// NOTE(@jniles) - this is a naive way of undoing reversals. If no value is
// matched, nothing happens. This can be improved in the future by first
// checking if the voucher's transaction_type is a reversal type, and then
// performing or skipping this step based on that result.

const TOGGLE_INVOICE_REVERSAL = `
UPDATE invoice
JOIN voucher ON invoice.uuid = voucher.reference_uuid
SET invoice.reversed = 0
WHERE voucher.uuid = ?;
`;

const TOGGLE_CASH_REVERSAL = `
UPDATE cash
JOIN voucher ON cash.uuid = voucher.reference_uuid
SET cash.reversed = 0
WHERE voucher.uuid = ?;
`;

return shared.isRemovableTransaction(guid)
.then(() => {
const binaryUuid = db.bid(guid);
Expand All @@ -275,7 +297,9 @@ function safelyDeleteVoucher(guid) {
.addQuery(DELETE_TRANSACTION, binaryUuid)
.addQuery(DELETE_TRANSACTION_HISTORY, binaryUuid)
.addQuery(DELETE_VOUCHER, binaryUuid)
.addQuery(DELETE_DOCUMENT_MAP, binaryUuid);
.addQuery(DELETE_DOCUMENT_MAP, binaryUuid)
.addQuery(TOGGLE_INVOICE_REVERSAL, binaryUuid)
.addQuery(TOGGLE_CASH_REVERSAL, binaryUuid);

return transaction.execute();
});
Expand Down
2 changes: 1 addition & 1 deletion server/models/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ CREATE TABLE `purchase` (
`user_id` SMALLINT(5) UNSIGNED NOT NULL,
`payment_method` TEXT,
`note` TEXT,
`status_id` TINYINT(3) UNSIGNED NOT NULL,
`status_id` TINYINT(3) UNSIGNED NOT NULL,
PRIMARY KEY (`uuid`),
UNIQUE KEY `purchase_1` (`project_id`, `reference`),
KEY `project_id` (`project_id`),
Expand Down

0 comments on commit 7d46062

Please sign in to comment.