Skip to content

Commit

Permalink
fix(vouchers): correctly link reversals
Browse files Browse the repository at this point in the history
This commit fixes all the voucher links that were previously only
searching on `document_uuid`.  While this is correct for the generic
case of vouchers, for credit notes and debit notes, the reference is not
a direct link.  Instead, the transaction is simply reversed (debits and
credits are swapped) and the reference is stored in the reference_uuid
column.  For this reason, we have to search on both.

Closes #2222.
  • Loading branch information
jniles committed Oct 19, 2017
1 parent cb00962 commit 96a4b73
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions server/controllers/finance/vouchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ function lookupVoucher(vUuid) {
});
}

// NOTE(@jniles) - this is used to find references for both vouchers and credit notes.
const REFERENCE_SQL = `
v.uuid IN (
SELECT DISTINCT voucher.uuid FROM voucher JOIN voucher_item
ON voucher.uuid = voucher_item.voucher_uuid
WHERE voucher_item.document_uuid = ? OR voucher.reference_uuid = ?
)`;

function find(options) {
db.convert(options, ['uuid', 'reference_uuid', 'entity_uuid', 'cash_uuid', 'invoice_uuid']);

Expand Down Expand Up @@ -151,9 +159,8 @@ function find(options) {
// @todo - could this be improved
filters.custom('account_id', 'v.uuid IN (SELECT DISTINCT voucher_uuid FROM voucher_item WHERE account_id = ?)');

filters.custom('invoice_uuid', 'v.uuid IN (SELECT DISTINCT voucher_uuid FROM voucher_item WHERE document_uuid = ?)');

filters.custom('cash_uuid', 'v.uuid IN (SELECT DISTINCT voucher_uuid FROM voucher_item WHERE document_uuid = ?)');
filters.custom('invoice_uuid', REFERENCE_SQL, [options.invoice_uuid, options.invoice_uuid]);
filters.custom('cash_uuid', REFERENCE_SQL, [options.invoice_uuid, options.invoice_uuid]);

// @TODO Support ordering query (reference support for limit)?
filters.setOrder('ORDER BY v.date DESC');
Expand Down

0 comments on commit 96a4b73

Please sign in to comment.