// GET PARTNER, MERCHANT, TERMINAL FROM TID const generateInvoice = async (invoiceDate, repost, trx) => { let result = await trx.raw(`SELECT * FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`) return result.rows || result; } const insertPostingEt = async (invoice, trx) => { console.log("invoice : ", invoice) let type = ['S', 'H'] let et = [] let posting_et = [] type.forEach((item, index) => { let data = { file_name: invoice.file_name, tcode: invoice.tcode, reversal: invoice.reversal, doc_type: invoice.doc_type, fiscal_year: invoice.fisc_year, posting_date: invoice.posting_date, doc_date: invoice.doc_date, no_dok: invoice.c_invoice_no, reference: invoice.c_invoice_no, amount: invoice.amount, ba: invoice.ba, indeks: (index + 1), totalidx: type.length, doc_header_text: invoice.c_invoice_no, pos_status: 3 } if (item == 'H') { data = { ...data, ...{ text: `Posting Invoice CMS ${invoice.c_invoice_no}`, debit_kredit: item, account: invoice.c_account_topup, customer: null } } } else { data = { ...data, ...{ text: `Posting Invoice CMS ${invoice.c_invoice_no}`, debit_kredit: item, account: null, customer: invoice.c_account_code_topup } } } et.push(data) }); posting_et = await trx('public.posting_et').insert(et, ['indeks', 'customer', 'account', 'ba', 'fiscal_year', 'amount', 'no_dok', 'text', 'debit_kredit']) return posting_et[0] } const getPostingEt = async (trx) => { const result = await trx.raw(` WITH params AS ( SELECT EXTRACT(YEAR FROM CURRENT_DATE)::TEXT AS curr_year, EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year')::TEXT AS prev_year, TO_CHAR(CURRENT_DATE, 'MM') AS curr_month ), base AS ( SELECT pe.doc_header_text, pe.ba, pe.doc_date, pe.posting_date, pe.fiscal_year, TO_CHAR(pe.doc_date::DATE, 'MM') AS fis_period, pe.doc_type, pe.reference, pe.tcode, pe.amount, pe.customer, pe.account, pe.text, pe.debit_kredit FROM public.posting_et pe CROSS JOIN params p WHERE pe.tcode = 'FB70' AND pe.pos_status = '3' -- Prefix search still index friendly AND pe.doc_header_text LIKE 'INV%' AND ( -- Current year all months pe.fiscal_year = p.curr_year OR ( -- Previous year December only -- only when current month is January p.curr_month = '01' AND pe.fiscal_year = p.prev_year AND TO_CHAR(pe.doc_date::DATE, 'MM') = '12' ) ) ) SELECT b.doc_header_text, b.ba, b.doc_date, b.posting_date, b.fiscal_year, b.fis_period, b.doc_type, b.reference, b.tcode, b.amount, 'IDR'::VARCHAR AS currency, MAX(b.customer) FILTER ( WHERE b.debit_kredit = 'S' ) AS customer, MAX(b.account) FILTER ( WHERE b.debit_kredit = 'H' ) AS account, MAX(b.text) FILTER ( WHERE b.debit_kredit = 'S' ) AS text1, MAX(b.text) FILTER ( WHERE b.debit_kredit = 'H' ) AS text2 FROM base b GROUP BY b.doc_header_text, b.ba, b.doc_date, b.posting_date, b.fiscal_year, b.fis_period, b.doc_type, b.reference, b.tcode, b.amount ORDER BY b.fiscal_year, b.doc_date, b.reference `); return result.rows || result } const setReturnSAP = async (invoice, resSAP, trx, posting = false, c_invoice_no) => { let t_invoice = {} let t_load_balance = {} let posting_et = {} const paymentRequestSql = ` SELECT c_load_balance_no FROM payment.t_d_payment_request_topup_online WHERE c_payment = ? `; if (resSAP.type == 'S') { // update invoice t_invoice = await trx.raw( ` UPDATE payment.t_d_invoice SET c_return_sap = ? WHERE c_invoice_no = ? RETURNING * `, [resSAP.data.E_DOCUMENTNUMBER, c_invoice_no] ); // update load balance t_load_balance = await trx.raw( ` UPDATE trx.t_d_load_balance SET c_return_sap = ? WHERE c_load_balance_no IN ( ${paymentRequestSql} ) RETURNING c_load_balance_no, c_return_sap `, [resSAP.data.E_DOCUMENTNUMBER, c_invoice_no] ); posting_et = await trx.raw( ` UPDATE public.posting_et SET pos_status = 0, status0 = ?, no_doksap = ?, status1 = ? WHERE no_dok = ? RETURNING file_name, status0 `, [ resSAP.type, resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null, resSAP.message ? resSAP.message : null, c_invoice_no ] ); if (posting) { await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], "SUCCESSFULLY", 102, trx) } else { await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], "SUCCESS TO RETRY SAP", 118, trx) } } 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] posting_et = await trx.raw( ` UPDATE public.posting_et SET pos_status = 0, status0 = 'S', no_doksap = ?, status1 = ? WHERE no_dok = ? RETURNING file_name, status0 `, [doc_number, resSAP.message ? resSAP.message : null, c_invoice_no] ); // update invoice t_invoice = await trx.raw( ` UPDATE payment.t_d_invoice SET c_return_sap = ? WHERE c_invoice_no = ? RETURNING * `, [doc_number, c_invoice_no] ); // update load balance t_load_balance = await trx.raw( ` UPDATE trx.t_d_load_balance SET c_return_sap = ? WHERE c_load_balance_no IN ( ${paymentRequestSql} ) RETURNING c_load_balance_no, c_return_sap `, [doc_number, c_invoice_no] ); if (posting) { await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], "SUCCESSFULLY", 102, trx) } else { await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], "SUCCESS TO RETRY SAP", 118, trx) } } else { // update posting et posting_et = await trx.raw( ` UPDATE public.posting_et SET pos_status = ?, status0 = ?, no_doksap = ?, status1 = ? WHERE no_dok = ? RETURNING file_name, status0 `, [ resSAP.type == 'S' ? 0 : 3, resSAP.type, resSAP.data.E_DOCUMENTNUMBER ? resSAP.data.E_DOCUMENTNUMBER : null, resSAP.message ? resSAP.message : null, c_invoice_no ] ); // update invoice t_invoice = await trx.raw( ` SELECT * FROM payment.t_d_invoice WHERE c_invoice_no = ? `, [c_invoice_no] ); if (posting) { await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], `SUCCESS WITH SAP ERR: ${resSAP.message}`, 102, trx) } else { await writePaynentHistory(t_invoice.rows?.[0] || t_invoice[0], `FAIL TO RETRY WITH SAP ERR: ${resSAP.message}`, 118, trx) } } } // update posting et return { invoice: t_invoice.rows || t_invoice, load_balance: t_load_balance.rows || t_load_balance, posting_et: posting_et.rows || posting_et } } 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: 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: message, c_status: 'S', d_created: trx.raw('NOW()'), m_amount: data?.amount || 0, m_mdr_amount: 0, m_mdr_net: data?.amount || 0 }, ['i_log_name', 'c_note']) return result } 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') .where('c_invoice_no', '=', code) .first() return result } module.exports = { generateInvoice, insertPostingEt, setReturnSAP, getPostingEt, writePaynentHistory, getParameterSetting, getInvoice };