diff --git a/index.js b/index.js index a7e4216..25d5085 100644 --- a/index.js +++ b/index.js @@ -143,8 +143,13 @@ function splitPostpaidDetail(str) { return retval; } -function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, billCount) { - const keywords = customKeywords || ['TAGIHANASLI', 'TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA']; +function calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval) { + const tagihanAsliKeyvalKeywords = ((productKeyval && productKeyval.KEYWORD_TAGASLI) || '') + .split(/ *, */); + + const keywords = (tagihanAsliKeyvalKeywords && Array.isArray(tagihanAsliKeyvalKeywords) && tagihanAsliKeyvalKeywords.length && tagihanAsliKeyvalKeywords) + || customKeywords + || ['TAGIHANASLI', 'TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA']; let retval = 0; let detailCount = (detailSplitted || []).length; @@ -157,8 +162,18 @@ function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, } } - if (retval) return retval; - if (!productKeyval || !Array.isArray(productKeyval) || !productKeyval.length) return 0; + return retval; +} + +function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, billCount) { + if ( + !productKeyval + || !Array.isArray(productKeyval) + || !productKeyval.KEYWORD_TOTALTAG + || !productKeyval.KEYWORD_ADMINFEE + ) { + return calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval, billCount); + } const billCountOrDefault = billCount || 1; @@ -182,6 +197,7 @@ function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, return totalTagihanValue - (adminFeeValue * billCountOrDefault); } + function getBillCount(msg) { const matches = (msg || '').match(/JMLBLN:(\d+)BLN/); return (matches && Number(matches[1])) || null;