diff --git a/application/helpers/helper.js b/application/helpers/helper.js index 6da5380..7c51dd7 100644 --- a/application/helpers/helper.js +++ b/application/helpers/helper.js @@ -250,13 +250,13 @@ function getXMLData(response) { if (!Array.isArray(soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'])) { // resSAP.data.ITEM.push(soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item']) resSAP.type = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item']['TYPE'] - resSAP.message = soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item']['MESSAGE_1'] + resSAP.message = "[SAP] ".soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item']['MESSAGE_1'] } 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'] 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'] + let message = "[SAP] ".soapBody['ZFMCMS_FI_001.Response']['C_T_RETURN']['item'][indexMessage]['MESSAGE_1'] resSAP.message = message } diff --git a/application/modules/invoiceonline/invoice.controller.js b/application/modules/invoiceonline/invoice.controller.js index be47ac7..8d43b54 100644 --- a/application/modules/invoiceonline/invoice.controller.js +++ b/application/modules/invoiceonline/invoice.controller.js @@ -178,154 +178,173 @@ const _generateInvoice = async (invoiceDate, uniqueCode, repost) => { invoice )}` ); + if (invoice.length > 0) { - 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 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 = { - 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) => { - 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} preparing posting FB70 to SAP` ); + + 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") + + for (const key in invoice) { + let prepare = { + status : true, + cms : true, + message: "Success", + } + + if(!credUsername) { + prepare = { + status : false, + cms : false, + message: "[CMS] FAILED GET SETTING FOR SAP_AUTH_USERNAME", + } + } + + if(!credPassword) { + prepare = { + status : false, + cms : false, + message: "[CMS] FAILED GET SETTING FOR SAP_AUTH_PASSWORD", + } + } + + if(!parameter) { + prepare = { + status : false, + cms : false, + message: "[CMS] FAILED GET SETTING FOR SAP_AUTH_PASSWORD", + } + } + + const credential = { + username: credUsername.c_value, + password: credPassword.c_value + } - winston.logger.debug( - `SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}` - ); - - let response = {} - - if(prepare.status){ - - 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) => { - - let resSAP - if(prepare.status){ - resSAP = helper.getXMLData(response); - }else { - resSAP = { - type: 'E', - message : prepare?.message || "FAILED GET PARAMETER SETTING", - data : {} + if(invoice[key].c_account_code_topup == null || invoice[key].c_account_topup == null) { + prepare = { + status : false, + cms : false, + message: "[CMS] FAILED GET PARTNER ACCOUNT CODE TOPUP / ACCOUNT TOPUP", } } - winston.logger.info( - `SCHEDULER-FB70-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify( - resSAP - )}` + if(invoice[key].c_account_code_topup == "" || invoice[key].c_account_topup == "") { + prepare = { + status : false, + cms : false, + message: "[CMS] FAILED GET PARTNER ACCOUNT CODE TOPUP / ACCOUNT TOPUP", + } + } + + 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) => { + 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 ); - - const cms = await model.setReturnSAP(invoice[key], resSAP, trx, true); - + winston.logger.info( - `SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}` + `SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}` ); + + let response = {} - collection.push(cms.invoice[0]) + if(prepare.status){ + + 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) => { + + 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, true); + + winston.logger.info( + `SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}` + ); + + collection.push(cms.invoice[0]) + + // }); + } - }); } - } + }); return collection; }; diff --git a/server/index.js b/server/index.js index 2043d61..0537970 100644 --- a/server/index.js +++ b/server/index.js @@ -82,6 +82,12 @@ if (process.env.NODE_ENV.toUpperCase() == "PRODUCTION") { // generate // )}` // ); + // const reGenerate = await _generateInvoice(ivoiceDate, uniqueCode, true); + // winston.logger.debug( + // `END SCHEDULER-FB70 RE-GENERATE INVOICE THAT HAVE BEEN DELETED ${ivoiceDate} : ${JSON.stringify( + // generate + // )}` + // ); // } catch (error) { // // create log // winston.logger.error(