Skip to content

Commit

Permalink
Merge pull request #44 from mihaisolomon/feature/timesheet-dashboard
Browse files Browse the repository at this point in the history
Timesheet dashboard
  • Loading branch information
heloufir authored Jan 16, 2023
2 parents 23a0714 + 647390f commit e556943
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 35 deletions.
4 changes: 3 additions & 1 deletion app/Filament/Pages/TimesheetDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Pages;

use App\Filament\Widgets\Timesheet\ActivitiesReport;
use App\Filament\Widgets\Timesheet\MonthlyReport;
use Filament\Pages\Page;

Expand Down Expand Up @@ -33,7 +34,8 @@ protected static function getNavigationGroup(): ?string
protected function getWidgets(): array
{
return [
MonthlyReport::class
MonthlyReport::class,
ActivitiesReport::class
];
}
}
91 changes: 91 additions & 0 deletions app/Filament/Widgets/Timesheet/ActivitiesReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace App\Filament\Widgets\Timesheet;

use App\Models\TicketHour;
use App\Models\User;
use Carbon\Carbon;
use Filament\Widgets\BarChartWidget;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

class ActivitiesReport extends BarChartWidget
{
protected int|string|array $columnSpan = [
'sm' => 1,
'md' => 6,
'lg' => 3
];

public ?string $filter = '2023';

protected function getHeading(): string
{
return __('Logged time by activity');
}

protected function getFilters(): ?array
{
return [
2022 => 2022,
2023 => 2023
];
}

protected function getData(): array
{
$collection = $this->filter(auth()->user(), [
'year' => $this->filter
]);

$datasets = $this->getDatasets($collection);

return [
'datasets' => [
[
'label' => __('Total time logged'),
'data' => $datasets['sets'],
'backgroundColor' => [
'rgba(54, 162, 235, .6)'
],
'borderColor' => [
'rgba(54, 162, 235, .8)'
],
],
],
'labels' => $datasets['labels'],
];
}

protected function getDatasets(Collection $collection): array
{
$datasets = [
'sets' => [],
'labels' => []
];

foreach ($collection as $item) {
$datasets['sets'][] = $item->value;
$datasets['labels'][] = $item->activity->name;
}

return $datasets;
}

protected function filter(User $user, array $params): Collection
{
return TicketHour::with('activity')
->select([
'activity_id',
DB::raw('SUM(value) as value'),
])
->whereRaw(
DB::raw("YEAR(created_at)=" . (is_null($params['year']) ? Carbon::now()->format('Y') : $params['year']))
)
->where('user_id', $user->id)
->groupBy('activity_id')
->get();
}
}
105 changes: 71 additions & 34 deletions app/Filament/Widgets/Timesheet/MonthlyReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\User;
use Carbon\Carbon;
use Filament\Widgets\BarChartWidget;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

class MonthlyReport extends BarChartWidget
Expand All @@ -17,44 +18,21 @@ protected function getHeading(): string
return __('Logged time monthly');
}

public ?string $filter = '2023';

protected function getData(): array
{
$months = [
1 => ['January', 0],
2 => ['February', 0],
3 => ['March', 0],
4 => ['April', 0],
5 => ['May', 0],
6 => ['June', 0],
7 => ['July', 0],
8 => ['August', 0],
9 => ['September', 0],
10 => ['October', 0],
11 => ['November', 0],
12 => ['December', 0]
];

$collection = $this->filter(auth()->user(), [
'year' => $this->filter
]);

$data = $this->filter(auth()->user());

foreach ($data as $value) {
if (isset($months[(int)$value->month])) {
$months[(int)$value->month][1] = (float)$value->value;
}
}

$datasets = [];
$labels = [];
foreach ($months as $month) {
$datasets[] = $month[1];
$labels[] = $month[0];
}
$datasets = $this->getDatasets($this->buildRapport($collection));

return [
'datasets' => [
[
'label' => __('Total time logged'),
'data' => $datasets,
'data' => $datasets['sets'],
'backgroundColor' => [
'rgba(54, 162, 235, .6)'
],
Expand All @@ -63,25 +41,84 @@ protected function getData(): array
],
],
],
'labels' => $labels,
'labels' => $datasets['labels'],
];
}

protected function getFilters(): ?array
{
return [
2022 => 2022,
2023 => 2023
];
}

protected static ?array $options = [
'plugins' => [
'legend' => [
'display' => true,
],
],
];

protected int|string|array $columnSpan = [
'sm' => 1,
'md' => 6,
'lg' => 3
];

protected function filter(User $user)
protected function filter(User $user, array $params)
{
return TicketHour::select([
DB::raw("DATE_FORMAT (created_at, '%m') as month"),
DB::raw("DATE_FORMAT(created_at,'%m') as month"),
DB::raw('SUM(value) as value'),
])
->whereRaw(
DB::raw("YEAR(created_at)=" . (is_null($params['year']) ? Carbon::now()->format('Y') : $params['year']))
)
->where('user_id', $user->id)
->whereRaw(DB::raw("YEAR(created_at)=") . Carbon::now()->format('Y'))
->groupBy(DB::raw("DATE_FORMAT (created_at, '%m')"))
->get();
}

protected function getDatasets(array $rapportData): array
{
$datasets = [
'sets' => [],
'labels' => []
];

foreach ($rapportData as $data) {
$datasets['sets'][] = $data[1];
$datasets['labels'][] = $data[0];
}

return $datasets;
}

protected function buildRapport(Collection $collection): array
{
$months = [
1 => ['January', 0],
2 => ['February', 0],
3 => ['March', 0],
4 => ['April', 0],
5 => ['May', 0],
6 => ['June', 0],
7 => ['July', 0],
8 => ['August', 0],
9 => ['September', 0],
10 => ['October', 0],
11 => ['November', 0],
12 => ['December', 0]
];

foreach ($collection as $value) {
if (isset($months[(int)$value->month])) {
$months[(int)$value->month][1] = (float)$value->value;
}
}

return $months;
}
}

0 comments on commit e556943

Please sign in to comment.