Skip to content

Commit

Permalink
solve payment method delete issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvirsinh_gohil committed Jan 13, 2022
1 parent e3f3809 commit dd324c8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
Expand Up @@ -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,
Expand Down
Expand Up @@ -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([
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/V1/PDF/DownloadReceiptController.php
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions app/Models/Invoice.php
Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions app/Models/PaymentMethod.php
Expand Up @@ -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);
Expand Down

0 comments on commit dd324c8

Please sign in to comment.