Skip to content

Commit

Permalink
feat(invoice): add invoice balance function
Browse files Browse the repository at this point in the history
This commit adds a MySQL function for computing the balance of an
invoice.  It is for analysis purposes currently.
  • Loading branch information
jniles committed Mar 23, 2022
1 parent d171c5a commit 0cae825
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions server/models/admin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,47 @@ BEGIN
UPDATE lot SET barcode = CONCAT('LT', LEFT(HEX(lot.uuid), 8));
END $$

/**
zGetInvoiceBalance()
Gets the balance on an invoice due to a debtor.
*/
CREATE FUNCTION zGetInvoiceBalance(invoiceUuid BINARY(16))
RETURNS DOUBLE DETERMINISTIC
BEGIN
DECLARE entityUuid BINARY(16);

-- get the debtorUuid
SELECT debtor_uuid INTO entityUuid FROM invoice WHERE invoice.uuid = invoiceUuid;

-- return the balance
RETURN (
SELECT SUM(debit - credit) AS balance
FROM (
SELECT record_uuid AS uuid, debit_equiv as debit, credit_equiv as credit, entity_uuid
FROM posting_journal
WHERE posting_journal.record_uuid = invoiceUuid AND entity_uuid = entityUuid

UNION ALL

SELECT record_uuid AS uuid, debit_equiv as debit, credit_equiv as credit, entity_uuid
FROM general_ledger
WHERE general_ledger.record_uuid = invoiceUuid AND entity_uuid = entityUuid

UNION ALL

SELECT reference_uuid AS uuid, debit_equiv as debit, credit_equiv as credit, entity_uuid
FROM posting_journal
WHERE posting_journal.reference_uuid = invoiceUuid AND entity_uuid = entityUuid

UNION ALL

SELECT reference_uuid AS uuid, debit_equiv as debit, credit_equiv as credit, entity_uuid
FROM general_ledger
WHERE general_ledger.reference_uuid = invoiceUuid AND entity_uuid = entityUuid
) AS ledger
GROUP BY ledger.uuid
);
END $$

DELIMITER ;

0 comments on commit 0cae825

Please sign in to comment.