diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index b753937..dd8c986 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -31,8 +31,6 @@ class HomeController extends Controller if (! $o->exists) $o = Auth::user(); - $o->load(['services.invoice_items','services.type']); - return View('home',['o'=>$o]); } diff --git a/app/Models/Account.php b/app/Models/Account.php index 644727f..ff977d4 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -64,9 +64,15 @@ class Account extends Model implements IDs return $this->hasOneThrough(Group::class,AccountGroup::class,'account_id','id','id','group_id'); } + /** + * @return mixed + * @todo This needs to be optimised, to only return outstanding invoices and invoices for a specific age (eg: 2 years worth) + */ public function invoices() { - return $this->hasMany(Invoice::class); + return $this->hasMany(Invoice::class) + ->active() + ->with(['items.taxes','paymentitems.payment']); } public function language() @@ -76,7 +82,9 @@ class Account extends Model implements IDs public function payments() { - return $this->hasMany(Payment::class); + return $this->hasMany(Payment::class) + ->active() + ->with(['items']); } public function providers() @@ -89,7 +97,8 @@ class Account extends Model implements IDs public function services($active=FALSE) { $query = $this->hasMany(Service::class,['account_id','site_id'],['id','site_id']) - ->withoutGlobalScope(SiteScope::class); + ->withoutGlobalScope(SiteScope::class) + ->with(['product.translate','invoice_items']); return $active ? $query->active() : $query; } diff --git a/app/Models/Service.php b/app/Models/Service.php index cc5cde2..71508cb 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -97,7 +97,7 @@ class Service extends Model implements IDs protected $with = [ //'invoice_items', //'product.type.supplied', - //'type', + 'type', ]; public const INACTIVE_STATUS = [ diff --git a/app/Models/User.php b/app/Models/User.php index 722a0ca..a39cde5 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -105,14 +105,14 @@ class User extends Authenticatable implements IDs { return $this->hasMany(Account::class) ->orWhereIn('id',$this->rtm_accounts()->pluck('id')) - ->active() - ->with(['services']); + ->active(); } /** * This users invoices * * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough + * @deprecated Accounts have invoices, not users */ public function invoices() { @@ -130,16 +130,6 @@ class User extends Authenticatable implements IDs return $this->belongsTo(Language::class); } - /** - * The payments this user has made - * - * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough - */ - public function payments() - { - return $this->hasManyThrough(Payment::class,Account::class); - } - /** * Return the routes to market account for this user * @@ -151,9 +141,10 @@ class User extends Authenticatable implements IDs } /** - * THe services this user has + * The services this user has * * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough + * @deprecated Accounts have services, not users */ public function services() { @@ -162,6 +153,12 @@ class User extends Authenticatable implements IDs ->active(); } + /** + * Supplier configuration for this user + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + * @deprecated Move to account->suppliers() + */ public function suppliers() { return $this->belongsToMany(Supplier::class) @@ -178,7 +175,7 @@ class User extends Authenticatable implements IDs */ public function getNameAttribute(): string { - return $this->getFullNameAttribute(); + return $this->full_name; } /** @@ -192,13 +189,20 @@ class User extends Authenticatable implements IDs } /** - * Return my accounts + * Return my accounts, but only those accounts with the same group_id * + * @note Users can only manage accounts with the same group ID, thereby ensuring they dont see different + * pricing options - since prices can be controlled by groups * @return Collection */ public function getMyAccountsAttribute(): Collection { - return $this->accounts->where('user_id',$this->id); + $result = $this->accounts->where('user_id',$this->id); + + if (! $result->count()) + return $result; + + return $this->isReseller() ? $result : $result->groupBy('group.id')->first(); } /** diff --git a/resources/views/theme/backend/adminlte/home.blade.php b/resources/views/theme/backend/adminlte/home.blade.php index f781df6..c1e88bf 100644 --- a/resources/views/theme/backend/adminlte/home.blade.php +++ b/resources/views/theme/backend/adminlte/home.blade.php @@ -20,76 +20,102 @@ @include('common.account.widget.summary') -