diff --git a/application/helpers/helper.js b/application/helpers/helper.js index 04378d5..6da5380 100644 --- a/application/helpers/helper.js +++ b/application/helpers/helper.js @@ -155,16 +155,11 @@ function generateXMLRequest(urn, headers, items) { } // POSTING SAP -async function postingSAP(url, body){ +async function postingSAP(url, body, credential){ try { const axios = require("axios"); - const credential = { - username: "basisxiqas", - password: "12345678" - } - const config = { headers : { 'Content-Type': 'text/xml', @@ -259,7 +254,11 @@ function getXMLData(response) { } else { // resSAP.data.ITEM = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'] resSAP.type = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'][0]['TYPE'] - resSAP.message = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'][0]['MESSAGE_1'] + + const indexMessage = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'].length - 1 + let message = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'][indexMessage]['MESSAGE_1'] + + resSAP.message = message } } diff --git a/application/modules/cashin/cahsin.controller.js b/application/modules/cashin/cahsin.controller.js index a1b8dd4..20d9163 100644 --- a/application/modules/cashin/cahsin.controller.js +++ b/application/modules/cashin/cahsin.controller.js @@ -37,7 +37,7 @@ const apiGenerateCahsIn = async (req, res) => { // log info winston.logger.info( - `${uniqueCode} REQUEST generate invoice: ${JSON.stringify(req.body)}` + `${uniqueCode} REQUEST generate cashin: ${JSON.stringify(req.body)}` ); // check validator @@ -51,7 +51,7 @@ const apiGenerateCahsIn = async (req, res) => { // log warn winston.logger.warn( - `${uniqueCode} RESPONSE generate invoice: ${JSON.stringify(result)}` + `${uniqueCode} RESPONSE generate cashin: ${JSON.stringify(result)}` ); return res.status(200).json(result); @@ -167,6 +167,8 @@ const _generateCashIn = async (uniqueCode) => { winston.logger.info(`SCHEDULER-FB70-${uniqueCode} cash in ${code} data : ${JSON.stringify(dataCahsIn)}`) const cashIn = await model.insertCashIn(trx, dataCahsIn) + const paymentyHistory = await model.writePaynentHistory(dataCahsIn, "SUCCESSFULLY", 0, trx) + // Insert cahs in detail let data = [] const reserv = await model.getReserve(trx, item.c_partner_code, item.d_payment) diff --git a/application/modules/cashin/cahsin.model.js b/application/modules/cashin/cahsin.model.js index 9d8f1bb..94e7ba9 100644 --- a/application/modules/cashin/cahsin.model.js +++ b/application/modules/cashin/cahsin.model.js @@ -154,28 +154,6 @@ const setReturnSAP = async (code, resSAP, trx) => { } -const writePaynentHistory = async (data, trx) => { - console.log(data) - const result = await trx('log.t_log_invoice').insert({ - i_log_name : 102, - d_log : trx.raw('NOW()'), - c_invoice_no : data.c_invoice_no, - c_partner_name : data.c_partner_name, - j_new_data : JSON.stringify(data), - d_payment_date: trx.raw('NOW()'), - c_note : 'SUCCESSFULLY', - c_status : 'S', - d_created : trx.raw('NOW()'), - m_amount : data.amount, - m_mdr_amount : 0, - m_mdr_net : data.amount - - }, ['i_log_name', 'c_note']) - - return result - -} - const getRecon = async (trx) => { let sql = `SELECT @@ -303,9 +281,11 @@ const getPartnerRecon = async(trx) => { .leftJoin('public.t_m_partner as tmp', function () { this.on('tmp.c_code', '=', 'tdrb.c_partner_code') }) + .where("tdrb.c_status", "=", "A") .where("tdi.c_status", "!=", "X") .where("tdpr.c_status", "=", "S") + // .where("tdi.c_return_sap", "LIKE", "S-%") .whereNotNull("tdi.c_return_sap") .whereNotNull("tdrb.c_load_balance_no") .whereRaw(`tdrb.c_reservation_no in (SELECT c_reservation_no FROM payment.t_d_payment_recon where c_status = 'S' ) AND tdrb.c_reservation_no NOT IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail WHERE b_reversal IS NOT TRUE)`) @@ -353,6 +333,29 @@ const insertCashIn = async (trx, data) => { return result[0] } +const writePaynentHistory = async (data, message, i_log_name, trx) => { + console.log(data) + const result = await trx('log.t_log_invoice').insert({ + // i_log_name : 102, + i_log_name : 119, + d_log : trx.raw('NOW()'), + c_invoice_no : data.c_cash_in_no, + c_partner_name : data.c_partner_name, + j_new_data : JSON.stringify(data), + d_payment_date: trx.raw('NOW()'), + c_note : message, + c_status : 'S', + d_created : trx.raw('NOW()'), + m_amount : data.amount, + m_mdr_amount : 0, + m_mdr_net : data.amount + + }, ['i_log_name', 'c_note']) + + return result + + } + module.exports = { generateInvoice, insertPostingEt, diff --git a/application/modules/invoiceonline/invoice.controller.js b/application/modules/invoiceonline/invoice.controller.js index 492a813..be47ac7 100644 --- a/application/modules/invoiceonline/invoice.controller.js +++ b/application/modules/invoiceonline/invoice.controller.js @@ -163,7 +163,7 @@ const apiPostingInvoice = async (req, res) => { const _generateInvoice = async (invoiceDate, uniqueCode, repost) => { let invoice = []; - + const collection = [] await knex.transaction(async (trx) => { winston.logger.info(`SCHEDULER-FB70-${uniqueCode} TRANSACTION 1`); @@ -181,19 +181,6 @@ const _generateInvoice = async (invoiceDate, uniqueCode, repost) => { if (invoice.length > 0) for (const key in invoice) { - winston.logger.info( - `SCHEDULER-FB70-${uniqueCode} insert payment history et : ${JSON.stringify( - invoice - )}` - ); - - let history = await model.writePaynentHistory(invoice[key], trx); - - winston.logger.info( - `SCHEDULER-FB70-${uniqueCode} result insert payment history et : ${JSON.stringify( - history - )}` - ); winston.logger.info( `SCHEDULER-FB70-${uniqueCode} insert posting et ${JSON.stringify( @@ -216,8 +203,40 @@ const _generateInvoice = async (invoiceDate, uniqueCode, repost) => { `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 credUsername = await model.getParameterSetting(knex, "SAP_AUTH_USERNAME") + const credPassword = await model.getParameterSetting(knex, "SAP_AUTH_PASSWORD") + const parameter = await model.getParameterSetting(knex, "SAP_FI_URL_FOR_FB70") + + let prepare = { + status : true, + message: "Success", + } + + if(!credUsername) { + prepare = { + status : false, + message: "FAILED GET SETTING FOR SAP_AUTH_USERNAME", + } + } + + if(!credPassword) { + prepare = { + status : false, + message: "FAILED GET SETTING FOR SAP_AUTH_PASSWORD", + } + } + + if(!parameter) { + prepare = { + status : false, + message: "FAILED GET SETTING FOR SAP_AUTH_PASSWORD", + } + } + + const credential = { + username: credUsername.c_value, + password: credPassword.c_value + } for (const key in invoice) { let xmlHeader = { @@ -263,98 +282,161 @@ const _generateInvoice = async (invoiceDate, uniqueCode, repost) => { `SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}` ); - const response = await helper.postingSAP(url, xmlRequest); + let response = {} + + if(prepare.status){ - winston.logger.debug( - `SCHEDULER-FB70-${uniqueCode} RESPONSE posting ${ - invoice[key].c_invoice_no - } to SAP : ${JSON.stringify(response)}` - ); + response = await helper.postingSAP(parameter.c_value, xmlRequest, credential); + + winston.logger.debug( + `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 + let resSAP + if(prepare.status){ + resSAP = helper.getXMLData(response); + }else { + resSAP = { + type: 'E', + message : prepare?.message || "FAILED GET PARAMETER SETTING", + data : {} + } + } + winston.logger.info( `SCHEDULER-FB70-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify( resSAP )}` ); - const cms = await model.setReturnSAP(invoice[key], resSAP, trx); + const cms = await model.setReturnSAP(invoice[key], resSAP, trx, true); winston.logger.info( `SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}` ); + + collection.push(cms.invoice[0]) + }); } } - return invoice; + return collection; }; const _postingInvoice = async (uniqueCode) => { let postingEt = []; + const collection = [] postingEt = await model.getPostingEt(knex); winston.logger.info( `SCHEDULER-RETRY-${uniqueCode} GET POSTING ET : ${JSON.stringify( postingEt )}` ); - + + const credUsername = await model.getParameterSetting(knex, "SAP_AUTH_USERNAME") + const credPassword = await model.getParameterSetting(knex, "SAP_AUTH_PASSWORD") + const parameter = await model.getParameterSetting(knex, "SAP_FI_URL_FOR_FB70") + + let prepare = { + status : true, + message: "Success", + } + + if(!credUsername) { + prepare = { + status : false, + message: "FAILED GET SETTING FOR SAP_AUTH_USERNAME", + } + } + + if(!credPassword) { + prepare = { + status : false, + message: "FAILED GET SETTING FOR SAP_AUTH_PASSWORD", + } + } + + if(!parameter) { + prepare = { + status : false, + message: "FAILED GET SETTING FOR SAP_AUTH_PASSWORD", + } + } + + const credential = { + username: credUsername.c_value, + password: credPassword.c_value + } + for (const item of postingEt) { + await knex.transaction(async (trx) => { - 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 response = {} + + let resSAP = {} + + if(prepare.status){ + + 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 + ); - let xmlRequest = helper.generateXMLRequest( - "ZFMCMS_FI_001", - xmlHeader, - xmlItem - ); + winston.logger.info( + `SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.reference} to SAP : ${xmlRequest}` + ); - winston.logger.info( - `SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.reference} to SAP : ${xmlRequest}` - ); + response = await helper.postingSAP(parameter.c_value, xmlRequest, credential); + + winston.logger.debug( + `SCHEDULER-FB70-${uniqueCode} RESPONSE posting ${ + item.reference + } to SAP : ${JSON.stringify(response)}` + ); - 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"; + resSAP = helper.getXMLData(response); - const response = await helper.postingSAP(url, xmlRequest); + }else{ + resSAP = { + type: 'E', + message : prepare?.message || "FAILED GET PARAMETER SETTING", + data : {} + } - 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 @@ -364,21 +446,25 @@ const _postingInvoice = async (uniqueCode) => { )}` ); + const invoice = await model.getInvoice(item.reference, trx) + const cms = await model.setReturnSAP( - { c_invoice_no: item.reference }, + invoice, resSAP, - trx + trx, + false ); winston.logger.info( `SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}` ); + + collection.push(cms.invoice[0]) + }) - } + } - // }); - - return postingEt; + return collection; }; module.exports = { diff --git a/application/modules/invoiceonline/invoice.model.js b/application/modules/invoiceonline/invoice.model.js index b2a145b..193cfd5 100644 --- a/application/modules/invoiceonline/invoice.model.js +++ b/application/modules/invoiceonline/invoice.model.js @@ -85,15 +85,15 @@ const getPostingEt = async(trx) => { } -const setReturnSAP = async (invoice, resSAP, trx) => { +const setReturnSAP = async (invoice, resSAP, trx, posting = false) => { let t_invoice = {} let t_load_balance = {} let posting_et = {} 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']) + c_return_sap: "S-"+resSAP.data.E_DOCUMENTNUMBER + }, ['c_invoice_no','c_return_sap','m_amount']) .where({ c_invoice_no: invoice.c_invoice_no }); @@ -113,6 +113,18 @@ const setReturnSAP = async (invoice, resSAP, trx) => { reference: invoice.c_invoice_no }) + winston.logger.info( + `SCHEDULER-FB70-${uniqueCode} insert payment history et : ${JSON.stringify( + invoice + )}` + ); + + if(posting) { + await writePaynentHistory(invoice, "SUCCESSFULLY", 102, trx) + } else { + await writePaynentHistory(invoice, "SUCCESS TO RETRY SAP", 118, trx) + } + }else { let text = resSAP.message; @@ -137,7 +149,7 @@ const setReturnSAP = async (invoice, resSAP, trx) => { // update invoice t_invoice = await trx('payment.t_d_invoice').update({ c_return_sap: doc_number - }, ['c_invoice_no','c_return_sap']) + }, ['c_invoice_no','c_return_sap','m_amount']) .where({ c_invoice_no: invoice.c_invoice_no }); @@ -147,7 +159,13 @@ const setReturnSAP = async (invoice, resSAP, trx) => { c_return_sap: doc_number }, ['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}')`) - + + if(posting) { + await writePaynentHistory(invoice, "SUCCESSFULLY", 102, trx) + } else { + await writePaynentHistory(invoice, "SUCCESS TO RETRY SAP", 118, trx) + } + }else{ // update posting et posting_et = await trx('public.posting_et').update({ @@ -158,6 +176,19 @@ const setReturnSAP = async (invoice, resSAP, trx) => { }, ['file_name','status0']).where({ reference: invoice.c_invoice_no }) + + // update invoice + t_invoice = await trx('payment.t_d_invoice').select(['c_invoice_no','c_return_sap','m_amount']) + .where({ + c_invoice_no: invoice.c_invoice_no + }); + + if(posting) { + await writePaynentHistory(invoice, `SUCCESS WITH SAP ERR: ${resSAP.message}`,102, trx) + } else { + await writePaynentHistory(invoice, `FAIL TO RETRY WITH SAP ERR: ${resSAP.message}`,118, trx) + } + } } @@ -172,16 +203,17 @@ const setReturnSAP = async (invoice, resSAP, trx) => { } -const writePaynentHistory = async (data, trx) => { +const writePaynentHistory = async (data, message, i_log_name, trx) => { console.log(data) const result = await trx('log.t_log_invoice').insert({ - i_log_name : 102, + // i_log_name : 102, + i_log_name : i_log_name, d_log : trx.raw('NOW()'), c_invoice_no : data.c_invoice_no, c_partner_name : data.c_partner_name, j_new_data : JSON.stringify(data), d_payment_date: trx.raw('NOW()'), - c_note : 'SUCCESSFULLY', + c_note : message, c_status : 'S', d_created : trx.raw('NOW()'), m_amount : data.amount, @@ -194,10 +226,37 @@ const writePaynentHistory = async (data, trx) => { } +const getParameterSetting = async(trx, code) => { + + const result = await trx('public.t_m_global_parameter_sap AS tmgps').select([ + "c_value" + ]) + .where('c_code', '=', code) + .where('c_status', '=', "A") + .first() + + return result + +} + +const getInvoice = async(code, trx) => { + + const result = await trx('payment.t_d_invoice').select([ + "*" + ]) + .where('c_invoice_no', '=', code) + .first() + + return result + +} + module.exports = { generateInvoice, insertPostingEt, setReturnSAP, getPostingEt, - writePaynentHistory + writePaynentHistory, + getParameterSetting, + getInvoice }; \ No newline at end of file diff --git a/knexfile.js b/knexfile.js index aa65c35..805a293 100644 --- a/knexfile.js +++ b/knexfile.js @@ -39,10 +39,10 @@ module.exports = { production: { client: "pg", connection: { - host : '10.1.201.95', + host : '10.1.201.75', database: "postgres", - user: "cms", - password: "Cm5kc1#2021", + user: "postgres", + password: "inhc0mmute_cms", }, // connection: process.env.DB_URL, pool: {