Skip to content

Commit

Permalink
feat(cash): Posting Cash Payments
Browse files Browse the repository at this point in the history
This feature allows the module to post cash payments with cash items.
To accomplish this, a new file called `functions.sql` was added to the
build scripts since `procedures.sql` was getting far to large.  This
file contains functions used in the procedures or other queries.

Posting cautions is not yet supported.
  • Loading branch information
Jonathan Niles committed Sep 5, 2016
1 parent 348d0fa commit 469a60f
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 118 deletions.
5 changes: 4 additions & 1 deletion server/controllers/finance/cash.create.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ function create(req, res, next) {
}

transaction.addQuery('CALL WriteCash(?)', [cashUuid]);
// transaction.addQuery('CALL PostCash(?)', [cashUuid]);

if (!isCaution) {
transaction.addQuery('CALL PostCash(?)', [cashUuid]);
}

transaction.execute()
.then(() => {
Expand Down
92 changes: 92 additions & 0 deletions server/models/functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
This file contains all the stored functions used in bhima's database and queries.
It should be loaded after schema.sql.
*/

DELIMITER $$

/*
HUID(hexUuid)
Converts a hex uuid (36 chars) into a binary uuid (16 bytes)
*/
CREATE FUNCTION HUID(_uuid CHAR(36))
RETURNS BINARY(16) DETERMINISTIC
RETURN UNHEX(REPLACE(_uuid, '-', ''));
$$


/*
BUID(binaryUuid)
Converts a binary uuid (16 bytes) to dash-delimited hex UUID (36 characters).
*/
CREATE FUNCTION BUID(b BINARY(16))
RETURNS CHAR(36) DETERMINISTIC
BEGIN
DECLARE hex CHAR(32);
SET hex = HEX(b);
RETURN LCASE(CONCAT_WS('-', SUBSTR(hex,1, 8), SUBSTR(hex, 9,4), SUBSTR(hex, 13,4), SUBSTR(hex, 17,4), SUBSTR(hex, 21, 12)));
END
$$


/*
GetExchangeRate(enterpriseId, currencyId, date)
Returns the current exchange rate (`rate` column) for the given currency from
the database. It is up to the callee to determine how to treat the rate.
@TODO Is there some way to make this do the IF() select as well?
EXAMPLE
SET currentExchangeRate = GetExchangeRate(enterpriseId, currencyId, date);
SET currentExchangeRate = (SELECT IF(recordCurrencyId = enterpriseCurrencyId, 1, currentExchangeRate));
*/
CREATE FUNCTION GetExchangeRate(
enterpriseId INT,
currencyId INT,
date TIMESTAMP
)
RETURNS DECIMAL DETERMINISTIC
BEGIN
RETURN (
SELECT e.rate FROM exchange_rate AS e
WHERE e.enterprise_id = enterpriseId AND e.currency_id = currencyId AND e.date <= date
ORDER BY e.date DESC LIMIT 1
);
END $$


/*
GenerateTransactionId(projectId)
Returns a new transaction id to be stored in the database by scanning for used
ids in the posting_journal and general_ledger tables.
EXAMPLE
SET transId = GenerateTransactionid(projectid);
*/
CREATE FUNCTION GenerateTransactionId(
project_id SMALLINT(5)
)
RETURNS VARCHAR(100) DETERMINISTIC
BEGIN
DECLARE trans_id_length TINYINT(1) DEFAULT 4;
RETURN (
SELECT CONCAT(abbr, IFNULL(MAX(increment), 1)) AS id
FROM (
SELECT project.abbr, MAX(FLOOR(SUBSTR(trans_id, trans_id_length))) + 1 AS increment
FROM posting_journal JOIN project ON posting_journal.project_id = project.id
WHERE posting_journal.project_id = project_id
UNION
SELECT project.abbr, MAX(FLOOR(SUBSTR(trans_id, trans_id_length))) + 1 AS increment
FROM general_ledger JOIN project ON general_ledger.project_id = project.id
WHERE general_ledger.project_id = project_id)c
);
END $$


DELIMITER ;
Loading

0 comments on commit 469a60f

Please sign in to comment.