QUERY PLAN
Aggregate (cost=864.04..864.05 rows=1 width=32)
-> Hash Left Join (cost=726.03..848.52 rows=2069 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=178.98..295.62 rows=2220 width=73)
Hash Cond: (t.id = return.purchase_contract_id)
-> Hash Left Join (cost=168.35..279.16 rows=2220 width=41)
Hash Cond: (t.id = purchase.purchase_contract_id)
-> Hash Right Join (cost=151.15..256.11 rows=2220 width=5)
Hash Cond: (ord.purchase_contract_id = t.id)
-> Seq Scan on tbl_erp_purchase_orders ord (cost=0.00..99.14 rows=2214 width=5)
-> Hash (cost=123.40..123.40 rows=2220 width=4)
-> Seq Scan on tbl_purchase_contracts t (cost=0.00..123.40 rows=2220 width=4)
Filter: (cost_type = 1)
-> Hash (cost=14.88..14.88 rows=186 width=36)
-> Subquery Scan on purchase (cost=10.70..14.88 rows=186 width=36)
-> HashAggregate (cost=10.70..13.02 rows=186 width=36)
Group Key: t_1.purchase_contract_id
-> Seq Scan on tbl_erp_purchase_inbounds t_1 (cost=0.00..9.71 rows=197 width=10)
Filter: ((created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (verify_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=10.61..10.61 rows=1 width=36)
-> Subquery Scan on return (cost=10.58..10.61 rows=1 width=36)
-> GroupAggregate (cost=10.58..10.60 rows=1 width=36)
Group Key: pi.purchase_contract_id
-> Sort (cost=10.58..10.58 rows=1 width=36)
Sort Key: pi.purchase_contract_id
-> Nested Loop (cost=0.42..10.57 rows=1 width=36)
Join Filter: (esrps.erp_purchase_return_id = t_2.id)
-> Nested Loop Left Join (cost=0.42..9.54 rows=1 width=40)
-> Seq Scan on tbl_erp_purchase_return_products esrps (cost=0.00..1.01 rows=1 width=40)
-> Nested Loop Left Join (cost=0.42..8.52 rows=1 width=8)
-> Index Scan using tbl_erp_purchase_inbound_products_pkey on tbl_erp_purchase_inbound_products esips (cost=0.27..8.29 rows=1 width=8)
Index Cond: (id = esrps.erp_purchase_inbound_product_id)
-> Index Scan using tbl_erp_purchase_inbounds_pkey on tbl_erp_purchase_inbounds pi (cost=0.14..0.23 rows=1 width=8)
Index Cond: (id = esips.erp_purchase_inbound_id)
-> Seq Scan on tbl_erp_purchase_returns t_2 (cost=0.00..1.02 rows=1 width=4)
Filter: ((created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (verify_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=546.54..546.54 rows=41 width=36)
-> Subquery Scan on settle (cost=545.31..546.54 rows=41 width=36)
-> GroupAggregate (cost=545.31..546.13 rows=41 width=36)
Group Key: "*SELECT* 1".contract_id
-> Sort (cost=545.31..545.41 rows=41 width=16)
Sort Key: "*SELECT* 1".contract_id
-> Append (cost=115.53..544.21 rows=41 width=16)
-> Subquery Scan on "*SELECT* 1" (cost=115.53..163.26 rows=32 width=10)
-> Hash Join (cost=115.53..162.94 rows=32 width=106)
Hash Cond: (r.erp_payment_ticket_id = t_3.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..41.01 rows=2435 width=14)
Filter: (purchase_contract_id IS NOT NULL)
-> Hash (cost=115.14..115.14 rows=31 width=4)
-> Seq Scan on tbl_erp_payment_tickets t_3 (cost=0.00..115.14 rows=31 width=4)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 2" (cost=1.18..4.15 rows=7 width=36)
-> Hash Join (cost=1.18..4.08 rows=7 width=132)
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..2.70 rows=35 width=4)
Filter: ((created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.08..1.08 rows=8 width=40)
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..1.08 rows=8 width=40)
Filter: (purchase_contract_id IS NOT NULL)
-> Subquery Scan on "*SELECT* 3" (cost=0.29..323.50 rows=1 width=36)
-> Nested Loop (cost=0.29..323.49 rows=1 width=132)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..1.45 rows=45 width=40)
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..7.15 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 (bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 4" (cost=0.28..53.30 rows=1 width=36)
-> Nested Loop (cost=0.28..53.29 rows=1 width=132)
-> Seq Scan on tbl_erp_collect_ticket_settles r_3 (cost=0.00..35.83 rows=2 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 (bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))