Start of ordering

This commit is contained in:
Deon George
2018-08-10 00:10:51 +10:00
parent ca402df525
commit 499d44289e
15 changed files with 554 additions and 73 deletions

View File

@@ -95,6 +95,8 @@ class User extends Authenticatable
return $this->hasMany(static::class,'parent_id','id');
}
/** Attributes **/
public function getActiveDisplayAttribute($value)
{
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'primary' : 'danger',$this->active ? 'Active' : 'Inactive');
@@ -110,24 +112,6 @@ class User extends Authenticatable
return sprintf('%s %s',$this->firstname,$this->lastname);
}
public function getSurFirstNameAttribute()
{
return sprintf('%s, %s',$this->lastname,$this->firstname);
}
/**
* Return a Carbon Date if it has a value.
*
* @param $value
* @return \Leenooks\Carbon
* @todo This attribute is not in the schema
*/
public function getLastAccessAttribute($value)
{
if (! is_null($value))
return new Carbon($value);
}
public function getInvoicesDueAttribute()
{
return $this->invoices
@@ -144,6 +128,28 @@ class User extends Authenticatable
return config('SITE_SETUP')->language;
}
/**
* Return a Carbon Date if it has a value.
*
* @param $value
* @return \Leenooks\Carbon
* @todo This attribute is not in the schema
*/
public function getLastAccessAttribute($value)
{
if (! is_null($value))
return new Carbon($value);
}
/**
* @deprecated Use static::getFullNameAttribute()
* @return mixed
*/
public function getNameAttribute()
{
return $this->full_name;
}
public function getPaymentHistoryAttribute()
{
return $this->payments
@@ -157,20 +163,16 @@ class User extends Authenticatable
->where('active',TRUE);
}
/**
* @deprecated Use static::getFullNameAttribute()
* @return mixed
*/
public function getNameAttribute()
{
return $this->full_name;
}
public function getServicesCountHtmlAttribute()
{
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()
{
return sprintf('<a href="/a/switch/start/%s"><i class="fa fa-external-link"></i></a>',$this->id);
@@ -186,41 +188,14 @@ class User extends Authenticatable
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->user_id);
}
/** Scopes **/
public function scopeActive()
{
return $this->where('active',TRUE);
}
public function isAdmin($id)
{
return $id AND in_array($this->role(),['wholesaler','reseller']) AND in_array($id,$this->all_accounts()->pluck('id')->toArray());
}
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
// List all the agents, including agents of agents
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();
}
/** Functions */
public function all_accounts()
{
@@ -253,6 +228,49 @@ class User extends Authenticatable
return $result->flatten();
}
// List all the agents, including agents of agents
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();
}
/**
* Determine if the user is an admin of the account with $id
*
* @param $id
* @return bool
*/
public function isAdmin($id)
{
return $id AND $this->isReseller() AND in_array($id,$this->all_accounts()->pluck('id')->toArray());
}
/**
* Determine if the logged in user is a reseller or wholesaler
*
* @return bool
*/
public function isReseller()
{
return in_array($this->role(),['wholesaler','reseller']);
}
public function role()
{
// If I have agents and no parent, I am the wholesaler
@@ -267,4 +285,8 @@ class User extends Authenticatable
elseif (! $this->all_agents()->count() AND ! $this->all_clients()->count())
return 'customer';
}
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
}