178 lines
6.5 KiB
PHP
178 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\DailyReports\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Modules\DailyReports\Models\DailyReport;
|
|
use Modules\DailyReports\Http\Requests\StoreDailyReportRequest;
|
|
use Modules\DailyReports\Http\Requests\UpdateDailyReportRequest;
|
|
use Inertia\Inertia;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DailyReportsController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$projectUlid = $request->query('project');
|
|
$project = $projectUlid ? \Modules\ProjectManagement\Models\Project::where('ulid', $projectUlid)->firstOrFail() : null;
|
|
|
|
$query = DailyReport::with(['user']);
|
|
if ($project) {
|
|
$query->where('project_id', $project->id);
|
|
}
|
|
|
|
$reports = $query->orderBy('report_date', 'desc')->paginate(15);
|
|
|
|
return Inertia::render('DailyReports::Index', [
|
|
'project' => $project,
|
|
'projects' => \Modules\ProjectManagement\Models\Project::with('parentProject:id,ulid,name,code')->get(['id', 'ulid', 'name', 'code', 'project_type', 'parent_project_id']),
|
|
'reports' => $reports,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create(Request $request)
|
|
{
|
|
$projectUlid = $request->query('project');
|
|
$project = $projectUlid ? \Modules\ProjectManagement\Models\Project::where('ulid', $projectUlid)->firstOrFail() : null;
|
|
|
|
return Inertia::render('DailyReports::Create', [
|
|
'project' => $project,
|
|
'projects' => \Modules\ProjectManagement\Models\Project::with('parentProject:id,ulid,name,code')->get(['id', 'ulid', 'name', 'code', 'project_type', 'parent_project_id']),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StoreDailyReportRequest $request)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$report = DailyReport::create(array_merge(
|
|
$request->safe()->except(['labors', 'equipment', 'activities', 'materials', 'issues', 'attachments']),
|
|
['user_id' => auth()->id()]
|
|
));
|
|
|
|
if ($request->has('labors')) {
|
|
$report->labors()->createMany($request->labors);
|
|
}
|
|
if ($request->has('equipment')) {
|
|
$report->equipment()->createMany($request->equipment);
|
|
}
|
|
if ($request->has('activities')) {
|
|
$report->activities()->createMany($request->activities);
|
|
}
|
|
if ($request->has('materials')) {
|
|
$report->materials()->createMany($request->materials);
|
|
}
|
|
if ($request->has('issues')) {
|
|
$report->issues()->createMany($request->issues);
|
|
}
|
|
|
|
if ($request->hasFile('attachments')) {
|
|
foreach ($request->file('attachments') as $file) {
|
|
$report->addMedia($file)->toMediaCollection('attachments');
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return redirect()->route('projects.daily-reports.index', ['project' => $report->project->ulid])
|
|
->with('success', 'Daily report created successfully.');
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return back()->with('error', 'Error creating report: ' . $e->getMessage())->withInput();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show(DailyReport $daily_report)
|
|
{
|
|
$daily_report->load(['project', 'user', 'labors', 'equipment', 'activities', 'materials', 'issues', 'media']);
|
|
|
|
return Inertia::render('DailyReports::Show', [
|
|
'report' => $daily_report,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(DailyReport $daily_report)
|
|
{
|
|
$daily_report->load(['project', 'labors', 'equipment', 'activities', 'materials', 'issues', 'media']);
|
|
|
|
return Inertia::render('DailyReports::Edit', [
|
|
'report' => $daily_report,
|
|
'projects' => \Modules\ProjectManagement\Models\Project::with('parentProject:id,ulid,name,code')->get(['id', 'ulid', 'name', 'code', 'project_type', 'parent_project_id']),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(UpdateDailyReportRequest $request, DailyReport $daily_report)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$daily_report->update($request->safe()->except(['labors', 'equipment', 'activities', 'materials', 'issues', 'attachments']));
|
|
|
|
// Sync nested relations (delete and recreate for simplicity)
|
|
if ($request->has('labors')) {
|
|
$daily_report->labors()->delete();
|
|
$daily_report->labors()->createMany($request->labors);
|
|
}
|
|
if ($request->has('equipment')) {
|
|
$daily_report->equipment()->delete();
|
|
$daily_report->equipment()->createMany($request->equipment);
|
|
}
|
|
if ($request->has('activities')) {
|
|
$daily_report->activities()->delete();
|
|
$daily_report->activities()->createMany($request->activities);
|
|
}
|
|
if ($request->has('materials')) {
|
|
$daily_report->materials()->delete();
|
|
$daily_report->materials()->createMany($request->materials);
|
|
}
|
|
if ($request->has('issues')) {
|
|
$daily_report->issues()->delete();
|
|
$daily_report->issues()->createMany($request->issues);
|
|
}
|
|
|
|
if ($request->hasFile('attachments')) {
|
|
foreach ($request->file('attachments') as $file) {
|
|
$daily_report->addMedia($file)->toMediaCollection('attachments');
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return redirect()->route('projects.daily-reports.index', ['project' => $daily_report->project->ulid])
|
|
->with('success', 'Daily report updated successfully.');
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return back()->with('error', 'Error updating report: ' . $e->getMessage())->withInput();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(DailyReport $daily_report)
|
|
{
|
|
$daily_report->delete();
|
|
return redirect()->back()->with('success', 'Daily report deleted successfully.');
|
|
}
|
|
}
|