From 78d55fb4236441ca835727fffebd4aeefac779ce Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 7 Jun 2019 22:34:41 +1000 Subject: [PATCH] Changed ezypay matching to use referenceid, fixed NextKey for existing records --- app/Console/Commands/EzypayImport.php | 22 ++++++++++++---------- app/Traits/NextKey.php | 12 +++++++----- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/Console/Commands/EzypayImport.php b/app/Console/Commands/EzypayImport.php index dd42115..1e38740 100644 --- a/app/Console/Commands/EzypayImport.php +++ b/app/Console/Commands/EzypayImport.php @@ -63,17 +63,19 @@ class EzypayImport extends Command } - // Load Direct Debit Details - $ab = AccoutBilling::whereIN('checkout_id',$cos)->where('checkout_data',$c->EzypayReferenceNumber)->first(); + // 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 (! $ab) + if (! $ao) { - $this->warn(sprintf('Missing: [%s] %s %s',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname)); + $this->warn(sprintf('Missing: [%s] %s %s (%s)',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$c->ReferenceId)); continue; } // Find the last payment logged - $last = Carbon::createFromTimestamp(Payment::whereIN('checkout_id',$cos)->where('account_id',$ab->service->account_id)->max('date_payment')); + $last = Carbon::createFromTimestamp(Payment::whereIN('checkout_id',$cos)->where('account_id',$ao->id)->max('date_payment')); $o = $poo->getDebits([ 'customerId'=>$c->Id, @@ -88,11 +90,11 @@ class EzypayImport extends Command foreach ($o->reverse() as $p) { // If not success, ignore it. - if (! $p->Status == 'Success') - continue; + if ($p->Status != 'Success') + continue; $pd = Carbon::createFromFormat('Y-m-d?H:i:s.u',$p->Date); - $lp = $ab->service->account->payments->last(); + $lp = $ao->payments->last(); if ($lp AND (($pd == $lp->date_payment) OR ($p->Id == $lp->checkout_data))) continue; @@ -104,9 +106,9 @@ class EzypayImport extends Command $po->checkout_id = '999'; // @todo $po->checkout_data = $p->Id; $po->total_amt = $p->Amount; - $ab->service->account->payments()->save($po); + $ao->payments()->save($po); - $this->info(sprintf('Recorded: [%s] %s %s (%s)',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$po->id)); + $this->info(sprintf('Recorded: Payment for [%s] %s %s (%s) on %s',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$po->id,$pd)); } } diff --git a/app/Traits/NextKey.php b/app/Traits/NextKey.php index a906783..fe3ca51 100644 --- a/app/Traits/NextKey.php +++ b/app/Traits/NextKey.php @@ -22,12 +22,14 @@ trait NextKey static::saved(function($model) { - if (! $model::RECORD_ID) - throw new \Exception('Missing record_id const for '.get_class($model)); + if ($model->wasRecentlyCreated) { + if (! defined(get_class($model).'::RECORD_ID')) + throw new \Exception('Missing record_id const for '.get_class($model)); - $mo = Module::where('name',$model::RECORD_ID)->firstOrFail(); - $mo->record->id = $model->id; - $mo->record->save(); + $mo = Module::where('name',$model::RECORD_ID)->firstOrFail(); + $mo->record->id = $model->id; + $mo->record->save(); + } }); }