osb/app/Console/Commands/PaymentsEzypayNext.php

90 lines
2.4 KiB
PHP
Raw Normal View History

2019-06-11 02:36:58 +00:00
<?php
namespace App\Console\Commands;
use Carbon\Carbon;
2019-06-11 02:36:58 +00:00
use Illuminate\Console\Command;
2020-02-01 11:12:31 +00:00
use App\Classes\External\Payments\Ezypay;
2019-06-11 02:36:58 +00:00
use App\Models\Account;
2020-02-01 11:12:31 +00:00
class PaymentsEzypayNext extends Command
2019-06-11 02:36:58 +00:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
2020-02-01 11:12:31 +00:00
protected $signature = 'payments:ezypay:next';
2019-06-11 02:36:58 +00:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Load next payments, and ensure they cover the next invoice';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$poo = new Ezypay;
2019-06-11 02:36:58 +00:00
foreach ($poo->getCustomers() as $c) {
if ($c->BillingStatus == 'Inactive') {
2019-06-11 02:36:58 +00:00
$this->info(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();
2019-06-11 02:36:58 +00:00
if (! $ao) {
2019-06-11 02:36:58 +00:00
$this->warn(sprintf('Missing: [%s] %s %s (%s)',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$c->ReferenceId));
continue;
}
// Get Due Invoices
$account_due = $ao->dueInvoices()->sum('due');
$next_pay = $poo->getDebits([
'customerId'=>$c->Id,
'dateFrom'=>now()->format('Y-m-d'),
'dateTo'=>now()->addQuarter()->format('Y-m-d'),
])->reverse()->first();
if ($next_pay->Status !== 'Pending') {
$this->warn(sprintf('Next payment is not pending for (%s)',$ao->name));
continue;
}
$next_paydate = Carbon::createFromTimeString($next_pay->Date);
2019-06-11 02:36:58 +00:00
if ($next_pay->Amount < $account_due)
$this->warn(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)));
2019-06-11 02:36:58 +00:00
elseif ($next_pay->Amount > $account_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)));
2019-06-11 02:36:58 +00:00
else
$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)));
2019-06-11 02:36:58 +00:00
}
}
}