App\Ninja\Repositories\DashboardRepository::totals PHP Method

totals() public method

public totals ( $accountId, $userId, $viewAll )
    public function totals($accountId, $userId, $viewAll)
    {
        // total_income, billed_clients, invoice_sent and active_clients
        $select = DB::raw('COUNT(DISTINCT CASE WHEN ' . DB::getQueryGrammar()->wrap('invoices.id', true) . ' IS NOT NULL THEN ' . DB::getQueryGrammar()->wrap('clients.id', true) . ' ELSE null END) billed_clients,
            SUM(CASE WHEN ' . DB::getQueryGrammar()->wrap('invoices.invoice_status_id', true) . ' >= ' . INVOICE_STATUS_SENT . ' THEN 1 ELSE 0 END) invoices_sent,
            COUNT(DISTINCT ' . DB::getQueryGrammar()->wrap('clients.id', true) . ') active_clients');
        $metrics = DB::table('accounts')->select($select)->leftJoin('clients', 'accounts.id', '=', 'clients.account_id')->leftJoin('invoices', 'clients.id', '=', 'invoices.client_id')->where('accounts.id', '=', $accountId)->where('clients.is_deleted', '=', false)->where('invoices.is_deleted', '=', false)->where('invoices.is_recurring', '=', false)->where('invoices.invoice_type_id', '=', INVOICE_TYPE_STANDARD);
        if (!$viewAll) {
            $metrics = $metrics->where(function ($query) use($userId) {
                $query->where('invoices.user_id', '=', $userId);
                $query->orwhere(function ($query) use($userId) {
                    $query->where('invoices.user_id', '=', null);
                    $query->where('clients.user_id', '=', $userId);
                });
            });
        }
        return $metrics->groupBy('accounts.id')->first();
    }