Added Reseller view

This commit is contained in:
Deon George
2018-07-17 14:10:40 +10:00
parent 1821810570
commit 0ce640c283
12 changed files with 308 additions and 28 deletions

View File

@@ -14,6 +14,7 @@ class User extends Authenticatable
use HasApiTokens,Notifiable,UserSwitch;
protected $dates = ['created_at','updated_at','last_access'];
protected $with = ['accounts'];
/**
* The attributes that are mass assignable.
@@ -33,17 +34,28 @@ class User extends Authenticatable
'password', 'remember_token',
];
public function accounts()
protected $appends = [
'surfirstname',
'user_id_url',
];
protected $visible = [
'id',
'surfirstname',
'level',
'user_id_url',
];
public function accounts()
{
return $this->hasMany(Models\Account::class);
}
protected function agents() {
return $this->hasMany(static::class,'parent_id','id');
public function agents() {
return $this->hasMany(static::class,'parent_id','id')->with('agents');
}
protected function clients() {
return $this->hasMany(\App\User::class);
public function clients() {
return $this->hasMany(static::class,'parent_id','id')->with('clients');
}
public function invoices()
@@ -80,6 +92,11 @@ 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.
*
@@ -92,6 +109,7 @@ class User extends Authenticatable
if (! is_null($value))
return new Carbon($value);
}
public function getInvoicesDueAttribute()
{
return $this->invoices
@@ -124,33 +142,73 @@ class User extends Authenticatable
return $this->full_name;
}
public function getUserIdAttribute()
{
return sprintf('%02s-%04s',$this->site_id,$this->id);
}
public function getUserIdUrlAttribute()
{
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->user_id);
}
public function scopeActive()
{
return $this->where('active',TRUE);
}
// List all the agents, including agents of agents
public function all_agents()
public function all_agents($level=0)
{
$result = collect();
foreach ($this->agents()->orderBy('id')->get() as $o)
foreach ($this->agents as $o)
{
if (! $o->active)
if (! $o->active OR ! $o->agents->count())
continue;
$result->push($o->all_agents());
$result->push($this);
$o->level = $level;
$result->push($o);
// Include agents of agents
$result->push($o->all_agents($level+1));
}
return $result->flatten();
}
public function all_clients()
public function all_accounts()
{
// List all the clients of my agents
$result = collect();
foreach ($this->all_clients() as $o)
{
$result->push($o->accounts->where('active',TRUE));
}
return $result->flatten();
}
public function all_suppliers()
public function all_clients($level=0)
{
// For each supplier, so if that supplier has a parent
}
$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();
}
public function role()
{
// If I have agents and no parent, I am the wholesaler