ldc-supportweb/app/Http/Controllers/ScheduleController.php
2021-12-18 16:52:55 +01:00

117 lines
4.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Enums\Planning\ActivityType;
use App\Enums\Role;
use App\Models\Schedule;
use App\Models\User;
use App\Models\WeekPlanning;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Carbon\CarbonImmutable;
/**
* Alternate planner for Dev Team
*
* @author Olivier Legat <olivier.legat@ldc.fr>
*
*/
class ScheduleController extends Controller
{
public function index(Request $request)
{
$date = CarbonImmutable::now()->startOfWeek();
$nbweeks = 2;
if ($request->p) {
$date = CarbonImmutable::createFromFormat('d/m/Y', $request->p)->startOfWeek();
}
$dbusers = User::where('role', Role::developper())->get();
$users = [];
foreach ($dbusers as $user) {
$users[$user->id] = ['name' => $user->name];
}
$schedules = Schedule::whereDate('date', '>=', $date)
->whereDate('date', '<=', $date->addDays($nbweeks * 7 - 1))
->orderBy('user_id')
->orderBy('slot')
->orderBy('date')
->get();
$arSchedules = array();
$prevUserId = null;
$prevDate = null;
$prevSlot = null;
foreach ($schedules as $schedule) {
$id_lie = null;
if(!is_null($schedule->model_lie)) {
if (isset($schedule->relatedModel->name)) {
$id_lie = array('label'=>$schedule->relatedModel->name, 'value'=>$schedule->id_lie);
} else {
$id_lie = array('label'=>'Ticket '.$schedule->id_lie, 'value'=>$schedule->id_lie);
}
}
if ($prevUserId != $schedule->user_id) {
//nouveau user
$arSchedules[$schedule->user_id] = array(
$schedule->slot => array(
$schedule->date->format('d/m/Y') => array(
'id' => $schedule->id,
'type' => $schedule->activity,
'text' => $schedule->description,
'id_lie' => $id_lie, //$schedule->id_lie,
'model_lie' => $schedule->model_lie,
),
)
);
} else {
//same user
if ($prevSlot != $schedule->slot) {
//new slot
$arSchedules[$schedule->user_id][$schedule->slot] = array(
$schedule->date->format('d/m/Y') => array(
'id' => $schedule->id,
'type' => $schedule->activity,
'text' => $schedule->description,
'id_lie' => $id_lie, //$schedule->id_lie,
'model_lie' => $schedule->model_lie,
),
);
} else {
//same slot
if ($prevDate != $schedule->date) {
//new date
$arSchedules[$schedule->user_id][$schedule->slot][$schedule->date->format('d/m/Y')] = array(
'id' => $schedule->id,
'type' => $schedule->activity,
'text' => $schedule->description,
'id_lie' => $id_lie, //$schedule->id_lie,
'model_lie' => $schedule->model_lie,
);
} else {
//same date
//should never be here
}
}
}
$prevUserId = $schedule->user_id;
$prevDate = $schedule->date;
$prevSlot = $schedule->slot;
}
$weekCells = array();
for ($i=0; $i < $nbweeks; $i++) {
$weekCells[] = array(
"num" => $date->addDays($i * 7)->weekOfYear,
"month" => $date->addDays($i * 7)->shortLocaleMonth,
"year" => $date->addDays($i * 7)->format('Y')
);
}
return view('schedule.index', compact('users', 'date', 'weekCells', 'arSchedules'));
}
}