Removed many redundant functions from User::class

This commit is contained in:
Deon George
2022-04-21 16:25:29 +10:00
parent 796c72dd09
commit d53643ef55
25 changed files with 87 additions and 625 deletions

View File

@@ -131,24 +131,6 @@ class Account extends Model implements IDs
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'success' : 'danger',$this->active ? 'Active' : 'Inactive');
}
/**
* @deprecated use getAIDAttribute()
*/
public function getAccountIdAttribute()
{
abort(500,'deprecated');
return $this->getAIDAttribute();
}
/**
* @deprecated use getUrlAdminAttribute()
*/
public function getAccountIdUrlAttribute()
{
abort(500,'deprecated');
return $this->getUrlAdminAttribute();
}
/**
* Get the address for the account
*
@@ -163,17 +145,6 @@ class Account extends Model implements IDs
];
}
/**
* Return the Account Unique Identifier
* @return string
* @deprecated use getSIDAttribute()
*/
public function getAIDAttribute()
{
abort(500,'deprecated');
return $this->getSIDAttribute();
}
/**
* Return the account name
*
@@ -181,19 +152,7 @@ class Account extends Model implements IDs
*/
public function getNameAttribute(): string
{
return $this->company ?: ($this->user_id ? $this->user->SurFirstName : 'AID:'.$this->id);
}
public function getServicesCountHtmlAttribute()
{
abort(500,'deprecated');
return sprintf('%s <small>/%s</small>',$this->services()->noEagerLoads()->where('active',TRUE)->count(),$this->services()->noEagerLoads()->count());
}
public function getSwitchUrlAttribute()
{
abort(500,'deprecated');
return sprintf('<a href="/r/switch/start/%s"><i class="fas fa-external-link-alt"></i></a>',$this->user_id);
return $this->company ?: ($this->user_id ? $this->user->getSurFirstNameAttribute() : 'LID:'.$this->id);
}
public function getTypeAttribute()
@@ -205,6 +164,7 @@ class Account extends Model implements IDs
* Return the Admin URL to manage the account
*
* @return string
* @deprecated
*/
public function getUrlAdminAttribute(): string
{
@@ -216,6 +176,7 @@ class Account extends Model implements IDs
* Return the User URL to manage the account
*
* @return string
* @deprecated
*/
public function getUrlUserAttribute(): string
{

View File

@@ -26,7 +26,7 @@ class AccountPolicy
OR $uo->isWholesaler()
// The user is the reseller
OR ($uo->all_accounts()->pluck('id')->search($ao->id) !== FALSE);
OR $uo->isReseller();
}
/**

View File

@@ -26,7 +26,7 @@ class InvoicePolicy
OR $uo->isWholesaler()
// The user is the reseller
OR ($uo->all_accounts()->pluck('id')->search($io->account_id) !== FALSE);
OR $uo->isReseller();
}
/**

View File

@@ -26,7 +26,7 @@ class ServicePolicy
OR $uo->isWholesaler()
// The user is the reseller
OR ($uo->all_accounts()->pluck('id')->search($so->account_id) !== FALSE);
OR $uo->isReseller();
}
/**

View File

@@ -26,7 +26,7 @@ class UserPolicy
OR $uo->isWholesaler()
// The user is the reseller
OR ($uo->all_clients()->pluck('id')->search($o->id) !== FALSE);
OR $uo->isReseller();
}
/**

View File

@@ -49,17 +49,6 @@ class Service extends Model implements IDs
{
use HasFactory,ScopeServiceUserAuthorised;
protected $appends = [
'account_name',
'admin_service_id_url',
'billing_price',
'name_short',
'next_invoice',
'service_id',
'service_id_url',
'status',
];
protected $casts = [
'order_info'=>AsCollection::class,
];
@@ -71,35 +60,12 @@ class Service extends Model implements IDs
'stop_at',
];
protected $visible = [
'account_name',
'admin_service_id_url',
'active',
'billing_price',
'data_orig',
'id',
'name_short',
'next_invoice',
'product_category',
'product_name',
'service_id',
'service_id_url',
'status',
];
protected $with = [
'invoice_items',
'product.type.supplied',
'type',
];
// @todo Change to self::INACTIVE_STATUS
private $inactive_status = [
'CANCELLED',
'ORDER-REJECTED',
'ORDER-CANCELLED',
];
public const INACTIVE_STATUS = [
'CANCELLED',
'ORDER-REJECTED',
@@ -406,7 +372,7 @@ class Service extends Model implements IDs
{
return $query->where(function () use ($query) {
$query->where($this->getTable().'.active',TRUE)
->orWhereNotIn('order_status',$this->inactive_status);
->orWhereNotIn('order_status',self::INACTIVE_STATUS);
});
}
@@ -420,7 +386,7 @@ class Service extends Model implements IDs
{
return $query->where(function () use ($query) {
$query->where($this->getTable().'.active',FALSE)
->orWhereIn('order_status',$this->inactive_status);
->orWhereIn('order_status',self::INACTIVE_STATUS);
});
}
@@ -459,14 +425,6 @@ class Service extends Model implements IDs
return $this->account->name;
}
/**
* @deprecated Use getUrlAdminAttribute()
*/
public function getAdminServiceIdUrlAttribute()
{
return $this->getUrlAdminAttribute();
}
/**
* Return the auto billing details
*
@@ -1170,7 +1128,7 @@ class Service extends Model implements IDs
public function getStageParameters(string $stage): Collection
{
$result = Arr::get(self::ACTION_PROGRESS,$stage);
$myrole = array_search(Auth::user()->role(),User::$role_order);
$myrole = array_search(Auth::user()->role(),User::role_order);
// If we have no valid next stage, return an empty collection.
if (($myrole === FALSE) || (! $result))
@@ -1185,7 +1143,7 @@ class Service extends Model implements IDs
$cando = FALSE;
foreach ($roles as $role) {
if ($myrole <= array_search($role,User::$role_order)) {
if ($myrole <= array_search($role,User::role_order)) {
$cando = TRUE;
break;
@@ -1234,14 +1192,14 @@ class Service extends Model implements IDs
}
/**
* Determine if a service is active. It is active, if active=1, or the order_status is not in inactive_status[]
* Determine if a service is active. It is active, if active=1, or the order_status is not in self::INACTIVE_STATUS[]
*
* @return bool
* @todo Remove active and have order_status reflect whether active or not
*/
public function isActive(): bool
{
return $this->active OR ($this->order_status AND ! in_array($this->order_status,$this->inactive_status));
return $this->active OR ($this->order_status AND ! in_array($this->order_status,self::INACTIVE_STATUS));
}
/**
@@ -1323,7 +1281,7 @@ class Service extends Model implements IDs
{
return ! $this->active
AND ! is_null($this->order_status)
AND ! in_array($this->order_status,array_merge($this->inactive_status,['INACTIVE']));
AND ! in_array($this->order_status,array_merge(self::INACTIVE_STATUS,['INACTIVE']));
}
/**
@@ -1464,6 +1422,6 @@ class Service extends Model implements IDs
*/
public function wasCancelled(): bool
{
return in_array($this->order_status,$this->inactive_status);
return in_array($this->order_status,self::INACTIVE_STATUS);
}
}

View File

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