52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\DailyReport;
|
|
use App\Models\ReportOverride;
|
|
use App\Models\StaffMember;
|
|
use App\Http\Requests\StoreDailyReportRequest;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\Mail\DailyReportSubmitted;
|
|
|
|
class DailyReportController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$reports = auth()->user()->dailyReports()
|
|
->orderBy('report_date', 'desc')
|
|
->paginate(15);
|
|
|
|
return view('reports.index', compact('reports'));
|
|
}
|
|
|
|
public function store(StoreDailyReportRequest $request)
|
|
{
|
|
$report = DailyReport::create([
|
|
'user_id' => auth()->id(),
|
|
'report_date' => $request->report_date,
|
|
'content' => $request->content,
|
|
]);
|
|
|
|
// Reward the staff member with points (e.g., 10 points per report)
|
|
$staff = auth()->user();
|
|
$staff->increment('report_points', 10);
|
|
|
|
ReportOverride::where('user_id', auth()->id())
|
|
->where('report_date', $request->report_date)
|
|
->delete();
|
|
|
|
/// modify this to make it dynamic
|
|
$managementEmails = StaffMember::with('department')->whereIn('department_id', [5])->pluck('email')->toArray();
|
|
|
|
if (env('APP_ENV') == 'production') {
|
|
Mail::to($managementEmails)->send(new DailyReportSubmitted($report, auth()->user()));
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Daily report submitted successfully! You earned 10 points.',
|
|
'new_points_total' => $staff->report_points // Optional: send back to update UI dynamically
|
|
]);
|
|
}
|
|
} |