mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 19:56:27 +00:00
Update invoice
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
const { param, body, validationResult } = require("express-validator");
|
||||
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");
|
||||
@@ -11,105 +10,125 @@ moment.locale("id");
|
||||
var result = {};
|
||||
var uniqueCode;
|
||||
|
||||
// VALIDATION
|
||||
const validate = (method) => {
|
||||
switch (method) {
|
||||
case "invoiceVerification":
|
||||
return [
|
||||
body("date")
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("Date is required")
|
||||
.isDate({ format: "YYYY-MM-DD" })
|
||||
.withMessage("Invalid format date. Format is YYYY-MM-DD"),
|
||||
body("log")
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("Log is required")
|
||||
.isArray()
|
||||
.withMessage("Log is invalid request"),
|
||||
];
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
// POSTING INVOICE
|
||||
const postingInvoice = async (req, res) => {
|
||||
try {
|
||||
// generate unique code
|
||||
uniqueCode = await helper.getUniqueCode();
|
||||
|
||||
// param
|
||||
let { log } = req.body;
|
||||
let tid = req.tid_token;
|
||||
// time
|
||||
let invoiceDate = moment('2022-06-25').subtract(1, "days").format("YYYY-MM-DD");
|
||||
|
||||
// log info
|
||||
winston.logger.info(
|
||||
`${uniqueCode} REQUEST posting invoice : ${JSON.stringify(req.body)}`
|
||||
`${uniqueCode} Generating invoice ${invoiceDate}`
|
||||
);
|
||||
|
||||
// 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 posting invoice : ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
// time
|
||||
let invoiceDate = moment().subtract(1, "days").format("YYYYMMDD");
|
||||
|
||||
// log debug
|
||||
winston.logger.debug(`${uniqueCode} checking partner...`);
|
||||
|
||||
await db.transaction(async trx => {
|
||||
winston.logger.info(`${uniqueCode} Start Transaction`);
|
||||
|
||||
let invoice = [];
|
||||
|
||||
await knex.transaction(async trx => {
|
||||
|
||||
winston.logger.info(`${uniqueCode} execute payment.sp_payment_req_topup_online`);
|
||||
|
||||
invoice = await model.generateInvoice(invoiceDate, trx)
|
||||
|
||||
winston.logger.info(`${uniqueCode} result payment.sp_payment_req_topup_online ${JSON.stringify(invoice)}`);
|
||||
|
||||
if(invoice){
|
||||
for (const key in invoice) {
|
||||
|
||||
winston.logger.info(`${uniqueCode} insert posting et ${JSON.stringify(invoice[key])}`)
|
||||
|
||||
let postingEt = await model.insertPostingEt(invoice[key], trx)
|
||||
|
||||
winston.logger.info(`${uniqueCode} success insert posting : ${postingEt}`)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let invoice = await model.getInvoice(invoiceDate, trx);
|
||||
})
|
||||
|
||||
|
||||
// get partner, merchant, terminal from tid
|
||||
let getPartner = await model.getPartner(mid, tid);
|
||||
if (!getPartner) {
|
||||
result = {
|
||||
code: "412",
|
||||
message: "terminal id is not valid",
|
||||
data: {},
|
||||
};
|
||||
// insert into posting_et
|
||||
|
||||
// log warn
|
||||
winston.logger.warn(
|
||||
`${uniqueCode} RESPONSE payment verification: ${JSON.stringify(result)}`
|
||||
);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
let data = [];
|
||||
|
||||
// log info
|
||||
winston.logger.info(
|
||||
`${uniqueCode} RESPONSE payment verification: ${JSON.stringify(result)}`
|
||||
);
|
||||
result = {
|
||||
code: "200",
|
||||
message: "Succes posting",
|
||||
data: invoice,
|
||||
};
|
||||
|
||||
return res.status(200).json(result);
|
||||
|
||||
if (invoice[0].c_invoice_no) {
|
||||
|
||||
winston.logger.info(`${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'
|
||||
|
||||
const credential = {
|
||||
username: "basisxiqas",
|
||||
password: "12345678"
|
||||
}
|
||||
|
||||
invoice.forEach( async function (item) {
|
||||
|
||||
winston.logger.info(`${uniqueCode} REQUEST posting ${item.c_invoice_no} to SAP`);
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'text/xml',
|
||||
'Authorization': 'Basic' + base64.encode(credential.username + ":" + credential.password)
|
||||
},
|
||||
body: xmlRequest
|
||||
});
|
||||
|
||||
const soap = await response.text();
|
||||
|
||||
winston.logger.info(`${uniqueCode} RESPONSE posting ${item.c_invoice_no} to SAP : ${soap}`);
|
||||
|
||||
let resSAP = {
|
||||
E_MATDOCUMENTYEAR: '',
|
||||
E_MATERIALDOCUMENT: '',
|
||||
ITEM: [],
|
||||
}
|
||||
|
||||
xml2js.parseString(soap, {
|
||||
explicitArray: false,
|
||||
tagNameProcessors: [xml2js.processors.stripPrefix]
|
||||
}, (err, result) => {
|
||||
if (err) {
|
||||
winston.logger.error(`${uniqueCode} ERROR parsing XML : ${err}`);
|
||||
return;
|
||||
}
|
||||
|
||||
let soapBody = result.Envelope.Body;
|
||||
|
||||
if (soapBody.$) delete soapBody.$
|
||||
|
||||
resSAP.E_MATDOCUMENTYEAR = soapBody['ZKCIMM_CMS02.Response']['E_MATDOCUMENTYEAR']
|
||||
resSAP.E_MATERIALDOCUMENT = soapBody['ZKCIMM_CMS02.Response']['E_MATERIALDOCUMENT']
|
||||
|
||||
if (!Array.isArray(soapBody['ZKCIMM_CMS02.Response']['C_T_RETURN']['item'])) {
|
||||
resSAP.ITEM.push(soapBody['ZKCIMM_CMS02.Response']['C_T_RETURN']['item'])
|
||||
} else {
|
||||
resSAP.ITEM = soapBody['ZKCIMM_CMS02.Response']['C_T_RETURN']['item']
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// UPDATE CMS
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
// create log
|
||||
winston.logger.error(
|
||||
`500 internal server error - backend server | ${error.message}`
|
||||
);
|
||||
|
||||
return res.status(200).json({
|
||||
code: "500",
|
||||
message:
|
||||
@@ -122,6 +141,5 @@ const postingInvoice = async (req, res) => {
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
validate,
|
||||
postingInvoice,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user