- Add personnal stats

This commit is contained in:
Vincent BRIDIER 2022-02-09 13:11:29 +00:00
parent 47a82ef063
commit 7fd62e5cc3
5 changed files with 91 additions and 0 deletions

View file

@ -3,9 +3,11 @@
namespace App\Http\Controllers;
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Models\Schedule;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Kwaadpepper\CrudPolicies\Traits\CrudController;
class UserController extends Controller
@ -31,4 +33,33 @@ class UserController extends Controller
$request = new Request(['email' => $user->email]);
return app(ForgotPasswordController::class)->sendResetLinkEmail($request);
}
public function stats()
{
$user = Auth::user();
$stats = [];
for ($i = 2021; $i <= date('Y'); $i++) {
$start = $i . '-03-01';
$end = $i + 1 . '-02-' . $this->getLastDayFebruary($i + 1);
$datas = $user->schedules()->selectRaw('count(id) as nb, activity')
->whereBetween('date', [$start, $end])
->groupBy('activity')
->orderBy('activity', 'ASC')
->get();
$stats[sprintf("%d/%d", $i, $i + 1)] = $datas;
}
return view('users.stats', ['stats' => $stats]);
}
private function getLastDayFebruary($year)
{
if ((is_int($year / 4) && !is_int($year / 100)) || is_int($year / 400)) {
return 29;
}
return 28;
}
}

View file

@ -186,6 +186,11 @@ class User extends Authenticatable
return $this->belongsToMany(Site::class);
}
public function schedules()
{
return $this->hasMany(Schedule::class);
}
public function sendMailConfirm()
{
dispatch(new SendUserCreatedEmail($this));

View file

@ -65,6 +65,11 @@
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
@if (Auth::user()->role->value === Role::developper()->value)
<a class="dropdown-item" href="{{ route('users.stats') }}">
{{ __('Statistiques') }}
</a>
@endif
<a class="dropdown-item" href="{{ route('users.passwordReset') }}">
{{ __('Identifiants') }}
</a>

View file

@ -0,0 +1,49 @@
@extends('layout.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Statistiques') }}</div>
<div class="card-body">
@foreach ($stats as $index => $stat)
<table class="table">
<thead class="table-dark">
<tr>
<th>{{ $index }}</th>
<th>{{ __('Jour(s)') }}</th>
<th>%</th>
</tr>
</thead>
<tbody>
@php
$workdays = 203;
if (strpos($index, '2021') !== false) {
$workdays = 92;
}
$sum = 0;
@endphp
@foreach ($stat as $data)
<tr style="background-color: {{ CellType::make($data->activity->value)->value }};">
<td><b>{{ $data->activity->label }}</b></td>
<td><b>{{ $data->nb * 0.25 }}</b></td>
<td>{{ round((($data->nb * 0.25) / $workdays) * 100) }}%</td>
</tr>
@php $sum += $data->nb; @endphp
@endforeach
<tr>
<td><b>{{ __('Total') }}</b></td>
<td><b>{{ $sum * 0.25 }} / {{ $workdays }}</b></td>
<td><b>{{ round((($sum * 0.25) / $workdays) * 100) }}%</b></td>
</tr>
</tbody>
</table>
@endforeach
</div>
</div>
</div>
</div>
</div>
@endsection

View file

@ -43,6 +43,7 @@ Route::middleware(['auth'])->group(function () {
Route::view('/users/change-password', 'users.passwordReset')->name('users.passwordReset');
Route::post('/users/change-password', [UserController::class, 'passwordResetSubmit'])
->name('users.passwordResetSubmit');
Route::get('/users/stats', [UserController::class, 'stats'])->name('users.stats');
// Ticket
Route::resource('ticket', TicketController::class)->except(['destroy']);