/
home
/
sjslayjy
/
public_html
/
ccbfsoution
/
app
/
Http
/
Controllers
/
Admin
/
Upload File
HOME
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; USE CArbon\Carbon; use Illuminate\Pagination\Paginator; class NotificationController extends Controller { public function create(Request $request) { $blocks = DB::table('users') ->whereNotNull('block_name') ->distinct() ->pluck('block_name'); $plots = DB::table('users') ->whereNotNull('plot_name') ->select('plot_name', 'block_name') ->get(); $users = DB::table('users') ->whereNotNull('block_name') ->whereNotNull('plot_name') ->select('id', 'name', 'block_name', 'plot_name', 'role') ->get(); // Weekly Notifications $startOfWeek = Carbon::now()->startOfWeek(); $weeklyNotifications = []; for ($i = 0; $i < 7; $i++) { $day = $startOfWeek->copy()->addDays($i); $dayName = $day->format('D'); $notifications = DB::select(" SELECT users.name, notifications.message, notifications.status, notifications.priority, notifications.notification_type FROM notifications JOIN users ON users.id = notifications.user_id WHERE DATE(notifications.created_at) = ? ", [$day->format('Y-m-d')]); $weeklyNotifications[$dayName] = $notifications; } // Date filter $startDate = $request->get('start_date'); $endDate = $request->get('end_date'); $currentTasksQuery = DB::table('notifications') ->join('users', 'users.id', '=', 'notifications.user_id') ->orderByDesc('notifications.created_at') ->select( 'users.name', 'users.block_name as block', 'users.plot_name as plot', 'notifications.message', 'notifications.priority', 'notifications.notification_type', 'notifications.created_at' ); if ($startDate && $endDate) { $currentTasksQuery->whereBetween(DB::raw('DATE(notifications.created_at)'), [$startDate, $endDate]); } else { $currentTasksQuery->whereDate('notifications.created_at', '>=', Carbon::now()->subDays(7)); } // Paginate current tasks $currentTasks = $currentTasksQuery->paginate(5)->withQueryString(); // Show 5 per page return view('notifications.create', compact( 'blocks', 'plots', 'users', 'weeklyNotifications', 'currentTasks' )); } // Get user details via AJAX public function getUserDetails($userId) { $user = DB::table('users') ->where('id', $userId) ->select('block_name', 'plot_name', 'role') ->first(); return response()->json($user); } // Get plots for selected block via AJAX public function getPlots($block) { $plots = DB::table('users') ->where('block_name', $block) ->distinct() ->pluck('plot_name'); return response()->json($plots); } // Store new notification public function store(Request $request) { $validated = $request->validate([ 'user_id' => 'required|exists:users,id', 'block' => 'required|string', 'plot' => 'required|string', 'priority' => 'required|in:Low,Medium,High,Critical', 'notification_type' => 'required|string', 'notification' => 'required|string', 'roll' => 'nullable|string', // Make roll optional ]); // Generate ST number $stNo = 'ST-' . strtoupper(Str::random(6)); // Insert notification with null roll if not provided DB::table('notifications')->insert([ 'st_no' => $stNo, 'user_id' => $validated['user_id'], 'block_name' => $validated['block'], 'plot_name' => $validated['plot'], 'priority' => $validated['priority'], 'notification_type' => $validated['notification_type'], 'roll' => $validated['roll'] ?? null, // Handle missing roll 'message' => $validated['notification'], 'status' => 'Pending', 'created_at' => now(), 'updated_at' => now(), ]); return redirect()->route('notifications.create')->with('success', 'Notification created successfully!'); } // Show weekly notifications public function weekly() { $startOfWeek = Carbon::now()->startOfWeek(); // Monday $weeklyNotifications = []; for ($i = 0; $i < 7; $i++) { $day = $startOfWeek->copy()->addDays($i); $dayName = $day->format('D'); // Mon, Tue, etc. $notifications = DB::select(" SELECT users.name, notifications.message, notifications.status FROM notifications JOIN users ON users.id = notifications.user_id WHERE DATE(notifications.created_at) = ? ", [$day->format('Y-m-d')]); $weeklyNotifications[$dayName] = $notifications; } // Get current tasks $currentTasks = DB::select(" SELECT users.name, notifications.message FROM notifications JOIN users ON users.id = notifications.user_id WHERE DATE(notifications.created_at) >= ? ORDER BY notifications.created_at DESC LIMIT 3 ", [Carbon::now()->subDays(7)->format('Y-m-d')]); return view('notifications.weekly', compact('weeklyNotifications', 'currentTasks')); } // Assign notification to user public function assign(Request $request, $notificationId) { $validated = $request->validate([ 'assigned_to' => 'required|exists:users,id', 'days' => 'required|array', 'days.*' => 'in:Mon,Tue,Wed,Thurs,Fri,Sat,Sun', ]); DB::transaction(function () use ($notificationId, $validated) { // Create assignment DB::table('notification_assignments')->insert([ 'notification_id' => $notificationId, 'assigned_to' => $validated['assigned_to'], 'assigned_by' => auth()->id(), 'status' => 'Assigned', 'assigned_at' => now(), ]); // Update notification status DB::table('notifications') ->where('id', $notificationId) ->update(['status' => 'Assigned']); // Create schedule entries $scheduleData = array_map(function ($day) use ($notificationId) { return [ 'notification_id' => $notificationId, 'day_of_week' => $day, 'status' => 'Assigned', ]; }, $validated['days']); DB::table('notification_schedule')->insert($scheduleData); }); return redirect()->back()->with('success', 'Notification assigned successfully!'); } }