Added Reseller view

This commit is contained in:
Deon George 2018-07-17 14:10:40 +10:00
parent 1821810570
commit 0ce640c283
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
12 changed files with 308 additions and 28 deletions

View File

@ -0,0 +1,18 @@
<?php
namespace App\Http\Controllers;
use Auth;
class ResellerServicesController extends Controller
{
public function agents()
{
return ['data'=>Auth::user()->all_agents()->values()];
}
public function accounts()
{
return ['data'=>Auth::user()->all_accounts()->values()];
}
}

View File

@ -15,13 +15,13 @@ class UserHomeController extends Controller
{ {
switch (Auth::user()->role()) { switch (Auth::user()->role()) {
case 'Customer': case 'Customer':
return View('home'); return View('userhome',['o'=>Auth::user()]);
case 'Reseller': case 'Reseller':
break; return View('resellerhome',['o'=>Auth::user()]);
case 'Wholesaler': case 'Wholesaler':
break; return View('resellerhome',['o'=>Auth::user()]);
default: default:
abort(500,'Unknown role: ',Auth::user()->role()); abort(500,'Unknown role: ',Auth::user()->role());

View File

@ -2,7 +2,6 @@
namespace App\Http; namespace App\Http;
use App\Http\Middleware\SetSite;
use Illuminate\Foundation\Http\Kernel as HttpKernel; use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel class Kernel extends HttpKernel
@ -62,6 +61,7 @@ class Kernel extends HttpKernel
'can' => \Illuminate\Auth\Middleware\Authorize::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class,
'demoMode' => \Spatie\DemoMode\DemoMode::class, 'demoMode' => \Spatie\DemoMode\DemoMode::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'reseller' => \App\Http\Middleware\Reseller::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'theme' => \Igaster\LaravelTheme\Middleware\setTheme::class, 'theme' => \Igaster\LaravelTheme\Middleware\setTheme::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

View File

@ -0,0 +1,19 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Closure;
class Reseller
{
public function handle($request, Closure $next)
{
if (! in_array(Auth::user()->role(),['Wholesaler','Reseller']))
{
abort(303,'Not Reseller');
} else
return $next($request);
}
}

View File

@ -9,6 +9,15 @@ class Account extends Model
protected $table = 'ab_account'; protected $table = 'ab_account';
public $timestamps = FALSE; public $timestamps = FALSE;
protected $appends = [
'active_display',
];
protected $visible = [
'id',
'company',
'active_display',
];
/** /**
* Return the country the user belongs to * Return the country the user belongs to
*/ */
@ -26,4 +35,14 @@ class Account extends Model
{ {
return $this->belongsTo(\App\User::class); return $this->belongsTo(\App\User::class);
} }
public function getCompanyAttribute($value)
{
return $value ? $value : $this->user->SurFirstName;
}
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');
}
} }

View File

@ -14,6 +14,7 @@ class User extends Authenticatable
use HasApiTokens,Notifiable,UserSwitch; use HasApiTokens,Notifiable,UserSwitch;
protected $dates = ['created_at','updated_at','last_access']; protected $dates = ['created_at','updated_at','last_access'];
protected $with = ['accounts'];
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
@ -33,17 +34,28 @@ class User extends Authenticatable
'password', 'remember_token', 'password', 'remember_token',
]; ];
protected $appends = [
'surfirstname',
'user_id_url',
];
protected $visible = [
'id',
'surfirstname',
'level',
'user_id_url',
];
public function accounts() public function accounts()
{ {
return $this->hasMany(Models\Account::class); return $this->hasMany(Models\Account::class);
} }
protected function agents() { public function agents() {
return $this->hasMany(static::class,'parent_id','id'); return $this->hasMany(static::class,'parent_id','id')->with('agents');
} }
protected function clients() { public function clients() {
return $this->hasMany(\App\User::class); return $this->hasMany(static::class,'parent_id','id')->with('clients');
} }
public function invoices() public function invoices()
@ -80,6 +92,11 @@ class User extends Authenticatable
return sprintf('%s %s',$this->firstname,$this->lastname); return sprintf('%s %s',$this->firstname,$this->lastname);
} }
public function getSurFirstNameAttribute()
{
return sprintf('%s, %s',$this->lastname,$this->firstname);
}
/** /**
* Return a Carbon Date if it has a value. * Return a Carbon Date if it has a value.
* *
@ -92,6 +109,7 @@ class User extends Authenticatable
if (! is_null($value)) if (! is_null($value))
return new Carbon($value); return new Carbon($value);
} }
public function getInvoicesDueAttribute() public function getInvoicesDueAttribute()
{ {
return $this->invoices return $this->invoices
@ -124,33 +142,73 @@ class User extends Authenticatable
return $this->full_name; return $this->full_name;
} }
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);
}
public function scopeActive()
{
return $this->where('active',TRUE);
}
// List all the agents, including agents of agents // List all the agents, including agents of agents
public function all_agents() public function all_agents($level=0)
{ {
$result = collect(); $result = collect();
foreach ($this->agents()->orderBy('id')->get() as $o) foreach ($this->agents as $o)
{ {
if (! $o->active) if (! $o->active OR ! $o->agents->count())
continue; continue;
$result->push($o->all_agents()); $o->level = $level;
$result->push($this);
$result->push($o);
// Include agents of agents
$result->push($o->all_agents($level+1));
} }
return $result->flatten(); return $result->flatten();
} }
public function all_clients() public function all_accounts()
{ {
// List all the clients of my agents $result = collect();
foreach ($this->all_clients() as $o)
{
$result->push($o->accounts->where('active',TRUE));
} }
public function all_suppliers() return $result->flatten();
{
// For each supplier, so if that supplier has a parent
} }
public function all_clients($level=0)
{
$result = collect();
foreach ($this->clients as $o)
{
if (! $o->active)
continue;
$o->level = $level;
$result->push($o);
// Include clients of agents
$result->push($o->all_clients($level+1));
}
return $result->flatten();
}
public function role() public function role()
{ {
// If I have agents and no parent, I am the wholesaler // If I have agents and no parent, I am the wholesaler

View File

@ -0,0 +1,69 @@
<div class="box box-success small">
<div class="box-header">
<h3 class="box-title">Accounts</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
<i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body">
@if ($user->all_accounts()->count())
<table class="table table-bordered table-striped table-hover" id="clients" style="width: 100%;">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Active</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Count {{ $user->all_accounts()->count() }}</th>
<th colspan="2">&nbsp;</th>
</tr>
</tfoot>
</table>
@else
<p>No Clients Active</p>
@endif
</div>
</div>
@section('page-scripts')
@css('https://cdn.datatables.net/responsive/2.2.1/css/responsive.dataTables.min.css')
@css('https://cdn.datatables.net/rowgroup/1.0.2/css/rowGroup.dataTables.min.css')
@js('https://cdn.datatables.net/responsive/2.2.1/js/dataTables.responsive.min.js')
@js('https://cdn.datatables.net/rowgroup/1.0.2/js/dataTables.rowGroup.min.js')
<style>
table.dataTable td {
outline: none;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$('#clients').DataTable( {
responsive: true,
ajax: {
url: "/api/r/accounts"
},
columns: [
{ data: "id" },
{ data: "company" },
{ data: "active_display" }
],
language: {
emptyTable: "No Active Clients"
},
order: [1, 'asc']
});
$('#clients tbody').on('click','tr', function () {
$(this).toggleClass('selected');
});
});
</script>
@append

View File

@ -0,0 +1,69 @@
<div class="box box-warning small">
<div class="box-header">
<h3 class="box-title">Agents</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
<i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body">
@if ($user->all_agents()->count())
<table class="table table-bordered table-striped table-hover" id="agents" style="width: 100%;">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Level</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Count {{ $user->all_agents()->count() }}</th>
<th colspan="2">&nbsp;</th>
</tr>
</tfoot>
</table>
@else
<p>No Agents Active</p>
@endif
</div>
</div>
@section('page-scripts')
@css('https://cdn.datatables.net/responsive/2.2.1/css/responsive.dataTables.min.css')
@css('https://cdn.datatables.net/rowgroup/1.0.2/css/rowGroup.dataTables.min.css')
@js('https://cdn.datatables.net/responsive/2.2.1/js/dataTables.responsive.min.js')
@js('https://cdn.datatables.net/rowgroup/1.0.2/js/dataTables.rowGroup.min.js')
<style>
table.dataTable td {
outline: none;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$('#agents').DataTable( {
responsive: true,
ajax: {
url: "/api/r/agents"
},
columns: [
{ data: "user_id_url" },
{ data: "surfirstname" },
{ data: "level" }
],
language: {
emptyTable: "No Active Agents"
},
order: [1, 'asc']
});
$('#agents tbody').on('click','tr', function () {
$(this).toggleClass('selected');
});
});
</script>
@append

View File

@ -0,0 +1,25 @@
@extends('adminlte::layouts.app')
@section('htmlheader_title')
Home
@endsection
@section('contentheader_title')
{{ $o->full_name }}
@endsection
@section('contentheader_description')
Home
@endsection
@section('main-content')
<div class="content">
<div class="row">
<div class="col-xs-6">
@include('r.agents')
</div>
<div class="col-xs-6">
@include('r.accounts')
</div>
</div>
</div>
@endsection

View File

@ -5,7 +5,7 @@
@endsection @endsection
@section('contentheader_title') @section('contentheader_title')
{{ $user->full_name }} {{ $o->full_name }}
@endsection @endsection
@section('contentheader_description') @section('contentheader_description')
Home Home
@ -14,13 +14,13 @@
@section('main-content') @section('main-content')
<div class="content"> <div class="content">
<div class="row"> <div class="row">
@if ($user->accounts->count() > 2) @if ($o->accounts->count() > 2)
<div class="col-sm-3"> <div class="col-sm-3">
<div class="info-box"> <div class="info-box">
<span class="info-box-icon bg-orange"><i class="fa fa-user"></i></span> <span class="info-box-icon bg-orange"><i class="fa fa-user"></i></span>
<div class="info-box-content"> <div class="info-box-content">
<span class="info-box-text">Accounts Linked</span> <span class="info-box-text">Accounts Linked</span>
<span class="info-box-number">{{ $user->accounts->count() }}</span> <span class="info-box-number">{{ $o->accounts->count() }}</span>
</div> </div>
</div> </div>
</div> </div>
@ -30,7 +30,7 @@
<span class="info-box-icon bg-red"><i class="fa fa-dollar"></i></span> <span class="info-box-icon bg-red"><i class="fa fa-dollar"></i></span>
<div class="info-box-content"> <div class="info-box-content">
<span class="info-box-text">Account Balance</span> <span class="info-box-text">Account Balance</span>
<span class="info-box-number"><small>$</small> {{ number_format($user->invoices_due->sum('due'),2) }}</span> <span class="info-box-number"><small>$</small> {{ number_format($o->invoices_due->sum('due'),2) }}</span>
</div> </div>
</div> </div>
</div> </div>
@ -39,7 +39,7 @@
<span class="info-box-icon bg-green"><i class="fa fa-clone"></i></span> <span class="info-box-icon bg-green"><i class="fa fa-clone"></i></span>
<div class="info-box-content"> <div class="info-box-content">
<span class="info-box-text">Active Services</span> <span class="info-box-text">Active Services</span>
<span class="info-box-number">{{ $user->services_active->count() }} <small>/{{ $user->services->count() }}</small></span> <span class="info-box-number">{{ $o->services_active->count() }} <small>/{{ $o->services->count() }}</small></span>
</div> </div>
</div> </div>
</div> </div>
@ -48,7 +48,7 @@
<span class="info-box-icon bg-blue"><i class="fa fa-hashtag"></i></span> <span class="info-box-icon bg-blue"><i class="fa fa-hashtag"></i></span>
<div class="info-box-content"> <div class="info-box-content">
<span class="info-box-text">Invoices Due</span> <span class="info-box-text">Invoices Due</span>
<span class="info-box-number">{{ $user->invoices_due->count() }}</span> <span class="info-box-number">{{ $o->invoices_due->count() }}</span>
</div> </div>
</div> </div>
</div> </div>
@ -62,7 +62,7 @@
@include('widgets.invoices_due') @include('widgets.invoices_due')
</div> </div>
<div class="col-xs-5"> <div class="col-xs-5">
@include('widgets.payment_history',['limit'=>10]) @include('widgets.payment_history')
</div> </div>
</div> </div>
</div> </div>

View File

@ -77,8 +77,6 @@
$('#services tbody').on('click','tr', function () { $('#services tbody').on('click','tr', function () {
$(this).toggleClass('selected'); $(this).toggleClass('selected');
//var data = table.row(this).data();
//window.location.href = '/u/service/view/'+data.id;
}); });
}); });
</script> </script>

View File

@ -19,6 +19,11 @@ Route::middleware('auth:api')->get('/user', function (Request $request) {
}); });
*/ */
Route::group(['middleware'=>['auth:api','reseller']], function() {
Route::get('/r/agents','ResellerServicesController@agents');
Route::get('/r/accounts','ResellerServicesController@accounts');
});
Route::group(['middleware'=>'auth:api'], function() { Route::group(['middleware'=>'auth:api'], function() {
Route::get('/u/invoices','UserServicesController@invoices'); Route::get('/u/invoices','UserServicesController@invoices');
Route::get('/u/payments','UserServicesController@payments'); Route::get('/u/payments','UserServicesController@payments');