where('credit', 0) ->where('type', DepositHistory::TYPE_DEPOSIT); if ($request->q != '') { $deposits->where(function ($query) use ($request) { $query->where('description', 'like', "%$request->q%") ->orWhereHas('customer', function ($query) use ($request) { $query->where('fullname', 'like', "%$request->q%") ->orWhere('email', 'like', "%$request->q%") ->orWhere('phone', 'like', "%$request->q%"); }); }); } $sortBy = 'updated_at'; $sortRule = 'desc'; if ($request->sortBy != '' && $request->sortRule != '') { $sortBy = $request->sortBy; $sortRule = $request->sortRule; } $deposits->orderBy($sortBy, $sortRule); if ($request->status != '') { $deposits->where('is_valid', $request->status); } $customers = Customer::with(['paylater'])->orderBy('deposit_balance', 'desc'); $stats = [ 'deposit_this_month' => DepositHistory::whereMonth('created_at', now()->month) ->whereYear('created_at', now()->year) ->where('is_valid', DepositHistory::STATUS_VALID) ->where('type', DepositHistory::TYPE_DEPOSIT) ->sum('debit'), 'deposit_today' => DepositHistory::whereDate('created_at', now()) ->where('is_valid', DepositHistory::STATUS_VALID) ->where('type', DepositHistory::TYPE_DEPOSIT) ->sum('debit'), 'paylater_this_month' => PaylaterHistory::whereMonth('created_at', now()->month) ->where('is_valid', PaylaterHistory::STATUS_VALID) ->where('type', DepositHistory::TYPE_DEPOSIT) ->whereYear('created_at', now()->year) ->sum('debit'), 'paylater_today' => PaylaterHistory::whereDate('created_at', now()) ->where('is_valid', PaylaterHistory::STATUS_VALID) ->where('type', DepositHistory::TYPE_DEPOSIT) ->sum('debit'), ]; return inertia('DepositHistory/Index', [ 'deposits' => $deposits->paginate(), '_q' => $request->q, '_sortBy' => $sortBy, '_sortRule' => $sortRule, 'customers' => $customers->paginate(10, '*', 'customer_page'), 'stats' => $stats, ]); } public function edit(DepositHistory $deposit) { return inertia('DepositHistory/Form', [ 'deposit' => $deposit->load(['customer', 'account', 'depositLocation', 'editor']), ]); } public function update(Request $request, DepositHistory $deposit) { $request->validate([ 'is_valid' => [ 'required', Rule::in([DepositHistory::STATUS_VALID, DepositHistory::STATUS_REJECT]), ], 'debit' => 'required|numeric', ]); if ($request->is_valid == DepositHistory::STATUS_REJECT) { $request->validate(['reject_reason' => 'required|string']); } DB::beginTransaction(); $deposit->update([ 'debit' => $request->debit, 'is_valid' => $request->is_valid, 'note' => $request->reject_reason, ]); if ($request->is_valid == DepositHistory::STATUS_VALID) { $deposit->update_customer_balance(); $deposit->create_notification_user(); } DB::commit(); return redirect()->route('deposit.index') ->with('message', ['type' => 'success', 'message' => 'Item has beed updated']); } }