2019-06-21 06:21:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2021-10-08 01:19:33 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2019-06-21 06:21:48 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-07-23 07:25:26 +00:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2019-06-21 06:21:48 +00:00
|
|
|
|
2024-07-06 12:56:51 +00:00
|
|
|
use App\Models\{Account,Invoice,Payment,Service,User};
|
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
|
2021-10-08 01:19:33 +00:00
|
|
|
* @return Collection
|
2019-06-21 06:21:48 +00:00
|
|
|
*/
|
2021-10-08 01:19:33 +00:00
|
|
|
public function search(Request $request): Collection
|
2019-06-21 06:21:48 +00:00
|
|
|
{
|
2021-10-08 01:19:33 +00:00
|
|
|
$result = collect();
|
|
|
|
|
2022-08-03 06:34:23 +00:00
|
|
|
// If the user isnt logged in
|
|
|
|
if (! Auth::user())
|
|
|
|
abort(401,'Need to login');
|
|
|
|
|
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'))
|
2021-10-08 01:19:33 +00:00
|
|
|
return $result;
|
2019-06-21 06:21:48 +00:00
|
|
|
|
2024-07-06 12:56:51 +00:00
|
|
|
$account_ids = ($x=Auth::user()->accounts_all)->pluck('id');
|
|
|
|
$user_ids = $x->pluck('user_id')->unique();
|
2019-06-21 06:21:48 +00:00
|
|
|
|
2024-07-06 12:56:51 +00:00
|
|
|
// Look for User
|
2020-02-06 22:11:02 +00:00
|
|
|
foreach (User::Search($request->input('term'))
|
2022-04-21 06:25:29 +00:00
|
|
|
->whereIN('id',$user_ids)
|
2020-02-06 22:11:02 +00:00
|
|
|
->orderBy('lastname')
|
|
|
|
->orderBy('firstname')
|
2019-06-21 06:21:48 +00:00
|
|
|
->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']);
|
2020-01-12 12:42:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-06 12:56:51 +00:00
|
|
|
// Look for User by their Supplier ID with some suppliers
|
2022-08-07 02:17:20 +00:00
|
|
|
if (is_numeric($request->input('term')))
|
2024-07-24 04:53:18 +00:00
|
|
|
foreach (Account::select(['user_id','suppliers.name AS supplier_name','account_supplier.supplier_ref AS pivot_id'])
|
|
|
|
->join('account_supplier',['account_supplier.account_id'=>'accounts.id'])
|
|
|
|
->join('suppliers',['suppliers.id'=>'account_supplier.supplier_id'])
|
|
|
|
->whereIN('accounts.id',$account_ids)
|
|
|
|
->where('account_supplier.supplier_ref','like','%'.$request->input('term').'%')
|
|
|
|
->orderBy('company')
|
2022-08-07 02:17:20 +00:00
|
|
|
->limit(10)->get() as $o)
|
|
|
|
{
|
2024-07-24 04:53:18 +00:00
|
|
|
$result->push(['name'=>sprintf('%s (%s:%s)',$o->company,$o->supplier_name,$o->supplier_ref),'value'=>'/u/home/'.$o->user_id,'category'=>'Suppliers']);
|
2022-08-07 02:17:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-06 12:56:51 +00:00
|
|
|
// Look for Account
|
2020-02-08 11:51:50 +00:00
|
|
|
foreach (Account::Search($request->input('term'))
|
2024-07-06 12:56:51 +00:00
|
|
|
->whereIN('id',$account_ids)
|
2022-04-22 00:36:41 +00:00
|
|
|
->orderBy('company')
|
|
|
|
->limit(10)->get() as $o)
|
2020-02-08 11:51:50 +00:00
|
|
|
{
|
2024-07-06 12:56:51 +00:00
|
|
|
$result->push(['name'=>sprintf('%s (%s)',$o->name,$o->lid),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']);
|
2020-02-08 11:51:50 +00:00
|
|
|
}
|
|
|
|
|
2024-07-06 12:56:51 +00:00
|
|
|
// Look for a Service
|
2020-01-12 12:42:32 +00:00
|
|
|
foreach (Service::Search($request->input('term'))
|
2022-04-22 00:36:41 +00:00
|
|
|
->whereIN('account_id',$account_ids)
|
|
|
|
->orderBy('id')
|
2024-07-29 13:12:53 +00:00
|
|
|
->limit(20)
|
|
|
|
->with(['product'])
|
|
|
|
->get() as $o)
|
2020-01-12 12:42:32 +00:00
|
|
|
{
|
2024-07-29 13:12:53 +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->product->category_name]);
|
2020-01-12 12:42:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-06 12:56:51 +00:00
|
|
|
// Look for an Invoice
|
2020-01-12 12:42:32 +00:00
|
|
|
foreach (Invoice::Search($request->input('term'))
|
2022-04-22 00:36:41 +00:00
|
|
|
->whereIN('account_id',$account_ids)
|
|
|
|
->orderBy('id')
|
|
|
|
->limit(10)->get() as $o)
|
2020-01-12 12:42:32 +00:00
|
|
|
{
|
2022-04-22 00:36:41 +00:00
|
|
|
$result->push(['name'=>sprintf('%s: %s',$o->lid,$o->account->name),'value'=>'/u/invoice/'.$o->id,'category'=>'Invoices']);
|
2019-06-21 06:21:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
if (Gate::any(['wholesaler'],new Payment)) {
|
2024-07-06 12:56:51 +00:00
|
|
|
// Look for Payments
|
2021-07-23 07:25:26 +00:00
|
|
|
foreach (Payment::Search($request->input('term'))
|
2022-04-22 00:36:41 +00:00
|
|
|
->whereIN('account_id',$account_ids)
|
|
|
|
->limit(10)->get() as $o)
|
2021-07-23 07:25:26 +00:00
|
|
|
{
|
2022-04-22 00:36:41 +00:00
|
|
|
$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']);
|
2021-07-23 07:25:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return $result
|
2024-07-29 13:12:53 +00:00
|
|
|
->sortBy(fn($item)=>$item['category'].$item['name'])
|
2024-07-04 05:03:11 +00:00
|
|
|
->values();
|
2019-06-21 06:21:48 +00:00
|
|
|
}
|
|
|
|
}
|