Home screen improvements, testing for role, work on user/account models

This commit is contained in:
Deon George
2022-04-21 14:41:26 +10:00
parent 40d12b906b
commit 796c72dd09
18 changed files with 528 additions and 241 deletions

View File

@@ -24,25 +24,17 @@ class Account extends Model implements IDs
{
use HasFactory,ScopeActive;
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
/* INTERFACES */
protected $appends = [
'active_display',
'name',
'services_count_html',
'switch_url',
];
public function getLIDAttribute(): string
{
return sprintf('%04s',$this->id);
}
public $dateFormat = 'U';
protected $visible = [
'id',
'active_display',
'name',
'services_count_html',
'switch_url',
];
public function getSIDAttribute(): string
{
return sprintf('%02s-%s',$this->site_id,$this->getLIDAttribute());
}
/* RELATIONS */
@@ -135,6 +127,7 @@ class Account extends Model implements IDs
public function getActiveDisplayAttribute($value)
{
abort(500,'deprecated');
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'success' : 'danger',$this->active ? 'Active' : 'Inactive');
}
@@ -143,6 +136,7 @@ class Account extends Model implements IDs
*/
public function getAccountIdAttribute()
{
abort(500,'deprecated');
return $this->getAIDAttribute();
}
@@ -151,6 +145,7 @@ class Account extends Model implements IDs
*/
public function getAccountIdUrlAttribute()
{
abort(500,'deprecated');
return $this->getUrlAdminAttribute();
}
@@ -175,41 +170,29 @@ class Account extends Model implements IDs
*/
public function getAIDAttribute()
{
abort(500,'deprecated');
return $this->getSIDAttribute();
}
/**
* Account Local ID
* Return the account name
*
* @return string
* @return mixed|string
*/
public function getLIDAttribute(): string
{
return sprintf('%04s',$this->id);
}
public function getNameAttribute()
public function getNameAttribute(): string
{
return $this->company ?: ($this->user_id ? $this->user->SurFirstName : 'AID:'.$this->id);
}
public function getServicesCountHtmlAttribute()
{
abort(500,'deprecated');
return sprintf('%s <small>/%s</small>',$this->services()->noEagerLoads()->where('active',TRUE)->count(),$this->services()->noEagerLoads()->count());
}
/**
* Account System ID
*
* @return string
*/
public function getSIDAttribute(): string
{
return sprintf('%02s-%s',$this->site_id,$this->getLIDAttribute());
}
public function getSwitchUrlAttribute()
{
abort(500,'deprecated');
return sprintf('<a href="/r/switch/start/%s"><i class="fas fa-external-link-alt"></i></a>',$this->user_id);
}
@@ -225,6 +208,7 @@ class Account extends Model implements IDs
*/
public function getUrlAdminAttribute(): string
{
abort(500,'deprecated');
return sprintf('<a href="/r/account/view/%s">%s</a>',$this->id,$this->account_id);
}
@@ -235,6 +219,7 @@ class Account extends Model implements IDs
*/
public function getUrlUserAttribute(): string
{
abort(500,'deprecated');
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->account_id);
}

View File

