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

@ -29,26 +29,10 @@ class HomeController extends Controller
*/ */
public function home(User $o): View public function home(User $o): View
{ {
// If we are passed a user to view, we'll open up their home page. if (! $o->exists)
if ($o->exists) {
$o->load(['accounts','services']);
return View('u.home',['o'=>$o]);
}
// If User was null, then test and see what type of logged on user we have
$o = Auth::user(); $o = Auth::user();
switch (Auth::user()->role()) {
case 'customer':
return View('u.home',['o'=>$o]); return View('u.home',['o'=>$o]);
case 'reseller':
case 'wholesaler':
return View('r.home',['o'=>$o]);
default:
abort(404,'Unknown role: '.$o->role());
}
} }
/** /**
@ -126,7 +110,6 @@ class HomeController extends Controller
*/ */
public function service_progress(Service $o,string $status) public function service_progress(Service $o,string $status)
{ {
abort(500,'deprecated');
return redirect()->to($o->action($status) ?: url('u/service',$o->id)); return redirect()->to($o->action($status) ?: url('u/service',$o->id));
} }
} }

View File

@ -24,25 +24,17 @@ class Account extends Model implements IDs
{ {
use HasFactory,ScopeActive; use HasFactory,ScopeActive;
const CREATED_AT = 'date_orig'; /* INTERFACES */
const UPDATED_AT = 'date_last';
protected $appends = [ public function getLIDAttribute(): string
'active_display', {
'name', return sprintf('%04s',$this->id);
'services_count_html', }
'switch_url',
];
public $dateFormat = 'U'; public function getSIDAttribute(): string
{
protected $visible = [ return sprintf('%02s-%s',$this->site_id,$this->getLIDAttribute());
'id', }
'active_display',
'name',
'services_count_html',
'switch_url',
];
/* RELATIONS */ /* RELATIONS */
@ -135,6 +127,7 @@ class Account extends Model implements IDs
public function getActiveDisplayAttribute($value) 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'); 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() public function getAccountIdAttribute()
{ {
abort(500,'deprecated');
return $this->getAIDAttribute(); return $this->getAIDAttribute();
} }
@ -151,6 +145,7 @@ class Account extends Model implements IDs
*/ */
public function getAccountIdUrlAttribute() public function getAccountIdUrlAttribute()
{ {
abort(500,'deprecated');
return $this->getUrlAdminAttribute(); return $this->getUrlAdminAttribute();
} }
@ -175,41 +170,29 @@ class Account extends Model implements IDs
*/ */
public function getAIDAttribute() public function getAIDAttribute()
{ {
abort(500,'deprecated');
return $this->getSIDAttribute(); return $this->getSIDAttribute();
} }
/** /**
* Account Local ID * Return the account name
* *
* @return string * @return mixed|string
*/ */
public function getLIDAttribute(): string public function getNameAttribute(): string
{
return sprintf('%04s',$this->id);
}
public function getNameAttribute()
{ {
return $this->company ?: ($this->user_id ? $this->user->SurFirstName : 'AID:'.$this->id); return $this->company ?: ($this->user_id ? $this->user->SurFirstName : 'AID:'.$this->id);
} }
public function getServicesCountHtmlAttribute() public function getServicesCountHtmlAttribute()
{ {
abort(500,'deprecated');
return sprintf('%s <small>/%s</small>',$this->services()->noEagerLoads()->where('active',TRUE)->count(),$this->services()->noEagerLoads()->count()); 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() 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); 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 public function getUrlAdminAttribute(): string
{ {
abort(500,'deprecated');
return sprintf('<a href="/r/account/view/%s">%s</a>',$this->id,$this->account_id); 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 public function getUrlUserAttribute(): string
{ {
abort(500,'deprecated');
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->account_id); return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->account_id);
} }

View File

@ -2,10 +2,25 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Rtm extends Model class Rtm extends Model
{ {
protected $table = 'ab_rtm'; use HasFactory;
protected $table = 'rtm';
public $timestamps = FALSE; 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 Symfony\Component\HttpKernel\Exception\HttpException;
use App\Interfaces\IDs; use App\Interfaces\IDs;
use App\Traits\ScopeServiceUserAuthorised;
/** /**
* Class Service * Class Service
@ -46,7 +47,7 @@ use App\Interfaces\IDs;
// @todo All the methods/attributes in this file need to be checked. // @todo All the methods/attributes in this file need to be checked.
class Service extends Model implements IDs class Service extends Model implements IDs
{ {
use HasFactory; use HasFactory,ScopeServiceUserAuthorised;
protected $appends = [ protected $appends = [
'account_name', 'account_name',
@ -86,15 +87,11 @@ class Service extends Model implements IDs
'status', 'status',
]; ];
/*
protected $with = [ protected $with = [
'account.language',
'charges',
'invoice_items', 'invoice_items',
'product', 'product.type.supplied',
'type', 'type',
]; ];
*/
// @todo Change to self::INACTIVE_STATUS // @todo Change to self::INACTIVE_STATUS
private $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. * Find inactive services.
* *
@ -1345,6 +1333,7 @@ class Service extends Model implements IDs
* @return Collection * @return Collection
* @throws Exception * @throws Exception
* @todo Use self::isBilled(); * @todo Use self::isBilled();
* @todo This query is expensive.
*/ */
public function next_invoice_items(bool $future,Carbon $billdate=NULL): Collection public function next_invoice_items(bool $future,Carbon $billdate=NULL): Collection
{ {

View File

@ -2,29 +2,32 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection; use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Laravel\Passport\HasApiTokens; use Laravel\Passport\HasApiTokens;
use Leenooks\Carbon; use Leenooks\Carbon;
use Leenooks\Traits\UserSwitch; use Leenooks\Traits\UserSwitch;
use App\Notifications\ResetPassword as ResetPasswordNotification; 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 class User extends Authenticatable
{ {
use HasApiTokens,Notifiable,UserSwitch,SiteID; use HasFactory,HasApiTokens,Notifiable,UserSwitch,QueryCacheableConfig,SiteID;
protected $appends = [ private const CACHE_TIME = 3600;
'active_display',
'services_count_html',
'surfirstname',
'switch_url',
'user_id_url',
];
protected $dates = [ protected $dates = [
'created_at', 'created_at',
@ -51,18 +54,6 @@ class User extends Authenticatable
'remember_token', 'remember_token',
]; ];
protected $visible = [
'active_display',
'id',
'level',
'services_count_html',
'switch_url',
'surfirstname',
'user_id_url',
];
protected $with = ['accounts'];
/** /**
* Role hierarchy order * Role hierarchy order
* @var array * @var array
@ -79,10 +70,14 @@ class User extends Authenticatable
* The accounts that this user manages * The accounts that this user manages
* *
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
* @note This cannot be loaded with "with"?
*/ */
public function accounts() 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 $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 * THe services this user has
* *
@ -181,6 +186,7 @@ class User extends Authenticatable
public function getActiveDisplayAttribute($value) 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'); 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 new Carbon($value);
} }
/**
* Return my accounts
*
* @return Collection
*/
public function getMyAccountsAttribute(): Collection
{
return $this->accounts->where('user_id',$this->id);
}
/** /**
* @deprecated Use static::getFullNameAttribute() * @deprecated Use static::getFullNameAttribute()
* @return mixed * @return mixed
@ -245,8 +261,18 @@ class User extends Authenticatable
->reverse(); ->reverse();
} }
/**
* Return a friendly string of this persons role
* @return string
*/
public function getRoleAttribute(): string
{
return ucfirst($this->role());
}
public function getServicesCountHtmlAttribute() public function getServicesCountHtmlAttribute()
{ {
abort(500,'deprecated:'.__METHOD__);
return sprintf('%s <small>/%s</small>',$this->services->where('active',TRUE)->count(),$this->services->count()); 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() 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); return sprintf('<a href="/a/switch/start/%s"><i class="fas fa-external-link-alt"></i></a>',$this->id);
} }
public function getUserIdAttribute() public function getUserIdAttribute()
{ {
abort(500,'deprecated:'.__METHOD__);
return sprintf('%02s-%04s',$this->site_id,$this->id); return sprintf('%02s-%04s',$this->site_id,$this->id);
} }
public function getUserIdUrlAttribute() public function getUserIdUrlAttribute()
{ {
abort(500,'deprecated:'.__METHOD__);
return sprintf('<a href="/u/account/view/%s">%s</a>',$this->id,$this->user_id); 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; return $query;
} }
/* GENERAL METHODS */ /* METHODS */
/** /**
* Determine if the user is an admin of the user with $id * 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 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 * Get a list of accounts for the clients of this user
* *
* @return DatabaseCollection * @return DatabaseCollection
* @deprecated Use rtm_accounts()
*/ */
public function all_accounts(): DatabaseCollection public function all_accounts(): DatabaseCollection
{ {
throw new \Exception('deprecated');
abort(500,'deprecated:'.__METHOD__);
$result = new DatabaseCollection(); $result = new DatabaseCollection();
$clients = $this->all_clients(); $clients = $this->all_clients();
@ -376,7 +408,9 @@ class User extends Authenticatable
* Get a list of clients that this user is responsible for. * Get a list of clients that this user is responsible for.
* *
* @param int $level * @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 public function all_clients($level=0,DatabaseCollection $clients=NULL): DatabaseCollection
{ {
@ -398,6 +432,10 @@ class User extends Authenticatable
return $result; return $result;
} }
/**
* @return mixed
* @deprecated Use rtm_accounts() to determine this list
*/
public function all_client_service_inactive() public function all_client_service_inactive()
{ {
$s = Service::InActive(); $s = Service::InActive();
@ -413,6 +451,7 @@ class User extends Authenticatable
* *
* @param int $level * @param int $level
* @return Collection * @return Collection
* @deprecated Use rtm_accounts()
*/ */
public function all_agents($level=0) public function all_agents($level=0)
{ {
@ -443,7 +482,7 @@ class User extends Authenticatable
public function client_service_movements(): DatabaseCollection public function client_service_movements(): DatabaseCollection
{ {
return Service::active() return Service::active()
->authorised($this) ->serviceUserAuthorised($this)
->where('order_status','!=','ACTIVE') ->where('order_status','!=','ACTIVE')
->with(['account','product']) ->with(['account','product'])
->get(); ->get();
@ -558,7 +597,7 @@ class User extends Authenticatable
]) ])
->from($this->query_invoice_items(),'II') ->from($this->query_invoice_items(),'II')
->join('ab_invoice',['ab_invoice.id'=>'II.invoice_id']) ->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) ->where('ab_invoice.active',TRUE)
->groupBy(['invoice_id']); ->groupBy(['invoice_id']);
@ -574,7 +613,7 @@ class User extends Authenticatable
]) ])
->from($this->query_payment_items(),'PI') ->from($this->query_payment_items(),'PI')
->join('payments',['payments.id'=>'PI.payment_id']) ->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 //->where('payments.active',TRUE) // @todo To implement
->groupBy(['invoice_id']); ->groupBy(['invoice_id']);
@ -647,16 +686,67 @@ class User extends Authenticatable
*/ */
public function role() public function role()
{ {
// If I have agents and no parent, I am the wholesaler // Cache our role for this session
if (is_null($this->parent_id) AND ($this->all_agents()->count() OR $this->all_clients()->count())) $cache_key = sprintf('%s:%s:%s',$this->id,__METHOD__,Session::getId());
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 parent, I am the wholesaler
if ($rtms->whereNull('parent_id')->count())
return 'wholesaler'; return 'wholesaler';
// If I have agents and a parent, I am a reseller // If I exist in the RTM table, I'm a reseller
elseif ($this->parent_id AND ($this->all_agents()->count() OR $this->all_clients()->count())) else if ($rtms->count())
return 'reseller'; return 'reseller';
// If I have no agents and a parent, I am a customer // Otherwise a client
elseif (! $this->all_agents()->count() AND ! $this->all_clients()->count()) else
return 'customer'; 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; });
} }
} }

View File

@ -0,0 +1,17 @@
<?php
/**
* Set defaults of QueryCacheable
*/
namespace App\Traits;
use Rennokki\QueryCache\Traits\QueryCacheable;
trait QueryCacheableConfig
{
use QueryCacheable;
public $cacheFor = 3600*6; // cache time, in seconds
protected static $flushCacheOnUpdate = TRUE;
public $cacheDriver = 'memcached';
}

View File

@ -16,6 +16,6 @@ trait ScopeServiceUserAuthorised
public function scopeServiceUserAuthorised($query,User $uo) public function scopeServiceUserAuthorised($query,User $uo)
{ {
return $query return $query
->whereIN('services.account_id',$uo->all_accounts()->pluck('id')->unique()->toArray()); ->whereIN('services.account_id',$uo->accounts->pluck('id'));
} }
} }

View File

@ -0,0 +1,27 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Rtm>
*/
class RtmFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'id' => $this->faker->numberBetween(2048,65535),
//* 'site_id', // Needs to be passed in
//* 'account_id', // Needs to be passed in
// 'name',
//* 'parent_id', // Needs to be passed in
];
}
}

View File

@ -1,23 +1,49 @@
<?php <?php
use Faker\Generator as Faker; namespace Database\Factories;
/* use App\Models\Country;
|-------------------------------------------------------------------------- use App\Models\Language;
| Model Factories use Illuminate\Database\Eloquent\Factories\Factory;
|-------------------------------------------------------------------------- use Illuminate\Support\Str;
|
| This directory should contain each of the model factory definitions for /**
| your application. Factories provide a convenient way to generate new * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
| model instances for testing / seeding your application's database. */
| class UserFactory extends Factory
*/ {
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
// Create Dependencies - should be loaded by seeding.
$co = Country::findOrFail(61);
$lo = Language::findOrFail(1);
$factory->define(App\Models\User::class, function (Faker $faker) {
return [ return [
'name' => $faker->name, 'id' => $this->faker->numberBetween(2048,65535),
'email' => $faker->unique()->safeEmail, // 'created_at'
// 'updated_at'
//* 'site_id', // Needs to be passed in
'email' => $this->faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10), 'remember_token' => Str::random(10),
'active' => 1,
// 'title'
'firstname' => $this->faker->name,
'lastname' => $this->faker->name,
'country_id' => $co->id,
// 'address1'
// 'address2'
// 'city'
// 'state'
// 'postcode'
'emailable' => true,
// 'parent_id''
'language_id' => $lo->id,
]; ];
}); }
}

