CompanySummaryModel.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Models;
  3. class CompanySummaryModel
  4. {
  5. private \PDO $pdo;
  6. public function __construct()
  7. {
  8. if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof \PDO) {
  9. $this->pdo = $GLOBALS['pdo'];
  10. return;
  11. }
  12. throw new \RuntimeException('Global PDO connection not initialized');
  13. }
  14. public function getSummary(int $companyId): array
  15. {
  16. return [
  17. 'total_tokens' => $this->countCompanyTokens($companyId),
  18. 'active_operations' => $this->countActiveOperations($companyId),
  19. 'total_cprs' => $this->countCompanyCprs($companyId),
  20. 'total_users' => $this->countCompanyUsers($companyId),
  21. ];
  22. }
  23. private function countCompanyTokens(int $companyId): int
  24. {
  25. $stmt = $this->pdo->prepare(
  26. 'SELECT COUNT(*)
  27. FROM "token" t
  28. INNER JOIN "wallet" w ON w.wallet_id = t.wallet_id
  29. WHERE w.company_id = :company_id'
  30. );
  31. $stmt->execute(['company_id' => $companyId]);
  32. return (int)$stmt->fetchColumn();
  33. }
  34. private function countActiveOperations(int $companyId): int
  35. {
  36. $stmt = $this->pdo->prepare(
  37. 'SELECT COUNT(*)
  38. FROM "orderbook" o
  39. INNER JOIN "wallet" w ON w.wallet_id = o.wallet_id
  40. WHERE w.company_id = :company_id
  41. AND o.status_id = :status_open'
  42. );
  43. $stmt->execute([
  44. 'company_id' => $companyId,
  45. 'status_open' => OrderbookModel::STATUS_OPEN,
  46. ]);
  47. return (int)$stmt->fetchColumn();
  48. }
  49. private function countCompanyCprs(int $companyId): int
  50. {
  51. $stmt = $this->pdo->prepare(
  52. 'SELECT COUNT(*) FROM "cpr" WHERE company_id = :company_id'
  53. );
  54. $stmt->execute(['company_id' => $companyId]);
  55. return (int)$stmt->fetchColumn();
  56. }
  57. private function countCompanyUsers(int $companyId): int
  58. {
  59. $stmt = $this->pdo->prepare(
  60. 'SELECT COUNT(*) FROM "user" WHERE company_id = :company_id'
  61. );
  62. $stmt->execute(['company_id' => $companyId]);
  63. return (int)$stmt->fetchColumn();
  64. }
  65. }