hasMany(Models\Account::class);
}
public function agents() {
return $this->hasMany(static::class,'parent_id','id')->with('agents');
}
public function clients() {
return $this->hasMany(static::class,'parent_id','id')->with('clients');
}
public function language()
{
return $this->belongsTo(Models\Language::class);
}
public function invoices()
{
return $this->hasManyThrough(Models\Invoice::class,Models\Account::class);
}
public function payments()
{
return $this->hasManyThrough(Models\Payment::class,Models\Account::class);
}
public function services()
{
return $this->hasManyThrough(Models\Service::class,Models\Account::class);
}
protected function supplier()
{
return $this->belongsTo(static::class,'parent_id','id');
}
protected function suppliers() {
return $this->hasMany(static::class,'parent_id','id');
}
/** Attributes **/
public function getActiveDisplayAttribute($value)
{
return sprintf('%s',$this->active ? 'primary' : 'danger',$this->active ? 'Active' : 'Inactive');
}
/**
* Logged in users full name
*
* @return string
*/
public function getFullNameAttribute()
{
return sprintf('%s %s',$this->firstname,$this->lastname);
}
public function getInvoicesDueAttribute()
{
return $this->invoices
->where('active',TRUE)
->sortBy('id')
->transform(function ($item) { if ($item->due > 0) return $item; })
->reverse()
->filter();
}
public function getLanguageAttribute($value)
{
if (is_null($this->language_id))
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
->sortBy('date_payment')
->reverse();
}
public function getServicesActiveAttribute()
{
return $this->services
->where('active',TRUE);
}
public function getServicesCountHtmlAttribute()
{
return sprintf('%s /%s',$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('',$this->id);
}
public function getUserIdAttribute()
{
return sprintf('%02s-%04s',$this->site_id,$this->id);
}
public function getUserIdUrlAttribute()
{
return sprintf('%s',$this->id,$this->user_id);
}
/** Scopes **/
public function scopeActive()
{
return $this->where('active',TRUE);
}
/** Functions */
public function all_accounts()
{
$result = collect();
foreach ($this->all_clients() as $o)
{
$result->push($o->accounts->where('active',TRUE));
}
return $result->flatten();
}
public function all_clients($level=0)
{
$result = collect();
foreach ($this->clients 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();
}
// 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
if (is_null($this->parent_id) AND ($this->all_agents()->count() OR $this->all_clients()->count()))
return 'wholesaler';
// 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';
// If I have no agents and a parent, I am a customer
elseif (! $this->all_agents()->count() AND ! $this->all_clients()->count())
return 'customer';
}
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
}