get('userposid'); $userId = session()->get('userid'); $db = \Config\Database::connect(); // Hanya Untuk Manager TSO if ($userPosId == 1 && $userId == 1) { // Eksekusi HANYA 1 QUERY untuk mengambil semua perhitungan $result = $db->table('certificates') ->select(" SUM(CASE WHEN cert_type = 'UTC' THEN 1 ELSE 0 END) as totalUtc, SUM(CASE WHEN cert_type = 'MC' THEN 1 ELSE 0 END) as totalMc, SUM(CASE WHEN cert_type = 'IC' THEN 1 ELSE 0 END) as totalIc ") ->where('certificates.status', 'unvalidated') ->Where('certificates.manager_validation_at', null) ->where('certificates.deleted_at', null) ->get() ->getRow(); // Assign hasil ke property class, pastikan menjadi integer (fallback ke 0 jika tabel kosong) $this->countUtc = (int) ($result->totalUtc ?? 0); $this->countMc = (int) ($result->totalMc ?? 0); $this->countIc = (int) ($result->totalIc ?? 0); // Untuk SPV IVD } else if ($userPosId == 2) { // Eksekusi HANYA 1 QUERY untuk mengambil semua perhitungan $result = $db->table('certificates') ->select(" SUM(CASE WHEN certificates.cert_type = 'UTC' THEN 1 ELSE 0 END) as totalUtc, SUM(CASE WHEN certificates.cert_type = 'MC' THEN 1 ELSE 0 END) as totalMc, SUM(CASE WHEN certificates.cert_type = 'IC' THEN 1 ELSE 0 END) as totalIc ") ->join('users', 'users.userid = certificates.user_id', 'left') ->where('users.reportto', $userId) ->where('certificates.status', 'unvalidated') ->Where('certificates.spv_validation_at', null) ->where('certificates.deleted_at', null) ->get() ->getRow(); // Assign hasil ke property class, pastikan menjadi integer (fallback ke 0 jika tabel kosong) $this->countUtc = (int) ($result->totalUtc ?? 0); $this->countMc = (int) ($result->totalMc ?? 0); $this->countIc = (int) ($result->totalIc ?? 0); // Untuk TSO IVD } else if ($userPosId == 4) { // Eksekusi HANYA 1 QUERY untuk mengambil semua perhitungan $result = $db->table('certificates') ->select(" SUM(CASE WHEN cert_type = 'UTC' THEN 1 ELSE 0 END) as totalUtc, SUM(CASE WHEN cert_type = 'MC' THEN 1 ELSE 0 END) as totalMc, SUM(CASE WHEN cert_type = 'IC' THEN 1 ELSE 0 END) as totalIc ") ->where('user_id', $userId) ->where('user_validation_at', null) ->where('deleted_at', null) ->get() ->getRow(); // Assign hasil ke property class, pastikan menjadi integer (fallback ke 0 jika tabel kosong) $this->countUtc = (int) ($result->totalUtc ?? 0); $this->countMc = (int) ($result->totalMc ?? 0); $this->countIc = (int) ($result->totalIc ?? 0); } // Hitung total keseluruhan di level PHP, bukan di Database $this->countAll = $this->countUtc + $this->countMc + $this->countIc; } }