Next/Future invoices for users
This commit is contained in:
208
app/User.php
208
app/User.php
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Models\Site;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -12,32 +11,38 @@ use Laravel\Passport\HasApiTokens;
|
||||
use Leenooks\Carbon;
|
||||
use Leenooks\Traits\UserSwitch;
|
||||
use App\Notifications\ResetPassword as ResetPasswordNotification;
|
||||
use App\Models\Site;
|
||||
use App\Models\Service;
|
||||
use Spinen\QuickBooks\HasQuickBooksToken;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens,Notifiable,UserSwitch,HasQuickBooksToken;
|
||||
use HasApiTokens,Notifiable,UserSwitch,HasQuickBooksToken;
|
||||
|
||||
protected $dates = ['created_at','updated_at','last_access'];
|
||||
protected $dates = [
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'last_access'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name', 'email', 'password',
|
||||
];
|
||||
/**
|
||||
* 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',
|
||||
];
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'active_display',
|
||||
@@ -57,49 +62,92 @@ class User extends Authenticatable
|
||||
'user_id_url',
|
||||
];
|
||||
|
||||
/**
|
||||
* The accounts that this user manages
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function accounts()
|
||||
{
|
||||
{
|
||||
return $this->hasMany(Models\Account::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The agents that this users manages
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function agents() {
|
||||
return $this->hasMany(static::class,'parent_id','id')->with('agents');
|
||||
}
|
||||
|
||||
/**
|
||||
* The clients that this user has
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function clients() {
|
||||
return $this->hasMany(static::class,'parent_id','id')->with('clients');
|
||||
return $this
|
||||
->hasMany(static::class,'parent_id','id')
|
||||
->with('clients');
|
||||
}
|
||||
|
||||
/**
|
||||
* This users language configuration
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function language()
|
||||
{
|
||||
return $this->belongsTo(Models\Language::class);
|
||||
}
|
||||
|
||||
/**b
|
||||
* This users invoices
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function invoices()
|
||||
{
|
||||
return $this->hasManyThrough(Models\Invoice::class,Models\Account::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* The payments this user has made
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function payments()
|
||||
{
|
||||
return $this->hasManyThrough(Models\Payment::class,Models\Account::class);
|
||||
}
|
||||
|
||||
public function site()
|
||||
{
|
||||
return $this->belongsTo(Site::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* THe services this user has
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function services()
|
||||
{
|
||||
return $this->hasManyThrough(Models\Service::class,Models\Account::class);
|
||||
return $this->hasManyThrough(Models\Service::class,Models\Account::class)
|
||||
->with(['account','product','invoices.items.tax','type']);
|
||||
}
|
||||
|
||||
/**
|
||||
* This users supplier/reseller
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
protected function supplier()
|
||||
{
|
||||
return $this->belongsTo(static::class,'parent_id','id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Who this user supplies to
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
protected function suppliers() {
|
||||
return $this->hasMany(static::class,'parent_id','id');
|
||||
}
|
||||
@@ -116,11 +164,16 @@ class User extends Authenticatable
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFullNameAttribute()
|
||||
public function getFullNameAttribute(): string
|
||||
{
|
||||
return sprintf('%s %s',$this->firstname,$this->lastname);
|
||||
}
|
||||
|
||||
/**
|
||||
* A list of all invoices currently unpaid
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getInvoicesDueAttribute()
|
||||
{
|
||||
return $this->invoices
|
||||
@@ -131,9 +184,17 @@ class User extends Authenticatable
|
||||
->filter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the users language
|
||||
*
|
||||
* For non logged in users we need to populate with a default language
|
||||
* @param $value
|
||||
* @return mixed
|
||||
* @todo This doesnt appear to be used?
|
||||
*/
|
||||
public function getLanguageAttribute($value)
|
||||
{
|
||||
if (is_null($this->language_id))
|
||||
if (is_null($value))
|
||||
return config('SITE_SETUP')->language;
|
||||
}
|
||||
|
||||
@@ -141,7 +202,8 @@ class User extends Authenticatable
|
||||
* Return a Carbon Date if it has a value.
|
||||
*
|
||||
* @param $value
|
||||
* @return \Leenooks\Carbon
|
||||
* @return Carbon
|
||||
* @throws \Exception
|
||||
* @todo This attribute is not in the schema
|
||||
*/
|
||||
public function getLastAccessAttribute($value)
|
||||
@@ -159,6 +221,12 @@ class User extends Authenticatable
|
||||
return $this->full_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of the payments that the user has made
|
||||
*
|
||||
* @return mixed
|
||||
* @todo Merge this with payments()
|
||||
*/
|
||||
public function getPaymentHistoryAttribute()
|
||||
{
|
||||
return $this->payments
|
||||
@@ -166,11 +234,18 @@ class User extends Authenticatable
|
||||
->reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* The users active services
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getServicesActiveAttribute()
|
||||
{
|
||||
return $this->services->filter(function($item) {
|
||||
return $item->isActive();
|
||||
});
|
||||
return $this->services
|
||||
->filter(function($item)
|
||||
{
|
||||
return $item->isActive();
|
||||
});
|
||||
}
|
||||
|
||||
public function getServicesCountHtmlAttribute()
|
||||
@@ -198,6 +273,11 @@ class User extends Authenticatable
|
||||
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->user_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Users password reset email notification
|
||||
*
|
||||
* @param string $token
|
||||
*/
|
||||
public function sendPasswordResetNotification($token)
|
||||
{
|
||||
$this->notify((new ResetPasswordNotification($token))->onQueue('high'));
|
||||
@@ -213,7 +293,7 @@ class User extends Authenticatable
|
||||
/**
|
||||
* Search for a record
|
||||
*
|
||||
* @param $query
|
||||
* @param $query
|
||||
* @param string $term
|
||||
* @return
|
||||
*/
|
||||
@@ -253,12 +333,12 @@ class User extends Authenticatable
|
||||
* @param $id
|
||||
* @return bool
|
||||
*/
|
||||
public function isAdmin($id)
|
||||
public function isAdmin($id): bool
|
||||
{
|
||||
return $id AND $this->isReseller() AND in_array($id,$this->all_accounts()->pluck('id')->toArray());
|
||||
}
|
||||
|
||||
/** Functions */
|
||||
/** FUNCTIONS */
|
||||
|
||||
/**
|
||||
* Get a list of accounts for the clients of this user
|
||||
@@ -337,13 +417,17 @@ class User extends Authenticatable
|
||||
});
|
||||
}
|
||||
|
||||
// List all the agents, including agents of agents
|
||||
/**
|
||||
* List of all this users agents, recursively
|
||||
*
|
||||
* @param int $level
|
||||
* @return Collection
|
||||
*/
|
||||
public function all_agents($level=0)
|
||||
{
|
||||
$result = collect();
|
||||
|
||||
foreach ($this->agents as $o)
|
||||
{
|
||||
foreach ($this->agents as $o) {
|
||||
if (! $o->active OR ! $o->agents->count())
|
||||
continue;
|
||||
|
||||
@@ -363,16 +447,56 @@ class User extends Authenticatable
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isReseller()
|
||||
public function isReseller(): bool
|
||||
{
|
||||
return in_array($this->role(),['wholesaler','reseller']);
|
||||
}
|
||||
|
||||
public function isWholesaler()
|
||||
/**
|
||||
* Determine if the logged in user is a wholesaler
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isWholesaler(): bool
|
||||
{
|
||||
return in_array($this->role(),['wholesaler']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the items for the next invoice
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function next_invoice_items(bool $future=FALSE): DatabaseCollection
|
||||
{
|
||||
$result = new DatabaseCollection;
|
||||
$this->load([
|
||||
'services.charges',
|
||||
'services.invoice_items'
|
||||
]);
|
||||
|
||||
foreach ($this->services as $o) {
|
||||
if ($future) {
|
||||
if ($o->invoice_next->subDays(config('app.invoice_inadvance'))->isPast())
|
||||
continue;
|
||||
|
||||
} else {
|
||||
if ($o->invoice_next->subDays(config('app.invoice_inadvance'))->isFuture())
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($o->next_invoice_items() as $oo)
|
||||
$result->push($oo);
|
||||
}
|
||||
|
||||
$result->load([
|
||||
'product.descriptions',
|
||||
'service.type',
|
||||
]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function role()
|
||||
{
|
||||
// If I have agents and no parent, I am the wholesaler
|
||||
|
Reference in New Issue
Block a user