This commit is contained in:
Adrian Hopek 2022-04-06 10:33:48 +02:00
parent 9712d3fc0c
commit 6dceabb857
4 changed files with 35 additions and 8 deletions

View File

@ -4,20 +4,21 @@ declare(strict_types=1);
namespace Toby\Domain; namespace Toby\Domain;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\WithMultipleSheets; use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\User;
class TimesheetExport implements WithMultipleSheets class TimesheetExport implements WithMultipleSheets
{ {
protected Collection $users; protected Collection $users;
protected Collection $types;
protected Carbon $month; protected Carbon $month;
public function sheets(): array public function sheets(): array
{ {
return $this->users return $this->users
->map(fn(User $user) => new TimesheetPerUserSheet($user, $this->month)) ->map(fn(User $user) => new TimesheetPerUserSheet($user, $this->month, $this->types))
->toArray(); ->toArray();
} }
@ -34,4 +35,11 @@ class TimesheetExport implements WithMultipleSheets
return $this; return $this;
} }
public function forVacationTypes(Collection $types): static
{
$this->types = $types;
return $this;
}
} }

View File

@ -7,6 +7,7 @@ namespace Toby\Domain;
use Carbon\CarbonInterface; use Carbon\CarbonInterface;
use Carbon\CarbonPeriod; use Carbon\CarbonPeriod;
use Generator; use Generator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromGenerator; use Maatwebsite\Excel\Concerns\FromGenerator;
@ -40,6 +41,7 @@ class TimesheetPerUserSheet implements WithTitle, WithHeadings, WithEvents, With
public function __construct( public function __construct(
protected User $user, protected User $user,
protected Carbon $month, protected Carbon $month,
protected Collection $types,
) {} ) {}
public function title(): string public function title(): string
@ -49,8 +51,6 @@ class TimesheetPerUserSheet implements WithTitle, WithHeadings, WithEvents, With
public function headings(): array public function headings(): array
{ {
$types = VacationType::cases();
$headings = [ $headings = [
__("Date"), __("Date"),
__("Day of week"), __("Day of week"),
@ -59,7 +59,7 @@ class TimesheetPerUserSheet implements WithTitle, WithHeadings, WithEvents, With
__("Worked hours"), __("Worked hours"),
]; ];
foreach ($types as $type) { foreach ($this->types as $type) {
$headings[] = $type->label(); $headings[] = $type->label();
} }
@ -187,6 +187,7 @@ class TimesheetPerUserSheet implements WithTitle, WithHeadings, WithEvents, With
{ {
return $user->vacations() return $user->vacations()
->with("vacationRequest") ->with("vacationRequest")
->whereRelation("vacationRequest", fn(Builder $query) => $query->whereIn("type", $this->types))
->whereBetween("date", [$period->start, $period->end]) ->whereBetween("date", [$period->start, $period->end])
->approved() ->approved()
->get() ->get()

View File

@ -7,30 +7,43 @@ namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Toby\Domain\Enums\EmploymentForm;
use Toby\Domain\Enums\Month; use Toby\Domain\Enums\Month;
use Toby\Domain\Enums\VacationType;
use Toby\Domain\TimesheetExport; use Toby\Domain\TimesheetExport;
use Toby\Domain\VacationTypeConfigRetriever;
use Toby\Eloquent\Helpers\YearPeriodRetriever; use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\User;
class TimesheetController extends Controller class TimesheetController extends Controller
{ {
public function __invoke(Month $month, YearPeriodRetriever $yearPeriodRetriever): BinaryFileResponse public function __invoke(
{ Month $month,
YearPeriodRetriever $yearPeriodRetriever,
VacationTypeConfigRetriever $configRetriever,
): BinaryFileResponse {
$this->authorize("generateTimesheet"); $this->authorize("generateTimesheet");
$yearPeriod = $yearPeriodRetriever->selected(); $yearPeriod = $yearPeriodRetriever->selected();
$carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber()); $carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber());
$users = User::query() $users = User::query()
->where("employment_form", EmploymentForm::EmploymentContract)
->orderBy("last_name") ->orderBy("last_name")
->orderBy("first_name") ->orderBy("first_name")
->get(); ->get();
$types = VacationType::all()
->filter(
fn(VacationType $type) => $configRetriever->isAvailableFor($type, EmploymentForm::EmploymentContract),
);
$filename = "{$carbonMonth->translatedFormat("F Y")}.xlsx"; $filename = "{$carbonMonth->translatedFormat("F Y")}.xlsx";
$timesheet = (new TimesheetExport()) $timesheet = (new TimesheetExport())
->forMonth($carbonMonth) ->forMonth($carbonMonth)
->forUsers($users); ->forUsers($users)
->forVacationTypes($types);
return Excel::download($timesheet, $filename); return Excel::download($timesheet, $filename);
} }

View File

@ -6,6 +6,7 @@ namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\FeatureTestCase; use Tests\FeatureTestCase;
use Toby\Domain\Enums\EmploymentForm;
use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\User;
class VacationCalendarTest extends FeatureTestCase class VacationCalendarTest extends FeatureTestCase
@ -16,6 +17,10 @@ class VacationCalendarTest extends FeatureTestCase
{ {
$administrativeApprover = User::factory()->administrativeApprover()->create(); $administrativeApprover = User::factory()->administrativeApprover()->create();
User::factory(["employment_form" => EmploymentForm::EmploymentContract])
->count(10)
->create();
$this->actingAs($administrativeApprover) $this->actingAs($administrativeApprover)
->get("/vacation/timesheet/january") ->get("/vacation/timesheet/january")
->assertOk(); ->assertOk();