Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#444 - overtime report #449

Merged
merged 15 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/Domain/OvertimeTimesheetExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Toby\Domain;

use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Toby\Models\User;

class OvertimeTimesheetExport implements WithMultipleSheets
{
protected Collection $users;
protected Carbon $month;

public function sheets(): array
{
return $this->users
->map(fn(User $user): OvertimeTimesheetPerUserSheet => new OvertimeTimesheetPerUserSheet($user, $this->month))
->toArray();
}

public function forUsers(Collection $users): static
{
$this->users = $users;

return $this;
}

public function forMonth(Carbon $month): static
{
$this->month = $month;

return $this;
}
}
189 changes: 189 additions & 0 deletions app/Domain/OvertimeTimesheetPerUserSheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

declare(strict_types=1);

namespace Toby\Domain;

use Carbon\CarbonPeriod;
use Generator;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromGenerator;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Toby\Helpers\DateFormats;
use Toby\Models\OvertimeRequest;
use Toby\Models\User;
use Toby\States\OvertimeRequest\Approved;

class OvertimeTimesheetPerUserSheet implements WithTitle, WithHeadings, WithEvents, WithStyles, WithStrictNullComparison, ShouldAutoSize, FromGenerator
{
use RegistersEventListeners;

public function __construct(
protected User $user,
protected Carbon $month,
) {}

public function title(): string
{
return $this->user->profile->full_name;
}

public function headings(): array
{
return [
__("Date"),
__("Day of week"),
__("Start date"),
__("End date"),
__("Overtime hours"),
__("Overtime settlement"),
];
}

public function generator(): Generator
{
$period = CarbonPeriod::create($this->month->copy()->startOfMonth(), $this->month->copy()->endOfMonth());
$overtimeRequests = $this->getOvertimeForPeriod($this->user, $period);

foreach ($period as $day) {
$overtimeRequestsForDay = $overtimeRequests->get($day->toDateString(), new Collection());

if (!$overtimeRequestsForDay->isEmpty()) {
/** @var OvertimeRequest $overtimeForDay */
foreach ($overtimeRequestsForDay as $overtimeForDay) {
$row = [
Date::dateTimeToExcel($day),
$day->translatedFormat("l"),
$overtimeForDay->from->format(DateFormats::DATETIME),
$overtimeForDay->to->format(DateFormats::DATETIME),
$overtimeForDay->hours,
__($overtimeForDay->settlement_type->value),
];

yield $row;
}
} else {
$row = [
Date::dateTimeToExcel($day),
$day->translatedFormat("l"),
];

yield $row;
}
}
}

public function styles(Worksheet $sheet): void
{
$lastRow = $sheet->getHighestRow();
$lastColumn = $sheet->getHighestColumn();

$sheet->getStyle("A1:{$lastColumn}1")
->getFont()->setBold(true);

$sheet->getStyle("A1:{$lastColumn}1")
->getAlignment()
->setVertical(Alignment::VERTICAL_CENTER);

$sheet->getStyle("A1:{$lastColumn}1")
->getFill()
->setFillType(Fill::FILL_SOLID)
->getStartColor()
->setRGB("D9D9D9");

$sheet->getStyle("C1:{$lastColumn}{$lastRow}")
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER);

$sheet->getStyle("A2:A{$lastRow}")
->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_DATE_DDMMYYYY);

$sheet->getStyle("C1:D{$lastRow}")
->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_DATE_TIME3);

$sheet->getStyle("A2:A{$lastRow}")
->getFont()
->setBold(true);

for ($i = 2; $i < $lastRow; $i++) {
$date = Date::excelToDateTimeObject($sheet->getCell("A{$i}")->getValue());

if (Carbon::createFromInterface($date)->isWeekend()) {
$sheet->getStyle("A{$i}:{$lastColumn}{$i}")
->getFill()
->setFillType(Fill::FILL_SOLID)
->getStartColor()
->setRGB("FEE2E2");
}
}

$sheet->getStyle("A1:{$lastColumn}{$lastRow}")
->getBorders()
->getAllBorders()
->setBorderStyle(Border::BORDER_THIN)
->getColor()
->setRGB("B7B7B7");
}

