From 0ce640c2830586647f04fb606e7861f93bfd23b3 Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 17 Jul 2018 14:10:40 +1000 Subject: [PATCH] Added Reseller view --- .../ResellerServicesController.php | 18 ++++ app/Http/Controllers/UserHomeController.php | 6 +- app/Http/Kernel.php | 2 +- app/Http/Middleware/Reseller.php | 19 ++++ app/Models/Account.php | 19 ++++ app/User.php | 88 +++++++++++++++---- .../backend/adminlte/r/accounts.blade.php | 69 +++++++++++++++ .../theme/backend/adminlte/r/agents.blade.php | 69 +++++++++++++++ .../backend/adminlte/resellerhome.blade.php | 25 ++++++ .../{home.blade.php => userhome.blade.php} | 14 +-- .../widgets/services_active.blade.php | 2 - routes/api.php | 5 ++ 12 files changed, 308 insertions(+), 28 deletions(-) create mode 100644 app/Http/Controllers/ResellerServicesController.php create mode 100644 app/Http/Middleware/Reseller.php create mode 100644 resources/theme/backend/adminlte/r/accounts.blade.php create mode 100644 resources/theme/backend/adminlte/r/agents.blade.php create mode 100644 resources/theme/backend/adminlte/resellerhome.blade.php rename resources/theme/backend/adminlte/{home.blade.php => userhome.blade.php} (78%) diff --git a/app/Http/Controllers/ResellerServicesController.php b/app/Http/Controllers/ResellerServicesController.php new file mode 100644 index 0000000..3a44b5e --- /dev/null +++ b/app/Http/Controllers/ResellerServicesController.php @@ -0,0 +1,18 @@ +Auth::user()->all_agents()->values()]; + } + + public function accounts() + { + return ['data'=>Auth::user()->all_accounts()->values()]; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/UserHomeController.php b/app/Http/Controllers/UserHomeController.php index 25e225f..30c4f2d 100644 --- a/app/Http/Controllers/UserHomeController.php +++ b/app/Http/Controllers/UserHomeController.php @@ -15,13 +15,13 @@ class UserHomeController extends Controller { switch (Auth::user()->role()) { case 'Customer': - return View('home'); + return View('userhome',['o'=>Auth::user()]); case 'Reseller': - break; + return View('resellerhome',['o'=>Auth::user()]); case 'Wholesaler': - break; + return View('resellerhome',['o'=>Auth::user()]); default: abort(500,'Unknown role: ',Auth::user()->role()); diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 0aa406d..ea8d0c9 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -2,7 +2,6 @@ namespace App\Http; -use App\Http\Middleware\SetSite; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel @@ -62,6 +61,7 @@ class Kernel extends HttpKernel 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'demoMode' => \Spatie\DemoMode\DemoMode::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, + 'reseller' => \App\Http\Middleware\Reseller::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'theme' => \Igaster\LaravelTheme\Middleware\setTheme::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, diff --git a/app/Http/Middleware/Reseller.php b/app/Http/Middleware/Reseller.php new file mode 100644 index 0000000..f7bd289 --- /dev/null +++ b/app/Http/Middleware/Reseller.php @@ -0,0 +1,19 @@ +role(),['Wholesaler','Reseller'])) + { + abort(303,'Not Reseller'); + + } else + return $next($request); + } +} \ No newline at end of file diff --git a/app/Models/Account.php b/app/Models/Account.php index 8c49177..963d560 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -9,6 +9,15 @@ class Account extends Model protected $table = 'ab_account'; public $timestamps = FALSE; + protected $appends = [ + 'active_display', + ]; + protected $visible = [ + 'id', + 'company', + 'active_display', + ]; + /** * Return the country the user belongs to */ @@ -26,4 +35,14 @@ class Account extends Model { return $this->belongsTo(\App\User::class); } + + public function getCompanyAttribute($value) + { + return $value ? $value : $this->user->SurFirstName; + } + + public function getActiveDisplayAttribute($value) + { + return sprintf('%s',$this->active ? 'primary' : 'danger',$this->active ? 'Active' : 'Inactive'); + } } \ No newline at end of file diff --git a/app/User.php b/app/User.php index c13946e..442f2d5 100644 --- a/app/User.php +++ b/app/User.php @@ -14,6 +14,7 @@ class User extends Authenticatable use HasApiTokens,Notifiable,UserSwitch; protected $dates = ['created_at','updated_at','last_access']; + protected $with = ['accounts']; /** * The attributes that are mass assignable. @@ -33,17 +34,28 @@ class User extends Authenticatable 'password', 'remember_token', ]; - public function accounts() + protected $appends = [ + 'surfirstname', + 'user_id_url', + ]; + protected $visible = [ + 'id', + 'surfirstname', + 'level', + 'user_id_url', + ]; + + public function accounts() { return $this->hasMany(Models\Account::class); } - protected function agents() { - return $this->hasMany(static::class,'parent_id','id'); + public function agents() { + return $this->hasMany(static::class,'parent_id','id')->with('agents'); } - protected function clients() { - return $this->hasMany(\App\User::class); + public function clients() { + return $this->hasMany(static::class,'parent_id','id')->with('clients'); } public function invoices() @@ -80,6 +92,11 @@ class User extends Authenticatable 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. * @@ -92,6 +109,7 @@ class User extends Authenticatable if (! is_null($value)) return new Carbon($value); } + public function getInvoicesDueAttribute() { return $this->invoices @@ -124,33 +142,73 @@ class User extends Authenticatable return $this->full_name; } + public function getUserIdAttribute() + { + return sprintf('%02s-%04s',$this->site_id,$this->id); + } + + public function getUserIdUrlAttribute() + { + return sprintf('%s',$this->id,$this->user_id); + } + + public function scopeActive() + { + return $this->where('active',TRUE); + } + // List all the agents, including agents of agents - public function all_agents() + public function all_agents($level=0) { $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; - $result->push($o->all_agents()); - $result->push($this); + $o->level = $level; + + $result->push($o); + + // Include agents of agents + $result->push($o->all_agents($level+1)); } 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)); + } + + return $result->flatten(); } - public function all_suppliers() + public function all_clients($level=0) { - // For each supplier, so if that supplier has a parent - } + $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() { // If I have agents and no parent, I am the wholesaler diff --git a/resources/theme/backend/adminlte/r/accounts.blade.php b/resources/theme/backend/adminlte/r/accounts.blade.php new file mode 100644 index 0000000..411613e --- /dev/null +++ b/resources/theme/backend/adminlte/r/accounts.blade.php @@ -0,0 +1,69 @@ +
+
+

Accounts

+
+ + +
+
+ +
+ @if ($user->all_accounts()->count()) + + + + + + + + + + + + + + +
IDNameActive
Count {{ $user->all_accounts()->count() }} 
+ @else +

No Clients Active

+ @endif +
+
+ +@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') + + + +@append \ No newline at end of file diff --git a/resources/theme/backend/adminlte/r/agents.blade.php b/resources/theme/backend/adminlte/r/agents.blade.php new file mode 100644 index 0000000..1026ad5 --- /dev/null +++ b/resources/theme/backend/adminlte/r/agents.blade.php @@ -0,0 +1,69 @@ +
+
+

Agents

+
+ + +
+
+ +
+ @if ($user->all_agents()->count()) + + + + + + + + + + + + + + +
IDNameLevel
Count {{ $user->all_agents()->count() }} 
+ @else +

No Agents Active

+ @endif +
+
+ +@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') + + + +@append \ No newline at end of file diff --git a/resources/theme/backend/adminlte/resellerhome.blade.php b/resources/theme/backend/adminlte/resellerhome.blade.php new file mode 100644 index 0000000..a435556 --- /dev/null +++ b/resources/theme/backend/adminlte/resellerhome.blade.php @@ -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') +
+
+
+ @include('r.agents') +
+
+ @include('r.accounts') +
+
+
+@endsection \ No newline at end of file diff --git a/resources/theme/backend/adminlte/home.blade.php b/resources/theme/backend/adminlte/userhome.blade.php similarity index 78% rename from resources/theme/backend/adminlte/home.blade.php rename to resources/theme/backend/adminlte/userhome.blade.php index 3522ade..dcc387f 100644 --- a/resources/theme/backend/adminlte/home.blade.php +++ b/resources/theme/backend/adminlte/userhome.blade.php @@ -5,7 +5,7 @@ @endsection @section('contentheader_title') - {{ $user->full_name }} + {{ $o->full_name }} @endsection @section('contentheader_description') Home @@ -14,13 +14,13 @@ @section('main-content')
- @if ($user->accounts->count() > 2) + @if ($o->accounts->count() > 2)
Accounts Linked - {{ $user->accounts->count() }} + {{ $o->accounts->count() }}
@@ -30,7 +30,7 @@
Account Balance - $ {{ number_format($user->invoices_due->sum('due'),2) }} + $ {{ number_format($o->invoices_due->sum('due'),2) }}
@@ -39,7 +39,7 @@
Active Services - {{ $user->services_active->count() }} /{{ $user->services->count() }} + {{ $o->services_active->count() }} /{{ $o->services->count() }}
@@ -48,7 +48,7 @@
Invoices Due - {{ $user->invoices_due->count() }} + {{ $o->invoices_due->count() }}
@@ -62,7 +62,7 @@ @include('widgets.invoices_due')
- @include('widgets.payment_history',['limit'=>10]) + @include('widgets.payment_history')
diff --git a/resources/theme/backend/adminlte/widgets/services_active.blade.php b/resources/theme/backend/adminlte/widgets/services_active.blade.php index 958823f..2463377 100644 --- a/resources/theme/backend/adminlte/widgets/services_active.blade.php +++ b/resources/theme/backend/adminlte/widgets/services_active.blade.php @@ -77,8 +77,6 @@ $('#services tbody').on('click','tr', function () { $(this).toggleClass('selected'); - //var data = table.row(this).data(); - //window.location.href = '/u/service/view/'+data.id; }); }); diff --git a/routes/api.php b/routes/api.php index 180974e..d979855 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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::get('/u/invoices','UserServicesController@invoices'); Route::get('/u/payments','UserServicesController@payments');