Commit 332e2847cfbdaecc1767220170ef18812f73787c

Authored by Adhidarma Hadiwinoto
1 parent f59df686cc
Exists in master

Perbaikan validitas totalTagihanItem

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

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 calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval) { 146 function calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval) {
147 const tagihanAsliKeyvalKeywords = (productKeyval && productKeyval.KEYWORD_TAGASLI) ? productKeyval.KEYWORD_TAGASLI.split(/ *, */) 147 const tagihanAsliKeyvalKeywords = (productKeyval && productKeyval.KEYWORD_TAGASLI) ? productKeyval.KEYWORD_TAGASLI.split(/ *, */)
148 : null; 148 : null;
149 149
150 const keywords = (tagihanAsliKeyvalKeywords && Array.isArray(tagihanAsliKeyvalKeywords) && tagihanAsliKeyvalKeywords.length && tagihanAsliKeyvalKeywords) 150 const keywords = (tagihanAsliKeyvalKeywords && Array.isArray(tagihanAsliKeyvalKeywords) && tagihanAsliKeyvalKeywords.length && tagihanAsliKeyvalKeywords)
151 || customKeywords 151 || customKeywords
152 || ['TAGIHANASLI', 'TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA']; 152 || ['TAGIHANASLI', 'TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA'];
153 153
154 let retval = 0; 154 let retval = 0;
155 let detailCount = (detailSplitted || []).length; 155 let detailCount = (detailSplitted || []).length;
156 156
157 for (let i = 0; i < detailCount; i += 1) { 157 for (let i = 0; i < detailCount; i += 1) {
158 const item = detailSplitted[i]; 158 const item = detailSplitted[i];
159 if (keywords.indexOf(item.keyword.toUpperCase()) >= 0) { 159 if (keywords.indexOf(item.keyword.toUpperCase()) >= 0) {
160 const value = (item.value || '').trim().replace(/^rp */i, '').replace(/\./g, ''); 160 const value = (item.value || '').trim().replace(/^rp */i, '').replace(/\./g, '');
161 retval += Number(value) || 0; 161 retval += Number(value) || 0;
162 } 162 }
163 } 163 }
164 164
165 return retval; 165 return retval;
166 } 166 }
167 167
168 function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, billCount) { 168 function calculateBaseBillAmount(detailSplitted, customKeywords, productKeyval, billCount) {
169 if ( 169 if (
170 !productKeyval 170 !productKeyval
171 || !productKeyval.KEYWORD_TOTALTAG 171 || !productKeyval.KEYWORD_TOTALTAG
172 || !productKeyval.KEYWORD_ADMINFEE 172 || !productKeyval.KEYWORD_ADMINFEE
173 ) { 173 ) {
174 return calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval, billCount); 174 return calculateBaseBillAmountAuto(detailSplitted, customKeywords, productKeyval, billCount);
175 } 175 }
176 176
177 const billCountOrDefault = billCount || 1; 177 const billCountOrDefault = billCount || 1;
178 178
179 const totalTagihanKeyword = productKeyval.KEYWORD_TOTALTAG; 179 const totalTagihanKeyword = productKeyval.KEYWORD_TOTALTAG;
180 if (!totalTagihanKeyword) return 0; 180 if (!totalTagihanKeyword) return 0;
181 181
182 const totalTagihanItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === totalTagihanKeyword.toUpperCase()); 182 const totalTagihanItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === totalTagihanKeyword.toUpperCase());
183 const totalTagihanValue = Number((totalTagihanItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); 183 const totalTagihanValue = Number(
184 ((totalTagihanItem && totalTagihanItem.value) || '')
185 .trim()
186 .replace(/^rp */i, '')
187 .replace(/\./g, '')
188 );
189
184 if (!totalTagihanValue) { 190 if (!totalTagihanValue) {
185 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 191 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
186 // eslint-disable-next-line no-console 192 // eslint-disable-next-line no-console
187 console.log('=========== MISSING totalTagihanValue D7282FA7'); 193 console.log('=========== MISSING totalTagihanValue D7282FA7');
188 } 194 }
189 195
190 return 0; 196 return 0;
191 } 197 }
192 198
193 const defaultAdminFee = Number(productKeyval.ADMINFEE || ''); 199 const defaultAdminFee = Number(productKeyval.ADMINFEE || '');
194 200
195 const adminFeeKeyword = productKeyval.KEYWORD_ADMINFEE; 201 const adminFeeKeyword = productKeyval.KEYWORD_ADMINFEE;
196 if (!adminFeeKeyword && defaultAdminFee) { 202 if (!adminFeeKeyword && defaultAdminFee) {
197 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 203 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
198 // eslint-disable-next-line no-console 204 // eslint-disable-next-line no-console
199 console.log('======== TOTAL TAGIHAN VALUE - (DEFAULT ADMIN FEE * BILL COUNT) D35E56AD') ; 205 console.log('======== TOTAL TAGIHAN VALUE - (DEFAULT ADMIN FEE * BILL COUNT) D35E56AD') ;
200 } 206 }
201 return totalTagihanValue - (defaultAdminFee * billCountOrDefault); 207 return totalTagihanValue - (defaultAdminFee * billCountOrDefault);
202 } 208 }
203 209
204 if (!adminFeeKeyword) { 210 if (!adminFeeKeyword) {
205 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 211 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
206 // eslint-disable-next-line no-console 212 // eslint-disable-next-line no-console
207 console.log('======== Missing adminFeeKeyword'); 213 console.log('======== Missing adminFeeKeyword');
208 } 214 }
209 215
210 return 0; 216 return 0;
211 } 217 }
212 218
213 const adminFeeItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === adminFeeKeyword.toUpperCase()); 219 const adminFeeItem = detailSplitted.find((item) => (item.keyword || '').toUpperCase() === adminFeeKeyword.toUpperCase());
214 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) { 220 if (process.env.DEBUG_IRS_CALCULATE_BASE_BILL_AMOUNT) {
215 // eslint-disable-next-line no-console 221 // eslint-disable-next-line no-console
216 console.log(`======== adminFeeItem: ${adminFeeKeyword}`, adminFeeItem); 222 console.log(`======== adminFeeItem: ${adminFeeKeyword}`, adminFeeItem);
217 } 223 }
218 const adminFeeValue = Number((adminFeeItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, '')); 224 const adminFeeValue = Number((adminFeeItem.value || '').trim().replace(/^rp */i, '').replace(/\./g, ''));
219 if (!adminFeeValue) return 0; 225 if (!adminFeeValue) return 0;
220 226
221 return totalTagihanValue - (adminFeeValue * billCountOrDefault); 227 return totalTagihanValue - (adminFeeValue * billCountOrDefault);
222 } 228 }
223 229
224 230
225 function getBillCount(msg) { 231 function getBillCount(msg) {
226 const matches = (msg || '').match(/JMLBLN:(\d+)BLN/); 232 const matches = (msg || '').match(/JMLBLN:(\d+)BLN/);
227 return (matches && Number(matches[1])) || null; 233 return (matches && Number(matches[1])) || null;
228 } 234 }
229 235
230 exports.getRcFromMessage = getRcFromMessage; 236 exports.getRcFromMessage = getRcFromMessage;
231 exports.getPriceFromMessage = getPriceFromMessage; 237 exports.getPriceFromMessage = getPriceFromMessage;
232 exports.getSnFromMessage = getSnFromMessage; 238 exports.getSnFromMessage = getSnFromMessage;
233 exports.getBalanceFromMessage = getBalanceFromMessage; 239 exports.getBalanceFromMessage = getBalanceFromMessage;
234 exports.isInquiryResponseMessage = isInquiryResponseMessage; 240 exports.isInquiryResponseMessage = isInquiryResponseMessage;
235 exports.isPaymentResponseMessage = isPaymentResponseMessage; 241 exports.isPaymentResponseMessage = isPaymentResponseMessage;
236 exports.isPostpaidResponseMessage = isPostpaidResponseMessage; 242 exports.isPostpaidResponseMessage = isPostpaidResponseMessage;
237 exports.getDetailFromMessage = getDetailFromMessage; 243 exports.getDetailFromMessage = getDetailFromMessage;
238 exports.splitPostpaidDetail = splitPostpaidDetail; 244 exports.splitPostpaidDetail = splitPostpaidDetail;
239 exports.calculateBaseBillAmount = calculateBaseBillAmount; 245 exports.calculateBaseBillAmount = calculateBaseBillAmount;
240 exports.getBillCount = getBillCount; 246 exports.getBillCount = getBillCount;
241 247