Optimise User all_accounts(),all_clients(), added Invoice/Service to search
This commit is contained in:
parent
34c0380c99
commit
f13c084a4b
@ -3,43 +3,64 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
use App\Models\{Account,Service\Adsl};
|
use App\Models\{Account,Invoice,Service,Service\Adsl};
|
||||||
|
|
||||||
class SearchController extends Controller
|
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)
|
public function search(Request $request)
|
||||||
{
|
{
|
||||||
|
// If there isnt a term value, return null
|
||||||
if (! $request->input('term'))
|
if (! $request->input('term'))
|
||||||
return [];
|
return [];
|
||||||
|
|
||||||
$result = collect();
|
$result = collect();
|
||||||
$uo = Auth::user();
|
$accounts = Auth::user()->all_accounts()->pluck('id');
|
||||||
|
|
||||||
# Look for Account
|
# Look for Account
|
||||||
foreach (Account::Search($request->input('term'))
|
foreach (Account::Search($request->input('term'))
|
||||||
->whereIN('id',$uo->all_accounts()->pluck('id'))
|
->whereIN('id',$accounts)
|
||||||
->orderBy('company')
|
->orderBy('company')
|
||||||
->orderBy('last_name')
|
->orderBy('last_name')
|
||||||
->orderBy('first_name')
|
->orderBy('first_name')
|
||||||
->limit(10)->get() as $o)
|
->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
|
# Look for an ADSL/NBN Service
|
||||||
foreach (Adsl::Search($request->input('term'))
|
foreach (Adsl::Search($request->input('term'))
|
||||||
->whereIN('account_id',$uo->all_accounts()->pluck('id'))
|
->whereIN('account_id',$accounts)
|
||||||
->orderBy('service_number')
|
->orderBy('service_number')
|
||||||
->limit(10)->get() as $o)
|
->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;
|
return $result;
|
||||||
|
@ -43,6 +43,22 @@ class Invoice extends Model
|
|||||||
return $this->hasMany(PaymentItem::class);
|
return $this->hasMany(PaymentItem::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** SCOPES **/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for a record
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
* @param string $term
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public function scopeSearch($query,string $term)
|
||||||
|
{
|
||||||
|
return $query->where('id','like','%'.$term.'%');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** ATTRIBUTES **/
|
||||||
|
|
||||||
public function getDueAttribute()
|
public function getDueAttribute()
|
||||||
{
|
{
|
||||||
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total - $this->paid);
|
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total - $this->paid);
|
||||||
|
@ -219,6 +219,18 @@ class Service extends Model
|
|||||||
return $query->setEagerLoads([]);
|
return $query->setEagerLoads([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for a record
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
* @param string $term
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public function scopeSearch($query,string $term)
|
||||||
|
{
|
||||||
|
return $query->where('id','like','%'.$term.'%');
|
||||||
|
}
|
||||||
|
|
||||||
/** ATTRIBUTES **/
|
/** ATTRIBUTES **/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -326,7 +338,7 @@ class Service extends Model
|
|||||||
* For ADSL, this would be the phone number,
|
* For ADSL, this would be the phone number,
|
||||||
* For Hosting, this would be the domain name, etc
|
* For Hosting, this would be the domain name, etc
|
||||||
*/
|
*/
|
||||||
public function getNameShortAttribute(): string
|
public function getNameShortAttribute()
|
||||||
{
|
{
|
||||||
return $this->model ? $this->type->name : 'NAME UNKNOWN';
|
return $this->model ? $this->type->name : 'NAME UNKNOWN';
|
||||||
}
|
}
|
||||||
|
68
app/User.php
68
app/User.php
@ -5,6 +5,8 @@ namespace App;
|
|||||||
use App\Models\Site;
|
use App\Models\Site;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
|
||||||
use Laravel\Passport\HasApiTokens;
|
use Laravel\Passport\HasApiTokens;
|
||||||
|
|
||||||
use Leenooks\Carbon;
|
use Leenooks\Carbon;
|
||||||
@ -221,39 +223,61 @@ class User extends Authenticatable
|
|||||||
|
|
||||||
/** Functions */
|
/** Functions */
|
||||||
|
|
||||||
public function all_accounts()
|
/**
|
||||||
|
* Get a list of accounts for the clients of this user
|
||||||
|
*
|
||||||
|
* @return DatabaseCollection
|
||||||
|
*/
|
||||||
|
public function all_accounts(): DatabaseCollection
|
||||||
{
|
{
|
||||||
$result = collect();
|
$result = new DatabaseCollection();
|
||||||
|
$clients = $this->all_clients();
|
||||||
|
$clients->load('accounts');
|
||||||
|
|
||||||
foreach ($this->all_clients() as $o)
|
foreach ($clients->pluck('accounts') as $accounts) {
|
||||||
{
|
foreach ($accounts as $o) {
|
||||||
$result->push($o->accounts->where('active',TRUE));
|
if (! $o->active)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
$result->push($o);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Include my accounts
|
// Include my accounts
|
||||||
$result->push($this->accounts);
|
foreach ($this->accounts as $o) {
|
||||||
|
|
||||||
return $result->flatten();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function all_clients($level=0)
|
|
||||||
{
|
|
||||||
$result = collect();
|
|
||||||
|
|
||||||
foreach ($this->clients as $o)
|
|
||||||
{
|
|
||||||
if (! $o->active)
|
if (! $o->active)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$o->level = $level;
|
|
||||||
|
|
||||||
$result->push($o);
|
$result->push($o);
|
||||||
|
|
||||||
// Include clients of agents
|
|
||||||
$result->push($o->all_clients($level+1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result->flatten();
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of clients that this user is responsible for.
|
||||||
|
*
|
||||||
|
* @param int $level
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function all_clients($level=0,DatabaseCollection $clients=NULL): DatabaseCollection
|
||||||
|
{
|
||||||
|
$result = is_null($clients) ? $this->clients : $clients;
|
||||||
|
|
||||||
|
$result
|
||||||
|
->filter(function($item) { return $item->active; })
|
||||||
|
->transform(function($item) use ($level) { $item->level = $level; return $item; });
|
||||||
|
|
||||||
|
foreach ($result->pluck('clients') as $clients) {
|
||||||
|
foreach ($this->all_clients($level+1,$clients) as $o) {
|
||||||
|
if (! $o->active)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
$result->push($o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function all_client_service_inactive()
|
public function all_client_service_inactive()
|
||||||
|
Loading…
Reference in New Issue
Block a user