osb/app/Http/Controllers/SearchController.php
2022-04-22 15:25:13 +10:00

110 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use App\Models\{Account,Invoice,Payment,Service,Service\Broadband,Service\Phone,User};
class SearchController extends Controller
{
/**
* Search from the Application Dashboard.
*
* @param Request $request
* @return Collection
*/
public function search(Request $request): Collection
{
$result = collect();
// If there isnt a term value, return null
if (! $request->input('term'))
return $result;
$account_ids = ($x=Auth::user()->accounts)->pluck('id');
$user_ids = $x->transform(function($item) { return $item->user;})->pluck('id');
# Look for User
foreach (User::Search($request->input('term'))
->whereIN('id',$user_ids)
->orderBy('lastname')
->orderBy('firstname')
->limit(10)->get() as $o)
{
$result->push(['name'=>sprintf('%s (%s)',$o->lid,$o->name),'value'=>'/u/home/'.$o->id,'category'=>'Users']);
}
# Look for Account
foreach (Account::Search($request->input('term'))
->whereIN('user_id',$user_ids)
->orderBy('company')
->limit(10)->get() as $o)
{
$result->push(['name'=>sprintf('%s (%s)',$o->lid,$o->company),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']);
}
# Look for a Service
foreach (Service::Search($request->input('term'))
->whereIN('account_id',$account_ids)
->orderBy('id')
->limit(10)->get() as $o)
{
$result->push(['name'=>sprintf('%s (%s)',$o->name,$o->lid),'value'=>'/u/service/'.$o->id,'category'=>'Services']);
}
# Look for an Invoice
foreach (Invoice::Search($request->input('term'))
->whereIN('account_id',$account_ids)
->orderBy('id')
->limit(10)->get() as $o)
{
$result->push(['name'=>sprintf('%s: %s',$o->lid,$o->account->name),'value'=>'/u/invoice/'.$o->id,'category'=>'Invoices']);
}
# Look for a Broadband Service
foreach (Broadband::Search($request->input('term'))
->whereIN('account_id',$account_ids)
->orderBy('service_number')
->with(['service'])
->limit(10)->get() as $o)
{
$result->push(['name'=>sprintf('%s (%s)',$o->service->name,$o->service->lid),'value'=>'/u/service/'.$o->service_id,'category'=>'Broadband']);
}
# Look for a Phone Service
foreach (Phone::Search($request->input('term'))
->whereIN('account_id',$account_ids)
->orderBy('service_number')
->with(['service'])
->limit(10)->get() as $o)
{
$result->push(['name'=>sprintf('%s (%s)',$o->service->name,$o->service->lid),'value'=>'/u/service/'.$o->service_id,'category'=>'Phone']);
}
# Look for Domain Name
foreach (Service\Domain::Search($request->input('term'))
->whereIN('account_id',$account_ids)
->orderBy('domain_name')
->with(['service'])
->limit(10)->get() as $o)
{
$result->push(['name'=>sprintf('%s (%s)',$o->service->name,$o->service->lid),'value'=>'/u/service/'.$o->service_id,'category'=>'Domains']);
}
if (Gate::any(['wholesaler'],new Payment)) {
# Look for Payments
foreach (Payment::Search($request->input('term'))
->whereIN('account_id',$account_ids)
->limit(10)->get() as $o)
{
$result->push(['name'=>sprintf('%s: %s $%s',$o->lid,$o->account->name,number_format($o->total,2)),'value'=>'/a/payment/addedit/'.$o->id,'category'=>'Payments']);
}
}
return $result;
}
}