Commit 636a63c5644729707765908252118e11469c87e0

Authored by Adhidarma Hadiwinoto
1 parent d518d04536
Exists in master

splitPostpaidDetail mendukung titik dua maupun sama dengan

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

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