diff --git a/app/Enums/Planning/ActivityType.php b/app/Enums/Planning/ActivityType.php new file mode 100644 index 0000000..1fe2346 --- /dev/null +++ b/app/Enums/Planning/ActivityType.php @@ -0,0 +1,33 @@ + trans('Support'), + 'rnd' => trans('R&D'), + 'dev' => trans('Développement/Facturable'), + 'projects' => trans('Projets & Réunions'), + 'management' => trans('Organisation management'), + 'absence' => trans('Absence'), + ]; + } +} diff --git a/app/Http/Controllers/ScheduleController.php b/app/Http/Controllers/ScheduleController.php index a686e4d..b44da9a 100644 --- a/app/Http/Controllers/ScheduleController.php +++ b/app/Http/Controllers/ScheduleController.php @@ -2,13 +2,20 @@ namespace App\Http\Controllers; -use App\Enums\Planning\CellType; use App\Enums\Role; +use App\Models\Schedule; use App\Models\User; +use App\Models\WeekPlanning; +use Carbon\Carbon; use Illuminate\Http\Request; -use Illuminate\Support\Carbon; use Carbon\CarbonImmutable; +/** + * Alternate planner for Dev Team + * + * @author Olivier Legat + * + */ class ScheduleController extends Controller { public function index(Request $request) @@ -17,78 +24,129 @@ class ScheduleController extends Controller $nbweek = 2; $users = User::where('role', Role::developper())->get(); if ($request->p) { - $date = CarbonImmutable::createFromFormat('d/m/Y',$request->p)->startOfWeek(); + $date = CarbonImmutable::createFromFormat('d/m/Y', $request->p)->startOfWeek(); } - $schedules = array( - //userid - 1 => - array( - //day - '29/11/2021'=> - array( - //slot - 0 => array( - //elements - 'text'=>'item1', - 'type'=>'projects' - ), - 1 => array( - 'text'=>'item2', - 'type'=>'dev' - ), - 2 => array( - 'text'=>'item3', - 'type'=>'dev' - ), - ), - '01/12/2021' => - array( - 1 => array( - 'text'=>'item2', - 'type'=>'rnd' - ), - 2 => array( - 'text'=>'item3', - 'type'=>'support' - ), - ), - ), - 7 => - array( - //day - '03/12/2021'=> - array( - //slot - 0 => array( - //elements - 'text'=>'item1', - 'type'=>'projects' - ), - 1 => array( - 'text'=>'item2', - 'type'=>'projects' - ), - 3 => array( - 'text'=>'item3', - 'type'=>'dev' - ), - ), - '06/12/2021' => - array( - 1 => array( - 'text'=>'item2', - 'type'=>'support' - ), - 2 => array( - 'text'=>'item3', - 'type'=>'projects' - ), - ), - ), + $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; + } + + return view('schedule.index', compact('users', 'date', 'nbweek', 'arSchedules')); + } + + /** + * Migrates data from week_plannings + * to schedules table + * + * @author Olivier Legat + * + */ + public function import(Request $request) + { + // $schedule_test = Schedule::first(); + // dd($schedule_test->relatedModel); + + $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 view('schedule.index', compact('users','date','nbweek','schedules')); + return $converter[$value] ?? null; } } diff --git a/app/Models/Schedule.php b/app/Models/Schedule.php new file mode 100644 index 0000000..01b2c00 --- /dev/null +++ b/app/Models/Schedule.php @@ -0,0 +1,65 @@ + ActivityType::class, + 'model_lie' => CellModel::class + ]; + + /** + * The attributes that should be cast to native types. + * + * @var array + */ + protected $casts = [ + 'date' => 'date', + ]; + + protected $fillable = [ + 'user_id', + 'date', + 'slot', + 'activity', + 'description', + 'id_lie', + 'model_lie' + ]; + + public function user() + { + return $this->belongsTo(User::class); + } + + //relationship to linked objet + public function relatedModel() + { + switch ($this->model_lie) { + case 'sites': + return $this->belongsTo(Site::class, 'id_lie'); + break; + case 'projects': + return $this->belongsTo(Project::class, 'id_lie'); + break; + case 'tickets': + return $this->belongsTo(Ticket::class, 'id_lie'); + break; + default: + //no model + return new BelongsTo($this->newQuery(), $this, '', '', ''); + break; + } + } +} diff --git a/database/migrations/2021_12_03_195755_create_schedules_table.php b/database/migrations/2021_12_03_195755_create_schedules_table.php new file mode 100644 index 0000000..21aaf36 --- /dev/null +++ b/database/migrations/2021_12_03_195755_create_schedules_table.php @@ -0,0 +1,39 @@ +id(); + $table->foreignId('user_id')->constrained('users'); + $table->date('date'); + $table->smallInteger('slot'); + $table->string('activity')->nullable(); + $table->text('description'); + $table->bigInteger('id_lie')->nullable(); + $table->string('model_lie')->nullable(); + $table->timestamps(); + $table->unique(['user_id','date','slot'], 'user_day_slot_unique'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('schedules'); + } +} diff --git a/resources/views/schedule/index.blade.php b/resources/views/schedule/index.blade.php index 339850a..4f11575 100644 --- a/resources/views/schedule/index.blade.php +++ b/resources/views/schedule/index.blade.php @@ -101,11 +101,11 @@ @foreach($users as $user)
{{$user->name}}
- @for ($k=0; $k < 4; $k++) + @for ($k=1; $k < 5; $k++) @for ($i = 0; $i < ($nbweek*7); $i++) -
- {{$schedules[$user->id][$date->addDays($i)->format('d/m/Y')][$k]['text'] ?? ''}} +
+ {{$arSchedules[$user->id][$date->addDays($i)->format('d/m/Y')][$k]['text'] ?? ''}}
@php if( $date->addDays($i)->dayOfWeek == 5) { diff --git a/routes/web.php b/routes/web.php index c2a179f..8554ae5 100644 --- a/routes/web.php +++ b/routes/web.php @@ -86,5 +86,6 @@ Route::middleware(['auth'])->group(function () { Route::prefix('/schedule')->name('schedule.')->group(function () { Route::get('/', [ScheduleController::class, 'index'])->name('index'); + Route::get('/import', [ScheduleController::class, 'import'])->name('import'); }); });