From b4f3db04fc70309a05dd614f55e56d033d470a66 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 7 Jul 2024 10:21:27 +1000 Subject: [PATCH] Removed redundant functions in Account, Optimised User a little more, Moved Ezypay Commands to new Ezypay folder --- .../PaymentNext.php} | 39 +++++++------ .../PaymentsImport.php} | 6 +- app/Models/Account.php | 55 +++++++++---------- app/Models/External/Integrations.php | 27 --------- app/Models/User.php | 35 +++++++++++- .../broadband_traffic_mismatch.blade.php | 2 +- .../views/email/system/social_link.blade.php | 2 +- .../views/email/system/test_email.blade.php | 2 +- resources/views/email/user/invoice.blade.php | 2 +- .../views/email/user/passwordreset.blade.php | 2 +- .../theme/backend/adminlte/home.blade.php | 4 +- 11 files changed, 91 insertions(+), 85 deletions(-) rename app/Console/Commands/{PaymentsEzypayNext.php => Ezypay/PaymentNext.php} (58%) rename app/Console/Commands/{PaymentsEzypayImport.php => Ezypay/PaymentsImport.php} (78%) delete mode 100644 app/Models/External/Integrations.php diff --git a/app/Console/Commands/PaymentsEzypayNext.php b/app/Console/Commands/Ezypay/PaymentNext.php similarity index 58% rename from app/Console/Commands/PaymentsEzypayNext.php rename to app/Console/Commands/Ezypay/PaymentNext.php index 1476d50..bdfff1d 100644 --- a/app/Console/Commands/PaymentsEzypayNext.php +++ b/app/Console/Commands/Ezypay/PaymentNext.php @@ -1,6 +1,6 @@ getCustomers() as $c) { if ($c->BillingStatus == 'Inactive') { - $this->info(sprintf('Ignoring INACTIVE: [%s] %s %s',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname)); + $this->comment(sprintf('Ignoring INACTIVE: [%s] %s %s',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname)); continue; } // Load Account Details from ReferenceId - $ao = Account::where('site_id',(int)substr($c->ReferenceId,0,2)) - ->where('id',(int)substr($c->ReferenceId,2,4)) - ->single(); + $ao = Account::find((int)substr($c->ReferenceId,2,4)); if (! $ao) { $this->warn(sprintf('Missing: [%s] %s %s (%s)',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$c->ReferenceId)); @@ -50,7 +48,14 @@ class PaymentsEzypayNext extends Command } // Get Due Invoices - $account_due = $ao->dueInvoices()->sum('due'); + $invoice_due = $ao->invoiceSummaryDue()->get(); + + $this->info(sprintf('Account [%s] (%s) has [%d] invoices due, totalling [%3.2f]', + $ao->lid, + $ao->name, + $invoice_due->count(), + ($due=$invoice_due->sum('_balance')), + )); $next_pay = $poo->getDebits([ 'customerId'=>$c->Id, @@ -59,32 +64,32 @@ class PaymentsEzypayNext extends Command ])->reverse()->first(); if ($next_pay->Status !== 'Pending') { - $this->warn(sprintf('Next payment is not pending for (%s)',$ao->name)); + $this->warn(sprintf('- Next payment is not pending for (%s)',$ao->name)); continue; } $next_paydate = Carbon::createFromTimeString($next_pay->Date); - if ($next_pay->Amount < $account_due) - $this->warn(sprintf('Next payment on [%s] for (%s) [%s] not sufficient for outstanding balance [%s]', + if ($next_pay->Amount < $due) + $this->error(sprintf('- Next payment on [%s] for (%s) [%s] not sufficient for outstanding balance [%s]', $next_paydate->format('Y-m-d'), $ao->name, number_format($next_pay->Amount,2), - number_format($account_due,2))); + number_format($due,2))); - elseif ($next_pay->Amount > $account_due) - $this->warn(sprintf('Next payment on [%s] for (%s) [%s] is too much for outstanding balance [%s]', + elseif ($next_pay->Amount > $due) + $this->warn(sprintf('- Next payment on [%s] for (%s) [%s] is too much for outstanding balance [%s]', $next_paydate->format('Y-m-d'), $ao->name, number_format($next_pay->Amount,2), - number_format($account_due,2))); + number_format($due,2))); else - $this->info(sprintf('Next payment on [%s] for (%s) [%s] will cover outstanding balance [%s]', + $this->info(sprintf('- Next payment on [%s] for (%s) [%s] will cover outstanding balance [%s]', $next_paydate->format('Y-m-d'), $ao->name, number_format($next_pay->Amount,2), - number_format($account_due,2))); + number_format($due,2))); } } } \ No newline at end of file diff --git a/app/Console/Commands/PaymentsEzypayImport.php b/app/Console/Commands/Ezypay/PaymentsImport.php similarity index 78% rename from app/Console/Commands/PaymentsEzypayImport.php rename to app/Console/Commands/Ezypay/PaymentsImport.php index bfaae7d..0e158a4 100644 --- a/app/Console/Commands/PaymentsEzypayImport.php +++ b/app/Console/Commands/Ezypay/PaymentsImport.php @@ -1,20 +1,20 @@ get(); } + /** + * A list of invoices that are due for all accounts + * + * @param Collection|NULL $invoices + * @return Collection + */ public static function InvoicesDue(Collection $invoices=NULL): Collection { return (new self) @@ -73,11 +85,12 @@ class Account extends Model implements IDs return $this->belongsTo(Country::class); } - public function external() - { - return $this->belongsToMany(External\Integrations::class,'external_account',NULL,'external_integration_id'); - } - + /** + * Group this account is assigned to + * Groups are used for pricing control + * + * @return \Illuminate\Database\Eloquent\Relations\HasOneThrough + */ public function group() { return $this->hasOneThrough(Group::class,AccountGroup::class,'account_id','id','id','group_id'); @@ -85,8 +98,6 @@ class Account extends Model implements IDs /** * Invoices created for this account - * - * @todo This needs to be optimised, to only return outstanding invoices and invoices for a specific age (eg: 2 years worth) */ public function invoices() { @@ -96,13 +107,11 @@ class Account extends Model implements IDs /** * Relation to only return active invoices - * - * @todo Only return active invoice_items */ public function invoices_active() { return $this->invoices() - ->active(); + ->with('active',TRUE);; } /** @@ -116,19 +125,19 @@ class Account extends Model implements IDs /** * Relation to only return active payments - * - * @todo Only return active payment_items */ public function payments_active() { return $this->payments() - ->active(); + ->with('active',TRUE); } + /** + * Return the link to a provider's info for this account + */ public function providers() { return $this->belongsToMany(ProviderOauth::class,'account__provider') - ->where('account__provider.site_id',$this->site_id) ->withPivot('ref','synctoken','created_at','updated_at'); } @@ -150,6 +159,9 @@ class Account extends Model implements IDs ->active(); } + /** + * Taxes applicable for this account + */ public function taxes() { return $this->hasMany(Tax::class,'country_id','country_id') @@ -219,7 +231,7 @@ class Account extends Model implements IDs */ public function getNameAttribute(): string { - return $this->company ?: ($this->user_id ? $this->user->getSurFirstNameAttribute() : 'LID:'.$this->id); + return $this->company ?: ($this->user_id ? $this->user->getNameSurFirstAttribute() : 'LID:'.$this->id); } /** @@ -234,19 +246,6 @@ class Account extends Model implements IDs /* METHODS */ - /** - * Get the due invoices on an account - * - * @return mixed - * @deprecated use invoiceSummary->filter(_balance > 0) - */ - public function dueInvoices() - { - return $this->invoices->filter(function($item) { - return $item->active AND $item->due > 0; - }); - } - /** * List of invoices (summary) for this account * diff --git a/app/Models/External/Integrations.php b/app/Models/External/Integrations.php deleted file mode 100644 index 7bf7e53..0000000 --- a/app/Models/External/Integrations.php +++ /dev/null @@ -1,27 +0,0 @@ -belongsTo(User::class); - } - - function scopeActive() - { - return $this->where('active',TRUE); - } - - function scopeType($query,string $type) - { - return $query->where('type',$type); - } -} \ No newline at end of file diff --git a/app/Models/User.php b/app/Models/User.php index aca9d13..f57caa6 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,6 +7,7 @@ use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Collection as DatabaseCollection; +use Illuminate\Support\Facades\Log; use Laravel\Passport\HasApiTokens; use Leenooks\Traits\ScopeActive; use Leenooks\Traits\UserSwitch; @@ -142,6 +143,18 @@ class User extends Authenticatable implements IDs /* ATTRIBUTES */ + /** + * Logged in user's full name + * + * @return string + * @deprecated use getNameFullAttribute() + */ + public function getFullNameAttribute(): string + { + Log::alert('UMO:! Deprecated function getFullNameAttribute()'); + return $this->getNameFullAttribute(); + } + /** * This is an alias method, as it is used by the framework * @@ -149,7 +162,7 @@ class User extends Authenticatable implements IDs */ public function getNameAttribute(): string { - return $this->full_name; + return $this->name_full; } /** @@ -157,11 +170,21 @@ class User extends Authenticatable implements IDs * * @return string */ - public function getFullNameAttribute(): string + public function getNameFullAttribute(): string { return sprintf('%s %s',$this->firstname,$this->lastname); } + /** + * Return the name, surname first + * + * @return string + */ + public function getNameSurFirstAttribute() + { + return sprintf('%s, %s',$this->lastname,$this->firstname); + } + /** * Return a friendly string of this persons role * @@ -172,9 +195,15 @@ class User extends Authenticatable implements IDs return ucfirst($this->role()); } + /** + * Return the name, surname first + * @return string + * @deprecated use getNameSurFirstAttribute() + */ public function getSurFirstNameAttribute() { - return sprintf('%s, %s',$this->lastname,$this->firstname); + Log::alert('UMO:! Deprecated function getSurFirstNameAttribute()'); + return $this->getNameSurFirstAttribute(); } /* SCOPES */ diff --git a/resources/views/email/system/broadband_traffic_mismatch.blade.php b/resources/views/email/system/broadband_traffic_mismatch.blade.php index aa2eaa3..7309af7 100644 --- a/resources/views/email/system/broadband_traffic_mismatch.blade.php +++ b/resources/views/email/system/broadband_traffic_mismatch.blade.php @@ -1,5 +1,5 @@ @component('mail::message',['site'=>$site,'heading'=>'Traffic Mismatch: '.$date]) -Hi {{ isset($user) ? $user->full_name.',' : '' }} +Hi {{ isset($user) ? $user->name_full.',' : '' }} The traffic import today, had mismatching records. A request to have those login removed has been generated. diff --git a/resources/views/email/system/social_link.blade.php b/resources/views/email/system/social_link.blade.php index f4de80b..e504854 100644 --- a/resources/views/email/system/social_link.blade.php +++ b/resources/views/email/system/social_link.blade.php @@ -1,5 +1,5 @@ @component('mail::message',['site'=>$site,'heading'=>'Link Your Account']) -Hi {{ isset($user) ? $user->full_name.',' : '' }} +Hi {{ isset($user) ? $user->name_full.',' : '' }} A request was made to link your account to a social login. If you didnt make this request, you can ignore this, and the request will be ignored. diff --git a/resources/views/email/system/test_email.blade.php b/resources/views/email/system/test_email.blade.php index d3abfe1..b27682e 100644 --- a/resources/views/email/system/test_email.blade.php +++ b/resources/views/email/system/test_email.blade.php @@ -1,5 +1,5 @@ @component('mail::message',['site'=>$site,'heading'=>'System Test Email']) -Hi {{ isset($user) ? $user->full_name.',' : '' }} +Hi {{ isset($user) ? $user->name_full.',' : '' }} This is just a test email to validate that you can receive emails from us. diff --git a/resources/views/email/user/invoice.blade.php b/resources/views/email/user/invoice.blade.php index f68fe17..8d1160b 100644 --- a/resources/views/email/user/invoice.blade.php +++ b/resources/views/email/user/invoice.blade.php @@ -1,5 +1,5 @@ @component('mail::message',['site'=>$site,'heading'=>'Invoice: '.$invoice->id]) -Hi {{ isset($user) ? $user->full_name.',' : '' }} +Hi {{ isset($user) ? $user->name_full.',' : '' }} A new invoice has been generated on your account. A summary of that invoice is below. diff --git a/resources/views/email/user/passwordreset.blade.php b/resources/views/email/user/passwordreset.blade.php index 800b6a7..14ece25 100644 --- a/resources/views/email/user/passwordreset.blade.php +++ b/resources/views/email/user/passwordreset.blade.php @@ -1,5 +1,5 @@ @component('mail::message',['site'=>$site,'heading'=>'Password Reset']) -Hi {{ isset($user) ? $user->full_name.',' : '' }} +Hi {{ isset($user) ? $user->name_full.',' : '' }} You are receiving this email because we received a password reset request for your account. diff --git a/resources/views/theme/backend/adminlte/home.blade.php b/resources/views/theme/backend/adminlte/home.blade.php index 5d1a04d..254bba8 100644 --- a/resources/views/theme/backend/adminlte/home.blade.php +++ b/resources/views/theme/backend/adminlte/home.blade.php @@ -4,11 +4,11 @@ {{ $o->role }} Home @endsection @section('page_title') - {{ $o->full_name }} + {{ $o->name_full }} @endsection @section('contentheader_title') - {{ $o->full_name }} + {{ $o->name_full }} @endsection @section('contentheader_description') {{ $o->role }}