public function find($filter = null, $userId = false)
{
$query = DB::table('clients')->join('accounts', 'accounts.id', '=', 'clients.account_id')->join('contacts', 'contacts.client_id', '=', 'clients.id')->where('clients.account_id', '=', \Auth::user()->account_id)->where('contacts.is_primary', '=', true)->where('contacts.deleted_at', '=', null)->select(DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'), DB::raw('COALESCE(clients.country_id, accounts.country_id) country_id'), 'clients.public_id', 'clients.name', 'contacts.first_name', 'contacts.last_name', 'clients.balance', 'clients.last_login', 'clients.created_at', 'clients.work_phone', 'contacts.email', 'clients.deleted_at', 'clients.is_deleted', 'clients.user_id');
if (!\Session::get('show_trash:client')) {
$query->where('clients.deleted_at', '=', null);
}
if ($filter) {
$query->where(function ($query) use($filter) {
$query->where('clients.name', 'like', '%' . $filter . '%')->orWhere('contacts.first_name', 'like', '%' . $filter . '%')->orWhere('contacts.last_name', 'like', '%' . $filter . '%')->orWhere('contacts.email', 'like', '%' . $filter . '%');
});
}
if ($userId) {
$query->where('clients.user_id', '=', $userId);
}
return $query;
}