@@ -2,10 +2,25 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Rtm extends Model
{
protected $table = 'ab_rtm';
use HasFactory;
protected $table = 'rtm';
public $timestamps = FALSE;
/* RELATIONS */
/**
* Subordinate RTM entries
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function children()
{
return $this->hasMany(self::class,'parent_id');
}
}

View File

@@ -18,6 +18,7 @@ use Leenooks\Carbon;
use Symfony\Component\HttpKernel\Exception\HttpException;
use App\Interfaces\IDs;
use App\Traits\ScopeServiceUserAuthorised;
/**
* Class Service
@@ -46,7 +47,7 @@ use App\Interfaces\IDs;
// @todo All the methods/attributes in this file need to be checked.
class Service extends Model implements IDs
{
use HasFactory;
use HasFactory,ScopeServiceUserAuthorised;
protected $appends = [
'account_name',
@@ -86,15 +87,11 @@ class Service extends Model implements IDs
'status',
];
/*
protected $with = [
'account.language',
'charges',
'invoice_items',
'product',
'product.type.supplied',
'type',
];
*/
// @todo Change to self::INACTIVE_STATUS
private $inactive_status = [
@@ -413,15 +410,6 @@ class Service extends Model implements IDs
});
}
/**
* Only query records that the user is authorised to see
*/
public function scopeAuthorised($query,User $uo)
{
return $query
->whereIN($this->getTable().'.account_id',$uo->all_accounts()->pluck('id')->unique()->toArray());
}
/**
* Find inactive services.
*
@@ -1345,6 +1333,7 @@ class Service extends Model implements IDs
* @return Collection
* @throws Exception
* @todo Use self::isBilled();
* @todo This query is expensive.
*/
public function next_invoice_items(bool $future,Carbon $billdate=NULL): Collection
{

View File

@@ -2,29 +2,32 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Laravel\Passport\HasApiTokens;
use Leenooks\Carbon;
use Leenooks\Traits\UserSwitch;
use App\Notifications\ResetPassword as ResetPasswordNotification;
use App\Traits\SiteID;
use App\Traits\{QueryCacheableConfig,SiteID};
/**
* Class User
*
* Attributes for users:
* + role : User's role
*/
class User extends Authenticatable
{
use HasApiTokens,Notifiable,UserSwitch,SiteID;
use HasFactory,HasApiTokens,Notifiable,UserSwitch,QueryCacheableConfig,SiteID;
protected $appends = [
'active_display',
'services_count_html',
'surfirstname',
'switch_url',
'user_id_url',
];
private const CACHE_TIME = 3600;
protected $dates = [
'created_at',
@@ -51,18 +54,6 @@ class User extends Authenticatable
'remember_token',
];
protected $visible = [
'active_display',
'id',
'level',
'services_count_html',
'switch_url',
'surfirstname',
'user_id_url',
];
protected $with = ['accounts'];
/**
* Role hierarchy order
* @var array
@@ -79,10 +70,14 @@ class User extends Authenticatable
* The accounts that this user manages
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @note This cannot be loaded with "with"?
*/
public function accounts()
{
return $this->hasMany(Account::class);
return $this->hasMany(Account::class)
->orWhereIn('id',$this->rtm_accounts()->pluck('id'))
->active()
->with(['services']);
}
/**
@@ -136,6 +131,16 @@ class User extends Authenticatable
return $this->hasManyThrough(Payment::class,Account::class);
}
/**
* Return the routes to market account for this user
*
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
*/
public function rtm()
{
return $this->hasOneThrough(Rtm::class,Account::class);
}
/**
* THe services this user has
*
@@ -181,6 +186,7 @@ class User extends Authenticatable
public function getActiveDisplayAttribute($value)
{
abort(500,'deprecated:'.__METHOD__);
return sprintf('<span class="btn-sm btn-block btn-%s text-center">%s</span>',$this->active ? 'primary' : 'danger',$this->active ? 'Active' : 'Inactive');
}
@@ -223,6 +229,16 @@ class User extends Authenticatable
return new Carbon($value);
}
/**
* Return my accounts
*
* @return Collection
*/
public function getMyAccountsAttribute(): Collection
{
return $this->accounts->where('user_id',$this->id);
}
/**
* @deprecated Use static::getFullNameAttribute()
* @return mixed
@@ -245,8 +261,18 @@ class User extends Authenticatable
->reverse();
}
/**
* Return a friendly string of this persons role
* @return string
*/
public function getRoleAttribute(): string
{
return ucfirst($this->role());
}
public function getServicesCountHtmlAttribute()
{
abort(500,'deprecated:'.__METHOD__);
return sprintf('%s <small>/%s</small>',$this->services->where('active',TRUE)->count(),$this->services->count());
}
@@ -257,16 +283,19 @@ class User extends Authenticatable
public function getSwitchUrlAttribute()
{
abort(500,'deprecated:'.__METHOD__);
return sprintf('<a href="/a/switch/start/%s"><i class="fas fa-external-link-alt"></i></a>',$this->id);
}
public function getUserIdAttribute()
{
abort(500,'deprecated:'.__METHOD__);
return sprintf('%02s-%04s',$this->site_id,$this->id);
}
public function getUserIdUrlAttribute()
{
abort(500,'deprecated:'.__METHOD__);
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->user_id);
}
@@ -327,7 +356,7 @@ class User extends Authenticatable
return $query;
}
/* GENERAL METHODS */
/* METHODS */
/**
* Determine if the user is an admin of the user with $id
@@ -337,16 +366,19 @@ class User extends Authenticatable
*/
public function isAdmin($id): bool
{
return $id AND $this->isReseller() AND in_array($id,$this->all_accounts()->pluck('user_id')->toArray());
return $id AND $this->isReseller() AND $this->accounts->pluck('user_id')->contains($id);
}
/**
* Get a list of accounts for the clients of this user
*
* @return DatabaseCollection
* @deprecated Use rtm_accounts()
*/
public function all_accounts(): DatabaseCollection
{
throw new \Exception('deprecated');
abort(500,'deprecated:'.__METHOD__);
$result = new DatabaseCollection();
$clients = $this->all_clients();
@@ -376,7 +408,9 @@ class User extends Authenticatable
* Get a list of clients that this user is responsible for.
*
* @param int $level
* @return Collection
* @param DatabaseCollection|null $clients
* @return DatabaseCollection
* @deprecated Use rtm_accounts() to determine this
*/
public function all_clients($level=0,DatabaseCollection $clients=NULL): DatabaseCollection
{
@@ -398,6 +432,10 @@ class User extends Authenticatable
return $result;
}
/**
* @return mixed
* @deprecated Use rtm_accounts() to determine this list
*/
public function all_client_service_inactive()
{
$s = Service::InActive();
@@ -413,6 +451,7 @@ class User extends Authenticatable
*
* @param int $level
* @return Collection
* @deprecated Use rtm_accounts()
*/
public function all_agents($level=0)
{
@@ -443,7 +482,7 @@ class User extends Authenticatable
public function client_service_movements(): DatabaseCollection
{
return Service::active()
->authorised($this)
->serviceUserAuthorised($this)
->where('order_status','!=','ACTIVE')
->with(['account','product'])
->get();
@@ -558,7 +597,7 @@ class User extends Authenticatable
])
->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())
->whereIN('account_id',$this->accounts->pluck('id'))
->where('ab_invoice.active',TRUE)
->groupBy(['invoice_id']);
@@ -574,7 +613,7 @@ class User extends Authenticatable
])
->from($this->query_payment_items(),'PI')
->join('payments',['payments.id'=>'PI.payment_id'])
->whereIN('account_id',$this->all_accounts()->pluck('id')->unique()->toArray())
->whereIN('account_id',$this->accounts->pluck('id'))
//->where('payments.active',TRUE) // @todo To implement
->groupBy(['invoice_id']);
@@ -647,16 +686,67 @@ class User extends Authenticatable
*/
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() OR $this->all_clients()->count()))
return 'wholesaler';
// Cache our role for this session
$cache_key = sprintf('%s:%s:%s',$this->id,__METHOD__,Session::getId());
// If I have agents and a parent, I am a reseller
elseif ($this->parent_id AND ($this->all_agents()->count() OR $this->all_clients()->count()))
return 'reseller';
return Cache::remember($cache_key,self::CACHE_TIME,function() {
// Get the RTM for our accounts
$rtms = Rtm::whereIn('account_id',$this->accounts->pluck('id'))->get();
// If I have no agents and a parent, I am a customer
elseif (! $this->all_agents()->count() AND ! $this->all_clients()->count())
return 'customer';
// If I have no parent, I am the wholesaler
if ($rtms->whereNull('parent_id')->count())
return 'wholesaler';
// If I exist in the RTM table, I'm a reseller
else if ($rtms->count())
return 'reseller';
// Otherwise a client
else
return 'customer';
});
}
/**
* Return the accounts that this user can manage
* This method is a helper to User::accounts() - use $user->accounts instead
*
* @return Collection
*/
private function rtm_accounts(): Collection
{
return Account::whereIn('rtm_id',$this->rtm_list()->pluck('id'))
->get();
}
/**
* Return the RTM hierarchy that this user can manage
*
* @param Rtm|null $rtm
* @return Collection
*/
public function rtm_list(Rtm $rtm=NULL): Collection
{
// If this user doesnt manage any accounts
if (! $this->exists || ! $this->rtm)
return collect();
$list = collect();
// Add this RTM to the list
if (! $rtm) {
$list->push($this->rtm);
$children = $this->rtm->children;
} else {
$list->push($rtm);
$children =$rtm->children;
}
// Capture any children
foreach ($children as $child)
$list->push($this->rtm_list($child));
return $rtm ? $list : $list->flatten()->unique(function($item) { return $item->id; });
}
}