osb/app/Models/User.php

662 lines
15 KiB
PHP
Raw Normal View History

2017-11-03 05:26:07 +00:00
<?php
2021-06-29 03:18:52 +00:00
namespace App\Models;
2017-11-03 05:26:07 +00:00
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
2020-07-21 06:39:23 +00:00
use Illuminate\Support\Facades\DB;
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;
2019-06-12 06:25:15 +00:00
use Spinen\QuickBooks\HasQuickBooksToken;
2017-11-03 05:26:07 +00:00
2020-07-21 06:39:23 +00:00
use App\Notifications\ResetPassword as ResetPasswordNotification;
2021-12-17 05:09:03 +00:00
use App\Traits\SiteID;
2020-07-21 06:39:23 +00:00
class User extends Authenticatable
2017-11-03 05:26:07 +00:00
{
2021-12-17 05:09:03 +00:00
use HasApiTokens,Notifiable,UserSwitch,HasQuickBooksToken,SiteID;
2020-02-08 11:51:50 +00:00
2020-02-10 11:07:46 +00:00
protected $appends = [
'active_display',
'services_count_html',
'surfirstname',
'switch_url',
'user_id_url',
];
2020-02-08 11:51:50 +00:00
protected $dates = [
'created_at',
'updated_at',
'last_access'
];
/**
* 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 $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',
];
2020-02-10 11:07:46 +00:00
protected $with = ['accounts'];
2020-04-18 22:33:41 +00:00
/**
* Role hierarchy order
* @var array
*/
public static $role_order = [
'wholesaler',
'reseller',
'customer',
];
/* RELATIONS */
2020-02-08 11:51:50 +00:00
/**
* The accounts that this user manages
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2018-07-17 04:10:40 +00:00
public function accounts()
2020-02-08 11:51:50 +00:00
{
2021-06-29 03:18:52 +00:00
return $this->hasMany(Account::class);
2020-02-08 11:51:50 +00:00
}
2018-05-20 12:53:14 +00:00
2020-02-08 11:51:50 +00:00
/**
* The agents that this users manages
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
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
}
2020-02-08 11:51:50 +00:00
/**
* The clients that this user has
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2018-07-17 04:10:40 +00:00
public function clients() {
2020-02-08 11:51:50 +00:00
return $this
->hasMany(static::class,'parent_id','id')
->with('clients');
2018-07-13 04:53:44 +00:00
}
2020-02-08 11:51:50 +00:00
/**
* This users language configuration
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2018-08-01 07:09:38 +00:00
public function language()
{
2021-06-29 03:18:52 +00:00
return $this->belongsTo(Language::class);
2018-08-01 07:09:38 +00:00
}
2020-04-14 07:40:47 +00:00
/**
2020-02-08 11:51:50 +00:00
* This users invoices
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
2018-07-13 04:53:44 +00:00
public function invoices()
{
2021-06-29 06:36:34 +00:00
return $this->hasManyThrough(Invoice::class,Account::class)
->active();
2018-07-13 04:53:44 +00:00
}
2020-02-08 11:51:50 +00:00
/**
* The payments this user has made
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
2018-07-13 04:53:44 +00:00
public function payments()
{
2021-06-29 03:18:52 +00:00
return $this->hasManyThrough(Payment::class,Account::class);
2018-07-13 04:53:44 +00:00
}
2020-02-08 11:51:50 +00:00
/**
* THe services this user has
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
2018-07-13 04:53:44 +00:00
public function services()
{
2021-06-29 06:36:34 +00:00
return $this->hasManyThrough(Service::class,Account::class)
->active();
2018-07-13 04:53:44 +00:00
}
/**
* The site this user is configured to access
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function site()
{
return $this->belongsTo(Site::class);
}
2020-02-08 11:51:50 +00:00
/**
* This users supplier/reseller
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2018-07-13 04:53:44 +00:00
protected function supplier()
{
return $this->belongsTo(static::class,'parent_id','id');
}
2020-02-08 11:51:50 +00:00
/**
* Who this user supplies to
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2018-07-13 04:53:44 +00:00
protected function suppliers() {
return $this->hasMany(static::class,'parent_id','id');
}
/* ATTRIBUTES */
2018-08-09 14:10:51 +00:00
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
*/
2020-02-08 11:51:50 +00:00
public function getFullNameAttribute(): string
2018-07-06 06:57:49 +00:00
{
return sprintf('%s %s',$this->firstname,$this->lastname);
}
2020-02-08 11:51:50 +00:00
/**
* A list of all invoices currently unpaid
*
* @return mixed
*/
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();
}
2018-07-06 06:57:49 +00:00
/**
* Return a Carbon Date if it has a value.
*
* @param $value
2020-02-08 11:51:50 +00:00
* @return Carbon
* @throws \Exception
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
}
2020-02-08 11:51:50 +00:00
/**
* Return a list of the payments that the user has made
*
* @return mixed
* @todo Merge this with payments()
*/
2018-07-13 04:53:44 +00:00
public function getPaymentHistoryAttribute()
{
return $this->payments
->sortBy('payment_date')
2018-07-13 04:53:44 +00:00
->reverse();
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()
{
2021-06-29 03:18:52 +00:00
return sprintf('<a href="/a/switch/start/%s"><i class="fas fa-external-link-alt"></i></a>',$this->id);
2018-08-08 23:33:51 +00:00
}
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);
}
/* METHODS */
2020-02-08 11:51:50 +00:00
/**
* Users password reset email notification
*
* @param string $token
*/
2018-08-11 05:09:41 +00:00
public function sendPasswordResetNotification($token)
{
2020-01-30 10:39:25 +00:00
$this->notify((new ResetPasswordNotification($token))->onQueue('high'));
2018-08-11 05:09:41 +00:00
}
/* SCOPES */
2018-08-09 14:10:51 +00:00
2021-06-29 03:18:52 +00:00
// @todo use trait
2018-08-07 04:26:33 +00:00
public function scopeActive()
{
return $this->where('active',TRUE);
}
/**
* Search for a record
*
2020-02-08 11:51:50 +00:00
* @param $query
* @param string $term
* @return
*/
public function scopeSearch($query,string $term)
{
// Build our where clause
// First Name, Last name
if (preg_match('/\ /',$term)) {
2020-04-14 07:40:47 +00:00
[$fn,$ln] = explode(' ',$term,2);
$query->where(function($query1) use ($fn,$ln,$term) {
$query1->where(function($query2) use ($fn,$ln) {
return $query2
->where('firstname','like','%'.$fn.'%')
->where('lastname','like','%'.$ln.'%');
});
});
} elseif (is_numeric($term)) {
$query->where('id','like','%'.$term.'%');
} elseif (preg_match('/\@/',$term)) {
$query->where('email','like','%'.$term.'%');
} else {
$query
->Where('firstname','like','%'.$term.'%')
->orWhere('lastname','like','%'.$term.'%');
}
return $query;
}
/* GENERAL METHODS */
2018-08-11 05:09:41 +00:00
/**
* Determine if the user is an admin of the user with $id
2018-08-11 05:09:41 +00:00
*
* @param $id
* @return bool
*/
2020-02-08 11:51:50 +00:00
public function isAdmin($id): bool
2018-08-11 05:09:41 +00:00
{
return $id AND $this->isReseller() AND in_array($id,$this->all_accounts()->pluck('user_id')->toArray());
2018-08-11 05:09:41 +00:00
}
/**
* Get a list of accounts for the clients of this user
*
* @return DatabaseCollection
*/
public function all_accounts(): DatabaseCollection
2018-07-06 06:57:49 +00:00
{
$result = new DatabaseCollection();
$clients = $this->all_clients();
2018-07-06 06:57:49 +00:00
foreach ($clients->pluck('accounts') as $accounts) {
foreach ($accounts as $o) {
if (! $o->active)
continue;
$result->push($o);
}
2018-07-06 06:57:49 +00:00
}
2019-06-29 00:14:12 +00:00
// Include my accounts
foreach ($this->accounts as $o) {
if (! $o->active)
continue;
$result->push($o);
}
2019-06-29 00:14:12 +00:00
$result->load('user.accounts');
return $result;
2018-07-06 06:57:49 +00:00
}
2018-08-23 05:17:26 +00:00
/**
* Get a list of clients that this user is responsible for.
*
* @param int $level
* @return Collection
*/
public function all_clients($level=0,DatabaseCollection $clients=NULL): DatabaseCollection
2018-07-06 06:57:49 +00:00
{
$result = is_null($clients) ? $this->clients : $clients;
2018-07-17 04:10:40 +00:00
$result
->filter(function($item) { return $item->active; })
->transform(function($item) use ($level) { $item->level = $level; return $item; });
2018-08-09 14:10:51 +00:00
foreach ($result->pluck('clients') as $clients) {
foreach ($this->all_clients($level+1,$clients) as $o) {
if (! $o->active)
continue;
2019-06-02 05:35:48 +00:00
$result->push($o);
}
2018-07-17 04:10:40 +00:00
}
return $result;
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);
});
}
2020-02-08 11:51:50 +00:00
/**
* List of all this users agents, recursively
*
* @param int $level
* @return Collection
*/
2018-08-09 14:10:51 +00:00
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
2020-02-08 11:51:50 +00:00
foreach ($this->agents as $o) {
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
2020-04-14 07:40:47 +00:00
/**
* Show this user's clients with service movements
*
* A service movement, is an active service where the status is not ACTIVE
*
* @return DatabaseCollection
*/
public function client_service_movements(): DatabaseCollection
{
return Service::active()
->authorised($this)
2020-04-14 07:40:47 +00:00
->where('order_status','!=','ACTIVE')
->with(['account','product'])
->get();
}
2018-08-09 14:10:51 +00:00
/**
* Determine if the logged in user is a reseller or wholesaler
*
* @return bool
*/
2020-02-08 11:51:50 +00:00
public function isReseller(): bool
2018-08-09 14:10:51 +00:00
{
return in_array($this->role(),['wholesaler','reseller']);
}
2020-02-08 11:51:50 +00:00
/**
* Determine if the logged in user is a wholesaler
*
* @return bool
*/
public function isWholesaler(): bool
2018-08-23 05:17:26 +00:00
{
return in_array($this->role(),['wholesaler']);
}
2020-02-08 11:51:50 +00:00
/**
* Get all the items for the next invoice
*
2020-04-14 07:40:47 +00:00
* @param bool $future
* @return DatabaseCollection
2020-02-08 11:51:50 +00:00
*/
public function next_invoice_items(bool $future=FALSE): DatabaseCollection
{
$result = new DatabaseCollection;
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;
}
2020-02-12 10:32:57 +00:00
foreach ($o->next_invoice_items($future) as $oo)
2020-02-08 11:51:50 +00:00
$result->push($oo);
}
$result->load([
'product.description',
2020-02-08 11:51:50 +00:00
'service.type',
]);
return $result;
}
2020-07-21 06:39:23 +00:00
/**
* Return an SQL query that will return a list of invoices
*
* @return \Illuminate\Database\Query\Builder
*/
private function query_invoice_items()
{
return DB::table('ab_invoice_item')
->select([
'invoice_id',
DB::raw('ab_invoice_item.id AS invoice_item_id'),
DB::raw('IFNULL(ab_invoice_item.discount_amt,0) AS discount'),
DB::raw('ROUND(CAST(quantity*price_base AS decimal(8,2)),2) AS base'),
DB::raw('ROUND(ab_invoice_item_tax.amount,2) AS tax'),
])
2021-06-29 06:36:34 +00:00
->leftjoin('ab_invoice_item_tax',['ab_invoice_item_tax.invoice_item_id'=>'ab_invoice_item.id'])
2020-07-21 06:39:23 +00:00
->where('active',TRUE);
}
/**
* Return an SQL query that will return payment summaries by invoices.
*
* @return \Illuminate\Database\Query\Builder
*/
private function query_payment_items()
{
return DB::table('payment_items')
2020-07-21 06:39:23 +00:00
->select([
'payment_id',
'invoice_id',
DB::raw('SUM(alloc_amt) AS allocate'),
])
->where('alloc_amt','>',0)
->groupBy(['invoice_id','payment_id']);
}
/**
* Return an SQL query that will summarise invoices with payments
*
* @return \Illuminate\Database\Query\Builder
2021-06-29 06:36:34 +00:00
* @todo change this to just return outstanding invoices as a collection.
2020-07-21 06:39:23 +00:00
*/
public function query_invoice_summary()
{
$invoices = (new Invoice)
->select([
'invoice_id',
DB::raw('SUM(discount) AS discount'),
DB::raw('SUM(base) AS base'),
DB::raw('SUM(tax) AS tax'),
DB::raw('ROUND(SUM(base)+SUM(tax)-SUM(discount),2) AS total'),
DB::raw('false AS payments'),
DB::raw('false AS payment_fees'),
])
->from($this->query_invoice_items(),'II')
->join('ab_invoice',['ab_invoice.id'=>'II.invoice_id'])
->whereIN('account_id',$this->all_accounts()->pluck('id')->unique()->toArray())
->where('ab_invoice.active',TRUE)
->groupBy(['invoice_id']);
$payments = (new Payment)
->select([
'invoice_id',
DB::raw('false AS discount'),
DB::raw('false AS base'),
DB::raw('false AS tax'),
DB::raw('false AS total'),
DB::raw('SUM(allocate) AS payments'),
DB::raw('SUM(fees_amt) AS payment_fees'),
])
->from($this->query_payment_items(),'PI')
->join('payments',['payments.id'=>'PI.payment_id'])
2020-07-21 06:39:23 +00:00
->whereIN('account_id',$this->all_accounts()->pluck('id')->unique()->toArray())
//->where('payments.active',TRUE) // @todo To implement
2020-07-21 06:39:23 +00:00
->groupBy(['invoice_id']);
$summary = (new Invoice)
->select([
'invoice_id',
DB::raw('SUM(discount) AS discount'),
DB::raw('SUM(base) AS invoice_base'),
DB::raw('SUM(tax) AS invoice_tax'),
DB::raw('SUM(total) AS invoice_total'),
DB::raw('SUM(payments) AS payments'),
DB::raw('SUM(payment_fees) AS payment_fees'),
])
->from($invoices->unionAll($payments),'invoices')
->groupBy(['invoice_id']);
return (new Invoice)
->select([
'account_id',
'id',
'due_date',
'date_orig',
'discount',
'invoice_base',
'invoice_tax',
'invoice_total',
'payments',
'payment_fees',
DB::raw('ROUND(invoice_total-payments,2) AS balance'),
])
->join('ab_invoice',['ab_invoice.id'=>'invoice_id'])
->from($summary,'summary');
}
public function query_payment_summary()
{
$payment = (new Payment)
->select([
DB::raw('payment_id AS id'),
DB::raw('SUM(allocate) AS allocate'),
])
->from($this->query_payment_items(),'PI')
//->where('payments.active',TRUE) // @todo To implement
->groupBy(['payment_id']);
return (new Payment)
->select([
DB::raw('payments.id AS id'),
'date_orig',
'payment_date',
'total_amt',
//'fees_amt',
DB::raw('total_amt-allocate AS balance'),
])
->rightJoin('payments',['payments.id'=>'summary.id'])
//->where('payments.active',TRUE) // @todo To implement
->whereIN('account_id',$this->all_accounts()->pluck('id')->unique()->toArray())
->from($payment,'summary');
}
/**
* Determine what the logged in user's role is
* + Wholesaler - aka Super User
* + Reseller - services accounts on behalf of their customers
* + Customer - end user customer
*
* @return string
*/
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
}
}