QUERY PLAN
Aggregate (cost=688.63..688.64 rows=1 width=32)
-> Hash Left Join (cost=599.17..688.05 rows=76 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))
-> Hash Left Join (cost=95.49..183.77 rows=229 width=72)
Hash Cond: (t.id = return.purchase_contract_id)
-> Hash Left Join (cost=81.41..169.09 rows=229 width=40)
Hash Cond: (t.id = purchase.purchase_contract_id)
-> Hash Join (cost=65.14..152.23 rows=229 width=4)
Hash Cond: (t.id = ord.purchase_contract_id)
-> Seq Scan on tbl_purchase_contracts t (cost=0.00..76.45 rows=2224 width=4)
Filter: (cost_type = 1)
-> Hash (cost=62.22..62.22 rows=234 width=4)
-> Seq Scan on tbl_erp_purchase_orders ord (cost=0.00..62.22 rows=234 width=4)
Filter: (NOT is_done)
-> Hash (cost=13.93..13.93 rows=187 width=36)
-> Subquery Scan on purchase (cost=9.72..13.93 rows=187 width=36)
-> HashAggregate (cost=9.72..12.05 rows=187 width=36)
Group Key: t_1.purchase_contract_id
-> Seq Scan on tbl_erp_purchase_inbounds t_1 (cost=0.00..8.73 rows=198 width=10)
Filter: ((verify_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=14.05..14.05 rows=3 width=36)
-> Subquery Scan on return (cost=13.96..14.05 rows=3 width=36)
-> GroupAggregate (cost=13.96..14.02 rows=3 width=36)
Group Key: pi.purchase_contract_id
-> Sort (cost=13.96..13.96 rows=3 width=9)
Sort Key: pi.purchase_contract_id
-> Nested Loop Left Join (cost=2.41..13.93 rows=3 width=9)
-> Hash Right Join (cost=2.27..13.25 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..9.33 rows=433 width=8)
-> Hash (cost=2.23..2.23 rows=3 width=9)
-> Hash Join (cost=1.14..2.23 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.10..1.10 rows=3 width=4)
-> Seq Scan on tbl_erp_purchase_returns t_2 (cost=0.00..1.10 rows=3 width=4)
Filter: ((verify_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (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.23 rows=1 width=8)
Index Cond: (id = esips.erp_purchase_inbound_id)
-> Hash (cost=503.25..503.25 rows=34 width=36)
-> Subquery Scan on settle (cost=502.23..503.25 rows=34 width=36)
-> GroupAggregate (cost=502.23..502.91 rows=34 width=36)
Group Key: "*SELECT* 1".contract_id
-> Sort (cost=502.23..502.32 rows=34 width=11)
Sort Key: "*SELECT* 1".contract_id
-> Append (cost=80.58..501.37 rows=34 width=11)
-> Subquery Scan on "*SELECT* 1" (cost=80.58..126.57 rows=30 width=10)
-> Hash Join (cost=80.58..126.27 rows=30 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=80.20..80.20 rows=31 width=4)
-> Seq Scan on tbl_erp_payment_tickets t_3 (cost=0.00..80.20 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.14..3.05 rows=2 width=10)
-> Hash Join (cost=1.14..3.03 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.70 rows=34 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.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..310.04 rows=1 width=9)
-> Nested Loop (cost=0.29..310.03 rows=1 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 (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..61.71 rows=1 width=36)
-> Nested Loop (cost=0.28..61.70 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 (bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))