Removed redundant functions in Account, Optimised User a little more, Moved Ezypay Commands to new Ezypay folder

This commit is contained in:
Deon George 2024-07-07 10:21:27 +10:00
parent 70e94bf6e6
commit b4f3db04fc
11 changed files with 91 additions and 85 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands\Ezypay;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Console\Command; use Illuminate\Console\Command;
@ -8,14 +8,14 @@ use Illuminate\Console\Command;
use App\Classes\External\Payments\Ezypay; use App\Classes\External\Payments\Ezypay;
use App\Models\Account; use App\Models\Account;
class PaymentsEzypayNext extends Command class PaymentNext extends Command
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'payments:ezypay:next'; protected $signature = 'ezypay:payment:next';
/** /**
* The console command description. * The console command description.
@ -35,14 +35,12 @@ class PaymentsEzypayNext extends Command
foreach ($poo->getCustomers() as $c) { foreach ($poo->getCustomers() as $c) {
if ($c->BillingStatus == 'Inactive') { 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; continue;
} }
// Load Account Details from ReferenceId // Load Account Details from ReferenceId
$ao = Account::where('site_id',(int)substr($c->ReferenceId,0,2)) $ao = Account::find((int)substr($c->ReferenceId,2,4));
->where('id',(int)substr($c->ReferenceId,2,4))
->single();
if (! $ao) { if (! $ao) {
$this->warn(sprintf('Missing: [%s] %s %s (%s)',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$c->ReferenceId)); $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 // 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([ $next_pay = $poo->getDebits([
'customerId'=>$c->Id, 'customerId'=>$c->Id,
@ -59,32 +64,32 @@ class PaymentsEzypayNext extends Command
])->reverse()->first(); ])->reverse()->first();
if ($next_pay->Status !== 'Pending') { 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; continue;
} }
$next_paydate = Carbon::createFromTimeString($next_pay->Date); $next_paydate = Carbon::createFromTimeString($next_pay->Date);
if ($next_pay->Amount < $account_due) if ($next_pay->Amount < $due)
$this->warn(sprintf('Next payment on [%s] for (%s) [%s] not sufficient for outstanding balance [%s]', $this->error(sprintf('- Next payment on [%s] for (%s) [%s] not sufficient for outstanding balance [%s]',
$next_paydate->format('Y-m-d'), $next_paydate->format('Y-m-d'),
$ao->name, $ao->name,
number_format($next_pay->Amount,2), number_format($next_pay->Amount,2),
number_format($account_due,2))); number_format($due,2)));
elseif ($next_pay->Amount > $account_due) elseif ($next_pay->Amount > $due)
$this->warn(sprintf('Next payment on [%s] for (%s) [%s] is too much for outstanding balance [%s]', $this->warn(sprintf('- Next payment on [%s] for (%s) [%s] is too much for outstanding balance [%s]',
$next_paydate->format('Y-m-d'), $next_paydate->format('Y-m-d'),
$ao->name, $ao->name,
number_format($next_pay->Amount,2), number_format($next_pay->Amount,2),
number_format($account_due,2))); number_format($due,2)));
else 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'), $next_paydate->format('Y-m-d'),
$ao->name, $ao->name,
number_format($next_pay->Amount,2), number_format($next_pay->Amount,2),
number_format($account_due,2))); number_format($due,2)));
} }
} }
} }

View File

@ -1,20 +1,20 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands\Ezypay;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use App\Classes\External\Payments\Ezypay; use App\Classes\External\Payments\Ezypay;
use App\Jobs\PaymentsImport as Job; use App\Jobs\PaymentsImport as Job;
class PaymentsEzypayImport extends Command class PaymentsImport extends Command
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'payments:ezypay:import'; protected $signature = 'ezypay:payment:import';
/** /**
* The console command description. * The console command description.

View File

@ -27,6 +27,12 @@ class Account extends Model implements IDs
/* STATIC */ /* STATIC */
/**
* A list of invoices that are in credit for all accounts
*
* @param Collection|NULL $invoices
* @return Collection
*/
public static function InvoicesCredit(Collection $invoices=NULL): Collection public static function InvoicesCredit(Collection $invoices=NULL): Collection
{ {
return (new self) return (new self)
@ -34,6 +40,12 @@ class Account extends Model implements IDs
->get(); ->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 public static function InvoicesDue(Collection $invoices=NULL): Collection
{ {
return (new self) return (new self)
@ -73,11 +85,12 @@ class Account extends Model implements IDs
return $this->belongsTo(Country::class); return $this->belongsTo(Country::class);
} }
public function external() /**
{ * Group this account is assigned to
return $this->belongsToMany(External\Integrations::class,'external_account',NULL,'external_integration_id'); * Groups are used for pricing control
} *
* @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
*/
public function group() public function group()
{ {
return $this->hasOneThrough(Group::class,AccountGroup::class,'account_id','id','id','group_id'); 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 * 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() public function invoices()
{ {
@ -96,13 +107,11 @@ class Account extends Model implements IDs
/** /**
* Relation to only return active invoices * Relation to only return active invoices
*
* @todo Only return active invoice_items
*/ */
public function invoices_active() public function invoices_active()
{ {
return $this->invoices() return $this->invoices()
->active(); ->with('active',TRUE);;
} }
/** /**
@ -116,19 +125,19 @@ class Account extends Model implements IDs
/** /**
* Relation to only return active payments * Relation to only return active payments
*
* @todo Only return active payment_items
*/ */
public function payments_active() public function payments_active()
{ {
return $this->payments() return $this->payments()
->active(); ->with('active',TRUE);
} }
/**
* Return the link to a provider's info for this account
*/
public function providers() public function providers()
{ {
return $this->belongsToMany(ProviderOauth::class,'account__provider') return $this->belongsToMany(ProviderOauth::class,'account__provider')
->where('account__provider.site_id',$this->site_id)
->withPivot('ref','synctoken','created_at','updated_at'); ->withPivot('ref','synctoken','created_at','updated_at');
} }
@ -150,6 +159,9 @@ class Account extends Model implements IDs
->active(); ->active();
} }
/**
* Taxes applicable for this account
*/
public function taxes() public function taxes()
{ {
return $this->hasMany(Tax::class,'country_id','country_id') return $this->hasMany(Tax::class,'country_id','country_id')
@ -219,7 +231,7 @@ class Account extends Model implements IDs
*/ */
public function getNameAttribute(): string 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 */ /* 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 * List of invoices (summary) for this account
* *

View File

@ -1,27 +0,0 @@
<?php
namespace App\Models\External;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;
class Integrations extends Model
{
public $table = 'external_integrations';
public function user()
{
return $this->belongsTo(User::class);
}
function scopeActive()
{
return $this->where('active',TRUE);
}
function scopeType($query,string $type)
{
return $query->where('type',$type);
}
}

View File

@ -7,6 +7,7 @@ use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection; use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Support\Facades\Log;
use Laravel\Passport\HasApiTokens; use Laravel\Passport\HasApiTokens;
use Leenooks\Traits\ScopeActive; use Leenooks\Traits\ScopeActive;
use Leenooks\Traits\UserSwitch; use Leenooks\Traits\UserSwitch;
@ -142,6 +143,18 @@ class User extends Authenticatable implements IDs
/* ATTRIBUTES */ /* 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 * 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 public function getNameAttribute(): string
{ {
return $this->full_name; return $this->name_full;
} }
/** /**
@ -157,11 +170,21 @@ class User extends Authenticatable implements IDs
* *
* @return string * @return string
*/ */
public function getFullNameAttribute(): string public function getNameFullAttribute(): string
{ {
return sprintf('%s %s',$this->firstname,$this->lastname); 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 * Return a friendly string of this persons role
* *
@ -172,9 +195,15 @@ class User extends Authenticatable implements IDs
return ucfirst($this->role()); return ucfirst($this->role());
} }
/**
* Return the name, surname first
* @return string
* @deprecated use getNameSurFirstAttribute()
*/
public function getSurFirstNameAttribute() public function getSurFirstNameAttribute()
{ {
return sprintf('%s, %s',$this->lastname,$this->firstname); Log::alert('UMO:! Deprecated function getSurFirstNameAttribute()');
return $this->getNameSurFirstAttribute();
} }
/* SCOPES */ /* SCOPES */

View File

@ -1,5 +1,5 @@
@component('mail::message',['site'=>$site,'heading'=>'Traffic Mismatch: '.$date]) @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. The traffic import today, had mismatching records. A request to have those login removed has been generated.

View File

@ -1,5 +1,5 @@
@component('mail::message',['site'=>$site,'heading'=>'Link Your Account']) @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. 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. If you didnt make this request, you can ignore this, and the request will be ignored.

View File

@ -1,5 +1,5 @@
@component('mail::message',['site'=>$site,'heading'=>'System Test Email']) @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. This is just a test email to validate that you can receive emails from us.

View File

@ -1,5 +1,5 @@
@component('mail::message',['site'=>$site,'heading'=>'Invoice: '.$invoice->id]) @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. A new invoice has been generated on your account. A summary of that invoice is below.

View File

@ -1,5 +1,5 @@
@component('mail::message',['site'=>$site,'heading'=>'Password Reset']) @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. You are receiving this email because we received a password reset request for your account.

View File

@ -4,11 +4,11 @@
{{ $o->role }} Home {{ $o->role }} Home
@endsection @endsection
@section('page_title') @section('page_title')
{{ $o->full_name }} {{ $o->name_full }}
@endsection @endsection
@section('contentheader_title') @section('contentheader_title')
{{ $o->full_name }} {{ $o->name_full }}
@endsection @endsection
@section('contentheader_description') @section('contentheader_description')
{{ $o->role }} {{ $o->role }}