enhance query get data from posting et

This commit is contained in:
Mohamad Ihsan
2026-04-27 18:55:24 +07:00
parent ce8239ca38
commit fefa11c544
3 changed files with 49 additions and 24 deletions
@@ -60,26 +60,40 @@ const insertPostingEt = async (invoice, trx) => {
const getPostingEt = async(trx) => { const getPostingEt = async(trx) => {
const result = await trx('public.posting_et AS pe').select([ const result = await trx('public.posting_et as pe')
'doc_header_text', .leftJoin('public.posting_et as pe2', 'pe2.reference', 'pe.reference')
'ba', .select([
'doc_date', 'pe.doc_header_text',
'posting_date', 'pe.ba',
'fiscal_year', 'pe.doc_date',
trx.raw(`TO_CHAR(doc_date::DATE, 'MM') AS fis_period`), 'pe.posting_date',
'doc_type', 'pe.fiscal_year',
'reference', trx.raw(`TO_CHAR(pe.doc_date::DATE, 'MM') AS fis_period`),
'tcode', 'pe.doc_type',
'amount', 'pe.reference',
'pe.tcode',
'pe.amount',
trx.raw(`'IDR' AS currency`), 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(`MAX(pe2.customer) FILTER (WHERE pe2.debit_kredit = 'S') AS customer`),
trx.raw(`(select pe2.text from posting_et pe2 where pe2.reference = pe.reference and debit_kredit = 'S' LIMIT 1) as text1`), trx.raw(`MAX(pe2.account) FILTER (WHERE pe2.debit_kredit = 'H') AS account`),
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.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") .where('pe.tcode', 'FB70')
.whereIn('pos_status', ["3"]) .whereIn('pe.pos_status', ['3'])
.groupBy([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12]) .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 return result
+6
View File
@@ -90,6 +90,12 @@ WHERE c_status <> 'X'
AND c_return_sap IS NOT NULL; 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 ## 5. Cash-in detail anti-join
Used by `getSalesKMT()`, `getSalesInternal()`, and `getTopUpCashlessInternal()` to exclude already-posted rows. Used by `getSalesKMT()`, `getSalesInternal()`, and `getTopUpCashlessInternal()` to exclude already-posted rows.
@@ -45,18 +45,18 @@ BEGIN
WHERE tmp.i_partner IN (SELECT i_partner FROM eligible_partners) WHERE tmp.i_partner IN (SELECT i_partner FROM eligible_partners)
), ),
payment_codes AS ( payment_codes AS (
-- Generate payment codes with sequence numbers using window functions
SELECT SELECT
ep.i_partner, ep.i_partner,
'INV' || pc.c_code || TO_CHAR(v_trx_date, 'YYMMDD') || 'INV' || pc.c_code || TO_CHAR(v_trx_date, 'YYMMDD') ||
LPAD((COALESCE(MAX(CAST(RIGHT(tpr.c_payment, 4) AS INTEGER)), 0) OVER LPAD(
(PARTITION BY tpr.i_partner ORDER BY tpr.d_date_payment_request))::TEXT, 4, '0') AS code_payment COALESCE(MAX(CAST(RIGHT(tpr.c_payment, 4) AS INTEGER)), 0)::TEXT
, 4, '0') AS code_payment
FROM eligible_partners ep 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 LEFT JOIN payment.t_d_payment_request_topup_online tpr
ON tpr.i_partner = ep.i_partner ON tpr.i_partner = ep.i_partner
AND tpr.d_date_payment_request::DATE = v_trx_date 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 GROUP BY ep.i_partner, pc.c_code
), ),
topup_requests AS ( 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); ON payment.t_d_payment_request_topup_online(d_date_payment_request, b_sent, b_void);
-- Note: -- Note:
-- Expected Performance Gain: 10-100x faster depending on partner volume and data size. -- 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);