add new service generate cash in topup cashless internal

This commit is contained in:
2025-06-30 17:52:22 +07:00
parent 8e54ebb704
commit d7c62eda3f
3 changed files with 275 additions and 8 deletions
+184 -8
View File
@@ -208,6 +208,60 @@ const apiGenerateCashInSalesInternal = async (req, res) => {
}
};
const apiGenerateCashInTopUpCashlessInternal = async (req, res) => {
try {
// generate unique code
uniqueCode = helper.getUniqueCode();
// return uniqueCode
// log info
winston.logger.info(
`${uniqueCode} REQUEST generate cashin topup cashless internal: ${JSON.stringify(req.body)}`
);
// check validator
const err = validationResult(req, res);
if (!err.isEmpty()) {
result = {
code: '400',
message: err.errors[0].msg,
data: {},
};
// log warn
winston.logger.warn(
`${uniqueCode} RESPONSE generate cashin topup cashless internal: ${JSON.stringify(result)}`
);
return res.status(200).json(result);
}
const cashIn = await _generateCashInTopUpCashlessInternal(uniqueCode);
result = {
code: '200',
message: 'Success generate cash-in',
data: cashIn,
};
return res.status(200).json(result);
} catch (error) {
// create log
winston.logger.error(
`500 internal server error - backend server | ${error.message}`
);
return res.status(200).json({
code: '500',
message:
process.env.NODE_ENV != 'production'
? error.message
: '500 internal server error - backend server.',
data: {},
});
}
};
// RETRY POSTING CASH IN
const apiPostingCahsIn = async (req, res) => {
try {
@@ -431,7 +485,6 @@ const _generateCashInSales = async (uniqueCode) => {
);
for (const item of partnerRecon) {
let reserve = [];
let code = '';
await knex.transaction(async (trx) => {
try {
@@ -535,7 +588,6 @@ const _generateCashInSales = async (uniqueCode) => {
}
}
await wait(10000);
winston.logger.info();
}
await trx.commit();
@@ -560,7 +612,6 @@ const _generateCashInSalesInternal = async (uniqueCode) => {
);
for (const item of partnerRecon) {
let reserve = [];
let code = '';
await knex.transaction(async (trx) => {
try {
@@ -662,17 +713,140 @@ const _generateCashInSalesInternal = async (uniqueCode) => {
}
}
await wait(10000);
winston.logger.info();
}
await trx.commit();
} catch (error) {
sts = "error";
console.log("XXXXXXXXXXXXXXXXXXXXXXXXXX");
throw new Error(error);
await trx.rollback();
throw new Error(error);
}
});
}
return partnerRecon;
};
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
)}`
);
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,
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_total_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_total_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_total_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);
}
});
@@ -765,9 +939,11 @@ module.exports = {
apiGenerateCahsIn,
apiGenerateCashInSales,
apiGenerateCashInSalesInternal,
apiGenerateCashInTopUpCashlessInternal,
apiPostingCahsIn,
_postingCashIn,
_generateCashIn,
_generateCashInSales,
_generateCashInSalesInternal,
_generateCashInTopUpCashlessInternal,
};
@@ -370,6 +370,36 @@ const getSalesInternal = async (trx, code, date, invoice) => {
return res;
};
const getTopUpCashlessInternal = async (trx, code, date, invoice) => {
const res = await trx
.select([
'tdskm.c_topup_no',
'tdskm.i_amount',
'tdpr.c_payment_recon_no',
'tdpr.d_payment',
'tdpr.d_payment_recon',
'tmp.c_code as c_partner_code',
'tmp.c_name as c_partner_name',
'tdsi.c_invoice_no',
'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')
.where('tdsi.c_status', '!=', 'X')
.where('tmp.c_code', '=', code)
.where('tdpr.d_payment', '=', date)
.where('tdsi.c_invoice_no', '=', invoice)
.where('tdpr.c_status', '=', 'S')
.whereNotNull('tdsi.c_return_cash_in_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)`
);
return res;
};
const getPartnerRecon = async (trx) => {
// const res = await trx
// .select([
@@ -584,6 +614,50 @@ const getPartnerReconSalesInternal = async (trx) => {
return res;
};
const getPartnerReconTopUpCashlessInternal = async (trx) => {
const res = await trx
.select([
'tmp.c_code as c_partner_code',
'tmp.c_name as c_partner_name',
'tdsi.c_invoice_no',
'tdsi.d_invoice_date',
trx.raw("to_char(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment"),
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'
END c_in_period`),
])
.from('trx.t_d_topup_cashless_internal AS tdskm')
.leftJoin('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 () {
this.on('tdpr.c_topup_no', '=', 'tdskm.c_topup_no');
})
.leftJoin('public.t_m_partner as tmp', function () {
this.on('tmp.i_partner', '=', 'tdskm.i_partner');
})
.where('tdsi.c_status', '!=', 'X')
.where('tdpr.c_status', '=', 'S')
.whereNotNull('tdsi.c_return_cash_in_sap')
.whereNull('tdskm.i_cash_in')
.groupBy([
'tmp.c_code',
'tmp.c_name',
'tdsi.c_invoice_no',
'tdsi.d_invoice_date',
'tdpr.d_payment',
'tmp.c_account_topup',
'tmp.c_account_code_topup'
]);
return res;
};
const generateCode = async (trx, c_partner_code, d_payment_recon) => {
let code = '';
let prefix = `CIN${c_partner_code}${d_payment_recon}`;
@@ -641,6 +715,13 @@ const updateIntoSalesInternal = async (trx, dataSales, salesNo) => {
return result;
};
const updateIntoTopUpCashlessInternal = async (trx, dataTopup, topupNo) => {
let result = await trx('trx.t_d_topup_cashless_internal')
.update(dataTopup, ['*'])
.where({ c_topup_no: topupNo });
return result;
};
const insertCashIn = async (trx, data) => {
let result = await trx('payment.t_d_cash_in').insert(data, ['*']);
return result[0];
@@ -682,11 +763,14 @@ module.exports = {
getPartnerRecon,
getPartnerReconSales,
getPartnerReconSalesInternal,
getPartnerReconTopUpCashlessInternal,
getReserve,
getSalesKMT,
getSalesInternal,
getTopUpCashlessInternal,
insertCashIn,
updateIntoReserve,
updateIntoSalesKMT,
updateIntoSalesInternal,
updateIntoTopUpCashlessInternal,
};
+7
View File
@@ -24,6 +24,13 @@ router.get(
controller.apiGenerateCashInSalesInternal
);
router.get(
"/online/cash-in-topup-cashless-internal",
// auth.authenticateToken,
// controller.validate("generateInvoice"),
controller.apiGenerateCashInTopUpCashlessInternal
);
router.post(
"/online/cash-in/posting",
// auth.authenticateToken,