osb/app/Http/Controllers/SearchController.php

95 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
2022-08-07 02:17:20 +00:00
use App\Models\{Account,Invoice,Payment,Service,Supplier,User};
class SearchController extends Controller
{
/**
* Search from the Application Dashboard.
*
* @param Request $request
* @return Collection
*/
public function search(Request $request): Collection
{
$result = collect();
// If the user isnt logged in
if (! Auth::user())
abort(401,'Need to login');
// 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');
2020-02-08 11:51:50 +00:00
# Look for User
foreach (User::Search($request->input('term'))
->whereIN('id',$user_ids)
->orderBy('lastname')
->orderBy('firstname')
->limit(10)->get() as $o)
{
2022-08-07 02:17:20 +00:00
$result->push(['name'=>sprintf('%s (%s) - %s',$o->name,$o->lid,$o->email),'value'=>'/u/home/'.$o->id,'category'=>'Users']);
}
2022-08-07 02:17:20 +00:00
# Look for User by Supplier
if (is_numeric($request->input('term')))
foreach (Supplier::Search($request->input('term'))
->whereIN('user_id',$user_ids)
->orderBy('name')
->limit(10)->get() as $o)
{
$oo = $o->users->filter(function($item) use ($request) { return str_contains($item->pivot->id,$request->input('term')); })->pop();
$result->push(['name'=>sprintf('%s (%s:%s)',$oo->name,$o->name,$oo->pivot->id),'value'=>'/u/home/'.$oo->id,'category'=>'Suppliers']);
}
2020-02-08 11:51:50 +00:00
# Look for Account
foreach (Account::Search($request->input('term'))
->whereIN('user_id',$user_ids)
->orderBy('company')
->limit(10)->get() as $o)
2020-02-08 11:51:50 +00:00
{
$result->push(['name'=>sprintf('%s (%s)',$o->company,$o->lid),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']);
2020-02-08 11:51:50 +00:00
}
# Look for a Service
foreach (Service::Search($request->input('term'))
->whereIN('account_id',$account_ids)
->orderBy('id')
2022-06-14 06:51:18 +00:00
->limit(20)->get() as $o)
{
2022-06-14 06:51:18 +00:00
$result->push(['name'=>sprintf('%s (%s) %s',$o->name,$o->lid,$o->active ? '' : '<small>INACT</small>'),'value'=>'/u/service/'.$o->id,'category'=>$o->category_name]);
}
# 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']);
}
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']);
}
}
2022-06-14 06:51:18 +00:00
return $result->sortBy(function($item) { return $item['category'].$item['name']; })->values();
}
}