Files
Online-Topup-V1/application/modules/cashin/cahsin.controller.js
T
Nuranggi Hermawan 52716f44a9 Update
2022-09-15 20:06:52 +07:00

256 lines
7.6 KiB
JavaScript

const { param, body, validationResult } = require("express-validator");
const helper = require("../../helpers/helper");
const winston = require("../../helpers/winston.logger");
const model = require("./cahsin.model");
const moment = require("moment");
const knex = require("../../../infrastructure/database/knex");
moment.locale("id");
var result = {};
var uniqueCode;
// VALIDATION
const validate = (method) => {
switch (method) {
case "generateInvoice":
return [
body('trx_date').notEmpty().withMessage('trx_date is required!').isISO8601().withMessage('invalid trx_date format YYYY-MM-DD !').escape().trim(),
body('repost').notEmpty().withMessage('repost is required!').isBoolean().withMessage('repost must boolean!').escape().trim(),
];
case "postingInvoice":
return [
body('trx_date').notEmpty().withMessage('trx_date is required!').isISO8601().withMessage('invalid trx_date format YYYY-MM-DD !').escape().trim(),
];
default:
break;
}
};
// POSTING INVOICE
const apiGenerateCahsIn = async (req, res) => {
try {
// generate unique code
uniqueCode = helper.getUniqueCode();
// return uniqueCode
// log info
winston.logger.info(
`${uniqueCode} REQUEST generate invoice: ${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 invoice: ${JSON.stringify(result)}`
);
return res.status(200).json(result);
}
const cashIn = await _generateCashIn(uniqueCode)
result = {
code: "200",
message: "Succes posting",
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
const apiPostingCahsIn = async (req, res) => {
try {
// generate unique code
uniqueCode = helper.getUniqueCode()
let { body } = req
// log info
winston.logger.info(
`${uniqueCode} REQUEST posting cash in : ${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 posting cash in : ${JSON.stringify(result)}`
);
return res.status(200).json(result);
}
// return uniqueCode
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RE-TRY POSTING CAHS IN`)
const invoice = await _postingCashIn(body.c_cash_in_no, uniqueCode)
result = {
code: "200",
message: "Succes",
data: invoice,
}
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: {},
});
}
}
const _generateCashIn = async (uniqueCode) => {
let partnerRecon = await model.getPartnerRecon(knex)
for (const item of partnerRecon) {
let reserve = []
let code = ""
await knex.transaction(async trx => {
code = await model.generateCode(trx, item.c_partner_code, moment().format("YYMMDD"))
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} generate cash in : ${code}`)
// Insert cahs in
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 : item.amount
}
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} cash in ${code} data : ${JSON.stringify(dataCahsIn)}`)
const cashIn = await model.insertCashIn(trx, dataCahsIn)
// Insert cahs in detail
const reserv = await model.getReserve(trx, item.c_partner_code, item.d_payment)
for (const detail of reserv) {
reserve.push(detail.c_reservation_no)
let data = {
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.insertCashInDetail(trx, data)
}
// await model.insertPostingEt(code, item, trx)
})
}
return partnerRecon
}
const _postingCashIn = async (reference, uniqueCode) => {
let postingEt = []
await knex.transaction(async trx => {
let postingEt = await model.getPostingEt(trx, reference)
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} GET POSTING ET : ${JSON.stringify(postingEt)}`)
let xmlHeader = {
doc_header_text : postingEt.doc_header_text ? postingEt.doc_header_text : "",
ba: postingEt.ba ? postingEt.ba : "",
doc_date : postingEt.doc_date ? postingEt.doc_date : "",
posting_date : postingEt.posting_date ? postingEt.posting_date : "",
fisc_year : postingEt.fiscal_year ? postingEt.fiscal_year : "",
fisc_period : postingEt.fis_period ? postingEt.fis_period: "",
doc_type: postingEt.doc_type ? postingEt.doc_type : "",
c_invoice_no : postingEt.reference ? postingEt.reference : ""
}
let xmlItem = ['S', 'H'].map(key => {
return {
c_account_code_topup : key == 'S' ? postingEt.customer : "",
c_account_topup : key == 'H' ? postingEt.account : "",
ba : postingEt.ba,
amount : postingEt.amount,
posting_date : postingEt.posting_date,
c_invoice_no : postingEt.reference,
item_text :key == 'S' ? postingEt.text1 : postingEt.text2,
de_cre : key
}
});
let xmlRequest = helper.generateXMLRequest('ZFMCMS_FI_001', xmlHeader, xmlItem)
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${postingEt.reference} to SAP : ${xmlRequest}`);
const url = 'http://piqas.kereta-api.co.id:51500/XISOAPAdapter/MessageServlet?senderParty=&senderService=KCI_ETICKETING_2&receiverParty=&receiverService=&interface=SI_KCI_CMS_FI01&interfaceNamespace=urn:Posting_ETicketing_2'
const response = await helper.postingSAP(url, xmlRequest)
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RESPONSE posting ${postingEt.reference} to SAP : ${JSON.stringify(response)}`);
const resSAP = helper.getXMLData(response)
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} TRANSACTION 2`);
// UPDATE CMS
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify(resSAP)}`)
const cms = await model.setReturnSAP(postingEt.reference, resSAP, trx)
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`)
})
return postingEt
}
module.exports = {
validate,
apiGenerateCahsIn,
apiPostingCahsIn,
_postingCashIn,
_generateCashIn
};