diff --git a/application/modules/invoiceonline/invoice.controller.js b/application/modules/invoiceonline/invoice.controller.js index 096475b..492a813 100644 --- a/application/modules/invoiceonline/invoice.controller.js +++ b/application/modules/invoiceonline/invoice.controller.js @@ -297,86 +297,86 @@ const _generateInvoice = async (invoiceDate, uniqueCode, repost) => { const _postingInvoice = async (uniqueCode) => { let postingEt = []; - await knex.transaction(async (trx) => { - postingEt = await model.getPostingEt(trx); - - winston.logger.info( - `SCHEDULER-RETRY-${uniqueCode} GET POSTING ET : ${JSON.stringify( - postingEt - )}` - ); - - for (const item of postingEt) { - 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, + postingEt = await model.getPostingEt(knex); + winston.logger.info( + `SCHEDULER-RETRY-${uniqueCode} GET POSTING ET : ${JSON.stringify( + postingEt + )}` + ); + + 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 xmlRequest = helper.generateXMLRequest( - "ZFMCMS_FI_001", - xmlHeader, - xmlItem - ); + 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, + }; + }); - winston.logger.info( - `SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.reference} to SAP : ${xmlRequest}` - ); + let xmlRequest = helper.generateXMLRequest( + "ZFMCMS_FI_001", + xmlHeader, + xmlItem + ); - 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"; + winston.logger.info( + `SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.reference} to SAP : ${xmlRequest}` + ); - const response = await helper.postingSAP(url, xmlRequest); + 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"; - winston.logger.info( - `SCHEDULER-RETRY-${uniqueCode} RESPONSE posting ${ - item.reference - } to SAP : ${JSON.stringify(response)}` - ); + const response = await helper.postingSAP(url, xmlRequest); - // new transaction - // await knex.transaction(async trx => { + winston.logger.info( + `SCHEDULER-RETRY-${uniqueCode} RESPONSE posting ${ + item.reference + } to SAP : ${JSON.stringify(response)}` + ); - const resSAP = helper.getXMLData(response); + // new transaction + // await knex.transaction(async trx => { - winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} TRANSACTION 2`); - // UPDATE CMS - winston.logger.info( - `SCHEDULER-RETRY-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify( + const resSAP = helper.getXMLData(response); + + winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} TRANSACTION 2`); + // UPDATE CMS + winston.logger.info( + `SCHEDULER-RETRY-${uniqueCode} SET resposn FB70 to CMS : ${JSON.stringify( resSAP - )}` - ); + )}` + ); - const cms = await model.setReturnSAP( - { c_invoice_no: item.reference }, - resSAP, - trx - ); + const cms = await model.setReturnSAP( + { c_invoice_no: item.reference }, + resSAP, + trx + ); - winston.logger.info( - `SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}` - ); - // }) + winston.logger.info( + `SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}` + ); + }) } - }); + + // }); return postingEt; }; diff --git a/application/modules/invoiceonline/invoice.model.js b/application/modules/invoiceonline/invoice.model.js index 9b897ae..0b08439 100644 --- a/application/modules/invoiceonline/invoice.model.js +++ b/application/modules/invoiceonline/invoice.model.js @@ -88,7 +88,7 @@ const getPostingEt = async(trx) => { const setReturnSAP = async (invoice, resSAP, trx) => { 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({ @@ -103,18 +103,52 @@ const setReturnSAP = async (invoice, resSAP, trx) => { 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}')`) + + posting_et = await trx('public.posting_et').update({ + pos_status : resSAP.type == 0, + status0 : resSAP.type, + no_doksap : resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null, + status1 : resSAP.message ? resSAP.message : null + }, ['file_name','status0']).where({ + reference: invoice.c_invoice_no + }) + + }else { + + let text = resSAP.message; + let result = text.toUpperCase(); + + if(result.includes("ALREADY")){ + let result = text.toUpperCase().split('-'); + let doc_number = result[1].trim().split(" ")[0] + // update invoice + const d = new Date(); + let year = d.getFullYear(); + + posting_et = await trx('public.posting_et').update({ + pos_status : 0, + status0 : 'S', + no_doksap : doc_number, + status1 : resSAP.message ? resSAP.message : null + }, ['file_name','status0']).where({ + reference: invoice.c_invoice_no + }) + + }else{ + // update posting et + 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({ + reference: 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({ - reference: invoice.c_invoice_no - }) return { invoice : t_invoice,