src/Controller/Admin/WorkersCrudController.php line 141

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Workers;
  4. use App\Entity\WorkHours;
  5. use App\Form\Type\WorkhourType;
  6. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
  7. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  8. use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
  9. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  10. use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
  11. use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
  12. use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
  14. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  16. use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
  17. use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
  18. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  19. use EasyCorp\Bundle\EasyAdminBundle\Field\TelephoneField;
  20. use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
  21. use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
  22. use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
  23. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  24. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  25. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\Response;
  28. class WorkersCrudController extends AbstractCrudController
  29. {
  30. public static function getEntityFqcn(): string
  31. {
  32. return Workers::class;
  33. }
  34. public function configureFields(string $pageName): iterable
  35. {
  36. yield IdField::new('id')->hideOnForm();
  37. yield TextField::new('full_name');
  38. yield MoneyField::new('price_hour', 'Órabér')->setCurrency('HUF')->setNumDecimals(0)->setStoredAsCents(false)->hideOnIndex();
  39. yield MoneyField::new('price_month', 'Havibér')->setCurrency('HUF')->setNumDecimals(0)->setStoredAsCents(false)->hideOnIndex();
  40. yield MoneyField::new('contribution', 'Járulék')->setCurrency('HUF')->setNumDecimals(0)->setStoredAsCents(false)->hideOnIndex();
  41. yield BooleanField::new('active', 'Aktív');
  42. yield CollectionField::new('workhours', 'Munkaóra')->onlyOnForms()->setEntryType(WorkhourType::class)->setCustomOption('download_uri', 'SSS')->addCssClass('small-cubes');
  43. }
  44. /**
  45. * Init crud
  46. */
  47. public function configureCrud(Crud $crud): Crud
  48. {
  49. return $crud
  50. ->setEntityLabelInSingular('Worker')
  51. ->setDefaultSort(['active' => 'DESC', 'created' => 'DESC'])
  52. ;
  53. }
  54. public function configureActions(Actions $actions): Actions
  55. {
  56. $actionBatch = Action::new('actionBatch', 'Fizetés', 'fa fa-money')->linkToCrudAction('actionBatch');
  57. $actions->add(Crud::PAGE_INDEX, $actionBatch);
  58. $actions->add(Crud::PAGE_INDEX, Action::DETAIL);
  59. $actions->add(Crud::PAGE_EDIT, Action::DETAIL);
  60. return $actions;
  61. }
  62. public function actionPaid(Request $request)
  63. {
  64. $worker = $this->getDoctrine()->getRepository(Workers::class)->findOneBy(['id' => $request->query->get('entityId')]);
  65. if (!$this->getUser()->hasRoles('ROLE_ADMIN') && $this->getUser()->getId() != $worker->getUser()->getId()) {
  66. throw $this->createAccessDeniedException();
  67. }
  68. $em = $this->getDoctrine()->getManager();
  69. $qb = $em->createQueryBuilder();
  70. $workers = $this->getDoctrine()->getRepository(Workers::class)->findBy(['active' => true]);
  71. $selectedDate = $request->query->get('date');
  72. $workHours = $qb->select(['w'])
  73. ->from('App:WorkHours', 'w')
  74. //->leftJoin('App:Projects', 'p', 'WITH', 'w.project = p.id')
  75. ->where('w.worker = :id')
  76. ->andWhere('w.payed is not null')
  77. ->setParameter('id', $request->query->get('entityId'))
  78. ->andWhere('w.day >= :monthStart')
  79. ->andWhere('w.day < :monthEnd')
  80. ->setParameter('monthStart', new \DateTimeImmutable(($selectedDate ?: '1970-01') . '-01 00:00:00'))
  81. ->setParameter('monthEnd', (new \DateTimeImmutable(($selectedDate ?: '1970-01') . '-01 00:00:00'))->modify('+1 month'))
  82. ->getQuery()
  83. //->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
  84. ->getResult();
  85. $workerDates = [];
  86. $qb = $em->createQueryBuilder();
  87. $workHoursAll = $qb->select(['ww'])
  88. ->from('App:WorkHours', 'ww')
  89. ->where('ww.worker = :id')
  90. ->andWhere('ww.payed is not null')
  91. ->setParameter('id', $request->query->get('entityId'))
  92. ->getQuery()
  93. ->getResult();
  94. foreach ($workHoursAll as $workHour) {
  95. $day = $workHour->getDay();
  96. if (!$day) {
  97. continue;
  98. }
  99. $yearMonth = $day->format('Y-m'); // example: 2026-09
  100. $workerDates[$yearMonth] = $yearMonth;
  101. }
  102. krsort($workerDates);
  103. $workerDates = array_values($workerDates);
  104. $selectedDate = $selectedDate && in_array($selectedDate, $workerDates, true)
  105. ? $selectedDate
  106. : ($workerDates[0] ?? null);
  107. if ($selectedDate) {
  108. $monthStart = new \DateTimeImmutable($selectedDate . '-01 00:00:00');
  109. $monthEnd = (new \DateTimeImmutable($selectedDate . '-01 00:00:00'))->modify('+1 month');
  110. $workHours = array_values(array_filter($workHours, static function ($workHour) use ($monthStart, $monthEnd) {
  111. $day = $workHour->getDay();
  112. return $day instanceof \DateTimeInterface
  113. && $day >= $monthStart
  114. && $day < $monthEnd;
  115. }));
  116. }
  117. if (!$this->getUser()->hasRoles('ROLE_ADMIN')) {
  118. return $this->render('workhour/paidView.html.twig', [
  119. 'worker' => $worker,
  120. 'hours' => $workHours,
  121. 'workerDates' => $workerDates,
  122. 'selectedDate' => $selectedDate,
  123. 'isAdmin' => $this->getUser()->hasRoles('ROLE_ADMIN')
  124. ]);
  125. } else {
  126. return $this->render('workhour/paidView.html.twig', [
  127. 'worker' => $worker,
  128. 'workers' => $workers,
  129. 'hours' => $workHours,
  130. 'workerDates' => $workerDates,
  131. 'selectedDate' => $selectedDate,
  132. 'isAdmin' => $this->getUser()->hasRoles('ROLE_ADMIN')
  133. ]);
  134. }
  135. }
  136. public function actionBatch(Request $request)
  137. {
  138. $worker = $this->getDoctrine()->getRepository(Workers::class)->findOneBy(['id' => $request->query->get('entityId')]);
  139. if (!$this->getUser()->hasRoles('ROLE_ADMIN') && $this->getUser()->getId() != $worker->getUser()->getId()) {
  140. throw $this->createAccessDeniedException();
  141. }
  142. $em = $this->getDoctrine()->getManager();
  143. if (isset($_POST['submit'])) {
  144. foreach ($_POST['workhours'] as $key => $value) {
  145. $hour = $this->getDoctrine()->getRepository(WorkHours::class)->findOneBy(['id' => $value]);
  146. $hour->setPayed(new \DateTime());
  147. $hour->setPayedHourPrice(intval($_POST['priceHour']));
  148. $em->persist($hour);
  149. $em->flush();
  150. }
  151. }
  152. $qb = $em->createQueryBuilder();
  153. $workers = $this->getDoctrine()->getRepository(Workers::class)->findBy(['active' => true]);
  154. $workHours = $qb->select(['w'])
  155. ->from('App:WorkHours', 'w')
  156. //->leftJoin('App:Projects', 'p', 'WITH', 'w.project = p.id')
  157. ->where('w.worker = :id')
  158. ->andWhere('w.payed is null')
  159. ->setParameter('id', $request->query->get('entityId'))
  160. ->getQuery()
  161. //->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
  162. ->getResult();
  163. if (!$this->getUser()->hasRoles('ROLE_ADMIN')) {
  164. return $this->render('workhour/payView.html.twig', [
  165. 'worker' => $worker,
  166. 'hours' => $workHours
  167. ]);
  168. } else {
  169. return $this->render('workhour/pay.html.twig', [
  170. 'worker' => $worker,
  171. 'workers' => $workers,
  172. 'hours' => $workHours
  173. ]);
  174. }
  175. }
  176. }