private function getAccountSearchData($user)
{
$account = $user->account;
$data = ['clients' => [], 'contacts' => [], 'invoices' => [], 'quotes' => []];
// include custom client fields in search
if ($account->custom_client_label1) {
$data[$account->custom_client_label1] = [];
}
if ($account->custom_client_label2) {
$data[$account->custom_client_label2] = [];
}
if ($user->hasPermission('view_all')) {
$clients = Client::scope()->with('contacts', 'invoices')->get();
} else {
$clients = Client::scope()->where('user_id', '=', $user->id)->with(['contacts', 'invoices' => function ($query) use($user) {
$query->where('user_id', '=', $user->id);
}])->get();
}
foreach ($clients as $client) {
if ($client->name) {
$data['clients'][] = ['value' => $client->name, 'tokens' => $client->name, 'url' => $client->present()->url];
}
if ($client->custom_value1) {
$data[$account->custom_client_label1][] = ['value' => "{$client->custom_value1}: " . $client->getDisplayName(), 'tokens' => $client->custom_value1, 'url' => $client->present()->url];
}
if ($client->custom_value2) {
$data[$account->custom_client_label2][] = ['value' => "{$client->custom_value2}: " . $client->getDisplayName(), 'tokens' => $client->custom_value2, 'url' => $client->present()->url];
}
foreach ($client->contacts as $contact) {
if ($contact->getFullName()) {
$data['contacts'][] = ['value' => $contact->getDisplayName(), 'tokens' => $contact->getDisplayName(), 'url' => $client->present()->url];
}
if ($contact->email) {
$data['contacts'][] = ['value' => $contact->email, 'tokens' => $contact->email, 'url' => $client->present()->url];
}
}
foreach ($client->invoices as $invoice) {
$entityType = $invoice->getEntityType();
$data["{$entityType}s"][] = ['value' => $invoice->getDisplayName() . ': ' . $client->getDisplayName(), 'tokens' => $invoice->getDisplayName() . ': ' . $client->getDisplayName(), 'url' => $invoice->present()->url];
}
}
return $data;
}