QUERY PLAN
Aggregate (cost=566.70..566.71 rows=1 width=32)
-> Hash Left Join (cost=486.02..559.37 rows=977 width=96)
Hash Cond: (t.id = settle.sales_contract_id)
Filter: (((sales.sales_contract_id IS NOT NULL) OR (settle.sales_contract_id IS NOT NULL)) AND ((((COALESCE(sales.amount, '0'::numeric) - COALESCE(return.amount, '0'::numeric)) - COALESCE(settle.amount, '0'::numeric)) >= '0'::numeric) OR ord.is_done))
-> Hash Left Join (cost=272.88..341.67 rows=1732 width=73)
Hash Cond: (t.id = return.sales_contract_id)
-> Hash Left Join (cost=184.69..248.92 rows=1732 width=41)
Hash Cond: (t.id = sales.sales_contract_id)
-> Hash Right Join (cost=89.97..149.65 rows=1732 width=5)
Hash Cond: (ord.sales_contract_id = t.id)
-> Seq Scan on tbl_erp_sales_orders ord (cost=0.00..55.16 rows=1716 width=5)
-> Hash (cost=68.32..68.32 rows=1732 width=4)
-> Seq Scan on tbl_sales_contracts t (cost=0.00..68.32 rows=1732 width=4)
-> Hash (cost=92.94..92.94 rows=142 width=36)
-> Subquery Scan on sales (cost=89.75..92.94 rows=142 width=36)
-> HashAggregate (cost=89.75..91.52 rows=142 width=36)
Group Key: sc.id
-> Hash Right Join (cost=8.47..89.03 rows=142 width=10)
Hash Cond: (sc.from_project = t_1.project_id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..68.32 rows=1732 width=8)
-> Hash (cost=6.70..6.70 rows=142 width=10)
-> Seq Scan on tbl_erp_sales_outbounds t_1 (cost=0.00..6.70 rows=142 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=88.14..88.14 rows=4 width=36)
-> Subquery Scan on return (cost=88.02..88.14 rows=4 width=36)
-> GroupAggregate (cost=88.02..88.10 rows=4 width=36)
Group Key: sc_1.id
-> Sort (cost=88.02..88.03 rows=4 width=10)
Sort Key: sc_1.id
-> Hash Right Join (cost=13.13..87.98 rows=4 width=10)
Hash Cond: (sc_1.from_project = pi.project_id)
-> Seq Scan on tbl_sales_contracts sc_1 (cost=0.00..68.32 rows=1732 width=8)
-> Hash (cost=13.08..13.08 rows=4 width=10)
-> Nested Loop Left Join (cost=2.44..13.08 rows=4 width=10)
-> Hash Right Join (cost=2.29..12.20 rows=4 width=10)
Hash Cond: (esips.id = esrps.erp_sales_outbound_spent_product_id)
-> Seq Scan on tbl_erp_sales_outbound_spent_products esips (cost=0.00..8.27 rows=427 width=8)
-> Hash (cost=2.24..2.24 rows=4 width=10)
-> Hash Join (cost=1.16..2.24 rows=4 width=10)
Hash Cond: (esrps.erp_sales_return_id = t_2.id)
-> Seq Scan on tbl_erp_sales_return_products esrps (cost=0.00..1.06 rows=6 width=14)
-> Hash (cost=1.10..1.10 rows=4 width=4)
-> Seq Scan on tbl_erp_sales_returns t_2 (cost=0.00..1.10 rows=4 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))
-> Index Scan using tbl_erp_sales_outbounds_pkey on tbl_erp_sales_outbounds pi (cost=0.14..0.22 rows=1 width=8)
Index Cond: (id = esips.erp_sales_outbound_id)
-> Hash (cost=212.89..212.89 rows=20 width=36)
-> Subquery Scan on settle (cost=212.29..212.89 rows=20 width=36)
-> GroupAggregate (cost=212.29..212.69 rows=20 width=36)
Group Key: "*SELECT* 1".contract_id
-> Sort (cost=212.29..212.34 rows=20 width=11)
Sort Key: "*SELECT* 1".contract_id
-> Append (cost=74.66..211.86 rows=20 width=11)
-> Subquery Scan on "*SELECT* 1" (cost=74.66..116.46 rows=18 width=10)
-> Hash Join (cost=74.66..116.28 rows=18 width=106)
Hash Cond: (r.erp_collect_ticket_id = t_3.id)
-> Seq Scan on tbl_erp_collect_ticket_settles r (cost=0.00..35.94 rows=2160 width=14)
Filter: (sales_contract_id IS NOT NULL)
-> Hash (cost=74.43..74.43 rows=18 width=4)
-> Seq Scan on tbl_erp_collect_tickets t_3 (cost=0.00..74.43 rows=18 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))
-> Subquery Scan on "*SELECT* 2" (cost=0.29..9.45 rows=1 width=9)
-> Nested Loop (cost=0.29..9.44 rows=1 width=105)
-> Seq Scan on tbl_erp_cash_cost_ticket_sales_contract_settles r_1 (cost=0.00..1.02 rows=1 width=13)
Filter: (sales_contract_id IS NOT NULL)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_4 (cost=0.29..8.31 rows=1 width=4)
Index Cond: (id = r_1.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* 3" (cost=0.28..85.95 rows=1 width=36)
-> Nested Loop (cost=0.28..85.94 rows=1 width=132)
-> Seq Scan on tbl_erp_payment_ticket_settles r_2 (cost=0.00..39.65 rows=6 width=14)
Filter: (sales_contract_id IS NOT NULL)
-> Index Scan using tbl_erp_payment_tickets_pkey on tbl_erp_payment_tickets t_5 (cost=0.28..7.64 rows=1 width=4)
Index Cond: (id = r_2.erp_payment_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))