where('business_id', auth()->user()->business_id) ->when(request('search'), function ($q) use ($request) { $q->where(function ($q) use ($request) { $q->where('balance', 'like', '%' . $request->search . '%') ->orWhere('name', 'like', '%' . $request->search . '%') ->orWhere('meta->account_number', 'like', '%' . $request->search . '%') ->orWhere('meta->bank_name', 'like', '%' . $request->search . '%') ->orWhere('meta->branch_name', 'like', '%' . $request->search . '%') ->orWhere('meta->account_holder', 'like', '%' . $request->search . '%'); }); }) ->latest() ->paginate($request->per_page ?? 20)->appends($request->query()); if ($request->ajax()) { return response()->json([ 'data' => view('business::banks.datas', compact('payment_types'))->render() ]); } return view('business::banks.index', compact('payment_types')); } public function store(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'branch_id' => 'nullable|exists:branches,id', 'opening_balance' => 'nullable|numeric|min:0', 'opening_date' => 'nullable|date', 'show_in_invoice' => 'boolean', 'meta' => 'nullable|array', 'meta.account_number' => 'nullable|string|max:255', 'meta.routing_number' => 'nullable|string|max:255', 'meta.upi_id' => 'nullable|string|max:255', 'meta.bank_name' => 'nullable|string|max:255', 'meta.branch' => 'nullable|string|max:255', 'meta.account_holder' => 'nullable|string|max:255', ]); $forbidden = ['cash', 'cheque']; if (in_array(strtolower($request->name), $forbidden)) { return response()->json([ 'message' => __('You cannot create a bank account with this name.'), ], 422); } PaymentType::create($request->except('business_id', 'show_in_invoice', 'balance', 'opening_balance') + [ 'business_id' => auth()->user()->business_id, 'opening_balance' => $request->opening_balance ?? 0, 'balance' => $request->opening_balance ?? 0, 'show_in_invoice' => $request->show_in_invoice, ]); return response()->json([ 'message' => __('Bank created Successfully'), 'redirect' => route('business.banks.index'), ]); } public function update(Request $request, string $id) { $request->validate([ 'name' => 'required|string|max:255', 'branch_id' => 'nullable|exists:branches,id', 'opening_balance' => 'nullable|numeric|min:0', 'opening_date' => 'nullable|date', 'show_in_invoice' => 'boolean', 'meta' => 'nullable|array', 'meta.account_number' => 'nullable|string|max:255', 'meta.routing_number' => 'nullable|string|max:255', 'meta.upi_id' => 'nullable|string|max:255', 'meta.bank_name' => 'nullable|string|max:255', 'meta.branch' => 'nullable|string|max:255', 'meta.account_holder' => 'nullable|string|max:255', ]); $payment_type = PaymentType::findOrFail($id); $forbidden = ['cash', 'cheque']; if (in_array(strtolower($request->name), $forbidden)) { return response()->json([ 'message' => __('You cannot create a bank account with this name.'), ], 422); } $hasTransactions = $payment_type->transactions()->exists(); if ($hasTransactions) { return response()->json([ 'message' => __("You can't Change opening balance because this bank already has transactions.") ], 422); } $updateData = $request->except('business_id'); // If transactions exist, do not update balance if ($hasTransactions) { unset($updateData['opening_balance'], $updateData['balance']); } else { // update balance $updateData['balance'] = $request->opening_balance ?? $payment_type->balance; } $payment_type->update($updateData); return response()->json([ 'message' => __('Bank Updated Successfully'), 'redirect' => route('business.banks.index'), ]); } public function destroy($id) { $paymentType = PaymentType::findOrFail($id); // Check if this payment type is used in any transaction $hasTransactions = $paymentType->transactions()->exists(); $balance = $paymentType->balance ?? 0; if ($hasTransactions && $balance != 0) { return response()->json([ 'message' => __('Bank can’t be deleted. It has transactions or balance.') ], 400); } $paymentType->delete(); return response()->json([ 'message' => __('Bank Deleted Successfully'), 'redirect' => route('business.banks.index'), ]); } public function deleteAll(Request $request) { $hasTransactions = Transaction::where(function ($query) use ($request) { $query->whereIn('payment_type_id', $request->ids) ->orWhereIn('from_bank', $request->ids) ->orWhereIn('to_bank', $request->ids); }) ->whereHas('paymentType', function ($q) { $q->where('balance', '!=', 0); }) ->pluck('payment_type_id') ->toArray(); PaymentType::whereIn('id', $request->ids) ->where(function ($q) use ($hasTransactions) { $q->whereNotIn('id', $hasTransactions) ->orWhere(function ($q) use ($hasTransactions) { $q->whereIn('id', $hasTransactions) ->where('balance', 0); }); }) ->delete(); if (count($hasTransactions) > 0) { return response()->json([ 'message' => 'Some banks were not deleted because they have transactions.', 'redirect' => route('business.banks.index') ], 422); } return response()->json([ 'message' => 'Selected Banks Deleted Successfully.', 'redirect' => route('business.banks.index'), ]); } public function getBanks() { $banks = PaymentType::select('id', 'name') ->where('business_id', auth()->user()->business_id) ->get(); return response()->json($banks); } }