Files
Online-Topup-V1/application/modules/cashin/cahsin.controller.js
T
2022-09-14 13:32:42 +07:00

347 lines
11 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 apiGenerateInvoice = 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)
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'
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"))
// Insert cahs in
const dataCahsIn = {
c_partner_code : item.c_partner_code,
c_partner_name : item.c_partner_name,
c_cash_in_no : code,
d_cash_in : item.d_payment,
c_invoice_no : item.c_invoice_no,
i_amount : item.amount
}
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
}
const insert = await model.insertCashInDetail(trx, data)
}
const insertPostingEt = await model.insertPostingEt(code, item, trx)
})
let xmlHeader = {
doc_header_text : code,
ba: item.ba,
doc_date : item.doc_date,
posting_date : item.posting_date,
fisc_year : item.fisc_year,
fisc_period : item.fisc_period,
doc_type: item.doc_type,
c_invoice_no : code
}
let xmlItem = ['S', 'H'].map(items => {
return {
c_account_code_topup : items == 'S' ? item.c_account_code_topup : "",
c_account_topup : items == 'H' ? item.c_account_topup : "",
ba : item.ba,
amount : item.amount,
posting_date : item.posting_date,
c_invoice_no : code,
item_text : `${items == 'S' ? "DEBIT" : "KREDIT"} CAHSIN CMS ${code}`,
de_cre : items
}
});
let xmlRequest = helper.generateXMLRequest('ZFMCMS_FI_001', xmlHeader, xmlItem)
winston.logger.debug(`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${code} to SAP : ${xmlRequest}`);
const response = await helper.postingSAP(url, xmlRequest)
winston.logger.debug(`SCHEDULER-FB70-${uniqueCode} RESPONSE posting ${code} to SAP : ${JSON.stringify(response)}`);
// new transaction
await knex.transaction(async trx => {
const resSAP = helper.getXMLData(response)
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} TRANSACTION 2`);
// UPDATE CMS
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify(resSAP)}`)
const cms = await model.setReturnSAP(code, reserve, resSAP, trx)
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`)
})
}
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 apiPostingInvoice = async (req, res) => {
try {
// generate unique code
uniqueCode = helper.getUniqueCode()
// log info
winston.logger.info(
`${uniqueCode} REQUEST posting 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);
}
// return uniqueCode
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RE-TRY POSTING INVOICE`)
const invoice = await _postingInvoice(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,
d_cash_in : 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 (uniqueCode) => {
let postingEt = []
await knex.transaction(async trx => {
postingEt = await model.getPostingEt(trx)
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} GET POSTING ET : ${JSON.stringify(postingEt)}`)
for (const item of postingEt) {
let xmlHeader = {
doc_header_text : item.doc_header_text ? item.doc_header_text : "",
ba: item.ba ? item.ba : "",
doc_date : item.doc_date ? item.doc_date : "",
posting_date : item.posting_date ? item.posting_date : "",
fisc_year : item.fiscal_year ? item.fiscal_year : "",
fisc_period : item.fis_period ? item.fis_period: "",
doc_type: item.doc_type ? item.doc_type : "",
c_invoice_no : item.reference ? item.reference : ""
}
let xmlItem = ['S', 'H'].map(key => {
return {
c_account_code_topup : key == 'S' ? item.customer : "",
c_account_topup : key == 'H' ? item.account : "",
ba : item.ba,
amount : item.amount,
posting_date : item.posting_date,
c_invoice_no : item.reference,
item_text :key == 'S' ? item.text1 : item.text2,
de_cre : key
}
});
let xmlRequest = helper.generateXMLRequest('ZFMCMS_FI_001', xmlHeader, xmlItem)
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.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 ${item.reference} to SAP : ${JSON.stringify(response)}`);
// new transaction
// await knex.transaction(async trx => {
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(item.reference, resSAP, trx)
winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`)
// })
}
})
return postingEt
}
module.exports = {
validate,
apiPostingInvoice,
apiGenerateInvoice,
_postingCashIn,
_generateCashIn
};