diff --git a/app/Http/Controllers/V1/Admin/Invoice/InvoicesController.php b/app/Http/Controllers/V1/Admin/Invoice/InvoicesController.php index f1c7fd21a..9aeefe518 100644 --- a/app/Http/Controllers/V1/Admin/Invoice/InvoicesController.php +++ b/app/Http/Controllers/V1/Admin/Invoice/InvoicesController.php @@ -102,7 +102,7 @@ public function delete(DeleteInvoiceRequest $request) { $this->authorize('delete multiple invoices'); - Invoice::destroy($request->ids); + Invoice::deleteInvoices($request->ids); return response()->json([ 'success' => true, diff --git a/app/Http/Controllers/V1/Admin/Payment/PaymentMethodsController.php b/app/Http/Controllers/V1/Admin/Payment/PaymentMethodsController.php index c4678516e..53fbcb920 100644 --- a/app/Http/Controllers/V1/Admin/Payment/PaymentMethodsController.php +++ b/app/Http/Controllers/V1/Admin/Payment/PaymentMethodsController.php @@ -84,12 +84,14 @@ public function destroy(PaymentMethod $paymentMethod) { $this->authorize('delete', $paymentMethod); - $payments = $paymentMethod->payments; - - if ($payments->count() > 0) { + if ($paymentMethod->payments()->exists()) { return respondJson('payments_attached', 'Payments Attached.'); } + if ($paymentMethod->expenses()->exists()) { + return respondJson('expenses_attached', 'Expenses Attached.'); + } + $paymentMethod->delete(); return response()->json([ diff --git a/app/Http/Controllers/V1/PDF/DownloadReceiptController.php b/app/Http/Controllers/V1/PDF/DownloadReceiptController.php index b53c7f23e..51f64692e 100644 --- a/app/Http/Controllers/V1/PDF/DownloadReceiptController.php +++ b/app/Http/Controllers/V1/PDF/DownloadReceiptController.php @@ -17,6 +17,8 @@ class DownloadReceiptController extends Controller */ public function __invoke(Expense $expense) { + $this->authorize('view', $expense); + if ($expense) { $media = $expense->getFirstMedia('receipts'); if ($media) { diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 57fa52007..e23c6ecad 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -698,4 +698,19 @@ public function changeInvoiceStatus($amount) $this->save(); } + + public static function deleteInvoices($ids) + { + foreach ($ids as $id) { + $invoice = self::find($id); + + if ($invoice->transactions()->exists()) { + $invoice->transactions()->delete(); + } + + $invoice->delete(); + } + + return true; + } } diff --git a/app/Models/PaymentMethod.php b/app/Models/PaymentMethod.php index 898fbd364..23723c8d4 100644 --- a/app/Models/PaymentMethod.php +++ b/app/Models/PaymentMethod.php @@ -31,6 +31,11 @@ public function payments() return $this->hasMany(Payment::class); } + public function expenses() + { + return $this->hasMany(Expense::class); + } + public function company() { return $this->belongsTo(Company::class);