Removed many redundant functions from User::class
This commit is contained in:
@@ -11,7 +11,7 @@ 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\ScopeActive;
|
||||
use Leenooks\Traits\UserSwitch;
|
||||
|
||||
use App\Notifications\ResetPassword as ResetPasswordNotification;
|
||||
@@ -25,7 +25,7 @@ use App\Traits\{QueryCacheableConfig,SiteID};
|
||||
*/
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasFactory,HasApiTokens,Notifiable,UserSwitch,QueryCacheableConfig,SiteID;
|
||||
use HasFactory,HasApiTokens,Notifiable,UserSwitch,QueryCacheableConfig,SiteID,ScopeActive;
|
||||
|
||||
private const CACHE_TIME = 3600;
|
||||
|
||||
@@ -41,7 +41,9 @@ class User extends Authenticatable
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name', 'email', 'password',
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -56,14 +58,27 @@ class User extends Authenticatable
|
||||
|
||||
/**
|
||||
* Role hierarchy order
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $role_order = [
|
||||
public const role_order = [
|
||||
'wholesaler',
|
||||
'reseller',
|
||||
'customer',
|
||||
];
|
||||
|
||||
/* OVERRIDES */
|
||||
|
||||
/**
|
||||
* Users password reset email notification
|
||||
*
|
||||
* @param string $token
|
||||
*/
|
||||
public function sendPasswordResetNotification($token)
|
||||
{
|
||||
$this->notify((new ResetPasswordNotification($token))->onQueue('high'));
|
||||
}
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
/**
|
||||
@@ -81,23 +96,14 @@ class User extends Authenticatable
|
||||
}
|
||||
|
||||
/**
|
||||
* The agents that this users manages
|
||||
* This users invoices
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function agents() {
|
||||
return $this->hasMany(static::class,'parent_id','id')->with('agents');
|
||||
}
|
||||
|
||||
/**
|
||||
* The clients that this user has
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function clients() {
|
||||
return $this
|
||||
->hasMany(static::class,'parent_id','id')
|
||||
->with('clients');
|
||||
public function invoices()
|
||||
{
|
||||
return $this->hasManyThrough(Invoice::class,Account::class)
|
||||
->active();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,17 +116,6 @@ class User extends Authenticatable
|
||||
return $this->belongsTo(Language::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* This users invoices
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function invoices()
|
||||
{
|
||||
return $this->hasManyThrough(Invoice::class,Account::class)
|
||||
->active();
|
||||
}
|
||||
|
||||
/**
|
||||
* The payments this user has made
|
||||
*
|
||||
@@ -153,41 +148,16 @@ class User extends Authenticatable
|
||||
->active();
|
||||
}
|
||||
|
||||
/**
|
||||
* The site this user is configured to access
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function site()
|
||||
{
|
||||
return $this->belongsTo(Site::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* This users supplier/reseller
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
protected function supplier()
|
||||
{
|
||||
return $this->belongsTo(static::class,'parent_id','id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Who this user supplies to
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
protected function suppliers() {
|
||||
return $this->hasMany(static::class,'parent_id','id');
|
||||
}
|
||||
|
||||
/* ATTRIBUTES */
|
||||
|
||||
public function getActiveDisplayAttribute($value)
|
||||
/**
|
||||
* This is an alias method, as it is used by the framework
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNameAttribute(): string
|
||||
{
|
||||
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');
|
||||
return $this->getFullNameAttribute();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -200,35 +170,6 @@ class User extends Authenticatable
|
||||
return sprintf('%s %s',$this->firstname,$this->lastname);
|
||||
}
|
||||
|
||||
/**
|
||||
* A list of all invoices currently unpaid
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getInvoicesDueAttribute()
|
||||
{
|
||||
return $this->invoices
|
||||
->where('active',TRUE)
|
||||
->sortBy('id')
|
||||
->transform(function ($item) { if ($item->due > 0) return $item; })
|
||||
->reverse()
|
||||
->filter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a Carbon Date if it has a value.
|
||||
*
|
||||
* @param $value
|
||||
* @return Carbon
|
||||
* @throws \Exception
|
||||
* @todo This attribute is not in the schema
|
||||
*/
|
||||
public function getLastAccessAttribute($value)
|
||||
{
|
||||
if (! is_null($value))
|
||||
return new Carbon($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return my accounts
|
||||
*
|
||||
@@ -239,28 +180,6 @@ class User extends Authenticatable
|
||||
return $this->accounts->where('user_id',$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use static::getFullNameAttribute()
|
||||
* @return mixed
|
||||
*/
|
||||
public function getNameAttribute()
|
||||
{
|
||||
return $this->full_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of the payments that the user has made
|
||||
*
|
||||
* @return mixed
|
||||
* @todo Merge this with payments()
|
||||
*/
|
||||
public function getPaymentHistoryAttribute()
|
||||
{
|
||||
return $this->payments
|
||||
->sortBy('payment_date')
|
||||
->reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a friendly string of this persons role
|
||||
* @return string
|
||||
@@ -270,61 +189,18 @@ class User extends Authenticatable
|
||||
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());
|
||||
}
|
||||
|
||||
public function getSurFirstNameAttribute()
|
||||
{
|
||||
return sprintf('%s, %s',$this->lastname,$this->firstname);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/* METHODS */
|
||||
|
||||
/**
|
||||
* Users password reset email notification
|
||||
*
|
||||
* @param string $token
|
||||
*/
|
||||
public function sendPasswordResetNotification($token)
|
||||
{
|
||||
$this->notify((new ResetPasswordNotification($token))->onQueue('high'));
|
||||
}
|
||||
|
||||
/* SCOPES */
|
||||
|
||||
// @todo use trait
|
||||
public function scopeActive()
|
||||
{
|
||||
return $this->where('active',TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for a record
|
||||
*
|
||||
* @param $query
|
||||
* @param string $term
|
||||
* @return
|
||||
*/
|
||||
public function scopeSearch($query,string $term)
|
||||
{
|
||||
@@ -358,120 +234,6 @@ class User extends Authenticatable
|
||||
|
||||
/* METHODS */
|
||||
|
||||
/**
|
||||
* Determine if the user is an admin of the user with $id
|
||||
*
|
||||
* @param $id
|
||||
* @return bool
|
||||
*/
|
||||
public function isAdmin($id): bool
|
||||
{
|
||||
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();
|
||||
|
||||
foreach ($clients->pluck('accounts') as $accounts) {
|
||||
foreach ($accounts as $o) {
|
||||
if (! $o->active)
|
||||
continue;
|
||||
|
||||
$result->push($o);
|
||||
}
|
||||
}
|
||||
|
||||
// Include my accounts
|
||||
foreach ($this->accounts as $o) {
|
||||
if (! $o->active)
|
||||
continue;
|
||||
|
||||
$result->push($o);
|
||||
}
|
||||
|
||||
$result->load('user.accounts');
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of clients that this user is responsible for.
|
||||
*
|
||||
* @param int $level
|
||||
* @param DatabaseCollection|null $clients
|
||||
* @return DatabaseCollection
|
||||
* @deprecated Use rtm_accounts() to determine this
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @deprecated Use rtm_accounts() to determine this list
|
||||
*/
|
||||
public function all_client_service_inactive()
|
||||
{
|
||||
$s = Service::InActive();
|
||||
$aa = $this->all_accounts()->pluck('id')->unique()->toArray();
|
||||
|
||||
return $s->get()->filter(function($item) use ($aa) {
|
||||
return in_array($item->account_id,$aa);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* List of all this users agents, recursively
|
||||
*
|
||||
* @param int $level
|
||||
* @return Collection
|
||||
* @deprecated Use rtm_accounts()
|
||||
*/
|
||||
public function all_agents($level=0)
|
||||
{
|
||||
$result = collect();
|
||||
|
||||
foreach ($this->agents as $o) {
|
||||
if (! $o->active OR ! $o->agents->count())
|
||||
continue;
|
||||
|
||||
$o->level = $level;
|
||||
|
||||
$result->push($o);
|
||||
|
||||
// Include agents of agents
|
||||
$result->push($o->all_agents($level+1));
|
||||
}
|
||||
|
||||
return $result->flatten();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show this user's clients with service movements
|
||||
*
|
||||
@@ -488,6 +250,17 @@ class User extends Authenticatable
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is an admin of the user with $id
|
||||
*
|
||||
* @param $id
|
||||
* @return bool
|
||||
*/
|
||||
public function isAdmin($id): bool
|
||||
{
|
||||
return $id AND $this->isReseller() AND $this->accounts->pluck('user_id')->contains($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the logged in user is a reseller or wholesaler
|
||||
*
|
||||
@@ -649,33 +422,6 @@ class User extends Authenticatable
|
||||
->from($summary,'summary');
|
||||
}
|
||||
|
||||
public function query_payment_summary()
|
||||
{
|
||||
$payment = (new Payment)
|
||||
->select([
|
||||
DB::raw('payment_id AS id'),
|
||||
DB::raw('SUM(allocate) AS allocate'),
|
||||
|
||||
])
|
||||
->from($this->query_payment_items(),'PI')
|
||||
//->where('payments.active',TRUE) // @todo To implement
|
||||
->groupBy(['payment_id']);
|
||||
|
||||
return (new Payment)
|
||||
->select([
|
||||
DB::raw('payments.id AS id'),
|
||||
'date_orig',
|
||||
'payment_date',
|
||||
'total_amt',
|
||||
//'fees_amt',
|
||||
DB::raw('total_amt-allocate AS balance'),
|
||||
])
|
||||
->rightJoin('payments',['payments.id'=>'summary.id'])
|
||||
//->where('payments.active',TRUE) // @todo To implement
|
||||
->whereIN('account_id',$this->all_accounts()->pluck('id')->unique()->toArray())
|
||||
->from($payment,'summary');
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine what the logged in user's role is
|
||||
* + Wholesaler - aka Super User
|
||||
|
Reference in New Issue
Block a user