migrate data to simple db table
This commit is contained in:
parent
7aea5dbc94
commit
f6801d72cd
6 changed files with 271 additions and 75 deletions
33
app/Enums/Planning/ActivityType.php
Normal file
33
app/Enums/Planning/ActivityType.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace App\Enums\Planning;
|
||||
|
||||
use Kwaadpepper\Enum\BaseEnumRoutable;
|
||||
|
||||
/**
|
||||
* This is an enumeration class,
|
||||
* the following methods are its possible values
|
||||
*
|
||||
* @method static self support()
|
||||
* @method static self rnd()
|
||||
* @method static self dev()
|
||||
* @method static self projects()
|
||||
* @method static self management()
|
||||
* @method static self absence()
|
||||
*
|
||||
*/
|
||||
class ActivityType extends BaseEnumRoutable
|
||||
{
|
||||
|
||||
public static function labels()
|
||||
{
|
||||
return [
|
||||
'support' => trans('Support'),
|
||||
'rnd' => trans('R&D'),
|
||||
'dev' => trans('Développement/Facturable'),
|
||||
'projects' => trans('Projets & Réunions'),
|
||||
'management' => trans('Organisation management'),
|
||||
'absence' => trans('Absence'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -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 <olivier.legat@ldc.fr>
|
||||
*
|
||||
*/
|
||||
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 <olivier.legat@ldc.fr>
|
||||
*
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
65
app/Models/Schedule.php
Normal file
65
app/Models/Schedule.php
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\Planning\ActivityType;
|
||||
use App\Enums\Planning\CellModel;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Kwaadpepper\Enum\Traits\CastsEnums;
|
||||
|
||||
class Schedule extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use CastsEnums;
|
||||
|
||||
protected $enumCasts = [
|
||||
'activity' => 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateSchedulesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('schedules', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
}
|
||||
|
|
@ -101,11 +101,11 @@
|
|||
@foreach($users as $user)
|
||||
<!-- user $j -->
|
||||
<div class="name">{{$user->name}}</div>
|
||||
@for ($k=0; $k < 4; $k++)
|
||||
@for ($k=1; $k < 5; $k++)
|
||||
<!--line slot $k-->
|
||||
@for ($i = 0; $i < ($nbweek*7); $i++)
|
||||
<div class="day {{ $schedules[$user->id][$date->addDays($i)->format('d/m/Y')][$k]['type'] ?? '' }} {{$date->addDays($i)->shortEnglishDayOfWeek}}">
|
||||
{{$schedules[$user->id][$date->addDays($i)->format('d/m/Y')][$k]['text'] ?? ''}}
|
||||
<div class="day {{ $arSchedules[$user->id][$date->addDays($i)->format('d/m/Y')][$k]['type'] ?? '' }} {{$date->addDays($i)->shortEnglishDayOfWeek}}">
|
||||
{{$arSchedules[$user->id][$date->addDays($i)->format('d/m/Y')][$k]['text'] ?? ''}}
|
||||
</div>
|
||||
@php
|
||||
if( $date->addDays($i)->dayOfWeek == 5) {
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue