-
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from mihaisolomon/feature/timesheet-dashboard
Timesheet dashboard
- Loading branch information
Showing
3 changed files
with
165 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters