Changed ezypay matching to use referenceid, fixed NextKey for existing records

This commit is contained in:
Deon George 2019-06-07 22:34:41 +10:00
parent 253eb55c19
commit 78d55fb423
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
2 changed files with 19 additions and 15 deletions

View File

@ -63,17 +63,19 @@ class EzypayImport extends Command
} }
// Load Direct Debit Details // Load Account Details from ReferenceId
$ab = AccoutBilling::whereIN('checkout_id',$cos)->where('checkout_data',$c->EzypayReferenceNumber)->first(); $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; continue;
} }
// Find the last payment logged // 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([ $o = $poo->getDebits([
'customerId'=>$c->Id, 'customerId'=>$c->Id,
@ -88,11 +90,11 @@ class EzypayImport extends Command
foreach ($o->reverse() as $p) foreach ($o->reverse() as $p)
{ {
// If not success, ignore it. // If not success, ignore it.
if (! $p->Status == 'Success') if ($p->Status != 'Success')
continue; continue;
$pd = Carbon::createFromFormat('Y-m-d?H:i:s.u',$p->Date); $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))) if ($lp AND (($pd == $lp->date_payment) OR ($p->Id == $lp->checkout_data)))
continue; continue;
@ -104,9 +106,9 @@ class EzypayImport extends Command
$po->checkout_id = '999'; // @todo $po->checkout_id = '999'; // @todo
$po->checkout_data = $p->Id; $po->checkout_data = $p->Id;
$po->total_amt = $p->Amount; $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));
} }
} }

View File

@ -22,12 +22,14 @@ trait NextKey
static::saved(function($model) static::saved(function($model)
{ {
if (! $model::RECORD_ID) if ($model->wasRecentlyCreated) {
throw new \Exception('Missing record_id const for '.get_class($model)); 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 = Module::where('name',$model::RECORD_ID)->firstOrFail();
$mo->record->id = $model->id; $mo->record->id = $model->id;
$mo->record->save(); $mo->record->save();
}
}); });
} }