From dde11f73f595f96b5cac50de2da4b96061528a37 Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 9 May 2023 19:28:51 +0900 Subject: [PATCH] Changed home screen to use account models instead of user model. Home screen now shows multiple accounts --- app/Http/Controllers/HomeController.php | 2 - app/Models/Account.php | 15 +- app/Models/Service.php | 2 +- app/Models/User.php | 36 +++-- .../theme/backend/adminlte/home.blade.php | 144 +++++++++++------- .../widgets => invoice/widget}/due.blade.php | 43 +++--- .../widgets => invoice/widget}/list.blade.php | 41 +++-- .../widgets => invoice/widget}/next.blade.php | 1 + .../widgets => payment/widget}/list.blade.php | 7 +- .../product/widget/services.blade.php | 6 +- .../adminlte/service/widget/active.blade.php | 34 ++--- 11 files changed, 174 insertions(+), 157 deletions(-) rename resources/views/theme/backend/adminlte/{u/invoice/widgets => invoice/widget}/due.blade.php (55%) rename resources/views/theme/backend/adminlte/{u/invoice/widgets => invoice/widget}/list.blade.php (57%) rename resources/views/theme/backend/adminlte/{u/invoice/widgets => invoice/widget}/next.blade.php (94%) rename resources/views/theme/backend/adminlte/{u/payment/widgets => payment/widget}/list.blade.php (87%) 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') -
-
-
- +
-
-
-
-
-
- @include('service.widget.active') -
- -
- @include('u.invoice.widgets.due') - - @include('u.invoice.widgets.list') - - @include('u.payment.widgets.list') -
-
-
- - {{-- - -
+
+
+ @foreach($o->my_accounts as $ao) +
- @include('u.invoice.widgets.next',['future'=>FALSE]) +
+ +
+ +
+
+
+
+
+ @include('service.widget.active',['o'=>$ao]) +
+ +
+ @include('invoice.widget.due',['o'=>$ao]) + + @include('invoice.widget.list',['o'=>$ao]) + + @include('payment.widget.list',['o'=>$ao]) +
+
+
+ + {{-- + +
+
+
+ @include('u.invoice.widgets.next',['future'=>FALSE]) +
+
+
+ --}} + +
+
+
+ @include('invoice.widget.next',['future'=>TRUE]) +
+
+
+ + @canany('reseller','wholesaler') +
+ @include('user.widget.supplier') +
+ @endcanany +
+
- --}} + @endforeach -
-
-
- @include('u.invoice.widgets.next',['future'=>TRUE]) -
-
-
- - @if ($o == $user) - @canany('reseller','wholesaler') -
- @include('r.home.widgets.home') -
- @endcanany - @endif + @if ($o == $user) @canany('reseller','wholesaler') -
- @include('user.widget.supplier') +
+ @include('r.home.widgets.home')
@endcanany -
+ @endif
diff --git a/resources/views/theme/backend/adminlte/u/invoice/widgets/due.blade.php b/resources/views/theme/backend/adminlte/invoice/widget/due.blade.php similarity index 55% rename from resources/views/theme/backend/adminlte/u/invoice/widgets/due.blade.php rename to resources/views/theme/backend/adminlte/invoice/widget/due.blade.php index 4897869..e2830f2 100644 --- a/resources/views/theme/backend/adminlte/u/invoice/widgets/due.blade.php +++ b/resources/views/theme/backend/adminlte/invoice/widget/due.blade.php @@ -1,3 +1,4 @@ +
@@ -5,16 +6,14 @@
- @if(($x=$o->invoices()->with(['items.taxes','paymentitems.payment','account'])->get()->where('due','>',0))->count()) - + @if(($x=$o->invoices->where('due','>',0))->count()) +
- - @@ -23,11 +22,9 @@ @foreach ($x as $oo) due_at->isPast()) class="table-danger" @endif> - - + - @endforeach @@ -45,23 +42,21 @@ @js(datatables,bootstrap4|rowgroup) @append \ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/u/invoice/widgets/list.blade.php b/resources/views/theme/backend/adminlte/invoice/widget/list.blade.php similarity index 57% rename from resources/views/theme/backend/adminlte/u/invoice/widgets/list.blade.php rename to resources/views/theme/backend/adminlte/invoice/widget/list.blade.php index c23e910..dfdccfb 100644 --- a/resources/views/theme/backend/adminlte/u/invoice/widgets/list.blade.php +++ b/resources/views/theme/backend/adminlte/invoice/widget/list.blade.php @@ -1,18 +1,18 @@ + -
+

