QUERY PLAN
Aggregate (cost=745.95..745.96 rows=1 width=32)
-> Hash Left Join (cost=644.82..730.44 rows=2068 width=96)
Hash Cond: (t.id = settle.purchase_contract_id)
Filter: (((purchase.purchase_contract_id IS NOT NULL) OR (settle.purchase_contract_id IS NOT NULL)) AND ((((COALESCE(purchase.amount, '0'::numeric) - COALESCE(return.amount, '0'::numeric)) - COALESCE(settle.amount, '0'::numeric)) >= '0'::numeric) OR ord.is_done))
-> Hash Left Join (cost=136.30..216.06 rows=2224 width=73)
Hash Cond: (t.id = return.purchase_contract_id)
-> Hash Left Join (cost=120.06..193.98 rows=2224 width=41)
Hash Cond: (t.id = purchase.purchase_contract_id)
-> Hash Right Join (cost=104.25..172.31 rows=2224 width=5)
Hash Cond: (ord.purchase_contract_id = t.id)
-> Seq Scan on tbl_erp_purchase_orders ord (cost=0.00..62.22 rows=2222 width=5)
-> Hash (cost=76.45..76.45 rows=2224 width=4)
-> Seq Scan on tbl_purchase_contracts t (cost=0.00..76.45 rows=2224 width=4)
Filter: (cost_type = 1)
-> Hash (cost=13.45..13.45 rows=189 width=36)
-> Subquery Scan on purchase (cost=9.20..13.45 rows=189 width=36)
-> HashAggregate (cost=9.20..11.56 rows=189 width=36)
Group Key: t_1.purchase_contract_id
-> Seq Scan on tbl_erp_purchase_inbounds t_1 (cost=0.00..8.20 rows=201 width=10)
Filter: ((created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=16.20..16.20 rows=3 width=36)
-> Subquery Scan on return (cost=16.11..16.20 rows=3 width=36)
-> GroupAggregate (cost=16.11..16.17 rows=3 width=36)
Group Key: pi.purchase_contract_id
-> Sort (cost=16.11..16.11 rows=3 width=9)
Sort Key: pi.purchase_contract_id
-> Nested Loop Left Join (cost=2.40..16.08 rows=3 width=9)
-> Hash Right Join (cost=2.25..15.43 rows=3 width=9)
Hash Cond: (esips.id = esrps.erp_purchase_inbound_product_id)
-> Seq Scan on tbl_erp_purchase_inbound_products esips (cost=0.00..11.20 rows=520 width=8)
-> Hash (cost=2.21..2.21 rows=3 width=9)
-> Hash Join (cost=1.13..2.21 rows=3 width=9)
Hash Cond: (esrps.erp_purchase_return_id = t_2.id)
-> Seq Scan on tbl_erp_purchase_return_products esrps (cost=0.00..1.06 rows=6 width=13)
-> Hash (cost=1.09..1.09 rows=3 width=4)
-> Seq Scan on tbl_erp_purchase_returns t_2 (cost=0.00..1.09 rows=3 width=4)
Filter: ((created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (status = 2))
-> Index Scan using tbl_erp_purchase_inbounds_pkey on tbl_erp_purchase_inbounds pi (cost=0.14..0.22 rows=1 width=8)
Index Cond: (id = esips.erp_purchase_inbound_id)
-> Hash (cost=506.03..506.03 rows=200 width=36)
-> Subquery Scan on settle (cost=501.53..506.03 rows=200 width=36)
-> HashAggregate (cost=501.53..504.03 rows=200 width=36)
Group Key: "*SELECT* 1".contract_id
-> Append (cost=77.85..500.10 rows=286 width=10)
-> Subquery Scan on "*SELECT* 1" (cost=77.85..126.29 rows=276 width=10)
-> Hash Join (cost=77.85..123.53 rows=276 width=106)
Hash Cond: (r.erp_payment_ticket_id = t_3.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..39.65 rows=2295 width=14)
Filter: (purchase_contract_id IS NOT NULL)
-> Hash (cost=74.31..74.31 rows=283 width=4)
-> Seq Scan on tbl_erp_payment_tickets t_3 (cost=0.00..74.31 rows=283 width=4)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 2" (cost=1.14..2.96 rows=2 width=10)
-> Hash Join (cost=1.14..2.94 rows=2 width=106)
Hash Cond: (t_4.id = r_1.erp_other_income_ticket_id)
-> Seq Scan on tbl_erp_other_income_tickets t_4 (cost=0.00..1.60 rows=36 width=4)
Filter: ((created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.11..1.11 rows=2 width=14)
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..1.11 rows=2 width=14)
Filter: (purchase_contract_id IS NOT NULL)
-> Subquery Scan on "*SELECT* 3" (cost=0.29..309.91 rows=7 width=9)
-> Nested Loop (cost=0.29..309.84 rows=7 width=105)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..1.53 rows=53 width=13)
Filter: (purchase_contract_id IS NOT NULL)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_5 (cost=0.29..5.82 rows=1 width=4)
Index Cond: (id = r_2.erp_cash_cost_ticket_id)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 4" (cost=0.28..60.94 rows=1 width=36)
-> Nested Loop (cost=0.28..60.93 rows=1 width=132)
-> Seq Scan on tbl_erp_collect_ticket_settles r_3 (cost=0.00..35.94 rows=3 width=14)
Filter: (purchase_contract_id IS NOT NULL)
-> Index Scan using tbl_erp_collect_tickets_pkey on tbl_erp_collect_tickets t_6 (cost=0.28..8.30 rows=1 width=4)
Index Cond: (id = r_3.erp_collect_ticket_id)
Filter: ((created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (status = 2))