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 @@ +
ID | +Name | +Active | +
---|---|---|
Count {{ $user->all_accounts()->count() }} | ++ |
No Clients Active
+ @endif +ID | +Name | +Level | +
---|---|---|
Count {{ $user->all_agents()->count() }} | ++ |
No Agents Active
+ @endif +