-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
244 additions
and
103 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
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
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,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Filament\Widgets\Timesheet; | ||
|
||
use App\Models\TicketHour; | ||
use App\Models\User; | ||
use Carbon\Carbon; | ||
use Carbon\CarbonPeriod; | ||
use Filament\Widgets\BarChartWidget; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class WeeklyReport extends BarChartWidget | ||
{ | ||
protected int|string|array $columnSpan = [ | ||
'sm' => 1, | ||
'md' => 6, | ||
'lg' => 3 | ||
]; | ||
|
||
protected function getData(): array | ||
{ | ||
$now = Carbon::now(); | ||
|
||
$weekStartDate = $now->startOfWeek()->format('Y-m-d'); | ||
$weekEndDate = $now->endOfWeek()->format('Y-m-d'); | ||
|
||
$collection = $this->filter(auth()->user(), [ | ||
'year' => null, | ||
'weekStartDate' => $weekStartDate, | ||
'weekEndDate' => $weekEndDate | ||
]); | ||
|
||
$dates = $this->buildDatesRange($weekStartDate, $weekEndDate); | ||
|
||
$datasets = $this->buildRapport($collection, $dates); | ||
|
||
return [ | ||
'datasets' => [ | ||
[ | ||
'label' => __('Weekly time logged'), | ||
'data' => $datasets, | ||
'backgroundColor' => [ | ||
'rgba(54, 162, 235, .6)' | ||
], | ||
'borderColor' => [ | ||
'rgba(54, 162, 235, .8)' | ||
], | ||
], | ||
], | ||
'labels' => $dates, | ||
]; | ||
} | ||
|
||
protected function buildRapport(Collection $collection, array $dates): array | ||
{ | ||
$template = $this->createReportTemplate($dates); | ||
foreach ($collection as $item) { | ||
$template[$item->day]['value'] = $item->value; | ||
} | ||
return collect($template)->pluck('value')->toArray(); | ||
} | ||
|
||
protected function filter(User $user, array $params) | ||
{ | ||
return TicketHour::select([ | ||
DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d') as day"), | ||
DB::raw('SUM(value) as value'), | ||
]) | ||
->whereBetween('created_at', [$params['weekStartDate'], $params['weekEndDate']]) | ||
->whereRaw( | ||
DB::raw("YEAR(created_at)=" . (is_null($params['year']) ? Carbon::now()->format('Y') : $params['year'])) | ||
) | ||
->where('user_id', $user->id) | ||
->groupBy(DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d')")) | ||
->get(); | ||
} | ||
|
||
protected function buildDatesRange($weekStartDate, $weekEndDate): array | ||
{ | ||
$period = CarbonPeriod::create($weekStartDate, $weekEndDate); | ||
|
||
$dates = []; | ||
foreach ($period as $item) { | ||
$dates[] = $item->format('Y-m-d'); | ||
} | ||
|
||
return $dates; | ||
} | ||
|
||
protected function createReportTemplate(array $dates): array | ||
{ | ||
$template = []; | ||
foreach ($dates as $date) { | ||
$template[$date]['value'] = 0; | ||
} | ||
return $template; | ||
} | ||
} |
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Livewire\Timesheet; | ||
|
||
use App\Models\Ticket; | ||
use Filament\Tables; | ||
use Filament\Tables\Concerns\InteractsWithTable; | ||
use Filament\Tables\Contracts\HasTable; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Livewire\Component; | ||
|
||
class TimeLogged extends Component implements HasTable | ||
{ | ||
use InteractsWithTable; | ||
|
||
public Ticket $ticket; | ||
|
||
protected function getFormModel(): Model|string|null | ||
{ | ||
return $this->ticket; | ||
} | ||
|
||
protected function getTableQuery(): Builder | ||
{ | ||
return $this->ticket->hours()->getQuery(); | ||
} | ||
|
||
protected function getTableColumns(): array | ||
{ | ||
return [ | ||
Tables\Columns\TextColumn::make('user.name') | ||
->label(__('Owner')) | ||
->sortable() | ||
->formatStateUsing(fn($record) => view('components.user-avatar', ['user' => $record->user])) | ||
->searchable(), | ||
|
||
Tables\Columns\TextColumn::make('value') | ||
->label(__('Hours')) | ||
->sortable() | ||
->searchable(), | ||
Tables\Columns\TextColumn::make('comment') | ||
->label(__('Comment')) | ||
->limit(50) | ||
->sortable() | ||
->searchable(), | ||
|
||
Tables\Columns\TextColumn::make('activity.name') | ||
->label(__('Activity')) | ||
->sortable(), | ||
|
||
Tables\Columns\TextColumn::make('ticket.name') | ||
->label(__('Ticket')) | ||
->sortable(), | ||
|
||
Tables\Columns\TextColumn::make('created_at') | ||
->label(__('Created at')) | ||
->dateTime() | ||
->sortable() | ||
->searchable(), | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.