Work in progress determining user type
This commit is contained in:
169
app/User.php
169
app/User.php
@@ -2,17 +2,17 @@
|
||||
|
||||
namespace App;
|
||||
|
||||
use Laravel\Passport\HasApiTokens;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Leenooks\Carbon;
|
||||
use App\Models\Account;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
protected $table = 'ab_account';
|
||||
// @todo We cannot use timestamps - we should create the normal timestamps columns under laravel.
|
||||
public $timestamps = FALSE;
|
||||
use Notifiable;
|
||||
|
||||
use HasApiTokens, Notifiable;
|
||||
protected $dates = ['created_at','updated_at','last_access'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
@@ -32,81 +32,100 @@ class User extends Authenticatable
|
||||
'password', 'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Return the country the user belongs to
|
||||
*/
|
||||
public function country()
|
||||
|
||||
public function accounts()
|
||||
{
|
||||
return $this->belongsTo(Models\Country::class);
|
||||
return $this->hasMany(Models\Account::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* This users invoices
|
||||
*/
|
||||
public function invoices()
|
||||
{
|
||||
return $this->hasMany(Models\Invoice::class,'account_id');
|
||||
}
|
||||
/**
|
||||
* Logged in users full name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFullNameAttribute()
|
||||
{
|
||||
return sprintf('%s %s',$this->firstname,$this->lastname);
|
||||
}
|
||||
|
||||
public function language()
|
||||
{
|
||||
return $this->belongsTo(Models\Language::class);
|
||||
}
|
||||
/**
|
||||
* 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 payments()
|
||||
{
|
||||
return $this->hasMany(Models\Payment::class,'account_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* This users invoices
|
||||
*/
|
||||
public function services()
|
||||
{
|
||||
return $this->hasMany(Models\Service::class,'account_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Only query active categories
|
||||
*/
|
||||
public function scopeActive()
|
||||
{
|
||||
return $this->where('active',TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the user's full name
|
||||
*/
|
||||
public function getFullNameAttribute()
|
||||
{
|
||||
return $this->first_name.' '.$this->last_name;
|
||||
}
|
||||
|
||||
public function getInvoicesDueAttribute()
|
||||
{
|
||||
return $this->invoices
|
||||
->where('active',TRUE)
|
||||
->sortBy('id')
|
||||
->transform(function ($item) { if ((float) $item->due) return $item; })
|
||||
->reverse()
|
||||
->filter();
|
||||
}
|
||||
|
||||
public function getPaymentHistoryAttribute()
|
||||
{
|
||||
return $this->payments
|
||||
->sortBy('date_payment')
|
||||
->reverse();
|
||||
}
|
||||
|
||||
public function getNameAttribute()
|
||||
{
|
||||
/**
|
||||
* @deprecated Use static::getFullNameAttribute()
|
||||
* @return mixed
|
||||
*/
|
||||
public function getNameAttribute()
|
||||
{
|
||||
return $this->full_name;
|
||||
}
|
||||
}
|
||||
|
||||
public function getServicesActiveAttribute()
|
||||
{
|
||||
return $this->services
|
||||
->where('active',TRUE);
|
||||
}
|
||||
protected function agents() {
|
||||
return $this->hasMany(static::class,'parent_id','id');
|
||||
}
|
||||
|
||||
protected function clients() {
|
||||
return $this->hasMany(\App\User::class);
|
||||
}
|
||||
|
||||
protected function supplier()
|
||||
{
|
||||
return $this->belongsTo(static::class,'parent_id','id');
|
||||
}
|
||||
|
||||
protected function suppliers() {
|
||||
return $this->hasMany(static::class,'parent_id','id');
|
||||
}
|
||||
|
||||
// List all the agents, including agents of agents
|
||||
public function all_agents()
|
||||
{
|
||||
$result = collect();
|
||||
|
||||
foreach ($this->agents()->orderBy('id')->get() as $o)
|
||||
{
|
||||
if (! $o->active)
|
||||
continue;
|
||||
|
||||
$result->push($o->all_agents());
|
||||
$result->push($this);
|
||||
}
|
||||
|
||||
return $result->flatten();
|
||||
}
|
||||
|
||||
public function all_clients()
|
||||
{
|
||||
// List all the clients of my agents
|
||||
}
|
||||
|
||||
public function all_suppliers()
|
||||
{
|
||||
// For each supplier, so if that supplier has a parent
|
||||
}
|
||||
|
||||
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())
|
||||
return 'Wholesaler';
|
||||
|
||||
// If I have agents and a parent, I am a reseller
|
||||
elseif ($this->parent_id AND $this->all_agents()->count())
|
||||
return 'Reseller';
|
||||
|
||||
// If I have no agents and a parent, I am a customer
|
||||
elseif (! $this->all_agents()->count())
|
||||
return 'Customer';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user