Skip to content

Commit e4e88b8

Browse files
committed
Timesheet export period filtering
1 parent 2509997 commit e4e88b8

4 files changed

Lines changed: 138 additions & 2 deletions

File tree

app/Exports/ProjectHoursExport.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\Project;
66
use App\Models\Ticket;
77
use App\Models\TicketHour;
8+
use Illuminate\Support\Collection;
89
use Maatwebsite\Excel\Concerns\FromCollection;
910
use Maatwebsite\Excel\Concerns\WithHeadings;
1011

@@ -31,9 +32,9 @@ public function headings(): array
3132
}
3233

3334
/**
34-
* @return \Illuminate\Support\Collection
35+
* @return Collection
3536
*/
36-
public function collection()
37+
public function collection(): Collection
3738
{
3839
$collection = collect();
3940
$this->project->tickets

app/Exports/TimesheetExport.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace App\Exports;
4+
5+
use App\Models\Project;
6+
use App\Models\Ticket;
7+
use App\Models\TicketHour;
8+
use Illuminate\Support\Collection;
9+
use Maatwebsite\Excel\Concerns\FromCollection;
10+
use Maatwebsite\Excel\Concerns\WithHeadings;
11+
12+
class TimesheetExport implements FromCollection, WithHeadings
13+
{
14+
protected array $params;
15+
16+
public function __construct(array $params)
17+
{
18+
$this->params = $params;
19+
}
20+
21+
public function headings(): array
22+
{
23+
return [
24+
'#',
25+
'Ticket',
26+
'User',
27+
'Time',
28+
'Hours',
29+
'Activity',
30+
'Date',
31+
];
32+
}
33+
34+
/**
35+
* @return Collection
36+
*/
37+
public function collection(): Collection
38+
{
39+
$collection = collect();
40+
41+
$hours = TicketHour::where('user_id', auth()->user()->id)
42+
->whereBetween('created_at', [$this->params['start_date'], $this->params['end_date']])
43+
->get();
44+
45+
foreach ($hours as $item) {
46+
$collection->push([
47+
'#' => $item->ticket->code,
48+
'ticket' => $item->ticket->name,
49+
'user' => $item->user->name,
50+
'time' => $item->forHumans,
51+
'hours' => number_format($item->value, 2, ',', ' '),
52+
'activity' => $item->activity ? $item->activity->name : '-',
53+
'date' => $item->created_at->format(__('Y-m-d g:i A')),
54+
]);
55+
}
56+
57+
return $collection;
58+
}
59+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Filament\Pages;
6+
7+
use Filament\Forms\Components\Card;
8+
use Filament\Forms\Components\Grid;
9+
use Filament\Forms\Concerns\InteractsWithForms;
10+
use Filament\Forms\Contracts\HasForms;
11+
use Filament\Pages\Page;
12+
use Filament\Forms\Components\DatePicker;
13+
use Maatwebsite\Excel\Facades\Excel;
14+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
15+
16+
class TimesheetExport extends Page implements HasForms
17+
{
18+
use InteractsWithForms;
19+
protected static ?string $slug = 'timesheet-export';
20+
21+
protected static ?int $navigationSort = 2;
22+
23+
protected static string $view = 'filament.pages.timesheet-export';
24+
25+
protected static function getNavigationGroup(): ?string
26+
{
27+
return __('Timesheet');
28+
}
29+
30+
public function mount(): void
31+
{
32+
$this->form->fill();
33+
}
34+
35+
protected function getFormSchema(): array
36+
{
37+
return [
38+
Card::make()->schema([
39+
Grid::make()
40+
->columns(2)
41+
->schema([
42+
DatePicker::make('start_date')
43+
->required()
44+
->reactive()
45+
->label('Star date'),
46+
DatePicker::make('end_date')
47+
->required()
48+
->reactive()
49+
->label('End date')
50+
])
51+
])
52+
];
53+
}
54+
55+
public function create(): BinaryFileResponse
56+
{
57+
$data = $this->form->getState();
58+
59+
return Excel::download(
60+
new \App\Exports\TimesheetExport($data),
61+
'time_' . time() . '.csv',
62+
\Maatwebsite\Excel\Excel::CSV,
63+
['Content-Type' => 'text/csv']
64+
);
65+
}
66+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<x-filament::page>
2+
<div class="w-full flex flex-col gap-10 justify-center items-center">
3+
<form wire:submit.prevent="create" class="lg:w-[50%] w-full">
4+
{{ $this->form }}
5+
<x-filament::button type="submit" form="create" >
6+
{{ __('Create report') }}
7+
</x-filament::button>
8+
</form>
9+
</div>
10+
</x-filament::page>

0 commit comments

Comments
 (0)