Files
Online-Topup-V1/application/modules/invoice/invoice.controller.js
T
Nuranggi Hermawan c37767be04 Update invooice
2022-07-17 22:00:15 +07:00

138 lines
4.5 KiB
JavaScript

const helper = require("../../helpers/helper");
const winston = require("../../helpers/winston.logger");
const model = require("./invoice.model");
const moment = require("moment");
const xml2js = require('xml2js');
const knex = require("../../../infrastructure/database/knex");
moment.locale("id");
var result = {};
var uniqueCode;
// POSTING INVOICE
const postingInvoice = async () => {
try {
// generate unique code
uniqueCode = helper.getUniqueCode();
// return uniqueCode
// time
let invoiceDate = moment('2020-01-25').format("YYYY-MM-DD")
let invoice = []
let xmlRequest = ''
await knex.transaction(async trx => {
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} TRANSACTION 1`);
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} execute payment.sp_payment_req_topup_online(${invoiceDate})`);
invoice = await model.generateInvoice(invoiceDate, trx)
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} result payment.sp_payment_req_topup_online ${JSON.stringify(invoice)}`);
if(invoice.length > 0) for (const key in invoice) {
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} insert posting et ${JSON.stringify(invoice[key])}`)
let postingEt = await model.insertPostingEt(invoice[key], trx)
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} success insert posting et : ${JSON.stringify(postingEt)}`)
}
// })
if(invoice.length > 0){
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} preparing posting FB70 to SAP`);
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'
for (const key in invoice) {
let xmlHeader = {
doc_header_text : invoice[key].doc_header_text ? invoice[key].doc_header_text : "",
ba: invoice[key].ba ? invoice[key].ba : "",
doc_date : invoice[key].doc_date ? invoice[key].doc_date : "",
posting_date : invoice[key].posting_date ? invoice[key].posting_date : "",
fisc_year : invoice[key].fisc_year ? invoice[key].fisc_year : "",
fisc_period : invoice[key].fisc_period ? invoice[key].fisc_period: "",
doc_type: invoice[key].doc_type ? invoice[key].doc_type : "",
c_invoice_no : invoice[key].c_invoice_no ? invoice[key].c_invoice_no : ""
}
let xmlItem = ['S', 'H'].map((item, index) => {
return {
c_account_code_topup : item == 'S' ? invoice[key].c_account_code_topup : "",
c_account_topup : item == 'H' ? invoice[key].c_account_topup : "",
ba : invoice[key].ba,
amount : invoice[key].amount,
posting_date : invoice[key].posting_date,
c_invoice_no : invoice[key].c_invoice_no,
item_text : `${item == 'S' ? "DEBIT" : "KREDIT"} INVOICE CMS ${invoice[key].doc_header_text}`,
de_cre : item
}
});
let xmlRequest = helper.generateXMLRequest('ZFMCMS_FI_001', xmlHeader, xmlItem)
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}`);
const response = await helper.postingSAP(url, xmlRequest)
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} RESPONSE posting ${invoice[key].c_invoice_no} 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(invoice[key], resSAP, trx)
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`)
// })
}
}
})
result = {
code: "200",
message: "Succes posting",
data: invoice,
};
return JSON.stringify(result)
} catch (error) {
// create log
winston.logger.error(
`500 internal server error - backend server | ${error.message}`
);
result = {
code: "500",
message:
process.env.NODE_ENV != "production"
? error.message
: "500 internal server error - backend server.",
data: {},
}
return JSON.stringify(result)
}
};
module.exports = {
postingInvoice,
};