QUERY PLAN
Aggregate (cost=522.28..522.29 rows=1 width=8)
-> Hash Left Join (cost=26.32..522.28 rows=1 width=0)
Hash Cond: ((b.object_type = g.object_type) AND (b.object_id = g.object_id) AND (b.event_type = g.event_type) AND (b.event_id = g.event_id))
Filter: ((g.success_count IS NULL) OR (g.success_count = 0))
-> Subquery Scan on b (cost=21.59..517.30 rows=24 width=16)
Filter: ((b.object_type <> ALL ('{20,20018}'::integer[])) OR b.have_invoice OR (SubPlan 1))
-> HashAggregate (cost=21.59..21.89 rows=24 width=149)
Group Key: e.id
Filter: (CASE WHEN (COALESCE(e.amount, '0'::numeric) > '0'::numeric) THEN COALESCE(e.amount, '0'::numeric) ELSE COALESCE(sum(s.actual_total_amount), '0'::numeric) END > '0'::numeric)
-> Hash Right Join (cost=7.00..21.15 rows=88 width=52)
Hash Cond: (s.erp_sales_outbound_id = e.id)
-> Seq Scan on tbl_erp_inventory_product_spents s (cost=0.00..12.65 rows=565 width=9)
-> Hash (cost=6.70..6.70 rows=24 width=47)
-> Seq Scan on tbl_erp_sales_outbounds e (cost=0.00..6.70 rows=24 width=47)
Filter: ((wrote_date >= '2026-02-01'::date) AND (wrote_date <= '2026-06-30'::date) AND (status = 2))
SubPlan 1
-> Nested Loop (cost=0.29..20.63 rows=1 width=0)
Join Filter: (r.taxonomy_id = tt.id)
-> Index Scan using tbl_term_taxonomy_relationships_object_id_term_id_idx on tbl_term_taxonomy_relationships r (cost=0.29..11.58 rows=1 width=4)
Index Cond: ((object_id = b.object_id) AND (term_id = 18))
Filter: (((b.object_type = 20) AND (object_type = 7)) OR ((b.object_type = 20018) AND (object_type = 11)))
-> Seq Scan on tbl_term_taxonomies tt (cost=0.00..8.99 rows=5 width=4)
Filter: ((serial_no)::text = ANY ('{221106,640301,640302,640303,600704}'::text[]))
-> Hash (cost=4.21..4.21 rows=26 width=24)
-> Subquery Scan on g (cost=3.62..4.21 rows=26 width=24)
-> HashAggregate (cost=3.62..3.94 rows=26 width=36)
Group Key: v.object_type, v.object_id, COALESCE((v.event_type)::integer, 0), COALESCE(v.event_id, 0)
-> Seq Scan on tbl_voucher_gen_items v (cost=0.00..2.81 rows=54 width=18)
Filter: (ledger_id = 1)