diff --git a/index.js b/index.js index d43966c..731a15b 100644 --- a/index.js +++ b/index.js @@ -168,29 +168,53 @@ function calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyv function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, billCount) { if ( !productKeyval - || !Array.isArray(productKeyval) - || !productKeyval.KEYWORD_TOTALTAG + || !productKeyval.KEYWORD_TOTALTAG || !productKeyval.KEYWORD_ADMINFEE ) { return calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval, billCount); } - + const billCountOrDefault = billCount || 1; const totalTagihanKeyword = productKeyval.KEYWORD_TOTALTAG; if (!totalTagihanKeyword) return 0; - const totalTagihanItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === totalTagihanKeyword); + const totalTagihanItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === totalTagihanKeyword.toUpperCase()); const totalTagihanValue = Number((totalTagihanItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); - if (!totalTagihanValue) return 0; + if (!totalTagihanValue) { + if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { + // eslint-disable-next-line no-console + console.log('=========== MISSING totalTagihanValue D7282FA7'); + } + + return 0; + } const defaultAdminFee = Number(productKeyval.ADMINFEE || ''); const adminFeeKeyword = productKeyval.KEYWORD_ADMINFEE; - if (!adminFeeKeyword && defaultAdminFee) return totalTagihanValue - (defaultAdminFee * billCountOrDefault); - if (!adminFeeKeyword) return 0; + if (!adminFeeKeyword && defaultAdminFee) { + if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { + // eslint-disable-next-line no-console + console.log('======== TOTAL TAGIHAN VALUE - (DEFAULT ADMIN FEE * BILL COUNT) D35E56AD') ; + } + return totalTagihanValue - (defaultAdminFee * billCountOrDefault); + } - const adminFeeItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === adminFeeKeyword); + if (!adminFeeKeyword) { + if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { + // eslint-disable-next-line no-console + console.log('======== Missing adminFeeKeyword'); + } + + return 0; + } + + const adminFeeItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === adminFeeKeyword.toUpperCase()); + if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { + // eslint-disable-next-line no-console + console.log(`======== adminFeeItem: ${adminFeeKeyword}`, adminFeeItem); + } const adminFeeValue = Number((adminFeeItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); if (!adminFeeValue) return 0; diff --git a/test/main.js b/test/main.js index 486aa17..671c3fc 100644 --- a/test/main.js +++ b/test/main.js @@ -179,6 +179,13 @@ describe('#irs', function() { splitted.find((item) => item.keyword === 'TAGIHAN').value.should.equal('131560'); splitted.find((item) => item.keyword === 'DENDA').value.should.equal('9000'); }); + + it('should return correct result - tajira', () => { + const splitted = irs.splitPostpaidDetail('REFID:20770812/NAMA:SALWA AZ ZAHRA/CABANG:1012-DEPOK/Periode:1/Admin:2500/TOTALBAYAR:28.000'); + // console.log(splitted); + splitted.find((item) => item.keyword === 'REFID').value.should.equal('20770812'); + splitted.find((item) => item.keyword === 'Admin').value.should.equal('2500'); + }); }); describe('#calculateBaseBillAmount', () => { @@ -214,6 +221,16 @@ describe('#irs', function() { // }, // 1 ).should.equal(10140); + + irs.calculateBaseBillAmount( + irs.splitPostpaidDetail('REFID:20770812/NAMA:SALWA AZ ZAHRA/CABANG:1012-DEPOK/Periode:1/Admin:2500/TOTALBAYAR:28.000'), + null, + { + KEYWORD_TOTALTAG: 'TOTALBAYAR', + KEYWORD_ADMINFEE: 'Admin', + }, + 1 + ).should.equal(25500); }); });