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