QUERY PLAN
Aggregate (cost=861.15..861.16 rows=1 width=32)
-> Hash Left Join (cost=723.14..845.63 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=176.86..293.50 rows=2220 width=73)
Hash Cond: (t.id = return.purchase_contract_id)
-> Hash Left Join (cost=166.24..277.04 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=13.42..13.42 rows=134 width=36)
-> Subquery Scan on purchase (cost=10.40..13.42 rows=134 width=36)
-> HashAggregate (cost=10.40..12.08 rows=134 width=36)
Group Key: t_1.purchase_contract_id
-> Seq Scan on tbl_erp_purchase_inbounds t_1 (cost=0.00..9.71 rows=138 width=10)
Filter: ((created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (verify_at <= '2025-12-31 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 <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=545.90..545.90 rows=30 width=36)
-> Subquery Scan on settle (cost=545.00..545.90 rows=30 width=36)
-> GroupAggregate (cost=545.00..545.60 rows=30 width=36)
Group Key: "*SELECT* 1".contract_id
-> Sort (cost=545.00..545.08 rows=30 width=16)
Sort Key: "*SELECT* 1".contract_id
-> Append (cost=115.42..544.27 rows=30 width=16)
-> Subquery Scan on "*SELECT* 1" (cost=115.42..163.06 rows=23 width=10)
-> Hash Join (cost=115.42..162.83 rows=23 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=22 width=4)
-> Seq Scan on tbl_erp_payment_tickets t_3 (cost=0.00..115.14 rows=22 width=4)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (bank_settle_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 2" (cost=1.18..4.07 rows=5 width=36)
-> Hash Join (cost=1.18..4.02 rows=5 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=25 width=4)
Filter: ((created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (bank_settle_at <= '2025-12-31 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.53 rows=1 width=36)
-> Nested Loop (cost=0.29..323.52 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 <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 4" (cost=0.28..53.60 rows=1 width=36)
-> Nested Loop (cost=0.28..53.59 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 <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))