Past Invoices

- @if(($x=$o->invoices()->where('invoices.created_at','>',\Carbon\Carbon::now()->subMonths(12))->with(['items.taxes','paymentitems.payment','account.country.currency'])->get()->where('due','<=',0))->count()) -
Account #Issued Due TotalPayments Outstanding
{{ $oo->account->name }}{{ $oo->sid }}{{ $oo->created_at->format('Y-m-d') }}{{ $oo->lid }} {{ $oo->due_at->format('Y-m-d') }} ${{ number_format($oo->total,2) }}${{ number_format($oo->paid,2) }} ${{ number_format($oo->due,2) }}
+ @if(($x=$o->invoices->where('created_at','>',\Carbon\Carbon::now()->subMonths(12))->where('due','<=',0))->count()) +
- @@ -24,7 +24,6 @@ - @@ -43,23 +42,21 @@ @js(datatables,bootstrap4|rowgroup) @append \ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/u/invoice/widgets/next.blade.php b/resources/views/theme/backend/adminlte/invoice/widget/next.blade.php similarity index 94% rename from resources/views/theme/backend/adminlte/u/invoice/widgets/next.blade.php rename to resources/views/theme/backend/adminlte/invoice/widget/next.blade.php index 1c4af45..9757fa5 100644 --- a/resources/views/theme/backend/adminlte/u/invoice/widgets/next.blade.php +++ b/resources/views/theme/backend/adminlte/invoice/widget/next.blade.php @@ -1,3 +1,4 @@ + @if ($o->next_invoice_items($future)->count())
diff --git a/resources/views/theme/backend/adminlte/u/payment/widgets/list.blade.php b/resources/views/theme/backend/adminlte/payment/widget/list.blade.php similarity index 87% rename from resources/views/theme/backend/adminlte/u/payment/widgets/list.blade.php rename to resources/views/theme/backend/adminlte/payment/widget/list.blade.php index a905a5b..0225798 100644 --- a/resources/views/theme/backend/adminlte/u/payment/widgets/list.blade.php +++ b/resources/views/theme/backend/adminlte/payment/widget/list.blade.php @@ -1,11 +1,12 @@ + -
+

Past Payments

- @if(($x=$o->payments()->active()->where('payments.created_at','>',\Carbon\Carbon::now()->subMonths(12)->unix())->with(['items','account'])->get())->count()) + @if(($x=$o->payments->where('created_at','>',\Carbon\Carbon::now()->subMonths(12)))->count())
Account # IssuedDue Paid Total
{{ $oo->account->name }} {{ $oo->sid }} {{ $oo->created_at->format('Y-m-d') }}{{ $oo->due_at->format('Y-m-d') }} {{ $oo->paid_date ? $oo->paid_date->format('Y-m-d') : '' }} ${{ number_format($oo->total,2) }}
@@ -22,7 +23,7 @@ @foreach ($x as $oo) - + {{----}} diff --git a/resources/views/theme/backend/adminlte/product/widget/services.blade.php b/resources/views/theme/backend/adminlte/product/widget/services.blade.php index d495161..f094746 100644 --- a/resources/views/theme/backend/adminlte/product/widget/services.blade.php +++ b/resources/views/theme/backend/adminlte/product/widget/services.blade.php @@ -52,11 +52,7 @@ color: #4c110f; } - +
{{ $oo->account->name }}{{ $oo->sid }}{{ $oo->lid }} {{ $oo->paid_at->format('Y-m-d') }} ${{ number_format($oo->total,2) }}${{ number_format($oo->balance,2) }}