<?php
namespace App\Controller\Admin;
use App\Entity\Workers;
use App\Entity\WorkHours;
use App\Form\Type\WorkhourType;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TelephoneField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class WorkersCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Workers::class;
}
public function configureFields(string $pageName): iterable
{
yield IdField::new('id')->hideOnForm();
yield TextField::new('full_name');
yield MoneyField::new('price_hour', 'Órabér')->setCurrency('HUF')->setNumDecimals(0)->setStoredAsCents(false)->hideOnIndex();
yield MoneyField::new('price_month', 'Havibér')->setCurrency('HUF')->setNumDecimals(0)->setStoredAsCents(false)->hideOnIndex();
yield MoneyField::new('contribution', 'Járulék')->setCurrency('HUF')->setNumDecimals(0)->setStoredAsCents(false)->hideOnIndex();
yield BooleanField::new('active', 'Aktív');
yield CollectionField::new('workhours', 'Munkaóra')->onlyOnForms()->setEntryType(WorkhourType::class)->setCustomOption('download_uri', 'SSS')->addCssClass('small-cubes');
}
/**
* Init crud
*/
public function configureCrud(Crud $crud): Crud
{
return $crud
->setEntityLabelInSingular('Worker')
->setDefaultSort(['active' => 'DESC', 'created' => 'DESC'])
;
}
public function configureActions(Actions $actions): Actions
{
$actionBatch = Action::new('actionBatch', 'Fizetés', 'fa fa-money')->linkToCrudAction('actionBatch');
$actions->add(Crud::PAGE_INDEX, $actionBatch);
$actions->add(Crud::PAGE_INDEX, Action::DETAIL);
$actions->add(Crud::PAGE_EDIT, Action::DETAIL);
return $actions;
}
public function actionPaid(Request $request)
{
$worker = $this->getDoctrine()->getRepository(Workers::class)->findOneBy(['id' => $request->query->get('entityId')]);
if (!$this->getUser()->hasRoles('ROLE_ADMIN') && $this->getUser()->getId() != $worker->getUser()->getId()) {
throw $this->createAccessDeniedException();
}
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$workers = $this->getDoctrine()->getRepository(Workers::class)->findBy(['active' => true]);
$selectedDate = $request->query->get('date');
$workHours = $qb->select(['w'])
->from('App:WorkHours', 'w')
//->leftJoin('App:Projects', 'p', 'WITH', 'w.project = p.id')
->where('w.worker = :id')
->andWhere('w.payed is not null')
->setParameter('id', $request->query->get('entityId'))
->andWhere('w.day >= :monthStart')
->andWhere('w.day < :monthEnd')
->setParameter('monthStart', new \DateTimeImmutable(($selectedDate ?: '1970-01') . '-01 00:00:00'))
->setParameter('monthEnd', (new \DateTimeImmutable(($selectedDate ?: '1970-01') . '-01 00:00:00'))->modify('+1 month'))
->getQuery()
//->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
->getResult();
$workerDates = [];
$qb = $em->createQueryBuilder();
$workHoursAll = $qb->select(['ww'])
->from('App:WorkHours', 'ww')
->where('ww.worker = :id')
->andWhere('ww.payed is not null')
->setParameter('id', $request->query->get('entityId'))
->getQuery()
->getResult();
foreach ($workHoursAll as $workHour) {
$day = $workHour->getDay();
if (!$day) {
continue;
}
$yearMonth = $day->format('Y-m'); // example: 2026-09
$workerDates[$yearMonth] = $yearMonth;
}
krsort($workerDates);
$workerDates = array_values($workerDates);
$selectedDate = $selectedDate && in_array($selectedDate, $workerDates, true)
? $selectedDate
: ($workerDates[0] ?? null);
if ($selectedDate) {
$monthStart = new \DateTimeImmutable($selectedDate . '-01 00:00:00');
$monthEnd = (new \DateTimeImmutable($selectedDate . '-01 00:00:00'))->modify('+1 month');
$workHours = array_values(array_filter($workHours, static function ($workHour) use ($monthStart, $monthEnd) {
$day = $workHour->getDay();
return $day instanceof \DateTimeInterface
&& $day >= $monthStart
&& $day < $monthEnd;
}));
}
if (!$this->getUser()->hasRoles('ROLE_ADMIN')) {
return $this->render('workhour/paidView.html.twig', [
'worker' => $worker,
'hours' => $workHours,
'workerDates' => $workerDates,
'selectedDate' => $selectedDate,
'isAdmin' => $this->getUser()->hasRoles('ROLE_ADMIN')
]);
} else {
return $this->render('workhour/paidView.html.twig', [
'worker' => $worker,
'workers' => $workers,
'hours' => $workHours,
'workerDates' => $workerDates,
'selectedDate' => $selectedDate,
'isAdmin' => $this->getUser()->hasRoles('ROLE_ADMIN')
]);
}
}
public function actionBatch(Request $request)
{
$worker = $this->getDoctrine()->getRepository(Workers::class)->findOneBy(['id' => $request->query->get('entityId')]);
if (!$this->getUser()->hasRoles('ROLE_ADMIN') && $this->getUser()->getId() != $worker->getUser()->getId()) {
throw $this->createAccessDeniedException();
}
$em = $this->getDoctrine()->getManager();
if (isset($_POST['submit'])) {
foreach ($_POST['workhours'] as $key => $value) {
$hour = $this->getDoctrine()->getRepository(WorkHours::class)->findOneBy(['id' => $value]);
$hour->setPayed(new \DateTime());
$hour->setPayedHourPrice(intval($_POST['priceHour']));
$em->persist($hour);
$em->flush();
}
}
$qb = $em->createQueryBuilder();
$workers = $this->getDoctrine()->getRepository(Workers::class)->findBy(['active' => true]);
$workHours = $qb->select(['w'])
->from('App:WorkHours', 'w')
//->leftJoin('App:Projects', 'p', 'WITH', 'w.project = p.id')
->where('w.worker = :id')
->andWhere('w.payed is null')
->setParameter('id', $request->query->get('entityId'))
->getQuery()
//->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
->getResult();
if (!$this->getUser()->hasRoles('ROLE_ADMIN')) {
return $this->render('workhour/payView.html.twig', [
'worker' => $worker,
'hours' => $workHours
]);
} else {
return $this->render('workhour/pay.html.twig', [
'worker' => $worker,
'workers' => $workers,
'hours' => $workHours
]);
}
}
}