Optimise User all_accounts(),all_clients(), added Invoice/Service to search

This commit is contained in:
Deon George 2020-01-12 23:42:32 +11:00
parent 34c0380c99
commit f13c084a4b
4 changed files with 104 additions and 31 deletions

View File

@ -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;

View File

@ -43,6 +43,22 @@ class Invoice extends Model
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()
{
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total - $this->paid);

View File

@ -219,6 +219,18 @@ class Service extends Model
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 **/
/**
@ -326,7 +338,7 @@ class Service extends Model
* For ADSL, this would be the phone number,
* For Hosting, this would be the domain name, etc
*/
public function getNameShortAttribute(): string
public function getNameShortAttribute()
{
return $this->model ? $this->type->name : 'NAME UNKNOWN';
}

View File

@ -5,6 +5,8 @@ namespace App;
use App\Models\Site;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Laravel\Passport\HasApiTokens;
use Leenooks\Carbon;
@ -221,39 +223,61 @@ class User extends Authenticatable
/** 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)
{
$result->push($o->accounts->where('active',TRUE));
foreach ($clients->pluck('accounts') as $accounts) {
foreach ($accounts as $o) {
if (! $o->active)
continue;
$result->push($o);
}
}
// Include my accounts
$result->push($this->accounts);
return $result->flatten();
}
public function all_clients($level=0)
{
$result = collect();
foreach ($this->clients as $o)
{
foreach ($this->accounts as $o) {
if (! $o->active)
continue;
$o->level = $level;
$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()