mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 11:46:27 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fccb28c608 | |||
| f72153d463 | |||
| 899569221c |
@@ -100,6 +100,60 @@ const apiGenerateCahsIn = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const apiGenerateCashInSales = async (req, res) => {
|
||||||
|
try {
|
||||||
|
// generate unique code
|
||||||
|
uniqueCode = helper.getUniqueCode();
|
||||||
|
// return uniqueCode
|
||||||
|
|
||||||
|
// log info
|
||||||
|
winston.logger.info(
|
||||||
|
`${uniqueCode} REQUEST generate cashin: ${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: ${JSON.stringify(result)}`
|
||||||
|
);
|
||||||
|
|
||||||
|
return res.status(200).json(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cashIn = await _generateCashInSales(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 INVOICE
|
// RETRY POSTING INVOICE
|
||||||
const apiPostingCahsIn = async (req, res) => {
|
const apiPostingCahsIn = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -300,6 +354,135 @@ const _generateCashIn = async (uniqueCode) => {
|
|||||||
return partnerRecon;
|
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
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
|
||||||
|
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,
|
||||||
|
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.getSalesKMT(
|
||||||
|
trx,
|
||||||
|
item.c_partner_code,
|
||||||
|
item.d_payment,
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
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_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);
|
||||||
|
winston.logger.info();
|
||||||
|
}
|
||||||
|
await trx.commit();
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
sts = "error";
|
||||||
|
await trx.rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return partnerRecon;
|
||||||
|
};
|
||||||
|
|
||||||
const _postingCashIn = async (uniqueCode) => {
|
const _postingCashIn = async (uniqueCode) => {
|
||||||
let data = [];
|
let data = [];
|
||||||
let postingEt = await model.getPostingEt(knex);
|
let postingEt = await model.getPostingEt(knex);
|
||||||
@@ -383,7 +566,9 @@ function wait(milliseconds) {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
validate,
|
validate,
|
||||||
apiGenerateCahsIn,
|
apiGenerateCahsIn,
|
||||||
|
apiGenerateCashInSales,
|
||||||
apiPostingCahsIn,
|
apiPostingCahsIn,
|
||||||
_postingCashIn,
|
_postingCashIn,
|
||||||
_generateCashIn,
|
_generateCashIn,
|
||||||
|
_generateCashInSales
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -305,6 +305,39 @@ const getReserve = async (trx, code, date, invoice) => {
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const getSalesKMT = async (trx, code, date, invoice) => {
|
||||||
|
const res = await trx
|
||||||
|
.select([
|
||||||
|
'tdskm.c_sales_no',
|
||||||
|
'tdskm.i_total_billing',
|
||||||
|
'tdpr.c_payment_recon_no',
|
||||||
|
'tdpr.d_payment',
|
||||||
|
'tdpr.d_payment_recon',
|
||||||
|
'tdskm.c_partner_code',
|
||||||
|
'tdskm.c_partner_name',
|
||||||
|
'tdsi.c_invoice_no',
|
||||||
|
'tdsi.m_total_billing as invoice_amount'
|
||||||
|
])
|
||||||
|
.from('trx.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')
|
||||||
|
|
||||||
|
.where('tdskm.c_sales_status', '=', 'A')
|
||||||
|
.where('tdsi.c_status', '!=', 'X')
|
||||||
|
.where('tdskm.c_partner_code', '=', code)
|
||||||
|
.where('tdpr.d_payment', '=', date)
|
||||||
|
.where('tdsi.c_invoice_no', '=', 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)`
|
||||||
|
);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
const getPartnerRecon = async (trx) => {
|
const getPartnerRecon = async (trx) => {
|
||||||
// const res = await trx
|
// const res = await trx
|
||||||
// .select([
|
// .select([
|
||||||
@@ -430,6 +463,51 @@ const getPartnerRecon = async (trx) => {
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getPartnerReconSales = async (trx) => {
|
||||||
|
|
||||||
|
const res = await trx
|
||||||
|
.select([
|
||||||
|
'tdsi.c_partner_code',
|
||||||
|
'tdsi.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_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')
|
||||||
|
THEN 'IP' ELSE 'OP'
|
||||||
|
END c_in_period`),
|
||||||
|
])
|
||||||
|
.from('trx.t_d_sales_kmt_mobile AS tdskm')
|
||||||
|
|
||||||
|
.leftJoin('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 () {
|
||||||
|
this.on('tdpr.c_sales_no', '=', 'tdskm.c_sales_no');
|
||||||
|
})
|
||||||
|
.leftJoin('public.t_m_partner as tmp', function () {
|
||||||
|
this.on('tmp.i_partner', '=', 'tdskm.i_partner');
|
||||||
|
})
|
||||||
|
.where('tdskm.c_sales_status', '=', 'A')
|
||||||
|
.where('tdsi.c_status', '!=', 'X')
|
||||||
|
.where('tdpr.c_status', '=', 'S')
|
||||||
|
.whereNotNull('tdsi.c_return_sap')
|
||||||
|
.whereNull('tdskm.c_cash_in_no')
|
||||||
|
.groupBy([
|
||||||
|
'tdsi.c_partner_code',
|
||||||
|
'tdsi.c_partner_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) => {
|
const generateCode = async (trx, c_partner_code, d_payment_recon) => {
|
||||||
let code = '';
|
let code = '';
|
||||||
let prefix = `CIN${c_partner_code}${d_payment_recon}`;
|
let prefix = `CIN${c_partner_code}${d_payment_recon}`;
|
||||||
@@ -473,6 +551,13 @@ const updateIntoReserve = async (trx, dataReserve, reserveNo) => {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const updateIntoSalesKMT = async (trx, dataSales, salesNo) => {
|
||||||
|
let result = await trx('trx.t_d_sales_kmt_mobile')
|
||||||
|
.update(dataSales, ['*'])
|
||||||
|
.where({ c_sales_no: salesNo });
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
const insertCashIn = async (trx, data) => {
|
const insertCashIn = async (trx, data) => {
|
||||||
let result = await trx('payment.t_d_cash_in').insert(data, ['*']);
|
let result = await trx('payment.t_d_cash_in').insert(data, ['*']);
|
||||||
return result[0];
|
return result[0];
|
||||||
@@ -508,12 +593,14 @@ module.exports = {
|
|||||||
setReturnSAP,
|
setReturnSAP,
|
||||||
getPostingEt,
|
getPostingEt,
|
||||||
writePaynentHistory,
|
writePaynentHistory,
|
||||||
|
|
||||||
getRecon,
|
getRecon,
|
||||||
generateCode,
|
generateCode,
|
||||||
insertCashInDetail,
|
insertCashInDetail,
|
||||||
getReserve,
|
|
||||||
getPartnerRecon,
|
getPartnerRecon,
|
||||||
|
getPartnerReconSales,
|
||||||
|
getReserve,
|
||||||
|
getSalesKMT,
|
||||||
insertCashIn,
|
insertCashIn,
|
||||||
updateIntoReserve,
|
updateIntoReserve,
|
||||||
|
updateIntoSalesKMT
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,6 +10,13 @@ router.get(
|
|||||||
controller.apiGenerateCahsIn
|
controller.apiGenerateCahsIn
|
||||||
);
|
);
|
||||||
|
|
||||||
|
router.get(
|
||||||
|
"/online/cash-in-sales",
|
||||||
|
// auth.authenticateToken,
|
||||||
|
// controller.validate("generateInvoice"),
|
||||||
|
controller.apiGenerateCashInSales
|
||||||
|
);
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
"/online/cash-in/posting",
|
"/online/cash-in/posting",
|
||||||
// auth.authenticateToken,
|
// auth.authenticateToken,
|
||||||
|
|||||||
Reference in New Issue
Block a user