mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 17:46:28 +00:00
Compare commits
38 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 375d69c955 | |||
| 7890aed029 | |||
| 8152326743 | |||
| 30d8e70da2 | |||
| 9a4d741c05 | |||
| 18787a3605 | |||
| 69fe9b97d8 | |||
| 3027dd30b3 | |||
| 9feed0d542 | |||
| b556a40dba | |||
| e1af54e851 | |||
| 23fadc4cb0 | |||
| fefa11c544 | |||
| ce8239ca38 | |||
| c6f397d352 | |||
| 81addd40e8 | |||
| fef18ff448 | |||
| d84bc6941e | |||
| 676553409b | |||
| 72a94891ed | |||
| 84be7587a8 | |||
| 8080b89c46 | |||
| 6dbd9b6246 | |||
| 5bafb81d8d | |||
| 1dd7860c0b | |||
| 1cf06d1fdd | |||
| be2cedc869 | |||
| f5f75381ca | |||
| 5cbdccca11 | |||
| fd386788f8 | |||
| 6ddf225485 | |||
| 1645aa54aa | |||
| 797bf97bf8 | |||
| a7f5c8c8e0 | |||
| d2d44f3ba9 | |||
| 4dccbf20f5 | |||
| b888951ee3 | |||
| 87dedd241a |
+153
-25
@@ -1,4 +1,6 @@
|
||||
const CryptoJS = require('crypto-js');
|
||||
const { randomUUID } = require('crypto');
|
||||
const knex = require('../../infrastructure/database/knex');
|
||||
|
||||
// ENCRYPT TEXT
|
||||
const encryptText = (text) => {
|
||||
@@ -64,41 +66,35 @@ function generateXMLRequest(urn, headers, items) {
|
||||
xmlItem += `<item>
|
||||
<!--Optional:-->
|
||||
<ITEMNO_ACC>${String(index + 1).padStart(
|
||||
10,
|
||||
'0'
|
||||
)}</ITEMNO_ACC>
|
||||
10,
|
||||
'0'
|
||||
)}</ITEMNO_ACC>
|
||||
<!--Optional:-->
|
||||
<CUSTOMER>${
|
||||
item.c_account_code_topup
|
||||
? item.c_account_code_topup
|
||||
: ''
|
||||
}</CUSTOMER>
|
||||
<CUSTOMER>${item.c_account_code_topup
|
||||
? item.c_account_code_topup
|
||||
: ''
|
||||
}</CUSTOMER>
|
||||
<!--Optional:-->
|
||||
<GL_ACCOUNT>${
|
||||
item.c_account_topup ? item.c_account_topup : ''
|
||||
}</GL_ACCOUNT>
|
||||
<GL_ACCOUNT>${item.c_account_topup ? item.c_account_topup : ''
|
||||
}</GL_ACCOUNT>
|
||||
<!--Optional:-->
|
||||
<BUS_AREA>${item.ba ? item.ba : ''}</BUS_AREA>
|
||||
<!--Optional:-->
|
||||
<AMT_DOCCUR>${
|
||||
item.amount ? item.amount : ''
|
||||
}</AMT_DOCCUR>
|
||||
<AMT_DOCCUR>${item.amount ? item.amount : ''
|
||||
}</AMT_DOCCUR>
|
||||
<!--Optional:-->
|
||||
<BLINE_DATE>${
|
||||
item.posting_date ? item.posting_date : ''
|
||||
}</BLINE_DATE>
|
||||
<BLINE_DATE>${item.posting_date ? item.posting_date : ''
|
||||
}</BLINE_DATE>
|
||||
<!--Optional:-->
|
||||
<COSTCENTER></COSTCENTER>
|
||||
<!--Optional:-->
|
||||
<PROFITCENTER></PROFITCENTER>
|
||||
<!--Optional:-->
|
||||
<ALLOC_NMBR>${
|
||||
item.c_invoice_no ? item.c_invoice_no : ''
|
||||
}</ALLOC_NMBR>
|
||||
<ALLOC_NMBR>${item.c_invoice_no ? item.c_invoice_no : ''
|
||||
}</ALLOC_NMBR>
|
||||
<!--Optional:-->
|
||||
<ITEM_TEXT>${
|
||||
item.item_text ? item.item_text : ''
|
||||
}</ITEM_TEXT>
|
||||
<ITEM_TEXT>${item.item_text ? item.item_text : ''
|
||||
}</ITEM_TEXT>
|
||||
<!--Optional:-->
|
||||
<DE_CRE_IND>${item.de_cre}</DE_CRE_IND>
|
||||
</item>`;
|
||||
@@ -286,7 +282,7 @@ function getXMLData(response) {
|
||||
resSAP.message =
|
||||
'[SAP] ' +
|
||||
soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'][
|
||||
'MESSAGE_1'
|
||||
'MESSAGE_1'
|
||||
];
|
||||
} else {
|
||||
// resSAP.data.ITEM = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item']
|
||||
@@ -298,7 +294,7 @@ function getXMLData(response) {
|
||||
let message =
|
||||
'[SAP] ' +
|
||||
soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'][
|
||||
indexMessage
|
||||
indexMessage
|
||||
]['MESSAGE_1'];
|
||||
|
||||
resSAP.message = message;
|
||||
@@ -313,6 +309,136 @@ function getXMLData(response) {
|
||||
return resSAP;
|
||||
}
|
||||
|
||||
async function startScheduler(schedulerCode) {
|
||||
return await knex.transaction(async (trx) => {
|
||||
|
||||
const job = await knex('log.t_monitoring_scheduler_history')
|
||||
.transacting(trx)
|
||||
.where('c_scheduler_code', schedulerCode)
|
||||
.where('c_status', 'idle')
|
||||
.whereRaw(`
|
||||
d_run_datetime BETWEEN NOW() - INTERVAL '3 minutes'
|
||||
AND NOW() + INTERVAL '1 minute'
|
||||
`)
|
||||
.orderBy('d_run_datetime', 'desc')
|
||||
.forUpdate()
|
||||
.first();
|
||||
|
||||
let result;
|
||||
|
||||
if (!job) {
|
||||
const master = await knex('log.t_monitoring_master_scheduler_name as mn')
|
||||
.join('log.t_monitoring_master_scheduler_time as mt', 'mn.i_scheduler_name', 'mt.i_scheduler_name')
|
||||
.transacting(trx)
|
||||
.select(
|
||||
'mn.i_scheduler_name',
|
||||
'mn.c_scheduler_name',
|
||||
'mn.c_scheduler_code',
|
||||
'mn.c_category',
|
||||
'mt.i_scheduler_time'
|
||||
)
|
||||
.where('mn.c_scheduler_code', schedulerCode)
|
||||
.first();
|
||||
|
||||
if (!master) {
|
||||
throw new Error('Master scheduler not found');
|
||||
}
|
||||
|
||||
const token = randomUUID();
|
||||
|
||||
const inserted = await knex('log.t_monitoring_scheduler_history')
|
||||
.transacting(trx)
|
||||
.insert({
|
||||
i_scheduler_name: master.i_scheduler_name,
|
||||
i_scheduler_time: master.i_scheduler_time,
|
||||
c_scheduler_name: master.c_scheduler_name,
|
||||
d_run_datetime: knex.fn.now(),
|
||||
c_status: 'processing',
|
||||
d_started_at: knex.fn.now(),
|
||||
c_execution_token: token,
|
||||
c_category: master.c_category,
|
||||
c_type: 'AUTO',
|
||||
d_created: knex.fn.now(),
|
||||
i_create: 0,
|
||||
c_create_username: 'system',
|
||||
c_create_fullname: 'system',
|
||||
c_scheduler_code: master.c_scheduler_code
|
||||
})
|
||||
.returning('*');
|
||||
|
||||
result = inserted[0];
|
||||
|
||||
} else {
|
||||
const token = randomUUID();
|
||||
|
||||
const updated = await knex('log.t_monitoring_scheduler_history')
|
||||
.transacting(trx)
|
||||
.where('i_scheduler_history', job.i_scheduler_history)
|
||||
.update({
|
||||
c_status: 'processing',
|
||||
d_started_at: knex.fn.now(),
|
||||
c_execution_token: token
|
||||
})
|
||||
.returning('*');
|
||||
|
||||
result = updated[0];
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
async function finishScheduler(token = null, schedulerCode = null) {
|
||||
return await knex.transaction(async (trx) => {
|
||||
|
||||
let result = null;
|
||||
|
||||
if (token) {
|
||||
const updated = await knex('log.t_monitoring_scheduler_history')
|
||||
.transacting(trx)
|
||||
.where('c_execution_token', token)
|
||||
.where('c_status', 'processing')
|
||||
.update({
|
||||
c_status: 'finished',
|
||||
d_finished_at: knex.fn.now()
|
||||
})
|
||||
.returning('*');
|
||||
|
||||
if (updated.length > 0) {
|
||||
result = updated[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (!result && schedulerCode) {
|
||||
|
||||
const job = await knex('log.t_monitoring_scheduler_history')
|
||||
.transacting(trx)
|
||||
.where('c_scheduler_code', schedulerCode)
|
||||
.where('c_status', 'processing')
|
||||
.whereNotNull('d_started_at')
|
||||
.whereRaw(`d_started_at >= NOW() - INTERVAL '5 minutes'`)
|
||||
.orderBy('d_started_at', 'desc')
|
||||
.forUpdate()
|
||||
.first();
|
||||
|
||||
if (job) {
|
||||
const updated = await knex('log.t_monitoring_scheduler_history')
|
||||
.transacting(trx)
|
||||
.where('i_scheduler_history', job.i_scheduler_history)
|
||||
.update({
|
||||
c_status: 'finished',
|
||||
d_finished_at: knex.fn.now()
|
||||
})
|
||||
.returning('*');
|
||||
|
||||
result = updated[0];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
encryptText,
|
||||
decryptText,
|
||||
@@ -322,4 +448,6 @@ module.exports = {
|
||||
generateXMLRequest,
|
||||
postingSAP,
|
||||
getXMLData,
|
||||
startScheduler,
|
||||
finishScheduler,
|
||||
};
|
||||
|
||||
@@ -57,6 +57,10 @@ const apiGenerateCahsIn = async (req, res) => {
|
||||
`${uniqueCode} REQUEST generate cashin: ${JSON.stringify(req.body)}`
|
||||
);
|
||||
|
||||
const schedulerCode = 'GENERATECASHINONLINETOPUP';
|
||||
const startData = await helper.startScheduler(schedulerCode);
|
||||
const token = startData.c_execution_token ?? null;
|
||||
|
||||
// check validator
|
||||
const err = validationResult(req, res);
|
||||
if (!err.isEmpty()) {
|
||||
@@ -82,6 +86,8 @@ const apiGenerateCahsIn = async (req, res) => {
|
||||
data: cashIn,
|
||||
};
|
||||
|
||||
const finishData = await helper.finishScheduler(token, schedulerCode);
|
||||
|
||||
return res.status(200).json(result);
|
||||
} catch (error) {
|
||||
// create log
|
||||
@@ -165,6 +171,10 @@ const apiGenerateCashInSalesInternal = async (req, res) => {
|
||||
`${uniqueCode} REQUEST generate cashin sales internal: ${JSON.stringify(req.body)}`
|
||||
);
|
||||
|
||||
const schedulerCode = 'GENERATECASHINSALESSTATIONCASHLESS';
|
||||
const startData = await helper.startScheduler(schedulerCode);
|
||||
const token = startData.c_execution_token ?? null;
|
||||
|
||||
// check validator
|
||||
const err = validationResult(req, res);
|
||||
if (!err.isEmpty()) {
|
||||
@@ -190,6 +200,8 @@ const apiGenerateCashInSalesInternal = async (req, res) => {
|
||||
data: cashIn,
|
||||
};
|
||||
|
||||
const finishData = await helper.finishScheduler(token, schedulerCode);
|
||||
|
||||
return res.status(200).json(result);
|
||||
} catch (error) {
|
||||
// create log
|
||||
@@ -219,6 +231,10 @@ const apiGenerateCashInTopUpCashlessInternal = async (req, res) => {
|
||||
`${uniqueCode} REQUEST generate cashin topup cashless internal: ${JSON.stringify(req.body)}`
|
||||
);
|
||||
|
||||
const schedulerCode = 'GENERATECASHINTOPUPSTATIONCASHLESS';
|
||||
const startData = await helper.startScheduler(schedulerCode);
|
||||
const token = startData.c_execution_token ?? null;
|
||||
|
||||
// check validator
|
||||
const err = validationResult(req, res);
|
||||
if (!err.isEmpty()) {
|
||||
@@ -244,6 +260,8 @@ const apiGenerateCashInTopUpCashlessInternal = async (req, res) => {
|
||||
data: cashIn,
|
||||
};
|
||||
|
||||
const finishData = await helper.finishScheduler(token, schedulerCode);
|
||||
|
||||
return res.status(200).json(result);
|
||||
} catch (error) {
|
||||
// create log
|
||||
@@ -323,7 +341,6 @@ const apiPostingCahsIn = async (req, res) => {
|
||||
|
||||
const _generateCashIn = async (uniqueCode) => {
|
||||
let partnerRecon = await model.getPartnerRecon(knex);
|
||||
// return partnerRecon
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} getPartner Recon cash in : ${JSON.stringify(
|
||||
partnerRecon
|
||||
@@ -331,38 +348,8 @@ const _generateCashIn = async (uniqueCode) => {
|
||||
);
|
||||
|
||||
for (const item of partnerRecon) {
|
||||
let reserve = [];
|
||||
let code = '';
|
||||
await knex.transaction(async (trx) => {
|
||||
try {
|
||||
code = await model.generateCode(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
moment().format('YYMMDD')
|
||||
);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} generate cash in : ${code}`
|
||||
);
|
||||
|
||||
// Cash-in
|
||||
let dataCahsIn = {
|
||||
c_partner_code: item.c_partner_code,
|
||||
c_partner_name: item.c_partner_name,
|
||||
c_cash_in_no: code,
|
||||
c_in_period: item.c_in_period,
|
||||
d_credit: item.d_payment,
|
||||
c_invoice_no: item.c_invoice_no,
|
||||
i_amount: item.amount,
|
||||
};
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN ${code} DATA : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
|
||||
// CASH-IN DETAIL
|
||||
let data = [];
|
||||
try {
|
||||
await knex.transaction(async (trx) => {
|
||||
const reserv = await model.getReserve(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
@@ -370,114 +357,85 @@ const _generateCashIn = async (uniqueCode) => {
|
||||
item.c_invoice_no
|
||||
);
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} cashIn reserv ${code} DATA : ${JSON.stringify(
|
||||
reserv
|
||||
)}`
|
||||
);
|
||||
|
||||
//TOTAL AMOUNT CASH-IN
|
||||
let cashInAmount = 0;
|
||||
for (const itemCI of reserv) {
|
||||
cashInAmount += parseInt(itemCI.i_reservation_amount);
|
||||
if (!reserv || reserv.length === 0) {
|
||||
return;
|
||||
}
|
||||
dataCahsIn.i_amount = cashInAmount;
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} cashInAmount ${code} DATA : ${JSON.stringify(
|
||||
cashInAmount
|
||||
)}`
|
||||
const cashInAmount = reserv.reduce(
|
||||
(sum, row) => sum + parseInt(row.i_reservation_amount, 10),
|
||||
0
|
||||
);
|
||||
|
||||
if(cashInAmount > 0) {
|
||||
//INSERT CASH-IN
|
||||
const cashIn = await model.insertCashIn(trx, dataCahsIn);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN HISTORY ${code} DATA : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
const paymentyHistory = await model.writePaynentHistory(
|
||||
dataCahsIn,
|
||||
'SUCCESSFULLY',
|
||||
0,
|
||||
trx
|
||||
);
|
||||
|
||||
let m_amount = 0;
|
||||
for (const detail of reserv) {
|
||||
// data.push({
|
||||
// i_cash_in: cashIn.i_cash_in,
|
||||
// c_reservation_no: detail.c_reservation_no,
|
||||
// m_amount: detail.i_reservation_amount,
|
||||
// c_payment_recon_no: detail.c_payment_recon_no,
|
||||
// d_payment: detail.d_payment,
|
||||
// d_payment_recon: detail.d_payment_recon,
|
||||
// c_load_balance_no: detail.c_load_balance_no,
|
||||
// });
|
||||
let temp = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
c_reservation_no: detail.c_reservation_no,
|
||||
m_amount: detail.i_reservation_amount,
|
||||
c_payment_recon_no: detail.c_payment_recon_no,
|
||||
d_payment: detail.d_payment,
|
||||
d_payment_recon: detail.d_payment_recon,
|
||||
c_load_balance_no: detail.c_load_balance_no,
|
||||
};
|
||||
|
||||
let reserveNo = detail.c_reservation_no;
|
||||
let dataReserve = {
|
||||
c_cash_in_no: cashIn.c_cash_in_no,
|
||||
d_credit: cashIn.d_credit,
|
||||
c_in_period: cashIn.c_in_period,
|
||||
};
|
||||
m_amount += parseInt(detail.i_reservation_amount);
|
||||
|
||||
if(m_amount <= detail.invoice_amount) {
|
||||
await model.updateIntoReserve(trx, dataReserve, reserveNo);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} CASH-IN UPDATE RESERVE BALANCE ${reserveNo} DATA : ${JSON.stringify(
|
||||
dataReserve
|
||||
)}`
|
||||
);
|
||||
await model.insertCashInDetail(trx, temp);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN DETAIL, ID CASH-IN ${cashIn.i_cash_in} : ${JSON.stringify(
|
||||
temp
|
||||
)}`
|
||||
);
|
||||
} else {
|
||||
await trx.rollback();
|
||||
winston.logger.info(
|
||||
`${uniqueCode} ROLLBACK CASH-IN: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${m_amount} > INVOICE AMOUNT (${detail.invoice_amount}) : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
await wait(10000);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN TO RESERVE ${code} DATA : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
if (cashInAmount <= 0) {
|
||||
return;
|
||||
}
|
||||
await trx.commit();
|
||||
|
||||
} catch (error) {
|
||||
sts = "error";
|
||||
await trx.rollback();
|
||||
}
|
||||
|
||||
});
|
||||
let runningAmount = 0;
|
||||
for (const detail of reserv) {
|
||||
runningAmount += parseInt(detail.i_reservation_amount, 10);
|
||||
if (runningAmount > parseInt(detail.invoice_amount, 10)) {
|
||||
winston.logger.warn(
|
||||
`SCHEDULER-FB70-${uniqueCode} SKIP CASH-IN: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${runningAmount} > INVOICE AMOUNT (${detail.invoice_amount} on invoice ${detail.c_invoice_no})`
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const code = await model.generateCode(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
moment().format('YYMMDD')
|
||||
);
|
||||
|
||||
const dataCahsIn = {
|
||||
c_partner_code: item.c_partner_code,
|
||||
c_partner_name: item.c_partner_name,
|
||||
c_cash_in_no: code,
|
||||
c_in_period: item.c_in_period,
|
||||
d_credit: item.d_payment,
|
||||
c_invoice_no: item.c_invoice_no,
|
||||
i_amount: cashInAmount,
|
||||
};
|
||||
|
||||
const cashIn = await model.insertCashIn(trx, dataCahsIn);
|
||||
await model.writePaynentHistory(dataCahsIn, 'SUCCESSFULLY', 0, trx);
|
||||
|
||||
const dataReserve = {
|
||||
c_cash_in_no: cashIn.c_cash_in_no,
|
||||
d_credit: cashIn.d_credit,
|
||||
c_in_period: cashIn.c_in_period,
|
||||
};
|
||||
|
||||
const reserveNos = reserv.map((detail) => detail.c_reservation_no);
|
||||
const cashInDetails = reserv.map((detail) => ({
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
c_reservation_no: detail.c_reservation_no,
|
||||
m_amount: detail.i_reservation_amount,
|
||||
c_payment_recon_no: detail.c_payment_recon_no,
|
||||
d_payment: detail.d_payment,
|
||||
d_payment_recon: detail.d_payment_recon,
|
||||
c_load_balance_no: detail.c_load_balance_no,
|
||||
}));
|
||||
|
||||
await model.updateIntoReserveBulk(trx, dataReserve, reserveNos);
|
||||
await model.insertCashInDetailBulk(trx, cashInDetails);
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN ${code} SUCCESS. partner=${item.c_partner_code}, invoice=${item.c_invoice_no}, rows=${cashInDetails.length}, amount=${cashInAmount}`
|
||||
);
|
||||
});
|
||||
} catch (error) {
|
||||
winston.logger.error(
|
||||
`SCHEDULER-FB70-${uniqueCode} generate cash in failed for partner=${item.c_partner_code}, invoice=${item.c_invoice_no} | ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return partnerRecon;
|
||||
};
|
||||
|
||||
const _generateCashInSales = async (uniqueCode) => {
|
||||
let partnerRecon = await model.getPartnerReconSales(knex);
|
||||
|
||||
// return partnerRecon
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} getPartner Recon cash in : ${JSON.stringify(
|
||||
partnerRecon
|
||||
@@ -485,37 +443,8 @@ const _generateCashInSales = async (uniqueCode) => {
|
||||
);
|
||||
|
||||
for (const item of partnerRecon) {
|
||||
let code = '';
|
||||
await knex.transaction(async (trx) => {
|
||||
try {
|
||||
code = await model.generateCode(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
moment().format('YYMMDD')
|
||||
);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} generate cash in : ${code}`
|
||||
);
|
||||
|
||||
// Cash-in
|
||||
let dataCahsIn = {
|
||||
c_partner_code: item.c_partner_code,
|
||||
c_partner_name: item.c_partner_name,
|
||||
c_cash_in_no: code,
|
||||
c_in_period: item.c_in_period,
|
||||
d_credit: item.d_payment,
|
||||
c_invoice_no: item.c_invoice_no,
|
||||
i_amount: item.amount,
|
||||
c_source: 'S'
|
||||
};
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN SALES ${code} DATA : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
|
||||
// CASH-IN DETAIL
|
||||
try {
|
||||
await knex.transaction(async (trx) => {
|
||||
const sales = await model.getSalesKMT(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
@@ -523,88 +452,84 @@ const _generateCashInSales = async (uniqueCode) => {
|
||||
item.c_invoice_no
|
||||
);
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} DATA SALES ${code} : ${JSON.stringify( sales )}`
|
||||
if (!sales || sales.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const cashInAmount = sales.reduce(
|
||||
(sum, row) => sum + parseInt(row.i_total_billing, 10),
|
||||
0
|
||||
);
|
||||
|
||||
//TOTAL AMOUNT CASH-IN
|
||||
let cashInAmount = 0;
|
||||
for (const itemCI of sales) {
|
||||
cashInAmount += parseInt(itemCI.i_total_billing);
|
||||
|
||||
if (cashInAmount <= 0) {
|
||||
return;
|
||||
}
|
||||
dataCahsIn.i_amount = cashInAmount;
|
||||
|
||||
if(cashInAmount > 0) {
|
||||
//INSERT CASH-IN
|
||||
const cashIn = await model.insertCashIn(trx, dataCahsIn);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN HISTORY ${code} DATA : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
let runningAmount = 0;
|
||||
for (const detail of sales) {
|
||||
runningAmount += parseInt(detail.i_total_billing, 10);
|
||||
if (runningAmount > parseInt(detail.invoice_amount, 10)) {
|
||||
winston.logger.warn(
|
||||
`SCHEDULER-FB70-${uniqueCode} SKIP CASH-IN SALES: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${runningAmount} > INVOICE AMOUNT (${detail.invoice_amount} on invoice ${detail.c_invoice_no})`
|
||||
);
|
||||
const paymentyHistory = await model.writePaynentHistory(
|
||||
dataCahsIn, 'SUCCESSFULLY', 0, trx
|
||||
);
|
||||
|
||||
let m_amount = 0;
|
||||
for (const detail of sales) {
|
||||
let temp = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
c_reservation_no: detail.c_sales_no,
|
||||
m_amount: detail.i_total_billing,
|
||||
c_payment_recon_no: detail.c_payment_recon_no,
|
||||
d_payment: detail.d_payment,
|
||||
d_payment_recon: detail.d_payment_recon,
|
||||
};
|
||||
|
||||
let salesNo = detail.c_sales_no;
|
||||
let dataSales = {
|
||||
c_cash_in_no: cashIn.c_cash_in_no,
|
||||
d_credit: cashIn.d_credit,
|
||||
c_in_period: cashIn.c_in_period,
|
||||
};
|
||||
m_amount += parseInt(detail.i_total_billing);
|
||||
|
||||
if(m_amount <= detail.invoice_amount) {
|
||||
await model.updateIntoSalesKMT(trx, dataSales, salesNo);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} CASH-IN UPDATE SALES KMT MOBILE ${salesNo} DATA : ${JSON.stringify(
|
||||
dataSales
|
||||
)}`
|
||||
);
|
||||
await model.insertCashInDetail(trx, temp);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN DETAIL, ID CASH-IN ${cashIn.i_cash_in} : ${JSON.stringify(
|
||||
temp
|
||||
)}`
|
||||
);
|
||||
} else {
|
||||
await trx.rollback();
|
||||
winston.logger.info(
|
||||
`${uniqueCode} ROLLBACK CASH-IN: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${m_amount} > INVOICE AMOUNT (${detail.invoice_amount}) : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
await wait(10000);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await trx.commit();
|
||||
|
||||
} catch (error) {
|
||||
sts = "error";
|
||||
await trx.rollback();
|
||||
}
|
||||
|
||||
});
|
||||
const code = await model.generateCode(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
moment().format('YYMMDD')
|
||||
);
|
||||
|
||||
const dataCahsIn = {
|
||||
c_partner_code: item.c_partner_code,
|
||||
c_partner_name: item.c_partner_name,
|
||||
c_cash_in_no: code,
|
||||
c_in_period: item.c_in_period,
|
||||
d_credit: item.d_payment,
|
||||
c_invoice_no: item.c_invoice_no,
|
||||
i_amount: cashInAmount,
|
||||
c_source: 'S',
|
||||
};
|
||||
|
||||
const cashIn = await model.insertCashIn(trx, dataCahsIn);
|
||||
await model.writePaynentHistory(dataCahsIn, 'SUCCESSFULLY', 0, trx);
|
||||
|
||||
const dataSales = {
|
||||
c_cash_in_no: cashIn.c_cash_in_no,
|
||||
d_credit: cashIn.d_credit,
|
||||
c_in_period: cashIn.c_in_period,
|
||||
};
|
||||
|
||||
const salesNos = sales.map((detail) => detail.c_sales_no);
|
||||
const cashInDetails = sales.map((detail) => ({
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
c_reservation_no: detail.c_sales_no,
|
||||
m_amount: detail.i_total_billing,
|
||||
c_payment_recon_no: detail.c_payment_recon_no,
|
||||
d_payment: detail.d_payment,
|
||||
d_payment_recon: detail.d_payment_recon,
|
||||
}));
|
||||
|
||||
await model.updateIntoSalesKMTBulk(trx, dataSales, salesNos);
|
||||
await model.insertCashInDetailBulk(trx, cashInDetails);
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN SALES ${code} SUCCESS. partner=${item.c_partner_code}, invoice=${item.c_invoice_no}, rows=${cashInDetails.length}, amount=${cashInAmount}`
|
||||
);
|
||||
});
|
||||
} catch (error) {
|
||||
winston.logger.error(
|
||||
`SCHEDULER-FB70-${uniqueCode} generate cash in sales failed for partner=${item.c_partner_code}, invoice=${item.c_invoice_no} | ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
return partnerRecon;
|
||||
};
|
||||
|
||||
const _generateCashInSalesInternal = async (uniqueCode) => {
|
||||
let partnerRecon = await model.getPartnerReconSalesInternal(knex);
|
||||
|
||||
// return partnerRecon
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} getPartner Recon cash in : ${JSON.stringify(
|
||||
partnerRecon
|
||||
@@ -612,117 +537,78 @@ const _generateCashInSalesInternal = async (uniqueCode) => {
|
||||
);
|
||||
|
||||
for (const item of partnerRecon) {
|
||||
let code = '';
|
||||
await knex.transaction(async (trx) => {
|
||||
try {
|
||||
code = await model.generateCode(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
moment().format('YYMMDD')
|
||||
);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} generate cash in : ${code}`
|
||||
);
|
||||
const sales = await model.getSalesInternal(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
item.d_payment,
|
||||
item.c_invoice_no
|
||||
);
|
||||
|
||||
// Cash-in
|
||||
let dataCahsIn = {
|
||||
c_partner_code: item.c_partner_code,
|
||||
c_partner_name: item.c_partner_name,
|
||||
c_cash_in_no: code,
|
||||
c_in_period: item.c_in_period,
|
||||
d_credit: item.d_payment,
|
||||
c_invoice_no: item.c_invoice_no,
|
||||
i_amount: item.amount,
|
||||
c_source: 'S'
|
||||
};
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN SALES ${code} DATA : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
|
||||
// CASH-IN DETAIL
|
||||
const sales = await model.getSalesInternal(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
item.d_payment,
|
||||
item.c_invoice_no
|
||||
);
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} DATA SALES INTERNAL ${code} : ${JSON.stringify( sales )}`
|
||||
);
|
||||
|
||||
//TOTAL AMOUNT CASH-IN
|
||||
let cashInAmount = 0;
|
||||
for (const itemCI of sales) {
|
||||
cashInAmount += parseInt(itemCI.i_total_price);
|
||||
}
|
||||
dataCahsIn.i_amount = cashInAmount;
|
||||
|
||||
if(cashInAmount > 0) {
|
||||
//INSERT CASH-IN
|
||||
const cashIn = await model.insertCashIn(trx, dataCahsIn);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN HISTORY ${code} DATA : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
const paymentyHistory = await model.writePaynentHistory(
|
||||
dataCahsIn, 'SUCCESSFULLY', 0, trx
|
||||
);
|
||||
|
||||
let m_amount = 0;
|
||||
for (const detail of sales) {
|
||||
let temp = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
c_reservation_no: detail.c_sales_no,
|
||||
m_amount: detail.i_total_price,
|
||||
c_payment_recon_no: detail.c_payment_recon_no,
|
||||
d_payment: detail.d_payment,
|
||||
d_payment_recon: detail.d_payment_recon,
|
||||
};
|
||||
|
||||
let salesNo = detail.c_sales_no;
|
||||
let dataSales = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
};
|
||||
m_amount += parseInt(detail.i_total_price);
|
||||
|
||||
if(m_amount <= detail.invoice_amount) {
|
||||
await model.updateIntoSalesInternal(trx, dataSales, salesNo);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} CASH-IN UPDATE SALES INTERNAL ${salesNo} DATA : ${JSON.stringify(
|
||||
dataSales
|
||||
)}`
|
||||
);
|
||||
await model.insertCashInDetail(trx, temp);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN DETAIL, ID CASH-IN ${cashIn.i_cash_in} : ${JSON.stringify(
|
||||
temp
|
||||
)}`
|
||||
);
|
||||
} else {
|
||||
await trx.rollback();
|
||||
winston.logger.info(
|
||||
`${uniqueCode} ROLLBACK CASH-IN: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${m_amount} > INVOICE AMOUNT (${detail.invoice_amount}) : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
await wait(10000);
|
||||
}
|
||||
await trx.commit();
|
||||
|
||||
} catch (error) {
|
||||
sts = "error";
|
||||
await trx.rollback();
|
||||
|
||||
throw new Error(error);
|
||||
if (!sales || sales.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const cashInAmount = sales.reduce(
|
||||
(sum, row) => sum + parseInt(row.i_total_price, 10),
|
||||
0
|
||||
);
|
||||
|
||||
if (cashInAmount <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let runningAmount = 0;
|
||||
for (const detail of sales) {
|
||||
runningAmount += parseInt(detail.i_total_price, 10);
|
||||
if (runningAmount > parseInt(detail.invoice_amount, 10)) {
|
||||
winston.logger.warn(
|
||||
`SCHEDULER-FB70-${uniqueCode} SKIP CASH-IN SALES INTERNAL: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${runningAmount} > INVOICE AMOUNT (${detail.invoice_amount} on invoice ${detail.c_invoice_no})`
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const code = await model.generateCode(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
moment().format('YYMMDD')
|
||||
);
|
||||
|
||||
const dataCahsIn = {
|
||||
c_partner_code: item.c_partner_code,
|
||||
c_partner_name: item.c_partner_name,
|
||||
c_cash_in_no: code,
|
||||
c_in_period: item.c_in_period,
|
||||
d_credit: item.d_payment,
|
||||
c_invoice_no: item.c_invoice_no,
|
||||
i_amount: cashInAmount,
|
||||
c_source: 'S',
|
||||
};
|
||||
|
||||
const cashIn = await model.insertCashIn(trx, dataCahsIn);
|
||||
await model.writePaynentHistory(dataCahsIn, 'SUCCESSFULLY', 0, trx);
|
||||
|
||||
const dataSales = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
};
|
||||
|
||||
const salesNos = sales.map((detail) => detail.c_sales_no);
|
||||
const cashInDetails = sales.map((detail) => ({
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
c_reservation_no: detail.c_sales_no,
|
||||
m_amount: detail.i_total_price,
|
||||
c_payment_recon_no: detail.c_payment_recon_no,
|
||||
d_payment: detail.d_payment,
|
||||
d_payment_recon: detail.d_payment_recon,
|
||||
}));
|
||||
|
||||
await model.updateIntoSalesInternalBulk(trx, dataSales, salesNos);
|
||||
await model.insertCashInDetailBulk(trx, cashInDetails);
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN SALES INTERNAL ${code} SUCCESS. partner=${item.c_partner_code}, invoice=${item.c_invoice_no}, rows=${cashInDetails.length}, amount=${cashInAmount}`
|
||||
);
|
||||
});
|
||||
}
|
||||
return partnerRecon;
|
||||
@@ -730,8 +616,6 @@ const _generateCashInSalesInternal = async (uniqueCode) => {
|
||||
|
||||
const _generateCashInTopUpCashlessInternal = async (uniqueCode) => {
|
||||
let partnerRecon = await model.getPartnerReconTopUpCashlessInternal(knex);
|
||||
|
||||
// return partnerRecon
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} getPartner Recon cash in : ${JSON.stringify(
|
||||
partnerRecon
|
||||
@@ -739,117 +623,78 @@ const _generateCashInTopUpCashlessInternal = async (uniqueCode) => {
|
||||
);
|
||||
|
||||
for (const item of partnerRecon) {
|
||||
let code = '';
|
||||
await knex.transaction(async (trx) => {
|
||||
try {
|
||||
code = await model.generateCode(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
moment().format('YYMMDD')
|
||||
);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} generate cash in : ${code}`
|
||||
);
|
||||
const topup = await model.getTopUpCashlessInternal(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
item.d_payment,
|
||||
item.c_invoice_no
|
||||
);
|
||||
|
||||
// Cash-in
|
||||
let dataCahsIn = {
|
||||
c_partner_code: item.c_partner_code,
|
||||
c_partner_name: item.c_partner_name,
|
||||
c_cash_in_no: code,
|
||||
c_in_period: item.c_in_period,
|
||||
d_credit: item.d_payment,
|
||||
c_invoice_no: item.c_invoice_no,
|
||||
i_amount: item.amount,
|
||||
c_source: 'T'
|
||||
};
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN TOPUP CASHLESS INTERNAL ${code} DATA : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
|
||||
// CASH-IN DETAIL
|
||||
const topup = await model.getTopUpCashlessInternal(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
item.d_payment,
|
||||
item.c_invoice_no
|
||||
);
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} DATA TOPUP CASHLESS INTERNAL ${code} : ${JSON.stringify( topup )}`
|
||||
);
|
||||
|
||||
//TOTAL AMOUNT CASH-IN
|
||||
let cashInAmount = 0;
|
||||
for (const itemCI of topup) {
|
||||
cashInAmount += parseInt(itemCI.i_amount);
|
||||
}
|
||||
dataCahsIn.i_amount = cashInAmount;
|
||||
|
||||
if(cashInAmount > 0) {
|
||||
|
||||
//INSERT CASH-IN
|
||||
const cashIn = await model.insertCashIn(trx, dataCahsIn);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN HISTORY ${code} DATA : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
const paymentyHistory = await model.writePaynentHistory(
|
||||
dataCahsIn, 'SUCCESSFULLY', 0, trx
|
||||
);
|
||||
|
||||
let m_amount = 0;
|
||||
for (const detail of topup) {
|
||||
let temp = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
c_reservation_no: detail.c_topup_no,
|
||||
m_amount: detail.i_amount,
|
||||
c_payment_recon_no: detail.c_payment_recon_no,
|
||||
d_payment: detail.d_payment,
|
||||
d_payment_recon: detail.d_payment_recon,
|
||||
};
|
||||
|
||||
let topupNo = detail.c_topup_no;
|
||||
let dataTopup = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
};
|
||||
m_amount += parseInt(detail.i_amount);
|
||||
|
||||
if(m_amount <= detail.invoice_amount) {
|
||||
await model.updateIntoTopUpCashlessInternal(trx, dataTopup, topupNo);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} CASH-IN UPDATE TOPUP CASHLESS INTERNAL ${topupNo} DATA : ${JSON.stringify(
|
||||
dataTopup
|
||||
)}`
|
||||
);
|
||||
await model.insertCashInDetail(trx, temp);
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN DETAIL, ID CASH-IN ${cashIn.i_cash_in} : ${JSON.stringify(
|
||||
temp
|
||||
)}`
|
||||
);
|
||||
} else {
|
||||
await trx.rollback();
|
||||
winston.logger.info(
|
||||
`${uniqueCode} ROLLBACK CASH-IN: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${m_amount} > INVOICE AMOUNT (${detail.invoice_amount}) : ${JSON.stringify(
|
||||
dataCahsIn
|
||||
)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
await wait(10000);
|
||||
}
|
||||
await trx.commit();
|
||||
|
||||
} catch (error) {
|
||||
sts = "error";
|
||||
await trx.rollback();
|
||||
throw new Error(error);
|
||||
if (!topup || topup.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const cashInAmount = topup.reduce(
|
||||
(sum, row) => sum + parseInt(row.i_amount, 10),
|
||||
0
|
||||
);
|
||||
|
||||
if (cashInAmount <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let runningAmount = 0;
|
||||
for (const detail of topup) {
|
||||
runningAmount += parseInt(detail.i_amount, 10);
|
||||
if (runningAmount > parseInt(detail.invoice_amount, 10)) {
|
||||
winston.logger.warn(
|
||||
`SCHEDULER-FB70-${uniqueCode} SKIP CASH-IN TOPUP CASHLESS INTERNAL: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${runningAmount} > INVOICE AMOUNT (${detail.invoice_amount})`
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const code = await model.generateCode(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
moment().format('YYMMDD')
|
||||
);
|
||||
|
||||
const dataCahsIn = {
|
||||
c_partner_code: item.c_partner_code,
|
||||
c_partner_name: item.c_partner_name,
|
||||
c_cash_in_no: code,
|
||||
c_in_period: item.c_in_period,
|
||||
d_credit: item.d_payment,
|
||||
c_invoice_no: item.c_invoice_no,
|
||||
i_amount: cashInAmount,
|
||||
c_source: 'T',
|
||||
};
|
||||
|
||||
const cashIn = await model.insertCashIn(trx, dataCahsIn);
|
||||
await model.writePaynentHistory(dataCahsIn, 'SUCCESSFULLY', 0, trx);
|
||||
|
||||
const dataTopup = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
};
|
||||
|
||||
const topupNos = topup.map((detail) => detail.c_topup_no);
|
||||
const cashInDetails = topup.map((detail) => ({
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
c_reservation_no: detail.c_topup_no,
|
||||
m_amount: detail.i_amount,
|
||||
c_payment_recon_no: detail.c_payment_recon_no,
|
||||
d_payment: detail.d_payment,
|
||||
d_payment_recon: detail.d_payment_recon,
|
||||
}));
|
||||
|
||||
await model.updateIntoTopUpCashlessInternalBulk(trx, dataTopup, topupNos);
|
||||
await model.insertCashInDetailBulk(trx, cashInDetails);
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN TOPUP CASHLESS INTERNAL ${code} SUCCESS. partner=${item.c_partner_code}, invoice=${item.c_invoice_no}, rows=${cashInDetails.length}, amount=${cashInAmount}`
|
||||
);
|
||||
});
|
||||
}
|
||||
return partnerRecon;
|
||||
@@ -931,10 +776,6 @@ const _postingCashIn = async (uniqueCode) => {
|
||||
return postingEt;
|
||||
};
|
||||
|
||||
function wait(milliseconds) {
|
||||
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
validate,
|
||||
apiGenerateCahsIn,
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
// GET PARTNER, MERCHANT, TERMINAL FROM TID
|
||||
const generateInvoice = async (invoiceDate, repost, trx) => {
|
||||
let result = await trx.select(
|
||||
trx.raw(
|
||||
`* FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`
|
||||
)
|
||||
let result = await trx.raw(
|
||||
`SELECT * FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`
|
||||
);
|
||||
|
||||
return result;
|
||||
return result.rows || result;
|
||||
};
|
||||
|
||||
const insertPostingEt = async (code, dataCashIn, trx) => {
|
||||
@@ -281,14 +279,14 @@ const getReserve = async (trx, code, date, invoice) => {
|
||||
.leftJoin('trx.t_d_load_balance as tdlb', function () {
|
||||
this.on('tdrb.c_load_balance_no', '=', 'tdlb.c_load_balance_no');
|
||||
})
|
||||
.leftJoin('payment.t_d_invoice as tdi', function () {
|
||||
.innerJoin('payment.t_d_invoice as tdi', function () {
|
||||
this.on('tdi.c_invoice_no', '=', 'tdlb.c_invoice_no').orOn(
|
||||
'tdi.c_invoice_no',
|
||||
'=',
|
||||
'tdrb.c_invoice_no'
|
||||
);
|
||||
})
|
||||
.leftJoin('payment.t_d_payment_recon as tdpr', function () {
|
||||
.innerJoin('payment.t_d_payment_recon as tdpr', function () {
|
||||
this.on('tdpr.c_reservation_no', '=', 'tdrb.c_reservation_no');
|
||||
})
|
||||
.where('tdrb.c_status', '=', 'A')
|
||||
@@ -323,8 +321,8 @@ const getSalesKMT = async (trx, code, date, invoice) => {
|
||||
'tdsi.m_total_billing as invoice_amount'
|
||||
])
|
||||
.from('kmt_mobile.t_d_sales_kmt_mobile AS tdskm')
|
||||
.leftJoin('payment.t_d_payment_recon_sales_kmt_mobile as tdpr', 'tdpr.c_sales_no', 'tdskm.c_sales_no')
|
||||
.leftJoin('payment.t_d_sales_invoice as tdsi', 'tdsi.i_sales_invoice', 'tdskm.i_sales_invoice')
|
||||
.innerJoin('payment.t_d_payment_recon_sales_kmt_mobile as tdpr', 'tdpr.c_sales_no', 'tdskm.c_sales_no')
|
||||
.innerJoin('payment.t_d_sales_invoice as tdsi', 'tdsi.i_sales_invoice', 'tdskm.i_sales_invoice')
|
||||
|
||||
.where('tdskm.c_sales_status', '=', 'A')
|
||||
.where('tdsi.c_status', '!=', 'X')
|
||||
@@ -334,7 +332,12 @@ const getSalesKMT = async (trx, code, date, invoice) => {
|
||||
.where('tdpr.c_status', '=', 'S')
|
||||
.whereNotNull('tdsi.c_return_sap')
|
||||
.whereRaw(
|
||||
`tdskm.c_sales_no NOT IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail WHERE b_reversal IS NOT TRUE)`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM payment.t_d_cash_in_detail cid
|
||||
WHERE cid.c_reservation_no = tdskm.c_sales_no
|
||||
AND cid.b_reversal IS NOT TRUE
|
||||
)`
|
||||
);
|
||||
|
||||
return res;
|
||||
@@ -354,9 +357,9 @@ const getSalesInternal = async (trx, code, date, invoice) => {
|
||||
'tdsi.i_total_price as invoice_amount'
|
||||
])
|
||||
.from('trx.t_d_sales_internal AS tdskm')
|
||||
.leftJoin('payment.t_d_payment_recon_sales_internal as tdpr', 'tdpr.c_sales_no', 'tdskm.c_sales_no')
|
||||
.leftJoin('payment.t_d_sales_invoice_internal as tdsi', 'tdsi.i_sales_invoice_internal', 'tdskm.i_sales_invoice_internal')
|
||||
.leftJoin('public.t_m_partner as tmp', 'tmp.i_partner', 'tdskm.i_partner')
|
||||
.innerJoin('payment.t_d_payment_recon_sales_internal as tdpr', 'tdpr.c_sales_no', 'tdskm.c_sales_no')
|
||||
.innerJoin('payment.t_d_sales_invoice_internal as tdsi', 'tdsi.i_sales_invoice_internal', 'tdskm.i_sales_invoice_internal')
|
||||
.innerJoin('public.t_m_partner as tmp', 'tmp.i_partner', 'tdskm.i_partner')
|
||||
.where('tdsi.c_status', '!=', 'X')
|
||||
.where('tmp.c_code', '=', code)
|
||||
.where('tdpr.d_payment', '=', date)
|
||||
@@ -364,7 +367,11 @@ const getSalesInternal = async (trx, code, date, invoice) => {
|
||||
.where('tdpr.c_status', '=', 'S')
|
||||
.whereNotNull('tdsi.c_return_sap_fi')
|
||||
.whereRaw(
|
||||
`tdskm.c_sales_no NOT IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail WHERE b_reversal IS NOT TRUE)`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM payment.t_d_cash_in_detail cid
|
||||
WHERE cid.c_reservation_no = tdskm.c_sales_no
|
||||
AND cid.b_reversal IS NOT TRUE)`
|
||||
);
|
||||
|
||||
return res;
|
||||
@@ -384,9 +391,9 @@ const getTopUpCashlessInternal = async (trx, code, date, invoice) => {
|
||||
'tdsi.i_total_amount as invoice_amount'
|
||||
])
|
||||
.from('trx.t_d_topup_cashless_internal AS tdskm')
|
||||
.leftJoin('payment.t_d_payment_recon_topup_cashless_internal as tdpr', 'tdpr.c_topup_no', 'tdskm.c_topup_no')
|
||||
.leftJoin('payment.t_d_topup_cashless_invoice_internal as tdsi', 'tdsi.i_topup_cashless_invoice_internal', 'tdskm.i_topup_cashless_invoice_internal')
|
||||
.leftJoin('public.t_m_partner as tmp', 'tmp.i_partner', 'tdskm.i_partner')
|
||||
.innerJoin('payment.t_d_payment_recon_topup_cashless_internal as tdpr', 'tdpr.c_topup_no', 'tdskm.c_topup_no')
|
||||
.innerJoin('payment.t_d_topup_cashless_invoice_internal as tdsi', 'tdsi.i_topup_cashless_invoice_internal', 'tdskm.i_topup_cashless_invoice_internal')
|
||||
.innerJoin('public.t_m_partner as tmp', 'tmp.i_partner', 'tdskm.i_partner')
|
||||
.where('tdsi.c_status', '!=', 'X')
|
||||
.where('tmp.c_code', '=', code)
|
||||
.where('tdpr.d_payment', '=', date)
|
||||
@@ -394,7 +401,12 @@ const getTopUpCashlessInternal = async (trx, code, date, invoice) => {
|
||||
.where('tdpr.c_status', '=', 'S')
|
||||
.whereNotNull('tdsi.c_return_sap')
|
||||
.whereRaw(
|
||||
`tdskm.c_topup_no NOT IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail WHERE b_reversal IS NOT TRUE)`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM payment.t_d_cash_in_detail cid
|
||||
WHERE cid.c_reservation_no = tdskm.c_topup_no
|
||||
AND cid.b_reversal IS NOT TRUE
|
||||
)`
|
||||
);
|
||||
|
||||
return res;
|
||||
@@ -474,7 +486,7 @@ const getPartnerRecon = async (trx) => {
|
||||
'i_company',
|
||||
'i_holding',
|
||||
trx.raw(`CASE
|
||||
WHEN to_char(tdpr.d_payment, 'YYYYMM') >= to_char(tdi.d_invoice_date, 'YYYYMM')
|
||||
WHEN date_trunc('month', tdpr.d_payment) >= date_trunc('month', tdi.d_invoice_date)
|
||||
THEN 'IP'
|
||||
ELSE
|
||||
'OP'
|
||||
@@ -484,17 +496,17 @@ const getPartnerRecon = async (trx) => {
|
||||
.leftJoin('trx.t_d_load_balance as tdlb', function () {
|
||||
this.on('tdrb.c_load_balance_no', '=', 'tdlb.c_load_balance_no');
|
||||
})
|
||||
.leftJoin('payment.t_d_invoice as tdi', function () {
|
||||
.innerJoin('payment.t_d_invoice as tdi', function () {
|
||||
this.on('tdi.c_invoice_no', '=', 'tdlb.c_invoice_no').orOn(
|
||||
'tdi.c_invoice_no',
|
||||
'=',
|
||||
'tdrb.c_invoice_no'
|
||||
);
|
||||
})
|
||||
.leftJoin('payment.t_d_payment_recon as tdpr', function () {
|
||||
.innerJoin('payment.t_d_payment_recon as tdpr', function () {
|
||||
this.on('tdpr.c_reservation_no', '=', 'tdrb.c_reservation_no');
|
||||
})
|
||||
.leftJoin('public.t_m_partner as tmp', function () {
|
||||
.innerJoin('public.t_m_partner as tmp', function () {
|
||||
this.on('tmp.i_partner', '=', 'tdrb.i_partner');
|
||||
})
|
||||
// .leftJoin('payment.t_d_cash_in_detail as tdcid', function () {
|
||||
@@ -537,19 +549,19 @@ const getPartnerReconSales = async (trx) => {
|
||||
trx.raw('coalesce(SUM(tdskm.i_total_billing),0) AS amount'),
|
||||
'tmp.c_account_topup',
|
||||
'tmp.c_account_code_topup',
|
||||
trx.raw(`CASE WHEN to_char(tdpr.d_payment, 'YYYYMM') >= to_char(tdsi.d_invoice_date, 'YYYYMM')
|
||||
trx.raw(`CASE WHEN date_trunc('month', tdpr.d_payment) >= date_trunc('month', tdsi.d_invoice_date)
|
||||
THEN 'IP' ELSE 'OP'
|
||||
END c_in_period`),
|
||||
])
|
||||
.from('kmt_mobile.t_d_sales_kmt_mobile AS tdskm')
|
||||
|
||||
.leftJoin('payment.t_d_sales_invoice as tdsi', function () {
|
||||
.innerJoin('payment.t_d_sales_invoice as tdsi', function () {
|
||||
this.on('tdsi.i_sales_invoice', '=', 'tdskm.i_sales_invoice');
|
||||
})
|
||||
.leftJoin('payment.t_d_payment_recon_sales_kmt_mobile as tdpr', function () {
|
||||
.innerJoin('payment.t_d_payment_recon_sales_kmt_mobile as tdpr', function () {
|
||||
this.on('tdpr.c_sales_no', '=', 'tdskm.c_sales_no');
|
||||
})
|
||||
.leftJoin('public.t_m_partner as tmp', function () {
|
||||
.innerJoin('public.t_m_partner as tmp', function () {
|
||||
this.on('tmp.i_partner', '=', 'tdskm.i_partner');
|
||||
})
|
||||
.where('tdskm.c_sales_status', '=', 'A')
|
||||
@@ -582,19 +594,19 @@ const getPartnerReconSalesInternal = async (trx) => {
|
||||
trx.raw('coalesce(SUM(tdskm.i_total_price),0) AS amount'),
|
||||
'tmp.c_account_sales_revenue',
|
||||
'tmp.c_account_code_sales_ar',
|
||||
trx.raw(`CASE WHEN to_char(tdpr.d_payment, 'YYYYMM') >= to_char(tdsi.d_invoice_date, 'YYYYMM')
|
||||
THEN 'IP' ELSE 'OP'
|
||||
trx.raw(`CASE WHEN date_trunc('month', tdpr.d_payment) >= date_trunc('month', tdsi.d_invoice_date)
|
||||
THEN 'IP' ELSE 'IP'
|
||||
END c_in_period`),
|
||||
])
|
||||
.from('trx.t_d_sales_internal AS tdskm')
|
||||
|
||||
.leftJoin('payment.t_d_sales_invoice_internal as tdsi', function () {
|
||||
.innerJoin('payment.t_d_sales_invoice_internal as tdsi', function () {
|
||||
this.on('tdsi.i_sales_invoice_internal', '=', 'tdskm.i_sales_invoice_internal');
|
||||
})
|
||||
.leftJoin('payment.t_d_payment_recon_sales_internal as tdpr', function () {
|
||||
.innerJoin('payment.t_d_payment_recon_sales_internal as tdpr', function () {
|
||||
this.on('tdpr.c_sales_no', '=', 'tdskm.c_sales_no');
|
||||
})
|
||||
.leftJoin('public.t_m_partner as tmp', function () {
|
||||
.innerJoin('public.t_m_partner as tmp', function () {
|
||||
this.on('tmp.i_partner', '=', 'tdskm.i_partner');
|
||||
})
|
||||
.where('tdsi.c_status', '!=', 'X')
|
||||
@@ -626,19 +638,19 @@ const getPartnerReconTopUpCashlessInternal = async (trx) => {
|
||||
trx.raw('coalesce(SUM(tdskm.i_amount),0) AS amount'),
|
||||
'tmp.c_account_topup',
|
||||
'tmp.c_account_code_topup',
|
||||
trx.raw(`CASE WHEN to_char(tdpr.d_payment, 'YYYYMM') >= to_char(tdsi.d_invoice_date, 'YYYYMM')
|
||||
THEN 'IP' ELSE 'OP'
|
||||
trx.raw(`CASE WHEN date_trunc('month', tdpr.d_payment) >= date_trunc('month', tdsi.d_invoice_date)
|
||||
THEN 'IP' ELSE 'IP'
|
||||
END c_in_period`),
|
||||
])
|
||||
.from('trx.t_d_topup_cashless_internal AS tdskm')
|
||||
|
||||
.leftJoin('payment.t_d_topup_cashless_invoice_internal as tdsi', function () {
|
||||
.innerJoin('payment.t_d_topup_cashless_invoice_internal as tdsi', function () {
|
||||
this.on('tdsi.i_topup_cashless_invoice_internal', '=', 'tdskm.i_topup_cashless_invoice_internal');
|
||||
})
|
||||
.leftJoin('payment.t_d_payment_recon_topup_cashless_internal as tdpr', function () {
|
||||
.innerJoin('payment.t_d_payment_recon_topup_cashless_internal as tdpr', function () {
|
||||
this.on('tdpr.c_topup_no', '=', 'tdskm.c_topup_no');
|
||||
})
|
||||
.leftJoin('public.t_m_partner as tmp', function () {
|
||||
.innerJoin('public.t_m_partner as tmp', function () {
|
||||
this.on('tmp.i_partner', '=', 'tdskm.i_partner');
|
||||
})
|
||||
.where('tdsi.c_status', '!=', 'X')
|
||||
@@ -663,15 +675,15 @@ const generateCode = async (trx, c_partner_code, d_payment_recon) => {
|
||||
let prefix = `CIN${c_partner_code}${d_payment_recon}`;
|
||||
|
||||
let result = await trx
|
||||
.select([trx.raw(`substring(c_cash_in_no, 13, 3) AS code`)])
|
||||
.select([trx.raw(`right(c_cash_in_no, 3) AS code`)])
|
||||
.from('payment.t_d_cash_in')
|
||||
.whereRaw(`substring(c_cash_in_no, 1, 12) = '${prefix}'`)
|
||||
.where('c_cash_in_no', 'like', `${prefix}%`)
|
||||
// .where("d_project_date", projectDate)
|
||||
.orderBy('c_cash_in_no', 'DESC')
|
||||
.first();
|
||||
|
||||
if (result) {
|
||||
let counter = parseInt(result.code) + 1;
|
||||
let counter = parseInt(result.code, 10) + 1;
|
||||
counter = String(counter).padStart(3, '0');
|
||||
code = `${prefix}${counter}`;
|
||||
} else {
|
||||
@@ -701,6 +713,16 @@ const updateIntoReserve = async (trx, dataReserve, reserveNo) => {
|
||||
return result;
|
||||
};
|
||||
|
||||
const updateIntoReserveBulk = async (trx, dataReserve, reserveNos) => {
|
||||
if (!Array.isArray(reserveNos) || reserveNos.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return trx('trx.t_d_reservation_balance')
|
||||
.whereIn('c_reservation_no', reserveNos)
|
||||
.update(dataReserve);
|
||||
};
|
||||
|
||||
const updateIntoSalesKMT = async (trx, dataSales, salesNo) => {
|
||||
let result = await trx('kmt_mobile.t_d_sales_kmt_mobile')
|
||||
.update(dataSales, ['*'])
|
||||
@@ -708,6 +730,16 @@ const updateIntoSalesKMT = async (trx, dataSales, salesNo) => {
|
||||
return result;
|
||||
};
|
||||
|
||||
const updateIntoSalesKMTBulk = async (trx, dataSales, salesNos) => {
|
||||
if (!Array.isArray(salesNos) || salesNos.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return trx('kmt_mobile.t_d_sales_kmt_mobile')
|
||||
.whereIn('c_sales_no', salesNos)
|
||||
.update(dataSales);
|
||||
};
|
||||
|
||||
const updateIntoSalesInternal = async (trx, dataSales, salesNo) => {
|
||||
let result = await trx('trx.t_d_sales_internal')
|
||||
.update(dataSales, ['*'])
|
||||
@@ -715,6 +747,16 @@ const updateIntoSalesInternal = async (trx, dataSales, salesNo) => {
|
||||
return result;
|
||||
};
|
||||
|
||||
const updateIntoSalesInternalBulk = async (trx, dataSales, salesNos) => {
|
||||
if (!Array.isArray(salesNos) || salesNos.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return trx('trx.t_d_sales_internal')
|
||||
.whereIn('c_sales_no', salesNos)
|
||||
.update(dataSales);
|
||||
};
|
||||
|
||||
const updateIntoTopUpCashlessInternal = async (trx, dataTopup, topupNo) => {
|
||||
let result = await trx('trx.t_d_topup_cashless_internal')
|
||||
.update(dataTopup, ['*'])
|
||||
@@ -722,11 +764,33 @@ const updateIntoTopUpCashlessInternal = async (trx, dataTopup, topupNo) => {
|
||||
return result;
|
||||
};
|
||||
|
||||
const updateIntoTopUpCashlessInternalBulk = async (
|
||||
trx,
|
||||
dataTopup,
|
||||
topupNos
|
||||
) => {
|
||||
if (!Array.isArray(topupNos) || topupNos.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return trx('trx.t_d_topup_cashless_internal')
|
||||
.whereIn('c_topup_no', topupNos)
|
||||
.update(dataTopup);
|
||||
};
|
||||
|
||||
const insertCashIn = async (trx, data) => {
|
||||
let result = await trx('payment.t_d_cash_in').insert(data, ['*']);
|
||||
return result[0];
|
||||
};
|
||||
|
||||
const insertCashInDetailBulk = async (trx, data) => {
|
||||
if (!Array.isArray(data) || data.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return trx('payment.t_d_cash_in_detail').insert(data);
|
||||
};
|
||||
|
||||
const writePaynentHistory = async (data, message, i_log_name, trx) => {
|
||||
console.log(data);
|
||||
const result = await trx('log.t_log_invoice').insert(
|
||||
@@ -769,8 +833,13 @@ module.exports = {
|
||||
getSalesInternal,
|
||||
getTopUpCashlessInternal,
|
||||
insertCashIn,
|
||||
insertCashInDetailBulk,
|
||||
updateIntoReserve,
|
||||
updateIntoReserveBulk,
|
||||
updateIntoSalesKMT,
|
||||
updateIntoSalesKMTBulk,
|
||||
updateIntoSalesInternal,
|
||||
updateIntoSalesInternalBulk,
|
||||
updateIntoTopUpCashlessInternal,
|
||||
updateIntoTopUpCashlessInternalBulk,
|
||||
};
|
||||
|
||||
@@ -53,6 +53,10 @@ const apiGenerateInvoice = async (req, res) => {
|
||||
`${uniqueCode} REQUEST generate invoice: ${JSON.stringify(req.body)}`
|
||||
);
|
||||
|
||||
const schedulerCode = 'GENERATEINVOICEONLINETOPUP';
|
||||
const startData = await helper.startScheduler(schedulerCode);
|
||||
const token = startData.c_execution_token ?? null;
|
||||
|
||||
// check validator
|
||||
let err = validationResult(req, res);
|
||||
if (!err.isEmpty()) {
|
||||
@@ -81,10 +85,12 @@ const apiGenerateInvoice = async (req, res) => {
|
||||
|
||||
result = {
|
||||
code: '200',
|
||||
message: 'Succes posting',
|
||||
message: 'Succes generate invoice online topup',
|
||||
data: invoice,
|
||||
};
|
||||
|
||||
const finishData = await helper.finishScheduler(token, schedulerCode);
|
||||
|
||||
return res.status(200).json(result);
|
||||
} catch (error) {
|
||||
// create log
|
||||
@@ -115,6 +121,10 @@ const apiPostingInvoice = async (req, res) => {
|
||||
`${uniqueCode} REQUEST posting invoice: ${JSON.stringify(req.body)}`
|
||||
);
|
||||
|
||||
const schedulerCode = 'POSTINGINVOICEONLINETOPUP';
|
||||
const startData = await helper.startScheduler(schedulerCode);
|
||||
const token = startData.c_execution_token ?? null;
|
||||
|
||||
// check validator
|
||||
let err = validationResult(req, res);
|
||||
if (!err.isEmpty()) {
|
||||
@@ -143,6 +153,8 @@ const apiPostingInvoice = async (req, res) => {
|
||||
data: invoice,
|
||||
};
|
||||
|
||||
const finishData = await helper.finishScheduler(token, schedulerCode);
|
||||
|
||||
return res.status(200).json(result);
|
||||
} catch (error) {
|
||||
// create log
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// GET PARTNER, MERCHANT, TERMINAL FROM TID
|
||||
const generateInvoice = async (invoiceDate, repost, trx) => {
|
||||
|
||||
let result = await trx.select(trx.raw(`* FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`))
|
||||
let result = await trx.raw(`SELECT * FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`)
|
||||
|
||||
return result;
|
||||
return result.rows || result;
|
||||
|
||||
}
|
||||
|
||||
@@ -26,31 +26,35 @@ const insertPostingEt = async (invoice, trx) => {
|
||||
reference: invoice.c_invoice_no,
|
||||
amount: invoice.amount,
|
||||
ba: invoice.ba,
|
||||
indeks: (index+1),
|
||||
indeks: (index + 1),
|
||||
totalidx: type.length,
|
||||
doc_header_text: invoice.c_invoice_no,
|
||||
pos_status : 3
|
||||
pos_status: 3
|
||||
}
|
||||
|
||||
if(item == 'H'){
|
||||
data = {...data, ...{
|
||||
text: `Posting Invoice CMS ${invoice.c_invoice_no}`,
|
||||
debit_kredit: item,
|
||||
account: invoice.c_account_topup,
|
||||
customer: null
|
||||
}}
|
||||
}else{
|
||||
data = {...data, ...{
|
||||
text: `Posting Invoice CMS ${invoice.c_invoice_no}`,
|
||||
debit_kredit: item,
|
||||
account: null,
|
||||
customer: invoice.c_account_code_topup
|
||||
}}
|
||||
|
||||
if (item == 'H') {
|
||||
data = {
|
||||
...data, ...{
|
||||
text: `Posting Invoice CMS ${invoice.c_invoice_no}`,
|
||||
debit_kredit: item,
|
||||
account: invoice.c_account_topup,
|
||||
customer: null
|
||||
}
|
||||
}
|
||||
} else {
|
||||
data = {
|
||||
...data, ...{
|
||||
text: `Posting Invoice CMS ${invoice.c_invoice_no}`,
|
||||
debit_kredit: item,
|
||||
account: null,
|
||||
customer: invoice.c_account_code_topup
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
et.push(data)
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
posting_et = await trx('public.posting_et').insert(et, ['indeks', 'customer', 'account', 'ba', 'fiscal_year', 'amount', 'no_dok', 'text', 'debit_kredit'])
|
||||
|
||||
@@ -58,129 +62,266 @@ const insertPostingEt = async (invoice, trx) => {
|
||||
|
||||
}
|
||||
|
||||
const getPostingEt = async(trx) => {
|
||||
const getPostingEt = async (trx) => {
|
||||
|
||||
const result = await trx('public.posting_et AS pe').select([
|
||||
'doc_header_text',
|
||||
'ba',
|
||||
'doc_date',
|
||||
'posting_date',
|
||||
'fiscal_year',
|
||||
trx.raw(`TO_CHAR(doc_date::DATE, 'MM') AS fis_period`),
|
||||
'doc_type',
|
||||
'reference',
|
||||
'tcode',
|
||||
'amount',
|
||||
trx.raw(`'IDR' AS currency`),
|
||||
trx.raw(`(select pe2.customer from posting_et pe2 where pe2.reference = pe.reference and debit_kredit = 'S' LIMIT 1) as customer`),
|
||||
trx.raw(`(select pe2.account from posting_et pe2 where pe2.reference = pe.reference and debit_kredit = 'H' LIMIT 1) as account`),
|
||||
trx.raw(`(select pe2.text from posting_et pe2 where pe2.reference = pe.reference and debit_kredit = 'S' LIMIT 1) as text1`),
|
||||
trx.raw(`(select pe2.text from posting_et pe2 where pe2.reference = pe.reference and debit_kredit = 'H' LIMIT 1) as text2`),
|
||||
])
|
||||
.where('tcode', '=', "FB70")
|
||||
.whereIn('pos_status', ["3"])
|
||||
.groupBy([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12])
|
||||
const result = await trx.raw(`
|
||||
WITH params AS (
|
||||
|
||||
return result
|
||||
SELECT
|
||||
EXTRACT(YEAR FROM CURRENT_DATE)::TEXT AS curr_year,
|
||||
EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year')::TEXT AS prev_year,
|
||||
TO_CHAR(CURRENT_DATE, 'MM') AS curr_month
|
||||
|
||||
),
|
||||
|
||||
base AS (
|
||||
|
||||
SELECT
|
||||
pe.doc_header_text,
|
||||
pe.ba,
|
||||
pe.doc_date,
|
||||
pe.posting_date,
|
||||
pe.fiscal_year,
|
||||
TO_CHAR(pe.doc_date::DATE, 'MM') AS fis_period,
|
||||
pe.doc_type,
|
||||
pe.reference,
|
||||
pe.tcode,
|
||||
pe.amount,
|
||||
pe.customer,
|
||||
pe.account,
|
||||
pe.text,
|
||||
pe.debit_kredit
|
||||
|
||||
FROM public.posting_et pe
|
||||
CROSS JOIN params p
|
||||
|
||||
WHERE
|
||||
pe.tcode = 'FB70'
|
||||
AND pe.pos_status = '3'
|
||||
|
||||
-- Prefix search still index friendly
|
||||
AND pe.doc_header_text LIKE 'INV%'
|
||||
|
||||
AND (
|
||||
|
||||
-- Current year all months
|
||||
pe.fiscal_year = p.curr_year
|
||||
|
||||
OR (
|
||||
|
||||
-- Previous year December only
|
||||
-- only when current month is January
|
||||
p.curr_month = '01'
|
||||
|
||||
AND pe.fiscal_year = p.prev_year
|
||||
|
||||
AND TO_CHAR(pe.doc_date::DATE, 'MM') = '12'
|
||||
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
SELECT
|
||||
b.doc_header_text,
|
||||
b.ba,
|
||||
b.doc_date,
|
||||
b.posting_date,
|
||||
b.fiscal_year,
|
||||
b.fis_period,
|
||||
b.doc_type,
|
||||
b.reference,
|
||||
b.tcode,
|
||||
b.amount,
|
||||
'IDR'::VARCHAR AS currency,
|
||||
|
||||
MAX(b.customer) FILTER (
|
||||
WHERE b.debit_kredit = 'S'
|
||||
) AS customer,
|
||||
|
||||
MAX(b.account) FILTER (
|
||||
WHERE b.debit_kredit = 'H'
|
||||
) AS account,
|
||||
|
||||
MAX(b.text) FILTER (
|
||||
WHERE b.debit_kredit = 'S'
|
||||
) AS text1,
|
||||
|
||||
MAX(b.text) FILTER (
|
||||
WHERE b.debit_kredit = 'H'
|
||||
) AS text2
|
||||
|
||||
FROM base b
|
||||
|
||||
GROUP BY
|
||||
b.doc_header_text,
|
||||
b.ba,
|
||||
b.doc_date,
|
||||
b.posting_date,
|
||||
b.fiscal_year,
|
||||
b.fis_period,
|
||||
b.doc_type,
|
||||
b.reference,
|
||||
b.tcode,
|
||||
b.amount
|
||||
|
||||
ORDER BY
|
||||
b.fiscal_year,
|
||||
b.doc_date,
|
||||
b.reference
|
||||
`);
|
||||
|
||||
return result.rows || result
|
||||
|
||||
}
|
||||
|
||||
const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no) => {
|
||||
const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no) => {
|
||||
let t_invoice = {}
|
||||
let t_load_balance = {}
|
||||
let posting_et = {}
|
||||
if(resSAP.type == 'S'){
|
||||
// update invoice
|
||||
t_invoice = await trx('payment.t_d_invoice').update({
|
||||
c_return_sap: resSAP.data.E_DOCUMENTNUMBER
|
||||
}, ["*"])
|
||||
.where({
|
||||
c_invoice_no: c_invoice_no
|
||||
});
|
||||
|
||||
// update load balance
|
||||
t_load_balance = await trx('trx.t_d_load_balance').update({
|
||||
c_return_sap: resSAP.data.E_DOCUMENTNUMBER
|
||||
}, ['c_load_balance_no', 'c_return_sap'])
|
||||
.whereRaw(`c_load_balance_no IN (SELECT c_load_balance_no FROM payment.t_d_payment_request_topup_online WHERE c_payment = '${c_invoice_no}')`)
|
||||
|
||||
posting_et = await trx('public.posting_et').update({
|
||||
pos_status : 0,
|
||||
status0 : resSAP.type,
|
||||
no_doksap : resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null,
|
||||
status1 : resSAP.message ? resSAP.message : null
|
||||
}, ['file_name','status0']).where({
|
||||
reference: c_invoice_no
|
||||
})
|
||||
const paymentRequestSql = `
|
||||
SELECT c_load_balance_no
|
||||
FROM payment.t_d_payment_request_topup_online
|
||||
WHERE c_payment = ?
|
||||
`;
|
||||
|
||||
if(posting) {
|
||||
await writePaynentHistory(t_invoice[0], "SUCCESSFULLY", 102, trx)
|
||||
if (resSAP.type == 'S') {
|
||||
// update invoice
|
||||
t_invoice = await trx.raw(
|
||||
`
|
||||
UPDATE payment.t_d_invoice
|
||||
SET c_return_sap = ?
|
||||
WHERE c_invoice_no = ?
|
||||
RETURNING *
|
||||
`,
|
||||
[resSAP.data.E_DOCUMENTNUMBER, c_invoice_no]
|
||||
);
|
||||
|
||||
// update load balance
|
||||
t_load_balance = await trx.raw(
|
||||
`
|
||||
UPDATE trx.t_d_load_balance
|
||||
SET c_return_sap = ?
|
||||
WHERE c_load_balance_no IN (
|
||||
${paymentRequestSql}
|
||||
)
|
||||
RETURNING c_load_balance_no, c_return_sap
|
||||
`,
|
||||
[resSAP.data.E_DOCUMENTNUMBER, c_invoice_no]
|
||||
);
|
||||
|
||||
posting_et = await trx.raw(
|
||||
`
|
||||
UPDATE public.posting_et
|
||||
SET pos_status = 0,
|
||||
status0 = ?,
|
||||
no_doksap = ?,
|
||||
status1 = ?
|
||||
WHERE no_dok = ?
|
||||
RETURNING file_name, status0
|
||||
`,
|
||||
[
|
||||
resSAP.type,
|
||||
resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null,
|
||||
resSAP.message ? resSAP.message : null,
|
||||
c_invoice_no
|
||||
]
|
||||
);
|
||||
|
||||
if (posting) {
|
||||
await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], "SUCCESSFULLY", 102, trx)
|
||||
} else {
|
||||
await writePaynentHistory(t_invoice[0], "SUCCESS TO RETRY SAP", 118, trx)
|
||||
await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], "SUCCESS TO RETRY SAP", 118, trx)
|
||||
}
|
||||
|
||||
}else {
|
||||
} else {
|
||||
|
||||
let text = resSAP.message;
|
||||
let result = text.toUpperCase();
|
||||
|
||||
if(result.includes("ALREADY")){
|
||||
if (result.includes("ALREADY")) {
|
||||
let result = text.toUpperCase().split('-');
|
||||
let doc_number = result[1].trim().split(" ")[0]
|
||||
// update invoice
|
||||
const d = new Date();
|
||||
let year = d.getFullYear();
|
||||
|
||||
posting_et = await trx('public.posting_et').update({
|
||||
pos_status : 0,
|
||||
status0 : 'S',
|
||||
no_doksap : doc_number,
|
||||
status1 : resSAP.message ? resSAP.message : null
|
||||
}, ['file_name','status0']).where({
|
||||
reference: c_invoice_no
|
||||
})
|
||||
posting_et = await trx.raw(
|
||||
`
|
||||
UPDATE public.posting_et
|
||||
SET pos_status = 0,
|
||||
status0 = 'S',
|
||||
no_doksap = ?,
|
||||
status1 = ?
|
||||
WHERE no_dok = ?
|
||||
RETURNING file_name, status0
|
||||
`,
|
||||
[doc_number, resSAP.message ? resSAP.message : null, c_invoice_no]
|
||||
);
|
||||
|
||||
// update invoice
|
||||
t_invoice = await trx('payment.t_d_invoice').update({
|
||||
c_return_sap: doc_number
|
||||
}, ['*'])
|
||||
.where({
|
||||
c_invoice_no: c_invoice_no
|
||||
});
|
||||
|
||||
t_invoice = await trx.raw(
|
||||
`
|
||||
UPDATE payment.t_d_invoice
|
||||
SET c_return_sap = ?
|
||||
WHERE c_invoice_no = ?
|
||||
RETURNING *
|
||||
`,
|
||||
[doc_number, c_invoice_no]
|
||||
);
|
||||
|
||||
// update load balance
|
||||
t_load_balance = await trx('trx.t_d_load_balance').update({
|
||||
c_return_sap: doc_number
|
||||
}, ['c_load_balance_no', 'c_return_sap'])
|
||||
.whereRaw(`c_load_balance_no IN (SELECT c_load_balance_no FROM payment.t_d_payment_request_topup_online WHERE c_payment = '${c_invoice_no}')`)
|
||||
|
||||
if(posting) {
|
||||
await writePaynentHistory(t_invoice[0], "SUCCESSFULLY", 102, trx)
|
||||
t_load_balance = await trx.raw(
|
||||
`
|
||||
UPDATE trx.t_d_load_balance
|
||||
SET c_return_sap = ?
|
||||
WHERE c_load_balance_no IN (
|
||||
${paymentRequestSql}
|
||||
)
|
||||
RETURNING c_load_balance_no, c_return_sap
|
||||
`,
|
||||
[doc_number, c_invoice_no]
|
||||
);
|
||||
|
||||
if (posting) {
|
||||
await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], "SUCCESSFULLY", 102, trx)
|
||||
} else {
|
||||
await writePaynentHistory(t_invoice[0], "SUCCESS TO RETRY SAP", 118, trx)
|
||||
await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], "SUCCESS TO RETRY SAP", 118, trx)
|
||||
}
|
||||
|
||||
}else{
|
||||
|
||||
} else {
|
||||
// update posting et
|
||||
posting_et = await trx('public.posting_et').update({
|
||||
pos_status : resSAP.type == 'S' ? 0 : 3,
|
||||
status0 : resSAP.type,
|
||||
no_doksap : resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null,
|
||||
status1 : resSAP.message ? resSAP.message : null
|
||||
}, ['file_name','status0']).where({
|
||||
reference: c_invoice_no
|
||||
})
|
||||
posting_et = await trx.raw(
|
||||
`
|
||||
UPDATE public.posting_et
|
||||
SET pos_status = ?,
|
||||
status0 = ?,
|
||||
no_doksap = ?,
|
||||
status1 = ?
|
||||
WHERE no_dok = ?
|
||||
RETURNING file_name, status0
|
||||
`,
|
||||
[
|
||||
resSAP.type == 'S' ? 0 : 3,
|
||||
resSAP.type,
|
||||
resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null,
|
||||
resSAP.message ? resSAP.message : null,
|
||||
c_invoice_no
|
||||
]
|
||||
);
|
||||
|
||||
// update invoice
|
||||
t_invoice = await trx('payment.t_d_invoice').select(['*'])
|
||||
.where({
|
||||
c_invoice_no: c_invoice_no
|
||||
});
|
||||
|
||||
if(posting) {
|
||||
await writePaynentHistory(t_invoice[0], `SUCCESS WITH SAP ERR: ${resSAP.message}`,102, trx)
|
||||
t_invoice = await trx.raw(
|
||||
`
|
||||
SELECT *
|
||||
FROM payment.t_d_invoice
|
||||
WHERE c_invoice_no = ?
|
||||
`,
|
||||
[c_invoice_no]
|
||||
);
|
||||
|
||||
if (posting) {
|
||||
await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], `SUCCESS WITH SAP ERR: ${resSAP.message}`, 102, trx)
|
||||
} else {
|
||||
await writePaynentHistory(t_invoice[0], `FAIL TO RETRY WITH SAP ERR: ${resSAP.message}`,118, trx)
|
||||
await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], `FAIL TO RETRY WITH SAP ERR: ${resSAP.message}`, 118, trx)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -190,29 +331,29 @@ const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no
|
||||
// update posting et
|
||||
|
||||
return {
|
||||
invoice : t_invoice,
|
||||
load_balance : t_load_balance,
|
||||
posting_et : posting_et
|
||||
invoice: t_invoice.rows || t_invoice,
|
||||
load_balance: t_load_balance.rows || t_load_balance,
|
||||
posting_et: posting_et.rows || posting_et
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
const writePaynentHistory = async (data, message, i_log_name, trx) => {
|
||||
console.log(data)
|
||||
console.log(data)
|
||||
const result = await trx('log.t_log_invoice').insert({
|
||||
// i_log_name : 102,
|
||||
i_log_name : i_log_name,
|
||||
d_log : trx.raw('NOW()'),
|
||||
c_invoice_no : data?.c_invoice_no || '',
|
||||
c_partner_name : data?.c_partner_name || '',
|
||||
j_new_data : JSON.stringify(data),
|
||||
i_log_name: i_log_name,
|
||||
d_log: trx.raw('NOW()'),
|
||||
c_invoice_no: data?.c_invoice_no || '',
|
||||
c_partner_name: data?.c_partner_name || '',
|
||||
j_new_data: JSON.stringify(data),
|
||||
d_payment_date: trx.raw('NOW()'),
|
||||
c_note : message,
|
||||
c_status : 'S',
|
||||
d_created : trx.raw('NOW()'),
|
||||
m_amount : data?.amount || 0,
|
||||
m_mdr_amount : 0,
|
||||
m_mdr_net : data?.amount || 0
|
||||
c_note: message,
|
||||
c_status: 'S',
|
||||
d_created: trx.raw('NOW()'),
|
||||
m_amount: data?.amount || 0,
|
||||
m_mdr_amount: 0,
|
||||
m_mdr_net: data?.amount || 0
|
||||
|
||||
}, ['i_log_name', 'c_note'])
|
||||
|
||||
@@ -220,24 +361,24 @@ const writePaynentHistory = async (data, message, i_log_name, trx) => {
|
||||
|
||||
}
|
||||
|
||||
const getParameterSetting = async(trx, code) => {
|
||||
const getParameterSetting = async (trx, code) => {
|
||||
|
||||
const result = await trx('public.t_m_global_parameter_sap AS tmgps').select([
|
||||
const result = await trx('public.t_m_global_parameter_sap AS tmgps').select([
|
||||
"c_value"
|
||||
])
|
||||
.where('c_code', '=', code)
|
||||
.where('c_status', '=', "A")
|
||||
.first()
|
||||
.where('c_code', '=', code)
|
||||
.where('c_status', '=', "A")
|
||||
.first()
|
||||
|
||||
return result
|
||||
|
||||
}
|
||||
|
||||
const getInvoice = async(code, trx) => {
|
||||
const getInvoice = async (code, trx) => {
|
||||
|
||||
const result = await trx('payment.t_d_invoice')
|
||||
.where('c_invoice_no', '=', code)
|
||||
.first()
|
||||
const result = await trx('payment.t_d_invoice')
|
||||
.where('c_invoice_no', '=', code)
|
||||
.first()
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
// GET PARTNER, MERCHANT, TERMINAL FROM TID
|
||||
const generateInvoice = async (invoiceDate, repost, trx) => {
|
||||
|
||||
let result = await trx.select(trx.raw(`* FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`))
|
||||
let result = await trx.raw(
|
||||
`SELECT * FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`
|
||||
)
|
||||
|
||||
return result;
|
||||
return result.rows || result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -108,7 +108,14 @@ const getReservationPendingV2 = (cardNumber, time, flag) => {
|
||||
.whereIn('c_status', ['P', 'I'])
|
||||
// .where("d_expired", ">=", time)
|
||||
.whereRaw(
|
||||
`c_reservation_no NOT IN (SELECT c_reservation_no FROM trx.t_d_fraud tdf WHERE tdf.c_status = 'A' AND tdf.c_card_number = '${cardNumber}')`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM trx.t_d_fraud tdf
|
||||
WHERE tdf.c_reservation_no = trx.t_d_reservation_balance.c_reservation_no
|
||||
AND tdf.c_status = 'A'
|
||||
AND tdf.c_card_number = ?
|
||||
)`,
|
||||
[cardNumber]
|
||||
)
|
||||
.where(function () {
|
||||
this.orWhere('c_flag', flag).orWhere('c_flag', '-1');
|
||||
@@ -170,7 +177,14 @@ const getReservationPendingWithFlagCounterV2 = (cardNumber, time, flag) => {
|
||||
.whereIn('c_status', ['P', 'I'])
|
||||
.whereRaw('??::INT < ??', ['c_flag', parseInt(flag)])
|
||||
.whereRaw(
|
||||
`c_reservation_no NOT IN (SELECT c_reservation_no FROM trx.t_d_fraud tdf WHERE tdf.c_status = 'A' AND tdf.c_card_number = '${cardNumber}')`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM trx.t_d_fraud tdf
|
||||
WHERE tdf.c_reservation_no = trx.t_d_reservation_balance.c_reservation_no
|
||||
AND tdf.c_status = 'A'
|
||||
AND tdf.c_card_number = ?
|
||||
)`,
|
||||
[cardNumber]
|
||||
)
|
||||
// .where("d_expired", ">=", time)
|
||||
.where('c_flag', '!=', '-1');
|
||||
@@ -198,7 +212,14 @@ const getReservationPendingWithFlagV2 = (cardNumber, time) => {
|
||||
.where('c_card_number', cardNumber)
|
||||
.whereIn('c_status', ['P', 'I'])
|
||||
.whereRaw(
|
||||
`c_reservation_no NOT IN (SELECT c_reservation_no FROM trx.t_d_fraud tdf WHERE tdf.c_status = 'A' AND tdf.c_card_number = '${cardNumber}')`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM trx.t_d_fraud tdf
|
||||
WHERE tdf.c_reservation_no = trx.t_d_reservation_balance.c_reservation_no
|
||||
AND tdf.c_status = 'A'
|
||||
AND tdf.c_card_number = ?
|
||||
)`,
|
||||
[cardNumber]
|
||||
)
|
||||
.whereNotNull('c_flag_code');
|
||||
return result;
|
||||
@@ -727,7 +748,7 @@ const updateReservationAndLoadingBalance = async (
|
||||
})
|
||||
.then(() => {
|
||||
// winston.logger.debug(`${reservationNumber} commit transaction`);
|
||||
trx.commit();
|
||||
return trx.commit();
|
||||
})
|
||||
.catch((error) => {
|
||||
winston.logger.debug(
|
||||
@@ -876,17 +897,6 @@ const getReservationPendingWithoutFraud = (
|
||||
reservationNumber = []
|
||||
) => {
|
||||
if (reservationNumber.length > 0) {
|
||||
let rsv = '';
|
||||
if (reservationNumber.length > 0) {
|
||||
for (let index = 0; index < reservationNumber.length; index++) {
|
||||
if (index > 0) {
|
||||
rsv = rsv + ',';
|
||||
}
|
||||
|
||||
rsv = rsv + "'" + reservationNumber[index] + "'";
|
||||
}
|
||||
}
|
||||
|
||||
let result = knex('trx.t_d_reservation_balance')
|
||||
.select(
|
||||
'trx.t_d_reservation_balance.i_reservation_amount',
|
||||
@@ -896,7 +906,14 @@ const getReservationPendingWithoutFraud = (
|
||||
.where('trx.t_d_reservation_balance.c_card_number', cardNumber)
|
||||
.whereIn('trx.t_d_reservation_balance.c_status', ['P', 'I'])
|
||||
.whereRaw(
|
||||
`trx.t_d_reservation_balance.c_reservation_no NOT IN (SELECT c_reservation_no FROM trx.t_d_fraud WHERE trx.t_d_fraud.c_status = 'A' AND trx.t_d_fraud.c_reservation_no IN (${rsv}))`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM trx.t_d_fraud f
|
||||
WHERE f.c_reservation_no = trx.t_d_reservation_balance.c_reservation_no
|
||||
AND f.c_status = 'A'
|
||||
AND f.c_reservation_no = ANY(ARRAY[?])
|
||||
)`,
|
||||
[reservationNumber]
|
||||
)
|
||||
// .where("d_expired", ">=", time)
|
||||
.where(function () {
|
||||
@@ -946,17 +963,6 @@ const getLastHistory = (cardNumber) => {
|
||||
|
||||
// INSERT FRAUD TRANSACTION
|
||||
const insertFraudTransaction = (reservationNumber) => {
|
||||
let rsv = '';
|
||||
if (reservationNumber.length > 0) {
|
||||
for (let index = 0; index < reservationNumber.length; index++) {
|
||||
if (index > 0) {
|
||||
rsv = rsv + ',';
|
||||
}
|
||||
|
||||
rsv = rsv + "'" + reservationNumber[index] + "'";
|
||||
}
|
||||
}
|
||||
|
||||
let result = knex
|
||||
.into(
|
||||
knex.raw(
|
||||
@@ -1013,7 +1019,14 @@ const insertFraudTransaction = (reservationNumber) => {
|
||||
reservationNumber
|
||||
)
|
||||
.whereRaw(
|
||||
`trx.t_d_reservation_balance.c_reservation_no NOT IN (SELECT c_reservation_no FROM trx.t_d_fraud WHERE trx.t_d_fraud.c_status = 'A' AND trx.t_d_fraud.c_reservation_no IN (${rsv}))`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM trx.t_d_fraud f
|
||||
WHERE f.c_reservation_no = trx.t_d_reservation_balance.c_reservation_no
|
||||
AND f.c_status = 'A'
|
||||
AND f.c_reservation_no = ANY(ARRAY[?])
|
||||
)`,
|
||||
[reservationNumber]
|
||||
);
|
||||
});
|
||||
return result;
|
||||
@@ -1128,7 +1141,7 @@ const updateReservationAndInsertLoadingBalance = async (
|
||||
})
|
||||
.whereIn('c_reservation_no', reservationNumber);
|
||||
})
|
||||
.then(trx.commit)
|
||||
.then(() => trx.commit())
|
||||
.catch(trx.rollback);
|
||||
})
|
||||
.then((result) => {
|
||||
|
||||
@@ -80,7 +80,7 @@ const validate = (method) => {
|
||||
body('card_number')
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage('Card number is required')
|
||||
.withMessage('Card number is required'),
|
||||
];
|
||||
|
||||
//card_number, new_card_number, last_balance, security_code, unique_code
|
||||
@@ -1084,6 +1084,7 @@ const validateRedeem = async (req, res) => {
|
||||
let uniqueCode = helper.getUniqueCode();
|
||||
let result = {};
|
||||
let last_balance = null;
|
||||
let dataCardReplacement = {};
|
||||
let { card_number, station_code } = req.body;
|
||||
|
||||
console.log(
|
||||
@@ -1310,19 +1311,49 @@ const validateRedeem = async (req, res) => {
|
||||
|
||||
last_balance = dataCardReplacement.i_last_balance;
|
||||
if (last_balance == null) {
|
||||
result = {
|
||||
code: '415',
|
||||
message: 'Last Balance not updated.',
|
||||
data: {
|
||||
status: false,
|
||||
detail: {},
|
||||
let result = {};
|
||||
// const baseUrl = 'https://appdev.kci.id/cts/v2/api/info/last-balance'
|
||||
const baseUrl = `${process.env.CTS_URL}/v2/api/info/last-balance`;
|
||||
const payload = {
|
||||
card_number: card_number,
|
||||
};
|
||||
let config = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
};
|
||||
//LOG-INFO
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ RESPONSE VALIDATE-REDEEM ]: ${JSON.stringify(result)}`
|
||||
);
|
||||
return res.status(200).json(result);
|
||||
const url = baseUrl;
|
||||
const response = await axios.post(url, payload, config);
|
||||
if (response.data.code == undefined || response.data.code != 200) {
|
||||
if (response.data.code == 404) {
|
||||
result = {
|
||||
code: '494',
|
||||
message: 'No data found in CTS for the given card number',
|
||||
data: {
|
||||
status: false,
|
||||
detail: {},
|
||||
},
|
||||
};
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ FAILED HIT - card_number : ${card_number} ]: ${response.data.message}`
|
||||
);
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
result = {
|
||||
code: '415',
|
||||
message: 'Last Balance not updated.',
|
||||
data: {
|
||||
status: false,
|
||||
detail: {},
|
||||
},
|
||||
};
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ FAILED HIT - card_number : ${card_number} ]: ${response.data.message}`
|
||||
);
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
last_balance = response.data.data.balance_after;
|
||||
}
|
||||
|
||||
let security_code = await helper.createHMAC(card_number);
|
||||
@@ -1658,19 +1689,16 @@ const validateReplacement = async (req, res) => {
|
||||
// );
|
||||
// return res.status(200).json(result);
|
||||
|
||||
|
||||
|
||||
// if (is_warranty == true) {
|
||||
let result = {};
|
||||
const baseUrl = 'https://appdev.kci.id/cts/v2/api/info/last-balance'
|
||||
const bearer = 'Mv@2025qwertyuiopzxcvbnm'
|
||||
// const baseUrl = 'https://appdev.kci.id/cts/v2/api/info/last-balance'
|
||||
const baseUrl = `${process.env.CTS_URL}/v2/api/info/last-balance`;
|
||||
const payload = {
|
||||
card_number: card_number,
|
||||
};
|
||||
let config = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + bearer
|
||||
},
|
||||
};
|
||||
const url = baseUrl;
|
||||
@@ -1682,8 +1710,8 @@ const validateReplacement = async (req, res) => {
|
||||
message: 'No data found in CTS for the given card number',
|
||||
data: {
|
||||
status: false,
|
||||
detail: {}
|
||||
}
|
||||
detail: {},
|
||||
},
|
||||
};
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ FAILED HIT - card_number : ${card_number} ]: ${response.data.message}`
|
||||
@@ -1695,7 +1723,7 @@ const validateReplacement = async (req, res) => {
|
||||
message: 'Last Balance not updated.',
|
||||
data: {
|
||||
status: false,
|
||||
detail: {}
|
||||
detail: {},
|
||||
},
|
||||
};
|
||||
winston.logger.info(
|
||||
@@ -1705,10 +1733,13 @@ const validateReplacement = async (req, res) => {
|
||||
}
|
||||
last_balance = response.data.data.balance_after;
|
||||
let dataReplacementUpdate = {
|
||||
i_last_balance: last_balance
|
||||
i_last_balance: last_balance,
|
||||
};
|
||||
// update card_replacement
|
||||
await model.updateReplacementByCardNumber(card_number, dataReplacementUpdate);
|
||||
await model.updateReplacementByCardNumber(
|
||||
card_number,
|
||||
dataReplacementUpdate
|
||||
);
|
||||
// log debug
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ GET LAST BALANCE CTS ${card_number} ]: ${last_balance}`
|
||||
@@ -1934,8 +1965,7 @@ const validateNewCardReplacement = async (req, res) => {
|
||||
message: 'Successfully.',
|
||||
data: {
|
||||
status: true,
|
||||
detail: {
|
||||
},
|
||||
detail: {},
|
||||
},
|
||||
};
|
||||
//LOG-INFO
|
||||
@@ -2671,11 +2701,13 @@ const getLastBalanceCTS = async (req, res) => {
|
||||
try {
|
||||
let uniqueCode = helper.getUniqueCode();
|
||||
let result = {};
|
||||
const baseUrl = 'https://appdev.kci.id/cts/v2/api/info/last-balance'
|
||||
const bearer = 'Mv@2025qwertyuiopzxcvbnm'
|
||||
// const baseUrl = 'https://appdev.kci.id/cts/v2/api/info/last-balance'
|
||||
const baseUrl = `${process.env.CTS_URL}/v2/api/info/last-balance`;
|
||||
|
||||
console.log(
|
||||
`${uniqueCode} [ REQUEST GET LAST BALANCE CTS ]: ${JSON.stringify(req.body)}`
|
||||
`${uniqueCode} [ REQUEST GET LAST BALANCE CTS ]: ${JSON.stringify(
|
||||
req.body
|
||||
)}`
|
||||
);
|
||||
|
||||
//CARD REPLACEMENT VALIDATION
|
||||
@@ -2689,7 +2721,6 @@ const getLastBalanceCTS = async (req, res) => {
|
||||
let config = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + bearer
|
||||
},
|
||||
};
|
||||
const url = baseUrl;
|
||||
@@ -2701,8 +2732,8 @@ const getLastBalanceCTS = async (req, res) => {
|
||||
message: 'No data found in CTS for the given card number',
|
||||
data: {
|
||||
status: false,
|
||||
detail: {}
|
||||
}
|
||||
detail: {},
|
||||
},
|
||||
};
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ FAILED HIT - card_number : ${card.c_card_no} ]: ${response.data.message}`
|
||||
@@ -2711,11 +2742,11 @@ const getLastBalanceCTS = async (req, res) => {
|
||||
}
|
||||
result = {
|
||||
code: '500',
|
||||
message: "CTS service not avaliable",
|
||||
message: 'CTS service not avaliable',
|
||||
data: {
|
||||
status: false,
|
||||
detail: {}
|
||||
}
|
||||
detail: {},
|
||||
},
|
||||
};
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ FAILED HIT - card_number : ${card.c_card_no} ]: ${response.data.message}`
|
||||
@@ -2724,10 +2755,13 @@ const getLastBalanceCTS = async (req, res) => {
|
||||
}
|
||||
last_balance = response.data.data.balance_after;
|
||||
let dataReplacementUpdate = {
|
||||
i_last_balance: last_balance
|
||||
i_last_balance: last_balance,
|
||||
};
|
||||
// update card_replacement
|
||||
await model.updateReplacementByCardNumber(card.c_card_no, dataReplacementUpdate);
|
||||
await model.updateReplacementByCardNumber(
|
||||
card.c_card_no,
|
||||
dataReplacementUpdate
|
||||
);
|
||||
// log debug
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ GET LAST BALANCE CTS ${card.c_card_no} ]: ${last_balance}`
|
||||
@@ -2739,15 +2773,16 @@ const getLastBalanceCTS = async (req, res) => {
|
||||
message: 'Successfully.',
|
||||
data: {
|
||||
status: true,
|
||||
detail: {}
|
||||
}
|
||||
detail: {},
|
||||
},
|
||||
};
|
||||
//LOG-INFO
|
||||
winston.logger.info(
|
||||
`${uniqueCode} [ RESPONSE GET LAST BALANCE CTS ]: ${JSON.stringify(result)}`
|
||||
`${uniqueCode} [ RESPONSE GET LAST BALANCE CTS ]: ${JSON.stringify(
|
||||
result
|
||||
)}`
|
||||
);
|
||||
return res.status(200).json(result);
|
||||
|
||||
} catch (error) {
|
||||
// create log
|
||||
winston.logger.error(
|
||||
@@ -2761,8 +2796,8 @@ const getLastBalanceCTS = async (req, res) => {
|
||||
: '500 internal server error - backend server.',
|
||||
data: {
|
||||
status: false,
|
||||
detail: {}
|
||||
}
|
||||
detail: {},
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -2894,11 +2929,15 @@ const completeDataSalesInternal = async (req, res) => {
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
console.log(`${uniqueCode} [ COMPLETING DATA SALES INTERNAL - PPN ${ppn.c_value} % ]`);
|
||||
console.log(
|
||||
`${uniqueCode} [ COMPLETING DATA SALES INTERNAL - PPN ${ppn.c_value} % ]`
|
||||
);
|
||||
|
||||
ppn = parseFloat(ppn.c_value);
|
||||
|
||||
console.log(`${uniqueCode} [ COMPLETING DATA SALES INTERNAL - UPDATE DATA ]`);
|
||||
console.log(
|
||||
`${uniqueCode} [ COMPLETING DATA SALES INTERNAL - UPDATE DATA ]`
|
||||
);
|
||||
|
||||
//COMPLETING DATA SALES INTERNAL
|
||||
const response = await model.completingDataSalesInternal(ppn);
|
||||
@@ -2923,7 +2962,11 @@ const completeDataSalesInternal = async (req, res) => {
|
||||
data: {},
|
||||
};
|
||||
|
||||
console.log(`${uniqueCode} [ COMPLETING DATA SALES INTERNAL SUCCESS : ${JSON.stringify(result)} ]`);
|
||||
console.log(
|
||||
`${uniqueCode} [ COMPLETING DATA SALES INTERNAL SUCCESS : ${JSON.stringify(
|
||||
result
|
||||
)} ]`
|
||||
);
|
||||
return res.status(200).json(result);
|
||||
} catch (error) {
|
||||
// create log
|
||||
|
||||
@@ -170,6 +170,7 @@ const completingDataSalesInternal = async (ppn) => {
|
||||
trx.t_d_sales_internal.is_complete is not true
|
||||
AND trx.t_d_sales_internal.i_card_price > 0
|
||||
AND trx.t_d_sales_internal.i_sales_invoice_internal is null
|
||||
AND t_m_card_theme.c_status = 'A'
|
||||
ORDER BY
|
||||
trx.t_d_sales_internal.c_card_number,
|
||||
t_m_card_theme.c_card_type_code,
|
||||
@@ -208,12 +209,13 @@ const completingDataSalesInternal = async (ppn) => {
|
||||
SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 1, 1) = t_m_card_theme.c_card_type_code
|
||||
AND SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 2, 2) = t_m_card_theme.c_card_digit_key
|
||||
AND SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 4, 1) = t_m_card_theme.c_variant_code
|
||||
AND SUBSTRING(trx.t_d_sales_internal.c_card_number, 7, 2) = RIGHT(t_m_card_theme.i_year::text, 2)
|
||||
AND SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 7, 2) = RIGHT(t_m_card_theme.i_year::text, 2)
|
||||
WHERE
|
||||
trx.t_d_sales_internal.is_complete is not true
|
||||
AND trx.t_d_sales_internal.i_old_card_price > 0
|
||||
AND trx.t_d_sales_internal.c_category IN ('N', 'W')
|
||||
AND trx.t_d_sales_internal.i_sales_invoice_internal is null
|
||||
AND t_m_card_theme.c_status = 'A'
|
||||
ORDER BY
|
||||
trx.t_d_sales_internal.c_old_card_number,
|
||||
t_m_card_theme.c_card_type_code,
|
||||
@@ -275,6 +277,7 @@ const completingDataSalesNewCard = async (ppn) => {
|
||||
trx.t_d_sales_internal.is_complete is not true
|
||||
AND trx.t_d_sales_internal.i_card_price > 0
|
||||
AND trx.t_d_sales_internal.i_sales_invoice_internal is null
|
||||
AND t_m_card_theme.c_status = 'A'
|
||||
ORDER BY
|
||||
trx.t_d_sales_internal.c_card_number,
|
||||
t_m_card_theme.c_card_type_code,
|
||||
@@ -314,12 +317,13 @@ const completingDataSalesOldCard = async (ppn) => {
|
||||
SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 1, 1) = t_m_card_theme.c_card_type_code
|
||||
AND SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 2, 2) = t_m_card_theme.c_card_digit_key
|
||||
AND SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 4, 1) = t_m_card_theme.c_variant_code
|
||||
AND SUBSTRING(trx.t_d_sales_internal.c_card_number, 7, 2) = RIGHT(t_m_card_theme.i_year::text, 2)
|
||||
AND SUBSTRING(trx.t_d_sales_internal.c_old_card_number, 7, 2) = RIGHT(t_m_card_theme.i_year::text, 2)
|
||||
WHERE
|
||||
trx.t_d_sales_internal.is_complete is not true
|
||||
AND trx.t_d_sales_internal.i_old_card_price > 0
|
||||
AND trx.t_d_sales_internal.c_category IN ('N', 'W')
|
||||
AND trx.t_d_sales_internal.i_sales_invoice_internal is null
|
||||
AND t_m_card_theme.c_status = 'A'
|
||||
ORDER BY
|
||||
trx.t_d_sales_internal.c_old_card_number,
|
||||
t_m_card_theme.c_card_type_code,
|
||||
|
||||
@@ -651,7 +651,7 @@ const reservationValidate = async (req, res) => {
|
||||
let totalReservationPending =
|
||||
getReservationPending.total_nominal_reservation > 0
|
||||
? parseInt(getReservationPending.total_nominal_reservation) +
|
||||
parseInt(amount)
|
||||
parseInt(amount)
|
||||
: parseInt(amount);
|
||||
|
||||
// jika total amount reservasi melebihi maksimum reservasi sesuai dengan parameter setting di sistem CMS maka transaksi akan di tolak
|
||||
@@ -1254,7 +1254,7 @@ const reservation = async (req, res) => {
|
||||
try {
|
||||
// generate unique code
|
||||
// unique code ini digunakan untuk keperluan log agar mudah untuk dibaca
|
||||
let uniqueCode = helper.getUniqueCode();
|
||||
let uniqueCode = await helper.getUniqueCode();
|
||||
let result = {};
|
||||
|
||||
// param
|
||||
@@ -1595,7 +1595,7 @@ const reservation = async (req, res) => {
|
||||
let totalReservationPending =
|
||||
getReservationPending.total_nominal_reservation > 0
|
||||
? parseInt(getReservationPending.total_nominal_reservation) +
|
||||
parseInt(amount)
|
||||
parseInt(amount)
|
||||
: parseInt(amount);
|
||||
|
||||
// jika total amount reservasi melebihi maksimum reservasi sesuai dengan parameter setting di sistem CMS maka transaksi akan di tolak
|
||||
@@ -1712,11 +1712,11 @@ const reservation = async (req, res) => {
|
||||
// create log
|
||||
winston.logger.error(`server is busy. please try again.`);
|
||||
|
||||
return {
|
||||
return res.status(200).json({
|
||||
code: '500',
|
||||
message: 'server is busy. please try again.',
|
||||
data: {},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
maxLoop++;
|
||||
@@ -1841,9 +1841,8 @@ const reservation = async (req, res) => {
|
||||
m_balance_after: balance > 0 ? balance : 0,
|
||||
j_new_data: data,
|
||||
j_old_data: null,
|
||||
c_note: `RESERVED BALANCE ${amount} FROM ${
|
||||
getPartner ? getPartner.partner_name : null
|
||||
} SUCCESSFULLY`,
|
||||
c_note: `RESERVED BALANCE ${amount} FROM ${getPartner ? getPartner.partner_name : null
|
||||
} SUCCESSFULLY`,
|
||||
c_status: 'S',
|
||||
d_created: now,
|
||||
i_create: null,
|
||||
@@ -1923,7 +1922,7 @@ const reservationV2 = async (req, res) => {
|
||||
try {
|
||||
// generate unique code
|
||||
// unique code ini digunakan untuk keperluan log agar mudah untuk dibaca
|
||||
let uniqueCode = helper.getUniqueCode();
|
||||
let uniqueCode = await helper.getUniqueCode();
|
||||
let result = {};
|
||||
|
||||
// param
|
||||
@@ -1971,7 +1970,7 @@ const reservationV2 = async (req, res) => {
|
||||
|
||||
try {
|
||||
reservationData = helper.decryptText(reservation_data);
|
||||
} catch (error) {}
|
||||
} catch (error) { }
|
||||
|
||||
if (!reservationData) {
|
||||
result = {
|
||||
@@ -2547,11 +2546,11 @@ const reservationV2 = async (req, res) => {
|
||||
// create log
|
||||
winston.logger.error(`server is busy. please try again.`);
|
||||
|
||||
return {
|
||||
return res.status(200).json({
|
||||
code: '500',
|
||||
message: 'server is busy. please try again.',
|
||||
data: {},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
maxLoop++;
|
||||
@@ -2678,9 +2677,8 @@ const reservationV2 = async (req, res) => {
|
||||
m_balance_after: balance > 0 ? balance : 0,
|
||||
j_new_data: data,
|
||||
j_old_data: null,
|
||||
c_note: `RESERVED BALANCE ${amount} FROM ${
|
||||
getPartner ? getPartner.partner_name : null
|
||||
} SUCCESSFULLY`,
|
||||
c_note: `RESERVED BALANCE ${amount} FROM ${getPartner ? getPartner.partner_name : null
|
||||
} SUCCESSFULLY`,
|
||||
c_status: 'S',
|
||||
d_created: now,
|
||||
i_create: null,
|
||||
@@ -2884,8 +2882,8 @@ const cancelReservationValidate = async (req, res) => {
|
||||
// jika status dari reservation number yang di request adalah X = Deleted, maka proses cancel reservasi akan di tolak
|
||||
let cancelDate = getReservationCanBeCanceled.d_cancel
|
||||
? moment(getReservationCanBeCanceled.d_cancel).format(
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
: '';
|
||||
result = {
|
||||
code: '430',
|
||||
@@ -3075,8 +3073,8 @@ const cancelReservation = async (req, res) => {
|
||||
} else if (getReservationCanBeCanceled.c_status == 'X') {
|
||||
let cancelDate = getReservationCanBeCanceled.d_cancel
|
||||
? moment(getReservationCanBeCanceled.d_cancel).format(
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
: '';
|
||||
result = {
|
||||
code: '430',
|
||||
@@ -3176,9 +3174,8 @@ const cancelReservation = async (req, res) => {
|
||||
m_balance_after: balance > 0 ? balance : 0,
|
||||
j_new_data: data,
|
||||
j_old_data: getReservationCanBeCanceled,
|
||||
c_note: `CANCEL RESERVE BALANCE ${
|
||||
getReservationCanBeCanceled.i_reservation_amount
|
||||
} FROM ${getPartner ? getPartner.partner_name : null}`,
|
||||
c_note: `CANCEL RESERVE BALANCE ${getReservationCanBeCanceled.i_reservation_amount
|
||||
} FROM ${getPartner ? getPartner.partner_name : null}`,
|
||||
c_status: 'S',
|
||||
d_created: now,
|
||||
i_create: null,
|
||||
@@ -3329,6 +3326,14 @@ const inquiryReservationByReservation = async (req, res) => {
|
||||
|
||||
// check reservation
|
||||
result = await model.inquiryListByRefNumber(ref_no, show_complete_data);
|
||||
|
||||
if (!result) {
|
||||
return res.status(200).json({
|
||||
code: '414',
|
||||
message: "reservation doesn't exist",
|
||||
data: {},
|
||||
});
|
||||
}
|
||||
} else if (req.query.search === 'load-balance-number') {
|
||||
// validation parameter inquiry
|
||||
if (!load_balance_number) {
|
||||
@@ -3344,6 +3349,14 @@ const inquiryReservationByReservation = async (req, res) => {
|
||||
load_balance_number,
|
||||
show_complete_data
|
||||
);
|
||||
|
||||
if (result.length < 1) {
|
||||
return res.status(200).json({
|
||||
code: '414',
|
||||
message: "reservation doesn't exist",
|
||||
data: {},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// validation parameter inquiry
|
||||
if (!reservation_number) {
|
||||
@@ -3359,14 +3372,14 @@ const inquiryReservationByReservation = async (req, res) => {
|
||||
reservation_number,
|
||||
show_complete_data
|
||||
);
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
return res.status(200).json({
|
||||
code: '414',
|
||||
message: "reservation doesn't exist",
|
||||
data: {},
|
||||
});
|
||||
if (!result) {
|
||||
return res.status(200).json({
|
||||
code: '414',
|
||||
message: "reservation doesn't exist",
|
||||
data: {},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return res.json({
|
||||
@@ -3722,8 +3735,8 @@ const cancelReservationValidateV2 = async (req, res) => {
|
||||
// jika status dari reservation number yang di request adalah X = Deleted, maka proses cancel reservasi akan di tolak
|
||||
let cancelDate = getReservationCanBeCanceled.d_cancel
|
||||
? moment(getReservationCanBeCanceled.d_cancel).format(
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
: '';
|
||||
result = {
|
||||
code: '430',
|
||||
@@ -3879,7 +3892,7 @@ const cancelReservationV2 = async (req, res) => {
|
||||
|
||||
try {
|
||||
cancelReservationData = helper.decryptText(cancel_reservation_data);
|
||||
} catch (error) {}
|
||||
} catch (error) { }
|
||||
|
||||
if (!cancelReservationData) {
|
||||
result = {
|
||||
@@ -3989,8 +4002,8 @@ const cancelReservationV2 = async (req, res) => {
|
||||
} else if (getReservationCanBeCanceled.c_status == 'X') {
|
||||
let cancelDate = getReservationCanBeCanceled.d_cancel
|
||||
? moment(getReservationCanBeCanceled.d_cancel).format(
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
: '';
|
||||
result = {
|
||||
code: '430',
|
||||
@@ -4093,9 +4106,8 @@ const cancelReservationV2 = async (req, res) => {
|
||||
m_balance_after: balance > 0 ? balance : 0,
|
||||
j_new_data: data,
|
||||
j_old_data: getReservationCanBeCanceled,
|
||||
c_note: `CANCEL RESERVE BALANCE ${
|
||||
getReservationCanBeCanceled.i_reservation_amount
|
||||
} FROM ${getPartner ? getPartner.partner_name : null}`,
|
||||
c_note: `CANCEL RESERVE BALANCE ${getReservationCanBeCanceled.i_reservation_amount
|
||||
} FROM ${getPartner ? getPartner.partner_name : null}`,
|
||||
c_status: 'S',
|
||||
d_created: now,
|
||||
i_create: null,
|
||||
@@ -4249,8 +4261,8 @@ const cancelReservationV2_1 = async (req, res) => {
|
||||
// jika status dari reservation number yang di request adalah X = Deleted, maka proses cancel reservasi akan di tolak
|
||||
let cancelDate = getReservationCanBeCanceled.d_cancel
|
||||
? moment(getReservationCanBeCanceled.d_cancel).format(
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
'DD-MM-YYYY HH:mm:ss'
|
||||
)
|
||||
: '';
|
||||
result = {
|
||||
code: '430',
|
||||
@@ -4376,9 +4388,8 @@ const cancelReservationV2_1 = async (req, res) => {
|
||||
m_balance_after: balance > 0 ? balance : 0,
|
||||
j_new_data: data,
|
||||
j_old_data: getReservationCanBeCanceled,
|
||||
c_note: `CANCEL RESERVE BALANCE ${
|
||||
getReservationCanBeCanceled.i_reservation_amount
|
||||
} FROM ${getPartner ? getPartner.partner_name : null}`,
|
||||
c_note: `CANCEL RESERVE BALANCE ${getReservationCanBeCanceled.i_reservation_amount
|
||||
} FROM ${getPartner ? getPartner.partner_name : null}`,
|
||||
c_status: 'S',
|
||||
d_created: now,
|
||||
i_create: null,
|
||||
|
||||
@@ -152,7 +152,7 @@ const getMaxReservation = () => {
|
||||
};
|
||||
|
||||
// INQUIRY BY RESERVATION NUMBER
|
||||
let inquiryByReservationNumber = (reservationNumber, show_complete_data) => {
|
||||
const inquiryByReservationNumber = (reservationNumber, show_complete_data) => {
|
||||
let result = knex('trx.t_d_reservation_balance');
|
||||
if (show_complete_data === true) {
|
||||
result.select(
|
||||
@@ -198,14 +198,14 @@ let inquiryByReservationNumber = (reservationNumber, show_complete_data) => {
|
||||
);
|
||||
});
|
||||
|
||||
result
|
||||
result = result
|
||||
.where('trx.t_d_reservation_balance.c_reservation_no', reservationNumber)
|
||||
.first();
|
||||
return result;
|
||||
};
|
||||
|
||||
// INQUIRY LIST RESERVATION
|
||||
let inquiryListReservation = (partnerID, startDate, endDate) => {
|
||||
const inquiryListReservation = (partnerID, startDate, endDate) => {
|
||||
let result = knex('trx.t_d_reservation_balance');
|
||||
result.select(
|
||||
'trx.t_d_reservation_balance.c_reservation_no as reservation_number',
|
||||
@@ -253,7 +253,7 @@ let inquiryListReservation = (partnerID, startDate, endDate) => {
|
||||
};
|
||||
|
||||
// INQUIRY BY CARD NUMBER
|
||||
let inquiryByCardNumber = (cardNumber, startDate, endDate) => {
|
||||
const inquiryByCardNumber = (cardNumber, startDate, endDate) => {
|
||||
let result = knex('trx.t_d_reservation_balance')
|
||||
.select(
|
||||
'c_reservation_no as reservation_number',
|
||||
@@ -286,7 +286,14 @@ const getReservationPending = (cardNumber, time) => {
|
||||
.where('c_card_number', cardNumber)
|
||||
.where('c_status', 'P')
|
||||
.whereRaw(
|
||||
`c_reservation_no NOT IN (SELECT c_reservation_no FROM trx.t_d_fraud tdf WHERE tdf.c_status = 'A' AND tdf.c_card_number = '${cardNumber}')`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM trx.t_d_fraud f
|
||||
WHERE f.c_reservation_no = trx.t_d_reservation_balance.c_reservation_no
|
||||
AND f.c_status = 'A'
|
||||
AND f.c_card_number = ?
|
||||
)`,
|
||||
[cardNumber]
|
||||
)
|
||||
// .where("d_expired", ">=", time)
|
||||
.first();
|
||||
@@ -310,7 +317,14 @@ const getReservationPendingV2 = (cardNumber, time) => {
|
||||
.where('c_card_number', cardNumber)
|
||||
.where('c_status', 'P')
|
||||
.whereRaw(
|
||||
`c_reservation_no NOT IN (SELECT c_reservation_no FROM trx.t_d_fraud tdf WHERE tdf.c_status = 'A' AND tdf.c_card_number = '${cardNumber}')`
|
||||
`NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM trx.t_d_fraud f
|
||||
WHERE f.c_reservation_no = trx.t_d_reservation_balance.c_reservation_no
|
||||
AND f.c_status = 'A'
|
||||
AND f.c_card_number = ?
|
||||
)`,
|
||||
[cardNumber]
|
||||
);
|
||||
|
||||
return result;
|
||||
@@ -425,7 +439,7 @@ const deleteReservationNumberValidate = (reservationNumber) => {
|
||||
};
|
||||
|
||||
// INQUIRY BY REF NUMBER
|
||||
let inquiryListByRefNumber = (refNumber, show_complete_data) => {
|
||||
const inquiryListByRefNumber = (refNumber, show_complete_data) => {
|
||||
let result = knex('trx.t_d_reservation_balance');
|
||||
if (show_complete_data === true) {
|
||||
result.select(
|
||||
@@ -469,7 +483,7 @@ let inquiryListByRefNumber = (refNumber, show_complete_data) => {
|
||||
);
|
||||
});
|
||||
|
||||
result.where('trx.t_d_reservation_balance.c_ref_no', refNumber).first();
|
||||
result = result.where('trx.t_d_reservation_balance.c_ref_no', refNumber).first();
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
# Cash-in Index Checklist
|
||||
|
||||
These indexes are suggested for the heavy cash-in read paths in `application/modules/cashin/cahsin.model.js`.
|
||||
|
||||
Apply them first in a staging environment and validate with `EXPLAIN (ANALYZE, BUFFERS)` before promoting to production.
|
||||
|
||||
## 1. Reservation balance lookup
|
||||
|
||||
Used by `getReserve()` and `getPartnerRecon()`.
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdrb_cash_in_lookup
|
||||
ON trx.t_d_reservation_balance (c_partner_code, c_invoice_no, c_reservation_no)
|
||||
WHERE c_status = 'A'
|
||||
AND c_load_balance_no IS NOT NULL
|
||||
AND c_cash_in_no IS NULL;
|
||||
```
|
||||
|
||||
## 2. Reservation balance join key
|
||||
|
||||
Supports joins to payment recon and load balance.
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdrb_load_balance_no
|
||||
ON trx.t_d_reservation_balance (c_load_balance_no);
|
||||
```
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdrb_reservation_no
|
||||
ON trx.t_d_reservation_balance (c_reservation_no);
|
||||
```
|
||||
|
||||
## 3. Payment recon lookup
|
||||
|
||||
Used by `getReserve()`, `getPartnerRecon()`, and related sales/topup queries.
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdpr_reservation_status_payment
|
||||
ON payment.t_d_payment_recon (c_reservation_no, d_payment)
|
||||
WHERE c_status = 'S';
|
||||
```
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdpr_sales_status_payment
|
||||
ON payment.t_d_payment_recon_sales_kmt_mobile (c_sales_no, d_payment)
|
||||
WHERE c_status = 'S';
|
||||
```
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdpr_sales_internal_status_payment
|
||||
ON payment.t_d_payment_recon_sales_internal (c_sales_no, d_payment)
|
||||
WHERE c_status = 'S';
|
||||
```
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdpr_topup_internal_status_payment
|
||||
ON payment.t_d_payment_recon_topup_cashless_internal (c_topup_no, d_payment)
|
||||
WHERE c_status = 'S';
|
||||
```
|
||||
|
||||
## 4. Invoice lookup
|
||||
|
||||
Used by the cash-in and recon queries.
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdi_invoice_status_return
|
||||
ON payment.t_d_invoice (c_invoice_no)
|
||||
WHERE c_status <> 'X'
|
||||
AND c_return_sap IS NOT NULL;
|
||||
```
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdsi_invoice_status_return
|
||||
ON payment.t_d_sales_invoice (c_invoice_no)
|
||||
WHERE c_status <> 'X'
|
||||
AND c_return_sap IS NOT NULL;
|
||||
```
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdsi_internal_invoice_status_return
|
||||
ON payment.t_d_sales_invoice_internal (c_invoice_no)
|
||||
WHERE c_status <> 'X'
|
||||
AND c_return_sap_fi IS NOT NULL;
|
||||
```
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdsi_topup_internal_invoice_status_return
|
||||
ON payment.t_d_topup_cashless_invoice_internal (c_invoice_no)
|
||||
WHERE c_status <> 'X'
|
||||
AND c_return_sap IS NOT NULL;
|
||||
```
|
||||
|
||||
-- Indexes to optimize performance when posting invoice online topup
|
||||
```sql
|
||||
CREATE INDEX idx_posting_et_reference ON public.posting_et(reference);
|
||||
CREATE INDEX idx_posting_et_tcode_status ON public.posting_et(tcode, pos_status);
|
||||
```
|
||||
|
||||
## 5. Cash-in detail anti-join
|
||||
|
||||
Used by `getSalesKMT()`, `getSalesInternal()`, and `getTopUpCashlessInternal()` to exclude already-posted rows.
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdcid_reservation_not_reversed
|
||||
ON payment.t_d_cash_in_detail (c_reservation_no)
|
||||
WHERE b_reversal IS NOT TRUE;
|
||||
```
|
||||
|
||||
## 6. Cash-in code generation
|
||||
|
||||
Used by `generateCode()`.
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tdcashin_code_prefix
|
||||
ON payment.t_d_cash_in (c_cash_in_no);
|
||||
```
|
||||
|
||||
If prefix lookups still scan too much data, consider a functional index on the prefix pattern that matches your code format.
|
||||
|
||||
## 7. Partner lookup
|
||||
|
||||
Used by recon queries that join through `public.t_m_partner`.
|
||||
|
||||
```sql
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_tmp_i_partner
|
||||
ON public.t_m_partner (i_partner);
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- `CONCURRENTLY` avoids blocking writes, but it cannot run inside a transaction.
|
||||
- Partial indexes are the best fit here because the queries always filter on status and posting flags.
|
||||
- After creating indexes, run `EXPLAIN (ANALYZE, BUFFERS)` on:
|
||||
- `getReserve()`
|
||||
- `getPartnerRecon()`
|
||||
- `getPartnerReconSales()`
|
||||
- `getPartnerReconSalesInternal()`
|
||||
- `getPartnerReconTopUpCashlessInternal()`
|
||||
- If you want, I can also draft a migration file for these indexes in Knex format.
|
||||
@@ -0,0 +1,143 @@
|
||||
-- =========================================================
|
||||
-- RECOMMENDED INDEXES FOR PRODUCTION
|
||||
-- =========================================================
|
||||
|
||||
-- Main filtering index
|
||||
CREATE INDEX IF NOT EXISTS idx_posting_et_main_filter
|
||||
ON public.posting_et (
|
||||
tcode,
|
||||
pos_status,
|
||||
fiscal_year
|
||||
);
|
||||
|
||||
-- Prefix LIKE 'INV%'
|
||||
CREATE INDEX IF NOT EXISTS idx_posting_et_doc_header
|
||||
ON public.posting_et (doc_header_text);
|
||||
|
||||
-- Join & aggregation optimization
|
||||
CREATE INDEX IF NOT EXISTS idx_posting_et_reference_debit
|
||||
ON public.posting_et (
|
||||
reference,
|
||||
debit_kredit
|
||||
);
|
||||
|
||||
-- Optional covering index for large tables
|
||||
CREATE INDEX IF NOT EXISTS idx_posting_et_covering
|
||||
ON public.posting_et (
|
||||
tcode,
|
||||
pos_status,
|
||||
fiscal_year,
|
||||
reference
|
||||
);
|
||||
|
||||
|
||||
|
||||
-- =========================================================
|
||||
-- PRODUCTION QUERY
|
||||
-- =========================================================
|
||||
|
||||
WITH params AS (
|
||||
|
||||
SELECT
|
||||
EXTRACT(YEAR FROM CURRENT_DATE)::TEXT AS curr_year,
|
||||
EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year')::TEXT AS prev_year,
|
||||
TO_CHAR(CURRENT_DATE, 'MM') AS curr_month
|
||||
|
||||
),
|
||||
|
||||
base AS (
|
||||
|
||||
SELECT
|
||||
pe.doc_header_text,
|
||||
pe.ba,
|
||||
pe.doc_date,
|
||||
pe.posting_date,
|
||||
pe.fiscal_year,
|
||||
TO_CHAR(pe.doc_date::DATE, 'MM') AS fis_period,
|
||||
pe.doc_type,
|
||||
pe.reference,
|
||||
pe.tcode,
|
||||
pe.amount,
|
||||
pe.customer,
|
||||
pe.account,
|
||||
pe.text,
|
||||
pe.debit_kredit
|
||||
|
||||
FROM public.posting_et pe
|
||||
CROSS JOIN params p
|
||||
|
||||
WHERE
|
||||
pe.tcode = 'FB70'
|
||||
AND pe.pos_status = '3'
|
||||
|
||||
-- Prefix search still index friendly
|
||||
AND pe.doc_header_text LIKE 'INV%'
|
||||
|
||||
AND (
|
||||
|
||||
-- Current year all months
|
||||
pe.fiscal_year = p.curr_year
|
||||
|
||||
OR (
|
||||
|
||||
-- Previous year December only
|
||||
-- only when current month is January
|
||||
p.curr_month = '01'
|
||||
|
||||
AND pe.fiscal_year = p.prev_year
|
||||
|
||||
AND TO_CHAR(pe.doc_date::DATE, 'MM') = '12'
|
||||
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
SELECT
|
||||
b.doc_header_text,
|
||||
b.ba,
|
||||
b.doc_date,
|
||||
b.posting_date,
|
||||
b.fiscal_year,
|
||||
b.fis_period,
|
||||
b.doc_type,
|
||||
b.reference,
|
||||
b.tcode,
|
||||
b.amount,
|
||||
'IDR'::VARCHAR AS currency,
|
||||
|
||||
MAX(b.customer) FILTER (
|
||||
WHERE b.debit_kredit = 'S'
|
||||
) AS customer,
|
||||
|
||||
MAX(b.account) FILTER (
|
||||
WHERE b.debit_kredit = 'H'
|
||||
) AS account,
|
||||
|
||||
MAX(b.text) FILTER (
|
||||
WHERE b.debit_kredit = 'S'
|
||||
) AS text1,
|
||||
|
||||
MAX(b.text) FILTER (
|
||||
WHERE b.debit_kredit = 'H'
|
||||
) AS text2
|
||||
|
||||
FROM base b
|
||||
|
||||
GROUP BY
|
||||
b.doc_header_text,
|
||||
b.ba,
|
||||
b.doc_date,
|
||||
b.posting_date,
|
||||
b.fiscal_year,
|
||||
b.fis_period,
|
||||
b.doc_type,
|
||||
b.reference,
|
||||
b.tcode,
|
||||
b.amount
|
||||
|
||||
ORDER BY
|
||||
b.fiscal_year,
|
||||
b.doc_date,
|
||||
b.reference;
|
||||
@@ -0,0 +1,20 @@
|
||||
-- Indexes to optimize performance
|
||||
CREATE INDEX CONCURRENTLY idx_tdl_d_load_balance_void
|
||||
ON trx.t_d_load_balance(d_load_balance, b_void, c_invoice_no);
|
||||
|
||||
CREATE INDEX CONCURRENTLY idx_tdrb_invoice_status
|
||||
ON trx.t_d_reservation_balance(c_invoice_no, c_status);
|
||||
|
||||
CREATE INDEX CONCURRENTLY idx_tmp_b_invoice
|
||||
ON public.t_m_partner(i_partner, b_invoice);
|
||||
|
||||
CREATE INDEX CONCURRENTLY idx_tdprt_date_status
|
||||
ON payment.t_d_payment_request_topup_online(d_date_payment_request, b_sent, b_void);
|
||||
|
||||
-- Note:
|
||||
-- Expected Performance Gain: 10-100x faster depending on partner volume and data size.
|
||||
|
||||
|
||||
-- Indexes to optimize performance when posting invoice online topup
|
||||
CREATE INDEX idx_posting_et_reference ON public.posting_et(reference);
|
||||
CREATE INDEX idx_posting_et_tcode_status ON public.posting_et(tcode, pos_status);
|
||||
Generated
+2606
-8412
File diff suppressed because it is too large
Load Diff
+9
-7
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "topuponline",
|
||||
"version": "1.6.1",
|
||||
"description": "pos cms integration",
|
||||
"name": "onlinetopup-v1",
|
||||
"version": "1.6.6",
|
||||
"description": "optimize query and coding enhancement",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "nodemon index",
|
||||
@@ -23,7 +23,7 @@
|
||||
"homepage": "https://github.com/mohamadihsan/topuponline#readme",
|
||||
"dependencies": {
|
||||
"axios": "^1.7.7",
|
||||
"bcrypt": "^5.0.1",
|
||||
"bcrypt": "^4.0.1",
|
||||
"body-parser": "^1.19.0",
|
||||
"check-internet-connected": "^2.0.6",
|
||||
"cors": "^2.8.5",
|
||||
@@ -37,11 +37,13 @@
|
||||
"moment": "^2.29.3",
|
||||
"node-cron": "^3.0.1",
|
||||
"pg": "^8.6.0",
|
||||
"pm2": "^5.2.0",
|
||||
"pm2": "^6.0.13",
|
||||
"prom-client": "^14.2.0",
|
||||
"redis": "^4.6.5",
|
||||
"response-time": "^2.3.2",
|
||||
"simple-update-notifier": "^2.0.0",
|
||||
"sticky-session": "^0.1.0",
|
||||
"superagent": "^10.2.3",
|
||||
"winston": "^3.7.2",
|
||||
"winston-daily-rotate-file": "^4.7.1",
|
||||
"winston-loki": "^6.0.6",
|
||||
@@ -50,8 +52,8 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^29.0.2",
|
||||
"nodemon": "^2.0.7",
|
||||
"supertest": "^6.2.4"
|
||||
"nodemon": "^3.1.10",
|
||||
"supertest": "^7.1.3"
|
||||
},
|
||||
"jest": {
|
||||
"testEnvironment": "node",
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
-- query untuk mengecek invoice date yang terlambat tergenerate lebih dari 1 bulan setelah load balance date
|
||||
|
||||
SELECT t.d_reservation::date, t.d_load_balance::date, TO_DATE(SUBSTRING(t.c_invoice_no FROM 7 FOR 6), 'YYMMDD') as d_invoice, t.c_invoice_no, t.c_in_period, t.c_cash_in_no
|
||||
FROM trx.t_d_reservation_balance t
|
||||
WHERE
|
||||
t.d_reservation > '2026-01-01' AND
|
||||
ABS(
|
||||
t.d_load_balance::date
|
||||
- TO_DATE(SUBSTRING(t.c_invoice_no FROM 7 FOR 6), 'YYMMDD')
|
||||
) > 30;
|
||||
Reference in New Issue
Block a user