diff --git a/index.js b/index.js index 94e0fd9..ccfd4c1 100644 --- a/index.js +++ b/index.js @@ -119,7 +119,7 @@ function getDetailFromMessage(msg, rule, replacementRules) { match_idx: 1, } } - + return ( regexLooperReplace(extractFromMessage(msg, rule), replacementRules) || '' ).trim() || null; @@ -143,8 +143,8 @@ function splitPostpaidDetail(str) { return retval; } -function calculateBaseBillAmount(detailSplitted, customKeywords) { - const keywords = customKeywords || ['TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA']; +function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval) { + const keywords = customKeywords || ['TAGIHANASLI', 'TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA']; let retval = 0; let detailCount = (detailSplitted || []).length; @@ -157,7 +157,27 @@ function calculateBaseBillAmount(detailSplitted, customKeywords) { } } - return retval; + if (retval) return retval; + if (!productKeyval || !Array.isArray(productKeyval) || !productKeyval.length) return 0; + + const totalTagihanKeyword = productKeyval.KEYWORD_TOTALTAG; + if (!totalTagihanKeyword) return 0; + + const totalTagihanItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === totalTagihanKeyword); + const totalTagihanValue = Number((totalTagihanItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); + if (!totalTagihanValue) return 0; + + const defaultAdminFee = Number(productKeyval.ADMINFEE || ''); + + const adminFeeKeyword = productKeyval.KEYWORD_ADMINFEE; + if (!adminFeeKeyword && defaultAdminFee) return totalTagihanValue - defaultAdminFee; + if (!adminFeeKeyword) return 0; + + const adminFeeItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === adminFeeKeyword); + const adminFeeValue = Number((adminFeeItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); + if (!adminFeeValue) return 0; + + return totalTagihanValue - adminFeeValue; } function getBillCount(msg) {