Commit 68e64400dbca055bc09914927bc2807225f827c1

Authored by Adhidarma Hadiwinoto
1 parent 8830ca74d3
Exists in master

Fix getDetailFromMessage to support "Detail Tagihan" keyword

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

1 /* eslint-disable no-console */ 1 /* eslint-disable no-console */
2 const regexLooperReplace = require('tektrans-lib/regex-looper/replace'); 2 const regexLooperReplace = require('tektrans-lib/regex-looper/replace');
3 const rcFromMsg = require('komodo-sdk/rc-from-msg'); 3 const rcFromMsg = require('komodo-sdk/rc-from-msg');
4 const organicRc = require('./rc'); 4 const organicRc = require('./rc');
5 5
6 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 6 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
7 console.log('DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT detected'); 7 console.log('DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT detected');
8 } 8 }
9 9
10 function isInquiryResponseMessage(msg) { 10 function isInquiryResponseMessage(msg) {
11 return (msg || '').indexOf('CEK TAGIHAN') >= 0; 11 return (msg || '').indexOf('CEK TAGIHAN') >= 0;
12 } 12 }
13 13
14 function isPaymentResponseMessage(msg) { 14 function isPaymentResponseMessage(msg) {
15 return (msg || '').indexOf('BAYAR TAGIHAN') >= 0; 15 return (msg || '').indexOf('BAYAR TAGIHAN') >= 0;
16 } 16 }
17 17
18 function isPostpaidResponseMessage(msg) { 18 function isPostpaidResponseMessage(msg) {
19 return isInquiryResponseMessage(msg) || isPaymentResponseMessage(msg); 19 return isInquiryResponseMessage(msg) || isPaymentResponseMessage(msg);
20 } 20 }
21 21
22 function getRcFromMessage(msg, customRc) { 22 function getRcFromMessage(msg, customRc) {
23 let rc; 23 let rc;
24 if (customRc && Array.isArray(customRc) && customRc.length) { 24 if (customRc && Array.isArray(customRc) && customRc.length) {
25 rc = rcFromMsg(msg, customRc); 25 rc = rcFromMsg(msg, customRc);
26 } 26 }
27 27
28 if (!rc) { 28 if (!rc) {
29 rc = rcFromMsg(msg, organicRc); 29 rc = rcFromMsg(msg, organicRc);
30 } 30 }
31 31
32 return rc; 32 return rc;
33 } 33 }
34 34
35 function getPriceFromMessage(msg, rule) { 35 function getPriceFromMessage(msg, rule) {
36 if (typeof msg !== 'string') { 36 if (typeof msg !== 'string') {
37 return; 37 return;
38 } 38 }
39 39
40 if (process.env.DEBUG_IRS && !rule) { 40 if (process.env.DEBUG_IRS && !rule) {
41 console.log('** IRS.getPriceFromMessage no rule'); // eslint-disable-line no-console 41 console.log('** IRS.getPriceFromMessage no rule'); // eslint-disable-line no-console
42 } 42 }
43 43
44 if (process.env.DEBUG_IRS && rule) { 44 if (process.env.DEBUG_IRS && rule) {
45 console.log('** IRS.getPriceFromMessage rule: ' + JSON.stringify(rule, null, 2)); // eslint-disable-line no-console 45 console.log('** IRS.getPriceFromMessage rule: ' + JSON.stringify(rule, null, 2)); // eslint-disable-line no-console
46 } 46 }
47 47
48 //const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "Harga: ([\\d\\.]+?) "; 48 //const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "Harga: ([\\d\\.]+?) ";
49 const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "\\d+\\s+-\\s+([\\d,\\.]+)\\s+="; 49 const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "\\d+\\s+-\\s+([\\d,\\.]+)\\s+=";
50 const match_idx = (rule && typeof rule.match_idx === 'number') ? rule.match_idx : 1; 50 const match_idx = (rule && typeof rule.match_idx === 'number') ? rule.match_idx : 1;
51 51
52 const re = new RegExp(pattern); 52 const re = new RegExp(pattern);
53 const matches = msg.match(re); 53 const matches = msg.match(re);
54 if (process.env.DEBUG_IRS) { 54 if (process.env.DEBUG_IRS) {
55 console.log('** IRS.getPriceFromMessage msg: "' + msg + '" active_pattern: "' + pattern + '" active_match_idx: ' + match_idx); // eslint-disable-line no-console 55 console.log('** IRS.getPriceFromMessage msg: "' + msg + '" active_pattern: "' + pattern + '" active_match_idx: ' + match_idx); // eslint-disable-line no-console
56 console.log('** IRS.getPriceFromMessage matches:\n' + JSON.stringify(matches)); // eslint-disable-line no-console 56 console.log('** IRS.getPriceFromMessage matches:\n' + JSON.stringify(matches)); // eslint-disable-line no-console
57 } 57 }
58 58
59 if (matches && matches[match_idx]) { 59 if (matches && matches[match_idx]) {
60 const result = Number(matches[match_idx].replace(/\./g, '')); 60 const result = Number(matches[match_idx].replace(/\./g, ''));
61 if (process.env.DEBUG_IRS) { 61 if (process.env.DEBUG_IRS) {
62 console.log('** IRS.getPriceFromMessage SUPPLIER-PRICE: ' + result); // eslint-disable-line no-console 62 console.log('** IRS.getPriceFromMessage SUPPLIER-PRICE: ' + result); // eslint-disable-line no-console
63 } 63 }
64 return result; 64 return result;
65 } 65 }
66 } 66 }
67 67
68 function extractFromMessage(msg, rule) { 68 function extractFromMessage(msg, rule) {
69 if (typeof msg !== 'string') { return; } 69 if (typeof msg !== 'string') { return; }
70 70
71 if (!rule) { return; } 71 if (!rule) { return; }
72 72
73 if (typeof rule !== 'object') { 73 if (typeof rule !== 'object') {
74 return; 74 return;
75 } 75 }
76 76
77 rule.match_idx = Number(rule.match_idx); 77 rule.match_idx = Number(rule.match_idx);
78 78
79 if (!rule.match_idx) { 79 if (!rule.match_idx) {
80 rule.match_idx = 1; 80 rule.match_idx = 1;
81 } 81 }
82 82
83 const re = new RegExp(rule.pattern); 83 const re = new RegExp(rule.pattern);
84 const matches = msg.match(re); 84 const matches = msg.match(re);
85 if (matches && matches[rule.match_idx] && typeof matches[rule.match_idx] === 'string') { 85 if (matches && matches[rule.match_idx] && typeof matches[rule.match_idx] === 'string') {
86 return matches[rule.match_idx]; 86 return matches[rule.match_idx];
87 } 87 }
88 } 88 }
89 89
90 function getSnFromMessage(msg, rule) { 90 function getSnFromMessage(msg, rule) {
91 if (!rule) { 91 if (!rule) {
92 rule = { 92 rule = {
93 pattern: "[: ]S/*N:\\s*(.+?)\\s", 93 pattern: "[: ]S/*N:\\s*(.+?)\\s",
94 match_idx: 1 94 match_idx: 1
95 } 95 }
96 } 96 }
97 97
98 let sn = extractFromMessage(msg, rule); 98 let sn = extractFromMessage(msg, rule);
99 if (!sn || typeof sn !== 'string') { return; } 99 if (!sn || typeof sn !== 'string') { return; }
100 100
101 return sn.toUpperCase().replace(/[^a-zA-Z0-9:,/]/g, '-').replace(/-+/g, '-').replace(/-*\/-*/g, '/').replace(/^-+/, '').replace(/-+$/, ''); 101 return sn.toUpperCase().replace(/[^a-zA-Z0-9:,/]/g, '-').replace(/-+/g, '-').replace(/-*\/-*/g, '/').replace(/^-+/, '').replace(/-+$/, '');
102 } 102 }
103 103
104 function getBalanceFromMessage(msg, rule) { 104 function getBalanceFromMessage(msg, rule) {
105 if (!rule) { 105 if (!rule) {
106 rule = { 106 rule = {
107 pattern: "Saldo: .+? = ([\\d\\.]+) ", 107 pattern: "Saldo: .+? = ([\\d\\.]+) ",
108 match_idx: 1 108 match_idx: 1
109 } 109 }
110 } 110 }
111 111
112 let result = extractFromMessage(msg, rule); 112 let result = extractFromMessage(msg, rule);
113 if (!result || typeof result !== 'string') { return; } 113 if (!result || typeof result !== 'string') { return; }
114 114
115 return Number(result.replace(/\D/g, '')); 115 return Number(result.replace(/\D/g, ''));
116 } 116 }
117 117
118 function getDetailFromMessage(msg, rule, replacementRules) { 118 function getDetailFromMessage(msg, rule, replacementRules) {
119 if (!rule) { 119 if (!rule) {
120 rule = { 120 rule = {
121 pattern: " Detail:\\s*(.+?)\\s+Sisa [Ss]aldo", 121 pattern: " Detail(?: Tagihan)*:\\s*(.+?)\\s+Sisa [Ss]aldo",
122 match_idx: 1, 122 match_idx: 1,
123 } 123 }
124 } 124 }
125 125
126 return ( 126 return (
127 regexLooperReplace(extractFromMessage(msg, rule), replacementRules) || '' 127 regexLooperReplace(extractFromMessage(msg, rule), replacementRules) || ''
128 ).trim() || null; 128 ).trim() || null;
129 } 129 }
130 130
131 function splitPostpaidDetail(str) { 131 function splitPostpaidDetail(str) {
132 const retval = (str || '') 132 const retval = (str || '')
133 .split('/') 133 .split('/')
134 .map((item) => { 134 .map((item) => {
135 let [keyword, value] = item.split(/[:=]/); 135 let [keyword, value] = item.split(/[:=]/);
136 keyword = (keyword || '').trim() 136 keyword = (keyword || '').trim()
137 137
138 if (keyword === 'N') keyword = 'SN'; 138 if (keyword === 'N') keyword = 'SN';
139 139
140 return { 140 return {
141 keyword, 141 keyword,
142 value: (value || '').trim(), 142 value: (value || '').trim(),
143 }; 143 };
144 }) 144 })
145 .filter((item) => item.keyword.length && item.value.length); 145 .filter((item) => item.keyword.length && item.value.length);
146 return retval; 146 return retval;
147 } 147 }
148 148
149 function calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval) { 149 function calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval) {
150 const tagihanAsliKeyvalKeywords = (productKeyval && productKeyval.KEYWORD_TAGASLI) ? productKeyval.KEYWORD_TAGASLI.split(/ *, */) 150 const tagihanAsliKeyvalKeywords = (productKeyval && productKeyval.KEYWORD_TAGASLI) ? productKeyval.KEYWORD_TAGASLI.split(/ *, */)
151 : null; 151 : null;
152 152
153 const keywords = (tagihanAsliKeyvalKeywords && Array.isArray(tagihanAsliKeyvalKeywords) && tagihanAsliKeyvalKeywords.length && tagihanAsliKeyvalKeywords) 153 const keywords = (tagihanAsliKeyvalKeywords && Array.isArray(tagihanAsliKeyvalKeywords) && tagihanAsliKeyvalKeywords.length && tagihanAsliKeyvalKeywords)
154 || customKeywords 154 || customKeywords
155 || ['TAGIHANASLI', 'TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA']; 155 || ['TAGIHANASLI', 'TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA'];
156 156
157 let retval = 0; 157 let retval = 0;
158 let detailCount = (detailSplitted || []).length; 158 let detailCount = (detailSplitted || []).length;
159 159
160 for (let i = 0; i < detailCount; i += 1) { 160 for (let i = 0; i < detailCount; i += 1) {
161 const item = detailSplitted[i]; 161 const item = detailSplitted[i];
162 if (keywords.indexOf(item.keyword.toUpperCase()) >= 0) { 162 if (keywords.indexOf(item.keyword.toUpperCase()) >= 0) {
163 const value = (item.value || '').trim().replace(/^rp */i, '').replace(/\./g, ''); 163 const value = (item.value || '').trim().replace(/^rp */i, '').replace(/\./g, '');
164 retval += Number(value) || 0; 164 retval += Number(value) || 0;
165 } 165 }
166 } 166 }
167 167
168 return retval; 168 return retval;
169 } 169 }
170 170
171 function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, billCount) { 171 function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, billCount) {
172 if ( 172 if (
173 !productKeyval 173 !productKeyval
174 || !productKeyval.KEYWORD_TOTALTAG 174 || !productKeyval.KEYWORD_TOTALTAG
175 || (!productKeyval.KEYWORD_ADMINFEE && !productKeyval.ADMINFEE) 175 || (!productKeyval.KEYWORD_ADMINFEE && !productKeyval.ADMINFEE)
176 ) { 176 ) {
177 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 177 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
178 console.log(`=========== 0D299D07 using calculateBaseBillAmountAuto KEYWORD_TOTALTAG=${productKeyval.KEYWORD_TOTALTAG} KEYWORD_KEYWORD_ADMINFEE=${productKeyval.KEYWORD_ADMINFEE} KEYWORD_ADMINFEE=${productKeyval.ADMINFEE}`); 178 console.log(`=========== 0D299D07 using calculateBaseBillAmountAuto KEYWORD_TOTALTAG=${productKeyval.KEYWORD_TOTALTAG} KEYWORD_KEYWORD_ADMINFEE=${productKeyval.KEYWORD_ADMINFEE} KEYWORD_ADMINFEE=${productKeyval.ADMINFEE}`);
179 } 179 }
180 return calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval, billCount); 180 return calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval, billCount);
181 } 181 }
182 182
183 const billCountOrDefault = billCount || 1; 183 const billCountOrDefault = billCount || 1;
184 184
185 const totalTagihanKeyword = productKeyval.KEYWORD_TOTALTAG; 185 const totalTagihanKeyword = productKeyval.KEYWORD_TOTALTAG;
186 if (!totalTagihanKeyword) return 0; 186 if (!totalTagihanKeyword) return 0;
187 187
188 const totalTagihanItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === totalTagihanKeyword.toUpperCase()); 188 const totalTagihanItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === totalTagihanKeyword.toUpperCase());
189 const totalTagihanValue = Number( 189 const totalTagihanValue = Number(
190 ((totalTagihanItem && totalTagihanItem.value) || '') 190 ((totalTagihanItem && totalTagihanItem.value) || '')
191 .trim() 191 .trim()
192 .replace(/^rp */i, '') 192 .replace(/^rp */i, '')
193 .replace(/\./g, '') 193 .replace(/\./g, '')
194 ); 194 );
195 195
196 if (!totalTagihanValue) { 196 if (!totalTagihanValue) {
197 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 197 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
198 console.log('=========== MISSING totalTagihanValue D7282FA7'); 198 console.log('=========== MISSING totalTagihanValue D7282FA7');
199 } 199 }
200 200
201 return 0; 201 return 0;
202 } 202 }
203 203
204 const defaultAdminFee = Number(productKeyval.ADMINFEE || ''); 204 const defaultAdminFee = Number(productKeyval.ADMINFEE || '');
205 205
206 const adminFeeKeyword = productKeyval.KEYWORD_ADMINFEE; 206 const adminFeeKeyword = productKeyval.KEYWORD_ADMINFEE;
207 if (!adminFeeKeyword && defaultAdminFee) { 207 if (!adminFeeKeyword && defaultAdminFee) {
208 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 208 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
209 // eslint-disable-next-line no-console 209 // eslint-disable-next-line no-console
210 console.log('======== TOTAL TAGIHAN VALUE - (DEFAULT ADMIN FEE * BILL COUNT) D35E56AD') ; 210 console.log('======== TOTAL TAGIHAN VALUE - (DEFAULT ADMIN FEE * BILL COUNT) D35E56AD') ;
211 } 211 }
212 return totalTagihanValue - (defaultAdminFee * billCountOrDefault); 212 return totalTagihanValue - (defaultAdminFee * billCountOrDefault);
213 } 213 }
214 214
215 if (!adminFeeKeyword) { 215 if (!adminFeeKeyword) {
216 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 216 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
217 // eslint-disable-next-line no-console 217 // eslint-disable-next-line no-console
218 console.log('======== Missing adminFeeKeyword'); 218 console.log('======== Missing adminFeeKeyword');
219 } 219 }
220 220
221 return 0; 221 return 0;
222 } 222 }
223 223
224 const adminFeeItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === adminFeeKeyword.toUpperCase()); 224 const adminFeeItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === adminFeeKeyword.toUpperCase());
225 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 225 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
226 // eslint-disable-next-line no-console 226 // eslint-disable-next-line no-console
227 console.log(`======== adminFeeItem: ${adminFeeKeyword}`, adminFeeItem); 227 console.log(`======== adminFeeItem: ${adminFeeKeyword}`, adminFeeItem);
228 } 228 }
229 const adminFeeValue = Number((adminFeeItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); 229 const adminFeeValue = Number((adminFeeItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, ''));
230 if (!adminFeeValue) return 0; 230 if (!adminFeeValue) return 0;
231 231
232 return totalTagihanValue - (adminFeeValue * billCountOrDefault); 232 return totalTagihanValue - (adminFeeValue * billCountOrDefault);
233 } 233 }
234 234
235 /** 235 /**
236 * get bill count using number of occurence of spliter 236 * get bill count using number of occurence of spliter
237 * @date 2020-10-02 237 * @date 2020-10-02
238 * @param {Object} detailSplitted 238 * @param {Object} detailSplitted
239 * @param {string} customKeyword 239 * @param {string} customKeyword
240 * @returns {number} 240 * @returns {number}
241 */ 241 */
242 function getBillCountUsingCounter(detailSplitted, customKeyword) { 242 function getBillCountUsingCounter(detailSplitted, customKeyword) {
243 if (!detailSplitted) return 0; 243 if (!detailSplitted) return 0;
244 244
245 return ((customKeyword && detailSplitted[customKeyword]) || '').split(',').length; 245 return ((customKeyword && detailSplitted[customKeyword]) || '').split(',').length;
246 } 246 }
247 247
248 /** 248 /**
249 * get bill count from splitted detail 249 * get bill count from splitted detail
250 * @date 2020-10-02 250 * @date 2020-10-02
251 * @param {Object} detailSplitted 251 * @param {Object} detailSplitted
252 * @param {string} [customKeyword] 252 * @param {string} [customKeyword]
253 * @returns {number} 253 * @returns {number}
254 */ 254 */
255 function getBillCount(detailSplitted, customKeyword) { 255 function getBillCount(detailSplitted, customKeyword) {
256 if (!detailSplitted) return 0; 256 if (!detailSplitted) return 0;
257 257
258 return Number( 258 return Number(
259 ( 259 (
260 (customKeyword && detailSplitted[customKeyword]) 260 (customKeyword && detailSplitted[customKeyword])
261 || detailSplitted['JMLBLN'] 261 || detailSplitted['JMLBLN']
262 || '' 262 || ''
263 ).replace(/\D+$/g, '') 263 ).replace(/\D+$/g, '')
264 ) || 0; 264 ) || 0;
265 } 265 }
266 266
267 exports.getRcFromMessage = getRcFromMessage; 267 exports.getRcFromMessage = getRcFromMessage;
268 exports.getPriceFromMessage = getPriceFromMessage; 268 exports.getPriceFromMessage = getPriceFromMessage;
269 exports.getSnFromMessage = getSnFromMessage; 269 exports.getSnFromMessage = getSnFromMessage;
270 exports.getBalanceFromMessage = getBalanceFromMessage; 270 exports.getBalanceFromMessage = getBalanceFromMessage;
271 exports.isInquiryResponseMessage = isInquiryResponseMessage; 271 exports.isInquiryResponseMessage = isInquiryResponseMessage;
272 exports.isPaymentResponseMessage = isPaymentResponseMessage; 272 exports.isPaymentResponseMessage = isPaymentResponseMessage;
273 exports.isPostpaidResponseMessage = isPostpaidResponseMessage; 273 exports.isPostpaidResponseMessage = isPostpaidResponseMessage;
274 exports.getDetailFromMessage = getDetailFromMessage; 274 exports.getDetailFromMessage = getDetailFromMessage;
275 exports.splitPostpaidDetail = splitPostpaidDetail; 275 exports.splitPostpaidDetail = splitPostpaidDetail;
276 exports.calculateBaseBillAmount = calculateBaseBillAmount; 276 exports.calculateBaseBillAmount = calculateBaseBillAmount;
277 exports.getBillCount = getBillCount; 277 exports.getBillCount = getBillCount;
278 exports.getBillCountUsingCounter = getBillCountUsingCounter; 278 exports.getBillCountUsingCounter = getBillCountUsingCounter;