diff --git a/app/Classes/Payments/Ezypay.php b/app/Classes/Payments/Ezypay.php index 415dd68..eee0f4f 100644 --- a/app/Classes/Payments/Ezypay.php +++ b/app/Classes/Payments/Ezypay.php @@ -38,10 +38,17 @@ class Ezypay extends Payments return json_decode($result->getBody()->getContents()); } + public function getCustomer(string $id) + { + return Cache::remember(__METHOD__.$id,86400,function() use ($id) { + return collect($this->connect('accounts/customerid/'.$id)); + }); + } + public function getCustomers(): Collection { return Cache::remember(__METHOD__,86400,function() { - return collect($this->connect('customers')); + return collect($this->connect('customers?pageSize=100')); }); } diff --git a/app/Console/Commands/EzypayImport.php b/app/Console/Commands/EzypayImport.php index 8710214..0b2b1bf 100644 --- a/app/Console/Commands/EzypayImport.php +++ b/app/Console/Commands/EzypayImport.php @@ -4,11 +4,9 @@ namespace App\Console\Commands; use Carbon\Carbon; use Illuminate\Console\Command; -use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Mail; use App\Classes\Payments\Ezypay; -use App\Models\{Account,AccoutBilling,Checkout,Payment}; +use App\Models\{Account,Checkout,Payment}; class EzypayImport extends Command { @@ -43,12 +41,6 @@ class EzypayImport extends Command */ public function handle() { - /* - DB::listen(function($query) { - $this->warn('- SQL:'.json_encode(['sql'=>$query->sql,'binding'=>$query->bindings])); - }); - */ - $poo = new Ezypay(); // Get our checkout IDs for this plugin @@ -110,7 +102,6 @@ class EzypayImport extends Command $this->info(sprintf('Recorded: Payment for [%s] %s %s (%s) on %s',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$po->id,$pd)); } } - } } } \ No newline at end of file diff --git a/app/Console/Commands/EzypayPayments.php b/app/Console/Commands/EzypayPayments.php new file mode 100644 index 0000000..03eb396 --- /dev/null +++ b/app/Console/Commands/EzypayPayments.php @@ -0,0 +1,81 @@ +getCustomers() as $c) + { + if ($c->BillingStatus == 'Inactive') + { + $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)) + ->first(); + + if (! $ao) + { + $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->Amount < $account_due) + $this->warn(sprintf('Next payment for (%s) [%s] not sufficient for outstanding balance [%s]',$ao->name,number_format($next_pay->Amount,2),number_format($account_due,2))); + elseif ($next_pay->Amount > $account_due) + $this->warn(sprintf('Next payment for (%s) [%s] is too much for outstanding balance [%s]',$ao->name,number_format($next_pay->Amount,2),number_format($account_due,2))); + else + $this->info(sprintf('Next payment for (%s) [%s] will cover outstanding balance [%s]',$ao->name,number_format($next_pay->Amount,2),number_format($account_due,2))); + } + } +} \ No newline at end of file diff --git a/app/Models/Account.php b/app/Models/Account.php index d525079..5a70466 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -36,6 +36,11 @@ class Account extends Model return $this->belongsTo(Country::class); } + public function invoices() + { + return $this->hasMany(Invoice::class); + } + public function language() { return $this->belongsTo(Language::class); @@ -114,4 +119,16 @@ class Account extends Model return join("\n",$this->_address()); } } + + /** + * Get the due invoices on an account + * + * @return mixed + */ + public function dueInvoices() + { + return $this->invoices->filter(function($item) { + return $item->active AND $item->due > 0; + }); + } } \ No newline at end of file