ldc-supportweb/app/Http/Controllers/ScheduleController.php

153 lines
5.3 KiB
PHP
Raw Normal View History

2021-12-04 17:09:22 +01:00
<?php
namespace App\Http\Controllers;
use App\Enums\Role;
2021-12-05 15:01:32 +01:00
use App\Models\Schedule;
2021-12-04 17:09:22 +01:00
use App\Models\User;
2021-12-05 15:01:32 +01:00
use App\Models\WeekPlanning;
use Carbon\Carbon;
2021-12-04 17:09:22 +01:00
use Illuminate\Http\Request;
use Carbon\CarbonImmutable;
2021-12-05 15:01:32 +01:00
/**
* Alternate planner for Dev Team
*
* @author Olivier Legat <olivier.legat@ldc.fr>
*
*/
2021-12-04 17:09:22 +01:00
class ScheduleController extends Controller
{
public function index(Request $request)
{
$date = CarbonImmutable::now()->startOfWeek();
$nbweek = 2;
$users = User::where('role', Role::developper())->get();
if ($request->p) {
2021-12-05 15:01:32 +01:00
$date = CarbonImmutable::createFromFormat('d/m/Y', $request->p)->startOfWeek();
2021-12-04 17:09:22 +01:00
}
2021-12-05 15:01:32 +01:00
$schedules = Schedule::whereDate('date', '>=', $date)
->whereDate('date', '<=', $date->addDays($nbweek * 7))
->orderBy('user_id')
->orderBy('date')
->orderBy('slot')
->get();
$arSchedules = array();
$prevUserId = null;
$prevDate = null;
$prevSlot = null;
foreach ($schedules as $schedule) {
if ($prevUserId != $schedule->user_id) {
//nouveau user
$arSchedules[$schedule->user_id] = array(
$schedule->date->format('d/m/Y') => array(
$schedule->slot => array(
'type' => $schedule->activity,
'text' => $schedule->description,
'id_lie' => $schedule->id_lie,
'model_lie' => $schedule->model_lie,
),
)
);
} else {
//same user
if ($prevDate != $schedule->date) {
//new date
$arSchedules[$schedule->user_id][$schedule->date->format('d/m/Y')] = array(
$schedule->slot => array(
'type' => $schedule->activity,
'text' => $schedule->description,
'id_lie' => $schedule->id_lie,
'model_lie' => $schedule->model_lie,
),
);
} else {
//same date
if ($prevSlot != $schedule->slot) {
//new slot
$arSchedules[$schedule->user_id][$schedule->date->format('d/m/Y')][$schedule->slot] = array(
'type' => $schedule->activity,
'text' => $schedule->description,
'id_lie' => $schedule->id_lie,
'model_lie' => $schedule->model_lie,
);
} else {
//same slot ?
//should never en here
}
}
}
$prevUserId = $schedule->user_id;
$prevDate = $schedule->date;
$prevSlot = $schedule->slot;
}
2021-12-04 17:09:22 +01:00
2021-12-05 15:01:32 +01:00
return view('schedule.index', compact('users', 'date', 'nbweek', 'arSchedules'));
}
/**
* Migrates data from week_plannings
* to schedules table
*
* @author Olivier Legat <olivier.legat@ldc.fr>
*
*/
public function import(Request $request)
{
// $schedule_test = Schedule::first();
// dd($schedule_test->relatedModel);
2021-12-04 17:09:22 +01:00
2021-12-05 15:01:32 +01:00
$week_plannings = WeekPlanning::all();
foreach ($week_plannings as $oneweek) {
$user_id = $oneweek->user_id;
$plan = $oneweek->planning;
foreach ($plan as $time => $data) {
$date = Carbon::createFromTimestamp($time);
foreach ($data as $dayslot => $cell) {
$activite = $this->convertColorToType($cell["type"]);
$description = $cell["text"];
$id_objetlie = $cell["foreign"];
$type_objetlie = $cell["cellModel"]; //c'est une table d'après l'Enum CellModel
$collected = [
'user_id' => $user_id,
'date' => $date,
'slot' => $dayslot,
'activity' => $activite,
'description' => $description,
'id_lie' => $id_objetlie,
'model_lie' => $type_objetlie
];
//si activite null et description null ne pas enregistrer
if (!is_null($activite) && !is_null($description)) {
$schedule = new Schedule();
$schedule->fill($collected);
$schedule->save();
}
}
}
}
dump('finished');
}
/**
* convertir une value de couleur en type
*/
private function convertColorToType($value) {
$converter = array(
'#c6e0b4' => 'support',
'#b4c6e7' => 'rnd',
'#00b0f0' => 'dev',
'#ffe699' => 'projects',
'#f4b084' => 'management',
'#aeaaaa' => 'absence',
'abscence' => 'absence'
);
return $converter[$value] ?? null;
2021-12-04 17:09:22 +01:00
}
}