This commit is contained in:
2022-07-18 13:50:34 +07:00
6 changed files with 491 additions and 115 deletions
@@ -11,138 +11,126 @@ var result = {};
var uniqueCode;
// POSTING INVOICE
const postingInvoice = async (req, res) => {
const postingInvoice = async () => {
try {
// generate unique code
uniqueCode = await helper.getUniqueCode();
uniqueCode = helper.getUniqueCode();
// return uniqueCode
// time
let invoiceDate = moment('2022-06-25').subtract(1, "days").format("YYYY-MM-DD");
let invoiceDate = moment('2020-01-25').format("YYYY-MM-DD")
let invoice = [];
let invoice = []
let xmlRequest = ''
await knex.transaction(async trx => {
winston.logger.info(`${uniqueCode} execute payment.sp_payment_req_topup_online`);
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(`${uniqueCode} result payment.sp_payment_req_topup_online ${JSON.stringify(invoice)}`);
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} result payment.sp_payment_req_topup_online ${JSON.stringify(invoice)}`);
if(invoice){
for (const key in invoice) {
if(invoice.length > 0) for (const key in invoice) {
winston.logger.info(`${uniqueCode} insert posting et ${JSON.stringify(invoice[key])}`)
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} insert posting et ${JSON.stringify(invoice[key])}`)
let postingEt = await model.insertPostingEt(invoice[key], trx)
winston.logger.info(`${uniqueCode} success insert posting : ${postingEt}`)
}
winston.logger.info(`SCHEDULER-FB70-${uniqueCode} success insert posting et : ${JSON.stringify(postingEt)}`)
}
// })
if(invoice){
winston.logger.info(`${uniqueCode} preparing posting FB70 to SAP`);
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'
const credential = {
username: "basisxiqas",
password: "12345678"
}
for (const key in invoice) {
winston.logger.info(`${uniqueCode} REQUEST posting ${invoice[key].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();
const soap = `<SOAP:Envelope
xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'>
<SOAP:Header/>
<SOAP:Body>
<ns0:ZFMCMS_FI_001.Response
xmlns:ns0='urn:sap-com:document:sap:rfc:functions'>
<E_MATDOCUMENTYEAR>2021</E_MATDOCUMENTYEAR>
<E_MATERIALDOCUMENT>4905741245</E_MATERIALDOCUMENT>
<C_T_RETURN/>
</ns0:ZFMCMS_FI_001.Response>
</SOAP:Body>
</SOAP:Envelope>`
winston.logger.info(`${uniqueCode} RESPONSE posting ${invoice[key].c_invoice_no} to SAP : ${soap}`);
let resSAP = {
E_MATDOCUMENTYEAR: '',
E_MATERIALDOCUMENT: '',
ITEM: [],
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 : ""
}
xml2js.parseString(soap, {
explicitArray: false,
tagNameProcessors: [xml2js.processors.stripPrefix]
}, (err, result) => {
if (err) {
winston.logger.error(`${uniqueCode} ERROR parsing XML : ${err}`);
return;
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 soapBody = result.Envelope.Body;
if (soapBody.$) delete soapBody.$
resSAP.E_MATDOCUMENTYEAR = soapBody['ZFMCMS_FI_001.Response']['E_MATDOCUMENTYEAR']
resSAP.E_MATERIALDOCUMENT = soapBody['ZFMCMS_FI_001.Response']['E_MATERIALDOCUMENT']
if (!Array.isArray(soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'])) {
resSAP.ITEM.push(soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'])
} else {
resSAP.ITEM = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item']
}
});
// UPDATE CMS
winston.logger.info(`${uniqueCode} UPDATE resposn FB70 to CMS ${JSON.stringify(resSAP)}`);
const cms = await model.setReturnSAP(invoice[key], resSAP, trx)
winston.logger.info(`${uniqueCode} SUKSES UPDATE resposn FB70 to CMS ${JSON.stringify(resSAP)}`);
};
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 res.status(200).json(result);
return JSON.stringify(result)
} catch (error) {
// create log
winston.logger.error(
`500 internal server error - backend server | ${error.message}`
);
return res.status(200).json({
result = {
code: "500",
message:
process.env.NODE_ENV != "production"
? error.message
: "500 internal server error - backend server.",
data: {},
});
}
return JSON.stringify(result)
}
};
module.exports = {
+66 -21
View File
@@ -1,13 +1,13 @@
// GET PARTNER, MERCHANT, TERMINAL FROM TID
const generateInvoice = (invoiceDate, trx) => {
const generateInvoice = async (invoiceDate, trx) => {
let result = trx.select(trx.raw(`* FROM payment.sp_payment_req_topup_online('${invoiceDate}')`))
let result = await trx.select(trx.raw(`* FROM payment.sp_payment_req_topup_online('${invoiceDate}')`))
return result;
}
const insertPostingEt = (invoice, trx) => {
const insertPostingEt = async (invoice, trx) => {
console.log("invoice : ", invoice)
let type = ['S', 'H']
let et = []
@@ -27,19 +27,22 @@ const insertPostingEt = (invoice, trx) => {
ba: invoice.ba,
indeks: (index+1),
totalidx: type.length,
doc_header_text: `POSTING INVOICE CMS ${invoice.c_invoice_no} - ${invoice.doc_header_text}`,
doc_header_text: `POSTING INVOICE CMS ${invoice.c_invoice_no}`,
pos_status : null
}
if(item == 'H'){
data = {...data, ...{
text: `DEBIT INVOICE CMS ${invoice.c_invoice_no} - ${invoice.doc_header_text}`,
text: `DEBIT INVOICE CMS ${invoice.doc_header_text}`,
debit_kredit: item,
account: invoice.c_account_topup,
customer: null
}}
}else{
data = {...data, ...{
text: `KREDIT INVOICE CMS ${invoice.c_invoice_no} - ${invoice.doc_header_text}`,
text: `KREDIT INVOICE CMS ${invoice.doc_header_text}`,
debit_kredit: item,
account: null,
customer: invoice.c_account_code_topup
}}
}
@@ -48,26 +51,68 @@ const insertPostingEt = (invoice, trx) => {
});
posting_et = trx('public.posting_et').insert(et, ['no_dok'])
posting_et = await trx('public.posting_et').insert(et, ['indeks', 'customer', 'account', 'ba', 'fiscal_year', 'amount', 'no_dok', 'text', 'debit_kredit'])
return posting_et
return posting_et[0]
}
const setReturnSAP = (invoice, resSAP, trx) => {
// update invoice
trx('trx.t_d_load_balance').update({
c_return_sap: resSAP.E_MATERIALDOCUMENT
}, ['c_load_balance'])
.whereRaw(`c_load_balance_no IN (SELECT c_load_balance_no FROM payment.t_d_payment_request_topup_online WHERE c_payment = ${invoice.c_invoice_no})`)
// update load balance
trx('trx.t_d_load_balance').update({
c_return_sap: resSAP.E_MATERIALDOCUMENT
})
.whereRaw(`c_load_balance_no IN (SELECT c_load_balance_no FROM payment.t_d_payment_request_topup_online WHERE c_payment = ${invoice.c_invoice_no})`)
// const getPostingEt = async(invoiceDate, trx) => {
// const result = await trx('public.posting_et').select([
// trx.raw(`LPAD(indeks::text, 10, '0') `),
// 'file_name',
// 't_code',
// 'doc_type',
// 'posting_date',
// 'doc_date',
// 'debit_kredit',
// 'account',
// 'amount',
// 'ba',
// 'customer',
// 'fiscal_year',
// 'text',
// 'doc_header_text'
// ])
// }
const setReturnSAP = async (invoice, resSAP, trx) => {
let t_invoice = {}
let t_load_balance = {}
if(resSAP.type == 'S'){
// update invoice
t_invoice = await trx('payment.t_d_invoice').update({
c_return_sap: resSAP.data.E_DOCUMENTNUMBER
}, ['c_invoice_no','c_return_sap'])
.where({
c_invoice_no: invoice.c_invoice_no
});
// update load balance
t_load_balance = await trx('trx.t_d_load_balance').update({
c_return_sap: resSAP.data.E_DOCUMENTNUMBER
}, ['c_load_balance_no', 'c_return_sap'])
.whereRaw(`c_load_balance_no IN (SELECT c_load_balance_no FROM payment.t_d_payment_request_topup_online WHERE c_payment = '${invoice.c_invoice_no}')`)
}
// update posting et
const posting_et = await trx('public.posting_et').update({
pos_status : resSAP.type == 'S' ? 0 : 3,
status0 : resSAP.type,
no_doksap : resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null,
status1 : resSAP.message ? resSAP.message : null
}, ['file_name','status0']).where({
file_name: invoice.c_invoice_no
})
return {
invoice : t_invoice,
load_balance : t_load_balance,
posting_et : posting_et
}
}
module.exports = {