const { param, body, validationResult } = require("express-validator"); const helper = require("../../helpers/helper"); const winston = require("../../helpers/winston.logger"); const model = require("./cahsin.model"); const moment = require("moment"); const knex = require("../../../infrastructure/database/knex"); moment.locale("id"); var result = {}; var uniqueCode; // VALIDATION const validate = (method) => { switch (method) { case "generateInvoice": return [ body('trx_date').notEmpty().withMessage('trx_date is required!').isISO8601().withMessage('invalid trx_date format YYYY-MM-DD !').escape().trim(), body('repost').notEmpty().withMessage('repost is required!').isBoolean().withMessage('repost must boolean!').escape().trim(), ]; case "postingInvoice": return [ body('trx_date').notEmpty().withMessage('trx_date is required!').isISO8601().withMessage('invalid trx_date format YYYY-MM-DD !').escape().trim(), ]; default: break; } }; // POSTING INVOICE const apiGenerateCahsIn = async (req, res) => { try { // generate unique code uniqueCode = helper.getUniqueCode(); // return uniqueCode // log info winston.logger.info( `${uniqueCode} REQUEST generate cashin: ${JSON.stringify(req.body)}` ); // check validator const err = validationResult(req, res); if (!err.isEmpty()) { result = { code: "400", message: err.errors[0].msg, data: {}, }; // log warn winston.logger.warn( `${uniqueCode} RESPONSE generate cashin: ${JSON.stringify(result)}` ); return res.status(200).json(result); } const cashIn = await _generateCashIn(uniqueCode) result = { code: "200", message: "Succes posting", data: cashIn }; return res.status(200).json(result); } catch (error) { // create log winston.logger.error( `500 internal server error - backend server | ${error.message}` ); return res.status(200).json({ code: "500", message: process.env.NODE_ENV != "production" ? error.message : "500 internal server error - backend server.", data: {}, }); } }; // RETRY POSTING INVOICE const apiPostingCahsIn = async (req, res) => { try { // generate unique code uniqueCode = helper.getUniqueCode() let { body } = req // log info winston.logger.info( `${uniqueCode} REQUEST posting cash in : ${JSON.stringify(req.body)}` ); // check validator const err = validationResult(req, res); if (!err.isEmpty()) { result = { code: "400", message: err.errors[0].msg, data: {}, }; // log warn winston.logger.warn( `${uniqueCode} RESPONSE generate posting cash in : ${JSON.stringify(result)}` ); return res.status(200).json(result); } // return uniqueCode winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RE-TRY POSTING CAHS IN`) const invoice = await _postingCashIn(uniqueCode) result = { code: "200", message: "Succes", data: invoice, } return res.status(200).json(result); } catch (error) { // create log winston.logger.error( `500 internal server error - backend server | ${error.message}` ); return res.status(200).json({ code: "500", message: process.env.NODE_ENV != "production" ? error.message : "500 internal server error - backend server.", data: {}, }); } } const _generateCashIn = async (uniqueCode) => { 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}`) // Insert cahs in const dataCahsIn = { c_partner_code : item.c_partner_code, c_partner_name : item.c_partner_name, c_cash_in_no : code, c_in_period : item.c_in_period, d_credit : 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) 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) for (const detail of reserv) { data.push({ 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 }) let reserveNo = detail.c_reservation_no let dataReserve = { c_cash_in_no : cashIn.c_cash_in_no, d_credit : cashIn.d_credit, c_in_period : cashIn.c_in_period, } await model.updateIntoReserve(trx, dataReserve, reserveNo) } // await model.insertPostingEt(code, item, trx) await model.insertCashInDetail(trx, data) }) } return partnerRecon } const _postingCashIn = async (uniqueCode) => { let data = [] let postingEt = await model.getPostingEt(knex) for (const item of postingEt) { await knex.transaction(async trx => { winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} GET POSTING ET : ${JSON.stringify(item)}`) 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) console.log(xmlRequest) winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} REQUEST posting ${item.reference} to SAP : ${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' const response = await helper.postingSAP(url, xmlRequest) winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RESPONSE posting ${item.reference} to SAP : ${JSON.stringify(response)}`); 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(item.no_dok, resSAP, trx) winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RESULT CMS : ${JSON.stringify(cms)}`) }) } return postingEt } module.exports = { validate, apiGenerateCahsIn, apiPostingCahsIn, _postingCashIn, _generateCashIn };