2019-06-21 06:21:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2020-01-12 12:42:32 +00:00
|
|
|
use Illuminate\Http\Response;
|
2019-06-21 06:21:48 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
2020-02-06 22:11:02 +00:00
|
|
|
use App\User;
|
2020-01-12 12:42:32 +00:00
|
|
|
use App\Models\{Account,Invoice,Service,Service\Adsl};
|
2019-06-21 06:21:48 +00:00
|
|
|
|
|
|
|
class SearchController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2020-01-12 12:42:32 +00:00
|
|
|
* Search from the Application Dashboard.
|
2019-06-21 06:21:48 +00:00
|
|
|
*
|
2020-01-12 12:42:32 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @return Response
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
|
|
|
public function search(Request $request)
|
|
|
|
{
|
2020-01-12 12:42:32 +00:00
|
|
|
// If there isnt a term value, return null
|
2019-06-21 06:21:48 +00:00
|
|
|
if (! $request->input('term'))
|
|
|
|
return [];
|
|
|
|
|
|
|
|
$result = collect();
|
2020-02-06 22:11:02 +00:00
|
|
|
$accounts = ($x=Auth::user()->all_accounts())->pluck('id');
|
|
|
|
$users = $x->transform(function($item) { return $item->user;});
|
2019-06-21 06:21:48 +00:00
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
# Look for User
|
2020-02-06 22:11:02 +00:00
|
|
|
foreach (User::Search($request->input('term'))
|
|
|
|
->whereIN('id',$users->pluck('id'))
|
|
|
|
->orderBy('lastname')
|
|
|
|
->orderBy('firstname')
|
2019-06-21 06:21:48 +00:00
|
|
|
->limit(10)->get() as $o)
|
|
|
|
{
|
2020-05-26 01:44:02 +00:00
|
|
|
$result->push(['name'=>sprintf('US:%s %s',$o->aid,$o->name),'value'=>'/u/home/'.$o->id,'category'=>'Users']);
|
2020-01-12 12:42:32 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 11:51:50 +00:00
|
|
|
# Look for Account
|
|
|
|
foreach (Account::Search($request->input('term'))
|
|
|
|
->whereIN('user_id',$users->pluck('id'))
|
|
|
|
->orderBy('company')
|
|
|
|
->limit(10)->get() as $o)
|
|
|
|
{
|
2020-05-26 01:44:02 +00:00
|
|
|
$result->push(['name'=>sprintf('AC:%s %s',$o->aid,$o->company),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']);
|
2020-02-08 11:51:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 12:42:32 +00:00
|
|
|
# Look for a Service
|
|
|
|
foreach (Service::Search($request->input('term'))
|
|
|
|
->whereIN('account_id',$accounts)
|
|
|
|
->orderBy('id')
|
|
|
|
->limit(10)->get() as $o)
|
|
|
|
{
|
2020-05-26 01:44:02 +00:00
|
|
|
$result->push(['name'=>sprintf('SV:%s (%s)',$o->name,$o->sid),'value'=>'/u/service/'.$o->id,'category'=>'Services']);
|
2020-01-12 12:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Look for an Invoice
|
|
|
|
foreach (Invoice::Search($request->input('term'))
|
|
|
|
->whereIN('account_id',$accounts)
|
|
|
|
->orderBy('id')
|
|
|
|
->limit(10)->get() as $o)
|
|
|
|
{
|
2020-05-26 01:44:02 +00:00
|
|
|
$result->push(['name'=>sprintf('IN:%s #%s',$o->account->name,$o->invoice_id),'value'=>'/u/invoice/'.$o->id,'category'=>'Invoices']);
|
2019-06-21 06:21:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-29 00:14:12 +00:00
|
|
|
# Look for an ADSL/NBN Service
|
|
|
|
foreach (Adsl::Search($request->input('term'))
|
2020-01-12 12:42:32 +00:00
|
|
|
->whereIN('account_id',$accounts)
|
2019-06-29 00:14:12 +00:00
|
|
|
->orderBy('service_number')
|
|
|
|
->limit(10)->get() as $o)
|
|
|
|
{
|
2020-05-26 01:44:02 +00:00
|
|
|
$result->push(['name'=>sprintf('SV:%s (%s)',$o->name,$o->service->sid),'value'=>'/u/service/'.$o->id,'category'=>'Broadband']);
|
2019-06-29 00:14:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:21:48 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|