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

averages() public method

public averages ( $account, $userId, $viewAll )
    public function averages($account, $userId, $viewAll)
    {
        $accountId = $account->id;
        $select = DB::raw('AVG(' . DB::getQueryGrammar()->wrap('invoices.amount', true) . ') as invoice_avg, ' . DB::getQueryGrammar()->wrap('clients.currency_id', true) . ' as currency_id');
        $averageInvoice = 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.invoice_type_id', '=', INVOICE_TYPE_STANDARD)->where('invoices.is_recurring', '=', false);
        if (!$viewAll) {
            $averageInvoice->where('invoices.user_id', '=', $userId);
        }
        if ($account->financial_year_start) {
            $yearStart = str_replace('2000', date('Y'), $account->financial_year_start);
            $averageInvoice->where('invoices.invoice_date', '>=', $yearStart);
        }
        return $averageInvoice->groupBy('accounts.id')->groupBy(DB::raw('CASE WHEN ' . DB::getQueryGrammar()->wrap('clients.currency_id', true) . ' IS NULL THEN CASE WHEN ' . DB::getQueryGrammar()->wrap('accounts.currency_id', true) . ' IS NULL THEN 1 ELSE ' . DB::getQueryGrammar()->wrap('accounts.currency_id', true) . ' END ELSE ' . DB::getQueryGrammar()->wrap('clients.currency_id', true) . ' END'))->get();
    }