validated(); if ($validate) { try { // Add created_by field and default values $validate['created_by'] = auth()->id(); $validate['status'] = 'pending'; $validate['authorized_status'] = 'pending'; // Save to database PeriodeStatement::create($validate); return redirect() ->route('periode-statements.index') ->with('success', 'Periode Statement created successfully'); } catch (Exception $e) { return redirect() ->route('periode-statements.create') ->with('error', 'Failed to create Periode Statement: ' . $e->getMessage()); } } } /** * Show the form for creating a new resource. */ public function create() { return view('webstatement::periode-statement.create'); } /** * Show the specified resource. */ public function show($id) { $periodeStatement = PeriodeStatement::find($id); return view('webstatement::periode-statement.show', compact('periodeStatement')); } /** * Show the form for editing the specified resource. */ public function edit($id) { $periodeStatement = PeriodeStatement::find($id); return view('webstatement::periode-statement.create', compact('periodeStatement')); } /** * Remove the specified resource from storage. */ public function destroy($id) { try { // Add deleted_by field $periodeStatement = PeriodeStatement::find($id); $periodeStatement->deleted_by = auth()->id(); $periodeStatement->save(); // Delete from database $periodeStatement->delete(); echo json_encode(['success' => true, 'message' => 'Periode Statement deleted successfully']); } catch (Exception $e) { echo json_encode([ 'success' => false, 'message' => 'Failed to delete Periode Statement: ' . $e->getMessage() ]); } } /** * Provide data for datatables. */ public function dataForDatatables(Request $request) { if (is_null($this->user) || !$this->user->can('periode-statements.view')) { //abort(403, 'Sorry! You are not allowed to view periode statement.'); } // Retrieve data from the database $query = PeriodeStatement::query(); // Apply search filter if provided if ($request->has('search') && !empty($request->get('search'))) { $search = $request->get('search'); $query->where(function ($q) use ($search) { $q->where('periode', 'LIKE', "%$search%"); $q->orWhere('status', 'LIKE', "%$search%"); $q->orWhere('authorized_status', 'LIKE', "%$search%"); $q->orWhere('notes', 'LIKE', "%$search%"); }); } // Apply sorting if provided if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) { $order = $request->get('sortOrder'); $column = $request->get('sortField'); $query->orderBy($column, $order); } // Get the total count of records $totalRecords = $query->count(); // Apply pagination if provided if ($request->has('page') && $request->has('size')) { $page = $request->get('page'); $size = $request->get('size'); $offset = ($page - 1) * $size; // Calculate the offset $query->skip($offset)->take($size); } // Get the filtered count of records $filteredRecords = $query->count(); // Get the data for the current page $data = $query->get(); // Calculate the page count $pageCount = ceil($filteredRecords / ($request->get('size') ?: 1)); // Calculate the current page number $currentPage = $request->get('page') ?: 1; // Return the response data as a JSON object return response()->json([ 'draw' => $request->get('draw'), 'recordsTotal' => $totalRecords, 'recordsFiltered' => $filteredRecords, 'pageCount' => $pageCount, 'page' => $currentPage, 'totalCount' => $totalRecords, 'data' => $data, ]); } /** * Delete multiple records. */ public function deleteMultiple(Request $request) { $ids = $request->input('ids'); // Add deleted_by to each record $periodeStatements = PeriodeStatement::whereIn('id', $ids)->get(); foreach ($periodeStatements as $periodeStatement) { $periodeStatement->deleted_by = auth()->id(); $periodeStatement->save(); } PeriodeStatement::whereIn('id', $ids)->delete(); return response()->json(['message' => 'Periode Statements deleted successfully']); } /** * Export data to Excel. */ public function export() { return Excel::download(new PeriodeStatementExport, 'periode-statements.xlsx'); } /** * Process the specified periode statement. */ public function process($id) { try { $periodeStatement = PeriodeStatement::find($id); // Update status to processing $periodeStatement->update([ 'status' => 'processing', 'notes' => ($periodeStatement->notes ? $periodeStatement->notes . "\n" : '') . 'Processing started at ' . Carbon::now() ->format('Y-m-d H:i:s'), 'updated_by' => auth()->id(), 'processed_at' => Carbon::now(), ]); return redirect() ->route('periode-statements.index') ->with('success', 'Periode statement processing has been initiated.'); } catch (Exception $e) { return redirect() ->route('periode-statements.index') ->with('error', 'Failed to process Periode Statement: ' . $e->getMessage()); } } /** * Update the specified resource in storage. */ public function update(PeriodeStatementRequest $request, $id) { $validate = $request->validated(); if ($validate) { try { // Add updated_by field $validate['updated_by'] = auth()->id(); if (request()->get('status') == 'approved') { $validate['status'] = 'completed'; $validate['authorized_status'] = 'approved'; $validate['processed_at'] = Carbon::now(); } else if (request()->get('status') == 'rejected') { $validate['status'] = 'failed'; $validate['authorized_status'] = 'rejected'; } $periodeStatement = PeriodeStatement::find($id); $validate['notes'] = ($periodeStatement->notes ? $periodeStatement->notes . "\n" : '') . request()->get('notes'); $validate['authorized_by'] = auth()->id(); $validate['authorized_at'] = Carbon::now(); // Update in database $periodeStatement->update($validate); return redirect() ->route('periode-statements.index') ->with('success', 'Periode Statement updated successfully'); } catch (Exception $e) { return redirect() ->route('periode-statements.edit', $id) ->with('error', 'Failed to update Periode Statement: ' . $e->getMessage()); } } } /** * Show the form for authorizing the specified resource. */ public function showAuthorize($id) { $periodeStatement = PeriodeStatement::find($id); return view('webstatement::periode-statement.authorize', compact('periodeStatement')); } /** * Authorize the specified resource in storage. */ public function authorize(Request $request, $id) { try { $periodeStatement = PeriodeStatement::find($id); $notes = $periodeStatement->notes ?? ''; if ($request->authorization_notes) { $notes .= ($notes ? "\n" : '') . 'Authorization note: ' . $request->authorization_notes; } $periodeStatement->update([ 'authorized_status' => $request->authorized_status, 'authorized_by' => auth()->id(), 'authorized_at' => Carbon::now(), 'notes' => $notes, 'updated_by' => auth()->id(), ]); return redirect() ->route('periode-statements.index') ->with('success', 'Periode statement has been ' . $request->authorized_status . '.'); } catch (Exception $e) { return redirect() ->route('periode-statements.index') ->with('error', 'Failed to authorize Periode Statement: ' . $e->getMessage()); } } /** * Display a listing of the resources pending authorization. */ public function pendingAuthorization() { return view('webstatement::periode-statement.pending_authorization'); } /** * Complete the specified periode statement. */ public function complete($id) { try { $periodeStatement = PeriodeStatement::find($id); // Update status to completed $periodeStatement->update([ 'status' => 'completed', 'notes' => ($periodeStatement->notes ? $periodeStatement->notes . "\n" : '') . 'Completed at ' . Carbon::now() ->format('Y-m-d H:i:s'), 'updated_by' => auth()->id(), ]); return redirect() ->route('periode-statements.index') ->with('success', 'Periode statement has been marked as completed.'); } catch (Exception $e) { return redirect() ->route('periode-statements.index') ->with('error', 'Failed to complete Periode Statement: ' . $e->getMessage()); } } /** * Mark the specified periode statement as failed. */ public function fail(Request $request, $id) { try { $periodeStatement = PeriodeStatement::find($id); // Update status to failed $periodeStatement->update([ 'status' => 'failed', 'notes' => ($periodeStatement->notes ? $periodeStatement->notes . "\n" : '') . 'Failed at ' . Carbon::now() ->format('Y-m-d H:i:s') . '. Reason: ' . $request->failure_reason, 'updated_by' => auth()->id(), ]); return redirect() ->route('periode-statements.index') ->with('success', 'Periode statement has been marked as failed.'); } catch (Exception $e) { return redirect() ->route('periode-statements.index') ->with('error', 'Failed to mark Periode Statement as failed: ' . $e->getMessage()); } } }