Commit ba1503e4e20ac22fe908b805516db679a10a9a48
1 parent
73a6b0a5b9
Exists in
master
Classic calculateBaseBillAmountAuto splitted from calculateBaseBillAmount
Showing 1 changed file with 20 additions and 4 deletions Inline Diff
index.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const regexLooperReplace = require('tektrans-lib/regex-looper/replace'); | 3 | const regexLooperReplace = require('tektrans-lib/regex-looper/replace'); |
4 | const rcFromMsg = require('komodo-sdk/rc-from-msg'); | 4 | const rcFromMsg = require('komodo-sdk/rc-from-msg'); |
5 | const organicRc = require('./rc'); | 5 | const organicRc = require('./rc'); |
6 | 6 | ||
7 | function isInquiryResponseMessage(msg) { | 7 | function isInquiryResponseMessage(msg) { |
8 | return (msg || '').indexOf('CEK TAGIHAN') >= 0; | 8 | return (msg || '').indexOf('CEK TAGIHAN') >= 0; |
9 | } | 9 | } |
10 | 10 | ||
11 | function isPaymentResponseMessage(msg) { | 11 | function isPaymentResponseMessage(msg) { |
12 | return (msg || '').indexOf('BAYAR TAGIHAN') >= 0; | 12 | return (msg || '').indexOf('BAYAR TAGIHAN') >= 0; |
13 | } | 13 | } |
14 | 14 | ||
15 | function isPostpaidResponseMessage(msg) { | 15 | function isPostpaidResponseMessage(msg) { |
16 | return isInquiryResponseMessage(msg) || isPaymentResponseMessage(msg); | 16 | return isInquiryResponseMessage(msg) || isPaymentResponseMessage(msg); |
17 | } | 17 | } |
18 | 18 | ||
19 | function getRcFromMessage(msg, customRc) { | 19 | function getRcFromMessage(msg, customRc) { |
20 | let rc; | 20 | let rc; |
21 | if (customRc && Array.isArray(customRc) && customRc.length) { | 21 | if (customRc && Array.isArray(customRc) && customRc.length) { |
22 | rc = rcFromMsg(msg, customRc); | 22 | rc = rcFromMsg(msg, customRc); |
23 | } | 23 | } |
24 | 24 | ||
25 | if (!rc) { | 25 | if (!rc) { |
26 | rc = rcFromMsg(msg, organicRc); | 26 | rc = rcFromMsg(msg, organicRc); |
27 | } | 27 | } |
28 | 28 | ||
29 | return rc; | 29 | return rc; |
30 | } | 30 | } |
31 | 31 | ||
32 | function getPriceFromMessage(msg, rule) { | 32 | function getPriceFromMessage(msg, rule) { |
33 | if (typeof msg !== 'string') { | 33 | if (typeof msg !== 'string') { |
34 | return; | 34 | return; |
35 | } | 35 | } |
36 | 36 | ||
37 | if (process.env.DEBUG_IRS && !rule) { | 37 | if (process.env.DEBUG_IRS && !rule) { |
38 | console.log('** IRS.getPriceFromMessage no rule'); // eslint-disable-line no-console | 38 | console.log('** IRS.getPriceFromMessage no rule'); // eslint-disable-line no-console |
39 | } | 39 | } |
40 | 40 | ||
41 | if (process.env.DEBUG_IRS && rule) { | 41 | if (process.env.DEBUG_IRS && rule) { |
42 | console.log('** IRS.getPriceFromMessage rule: ' + JSON.stringify(rule, null, 2)); // eslint-disable-line no-console | 42 | console.log('** IRS.getPriceFromMessage rule: ' + JSON.stringify(rule, null, 2)); // eslint-disable-line no-console |
43 | } | 43 | } |
44 | 44 | ||
45 | //const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "Harga: ([\\d\\.]+?) "; | 45 | //const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "Harga: ([\\d\\.]+?) "; |
46 | const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "\\d+\\s+-\\s+([\\d,\\.]+)\\s+="; | 46 | const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "\\d+\\s+-\\s+([\\d,\\.]+)\\s+="; |
47 | const match_idx = (rule && typeof rule.match_idx === 'number') ? rule.match_idx : 1; | 47 | const match_idx = (rule && typeof rule.match_idx === 'number') ? rule.match_idx : 1; |
48 | 48 | ||
49 | const re = new RegExp(pattern); | 49 | const re = new RegExp(pattern); |
50 | const matches = msg.match(re); | 50 | const matches = msg.match(re); |
51 | if (process.env.DEBUG_IRS) { | 51 | if (process.env.DEBUG_IRS) { |
52 | console.log('** IRS.getPriceFromMessage msg: "' + msg + '" active_pattern: "' + pattern + '" active_match_idx: ' + match_idx); // eslint-disable-line no-console | 52 | console.log('** IRS.getPriceFromMessage msg: "' + msg + '" active_pattern: "' + pattern + '" active_match_idx: ' + match_idx); // eslint-disable-line no-console |
53 | console.log('** IRS.getPriceFromMessage matches:\n' + JSON.stringify(matches)); // eslint-disable-line no-console | 53 | console.log('** IRS.getPriceFromMessage matches:\n' + JSON.stringify(matches)); // eslint-disable-line no-console |
54 | } | 54 | } |
55 | 55 | ||
56 | if (matches && matches[match_idx]) { | 56 | if (matches && matches[match_idx]) { |
57 | const result = Number(matches[match_idx].replace(/\./g, '')); | 57 | const result = Number(matches[match_idx].replace(/\./g, '')); |
58 | if (process.env.DEBUG_IRS) { | 58 | if (process.env.DEBUG_IRS) { |
59 | console.log('** IRS.getPriceFromMessage SUPPLIER-PRICE: ' + result); // eslint-disable-line no-console | 59 | console.log('** IRS.getPriceFromMessage SUPPLIER-PRICE: ' + result); // eslint-disable-line no-console |
60 | } | 60 | } |
61 | return result; | 61 | return result; |
62 | } | 62 | } |
63 | } | 63 | } |
64 | 64 | ||
65 | function extractFromMessage(msg, rule) { | 65 | function extractFromMessage(msg, rule) { |
66 | if (typeof msg !== 'string') { return; } | 66 | if (typeof msg !== 'string') { return; } |
67 | 67 | ||
68 | if (!rule) { return; } | 68 | if (!rule) { return; } |
69 | 69 | ||
70 | if (typeof rule !== 'object') { | 70 | if (typeof rule !== 'object') { |
71 | return; | 71 | return; |
72 | } | 72 | } |
73 | 73 | ||
74 | rule.match_idx = Number(rule.match_idx); | 74 | rule.match_idx = Number(rule.match_idx); |
75 | 75 | ||
76 | if (!rule.match_idx) { | 76 | if (!rule.match_idx) { |
77 | rule.match_idx = 1; | 77 | rule.match_idx = 1; |
78 | } | 78 | } |
79 | 79 | ||
80 | const re = new RegExp(rule.pattern); | 80 | const re = new RegExp(rule.pattern); |
81 | const matches = msg.match(re); | 81 | const matches = msg.match(re); |
82 | if (matches && matches[rule.match_idx] && typeof matches[rule.match_idx] === 'string') { | 82 | if (matches && matches[rule.match_idx] && typeof matches[rule.match_idx] === 'string') { |
83 | return matches[rule.match_idx]; | 83 | return matches[rule.match_idx]; |
84 | } | 84 | } |
85 | } | 85 | } |
86 | 86 | ||
87 | function getSnFromMessage(msg, rule) { | 87 | function getSnFromMessage(msg, rule) { |
88 | if (!rule) { | 88 | if (!rule) { |
89 | rule = { | 89 | rule = { |
90 | pattern: "[: ]S/*N:\\s*(.+?)\\s", | 90 | pattern: "[: ]S/*N:\\s*(.+?)\\s", |
91 | match_idx: 1 | 91 | match_idx: 1 |
92 | } | 92 | } |
93 | } | 93 | } |
94 | 94 | ||
95 | let sn = extractFromMessage(msg, rule); | 95 | let sn = extractFromMessage(msg, rule); |
96 | if (!sn || typeof sn !== 'string') { return; } | 96 | if (!sn || typeof sn !== 'string') { return; } |
97 | 97 | ||
98 | return sn.toUpperCase().replace(/[^a-zA-Z0-9:,/]/g, '-').replace(/-+/g, '-').replace(/-*\/-*/g, '/').replace(/^-+/, '').replace(/-+$/, ''); | 98 | return sn.toUpperCase().replace(/[^a-zA-Z0-9:,/]/g, '-').replace(/-+/g, '-').replace(/-*\/-*/g, '/').replace(/^-+/, '').replace(/-+$/, ''); |
99 | } | 99 | } |
100 | 100 | ||
101 | function getBalanceFromMessage(msg, rule) { | 101 | function getBalanceFromMessage(msg, rule) { |
102 | if (!rule) { | 102 | if (!rule) { |
103 | rule = { | 103 | rule = { |
104 | pattern: "Saldo: .+? = ([\\d\\.]+) ", | 104 | pattern: "Saldo: .+? = ([\\d\\.]+) ", |
105 | match_idx: 1 | 105 | match_idx: 1 |
106 | } | 106 | } |
107 | } | 107 | } |
108 | 108 | ||
109 | let result = extractFromMessage(msg, rule); | 109 | let result = extractFromMessage(msg, rule); |
110 | if (!result || typeof result !== 'string') { return; } | 110 | if (!result || typeof result !== 'string') { return; } |
111 | 111 | ||
112 | return Number(result.replace(/\./g, '')); | 112 | return Number(result.replace(/\./g, '')); |
113 | } | 113 | } |
114 | 114 | ||
115 | function getDetailFromMessage(msg, rule, replacementRules) { | 115 | function getDetailFromMessage(msg, rule, replacementRules) { |
116 | if (!rule) { | 116 | if (!rule) { |
117 | rule = { | 117 | rule = { |
118 | pattern: " Detail:\\s*(.+?)\\s+Sisa [Ss]aldo", | 118 | pattern: " Detail:\\s*(.+?)\\s+Sisa [Ss]aldo", |
119 | match_idx: 1, | 119 | match_idx: 1, |
120 | } | 120 | } |
121 | } | 121 | } |
122 | 122 | ||
123 | return ( | 123 | return ( |
124 | regexLooperReplace(extractFromMessage(msg, rule), replacementRules) || '' | 124 | regexLooperReplace(extractFromMessage(msg, rule), replacementRules) || '' |
125 | ).trim() || null; | 125 | ).trim() || null; |
126 | } | 126 | } |
127 | 127 | ||
128 | function splitPostpaidDetail(str) { | 128 | function splitPostpaidDetail(str) { |
129 | const retval = (str || '') | 129 | const retval = (str || '') |
130 | .split('/') | 130 | .split('/') |
131 | .map((item) => { | 131 | .map((item) => { |
132 | let [keyword, value] = item.split(':'); | 132 | let [keyword, value] = item.split(':'); |
133 | keyword = (keyword || '').trim() | 133 | keyword = (keyword || '').trim() |
134 | 134 | ||
135 | if (keyword === 'N') keyword = 'SN'; | 135 | if (keyword === 'N') keyword = 'SN'; |
136 | 136 | ||
137 | return { | 137 | return { |
138 | keyword, | 138 | keyword, |
139 | value: (value || '').trim(), | 139 | value: (value || '').trim(), |
140 | }; | 140 | }; |
141 | }) | 141 | }) |
142 | .filter((item) => item.keyword.length && item.value.length); | 142 | .filter((item) => item.keyword.length && item.value.length); |
143 | return retval; | 143 | return retval; |
144 | } | 144 | } |
145 | 145 | ||
146 | function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, billCount) { | 146 | function calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval) { |
147 | const keywords = customKeywords || ['TAGIHANASLI', 'TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA']; | 147 | const tagihanAsliKeyvalKeywords = ((productKeyval && productKeyval.KEYWORD_TAGASLI) || '') |
148 | .split(/ *, */); | ||
149 | |||
150 | const keywords = (tagihanAsliKeyvalKeywords && Array.isArray(tagihanAsliKeyvalKeywords) && tagihanAsliKeyvalKeywords.length && tagihanAsliKeyvalKeywords) | ||
151 | || customKeywords | ||
152 | || ['TAGIHANASLI', 'TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA']; | ||
148 | 153 | ||
149 | let retval = 0; | 154 | let retval = 0; |
150 | let detailCount = (detailSplitted || []).length; | 155 | let detailCount = (detailSplitted || []).length; |
151 | 156 | ||
152 | for (let i = 0; i < detailCount; i += 1) { | 157 | for (let i = 0; i < detailCount; i += 1) { |
153 | const item = detailSplitted[i]; | 158 | const item = detailSplitted[i]; |
154 | if (keywords.indexOf(item.keyword.toUpperCase()) >= 0) { | 159 | if (keywords.indexOf(item.keyword.toUpperCase()) >= 0) { |
155 | const value = (item.value || '').trim().replace(/^rp */i, '').replace(/\./g, ''); | 160 | const value = (item.value || '').trim().replace(/^rp */i, '').replace(/\./g, ''); |
156 | retval += Number(value) || 0; | 161 | retval += Number(value) || 0; |
157 | } | 162 | } |
158 | } | 163 | } |
159 | 164 | ||
160 | if (retval) return retval; | 165 | return retval; |
161 | if (!productKeyval || !Array.isArray(productKeyval) || !productKeyval.length) return 0; | 166 | } |
167 | |||
168 | function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, billCount) { | ||
169 | if ( | ||
170 | !productKeyval | ||
171 | || !Array.isArray(productKeyval) | ||
172 | || !productKeyval.KEYWORD_TOTALTAG | ||
173 | || !productKeyval.KEYWORD_ADMINFEE | ||
174 | ) { | ||
175 | return calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval, billCount); | ||
176 | } | ||
162 | 177 | ||
163 | const billCountOrDefault = billCount || 1; | 178 | const billCountOrDefault = billCount || 1; |
164 | 179 | ||
165 | const totalTagihanKeyword = productKeyval.KEYWORD_TOTALTAG; | 180 | const totalTagihanKeyword = productKeyval.KEYWORD_TOTALTAG; |
166 | if (!totalTagihanKeyword) return 0; | 181 | if (!totalTagihanKeyword) return 0; |
167 | 182 | ||
168 | const totalTagihanItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === totalTagihanKeyword); | 183 | const totalTagihanItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === totalTagihanKeyword); |
169 | const totalTagihanValue = Number((totalTagihanItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); | 184 | const totalTagihanValue = Number((totalTagihanItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); |
170 | if (!totalTagihanValue) return 0; | 185 | if (!totalTagihanValue) return 0; |
171 | 186 | ||
172 | const defaultAdminFee = Number(productKeyval.ADMINFEE || ''); | 187 | const defaultAdminFee = Number(productKeyval.ADMINFEE || ''); |
173 | 188 | ||
174 | const adminFeeKeyword = productKeyval.KEYWORD_ADMINFEE; | 189 | const adminFeeKeyword = productKeyval.KEYWORD_ADMINFEE; |
175 | if (!adminFeeKeyword && defaultAdminFee) return totalTagihanValue - (defaultAdminFee * billCountOrDefault); | 190 | if (!adminFeeKeyword && defaultAdminFee) return totalTagihanValue - (defaultAdminFee * billCountOrDefault); |
176 | if (!adminFeeKeyword) return 0; | 191 | if (!adminFeeKeyword) return 0; |
177 | 192 | ||
178 | const adminFeeItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === adminFeeKeyword); | 193 | const adminFeeItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === adminFeeKeyword); |
179 | const adminFeeValue = Number((adminFeeItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); | 194 | const adminFeeValue = Number((adminFeeItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); |
180 | if (!adminFeeValue) return 0; | 195 | if (!adminFeeValue) return 0; |
181 | 196 | ||
182 | return totalTagihanValue - (adminFeeValue * billCountOrDefault); | 197 | return totalTagihanValue - (adminFeeValue * billCountOrDefault); |
183 | } | 198 | } |
184 | 199 | ||
200 | |||
185 | function getBillCount(msg) { | 201 | function getBillCount(msg) { |
186 | const matches = (msg || '').match(/JMLBLN:(\d+)BLN/); | 202 | const matches = (msg || '').match(/JMLBLN:(\d+)BLN/); |
187 | return (matches && Number(matches[1])) || null; | 203 | return (matches && Number(matches[1])) || null; |
188 | } | 204 | } |
189 | 205 | ||
190 | exports.getRcFromMessage = getRcFromMessage; | 206 | exports.getRcFromMessage = getRcFromMessage; |
191 | exports.getPriceFromMessage = getPriceFromMessage; | 207 | exports.getPriceFromMessage = getPriceFromMessage; |
192 | exports.getSnFromMessage = getSnFromMessage; | 208 | exports.getSnFromMessage = getSnFromMessage; |
193 | exports.getBalanceFromMessage = getBalanceFromMessage; | 209 | exports.getBalanceFromMessage = getBalanceFromMessage; |
194 | exports.isInquiryResponseMessage = isInquiryResponseMessage; | 210 | exports.isInquiryResponseMessage = isInquiryResponseMessage; |
195 | exports.isPaymentResponseMessage = isPaymentResponseMessage; | 211 | exports.isPaymentResponseMessage = isPaymentResponseMessage; |
196 | exports.isPostpaidResponseMessage = isPostpaidResponseMessage; | 212 | exports.isPostpaidResponseMessage = isPostpaidResponseMessage; |
197 | exports.getDetailFromMessage = getDetailFromMessage; | 213 | exports.getDetailFromMessage = getDetailFromMessage; |
198 | exports.splitPostpaidDetail = splitPostpaidDetail; | 214 | exports.splitPostpaidDetail = splitPostpaidDetail; |
199 | exports.calculateBaseBillAmount = calculateBaseBillAmount; | 215 | exports.calculateBaseBillAmount = calculateBaseBillAmount; |
200 | exports.getBillCount = getBillCount; | 216 | exports.getBillCount = getBillCount; |
201 | 217 |