| 13:19:45.717 | 21.1 ms | SELECT | SELECT "sub"."id", "sub"."erp_sales_amount", "sub"."amount" FROM (SELECT
u.id,
COALESCE(sum(sub_in.total_amount), 0) + COALESCE(sum(start.budget), 0) as erp_sales_amount,
COALESCE(sum(sub_in.total_amount), 0) + COALESCE(sum(start.budget), 0) + COALESCE(sum(sub_other_in.amount), 0) - COALESCE(sum(sub_in.actual_total_amount), 0) - COALESCE(sum(start.cost), 0) - COALESCE(sum(sub_out.amount), 0) - COALESCE(sum(start.expense), 0) as amount
FROM "a57be577deb434"."tbl_users" "u" LEFT JOIN (SELECT "user_id", sum(actual_total_amount) as actual_total_amount, sum(total_amount) as total_amount FROM ((SELECT t.handler as user_id, sum(eipss.actual_total_amount) as actual_total_amount, sum(eipss.total_amount) as total_amount FROM "a57be577deb434"."tbl_erp_sales_outbounds" "t" LEFT JOIN "a57be577deb434"."tbl_erp_inventory_product_spents" "eipss" ON eipss.erp_sales_outbound_id = t.id WHERE ("t"."status"=2) AND (("t"."project_id" NOT IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) OR (t.project_id is null)) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("t"."handler" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) GROUP BY "t"."handler") UNION ALL ( SELECT am.user_id, sum(eipss.actual_total_amount * am.pct / 100) as actual_total_amount, sum(eipss.total_amount * am.pct / 100) as total_amount FROM "a57be577deb434"."tbl_erp_profit_distribute_pcts" "am" LEFT JOIN "a57be577deb434"."tbl_erp_profit_distributes" "pd" ON am.erp_profit_distribute_id = pd.id LEFT JOIN "a57be577deb434"."tbl_erp_sales_outbounds" "t" ON pd.project_id = t.project_id LEFT JOIN "a57be577deb434"."tbl_erp_inventory_product_spents" "eipss" ON eipss.erp_sales_outbound_id = t.id WHERE ("t"."status"=2) AND ("t"."project_id" IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) GROUP BY "am"."user_id" )) "sub" GROUP BY "user_id") "sub_in" ON sub_in.user_id = u.id LEFT JOIN (SELECT "user_id", sum(amount) as amount FROM ((SELECT am.user_id, sum(am.amount) as amount FROM "a57be577deb434"."tbl_erp_other_income_ticket_handlers" "am" LEFT JOIN "a57be577deb434"."tbl_erp_other_income_tickets" "t" ON am.erp_other_income_ticket_id = t.id LEFT JOIN "a57be577deb434"."tbl_sales_contracts" "sc" ON sc.id = t.sales_contract_id WHERE ("t"."status"=2) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) AND (("sc"."from_project" NOT IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) OR (sales_contract_id is null)) AND (("sc"."from_project" NOT IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) OR (sales_contract_id is null)) GROUP BY "am"."user_id") UNION ALL ( SELECT am.user_id, sum(t.amount * am.pct / 100) as amount FROM "a57be577deb434"."tbl_erp_profit_distribute_pcts" "am" LEFT JOIN "a57be577deb434"."tbl_erp_profit_distributes" "pd" ON am.erp_profit_distribute_id = pd.id LEFT JOIN "a57be577deb434"."tbl_sales_contracts" "sc" ON sc.from_project = pd.project_id LEFT JOIN "a57be577deb434"."tbl_erp_other_income_tickets" "t" ON sc.id = t.sales_contract_id WHERE ("t"."status"=2) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) AND ("pd"."project_id" IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) GROUP BY "am"."user_id" ) UNION ALL ( SELECT t.handler as user_id, sum(t.amount) as amount FROM "a57be577deb434"."tbl_erp_inventory_overflows" "t" WHERE ("t"."status"=2) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("t"."handler" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) GROUP BY "t"."handler" )) "sub" GROUP BY "user_id") "sub_other_in" ON sub_other_in.user_id = u.id LEFT JOIN (SELECT "user_id", sum(amount) as amount FROM ((SELECT am.user_id, sum(am.amount) as amount FROM "a57be577deb434"."tbl_erp_cash_cost_ticket_handlers" "am" LEFT JOIN "a57be577deb434"."tbl_erp_cash_cost_tickets" "t" ON am.erp_cash_cost_ticket_id = t.id LEFT JOIN "a57be577deb434"."tbl_sales_contracts" "sc" ON sc.id = t.sales_contract_id WHERE ("t"."status"=2) AND ("t"."is_payment_settle"=TRUE) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("t"."created_at" >= '2025-08-08 14:34:58') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) AND (("sc"."from_project" NOT IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) OR (sales_contract_id is null)) GROUP BY "am"."user_id") UNION ALL ( SELECT am.user_id, sum(t.amount * am.pct / 100) as amount FROM "a57be577deb434"."tbl_erp_profit_distribute_pcts" "am" LEFT JOIN "a57be577deb434"."tbl_erp_profit_distributes" "pd" ON am.erp_profit_distribute_id = pd.id LEFT JOIN "a57be577deb434"."tbl_sales_contracts" "sc" ON sc.from_project = pd.project_id LEFT JOIN "a57be577deb434"."tbl_erp_cash_cost_tickets" "t" ON sc.id = t.sales_contract_id WHERE ("t"."status"=2) AND ("t"."is_payment_settle"=TRUE) AND ("t"."created_at" >= '2025-08-08 14:34:58') AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) AND ("pd"."project_id" IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) GROUP BY "am"."user_id" ) UNION ALL ( SELECT am.user_id, sum(am.amount) as amount FROM "a57be577deb434"."tbl_erp_cost_ticket_handlers" "am" LEFT JOIN "a57be577deb434"."tbl_erp_cost_tickets" "t" ON am.erp_cost_ticket_id = t.id WHERE ("t"."status"=2) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) AND (("t"."project_id" NOT IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) OR (t.project_id is null)) GROUP BY "am"."user_id" ) UNION ALL ( SELECT am.user_id, sum(t.amount * am.pct / 100) as amount FROM "a57be577deb434"."tbl_erp_profit_distribute_pcts" "am" LEFT JOIN "a57be577deb434"."tbl_erp_profit_distributes" "pd" ON am.erp_profit_distribute_id = pd.id LEFT JOIN "a57be577deb434"."tbl_erp_cost_tickets" "t" ON pd.project_id = t.project_id WHERE ("t"."status"=2) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) AND ("pd"."project_id" IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) GROUP BY "am"."user_id" ) UNION ALL ( SELECT am.user_id, sum(am.amount) as amount FROM "a57be577deb434"."tbl_erp_cost_convert_ticket_handlers" "am" LEFT JOIN "a57be577deb434"."tbl_erp_cost_convert_tickets" "t" ON am.erp_cost_convert_ticket_id = t.id LEFT JOIN "a57be577deb434"."tbl_sales_contracts" "sc" ON sc.id = t.sales_contract_id WHERE ("t"."status"=2) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) AND (("sc"."from_project" NOT IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) OR (sales_contract_id is null)) GROUP BY "am"."user_id" ) UNION ALL ( SELECT am.user_id, sum(t.amount * am.pct / 100) as amount FROM "a57be577deb434"."tbl_erp_profit_distribute_pcts" "am" LEFT JOIN "a57be577deb434"."tbl_erp_profit_distributes" "pd" ON am.erp_profit_distribute_id = pd.id LEFT JOIN "a57be577deb434"."tbl_sales_contracts" "sc" ON sc.from_project = pd.project_id LEFT JOIN "a57be577deb434"."tbl_erp_cost_convert_tickets" "t" ON sc.id = t.sales_contract_id WHERE ("t"."status"=2) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) AND ("pd"."project_id" IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) GROUP BY "am"."user_id" ) UNION ALL ( SELECT t.handler as user_id, sum(eipss.actual_total_amount - eipss.total_amount) as amount FROM "a57be577deb434"."tbl_erp_sales_gifts" "t" LEFT JOIN "a57be577deb434"."tbl_erp_inventory_product_spents" "eipss" ON eipss.erp_sales_gift_id = t.id WHERE ("t"."status"=2) AND (("t"."project_id" NOT IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) OR (t.project_id is null)) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("t"."handler" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) GROUP BY "t"."handler" ) UNION ALL ( SELECT am.user_id, sum((eipss.actual_total_amount - eipss.total_amount) * am.pct / 100) as amount FROM "a57be577deb434"."tbl_erp_profit_distribute_pcts" "am" LEFT JOIN "a57be577deb434"."tbl_erp_profit_distributes" "pd" ON am.erp_profit_distribute_id = pd.id LEFT JOIN "a57be577deb434"."tbl_erp_sales_gifts" "t" ON pd.project_id = t.project_id LEFT JOIN "a57be577deb434"."tbl_erp_inventory_product_spents" "eipss" ON eipss.erp_sales_gift_id = t.id WHERE ("t"."status"=2) AND ("t"."project_id" IN (SELECT t.project_id FROM "a57be577deb434"."tbl_erp_profit_distributes" "t" WHERE ("t"."status"=2) AND (t.project_id IS NOT NULL) GROUP BY "t"."project_id")) AND ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59') AND ("am"."user_id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449)) GROUP BY "am"."user_id" )) "sub" GROUP BY "user_id") "sub_out" ON sub_out.user_id = u.id LEFT JOIN (SELECT user_id, budget, cost, expense, profit FROM "a57be577deb434"."tbl_erp_user_profits" "t" WHERE ("t"."created_at" >= '2026-01-01 00:00:00') AND ("t"."created_at" <= '2026-12-31 23:59:59')) "start" ON start.user_id = u.id WHERE "u"."id" IN (5332, 1001, 5087, 5467, 5300, 5311, 5295, 1013, 5476, 1004, 1005, 5449) GROUP BY "u"."id") "sub" |