builder; } return $this->builder->where('name', 'like', '%'.$name.'%'); } /** * Filter by balance. * * @param string $balance * @return Builder */ public function balance(string $balance = ''): Builder { if (strlen($balance) == 0 || count(explode(":", $balance)) < 2) { return $this->builder; } $parts = $this->split($balance); return $this->builder->where('balance', $parts->operator, $parts->value); } /** * Filter between balances. * * @param string $balance * @return Builder */ public function between_balance(string $balance = ''): Builder { $parts = explode(':', $balance); if (!is_array($parts) || count($parts) != 2) { return $this->builder; } return $this->builder->whereBetween('balance', [$parts[0], $parts[1]]); } public function email(string $email = ''): Builder { if (strlen($email) == 0) { return $this->builder; } return $this->builder->whereHas('contacts', function ($query) use ($email) { $query->where('email', $email); }); } public function client_id(string $client_id = ''): Builder { if (strlen($client_id) == 0) { return $this->builder; } return $this->builder->where('id', $this->decodePrimaryKey($client_id)); } public function id_number(string $id_number = ''): Builder { if (strlen($id_number) == 0) { return $this->builder; } return $this->builder->where('id_number', $id_number); } public function number(string $number = ''): Builder { if (strlen($number) == 0) { return $this->builder; } return $this->builder->where('number', $number); } public function group(string $group_id = ''): Builder { if (strlen($group_id) == 0) { return $this->builder; } return $this->builder->where('group_settings_id', $this->decodePrimaryKey($group_id)); } /** * Filter based on search text. * * @param string $filter * @return Builder * @deprecated */ public function filter(string $filter = ''): Builder { if (strlen($filter) == 0) { return $this->builder; } $searchTerms = array_filter(explode(' ', $filter)); return $this->builder->where(function ($query) use ($searchTerms) { foreach ($searchTerms as $term) { $query->where(function ($subQuery) use ($term) { $subQuery->where('name', 'like', '%'.$term.'%') ->orWhere('id_number', 'like', '%'.$term.'%') ->orWhere('number', 'like', '%'.$term.'%') ->orWhereHas('contacts', function ($contactQuery) use ($term) { $contactQuery->where('first_name', 'like', '%'.$term.'%') ->orWhere('last_name', 'like', '%'.$term.'%') ->orWhere('email', 'like', '%'.$term.'%') ->orWhere('phone', 'like', '%'.$term.'%'); }) ->orWhere('custom_value1', 'like', '%'.$term.'%') ->orWhere('custom_value2', 'like', '%'.$term.'%') ->orWhere('custom_value3', 'like', '%'.$term.'%') ->orWhere('custom_value4', 'like', '%'.$term.'%'); }); } }); } /** * Sorts the list based on $sort. * * @param string $sort formatted as column|asc * @return Builder */ public function sort(string $sort = ''): Builder { $sort_col = explode('|', $sort); if (isset($sort_col[0]) && $sort_col[0] == 'documents') { return $this->builder; } if (isset($sort_col[0]) && $sort_col[0] == 'display_name') { $sort_col[0] = 'name'; } if(is_array($sort_col) && $sort_col[0] == 'contacts'){ } elseif (!is_array($sort_col) || count($sort_col) != 2 || !in_array($sort_col[0], \Illuminate\Support\Facades\Schema::getColumnListing($this->builder->getModel()->getTable()))) { return $this->builder; } $dir = ($sort_col[1] == 'asc') ? 'asc' : 'desc'; if ($sort_col[0] == 'number') { return $this->builder->orderByRaw("REGEXP_REPLACE(number,'[^0-9]+','')+0 " . $dir); } if ($sort_col[0] == 'name') { // Use a raw subquery in the ORDER BY instead of adding it to SELECT // This avoids conflicts with the Excludable trait return $this->builder->orderByRaw(" COALESCE( NULLIF(clients.name, ''), ( SELECT COALESCE(NULLIF(first_name, ''), email) FROM client_contacts WHERE client_contacts.client_id = clients.id AND client_contacts.deleted_at IS NULL LIMIT 1 ) ) " . $dir ); } if($sort_col[0] == 'contacts'){ return $this->builder->orderByRaw(" ( SELECT CASE WHEN first_name IS NOT NULL AND first_name != '' AND last_name IS NOT NULL AND last_name != '' THEN CONCAT(first_name, ' ', last_name) WHEN first_name IS NOT NULL AND first_name != '' THEN first_name WHEN last_name IS NOT NULL AND last_name != '' THEN last_name ELSE email END FROM client_contacts WHERE client_contacts.client_id = clients.id AND client_contacts.deleted_at IS NULL ORDER BY CASE WHEN first_name IS NOT NULL AND first_name != '' AND last_name IS NOT NULL AND last_name != '' THEN 1 WHEN first_name IS NOT NULL AND first_name != '' THEN 2 WHEN last_name IS NOT NULL AND last_name != '' THEN 3 ELSE 4 END, first_name ASC, last_name ASC, email ASC LIMIT 1 ) " . $dir ); } return $this->builder->orderBy($sort_col[0], $dir); } /** * Filters the query by the users company ID. * * @return Builder */ public function entityFilter(): Builder { return $this->builder->company(); } public function filter_details(string $filter = '') { if ($filter == 'true') { return $this->builder->select('id', 'name', 'number', 'id_number'); } return $this->builder; } }