mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 10:46:28 +00:00
enhancement slow query
This commit is contained in:
@@ -323,7 +323,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 +330,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 = [];
|
||||
await knex.transaction(async (trx) => {
|
||||
const reserv = await model.getReserve(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
@@ -370,52 +339,56 @@ 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
|
||||
);
|
||||
if (cashInAmount <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let m_amount = 0;
|
||||
let runningAmount = 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 = {
|
||||
runningAmount += parseInt(detail.i_reservation_amount, 10);
|
||||
if (runningAmount > parseInt(detail.invoice_amount, 10)) {
|
||||
throw new Error(
|
||||
`ROLLBACK CASH-IN: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${runningAmount} > INVOICE AMOUNT (${detail.invoice_amount})`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -423,61 +396,27 @@ const _generateCashIn = async (uniqueCode) => {
|
||||
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);
|
||||
await model.updateIntoReserveBulk(trx, dataReserve, reserveNos);
|
||||
await model.insertCashInDetailBulk(trx, cashInDetails);
|
||||
|
||||
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
|
||||
)}`
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN ${code} SUCCESS. partner=${item.c_partner_code}, invoice=${item.c_invoice_no}, rows=${cashInDetails.length}, amount=${cashInAmount}`
|
||||
);
|
||||
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
|
||||
)}`
|
||||
);
|
||||
}
|
||||
await trx.commit();
|
||||
|
||||
} catch (error) {
|
||||
sts = "error";
|
||||
await trx.rollback();
|
||||
}
|
||||
|
||||
});
|
||||
} 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 +424,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
|
||||
await knex.transaction(async (trx) => {
|
||||
const sales = await model.getSalesKMT(
|
||||
trx,
|
||||
item.c_partner_code,
|
||||
@@ -523,88 +433,83 @@ const _generateCashInSales = async (uniqueCode) => {
|
||||
item.c_invoice_no
|
||||
);
|
||||
|
||||
winston.logger.info(
|
||||
`SCHEDULER-FB70-${uniqueCode} DATA SALES ${code} : ${JSON.stringify( sales )}`
|
||||
);
|
||||
|
||||
//TOTAL AMOUNT CASH-IN
|
||||
let cashInAmount = 0;
|
||||
for (const itemCI of sales) {
|
||||
cashInAmount += parseInt(itemCI.i_total_billing);
|
||||
if (!sales || sales.length === 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
|
||||
)}`
|
||||
);
|
||||
const paymentyHistory = await model.writePaynentHistory(
|
||||
dataCahsIn, 'SUCCESSFULLY', 0, trx
|
||||
const cashInAmount = sales.reduce(
|
||||
(sum, row) => sum + parseInt(row.i_total_billing, 10),
|
||||
0
|
||||
);
|
||||
|
||||
let m_amount = 0;
|
||||
if (cashInAmount <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let runningAmount = 0;
|
||||
for (const detail of sales) {
|
||||
let temp = {
|
||||
runningAmount += parseInt(detail.i_total_billing, 10);
|
||||
if (runningAmount > parseInt(detail.invoice_amount, 10)) {
|
||||
throw new Error(
|
||||
`ROLLBACK CASH-IN: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${runningAmount} > INVOICE AMOUNT (${detail.invoice_amount})`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
}));
|
||||
|
||||
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);
|
||||
await model.updateIntoSalesKMTBulk(trx, dataSales, salesNos);
|
||||
await model.insertCashInDetailBulk(trx, cashInDetails);
|
||||
|
||||
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
|
||||
)}`
|
||||
`SCHEDULER-FB70-${uniqueCode} INSERT CASH-IN SALES ${code} SUCCESS. partner=${item.c_partner_code}, invoice=${item.c_invoice_no}, rows=${cashInDetails.length}, amount=${cashInAmount}`
|
||||
);
|
||||
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();
|
||||
}
|
||||
|
||||
});
|
||||
} 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,37 +517,7 @@ 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}`
|
||||
);
|
||||
|
||||
// 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,
|
||||
@@ -650,79 +525,69 @@ const _generateCashInSalesInternal = async (uniqueCode) => {
|
||||
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);
|
||||
if (!sales || sales.length === 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
|
||||
)}`
|
||||
);
|
||||
const paymentyHistory = await model.writePaynentHistory(
|
||||
dataCahsIn, 'SUCCESSFULLY', 0, trx
|
||||
const cashInAmount = sales.reduce(
|
||||
(sum, row) => sum + parseInt(row.i_total_price, 10),
|
||||
0
|
||||
);
|
||||
|
||||
let m_amount = 0;
|
||||
if (cashInAmount <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let runningAmount = 0;
|
||||
for (const detail of sales) {
|
||||
let temp = {
|
||||
runningAmount += parseInt(detail.i_total_price, 10);
|
||||
if (runningAmount > parseInt(detail.invoice_amount, 10)) {
|
||||
throw new Error(
|
||||
`ROLLBACK CASH-IN: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${runningAmount} > INVOICE AMOUNT (${detail.invoice_amount})`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
}));
|
||||
|
||||
let salesNo = detail.c_sales_no;
|
||||
let dataSales = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
};
|
||||
m_amount += parseInt(detail.i_total_price);
|
||||
await model.updateIntoSalesInternalBulk(trx, dataSales, salesNos);
|
||||
await model.insertCashInDetailBulk(trx, cashInDetails);
|
||||
|
||||
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
|
||||
)}`
|
||||
`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}`
|
||||
);
|
||||
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);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
return partnerRecon;
|
||||
@@ -730,8 +595,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,37 +602,7 @@ 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}`
|
||||
);
|
||||
|
||||
// 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,
|
||||
@@ -777,79 +610,69 @@ const _generateCashInTopUpCashlessInternal = async (uniqueCode) => {
|
||||
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);
|
||||
if (!topup || topup.length === 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
|
||||
)}`
|
||||
);
|
||||
const paymentyHistory = await model.writePaynentHistory(
|
||||
dataCahsIn, 'SUCCESSFULLY', 0, trx
|
||||
const cashInAmount = topup.reduce(
|
||||
(sum, row) => sum + parseInt(row.i_amount, 10),
|
||||
0
|
||||
);
|
||||
|
||||
let m_amount = 0;
|
||||
if (cashInAmount <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let runningAmount = 0;
|
||||
for (const detail of topup) {
|
||||
let temp = {
|
||||
runningAmount += parseInt(detail.i_amount, 10);
|
||||
if (runningAmount > parseInt(detail.invoice_amount, 10)) {
|
||||
throw new Error(
|
||||
`ROLLBACK CASH-IN: AMOUNT CASH-IN > TOTAL INVOICE. AMOUNT ${runningAmount} > INVOICE AMOUNT (${detail.invoice_amount})`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
}));
|
||||
|
||||
let topupNo = detail.c_topup_no;
|
||||
let dataTopup = {
|
||||
i_cash_in: cashIn.i_cash_in,
|
||||
};
|
||||
m_amount += parseInt(detail.i_amount);
|
||||
await model.updateIntoTopUpCashlessInternalBulk(trx, dataTopup, topupNos);
|
||||
await model.insertCashInDetailBulk(trx, cashInDetails);
|
||||
|
||||
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
|
||||
)}`
|
||||
`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}`
|
||||
);
|
||||
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);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
return partnerRecon;
|
||||
@@ -931,10 +754,6 @@ const _postingCashIn = async (uniqueCode) => {
|
||||
return postingEt;
|
||||
};
|
||||
|
||||
function wait(milliseconds) {
|
||||
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
validate,
|
||||
apiGenerateCahsIn,
|
||||
|
||||
@@ -281,14 +281,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 +323,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')
|
||||
@@ -359,9 +359,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)
|
||||
@@ -393,9 +393,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)
|
||||
@@ -488,7 +488,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'
|
||||
@@ -498,17 +498,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 () {
|
||||
@@ -551,19 +551,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')
|
||||
@@ -596,19 +596,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')
|
||||
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')
|
||||
@@ -640,19 +640,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')
|
||||
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')
|
||||
@@ -677,15 +677,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 {
|
||||
@@ -715,6 +715,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, ['*'])
|
||||
@@ -722,6 +732,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, ['*'])
|
||||
@@ -729,6 +749,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, ['*'])
|
||||
@@ -736,11 +766,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(
|
||||
@@ -783,8 +835,13 @@ module.exports = {
|
||||
getSalesInternal,
|
||||
getTopUpCashlessInternal,
|
||||
insertCashIn,
|
||||
insertCashInDetailBulk,
|
||||
updateIntoReserve,
|
||||
updateIntoReserveBulk,
|
||||
updateIntoSalesKMT,
|
||||
updateIntoSalesKMTBulk,
|
||||
updateIntoSalesInternal,
|
||||
updateIntoSalesInternalBulk,
|
||||
updateIntoTopUpCashlessInternal,
|
||||
updateIntoTopUpCashlessInternalBulk,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
# 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;
|
||||
```
|
||||
|
||||
## 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,200 @@
|
||||
-- DROP FUNCTION payment.sp_payment_req_topup_online(varchar, bool);
|
||||
|
||||
CREATE OR REPLACE FUNCTION payment.sp_payment_req_topup_online(character varying, boolean)
|
||||
RETURNS SETOF payment.invoice_rec
|
||||
LANGUAGE plpgsql
|
||||
AS $function$
|
||||
DECLARE
|
||||
p_load_balance ALIAS FOR $1;
|
||||
p_repost ALIAS FOR $2;
|
||||
v_trx_date DATE;
|
||||
result_record payment.invoice_rec;
|
||||
|
||||
BEGIN
|
||||
v_trx_date := p_load_balance::DATE;
|
||||
|
||||
-- OPTIMIZATION: Consolidated set-based processing with all CTEs to eliminate loop overhead
|
||||
-- Processes all eligible partners in a single set-based flow
|
||||
WITH eligible_partners AS (
|
||||
-- Get unique list of partners with billable open reservations
|
||||
SELECT DISTINCT tdrb.i_partner
|
||||
FROM trx.t_d_load_balance tdl
|
||||
INNER JOIN trx.t_d_reservation_balance tdrb
|
||||
ON tdrb.c_load_balance_no = tdl.c_load_balance_no
|
||||
INNER JOIN public.t_m_partner tmp
|
||||
ON tmp.i_partner = tdrb.i_partner
|
||||
WHERE tdl.d_load_balance < v_trx_date
|
||||
AND tdrb.c_invoice_no IS NULL
|
||||
AND tdl.c_invoice_no IS NULL
|
||||
AND tmp.b_invoice = TRUE
|
||||
AND tdrb.c_status = 'A'
|
||||
AND tdl.b_void = p_repost
|
||||
),
|
||||
partner_codes AS (
|
||||
-- Fetch partner details once to avoid repetitive t_m_partner queries
|
||||
SELECT
|
||||
tmp.i_partner,
|
||||
tmp.c_code,
|
||||
tmp.c_name,
|
||||
tmp.c_account_topup,
|
||||
tmp.c_account_code_topup,
|
||||
tmp.c_bank,
|
||||
tmp.c_bankaccount,
|
||||
tmp.c_bankaccountname
|
||||
FROM public.t_m_partner tmp
|
||||
WHERE tmp.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
),
|
||||
payment_codes AS (
|
||||
-- Generate payment codes with sequence numbers using window functions
|
||||
SELECT
|
||||
ep.i_partner,
|
||||
'INV' || pc.c_code || TO_CHAR(v_trx_date, 'YYMMDD') ||
|
||||
LPAD((COALESCE(MAX(CAST(RIGHT(tpr.c_payment, 4) AS INTEGER)), 0) OVER
|
||||
(PARTITION BY tpr.i_partner ORDER BY tpr.d_date_payment_request))::TEXT, 4, '0') AS code_payment
|
||||
FROM eligible_partners ep
|
||||
CROSS JOIN partner_codes pc
|
||||
LEFT JOIN payment.t_d_payment_request_topup_online tpr
|
||||
ON tpr.i_partner = ep.i_partner
|
||||
AND tpr.d_date_payment_request::DATE = v_trx_date
|
||||
WHERE pc.i_partner = ep.i_partner
|
||||
GROUP BY ep.i_partner, pc.c_code
|
||||
),
|
||||
topup_requests AS (
|
||||
-- Bulk insert payment requests and capture all load_balance_nos
|
||||
INSERT INTO payment.t_d_payment_request_topup_online
|
||||
(d_date_payment_request, i_partner, i_company, i_holding, c_load_balance_no, c_status, c_payment, m_amount, i_issuer_fee, i_channel_fee)
|
||||
SELECT
|
||||
v_trx_date,
|
||||
tdrb.i_partner,
|
||||
tmpc.i_company,
|
||||
1 AS i_holding,
|
||||
tdl.c_load_balance_no,
|
||||
tdl.c_status,
|
||||
pc.code_payment,
|
||||
SUM(tdrb.i_reservation_amount),
|
||||
SUM(tdrb.i_issuer_fee),
|
||||
SUM(tdrb.i_channel_fee)
|
||||
FROM trx.t_d_load_balance tdl
|
||||
INNER JOIN trx.t_d_reservation_balance tdrb
|
||||
ON tdrb.c_load_balance_no = tdl.c_load_balance_no
|
||||
INNER JOIN t_m_partner_company tmpc
|
||||
ON tmpc.i_partner = tdrb.i_partner
|
||||
INNER JOIN payment_codes pc
|
||||
ON pc.i_partner = tdrb.i_partner
|
||||
WHERE tdrb.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
AND tdrb.c_invoice_no IS NULL
|
||||
AND tdl.c_invoice_no IS NULL
|
||||
AND tdl.d_load_balance < v_trx_date
|
||||
AND tdl.b_void = p_repost
|
||||
AND tdrb.c_status = 'A'
|
||||
GROUP BY tdrb.i_partner, tmpc.i_company, tdl.c_load_balance_no, tdl.c_status, pc.code_payment, tdl.i_load_balance
|
||||
ORDER BY tdl.i_load_balance
|
||||
RETURNING c_load_balance_no, i_partner
|
||||
),
|
||||
invoices_created AS (
|
||||
-- Bulk insert invoices and mark payment requests as sent
|
||||
INSERT INTO payment.t_d_invoice
|
||||
(i_partner, c_invoice_no, c_partner_code, c_partner_name, c_status, c_status_name, i_company, i_holding, m_amount, m_issuer_fee, m_channel_fee, m_mdr_net, d_invoice_date, c_source)
|
||||
SELECT
|
||||
tdprt.i_partner,
|
||||
tdprt.c_payment,
|
||||
pc.c_code,
|
||||
pc.c_name,
|
||||
'B',
|
||||
'BILLED',
|
||||
tdprt.i_company,
|
||||
tdprt.i_holding,
|
||||
COALESCE(SUM(tdprt.m_amount), 0),
|
||||
COALESCE(SUM(tdprt.i_issuer_fee), 0),
|
||||
COALESCE(SUM(tdprt.i_channel_fee), 0),
|
||||
COALESCE(SUM(tdprt.m_amount), 0),
|
||||
v_trx_date,
|
||||
'ONLINE'
|
||||
FROM payment.t_d_payment_request_topup_online tdprt
|
||||
INNER JOIN partner_codes pc
|
||||
ON pc.i_partner = tdprt.i_partner
|
||||
WHERE tdprt.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
AND tdprt.d_date_payment_request::DATE = v_trx_date
|
||||
AND tdprt.b_sent = FALSE
|
||||
AND tdprt.b_void = FALSE
|
||||
AND tdprt.c_payment NOT LIKE 'MINV%'
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM topup_requests tr
|
||||
WHERE tr.c_load_balance_no = tdprt.c_load_balance_no
|
||||
)
|
||||
GROUP BY tdprt.i_partner, tdprt.c_payment, pc.c_code, pc.c_name, tdprt.i_company, tdprt.i_holding
|
||||
RETURNING c_invoice_no, i_partner
|
||||
),
|
||||
mark_requests_sent AS (
|
||||
-- Bulk update payment requests to mark as sent (in single operation, not per invoice)
|
||||
UPDATE payment.t_d_payment_request_topup_online tpr
|
||||
SET b_sent = TRUE
|
||||
WHERE tpr.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
AND tpr.d_date_payment_request::DATE = v_trx_date
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM invoices_created ic
|
||||
WHERE ic.i_partner = tpr.i_partner
|
||||
)
|
||||
),
|
||||
update_reservations AS (
|
||||
-- Bulk update reservations with invoice numbers (single operation, not per detail)
|
||||
UPDATE trx.t_d_reservation_balance tdrb
|
||||
SET c_invoice_no = tpr.c_payment
|
||||
FROM payment.t_d_payment_request_topup_online tpr
|
||||
WHERE tdrb.c_load_balance_no = tpr.c_load_balance_no
|
||||
AND tdrb.i_partner IN (SELECT i_partner FROM eligible_partners)
|
||||
AND tpr.d_date_payment_request::DATE = v_trx_date
|
||||
AND tpr.b_sent = TRUE
|
||||
),
|
||||
final_results AS (
|
||||
-- Build result set with all required fields
|
||||
SELECT
|
||||
tdi.i_partner,
|
||||
tdi.c_partner_name,
|
||||
tdi.c_invoice_no,
|
||||
tdi.m_amount AS amount,
|
||||
tdi.c_invoice_no AS c_file_name,
|
||||
'FB70' AS tcode,
|
||||
'A' AS reversal,
|
||||
'DR' AS doc_type,
|
||||
TO_CHAR(tdi.d_invoice_date, 'YYYYMMDD') AS posting_date,
|
||||
TO_CHAR(tdi.d_invoice_date, 'YYYYMMDD') AS doc_date,
|
||||
'C100' AS ba,
|
||||
pc.c_account_topup,
|
||||
pc.c_name,
|
||||
pc.c_account_code_topup,
|
||||
pc.c_bank,
|
||||
pc.c_bankaccount,
|
||||
pc.c_bankaccountname,
|
||||
CONCAT('IVSAP', tdi.c_invoice_no) AS doc_header_text,
|
||||
TO_CHAR(tdi.d_invoice_date, 'YYYY') AS fisc_year,
|
||||
TO_CHAR(tdi.d_invoice_date, 'MM') AS fisc_period
|
||||
FROM payment.t_d_invoice tdi
|
||||
INNER JOIN partner_codes pc
|
||||
ON pc.i_partner = tdi.i_partner
|
||||
WHERE tdi.d_invoice_date = v_trx_date
|
||||
AND tdi.c_source = 'ONLINE'
|
||||
)
|
||||
SELECT * FROM final_results;
|
||||
|
||||
RAISE NOTICE 'sp_payment_req_topup_online completed for date: %', v_trx_date;
|
||||
|
||||
END;
|
||||
$function$;
|
||||
|
||||
|
||||
-- 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.
|
||||
@@ -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