diff --git a/application/modules/cashin/cahsin.controller.js b/application/modules/cashin/cahsin.controller.js index b13e650..0ffe3cd 100644 --- a/application/modules/cashin/cahsin.controller.js +++ b/application/modules/cashin/cahsin.controller.js @@ -56,129 +56,105 @@ const apiGenerateInvoice = async (req, res) => { return res.status(200).json(result); } + const cashIn = await _generateCashIn(uniqueCode) + 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' - // time - // let invoiceDate = moment(req.body.trx_date).format("YYYY-MM-DD") - - // let invoice = await _generateInvoice(invoiceDate, uniqueCode, req.body.repost) - // let partnerRecon = await model.getPartnerRecon(knex) - let data = await model.getRecon(knex) - let reserve = [] - for (const item of data) { + let partnerRecon = await model.getPartnerRecon(knex) + for (const item of partnerRecon) { + let reserve = [] + let code = "" await knex.transaction(async trx => { - const ci = await model.getReserve(knex, item.c_partner_code, item.d_payment_recon) - if(ci.length > 0) reserve.push(ci) - - // let code = await model.generateCode(trx, item.c_partner_code, moment().format("YYMMDD")) - // let cashInDetail = {} - // for (const reserve of reserveData) { - // cashInDetail = { - // c_reservation_no: reserve.c_reservation_no, - // m_amount: reserve.i_reservation_amount, - // c_payment_recon_no: reserve.c_payment_recon_no, - // d_payment: reserve.d_payment, - // c_partner_code: reserve.c_partner_code, - // c_partner_name: reserve.c_partner_name, - // c_load_balance_no: reserve.c_load_balance_no - // } - // } + code = await model.generateCode(trx, item.c_partner_code, moment().format("YYMMDD")) + // Insert cahs in + const dataCahsIn = { + c_partner_code : item.c_partner_code, + c_partner_name : item.c_partner_name, + c_cash_in_no : code, + d_cash_in : item.d_payment, + c_invoice_no : item.c_invoice_no, + i_amount : item.amount + } + const cashIn = await model.insertCashIn(trx, dataCahsIn) - // let insertCashInDetail = await model.insertCashInDetail(trx, cashInDetail) - }) - } - - // let data1 = await model.getRecon(knex, reserve) - result = { - code: "200", - message: "Succes posting", - data: {data, reserve} - }; - return res.status(200).json(result); - // let data = await model.getRecon(knex, reserve) - let resultEt = [] + // Insert cahs in detail + const reserv = await model.getReserve(trx, item.c_partner_code, item.d_payment) + for (const detail of reserv) { + reserve.push(detail.c_reservation_no) + let data = { + i_cash_in : cashIn.i_cash_in, + c_reservation_no : detail.c_reservation_no, + m_amount : detail.i_reservation_amount, + c_payment_recon_no : detail.c_payment_recon_no, + d_payment : detail.d_payment, + d_payment_recon : detail.d_payment_recon, + c_load_balance_no : detail.c_load_balance_no + } + const insert = await model.insertCashInDetail(trx, data) - // if(data.length > 0) for (const item of data) { - // await knex.transaction(async trx => { - - // let code = await model.generateCode(trx, item.c_partner_code, moment().format("YYMMDD")) - - // let postingEt = await model.insertPostingEt(code, item, trx) - - // let dataCashIn = { - // "d_cash_in" : item.d_payment_recon, - // "c_cash_in_no" : code, - // "c_invoice_no" : item.c_invoice_no, - // "i_amount" : item.amount, - // } - - // let cashIn = await model.insertCahsIn(trx, dataCashIn) - - // }) - - // 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' - - // let xmlHeader = { - // doc_header_text : code, - // ba: item.ba, - // doc_date : item.doc_date, - // posting_date : item.posting_date, - // fisc_year : item.fisc_year, - // fisc_period : item.fisc_period, - // doc_type: item.doc_type, - // c_invoice_no : code - // } - - // let xmlItem = ['S', 'H'].map(item => { - // return { - // c_account_code_topup : item == 'S' ? item.c_account_code_topup : "", - // c_account_topup : item == 'H' ? item.c_account_topup : "", - // ba : item.ba, - // amount : item.amount, - // posting_date : item.posting_date, - // c_invoice_no : item.c_invoice_no, - // item_text : `${item == 'S' ? "DEBIT" : "KREDIT"} INVOICE CMS ${item.doc_header_text}`, - // de_cre : item - // } - // }); - - // let xmlRequest = helper.generateXMLRequest('ZFMCMS_FI_001', xmlHeader, xmlItem) - - // winston.logger.debug(`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}`); + } - // const response = await helper.postingSAP(url, xmlRequest) - - // 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 - // 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)}`) - - // }) + const insertPostingEt = await model.insertPostingEt(code, item, trx) - // resultEt.push(cashIn) + }) - // } + let xmlHeader = { + doc_header_text : code, + ba: item.ba, + doc_date : item.doc_date, + posting_date : item.posting_date, + fisc_year : item.fisc_year, + fisc_period : item.fisc_period, + doc_type: item.doc_type, + c_invoice_no : code + } + let xmlItem = ['S', 'H'].map(items => { + return { + c_account_code_topup : items == 'S' ? item.c_account_code_topup : "", + c_account_topup : items == 'H' ? item.c_account_topup : "", + ba : item.ba, + amount : item.amount, + posting_date : item.posting_date, + c_invoice_no : code, + item_text : `${items == 'S' ? "DEBIT" : "KREDIT"} CAHSIN CMS ${code}`, + de_cre : items + } + }); + + let xmlRequest = helper.generateXMLRequest('ZFMCMS_FI_001', xmlHeader, xmlItem) + + winston.logger.debug(`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${code} to SAP : ${xmlRequest}`); + + const response = await helper.postingSAP(url, xmlRequest) + + winston.logger.debug(`SCHEDULER-FB70-${uniqueCode} RESPONSE posting ${code} 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(code, reserve, resSAP, trx) + + winston.logger.info(`SCHEDULER-FB70-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`) + + }) + + } result = { code: "200", message: "Succes posting", - data: resultEt + data: cashIn }; return res.status(200).json(result); + } catch (error) { // create log winston.logger.error( @@ -255,100 +231,50 @@ const apiPostingInvoice = async (req, res) => { } } -const _generateInvoice = async (invoiceDate, uniqueCode, repost) => { +const _generateCashIn = async (uniqueCode) => { - let invoice = [] - - await knex.transaction(async trx => { - - winston.logger.info(`SCHEDULER-FB70-${uniqueCode} TRANSACTION 1`); - - winston.logger.info(`SCHEDULER-FB70-${uniqueCode} execute payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`); - - invoice = await model.generateInvoice(invoiceDate, repost, trx) - - winston.logger.info(`SCHEDULER-FB70-${uniqueCode} result payment.sp_payment_req_topup_online ${JSON.stringify(invoice)}`); - - 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(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 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' - - 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.debug(`SCHEDULER-FB70-${uniqueCode} REQUEST posting ${invoice[key].c_invoice_no} to SAP : ${xmlRequest}`); + let partnerRecon = await model.getPartnerRecon(knex) + for (const item of partnerRecon) { + let reserve = [] + let code = "" + await knex.transaction(async trx => { + code = await model.generateCode(trx, item.c_partner_code, moment().format("YYMMDD")) + winston.logger.info(`SCHEDULER-FB70-${uniqueCode} generate cash in : ${code}`) - const response = await helper.postingSAP(url, xmlRequest) + // Insert cahs in + const dataCahsIn = { + c_partner_code : item.c_partner_code, + c_partner_name : item.c_partner_name, + c_cash_in_no : code, + d_cash_in : item.d_payment, + c_invoice_no : item.c_invoice_no, + i_amount : item.amount + } + winston.logger.info(`SCHEDULER-FB70-${uniqueCode} cash in ${code} data : ${JSON.stringify(dataCahsIn)}`) + const cashIn = await model.insertCashIn(trx, dataCahsIn) - 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 - 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)}`) - - }) - } + // Insert cahs in detail + const reserv = await model.getReserve(trx, item.c_partner_code, item.d_payment) + for (const detail of reserv) { + reserve.push(detail.c_reservation_no) + let data = { + i_cash_in : cashIn.i_cash_in, + c_reservation_no : detail.c_reservation_no, + m_amount : detail.i_reservation_amount, + c_payment_recon_no : detail.c_payment_recon_no, + d_payment : detail.d_payment, + d_payment_recon : detail.d_payment_recon, + c_load_balance_no : detail.c_load_balance_no + } + await model.insertCashInDetail(trx, data) + } + await model.insertPostingEt(code, item, trx) + }) + return partnerRecon } - - return invoice - } -const _postingInvoice = async (uniqueCode) => { +const _postingCashIn = async (uniqueCode) => { let postingEt = [] await knex.transaction(async trx => { @@ -401,12 +327,11 @@ const _postingInvoice = async (uniqueCode) => { // 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(item.reference, resSAP, trx) winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`) // }) } - }) return postingEt @@ -417,6 +342,6 @@ module.exports = { validate, apiPostingInvoice, apiGenerateInvoice, - _postingInvoice, - _generateInvoice + _postingCashIn, + _generateCashIn }; \ No newline at end of file diff --git a/application/modules/cashin/cahsin.model.js b/application/modules/cashin/cahsin.model.js index 2b4817f..cc5bf33 100644 --- a/application/modules/cashin/cahsin.model.js +++ b/application/modules/cashin/cahsin.model.js @@ -85,40 +85,41 @@ const getPostingEt = async(trx) => { } -const setReturnSAP = async (invoice, resSAP, trx) => { +const setReturnSAP = async (code, reserve, resSAP, trx) => { let t_cash_in = {} - let t_load_balance = {} + let reserveBalance = {} if(resSAP.type == 'S'){ // update invoice t_cash_in = await trx('payment.t_d_cash_in').update({ c_return_sap: resSAP.data.E_DOCUMENTNUMBER - }, ['c_invoice_no','c_return_sap']) + }, ['c_cash_in_no','c_return_sap']) .where({ - c_invoice_no: invoice.c_invoice_no + c_cash_in_no: code }); // 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}')`) + reserveBalance = await trx('trx.t_d_reservation_balance').update({ + c_return_cash_in_sap: resSAP.data.E_DOCUMENTNUMBER, + c_cash_in_no: code + }, ['c_cash_in_no', 'c_return_sap']) + .whereIn("c_reservation_no", reserve) } // update posting et const posting_et = await trx('public.posting_et').update({ - pos_status : resSAP.type == 'S' ? 0 : 3, + pos_status : resSAP.type == 'S' ? 0 : 4, 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 + reference: code }) return { - invoice : t_cash_in, - load_balance : t_load_balance, + cash_in : t_cash_in, + reserveBalance : reserveBalance, posting_et : posting_et } @@ -205,9 +206,11 @@ const getReserve = async(trx, code, date) => { "tdrb.i_reservation_amount", "tdpr.c_payment_recon_no", "tdpr.d_payment", + "tdpr.d_payment_recon", "tdrb.c_partner_code", "tdrb.c_partner_name", - "tdrb.c_load_balance_no" + "tdrb.c_load_balance_no", + "tdi.c_invoice_no" ]) .from("trx.t_d_reservation_balance AS tdrb") .leftJoin('trx.t_d_load_balance as tdlb', function () { @@ -233,8 +236,28 @@ const getPartnerRecon = async(trx) => { const res = await trx.select([ "tdpr.c_partner_code", - "tdrb.i_reservation_amount", - "tdpr.c_payment_recon_no" + "tdrb.c_partner_name", + "tdi.c_invoice_no", + "tdi.d_invoice_date", + trx.raw("to_char(tdpr.d_payment, 'YYYY-MM-DD') AS d_payment"), + trx.raw("SUM(tdrb.i_reservation_amount) AS amount"), + trx.raw("to_char(tdpr.d_payment, 'MM') AS fisc_period"), + trx.raw("to_char(tdpr.d_payment, 'YYYY') AS fisc_year"), + trx.raw("to_char(tdpr.d_payment, 'YYYYMMDD') AS doc_date"), + trx.raw("'FB70' AS tcode"), + trx.raw("'A' as reversal"), + trx.raw("'DR' AS doc_type"), + trx.raw("'C100' AS ba"), + "tmp.c_account_topup", + "tmp.c_account_code_topup", + "i_company", + "i_holding", + trx.raw(`CASE + WHEN to_char(tdpr.d_payment, 'MM') = to_char(tdi.d_invoice_date, 'MM') + THEN FALSE + ELSE + TRUE + END b_past_month`), ]) .from("trx.t_d_reservation_balance AS tdrb") .leftJoin('trx.t_d_load_balance as tdlb', function () { @@ -246,13 +269,18 @@ const getPartnerRecon = async(trx) => { .leftJoin('payment.t_d_payment_recon as tdpr', function () { this.on('tdpr.c_reservation_no', '=', 'tdrb.c_reservation_no') }) + .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("tdrb.c_partner_code", "=", code) .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)`) + .groupBy(["tdpr.c_partner_code","tdrb.c_partner_name","tdi.c_invoice_no","tdi.d_invoice_date", "tdpr.d_payment", "tmp.c_account_topup", "tmp.c_account_code_topup", "tdi.i_company", "i_holding"]) + return res + } const generateCode = async (trx, c_partner_code, d_payment_recon) => { @@ -283,6 +311,11 @@ const insertCashInDetail = async (trx, data) => { return result } +const insertCashIn = async (trx, data) => { + let result = await trx('payment.t_d_cash_in').insert(data,["*"]) + return result[0] +} + module.exports = { generateInvoice, insertPostingEt, @@ -294,5 +327,6 @@ module.exports = { generateCode, insertCashInDetail, getReserve, - getPartnerRecon + getPartnerRecon, + insertCashIn }; \ No newline at end of file