public static function afterSheet(AfterSheet $event): void
{
$sheet = $event->getSheet();
$lastRow = $sheet->getDelegate()->getHighestRow();

$sheet->append([
__("Sum:"),
null,
null,
null,
"=SUM(E2:E{$lastRow})",
]);

$lastRow++;

$sheet->getDelegate()->getStyle("A{$lastRow}")
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_RIGHT);

$sheet->getDelegate()->getStyle("A{$lastRow}")
->getFont()
->setBold(true);

$sheet->getDelegate()->getStyle("E{$lastRow}")
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER);

$sheet->getDelegate()->mergeCells("A{$lastRow}:D{$lastRow}");

$sheet->getDelegate()->getStyle("A{$lastRow}:E{$lastRow}")
->getBorders()
->getAllBorders()
->setBorderStyle(Border::BORDER_THIN)
->getColor()
->setRGB("B7B7B7");
}

protected function getOvertimeForPeriod(User $user, CarbonPeriod $period): Collection
{
return $user->overtimeRequests()
->whereBetween("from", [$period->start, $period->end])
->states([Approved::$name])
->get()
->groupBy(fn(OvertimeRequest $overtime): string => $overtime->from->toDateString());
}
}
41 changes: 41 additions & 0 deletions app/Http/Controllers/OvertimeTimesheetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Toby\Http\Controllers;

use Illuminate\Support\Carbon;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Toby\Domain\OvertimeTimesheetExport;
use Toby\Enums\EmploymentForm;
use Toby\Enums\Month;
use Toby\Helpers\YearPeriodRetriever;
use Toby\Models\User;

class OvertimeTimesheetController extends Controller
{
public function __invoke(
Month $month,
YearPeriodRetriever $yearPeriodRetriever,
): BinaryFileResponse {
$this->authorize("manageRequestsAsAdministrativeApprover");

$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();

$filename = "overtime-{$carbonMonth->translatedFormat("F Y")}.xlsx";

$timesheet = (new OvertimeTimesheetExport())
->forMonth($carbonMonth)
->forUsers($users);

return Excel::download($timesheet, $filename);
}
}
4 changes: 4 additions & 0 deletions lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
"Day of week": "Dzień tygodnia",
"Start date": "Data rozpoczęcia",
"End date": "Data zakończenia",
"Overtime hours": "Liczba nadgodzin",
"Overtime settlement": "Sposób rozliczenia",
"hours": "godzinowe",
"money": "pieniężne",
"Worked hours": "Liczba godzin",
"Hi :user!": "Cześć :user!",
"The request :title changed state to :state.": "Wniosek :title zmienił status na :state.",
Expand Down
12 changes: 11 additions & 1 deletion resources/js/Pages/Calendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,23 @@ function linkVacationRequest(user){
</span>
</div>
</div>
<div v-if="auth.can.manageRequestsAsAdministrativeApprover">
<div
class="flex"
>
<a
v-if="auth.can.manageRequestsAsAdministrativeApprover"
:href="`/vacation/timesheet/${selectedMonth.value}`"
class="block py-3 px-4 ml-3 text-sm font-medium leading-4 text-center text-white bg-blumilk-600 hover:bg-blumilk-700 rounded-md border border-transparent focus:outline-none focus:ring-2 focus:ring-blumilk-500 focus:ring-offset-2 shadow-sm"
>
Pobierz plik Excel
</a>
<a
v-if="auth.can.manageOvertimeAsAdministrativeApprover"
:href="`/overtime/timesheet/${selectedMonth.value}`"
class="block py-3 px-4 ml-3 text-sm font-medium leading-4 text-center text-white bg-blumilk-600 hover:bg-blumilk-700 rounded-md border border-transparent focus:outline-none focus:ring-2 focus:ring-blumilk-500 focus:ring-offset-2 shadow-sm"
>
Pobierz nadgodziny
</a>
</div>
</div>
<div class="overflow-x-auto">
Expand Down
Loading
Loading