src/Service/AppointmentService.php line 103

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Appointment;
  4. use App\Constant\Common;
  5. use App\DTO\Appointment\AppointmentOutput;
  6. use App\Repository\AppointmentRepository;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. /**
  9.  * Class AppointmentService
  10.  * @package App\Service
  11.  */
  12. class AppointmentService extends BaseService
  13. {
  14.     /**
  15.      * AppointmentService constructor.
  16.      * @param AppointmentRepository $repository
  17.      */
  18.     public function __construct(
  19.         AppointmentRepository $repository,
  20.         BaseService $baseService,
  21.         TranslatorInterface $translator
  22.     ) {
  23.         $this->reflectFromParent($baseService);
  24.         $this->repository $repository;
  25.         $this->translator =  $translator;
  26.     }
  27.     public function getListAll($requestData null)
  28.     {
  29.         return $this->repository->getListAll($requestDataAppointmentOutput::class);
  30.     }
  31.     public function getById($id)
  32.     {
  33.         return $this->autoMapper->getById($idAppointmentOutput::class);
  34.     }
  35.     public function addEntity($request)
  36.     {
  37.         $selectedTimes $request->request->get('selectedTimes');
  38.         $date $request->request->get('date');
  39.         $type $request->request->get('type');
  40.         $selectedDate = new \DateTime($date);
  41.         $timeOfSelectedDate $this->repository->findBy(['date' => $selectedDate'type' => $type]);
  42.         foreach ($timeOfSelectedDate as $appointment) {
  43.             // Remove all appointment not exist AppointmentPerson at send selectedTimes(string) null
  44.             if (empty($selectedTimes) && count($appointment->getAppointmentPerson()) == 0) {
  45.                 $this->repository->delete($appointment);
  46.                 continue;
  47.             }
  48.             if (!in_array($appointment->getStartAt()->format('H:i:s'), $selectedTimes) && count($appointment->getAppointmentPerson()) == 0) {
  49.                 $this->repository->delete($appointment);
  50.             }
  51.         }
  52.         if ($selectedTimes && $date) {
  53.             foreach ($selectedTimes as $time) {
  54.                 if (!$time) continue;
  55.                 $formatTime = new \DateTime($time);
  56.                 $checkAppointment $this->repository->findBy(['date' => $selectedDate'startAt' => $formatTime'type' => $type]);
  57.                 if (!$checkAppointment) {
  58.                     $request->query->set('date'$date);
  59.                     $request->query->set('startAt'$time);
  60.                     $request->query->set('type'$type);
  61.                     $this->add($requestnullnullnullfalse, [], false);
  62.                 }
  63.             }
  64.         }
  65.         return ['status' => 'success'];
  66.     }
  67.     public function updateEntity($request)
  68.     {
  69.         $entity $this->get($request->get('id'));
  70.         $input = [];
  71.         $entity $this->update($entity$request$inputnull'ENTITY'falsetrue);
  72.         return $this->autoMapper->map($entity'App\DTO\Appointment\AppointmentOutput');
  73.     }
  74.     public function getListTimeOfDate($selectedDate$type)
  75.     {
  76.         $timeOfSelectedDate $this->repository->findBy(['date' => new \DateTime($selectedDate), 'type' => $type]);
  77.         return $this->mapTimeOfDate($timeOfSelectedDate);
  78.     }
  79.     public function getListDateAndTime($month null$year null$type)
  80.     {
  81.         if (!$month) {
  82.             $month date('m');
  83.         }
  84.         if (!$year) {
  85.             $year date('Y');
  86.         }
  87.         $startDate date($year '-' $month '-01');
  88.         $endDate  = new \DateTime($year '-' $month '-01');
  89.         $appointments $this->repository->getListByDateTime(null$startDate$endDate->format('Y-m-t'), $type);
  90.         $mapDate = [];
  91.         foreach ($appointments as $appointment) {
  92.             $date $appointment->getDate()->format('Y-m-d');
  93.             if (!isset($mapDate[$date])) {
  94.                 $mapDate[$date][] = $appointment;
  95.             } else {
  96.                 $mapDate[$date][] = $appointment;
  97.             }
  98.         }
  99.         $num cal_days_in_month(CAL_GREGORIAN$month$year);
  100.         $dates_month = array();
  101.         for ($i 1$i <= $num$i++) {
  102.             $mktime mktime(000$month$i$year);
  103.             $dates_month[$i]['full'] = date("Y-m-d"$mktime);
  104.             $dates_month[$i]['day'] = ucfirst(strftime("%a",  $mktime));
  105.             $dates_month[$i]['date'] = date("d"$mktime);
  106.             if (isset($mapDate[$dates_month[$i]['full']])) {
  107.                 $dates_month[$i]['times'] = $this->mapTimeOfDate($mapDate[$dates_month[$i]['full']]);
  108.             } else {
  109.                 $dates_month[$i]['times'] = [];
  110.             }
  111.         }
  112.         return array_values($dates_month);
  113.     }
  114.     private function mapTimeOfDate($timeOfSelectedDate)
  115.     {
  116.         $listTimes Common::listTimes();
  117.         foreach ($listTimes as $k => $time) {
  118.             foreach ($timeOfSelectedDate as $appointment) {
  119.                 $hasTime $appointment->getStartAt()->format('H:i:s');
  120.                 if ($hasTime == $time['value']) {
  121.                     if (!$appointment->getReservation() || $appointment->getReservation() == Appointment::REVERVATION_CANCELED) {
  122.                         $listTimes[$k]['status'] = 'create';
  123.                     } else if ($appointment->getIsEnded()) {
  124.                         $listTimes[$k]['status'] = 'ended';
  125.                     } else if ($appointment->getReservation() == Appointment::REVERVATION_VALIDATING) {
  126.                         $listTimes[$k]['status'] = 'validating';
  127.                         $listTimes[$k]['id'] = isset($appointment) ? $appointment->getId() : '';
  128.                     } else if ($appointment->getReservation() == Appointment::REVERVATION_CONFIRMED) {
  129.                         $listTimes[$k]['status'] = 'confirm';
  130.                         $listTimes[$k]['id'] = isset($appointment) ? $appointment->getId() : '';
  131.                     }
  132.                 }
  133.             }
  134.         }
  135.         return $listTimes;
  136.     }
  137. }