Commit 86c8c00efb8cc01a3eca0a1bc898bb0bb01abca9

Authored by Adhidarma Hadiwinoto
1 parent 4cc6eab525
Exists in master

TOTALTAGIHAN untuk FIF

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

1 "use strict"; 1 "use strict";
2 2
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 function isInquiryResponseMessage(msg) { 6 function isInquiryResponseMessage(msg) {
7 return (msg || '').indexOf('CEK TAGIHAN') >= 0; 7 return (msg || '').indexOf('CEK TAGIHAN') >= 0;
8 } 8 }
9 9
10 function isPaymentResponseMessage(msg) { 10 function isPaymentResponseMessage(msg) {
11 return (msg || '').indexOf('BAYAR TAGIHAN') >= 0; 11 return (msg || '').indexOf('BAYAR TAGIHAN') >= 0;
12 } 12 }
13 13
14 function isPostpaidResponseMessage(msg) { 14 function isPostpaidResponseMessage(msg) {
15 return isInquiryResponseMessage(msg) || isPaymentResponseMessage(msg); 15 return isInquiryResponseMessage(msg) || isPaymentResponseMessage(msg);
16 } 16 }
17 17
18 function getRcFromMessage(msg, customRc) { 18 function getRcFromMessage(msg, customRc) {
19 let rc; 19 let rc;
20 if (customRc && Array.isArray(customRc) && customRc.length) { 20 if (customRc && Array.isArray(customRc) && customRc.length) {
21 rc = rcFromMsg(msg, customRc); 21 rc = rcFromMsg(msg, customRc);
22 } 22 }
23 23
24 if (!rc) { 24 if (!rc) {
25 rc = rcFromMsg(msg, organicRc); 25 rc = rcFromMsg(msg, organicRc);
26 } 26 }
27 27
28 return rc; 28 return rc;
29 } 29 }
30 30
31 function getPriceFromMessage(msg, rule) { 31 function getPriceFromMessage(msg, rule) {
32 if (typeof msg !== 'string') { 32 if (typeof msg !== 'string') {
33 return; 33 return;
34 } 34 }
35 35
36 if (process.env.DEBUG_IRS && !rule) { 36 if (process.env.DEBUG_IRS && !rule) {
37 console.log('** IRS.getPriceFromMessage no rule'); // eslint-disable-line no-console 37 console.log('** IRS.getPriceFromMessage no rule'); // eslint-disable-line no-console
38 } 38 }
39 39
40 if (process.env.DEBUG_IRS && rule) { 40 if (process.env.DEBUG_IRS && rule) {
41 console.log('** IRS.getPriceFromMessage rule: ' + JSON.stringify(rule, null, 2)); // eslint-disable-line no-console 41 console.log('** IRS.getPriceFromMessage rule: ' + JSON.stringify(rule, null, 2)); // eslint-disable-line no-console
42 } 42 }
43 43
44 //const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "Harga: ([\\d\\.]+?) "; 44 //const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "Harga: ([\\d\\.]+?) ";
45 const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "\\d+\\s+-\\s+([\\d,\\.]+)\\s+="; 45 const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "\\d+\\s+-\\s+([\\d,\\.]+)\\s+=";
46 const match_idx = (rule && typeof rule.match_idx === 'number') ? rule.match_idx : 1; 46 const match_idx = (rule && typeof rule.match_idx === 'number') ? rule.match_idx : 1;
47 47
48 const re = new RegExp(pattern); 48 const re = new RegExp(pattern);
49 const matches = msg.match(re); 49 const matches = msg.match(re);
50 if (process.env.DEBUG_IRS) { 50 if (process.env.DEBUG_IRS) {
51 console.log('** IRS.getPriceFromMessage msg: "' + msg + '" active_pattern: "' + pattern + '" active_match_idx: ' + match_idx); // eslint-disable-line no-console 51 console.log('** IRS.getPriceFromMessage msg: "' + msg + '" active_pattern: "' + pattern + '" active_match_idx: ' + match_idx); // eslint-disable-line no-console
52 console.log('** IRS.getPriceFromMessage matches:\n' + JSON.stringify(matches)); // eslint-disable-line no-console 52 console.log('** IRS.getPriceFromMessage matches:\n' + JSON.stringify(matches)); // eslint-disable-line no-console
53 } 53 }
54 54
55 if (matches && matches[match_idx]) { 55 if (matches && matches[match_idx]) {
56 const result = Number(matches[match_idx].replace(/\./g, '')); 56 const result = Number(matches[match_idx].replace(/\./g, ''));
57 if (process.env.DEBUG_IRS) { 57 if (process.env.DEBUG_IRS) {
58 console.log('** IRS.getPriceFromMessage SUPPLIER-PRICE: ' + result); // eslint-disable-line no-console 58 console.log('** IRS.getPriceFromMessage SUPPLIER-PRICE: ' + result); // eslint-disable-line no-console
59 } 59 }
60 return result; 60 return result;
61 } 61 }
62 } 62 }
63 63
64 function extractFromMessage(msg, rule) { 64 function extractFromMessage(msg, rule) {
65 if (typeof msg !== 'string') { return; } 65 if (typeof msg !== 'string') { return; }
66 66
67 if (!rule) { return; } 67 if (!rule) { return; }
68 68
69 if (typeof rule !== 'object') { 69 if (typeof rule !== 'object') {
70 return; 70 return;
71 } 71 }
72 72
73 rule.match_idx = Number(rule.match_idx); 73 rule.match_idx = Number(rule.match_idx);
74 74
75 if (!rule.match_idx) { 75 if (!rule.match_idx) {
76 rule.match_idx = 1; 76 rule.match_idx = 1;
77 } 77 }
78 78
79 const re = new RegExp(rule.pattern); 79 const re = new RegExp(rule.pattern);
80 const matches = msg.match(re); 80 const matches = msg.match(re);
81 if (matches && matches[rule.match_idx] && typeof matches[rule.match_idx] === 'string') { 81 if (matches && matches[rule.match_idx] && typeof matches[rule.match_idx] === 'string') {
82 return matches[rule.match_idx]; 82 return matches[rule.match_idx];
83 } 83 }
84 } 84 }
85 85
86 function getSnFromMessage(msg, rule) { 86 function getSnFromMessage(msg, rule) {
87 if (!rule) { 87 if (!rule) {
88 rule = { 88 rule = {
89 pattern: "[: ]S/*N:\\s*(.+?)\\s", 89 pattern: "[: ]S/*N:\\s*(.+?)\\s",
90 match_idx: 1 90 match_idx: 1
91 } 91 }
92 } 92 }
93 93
94 let sn = extractFromMessage(msg, rule); 94 let sn = extractFromMessage(msg, rule);
95 if (!sn || typeof sn !== 'string') { return; } 95 if (!sn || typeof sn !== 'string') { return; }
96 96
97 return sn.toUpperCase().replace(/[^a-zA-Z0-9:,/]/g, '-').replace(/-+/g, '-').replace(/-*\/-*/g, '/').replace(/^-+/, '').replace(/-+$/, ''); 97 return sn.toUpperCase().replace(/[^a-zA-Z0-9:,/]/g, '-').replace(/-+/g, '-').replace(/-*\/-*/g, '/').replace(/^-+/, '').replace(/-+$/, '');
98 } 98 }
99 99
100 function getBalanceFromMessage(msg, rule) { 100 function getBalanceFromMessage(msg, rule) {
101 if (!rule) { 101 if (!rule) {
102 rule = { 102 rule = {
103 pattern: "Sisa Saldo: .+? = ([\\d\\.]+) ", 103 pattern: "Sisa Saldo: .+? = ([\\d\\.]+) ",
104 match_idx: 1 104 match_idx: 1
105 } 105 }
106 } 106 }
107 107
108 let result = extractFromMessage(msg, rule); 108 let result = extractFromMessage(msg, rule);
109 if (!result || typeof result !== 'string') { return; } 109 if (!result || typeof result !== 'string') { return; }
110 110
111 return Number(result.replace(/\./g, '')); 111 return Number(result.replace(/\./g, ''));
112 } 112 }
113 113
114 function getDetailFromMessage(msg, rule) { 114 function getDetailFromMessage(msg, rule) {
115 if (!rule) { 115 if (!rule) {
116 rule = { 116 rule = {
117 pattern: " Detail:\\s*(.+?)\\s+Sisa [Ss]aldo", 117 pattern: " Detail:\\s*(.+?)\\s+Sisa [Ss]aldo",
118 match_idx: 1, 118 match_idx: 1,
119 } 119 }
120 } 120 }
121 121
122 let result = extractFromMessage(msg, rule); 122 let result = extractFromMessage(msg, rule);
123 return (result || '').trim(); 123 return (result || '').trim();
124 } 124 }
125 125
126 function splitPostpaidDetail(str) { 126 function splitPostpaidDetail(str) {
127 const retval = (str || '') 127 const retval = (str || '')
128 .split('/') 128 .split('/')
129 .map((item) => { 129 .map((item) => {
130 let [keyword, value] = item.split(':'); 130 let [keyword, value] = item.split(':');
131 keyword = (keyword || '').trim() 131 keyword = (keyword || '').trim()
132 132
133 if (keyword === 'N') keyword = 'SN'; 133 if (keyword === 'N') keyword = 'SN';
134 134
135 return { 135 return {
136 keyword, 136 keyword,
137 value: (value || '').trim(), 137 value: (value || '').trim(),
138 }; 138 };
139 }) 139 })
140 .filter((item) => item.keyword.length && item.value.length); 140 .filter((item) => item.keyword.length && item.value.length);
141 return retval; 141 return retval;
142 } 142 }
143 143
144 function calculateBaseBillAmount(detailSplitted, customKeywords) { 144 function calculateBaseBillAmount(detailSplitted, customKeywords) {
145 const keywords = customKeywords || ['TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA']; 145 const keywords = customKeywords || ['TAGIHAN', 'DENDA', 'RPPREMI', 'BIAYA', 'TOTALTAGIHAN'];
146 146
147 let retval = 0; 147 let retval = 0;
148 let detailCount = (detailSplitted || []).length; 148 let detailCount = (detailSplitted || []).length;
149 149
150 for (let i = 0; i < detailCount; i += 1) { 150 for (let i = 0; i < detailCount; i += 1) {
151 const item = detailSplitted[i]; 151 const item = detailSplitted[i];
152 if (keywords.indexOf(item.keyword.toUpperCase()) >= 0) { 152 if (keywords.indexOf(item.keyword.toUpperCase()) >= 0) {
153 retval += Number(item.value) || 0; 153 const value = (item.value || '').trim().replace(/^rp */i, '').replace(/\./g, '');
154 retval += Number(value) || 0;
154 } 155 }
155 } 156 }
156 157
157 return retval; 158 return retval;
158 } 159 }
159 160
160 function getBillCount(msg) { 161 function getBillCount(msg) {
161 const matches = (msg || '').match(/JMLBLN:(\d+)BLN/); 162 const matches = (msg || '').match(/JMLBLN:(\d+)BLN/);
162 return (matches && Number(matches[1])) || null; 163 return (matches && Number(matches[1])) || null;
163 } 164 }
164 165
165 exports.getRcFromMessage = getRcFromMessage; 166 exports.getRcFromMessage = getRcFromMessage;
166 exports.getPriceFromMessage = getPriceFromMessage; 167 exports.getPriceFromMessage = getPriceFromMessage;
167 exports.getSnFromMessage = getSnFromMessage; 168 exports.getSnFromMessage = getSnFromMessage;
168 exports.getBalanceFromMessage = getBalanceFromMessage; 169 exports.getBalanceFromMessage = getBalanceFromMessage;
169 exports.isInquiryResponseMessage = isInquiryResponseMessage; 170 exports.isInquiryResponseMessage = isInquiryResponseMessage;
170 exports.isPaymentResponseMessage = isPaymentResponseMessage; 171 exports.isPaymentResponseMessage = isPaymentResponseMessage;
171 exports.isPostpaidResponseMessage = isPostpaidResponseMessage; 172 exports.isPostpaidResponseMessage = isPostpaidResponseMessage;
172 exports.getDetailFromMessage = getDetailFromMessage; 173 exports.getDetailFromMessage = getDetailFromMessage;
173 exports.splitPostpaidDetail = splitPostpaidDetail; 174 exports.splitPostpaidDetail = splitPostpaidDetail;
174 exports.calculateBaseBillAmount = calculateBaseBillAmount; 175 exports.calculateBaseBillAmount = calculateBaseBillAmount;
175 exports.getBillCount = getBillCount; 176 exports.getBillCount = getBillCount;
176 177