Home screen improvements, testing for role, work on user/account models
This commit is contained in:
@@ -2,29 +2,32 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Laravel\Passport\HasApiTokens;
|
||||
use Leenooks\Carbon;
|
||||
use Leenooks\Traits\UserSwitch;
|
||||
|
||||
use App\Notifications\ResetPassword as ResetPasswordNotification;
|
||||
use App\Traits\SiteID;
|
||||
use App\Traits\{QueryCacheableConfig,SiteID};
|
||||
|
||||
/**
|
||||
* Class User
|
||||
*
|
||||
* Attributes for users:
|
||||
* + role : User's role
|
||||
*/
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens,Notifiable,UserSwitch,SiteID;
|
||||
use HasFactory,HasApiTokens,Notifiable,UserSwitch,QueryCacheableConfig,SiteID;
|
||||
|
||||
protected $appends = [
|
||||
'active_display',
|
||||
'services_count_html',
|
||||
'surfirstname',
|
||||
'switch_url',
|
||||
'user_id_url',
|
||||
];
|
||||
private const CACHE_TIME = 3600;
|
||||
|
||||
protected $dates = [
|
||||
'created_at',
|
||||
@@ -51,18 +54,6 @@ class User extends Authenticatable
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
protected $visible = [
|
||||
'active_display',
|
||||
'id',
|
||||
'level',
|
||||
'services_count_html',
|
||||
'switch_url',
|
||||
'surfirstname',
|
||||
'user_id_url',
|
||||
];
|
||||
|
||||
protected $with = ['accounts'];
|
||||
|
||||
/**
|
||||
* Role hierarchy order
|
||||
* @var array
|
||||
@@ -79,10 +70,14 @@ class User extends Authenticatable
|
||||
* The accounts that this user manages
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
* @note This cannot be loaded with "with"?
|
||||
*/
|
||||
public function accounts()
|
||||
{
|
||||
return $this->hasMany(Account::class);
|
||||
return $this->hasMany(Account::class)
|
||||
->orWhereIn('id',$this->rtm_accounts()->pluck('id'))
|
||||
->active()
|
||||
->with(['services']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,6 +131,16 @@ class User extends Authenticatable
|
||||
return $this->hasManyThrough(Payment::class,Account::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the routes to market account for this user
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
|
||||
*/
|
||||
public function rtm()
|
||||
{
|
||||
return $this->hasOneThrough(Rtm::class,Account::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* THe services this user has
|
||||
*
|
||||
@@ -181,6 +186,7 @@ class User extends Authenticatable
|
||||
|
||||
public function getActiveDisplayAttribute($value)
|
||||
{
|
||||
abort(500,'deprecated:'.__METHOD__);
|
||||
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'primary' : 'danger',$this->active ? 'Active' : 'Inactive');
|
||||
}
|
||||
|
||||
@@ -223,6 +229,16 @@ class User extends Authenticatable
|
||||
return new Carbon($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return my accounts
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getMyAccountsAttribute(): Collection
|
||||
{
|
||||
return $this->accounts->where('user_id',$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use static::getFullNameAttribute()
|
||||
* @return mixed
|
||||
@@ -245,8 +261,18 @@ class User extends Authenticatable
|
||||
->reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a friendly string of this persons role
|
||||
* @return string
|
||||
*/
|
||||
public function getRoleAttribute(): string
|
||||
{
|
||||
return ucfirst($this->role());
|
||||
}
|
||||
|
||||
public function getServicesCountHtmlAttribute()
|
||||
{
|
||||
abort(500,'deprecated:'.__METHOD__);
|
||||
return sprintf('%s <small>/%s</small>',$this->services->where('active',TRUE)->count(),$this->services->count());
|
||||
}
|
||||
|
||||
@@ -257,16 +283,19 @@ class User extends Authenticatable
|
||||
|
||||
public function getSwitchUrlAttribute()
|
||||
{
|
||||
abort(500,'deprecated:'.__METHOD__);
|
||||
return sprintf('<a href="/a/switch/start/%s"><i class="fas fa-external-link-alt"></i></a>',$this->id);
|
||||
}
|
||||
|
||||
public function getUserIdAttribute()
|
||||
{
|
||||
abort(500,'deprecated:'.__METHOD__);
|
||||
return sprintf('%02s-%04s',$this->site_id,$this->id);
|
||||
}
|
||||
|
||||
public function getUserIdUrlAttribute()
|
||||
{
|
||||
abort(500,'deprecated:'.__METHOD__);
|
||||
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->user_id);
|
||||
}
|
||||
|
||||
@@ -327,7 +356,7 @@ class User extends Authenticatable
|
||||
return $query;
|
||||
}
|
||||
|
||||
/* GENERAL METHODS */
|
||||
/* METHODS */
|
||||
|
||||
/**
|
||||
* Determine if the user is an admin of the user with $id
|
||||
@@ -337,16 +366,19 @@ class User extends Authenticatable
|
||||
*/
|
||||
public function isAdmin($id): bool
|
||||
{
|
||||
return $id AND $this->isReseller() AND in_array($id,$this->all_accounts()->pluck('user_id')->toArray());
|
||||
return $id AND $this->isReseller() AND $this->accounts->pluck('user_id')->contains($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of accounts for the clients of this user
|
||||
*
|
||||
* @return DatabaseCollection
|
||||
* @deprecated Use rtm_accounts()
|
||||
*/
|
||||
public function all_accounts(): DatabaseCollection
|
||||
{
|
||||
throw new \Exception('deprecated');
|
||||
abort(500,'deprecated:'.__METHOD__);
|
||||
$result = new DatabaseCollection();
|
||||
$clients = $this->all_clients();
|
||||
|
||||
@@ -376,7 +408,9 @@ class User extends Authenticatable
|
||||
* Get a list of clients that this user is responsible for.
|
||||
*
|
||||
* @param int $level
|
||||
* @return Collection
|
||||
* @param DatabaseCollection|null $clients
|
||||
* @return DatabaseCollection
|
||||
* @deprecated Use rtm_accounts() to determine this
|
||||
*/
|
||||
public function all_clients($level=0,DatabaseCollection $clients=NULL): DatabaseCollection
|
||||
{
|
||||
@@ -398,6 +432,10 @@ class User extends Authenticatable
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @deprecated Use rtm_accounts() to determine this list
|
||||
*/
|
||||
public function all_client_service_inactive()
|
||||
{
|
||||
$s = Service::InActive();
|
||||
@@ -413,6 +451,7 @@ class User extends Authenticatable
|
||||
*
|
||||
* @param int $level
|
||||
* @return Collection
|
||||
* @deprecated Use rtm_accounts()
|
||||
*/
|
||||
public function all_agents($level=0)
|
||||
{
|
||||
@@ -443,7 +482,7 @@ class User extends Authenticatable
|
||||
public function client_service_movements(): DatabaseCollection
|
||||
{
|
||||
return Service::active()
|
||||
->authorised($this)
|
||||
->serviceUserAuthorised($this)
|
||||
->where('order_status','!=','ACTIVE')
|
||||
->with(['account','product'])
|
||||
->get();
|
||||
@@ -558,7 +597,7 @@ class User extends Authenticatable
|
||||
])
|
||||
->from($this->query_invoice_items(),'II')
|
||||
->join('ab_invoice',['ab_invoice.id'=>'II.invoice_id'])
|
||||
->whereIN('account_id',$this->all_accounts()->pluck('id')->unique()->toArray())
|
||||
->whereIN('account_id',$this->accounts->pluck('id'))
|
||||
->where('ab_invoice.active',TRUE)
|
||||
->groupBy(['invoice_id']);
|
||||
|
||||
@@ -574,7 +613,7 @@ class User extends Authenticatable
|
||||
])
|
||||
->from($this->query_payment_items(),'PI')
|
||||
->join('payments',['payments.id'=>'PI.payment_id'])
|
||||
->whereIN('account_id',$this->all_accounts()->pluck('id')->unique()->toArray())
|
||||
->whereIN('account_id',$this->accounts->pluck('id'))
|
||||
//->where('payments.active',TRUE) // @todo To implement
|
||||
->groupBy(['invoice_id']);
|
||||
|
||||
@@ -647,16 +686,67 @@ class User extends Authenticatable
|
||||
*/
|
||||
public function role()
|
||||
{
|
||||
// If I have agents and no parent, I am the wholesaler
|
||||
if (is_null($this->parent_id) AND ($this->all_agents()->count() OR $this->all_clients()->count()))
|
||||
return 'wholesaler';
|
||||
// Cache our role for this session
|
||||
$cache_key = sprintf('%s:%s:%s',$this->id,__METHOD__,Session::getId());
|
||||
|
||||
// If I have agents and a parent, I am a reseller
|
||||
elseif ($this->parent_id AND ($this->all_agents()->count() OR $this->all_clients()->count()))
|
||||
return 'reseller';
|
||||
return Cache::remember($cache_key,self::CACHE_TIME,function() {
|
||||
// Get the RTM for our accounts
|
||||
$rtms = Rtm::whereIn('account_id',$this->accounts->pluck('id'))->get();
|
||||
|
||||
// If I have no agents and a parent, I am a customer
|
||||
elseif (! $this->all_agents()->count() AND ! $this->all_clients()->count())
|
||||
return 'customer';
|
||||
// If I have no parent, I am the wholesaler
|
||||
if ($rtms->whereNull('parent_id')->count())
|
||||
return 'wholesaler';
|
||||
|
||||
// If I exist in the RTM table, I'm a reseller
|
||||
else if ($rtms->count())
|
||||
return 'reseller';
|
||||
|
||||
// Otherwise a client
|
||||
else
|
||||
return 'customer';
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the accounts that this user can manage
|
||||
* This method is a helper to User::accounts() - use $user->accounts instead
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
private function rtm_accounts(): Collection
|
||||
{
|
||||
return Account::whereIn('rtm_id',$this->rtm_list()->pluck('id'))
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the RTM hierarchy that this user can manage
|
||||
*
|
||||
* @param Rtm|null $rtm
|
||||
* @return Collection
|
||||
*/
|
||||
public function rtm_list(Rtm $rtm=NULL): Collection
|
||||
{
|
||||
// If this user doesnt manage any accounts
|
||||
if (! $this->exists || ! $this->rtm)
|
||||
return collect();
|
||||
|
||||
$list = collect();
|
||||
|
||||
// Add this RTM to the list
|
||||
if (! $rtm) {
|
||||
$list->push($this->rtm);
|
||||
$children = $this->rtm->children;
|
||||
|
||||
} else {
|
||||
$list->push($rtm);
|
||||
$children =$rtm->children;
|
||||
}
|
||||
|
||||
// Capture any children
|
||||
foreach ($children as $child)
|
||||
$list->push($this->rtm_list($child));
|
||||
|
||||
return $rtm ? $list : $list->flatten()->unique(function($item) { return $item->id; });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user