From 9e6948825426cca4045cdc0213a6b0579b4e0af9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 30 Nov 2022 14:39:45 +1100 Subject: [PATCH] Fixes for multiple deletes on a single invoice --- app/Jobs/Bank/MatchBankTransactions.php | 2 +- app/Services/Invoice/HandleRestore.php | 10 ++++------ app/Services/Invoice/MarkInvoiceDeleted.php | 21 ++++++--------------- tests/Feature/ClientApiTest.php | 2 -- tests/Feature/DeleteInvoiceTest.php | 1 + 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/app/Jobs/Bank/MatchBankTransactions.php b/app/Jobs/Bank/MatchBankTransactions.php index 286e3f6ef7ba..0532d51e8b58 100644 --- a/app/Jobs/Bank/MatchBankTransactions.php +++ b/app/Jobs/Bank/MatchBankTransactions.php @@ -266,7 +266,7 @@ class MatchBankTransactions implements ShouldQueue /* Create Payment */ $payment = PaymentFactory::create($this->invoice->company_id, $this->invoice->user_id); - $payment->amount = $amount; + $payment->amount = $this->bt->amount; $payment->applied = $this->applied_amount; $payment->status_id = Payment::STATUS_COMPLETED; $payment->client_id = $this->invoice->client_id; diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php index 412013aceb4c..f0e7a4e89425 100644 --- a/app/Services/Invoice/HandleRestore.php +++ b/app/Services/Invoice/HandleRestore.php @@ -56,8 +56,7 @@ class HandleRestore extends AbstractService $this->invoice->client ->service() - ->updateBalance($this->invoice->balance) - ->updatePaidToDate($this->invoice->paid_to_date) + ->updateBalanceAndPaidToDate($this->invoice->balance,$this->invoice->paid_to_date) ->save(); $this->windBackInvoiceNumber(); @@ -120,11 +119,11 @@ class HandleRestore extends AbstractService if ($this->adjustment_amount == $this->total_payments) { $this->invoice->payments()->update(['payments.deleted_at' => null, 'payments.is_deleted' => false]); - } else { + } //adjust payments down by the amount applied to the invoice payment. - $this->invoice->payments->each(function ($payment) { + $this->invoice->payments->fresh()->each(function ($payment) { $payment_adjustment = $payment->paymentables ->where('paymentable_type', '=', 'invoices') ->where('paymentable_id', $this->invoice->id) @@ -141,8 +140,7 @@ class HandleRestore extends AbstractService $payment->restore(); $payment->save(); }); - } - + return $this; } diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index 2d123c8c9175..e317c403ade6 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -77,26 +77,17 @@ class MarkInvoiceDeleted extends AbstractService return $this; } - // @deprecated - private function adjustBalance() - { - // $client = $this->invoice->client->fresh(); - // $client->balance += $this->balance_adjustment * -1; - // $client->save(); - - // $this->invoice->client->service()->updateBalance($this->balance_adjustment * -1)->save(); //reduces the client balance by the invoice amount. - - return $this; - } - /* Adjust the payment amounts */ private function adjustPayments() { //if total payments = adjustment amount - that means we need to delete the payments as well. - if ($this->adjustment_amount == $this->total_payments) { +nlog($this->adjustment_amount); +nlog($this->total_payments); + + if ($this->adjustment_amount == $this->total_payments) $this->invoice->payments()->update(['payments.deleted_at' => now(), 'payments.is_deleted' => true]); - } else { + //adjust payments down by the amount applied to the invoice payment. @@ -115,7 +106,7 @@ class MarkInvoiceDeleted extends AbstractService $payment->applied -= $payment_adjustment; $payment->save(); }); - } + return $this; } diff --git a/tests/Feature/ClientApiTest.php b/tests/Feature/ClientApiTest.php index 14633a1c655d..40b11fc70a4a 100644 --- a/tests/Feature/ClientApiTest.php +++ b/tests/Feature/ClientApiTest.php @@ -116,8 +116,6 @@ class ClientApiTest extends TestCase Client::reguard(); - nlog($c->toArray()); - $c->refresh(); $this->assertEquals("3", $c->settings->currency_id); diff --git a/tests/Feature/DeleteInvoiceTest.php b/tests/Feature/DeleteInvoiceTest.php index d49f6f27c9e9..8dcf2daf4764 100644 --- a/tests/Feature/DeleteInvoiceTest.php +++ b/tests/Feature/DeleteInvoiceTest.php @@ -162,6 +162,7 @@ class DeleteInvoiceTest extends TestCase $payment = $payment->fresh(); $this->assertTrue($payment->is_deleted); + $this->assertEquals(0, $payment->amount); $this->assertEquals(4, $payment->status_id); $client->fresh();