From 1720baccc8401da2209ebe73a3f511fa7291dff0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 15 Jan 2023 14:59:42 +1100 Subject: [PATCH 1/2] Checks inside transactions --- app/Services/Invoice/MarkPaid.php | 21 ++++++++++++--------- app/Services/Payment/DeletePayment.php | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index b692144f40..6c3a12d4cd 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -57,16 +57,19 @@ class MarkPaid extends AbstractService $this->invoice = Invoice::withTrashed()->where('id', $this->invoice->id)->lockForUpdate()->first(); - $this->payable_balance = $this->invoice->balance; - - $this->invoice - ->service() - ->setExchangeRate() - ->updateBalance($this->payable_balance * -1) - ->updatePaidToDate($this->payable_balance) - ->setStatus(Invoice::STATUS_PAID) - ->save(); + if($this->invoice) + { + $this->payable_balance = $this->invoice->balance; + $this->invoice + ->service() + ->setExchangeRate() + ->updateBalance($this->payable_balance * -1) + ->updatePaidToDate($this->payable_balance) + ->setStatus(Invoice::STATUS_PAID) + ->save(); + } + }, 1); /* Create Payment */ diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index 10db3699ee..5f209d5836 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -40,7 +40,7 @@ class DeletePayment $this->payment = Payment::withTrashed()->where('id', $this->payment->id)->lockForUpdate()->first(); - if (!$this->payment->is_deleted) { + if ($this->payment && !$this->payment->is_deleted) { $this->setStatus(Payment::STATUS_CANCELLED) //sets status of payment ->updateCreditables() //return the credits first From a743504ed384427eb51033235d5cb753ff3131a5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 15 Jan 2023 15:10:41 +1100 Subject: [PATCH 2/2] Minor fixes for transactions --- app/Jobs/Ledger/ClientLedgerBalanceUpdate.php | 37 ++++++++++--------- app/Services/Credit/CreditService.php | 2 +- app/Services/Invoice/MarkPaid.php | 18 +-------- app/Services/Payment/DeletePayment.php | 1 - 4 files changed, 22 insertions(+), 36 deletions(-) diff --git a/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php b/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php index 19db1c8290..11f1ca3587 100644 --- a/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php +++ b/app/Jobs/Ledger/ClientLedgerBalanceUpdate.php @@ -52,27 +52,30 @@ class ClientLedgerBalanceUpdate implements ShouldQueue MultiDB::setDb($this->company->db); CompanyLedger::where('balance', 0)->where('client_id', $this->client->id)->orderBy('updated_at', 'ASC')->cursor()->each(function ($company_ledger) { - if ($company_ledger->balance > 0) { - return; - } + + if ($company_ledger->balance == 0) + { - $last_record = CompanyLedger::where('client_id', $company_ledger->client_id) - ->where('company_id', $company_ledger->company_id) - ->where('balance', '!=', 0) - ->orderBy('id', 'DESC') - ->first(); - - if (! $last_record) { $last_record = CompanyLedger::where('client_id', $company_ledger->client_id) - ->where('company_id', $company_ledger->company_id) - ->orderBy('id', 'DESC') - ->first(); + ->where('company_id', $company_ledger->company_id) + ->where('balance', '!=', 0) + ->orderBy('id', 'DESC') + ->first(); + + if (! $last_record) { + $last_record = CompanyLedger::where('client_id', $company_ledger->client_id) + ->where('company_id', $company_ledger->company_id) + ->orderBy('id', 'DESC') + ->first(); + } + + // nlog("Updating Balance NOW"); + + $company_ledger->balance = $last_record->balance + $company_ledger->adjustment; + $company_ledger->save(); + } - // nlog("Updating Balance NOW"); - - $company_ledger->balance = $last_record->balance + $company_ledger->adjustment; - $company_ledger->save(); }); // nlog("Updating company ledger for client ". $this->client->id); diff --git a/app/Services/Credit/CreditService.php b/app/Services/Credit/CreditService.php index f09dad1750..4a1c5e52d2 100644 --- a/app/Services/Credit/CreditService.php +++ b/app/Services/Credit/CreditService.php @@ -167,7 +167,7 @@ class CreditService } public function adjustBalance($adjustment) - {nlog("adjusting by {$adjustment}"); + { $this->credit->balance += $adjustment; return $this; diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index 6c3a12d4cd..dedd6f1720 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -14,16 +14,10 @@ namespace App\Services\Invoice; use App\Events\Invoice\InvoiceWasPaid; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; -use App\Jobs\Invoice\InvoiceWorkflowSettings; -use App\Jobs\Ninja\TransactionLog; -use App\Jobs\Payment\EmailPayment; use App\Libraries\Currency\Conversion\CurrencyApi; -use App\Models\Client; use App\Models\Invoice; use App\Models\Payment; -use App\Models\TransactionEvent; use App\Services\AbstractService; -use App\Services\Client\ClientService; use App\Utils\Ninja; use App\Utils\Traits\GeneratesCounter; use Illuminate\Support\Carbon; @@ -69,7 +63,7 @@ class MarkPaid extends AbstractService ->setStatus(Invoice::STATUS_PAID) ->save(); } - + }, 1); /* Create Payment */ @@ -136,16 +130,6 @@ class MarkPaid extends AbstractService event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - $transaction = [ - 'invoice' => $this->invoice->transaction_event(), - 'payment' => $payment->transaction_event(), - 'client' => $this->invoice->client->transaction_event(), - 'credit' => [], - 'metadata' => [], - ]; - - // TransactionLog::dispatch(TransactionEvent::INVOICE_MARK_PAID, $transaction, $this->invoice->company->db); - event('eloquent.updated: App\Models\Invoice', $this->invoice); return $this->invoice; diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index 5f209d5836..a5a769ba2f 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -51,7 +51,6 @@ class DeletePayment } - }, 1); return $this->payment;