View File

@ -0,0 +1,48 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('accounts', function (Blueprint $table) {
$table->datetime('created_at')->nullable()->after('id');
$table->datetime('updated_at')->nullable()->after('created_at');
$table->date('expire_at')->nullable()->after('updated_at');
});
// Convert our dates
foreach (\App\Models\Account::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
if ($o->date_orig)
$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
if ($o->date_last)
$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
if ($o->date_expire)
$o->expire_at = \Carbon\Carbon::createFromTimestamp($o->date_expire);
$o->save();
}
Schema::table('accounts', function (Blueprint $table) {
$table->dropColumn(['date_orig','date_last','date_expire']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
abort(500,'Cant go back');
}
};

View File

@ -1,17 +1,17 @@
@if ($o->accounts->count() > 1) @if ($o->accounts->count() > 1)
<div class="col-sm-2"> <div class="col-12 col-sm-4 col-md-3">
<div class="info-box"> <div class="info-box">
<span class="info-box-icon bg-primary elevation-1"><i class="fas fa-user"></i></span> <span class="info-box-icon bg-primary elevation-1"><i class="fas fa-user"></i></span>
<div class="info-box-content"> <div class="info-box-content">
<span class="info-box-text">Linked Accounts</span> <span class="info-box-text">Linked Accounts</span>
<span class="info-box-number">{{ number_format($o->accounts->count()) }}</span> <span class="info-box-number">{{ number_format($o->my_accounts->count()) }}</span>
</div> </div>
</div> </div>
</div> </div>
@endif @endif
<div class="col-sm-2"> <div class="col-12 col-sm-4 col-md-3">
<div class="info-box"> <div class="info-box">
<span class="info-box-icon bg-info"><i class="fas fa-clone"></i></span> <span class="info-box-icon bg-info"><i class="fas fa-clone"></i></span>
@ -23,7 +23,7 @@
</div> </div>
</div> </div>
<div class="col-sm-2"> <div class="col-12 col-sm-4 col-md-3">
<div class="info-box"> <div class="info-box">
<span class="info-box-icon bg-danger"><i class="fas fa-dollar-sign"></i></span> <span class="info-box-icon bg-danger"><i class="fas fa-dollar-sign"></i></span>
@ -34,7 +34,7 @@
</div> </div>
</div> </div>
<div class="col-sm-2"> <div class="col-12 col-sm-4 col-md-3">
<div class="info-box"> <div class="info-box">
<span class="info-box-icon bg-dark"><i class="fas fa-hashtag"></i></span> <span class="info-box-icon bg-dark"><i class="fas fa-hashtag"></i></span>

View File

@ -4,21 +4,29 @@
</div> </div>
<div class="card-body"> <div class="card-body">
@if ($user->all_accounts()->count()) @if ($user->accounts->count())
<table class="table table-striped table-hover" id="accounts" style="width: 100%;"> <table class="table table-striped table-hover" id="accounts">
<thead> <thead>
<tr> <tr>
<th>Profile</th> <th>Profile</th>
<th>Name</th> <th>Name</th>
<th>Active</th> <th class="text-right">Services</th>
<th>Services</th>
</tr> </tr>
</thead> </thead>
<tbody>
@foreach ($user->accounts as $ao)
<tr>
<td><a href="{{ url('r/switch/start',$ao->user_id) }}"><i class="fas fa-external-link-alt"></i></a></td>
<td>{{ $ao->name }}</td>
<td class="text-right">{{ $ao->services->where('active',TRUE)->count() }} <small>/{{ $ao->services->count() }}</small></td>
</tr>
@endforeach
</tbody>
<tfoot> <tfoot>
<tr> <tr>
<th>Count {{ $user->all_accounts()->count() }}</th> <th>Count {{ $user->accounts->count() }}</th>
<th colspan="3">&nbsp;</th> <th colspan="2">&nbsp;</th>
</tr> </tr>
</tfoot> </tfoot>
</table> </table>
@ -35,21 +43,12 @@
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
$('#accounts').DataTable( { $('#accounts').DataTable({
ajax: {
url: "/api/r/accounts"
},
columns: [
{ data: "switch_url" },
{ data: "name" },
{ data: "active_display" },
{ data: "services_count_html" }
],
language: { language: {
emptyTable: "No Active Clients" emptyTable: "No Active Clients"
}, },
order: [1, 'asc'], order: [1,'asc'],
pageLength: 25 pageLength: 10
}); });
$('#accounts tbody').on('click','tr', function () { $('#accounts tbody').on('click','tr', function () {

View File

@ -1,77 +0,0 @@
@extends('adminlte::layouts.app')
@section('htmlheader_title')
Reseller Home
@endsection
@section('page_title')
{{ $o->full_name }}
@endsection
@section('contentheader_title')
{{ $o->full_name }}
@endsection
@section('contentheader_description')
Reseller Home
@endsection
@section('main-content')
<div class="row">
@include('common.account.widget.summary')
</div>
<div class="row">
<div class="col-md-12">
<div class="card-header bg-white">
<div class="card-header p-2">
<ul class="nav nav-pills">
<li class="nav-item"><a class="nav-link active" href="#tab-services" data-toggle="tab">Services</a></li>
<li class="nav-item"><a class="nav-link" href="#tab-clients" data-toggle="tab">Clients</a></li>
</ul>
</div>
</div>
<div class="card-body pl-0 pr-0">
<div class="tab-content">
<div class="active tab-pane" id="tab-services">
<div class="row">
<div class="col-7">
@include('u.service.widgets.active')
</div>
<div class="col-5">
@include('u.invoice.widgets.due')
@include('u.invoice.widgets.list')
@include('u.payment.widgets.list')
</div>
</div>
</div>
<div class="tab-pane" id="tab-clients">
<div class="row">
<div class="col-4">
@include('r.account.widgets.list')
</div>
<div class="col-8">
@include('r.service.widgets.movement')
@include('r.invoice.widgets.due')
</div>
{{--
<div class="col-xs-6">
@include('r.agents')
</div>
<div class="col-xs-6">
@include('r.clients')
</div>
--}}
</div>
</div>
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,18 @@
<div class="row">
<div class="col-4">
@include('r.account.widgets.list')
</div>
<div class="col-8">
@include('r.service.widgets.movement')
@include('r.invoice.widgets.due')
</div>
{{--
<div class="col-xs-6">
@include('r.agents')
</div>
<div class="col-xs-6">
@include('r.clients')
</div>
--}}
</div>

View File

@ -1,7 +1,7 @@
@extends('adminlte::layouts.app') @extends('adminlte::layouts.app')
@section('htmlheader_title') @section('htmlheader_title')
Client Home {{ $o->role }} Home
@endsection @endsection
@section('page_title') @section('page_title')
{{ $o->full_name }} {{ $o->full_name }}
@ -11,10 +11,11 @@
{{ $o->full_name }} {{ $o->full_name }}
@endsection @endsection
@section('contentheader_description') @section('contentheader_description')
Client Home {{ $o->role }}
@endsection @endsection
@section('main-content') @section('main-content')
<!-- Our Summary Home Page Boxes -->
<div class="row"> <div class="row">
@include('common.account.widget.summary') @include('common.account.widget.summary')
</div> </div>
@ -24,8 +25,17 @@
<div class="card-header bg-white"> <div class="card-header bg-white">
<ul class="nav nav-pills"> <ul class="nav nav-pills">
<li class="nav-item"><a class="nav-link active" href="#tab-services" data-toggle="tab">Services</a></li> <li class="nav-item"><a class="nav-link active" href="#tab-services" data-toggle="tab">Services</a></li>
{{--
<!-- @todo this is not working -->
<li class="nav-item"><a class="nav-link" href="#tab-nextinvoice" data-toggle="tab">Next Invoice</a></li> <li class="nav-item"><a class="nav-link" href="#tab-nextinvoice" data-toggle="tab">Next Invoice</a></li>
--}}
<li class="nav-item"><a class="nav-link" href="#tab-futureinvoice" data-toggle="tab">Future Invoice</a></li> <li class="nav-item"><a class="nav-link" href="#tab-futureinvoice" data-toggle="tab">Future Invoice</a></li>
@if ($o == $user)
@canany('reseller','wholesaler')
<li class="nav-item ml-auto"><a class="nav-link" href="#tab-reseller" data-toggle="tab">Reseller</a></li>
@endcanany
@endif
</ul> </ul>
</div> </div>
@ -33,11 +43,11 @@
<div class="tab-content"> <div class="tab-content">
<div class="active tab-pane" id="tab-services"> <div class="active tab-pane" id="tab-services">
<div class="row"> <div class="row">
<div class="col-7"> <div class="col-12 col-xl-7">
@include('u.service.widgets.active') @include('u.service.widgets.active')
</div> </div>
<div class="col-5"> <div class="col-12 col-xl-5">
@include('u.invoice.widgets.due') @include('u.invoice.widgets.due')
@include('u.invoice.widgets.list') @include('u.invoice.widgets.list')
@ -47,6 +57,8 @@
</div> </div>
</div> </div>
{{--
<!-- @todo this is not working -->
<div class="tab-pane" id="tab-nextinvoice"> <div class="tab-pane" id="tab-nextinvoice">
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
@ -54,16 +66,39 @@
</div> </div>
</div> </div>
</div> </div>
--}}
<div class="tab-pane" id="tab-futureinvoice"> <div class="tab-pane" id="tab-futureinvoice">
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12 col-xl-9">
@include('u.invoice.widgets.next',['future'=>TRUE]) @include('u.invoice.widgets.next',['future'=>TRUE])
</div> </div>
</div> </div>
</div> </div>
@if ($o == $user)
@canany('reseller','wholesaler')
<div class="tab-pane" id="tab-reseller">
@include('r.home.widgets.home')
</div>
@endcanany
@endif
</div> </div>
</div> </div>
</div> </div>
</div> </div>
@endsection @endsection
@section('page-scripts')
<script>
$(document).ready(function() {
$('body')
.addClass('sidebar-collapse')
.delay(500)
.queue(function () {
window.dispatchEvent(new Event('resize'));
$(this).dequeue();
});
});
</script>
@append

View File

@ -12,7 +12,7 @@
@foreach ($oo->groupBy('service_id') as $ooo) @foreach ($oo->groupBy('service_id') as $ooo)
<tr> <tr>
<td class="pt-0 pb-1" style="width: 8em;"><a href="{{ url('u/service',$ooo->first()->service_id) }}">{{ $ooo->first()->service->sid }}</a></td> <td class="pt-0 pb-1" style="width: 12em;"><a href="{{ url('u/service',$ooo->first()->service_id) }}">{{ $ooo->first()->service->sid }}</a></td>
<td class="pt-0 pb-1" colspan="3">{{ $ooo->first()->service->sname }}: {{ $ooo->first()->service->sdesc }}</td> <td class="pt-0 pb-1" colspan="3">{{ $ooo->first()->service->sname }}: {{ $ooo->first()->service->sdesc }}</td>
</tr> </tr>

View File

@ -59,7 +59,7 @@
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
$('#services_active').DataTable( { $('#services_active').DataTable({
order: [[1,'asc'],[2,'asc']], order: [[1,'asc'],[2,'asc']],
rowGroup: { rowGroup: {
dataSrc: 1, dataSrc: 1,

View File

@ -0,0 +1,132 @@
<?php
namespace Tests\Feature;
use App\Models\{Account,Rtm,Site,User};
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Tests\TestCase;
class AccountRoleTest extends TestCase
{
use DatabaseTransactions;
private Collection $setup;
private function init()
{
$this->setup = collect();
$this->setup->put('user',collect());
$this->setup->put('account',collect());
// Create our sites
$this->setup->put('site',[
'a'=>Site::factory()->create(['url'=>'Test A']),
'b'=>Site::factory()->create(['url'=>'Test B']),
]);
// For each site, create 9 accounts - 1 wholesaler, 2 resellers, 6 accounts
foreach ($this->setup['site'] as $key => $so) {
$this->setup->get('user')->put($key,
[
'w'=>User::factory()->create(['firstname'=>'Wholesaler','site_id'=>$so->site_id]),
'r1'=>User::factory()->create(['firstname'=>'Reseller 1','site_id'=>$so->site_id]),
'r2'=>User::factory()->create(['firstname'=>'Reseller 1-2','site_id'=>$so->site_id]),
'a1-1'=>User::factory()->create(['firstname'=>'Account 1-1','site_id'=>$so->site_id]),
'a1-2'=>User::factory()->create(['firstname'=>'Account 1-2','site_id'=>$so->site_id]),
'a1-3'=>User::factory()->create(['firstname'=>'Account 1-2','site_id'=>$so->site_id]),
'a2-1'=>User::factory()->create(['firstname'=>'Account 2-1','site_id'=>$so->site_id]),
'a2-2'=>User::factory()->create(['firstname'=>'Account 2-2','site_id'=>$so->site_id]),
'a2-3'=>User::factory()->create(['firstname'=>'Account 2-2','site_id'=>$so->site_id]),
]
);
$us = $this->setup->get('user');
$this->setup->get('account')->put($key,
[
'w'=>Account::factory()->create(['company'=>'Wholesaler','site_id'=>$so->site_id,'user_id'=>Arr::get($us,sprintf('%s.%s',$key,'w'))->id]),
'r1'=>Account::factory()->create(['company'=>'Reseller 1','site_id'=>$so->site_id,'user_id'=>Arr::get($us,sprintf('%s.%s',$key,'r1'))->id]),
'r2'=>Account::factory()->create(['company'=>'Reseller 1-2','site_id'=>$so->site_id,'user_id'=>Arr::get($us,sprintf('%s.%s',$key,'r2'))->id]),
]
);
$as = $this->setup->get('account');
// Setup the RTM to support
$w = Rtm::factory()->create(['name'=>'Wholesaler','site_id'=>$so->site_id,'account_id'=>Arr::get($as,sprintf('%s.%s',$key,'w'))->id,'parent_id'=>null]);
$r1 = Rtm::factory()->create(['name'=>'Reseller 1','site_id'=>$so->site_id,'account_id'=>Arr::get($as,sprintf('%s.%s',$key,'r1'))->id,'parent_id'=>$w->id]);
$r2 = Rtm::factory()->create(['name'=>'Reseller 1-2','site_id'=>$so->site_id,'account_id'=>Arr::get($as,sprintf('%s.%s',$key,'r2'))->id,'parent_id'=>$r1->id]);
// Update our RTM for the reseller accounts
Arr::get($as,sprintf('%s.%s',$key,'r1'))->forceFill(['rtm_id'=>$w->id])->save();
Arr::get($as,sprintf('%s.%s',$key,'r2'))->forceFill(['rtm_id'=>$r1->id])->save();
$this->setup->get('account')->put($key,array_merge($this->setup->get('account')->get($key),
[
'a1-1'=>Account::factory()->create(['company'=>'Account 1-1','site_id'=>$so->site_id,'rtm_id'=>$r1->id]),
'a1-2'=>Account::factory()->create(['company'=>'Account 1-2','site_id'=>$so->site_id,'rtm_id'=>$r1->id]),
'a1-3'=>Account::factory()->create(['company'=>'Account 1-3','site_id'=>$so->site_id,'rtm_id'=>$r1->id]),
'a2-1'=>Account::factory()->create(['company'=>'Account 2-1','site_id'=>$so->site_id,'rtm_id'=>$r2->id]),
'a2-2'=>Account::factory()->create(['company'=>'Account 2-2','site_id'=>$so->site_id,'rtm_id'=>$r2->id]),
'a2-3'=>Account::factory()->create(['company'=>'Account 2-3','site_id'=>$so->site_id,'rtm_id'=>$r2->id]),
]
));
}
}
/**
* Test our roles work correctly
*
* @return void
*/
public function test_account_role()
{
// Setup
$this->init();
// For each site
foreach ($this->setup['site'] as $site => $so) {
// Foreach user
foreach ($this->setup['user'] as $user => $uo) {
if ($site !== $user)
continue;
// Check our roles
$this->assertEquals('wholesaler',$uo['w']->role());
$this->assertEquals('reseller',$uo['r1']->role());
$this->assertEquals('reseller',$uo['r2']->role());
$this->assertEquals('customer',$uo['a1-1']->role());
$this->assertEquals('customer',$uo['a1-2']->role());
$this->assertEquals('customer',$uo['a1-3']->role());
$this->assertEquals('customer',$uo['a2-1']->role());
$this->assertEquals('customer',$uo['a2-2']->role());
$this->assertEquals('customer',$uo['a2-3']->role());
// Check that accounts do not have an RTM
$this->assertNull($uo['a1-1']->rtm);
$this->assertNull($uo['a1-2']->rtm);
$this->assertNull($uo['a1-3']->rtm);
$this->assertNull($uo['a2-1']->rtm);
$this->assertNull($uo['a2-2']->rtm);
$this->assertNull($uo['a2-3']->rtm);
// Check that the RTM exists for resellers and wholesalers
$this->assertModelExists($uo['r1']->rtm);
$this->assertModelExists($uo['r2']->rtm);
$this->assertModelExists($uo['w']->rtm);
// Check the hierarchy
$this->assertEquals(3,$uo['w']->rtm_list()->count());
$this->assertEquals(2,$uo['r1']->rtm_list()->count());
$this->assertEquals(1,$uo['r2']->rtm_list()->count());
// Check that the users get the right amount of accounts
$this->assertEquals(9,$uo['w']->accounts->count());
$this->assertEquals(8,$uo['r1']->accounts->count());
$this->assertEquals(4,$uo['r2']->accounts->count());
}
}
}
}