testapi/app/Http/Controllers/WorkHourController.php

169 lines
5.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Inertia\Inertia;
use App\Models\WorkHour;
use App\Models\SmartdokProfile;
use App\Models\Project;
class WorkHourController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request) {
$workHours = WorkHour::with(['user', 'project'])
->orderBy('created_at', 'desc')
->paginate(10)
->through(fn($wh) => [
'id' => $wh->id,
'user_id' => $wh->user_id,
'user_name' => $wh->user->name,
'ordinary_hours' => $wh->ordinary_hours,
'work_date' => $wh->work_date->toDateString(),
'comment' => $wh->comment,
'project_id' => $wh->project_id,
'project_name' => $wh->project->project_name,
]);
return Inertia::render('WorkHour/Index', [
'workHours' => $workHours,
'filters' => $request->only(['page']),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create() {
$profiles = SmartdokProfile::orderBy('name')
->get()
->map(fn($p) => ['id' => $p->id, 'name' => $p->name]);
$projects = Project::orderBy('project_name')
->get()
->map(fn($proj) => ['id' => $proj->id, 'project_name' => $proj->project_name]);
return Inertia::render('WorkHour/Create', [
'profiles' => $profiles,
'projects' => $projects,
'today' => now()->toDateString(),
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request) {
$data = $request->validate([
'user_id' => 'required|uuid|exists:smartdok_profiles,id',
'project_id' => 'required|integer|exists:projects,id',
'ordinary_hours' => 'required|numeric|min:0.5|multiple_of:0.5',
'work_date' => 'required|date',
'comment' => 'nullable|string',
]);
WorkHour::create([
'user_id' => $data['user_id'],
'project_id' => $data['project_id'],
'ordinary_hours' => $data['ordinary_hours'],
'work_date' => $data['work_date'],
'comment' => $data['comment'] ?? null,
]);
return redirect()
->route('smartdok.work-hours.index')
->with('success', 'Work hour entry created successfully.');
}
/**
* Display the specified resource.
*/
public function show(string $id) {
$wh = WorkHour::with(['user', 'project'])->findOrFail($id);
return Inertia::render('WorkHour/Show', [
'workHour' => [
'id' => $wh->id,
'user_id' => $wh->user_id,
'user_name' => $wh->user->name,
'ordinary_hours' => $wh->ordinary_hours,
'work_date' => $wh->work_date->toDateString(),
'comment' => $wh->comment,
'project_id' => $wh->project_id,
'project_name' => $wh->project->project_name,
],
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id) {
$wh = WorkHour::with(['user', 'project'])->findOrFail($id);
$profiles = SmartdokProfile::orderBy('name')
->get()
->map(fn($p) => ['id' => $p->id, 'name' => $p->name]);
$projects = Project::orderBy('project_name')
->get()
->map(fn($proj) => ['id' => $proj->id, 'project_name' => $proj->project_name]);
return Inertia::render('WorkHour/Edit', [
'workHour' => [
'id' => $wh->id,
'user_id' => $wh->user_id,
'ordinary_hours' => $wh->ordinary_hours,
'work_date' => $wh->work_date->toDateString(),
'comment' => $wh->comment,
'project_id' => $wh->project_id,
],
'profiles' => $profiles,
'projects' => $projects,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id) {
$wh = WorkHour::findOrFail($id);
$data = $request->validate([
'user_id' => 'required|uuid|exists:smartdok_profiles,id',
'project_id' => 'required|integer|exists:projects,id',
'ordinary_hours' => 'required|numeric|decimal:1|min:0.5',
'work_date' => 'required|date',
'comment' => 'nullable|string',
]);
$wh->update([
'user_id' => $data['user_id'],
'project_id' => $data['project_id'],
'ordinary_hours' => $data['ordinary_hours'],
'work_date' => $data['work_date'],
'comment' => $data['comment'] ?? null,
]);
return redirect()
->route('smartdok.work-hours.index')
->with('success', 'Work hour entry updated successfully.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id) {
WorkHour::findOrFail($id)->delete();
return redirect()
->route('smartdok.work-hours.index')
->with('success', 'Work hour entry deleted successfully.');
}
}