80 lines
2.7 KiB
PHP
80 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\ApiResource\EmployeeRttPaymentInput;
|
|
use App\Entity\Employee;
|
|
use App\Entity\EmployeeRttPayment;
|
|
use App\Repository\EmployeeRepository;
|
|
use App\Repository\EmployeeRttPaymentRepository;
|
|
use DateTimeImmutable;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
final readonly class EmployeeRttPaymentProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private EmployeeRepository $employeeRepository,
|
|
private EmployeeRttPaymentRepository $rttPaymentRepository,
|
|
private EntityManagerInterface $entityManager,
|
|
) {}
|
|
|
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): EmployeeRttPaymentInput
|
|
{
|
|
if (!$data instanceof EmployeeRttPaymentInput) {
|
|
throw new UnprocessableEntityHttpException('Invalid payload.');
|
|
}
|
|
|
|
$employeeId = (int) ($uriVariables['id'] ?? 0);
|
|
if ($employeeId <= 0) {
|
|
throw new UnprocessableEntityHttpException('id must be a positive integer.');
|
|
}
|
|
|
|
$employee = $this->employeeRepository->find($employeeId);
|
|
if (!$employee instanceof Employee) {
|
|
throw new NotFoundHttpException('Employee not found.');
|
|
}
|
|
|
|
if ($data->month < 1 || $data->month > 12) {
|
|
throw new UnprocessableEntityHttpException('month must be between 1 and 12.');
|
|
}
|
|
|
|
$year = $data->year ?? $this->resolveCurrentExerciseYear();
|
|
|
|
$payment = $this->rttPaymentRepository->findOneByEmployeeYearMonth($employee, $year, $data->month);
|
|
|
|
if (null === $payment) {
|
|
$payment = new EmployeeRttPayment();
|
|
$payment->setEmployee($employee);
|
|
$payment->setYear($year);
|
|
$payment->setMonth($data->month);
|
|
$this->entityManager->persist($payment);
|
|
}
|
|
|
|
$payment->setBase25Minutes($data->base25Minutes);
|
|
$payment->setBonus25Minutes($data->bonus25Minutes);
|
|
$payment->setBase50Minutes($data->base50Minutes);
|
|
$payment->setBonus50Minutes($data->bonus50Minutes);
|
|
$payment->touch();
|
|
$this->entityManager->flush();
|
|
|
|
$data->year = $year;
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function resolveCurrentExerciseYear(): int
|
|
{
|
|
$today = new DateTimeImmutable('today');
|
|
$year = (int) $today->format('Y');
|
|
$month = (int) $today->format('n');
|
|
|
|
return $month >= 6 ? $year + 1 : $year;
|
|
}
|
|
}
|