Optimise User all_accounts(),all_clients(), added Invoice/Service to search
This commit is contained in:
@@ -3,43 +3,64 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
use App\Models\{Account,Service\Adsl};
|
||||
use App\Models\{Account,Invoice,Service,Service\Adsl};
|
||||
|
||||
class SearchController extends Controller
|
||||
{
|
||||
/**
|
||||
* Show the application dashboard.
|
||||
* Search from the Application Dashboard.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function search(Request $request)
|
||||
{
|
||||
// If there isnt a term value, return null
|
||||
if (! $request->input('term'))
|
||||
return [];
|
||||
|
||||
$result = collect();
|
||||
$uo = Auth::user();
|
||||
$accounts = Auth::user()->all_accounts()->pluck('id');
|
||||
|
||||
# Look for Account
|
||||
foreach (Account::Search($request->input('term'))
|
||||
->whereIN('id',$uo->all_accounts()->pluck('id'))
|
||||
->whereIN('id',$accounts)
|
||||
->orderBy('company')
|
||||
->orderBy('last_name')
|
||||
->orderBy('first_name')
|
||||
->limit(10)->get() as $o)
|
||||
{
|
||||
$result->push(['label'=>sprintf('A:%s %s',$o->aid,$o->name),'value'=>'/u/account/'.$o->id]);
|
||||
$result->push(['label'=>sprintf('AC:%s %s',$o->aid,$o->name),'value'=>'/u/account/'.$o->id]);
|
||||
}
|
||||
|
||||
# Look for a Service
|
||||
foreach (Service::Search($request->input('term'))
|
||||
->whereIN('account_id',$accounts)
|
||||
->orderBy('id')
|
||||
->limit(10)->get() as $o)
|
||||
{
|
||||
$result->push(['label'=>sprintf('SV:%s (%s)',$o->name,$o->sid),'value'=>'/u/service/'.$o->id]);
|
||||
}
|
||||
|
||||
# Look for an Invoice
|
||||
foreach (Invoice::Search($request->input('term'))
|
||||
->whereIN('account_id',$accounts)
|
||||
->orderBy('id')
|
||||
->limit(10)->get() as $o)
|
||||
{
|
||||
$result->push(['label'=>sprintf('IN:%s #%s',$o->account->name,$o->invoice_id),'value'=>'/u/invoice/'.$o->id]);
|
||||
}
|
||||
|
||||
# Look for an ADSL/NBN Service
|
||||
foreach (Adsl::Search($request->input('term'))
|
||||
->whereIN('account_id',$uo->all_accounts()->pluck('id'))
|
||||
->whereIN('account_id',$accounts)
|
||||
->orderBy('service_number')
|
||||
->limit(10)->get() as $o)
|
||||
{
|
||||
$result->push(['label'=>sprintf('S:%s (%s)',$o->name,$o->service->sid),'value'=>'/u/service/'.$o->id]);
|
||||
$result->push(['label'=>sprintf('SV:%s (%s)',$o->name,$o->service->sid),'value'=>'/u/service/'.$o->id]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
Reference in New Issue
Block a user