toby/app/Infrastructure/Http/Controllers/TimesheetController.php
Adrian Hopek c69866bb52
#116 - integration with slack (#129)
* wip

* wip

* wip

* wip

* fix

* wip

* wip

* fix

* fix

* cs fix

* #116 - fix

* #116 - changed home-office icon

* Apply suggestions from code review

Co-authored-by: Krzysztof Rewak <krzysztof.rewak@gmail.com>

* #116 - cr fix

* #116 - cs fix

* #116 - cs fix

* Apply suggestions from code review

Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>

* #5 - bump codestyle

Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
Co-authored-by: Krzysztof Rewak <krzysztof.rewak@gmail.com>
Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>
2022-04-27 09:57:13 +02:00

54 lines
1.7 KiB
PHP

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