diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index 3b6812f2c87..698f8026877 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -64,16 +64,20 @@ public function getInvitationByKey($key): ?InvoiceInvitation */ public function delete($invoice): Invoice { - $invoice = $invoice->fresh(); + + $invoice = \DB::transaction(function () use ($invoice) { + return \App\Models\Invoice::lockForUpdate()->find($invoice->id); + }); - if ($invoice->is_deleted) { + if (!$invoice || $invoice->is_deleted) { return $invoice; } + $invoice->is_deleted = true; + $invoice->saveQuietly(); + $invoice = $invoice->service()->markDeleted()->save(); - parent::delete($invoice); - return $invoice; } diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index e34790321aa..3160f7af535 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -11,6 +11,7 @@ namespace App\Services\Invoice; +use App\Events\Invoice\InvoiceWasDeleted; use App\Jobs\Inventory\AdjustProductInventory; use App\Models\Invoice; use App\Models\Quote; @@ -33,10 +34,7 @@ public function __construct(public Invoice $invoice) public function run() { - if ($this->invoice->is_deleted) { - return $this->invoice; - } - + if ($this->invoice->company->track_inventory) { (new AdjustProductInventory($this->invoice->company, $this->invoice, []))->handleDeletedInvoice(); } @@ -49,6 +47,10 @@ public function run() ->adjustLedger() ->triggeredActions(); + $this->invoice->delete(); + + event(new \App\Events\Invoice\InvoiceWasDeleted($this->invoice, $this->invoice->company, \App\Utils\Ninja::eventVars(auth()->guard('api')->user() ? auth()->guard('api')->user()->id : null))); + return $this->invoice; } diff --git a/resources/views/email/template/admin.blade.php b/resources/views/email/template/admin.blade.php index 3cb45eb146b..6802fe7de23 100644 --- a/resources/views/email/template/admin.blade.php +++ b/resources/views/email/template/admin.blade.php @@ -251,11 +251,11 @@ font-family: Verdana, Geneva, Tahoma, sans-serif;">© {{ date('Y') }} Invoice Ninja, All Rights Reserved
- +