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;
|
2018-08-07 04:26:33 +00:00
|
|
|
use App\Notifications\ResetPasswordNotification;
|
2018-08-11 05:09:41 +00:00
|
|
|
use App\Models\Service;
|
2019-06-12 06:25:15 +00:00
|
|
|
use Spinen\QuickBooks\HasQuickBooksToken;
|
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
|
|
|
{
|
2019-06-12 06:25:15 +00:00
|
|
|
use HasApiTokens,Notifiable,UserSwitch,HasQuickBooksToken;
|
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-08-08 23:33:51 +00:00
|
|
|
protected $with = ['accounts.services'];
|
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-17 04:10:40 +00:00
|
|
|
protected $appends = [
|
2018-08-08 23:33:51 +00:00
|
|
|
'active_display',
|
|
|
|
'services_count_html',
|
2018-07-17 04:10:40 +00:00
|
|
|
'surfirstname',
|
2018-08-08 23:33:51 +00:00
|
|
|
'switch_url',
|
2018-07-17 04:10:40 +00:00
|
|
|
'user_id_url',
|
|
|
|
];
|
2018-08-08 23:33:51 +00:00
|
|
|
|
2018-07-17 04:10:40 +00:00
|
|
|
protected $visible = [
|
2018-08-08 23:33:51 +00:00
|
|
|
'active_display',
|
2018-07-17 04:10:40 +00:00
|
|
|
'id',
|
|
|
|
'level',
|
2018-08-08 23:33:51 +00:00
|
|
|
'services_count_html',
|
|
|
|
'switch_url',
|
|
|
|
'surfirstname',
|
2018-07-17 04:10:40 +00:00
|
|
|
'user_id_url',
|
|
|
|
];
|
|
|
|
|
|
|
|
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-17 04:10:40 +00:00
|
|
|
public function agents() {
|
|
|
|
return $this->hasMany(static::class,'parent_id','id')->with('agents');
|
2018-07-13 04:53:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 04:10:40 +00:00
|
|
|
public function clients() {
|
|
|
|
return $this->hasMany(static::class,'parent_id','id')->with('clients');
|
2018-07-13 04:53:44 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
public function language()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Models\Language::class);
|
|
|
|
}
|
|
|
|
|
2018-07-13 04:53:44 +00:00
|
|
|
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-08-09 14:10:51 +00:00
|
|
|
/** Attributes **/
|
|
|
|
|
2018-08-08 23:33:51 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
public function getInvoicesDueAttribute()
|
2018-07-17 04:10:40 +00:00
|
|
|
{
|
2018-08-09 14:10:51 +00:00
|
|
|
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;
|
2018-07-17 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 06:57:49 +00:00
|
|
|
/**
|
|
|
|
* 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-17 04:10:40 +00:00
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
/**
|
|
|
|
* @deprecated Use static::getFullNameAttribute()
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getNameAttribute()
|
2018-08-01 07:09:38 +00:00
|
|
|
{
|
2018-08-09 14:10:51 +00:00
|
|
|
return $this->full_name;
|
2018-08-01 07:09:38 +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-08-11 05:09:41 +00:00
|
|
|
return $this->services->filter(function($item) {
|
|
|
|
return $item->isActive();
|
|
|
|
});
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
public function getServicesCountHtmlAttribute()
|
2018-07-13 04:53:44 +00:00
|
|
|
{
|
2018-08-09 14:10:51 +00:00
|
|
|
return sprintf('%s <small>/%s</small>',$this->services->where('active',TRUE)->count(),$this->services->count());
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
public function getSurFirstNameAttribute()
|
2018-08-08 23:33:51 +00:00
|
|
|
{
|
2018-08-09 14:10:51 +00:00
|
|
|
return sprintf('%s, %s',$this->lastname,$this->firstname);
|
2018-08-08 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSwitchUrlAttribute()
|
|
|
|
{
|
|
|
|
return sprintf('<a href="/a/switch/start/%s"><i class="fa fa-external-link"></i></a>',$this->id);
|
|
|
|
}
|
|
|
|
|
2018-07-17 04:10:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
public function sendPasswordResetNotification($token)
|
|
|
|
{
|
|
|
|
$this->notify(new ResetPasswordNotification($token));
|
|
|
|
}
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
/** Scopes **/
|
|
|
|
|
2018-08-07 04:26:33 +00:00
|
|
|
public function scopeActive()
|
|
|
|
{
|
|
|
|
return $this->where('active',TRUE);
|
|
|
|
}
|
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
}
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
/** Functions */
|
2018-07-17 04:10:40 +00:00
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
public function all_accounts()
|
2018-07-06 06:57:49 +00:00
|
|
|
{
|
|
|
|
$result = collect();
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
foreach ($this->all_clients() as $o)
|
2018-07-06 06:57:49 +00:00
|
|
|
{
|
2018-08-09 14:10:51 +00:00
|
|
|
$result->push($o->accounts->where('active',TRUE));
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-29 00:14:12 +00:00
|
|
|
// Include my accounts
|
|
|
|
$result->push($this->accounts);
|
|
|
|
|
2018-07-06 06:57:49 +00:00
|
|
|
return $result->flatten();
|
|
|
|
}
|
2018-08-23 05:17:26 +00:00
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
public function all_clients($level=0)
|
2018-07-06 06:57:49 +00:00
|
|
|
{
|
2018-07-17 04:10:40 +00:00
|
|
|
$result = collect();
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
foreach ($this->clients as $o)
|
2018-07-17 04:10:40 +00:00
|
|
|
{
|
2018-08-09 14:10:51 +00:00
|
|
|
if (! $o->active)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$o->level = $level;
|
|
|
|
|
2019-06-02 05:35:48 +00:00
|
|
|
$result->push($o);
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
// Include clients of agents
|
|
|
|
$result->push($o->all_clients($level+1));
|
2018-07-17 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result->flatten();
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 02:46:16 +00:00
|
|
|
public function all_client_service_inactive()
|
|
|
|
{
|
|
|
|
$s = Service::InActive();
|
|
|
|
$aa = $this->all_accounts()->pluck('id')->unique()->toArray();
|
|
|
|
|
|
|
|
return $s->get()->filter(function($item) use ($aa) {
|
|
|
|
return in_array($item->account_id,$aa);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-11 05:09:41 +00:00
|
|
|
public function all_client_service_movements()
|
|
|
|
{
|
|
|
|
$s = Service::active()->where('order_status','!=','ACTIVE');
|
|
|
|
$aa = $this->all_accounts()->pluck('id')->unique()->toArray();
|
|
|
|
|
|
|
|
return $s->get()->filter(function($item) use ($aa) {
|
|
|
|
return in_array($item->account_id,$aa);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
// List all the agents, including agents of agents
|
|
|
|
public function all_agents($level=0)
|
2018-07-06 06:57:49 +00:00
|
|
|
{
|
2018-07-17 04:10:40 +00:00
|
|
|
$result = collect();
|
2018-07-06 06:57:49 +00:00
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
foreach ($this->agents as $o)
|
2018-07-17 04:10:40 +00:00
|
|
|
{
|
2018-08-09 14:10:51 +00:00
|
|
|
if (! $o->active OR ! $o->agents->count())
|
2018-07-17 04:10:40 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
$o->level = $level;
|
|
|
|
|
|
|
|
$result->push($o);
|
|
|
|
|
2018-08-09 14:10:51 +00:00
|
|
|
// Include agents of agents
|
|
|
|
$result->push($o->all_agents($level+1));
|
2018-07-17 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result->flatten();
|
|
|
|
}
|
2018-08-09 14:10:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the logged in user is a reseller or wholesaler
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isReseller()
|
|
|
|
{
|
|
|
|
return in_array($this->role(),['wholesaler','reseller']);
|
|
|
|
}
|
|
|
|
|
2018-08-23 05:17:26 +00:00
|
|
|
public function isWholesaler()
|
|
|
|
{
|
|
|
|
return in_array($this->role(),['wholesaler']);
|
|
|
|
}
|
|
|
|
|
2018-07-06 06:57:49 +00:00
|
|
|
public function role()
|
|
|
|
{
|
|
|
|
// If I have agents and no parent, I am the wholesaler
|
2018-08-08 23:33:51 +00:00
|
|
|
if (is_null($this->parent_id) AND ($this->all_agents()->count() OR $this->all_clients()->count()))
|
2018-07-31 04:11:00 +00:00
|
|
|
return 'wholesaler';
|
2018-07-06 06:57:49 +00:00
|
|
|
|
|
|
|
// If I have agents and a parent, I am a reseller
|
2018-08-08 23:33:51 +00:00
|
|
|
elseif ($this->parent_id AND ($this->all_agents()->count() OR $this->all_clients()->count()))
|
2018-07-31 04:11:00 +00:00
|
|
|
return 'reseller';
|
2018-07-06 06:57:49 +00:00
|
|
|
|
|
|
|
// If I have no agents and a parent, I am a customer
|
2018-08-08 23:33:51 +00:00
|
|
|
elseif (! $this->all_agents()->count() AND ! $this->all_clients()->count())
|
2018-07-31 04:11:00 +00:00
|
|
|
return 'customer';
|
2018-07-06 06:57:49 +00:00
|
|
|
}
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|