2017-11-03 05:26:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2017-12-12 05:28:49 +00:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
2018-07-13 04:53:44 +00:00
|
|
|
use Laravel\Passport\HasApiTokens;
|
|
|
|
|
2018-07-06 06:57:49 +00:00
|
|
|
use Leenooks\Carbon;
|
2018-07-13 04:53:44 +00:00
|
|
|
use Leenooks\Traits\UserSwitch;
|
2017-11-03 05:26:07 +00:00
|
|
|
|
2018-04-10 11:23:13 +00:00
|
|
|
class User extends Authenticatable
|
2017-11-03 05:26:07 +00:00
|
|
|
{
|
2018-07-13 04:53:44 +00:00
|
|
|
use HasApiTokens,Notifiable,UserSwitch;
|
2018-05-20 12:53:14 +00:00
|
|
|
|
2018-07-06 06:57:49 +00:00
|
|
|
protected $dates = ['created_at','updated_at','last_access'];
|
2018-04-10 11:23:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'name', 'email', 'password',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
'password', 'remember_token',
|
|
|
|
];
|
|
|
|
|
2018-07-06 06:57:49 +00:00
|
|
|
public function accounts()
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2018-07-06 06:57:49 +00:00
|
|
|
return $this->hasMany(Models\Account::class);
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 04:53:44 +00:00
|
|
|
protected function agents() {
|
|
|
|
return $this->hasMany(static::class,'parent_id','id');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function clients() {
|
|
|
|
return $this->hasMany(\App\User::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');
|
|
|
|
}
|
|
|
|
|
2018-07-06 06:57:49 +00:00
|
|
|
/**
|
|
|
|
* Logged in users full name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFullNameAttribute()
|
|
|
|
{
|
|
|
|
return sprintf('%s %s',$this->firstname,$this->lastname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a Carbon Date if it has a value.
|
|
|
|
*
|
|
|
|
* @param $value
|
2018-07-13 04:53:44 +00:00
|
|
|
* @return \Leenooks\Carbon
|
2018-07-06 06:57:49 +00:00
|
|
|
* @todo This attribute is not in the schema
|
|
|
|
*/
|
|
|
|
public function getLastAccessAttribute($value)
|
|
|
|
{
|
|
|
|
if (! is_null($value))
|
|
|
|
return new Carbon($value);
|
|
|
|
}
|
2018-07-13 04:53:44 +00:00
|
|
|
public function getInvoicesDueAttribute()
|
2018-07-06 06:57:49 +00:00
|
|
|
{
|
2018-07-13 04:53:44 +00:00
|
|
|
return $this->invoices
|
|
|
|
->where('active',TRUE)
|
|
|
|
->sortBy('id')
|
|
|
|
->transform(function ($item) { if ($item->due) return $item; })
|
|
|
|
->reverse()
|
|
|
|
->filter();
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 04:53:44 +00:00
|
|
|
public function getPaymentHistoryAttribute()
|
|
|
|
{
|
|
|
|
return $this->payments
|
|
|
|
->sortBy('date_payment')
|
|
|
|
->reverse();
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 04:53:44 +00:00
|
|
|
public function getServicesActiveAttribute()
|
2018-07-06 06:57:49 +00:00
|
|
|
{
|
2018-07-13 04:53:44 +00:00
|
|
|
return $this->services
|
|
|
|
->where('active',TRUE);
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 04:53:44 +00:00
|
|
|
/**
|
|
|
|
* @deprecated Use static::getFullNameAttribute()
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getNameAttribute()
|
|
|
|
{
|
|
|
|
return $this->full_name;
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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';
|
|
|
|
}
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|