From e30c78beb786bbd5b65fb4fb602c1713e13f4748 Mon Sep 17 00:00:00 2001 From: Mohamad Ihsan Date: Fri, 3 Nov 2023 15:50:11 +0700 Subject: [PATCH] update cash in penentuan in period atau out period --- application/modules/cashin/cahsin.model.js | 560 ++++++++++++--------- 1 file changed, 319 insertions(+), 241 deletions(-) diff --git a/application/modules/cashin/cahsin.model.js b/application/modules/cashin/cahsin.model.js index 75ddd59..9b3a5c8 100644 --- a/application/modules/cashin/cahsin.model.js +++ b/application/modules/cashin/cahsin.model.js @@ -1,17 +1,18 @@ - // GET PARTNER, MERCHANT, TERMINAL FROM TID const generateInvoice = async (invoiceDate, repost, trx) => { + let result = await trx.select( + trx.raw( + `* FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})` + ) + ); - let result = await trx.select(trx.raw(`* FROM payment.sp_payment_req_topup_online('${invoiceDate}', ${repost})`)) - return result; - -} +}; const insertPostingEt = async (code, dataCashIn, trx) => { - let type = ['S', 'H'] - let et = [] - let posting_et = [] + let type = ['S', 'H']; + let et = []; + let posting_et = []; type.forEach((item, index) => { let data = { file_name: code, @@ -25,137 +26,193 @@ const insertPostingEt = async (code, dataCashIn, trx) => { reference: code, amount: dataCashIn.amount, ba: dataCashIn.ba, - indeks: (index+1), + indeks: index + 1, totalidx: type.length, doc_header_text: code, - pos_status : 4 - } - - if(item == 'H'){ - data = {...data, ...{ - text: `Cash-in CMS ${code}`, - debit_kredit: item, - account: dataCashIn.c_account_topup, - customer: null - }} - }else{ - data = {...data, ...{ - text: `Cash-in CMS ${code}`, - debit_kredit: item, - account: null, - customer: dataCashIn.c_account_code_topup - }} + pos_status: 4, + }; + + if (item == 'H') { + data = { + ...data, + ...{ + text: `Cash-in CMS ${code}`, + debit_kredit: item, + account: dataCashIn.c_account_topup, + customer: null, + }, + }; + } else { + data = { + ...data, + ...{ + text: `Cash-in CMS ${code}`, + debit_kredit: item, + account: null, + customer: dataCashIn.c_account_code_topup, + }, + }; } - et.push(data) + 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('public.posting_et AS pe').select([ - 'doc_header_text', + posting_et = await trx('public.posting_et').insert(et, [ + 'indeks', + 'customer', + 'account', 'ba', - 'doc_date', - 'posting_date', 'fiscal_year', - trx.raw(`TO_CHAR(doc_date::DATE, 'MM') AS fis_period`), - 'doc_type', - 'reference', - 'no_dok', - 'tcode', 'amount', - trx.raw(`'IDR' AS currency`), - trx.raw(`(SELECT customer FROM public.posting_et where reference = pe.reference and debit_kredit = 'S') as customer`), - trx.raw(`(SELECT account FROM public.posting_et where reference = pe.reference and debit_kredit = 'H') as account `), - trx.raw(`(SELECT text FROM public.posting_et where reference = pe.reference and debit_kredit = 'S') as text1`), - trx.raw(`(SELECT text FROM public.posting_et where reference = pe.reference and debit_kredit = 'H') as text2`) - ]) - .where('tcode', '=', "FB70") - // .where('reference', '=', reference) - .whereIn('pos_status', ["4"]) + 'no_dok', + 'text', + 'debit_kredit', + ]); - .groupBy(['doc_header_text', 'ba', 'doc_header_text', 'tcode', 'ba', 'amount', 'posting_date', 'doc_date', 'doc_type', 'fiscal_year', 'reference', 'no_dok']) - - return result - -} + return posting_et[0]; +}; -const setReturnSAP = async (code, resSAP, trx) => { - let t_cash_in = [] - let reserveBalance = [] +const getPostingEt = async (trx) => { + const result = await trx('public.posting_et AS pe') + .select([ + 'doc_header_text', + 'ba', + 'doc_date', + 'posting_date', + 'fiscal_year', + trx.raw(`TO_CHAR(doc_date::DATE, 'MM') AS fis_period`), + 'doc_type', + 'reference', + 'no_dok', + 'tcode', + 'amount', + trx.raw(`'IDR' AS currency`), + trx.raw( + `(SELECT customer FROM public.posting_et where reference = pe.reference and debit_kredit = 'S') as customer` + ), + trx.raw( + `(SELECT account FROM public.posting_et where reference = pe.reference and debit_kredit = 'H') as account ` + ), + trx.raw( + `(SELECT text FROM public.posting_et where reference = pe.reference and debit_kredit = 'S') as text1` + ), + trx.raw( + `(SELECT text FROM public.posting_et where reference = pe.reference and debit_kredit = 'H') as text2` + ), + ]) + .where('tcode', '=', 'FB70') + // .where('reference', '=', reference) + .whereIn('pos_status', ['4']) - if(resSAP.type == 'S'){ + .groupBy([ + 'doc_header_text', + 'ba', + 'doc_header_text', + 'tcode', + 'ba', + 'amount', + 'posting_date', + 'doc_date', + 'doc_type', + 'fiscal_year', + 'reference', + 'no_dok', + ]); + + return result; +}; + +const setReturnSAP = async (code, resSAP, trx) => { + let t_cash_in = []; + 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_return_year_sap: resSAP.data.E_DOCUMENTYEAR - }, ['c_cash_in_no','c_return_sap']) - .where({ - c_cash_in_no: code - }); - - // update load balance - 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']) - .whereRaw(`c_reservation_no IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail tdcid LEFT JOIN payment.t_d_cash_in tdci ON tdcid.i_cash_in = tdci.i_cash_in WHERE tdci.c_cash_in_no = '${code}')`) + t_cash_in = await trx('payment.t_d_cash_in') + .update( + { + c_return_sap: resSAP.data.E_DOCUMENTNUMBER, + c_return_year_sap: resSAP.data.E_DOCUMENTYEAR, + }, + ['c_cash_in_no', 'c_return_sap'] + ) + .where({ + c_cash_in_no: code, + }); - }else { + // update load balance + 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'] + ) + .whereRaw( + `c_reservation_no IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail tdcid LEFT JOIN payment.t_d_cash_in tdci ON tdcid.i_cash_in = tdci.i_cash_in WHERE tdci.c_cash_in_no = '${code}')` + ); + } else { let text = resSAP.message; let result = text.toUpperCase(); - if(result.includes("ALREADY")){ + if (result.includes('ALREADY')) { let result = text.toUpperCase().split('-'); - let doc_number = result[1].trim().split(" ")[0] + let doc_number = result[1].trim().split(' ')[0]; // update invoice const d = new Date(); let year = d.getFullYear(); - t_cash_in = await trx('payment.t_d_cash_in').update({ - c_return_sap: doc_number, - c_return_year_sap: year - }, ['c_cash_in_no','c_return_sap']) - .where({ - c_cash_in_no: code - }); + t_cash_in = await trx('payment.t_d_cash_in') + .update( + { + c_return_sap: doc_number, + c_return_year_sap: year, + }, + ['c_cash_in_no', 'c_return_sap'] + ) + .where({ + c_cash_in_no: code, + }); // update load balance - 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']) - .whereRaw(`c_reservation_no IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail tdcid LEFT JOIN payment.t_d_cash_in tdci ON tdcid.i_cash_in = tdci.i_cash_in WHERE tdci.c_cash_in_no = '${code}')`) - + 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'] + ) + .whereRaw( + `c_reservation_no IN (SELECT c_reservation_no FROM payment.t_d_cash_in_detail tdcid LEFT JOIN payment.t_d_cash_in tdci ON tdcid.i_cash_in = tdci.i_cash_in WHERE tdci.c_cash_in_no = '${code}')` + ); } - } // update posting et - const posting_et = await trx('public.posting_et').update({ - 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({ - no_dok: code - }) + const posting_et = await trx('public.posting_et') + .update( + { + 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({ + no_dok: code, + }); return { - cash_in : t_cash_in[0], - reserveBalance : reserveBalance[0], - posting_et : posting_et[0] - } - -} + cash_in: t_cash_in[0], + reserveBalance: reserveBalance[0], + posting_et: posting_et[0], + }; +}; const getRecon = async (trx) => { - let sql = `SELECT tdi.c_invoice_no, 'FB70' AS tcode, @@ -200,160 +257,181 @@ const getRecon = async (trx) => { SELECT c_reservation_no FROM payment.t_d_payment_recon tdpr WHERE tdpr.c_status = 'S' ) GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, tdpr.d_payment, b_past_month - ORDER BY tdpr.d_payment` - let result = await trx.raw(sql) + ORDER BY tdpr.d_payment`; + let result = await trx.raw(sql); - return result.rows -} + return result.rows; +}; -const getReserve = async(trx, code, date, invoice) => { +const getReserve = async (trx, code, date, invoice) => { + const res = await trx + .select([ + 'tdrb.c_reservation_no', + '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', + 'tdi.c_invoice_no', + ]) + .from('trx.t_d_reservation_balance AS tdrb') + .leftJoin('trx.t_d_load_balance as tdlb', function () { + this.on('tdrb.c_load_balance_no', '=', 'tdlb.c_load_balance_no'); + }) + .leftJoin('payment.t_d_invoice as tdi', function () { + this.on('tdi.c_invoice_no', '=', 'tdlb.c_invoice_no').orOn( + 'tdi.c_invoice_no', + '=', + 'tdrb.c_invoice_no' + ); + }) + .leftJoin('payment.t_d_payment_recon as tdpr', function () { + this.on('tdpr.c_reservation_no', '=', 'tdrb.c_reservation_no'); + }) + .where('tdrb.c_status', '=', 'A') + .where('tdi.c_status', '!=', 'X') + .where('tdrb.c_partner_code', '=', code) + .where('tdpr.d_payment', '=', date) + .where('tdi.c_invoice_no', '=', invoice) + .where('tdpr.c_status', '=', 'S') + .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 WHERE b_reversal IS NOT TRUE)` + ); + return res; +}; - const res = await trx.select([ - "tdrb.c_reservation_no", - "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", - "tdi.c_invoice_no" - ]) - .from("trx.t_d_reservation_balance AS tdrb") - .leftJoin('trx.t_d_load_balance as tdlb', function () { - this.on('tdrb.c_load_balance_no', '=', 'tdlb.c_load_balance_no') - }) - .leftJoin('payment.t_d_invoice as tdi', function () { - this.on('tdi.c_invoice_no', '=', 'tdlb.c_invoice_no') - .orOn('tdi.c_invoice_no', '=', 'tdrb.c_invoice_no') - }) - .leftJoin('payment.t_d_payment_recon as tdpr', function () { - this.on('tdpr.c_reservation_no', '=', 'tdrb.c_reservation_no') - }) - .where("tdrb.c_status", "=", "A") - .where("tdi.c_status", "!=", "X") - .where("tdrb.c_partner_code", "=", code) - .where("tdpr.d_payment", "=", date) - .where("tdi.c_invoice_no", "=", invoice) - .where("tdpr.c_status", "=", "S") - .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 WHERE b_reversal IS NOT TRUE)`) - return res -} - -const getPartnerRecon = async(trx) => { - - const res = await trx.select([ - "tdpr.c_partner_code", - "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"), - "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') +const getPartnerRecon = async (trx) => { + const res = await trx + .select([ + 'tdpr.c_partner_code', + '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'), + '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 'IP' ELSE 'OP' END c_in_period`), - ]) - .from("trx.t_d_reservation_balance AS tdrb") - .leftJoin('trx.t_d_load_balance as tdlb', function () { - this.on('tdrb.c_load_balance_no', '=', 'tdlb.c_load_balance_no') - }) - .leftJoin('payment.t_d_invoice as tdi', function () { - this - .on('tdi.c_invoice_no', '=', 'tdlb.c_invoice_no') - .orOn('tdi.c_invoice_no', '=', 'tdrb.c_invoice_no') - }) - .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.i_partner', '=', 'tdrb.i_partner') - }) - - .where("tdrb.c_status", "=", "A") - .where("tdi.c_status", "!=", "X") - .where("tdpr.c_status", "=", "S") - .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 WHERE b_reversal IS NOT TRUE)`) - .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"]) + ]) + .from('trx.t_d_reservation_balance AS tdrb') + .leftJoin('trx.t_d_load_balance as tdlb', function () { + this.on('tdrb.c_load_balance_no', '=', 'tdlb.c_load_balance_no'); + }) + .leftJoin('payment.t_d_invoice as tdi', function () { + this.on('tdi.c_invoice_no', '=', 'tdlb.c_invoice_no').orOn( + 'tdi.c_invoice_no', + '=', + 'tdrb.c_invoice_no' + ); + }) + .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.i_partner', '=', 'tdrb.i_partner'); + }) + + .where('tdrb.c_status', '=', 'A') + .where('tdi.c_status', '!=', 'X') + .where('tdpr.c_status', '=', 'S') + .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 WHERE b_reversal IS NOT TRUE)` + ) + .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', + ]); // .toSql() - return res - -} + return res; +}; const generateCode = async (trx, c_partner_code, d_payment_recon) => { - let code = "" - let prefix = `CIN${c_partner_code}${d_payment_recon}` + let code = ''; + let prefix = `CIN${c_partner_code}${d_payment_recon}`; - let result =await trx.select([trx.raw(`substring(c_cash_in_no, 13, 3) AS code`)]) - .from("payment.t_d_cash_in") - .whereRaw(`substring(c_cash_in_no, 1, 12) = '${prefix}'`) - // .where("d_project_date", projectDate) - .orderBy('c_cash_in_no', 'DESC') - .first() + let result = await trx + .select([trx.raw(`substring(c_cash_in_no, 13, 3) AS code`)]) + .from('payment.t_d_cash_in') + .whereRaw(`substring(c_cash_in_no, 1, 12) = '${prefix}'`) + // .where("d_project_date", projectDate) + .orderBy('c_cash_in_no', 'DESC') + .first(); - if(result){ - let counter = parseInt(result.code)+1 + if (result) { + let counter = parseInt(result.code) + 1; counter = String(counter).padStart(3, '0'); - code = `${prefix}${counter}` - - }else{ - code = `${prefix}001` + code = `${prefix}${counter}`; + } else { + code = `${prefix}001`; } - return code -} + return code; +}; const insertCashInDetail = async (trx, data) => { - let result = [] - if(data.length > 0){ - result = await trx('payment.t_d_cash_in_detail').insert(data,["*"]) + let result = []; + if (data.length > 0) { + result = await trx('payment.t_d_cash_in_detail').insert(data, ['*']); } - return result -} + return result; +}; const updateIntoReserve = async (trx, dataReserve, reserveNo) => { - let result = await trx('trx.t_d_reservation_balance').update(dataReserve, ["*"]).where({c_reservation_no: reserveNo}) - return result -} + let result = await trx('trx.t_d_reservation_balance') + .update(dataReserve, ['*']) + .where({ c_reservation_no: reserveNo }); + return result; +}; const insertCashIn = async (trx, data) => { - let result = await trx('payment.t_d_cash_in').insert(data,["*"]) - return result[0] -} + let result = await trx('payment.t_d_cash_in').insert(data, ['*']); + return result[0]; +}; 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 : 119, - d_log : trx.raw('NOW()'), - c_invoice_no : data.c_cash_in_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, - m_mdr_amount : 0, - m_mdr_net : data.amount - - }, ['i_log_name', 'c_note']) - - return result - - } + console.log(data); + const result = await trx('log.t_log_invoice').insert( + { + // i_log_name : 102, + i_log_name: 119, + d_log: trx.raw('NOW()'), + c_invoice_no: data.c_cash_in_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, + m_mdr_amount: 0, + m_mdr_net: data.amount, + }, + ['i_log_name', 'c_note'] + ); + + return result; +}; module.exports = { generateInvoice, @@ -368,5 +446,5 @@ module.exports = { getReserve, getPartnerRecon, insertCashIn, - updateIntoReserve -}; \ No newline at end of file + updateIntoReserve, +};