diff --git a/application/modules/invoiceonline/invoice.model.js b/application/modules/invoiceonline/invoice.model.js index bc27273..7dbf037 100644 --- a/application/modules/invoiceonline/invoice.model.js +++ b/application/modules/invoiceonline/invoice.model.js @@ -60,26 +60,40 @@ const insertPostingEt = async (invoice, trx) => { 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', - 'tcode', - 'amount', + const result = await trx('public.posting_et as pe') + .leftJoin('public.posting_et as pe2', 'pe2.reference', 'pe.reference') + .select([ + 'pe.doc_header_text', + 'pe.ba', + 'pe.doc_date', + 'pe.posting_date', + 'pe.fiscal_year', + trx.raw(`TO_CHAR(pe.doc_date::DATE, 'MM') AS fis_period`), + 'pe.doc_type', + 'pe.reference', + 'pe.tcode', + 'pe.amount', trx.raw(`'IDR' AS currency`), - trx.raw(`(select pe2.customer from posting_et pe2 where pe2.reference = pe.reference and debit_kredit = 'S' LIMIT 1) as customer`), - trx.raw(`(select pe2.account from posting_et pe2 where pe2.reference = pe.reference and debit_kredit = 'H' LIMIT 1) as account`), - trx.raw(`(select pe2.text from posting_et pe2 where pe2.reference = pe.reference and debit_kredit = 'S' LIMIT 1) as text1`), - trx.raw(`(select pe2.text from posting_et pe2 where pe2.reference = pe.reference and debit_kredit = 'H' LIMIT 1) as text2`), + + trx.raw(`MAX(pe2.customer) FILTER (WHERE pe2.debit_kredit = 'S') AS customer`), + trx.raw(`MAX(pe2.account) FILTER (WHERE pe2.debit_kredit = 'H') AS account`), + trx.raw(`MAX(pe2.text) FILTER (WHERE pe2.debit_kredit = 'S') AS text1`), + trx.raw(`MAX(pe2.text) FILTER (WHERE pe2.debit_kredit = 'H') AS text2`) ]) - .where('tcode', '=', "FB70") - .whereIn('pos_status', ["3"]) - .groupBy([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12]) + .where('pe.tcode', 'FB70') + .whereIn('pe.pos_status', ['3']) + .groupBy([ + 'pe.doc_header_text', + 'pe.ba', + 'pe.doc_date', + 'pe.posting_date', + 'pe.fiscal_year', + trx.raw(`TO_CHAR(pe.doc_date::DATE, 'MM')`), + 'pe.doc_type', + 'pe.reference', + 'pe.tcode', + 'pe.amount' + ]); return result diff --git a/docs/cashin-index-checklist.md b/docs/cashin-index-checklist.md index c5670e0..bc95ef2 100644 --- a/docs/cashin-index-checklist.md +++ b/docs/cashin-index-checklist.md @@ -90,6 +90,12 @@ WHERE c_status <> 'X' AND c_return_sap IS NOT NULL; ``` +-- Indexes to optimize performance when posting invoice online topup +```sql +CREATE INDEX idx_posting_et_reference ON public.posting_et(reference); +CREATE INDEX idx_posting_et_tcode_status ON public.posting_et(tcode, pos_status); +``` + ## 5. Cash-in detail anti-join Used by `getSalesKMT()`, `getSalesInternal()`, and `getTopUpCashlessInternal()` to exclude already-posted rows. diff --git a/enhancement payment.sp_payment_req_topup_online.sql b/enhancement payment.sp_payment_req_topup_online.sql index c374e43..1b358c0 100644 --- a/enhancement payment.sp_payment_req_topup_online.sql +++ b/enhancement payment.sp_payment_req_topup_online.sql @@ -45,18 +45,18 @@ BEGIN WHERE tmp.i_partner IN (SELECT i_partner FROM eligible_partners) ), payment_codes AS ( - -- Generate payment codes with sequence numbers using window functions SELECT ep.i_partner, 'INV' || pc.c_code || TO_CHAR(v_trx_date, 'YYMMDD') || - LPAD((COALESCE(MAX(CAST(RIGHT(tpr.c_payment, 4) AS INTEGER)), 0) OVER - (PARTITION BY tpr.i_partner ORDER BY tpr.d_date_payment_request))::TEXT, 4, '0') AS code_payment + LPAD( + COALESCE(MAX(CAST(RIGHT(tpr.c_payment, 4) AS INTEGER)), 0)::TEXT + , 4, '0') AS code_payment FROM eligible_partners ep - CROSS JOIN partner_codes pc + JOIN partner_codes pc + ON pc.i_partner = ep.i_partner LEFT JOIN payment.t_d_payment_request_topup_online tpr ON tpr.i_partner = ep.i_partner AND tpr.d_date_payment_request::DATE = v_trx_date - WHERE pc.i_partner = ep.i_partner GROUP BY ep.i_partner, pc.c_code ), topup_requests AS ( @@ -197,4 +197,9 @@ CREATE INDEX CONCURRENTLY idx_tdprt_date_status ON payment.t_d_payment_request_topup_online(d_date_payment_request, b_sent, b_void); -- Note: --- Expected Performance Gain: 10-100x faster depending on partner volume and data size. \ No newline at end of file +-- Expected Performance Gain: 10-100x faster depending on partner volume and data size. + + +-- Indexes to optimize performance when posting invoice online topup +CREATE INDEX idx_posting_et_reference ON public.posting_et(reference); +CREATE INDEX idx_posting_et_tcode_status ON public.posting_et(tcode, pos_status); \ No newline at end of file