From 88f730b9a12c7b8c611464c6b680bfb99a62cc7f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 8 Mar 2022 21:49:33 +1100 Subject: [PATCH] Fixes for EWay --- app/Console/Commands/CheckData.php | 15 +++++++-------- app/PaymentDrivers/Eway/CreditCard.php | 4 ++-- app/PaymentDrivers/Eway/Token.php | 12 ++++++++++++ app/Services/Credit/CreditService.php | 15 ++++++++++----- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 74f5187afab2..b76ddc26670b 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -74,7 +74,7 @@ class CheckData extends Command /** * @var string */ - protected $signature = 'ninja:check-data {--database=} {--fix=} {--client_id=} {--vendor_id=} {--paid_to_date=} {--client_balance=}'; + protected $signature = 'ninja:check-data {--database=} {--fix=} {--client_id=} {--vendor_id=} {--paid_to_date=} {--client_balance=} {--ledger_balance=}'; /** * @var string @@ -102,7 +102,7 @@ class CheckData extends Command config(['database.default' => $database]); } - // $this->checkInvoiceBalances(); + $this->checkInvoiceBalances(); $this->checkInvoiceBalancesNew(); //$this->checkInvoicePayments(); @@ -482,6 +482,7 @@ class CheckData extends Command payments.id = paymentables.payment_id WHERE paymentable_type = ? AND paymentables.deleted_at is NULL + AND payments.amount > 0 AND payments.is_deleted = 0 AND payments.client_id = ?; "), [App\Models\Credit::class, $client->id] ); @@ -853,18 +854,16 @@ ORDER BY clients.id; foreach (Client::where('is_deleted', 0)->where('clients.updated_at', '>', now()->subDays(2))->cursor() as $client) { $invoice_balance = $client->invoices()->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance'); - $credit_balance = $client->credits()->where('is_deleted', false)->sum('balance'); - $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first(); - if ($ledger && number_format($invoice_balance, 4) != number_format($client->balance, 4)) { + if ($ledger && number_format($ledger->balance, 4) != number_format($client->balance, 4)) { $this->wrong_balances++; - $this->logMessage("# {$client->id} " . $client->present()->name.' - '.$client->number." - Balance Failure - Invoice Balances = {$invoice_balance} Client Balance = {$client->balance} Ledger Balance = {$ledger->balance}"); + $this->logMessage("# {$client->id} " . $client->present()->name.' - '.$client->number." - Balance Failure - Client Balance = {$client->balance} Ledger Balance = {$ledger->balance}"); $this->isValid = false; - if($this->option('client_balance')){ + if($this->option('ledger_balance')){ $this->logMessage("# {$client->id} " . $client->present()->name.' - '.$client->number." Fixing {$client->balance} to {$invoice_balance}"); $client->balance = $invoice_balance; @@ -879,7 +878,7 @@ ORDER BY clients.id; } } - $this->logMessage("{$this->wrong_balances} clients with incorrect balances"); + $this->logMessage("{$this->wrong_balances} clients with incorrect ledger balances"); } private function checkLogoFiles() diff --git a/app/PaymentDrivers/Eway/CreditCard.php b/app/PaymentDrivers/Eway/CreditCard.php index 8dc93a0ddc37..afed80768733 100644 --- a/app/PaymentDrivers/Eway/CreditCard.php +++ b/app/PaymentDrivers/Eway/CreditCard.php @@ -160,7 +160,7 @@ class CreditCard 'TotalAmount' => $this->convertAmountForEway(), 'CurrencyCode' => $this->eway_driver->client->currency()->code, 'InvoiceNumber' => $invoice_numbers, - 'Description' => $description, + 'InvoiceDescription' => $description, 'InvoiceReference' => $description, ], 'TransactionType' => \Eway\Rapid\Enum\TransactionType::PURCHASE, @@ -252,7 +252,7 @@ class CreditCard 'TotalAmount' => $this->convertAmountForEway($amount), 'CurrencyCode' => $this->eway_driver->client->currency()->code, 'InvoiceNumber' => $invoice_numbers, - 'Description' => $description, + 'InvoiceDescription' => $description, 'InvoiceReference' => $description, ], 'TransactionType' => \Eway\Rapid\Enum\TransactionType::RECURRING, diff --git a/app/PaymentDrivers/Eway/Token.php b/app/PaymentDrivers/Eway/Token.php index b49bf12a1439..1d40f7cc01f0 100644 --- a/app/PaymentDrivers/Eway/Token.php +++ b/app/PaymentDrivers/Eway/Token.php @@ -43,6 +43,14 @@ class Token $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total; + + $invoice_numbers = ''; + + if($this->eway_driver->payment_hash->data) + $invoice_numbers = collect($this->eway_driver->payment_hash->data->invoices)->pluck('invoice_number')->implode(','); + + $description = "Invoices: {$invoice_numbers} for {$amount} for client {$this->eway_driver->client->present()->name()}"; + $this->eway_driver->payment_hash = $payment_hash; $transaction = [ @@ -51,6 +59,10 @@ class Token ], 'Payment' => [ 'TotalAmount' => $this->eway_driver->convertAmount($amount), + 'CurrencyCode' => $this->eway_driver->client->currency()->code, + 'InvoiceNumber' => $invoice_numbers, + 'InvoiceDescription' => $description, + 'InvoiceReference' => $description, ], 'TransactionType' => \Eway\Rapid\Enum\TransactionType::RECURRING, ]; diff --git a/app/Services/Credit/CreditService.php b/app/Services/Credit/CreditService.php index e7b00b44f995..321399d25dcf 100644 --- a/app/Services/Credit/CreditService.php +++ b/app/Services/Credit/CreditService.php @@ -133,11 +133,16 @@ class CreditService ->attach($this->credit->id, ['amount' => $adjustment]); //reduce client paid_to_date by $this->credit->balance amount - $this->credit - ->client - ->service() - ->updatePaidToDate($adjustment) - ->save(); + // $this->credit + // ->client + // ->service() + // ->updatePaidToDate($adjustment) + // ->save(); + + $client = $this->credit->client->fresh(); + $client->service() + ->updatePaidToDate($adjustment) + ->save(); event('eloquent.created: App\Models\Payment', $payment);