<?php
namespace App\Service;
use App\Entity\Appointment;
use App\Constant\Common;
use App\DTO\Appointment\AppointmentOutput;
use App\Repository\AppointmentRepository;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class AppointmentService
* @package App\Service
*/
class AppointmentService extends BaseService
{
/**
* AppointmentService constructor.
* @param AppointmentRepository $repository
*/
public function __construct(
AppointmentRepository $repository,
BaseService $baseService,
TranslatorInterface $translator
) {
$this->reflectFromParent($baseService);
$this->repository = $repository;
$this->translator = $translator;
}
public function getListAll($requestData = null)
{
return $this->repository->getListAll($requestData, AppointmentOutput::class);
}
public function getById($id)
{
return $this->autoMapper->getById($id, AppointmentOutput::class);
}
public function addEntity($request)
{
$selectedTimes = $request->request->get('selectedTimes');
$date = $request->request->get('date');
$type = $request->request->get('type');
$selectedDate = new \DateTime($date);
$timeOfSelectedDate = $this->repository->findBy(['date' => $selectedDate, 'type' => $type]);
foreach ($timeOfSelectedDate as $appointment) {
// Remove all appointment not exist AppointmentPerson at send selectedTimes(string) null
if (empty($selectedTimes) && count($appointment->getAppointmentPerson()) == 0) {
$this->repository->delete($appointment);
continue;
}
if (!in_array($appointment->getStartAt()->format('H:i:s'), $selectedTimes) && count($appointment->getAppointmentPerson()) == 0) {
$this->repository->delete($appointment);
}
}
if ($selectedTimes && $date) {
foreach ($selectedTimes as $time) {
if (!$time) continue;
$formatTime = new \DateTime($time);
$checkAppointment = $this->repository->findBy(['date' => $selectedDate, 'startAt' => $formatTime, 'type' => $type]);
if (!$checkAppointment) {
$request->query->set('date', $date);
$request->query->set('startAt', $time);
$request->query->set('type', $type);
$this->add($request, null, null, null, false, [], false);
}
}
}
return ['status' => 'success'];
}
public function updateEntity($request)
{
$entity = $this->get($request->get('id'));
$input = [];
$entity = $this->update($entity, $request, $input, null, 'ENTITY', false, true);
return $this->autoMapper->map($entity, 'App\DTO\Appointment\AppointmentOutput');
}
public function getListTimeOfDate($selectedDate, $type)
{
$timeOfSelectedDate = $this->repository->findBy(['date' => new \DateTime($selectedDate), 'type' => $type]);
return $this->mapTimeOfDate($timeOfSelectedDate);
}
public function getListDateAndTime($month = null, $year = null, $type)
{
if (!$month) {
$month = date('m');
}
if (!$year) {
$year = date('Y');
}
$startDate = date($year . '-' . $month . '-01');
$endDate = new \DateTime($year . '-' . $month . '-01');
$appointments = $this->repository->getListByDateTime(null, $startDate, $endDate->format('Y-m-t'), $type);
$mapDate = [];
foreach ($appointments as $appointment) {
$date = $appointment->getDate()->format('Y-m-d');
if (!isset($mapDate[$date])) {
$mapDate[$date][] = $appointment;
} else {
$mapDate[$date][] = $appointment;
}
}
$num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$dates_month = array();
for ($i = 1; $i <= $num; $i++) {
$mktime = mktime(0, 0, 0, $month, $i, $year);
$dates_month[$i]['full'] = date("Y-m-d", $mktime);
$dates_month[$i]['day'] = ucfirst(strftime("%a", $mktime));
$dates_month[$i]['date'] = date("d", $mktime);
if (isset($mapDate[$dates_month[$i]['full']])) {
$dates_month[$i]['times'] = $this->mapTimeOfDate($mapDate[$dates_month[$i]['full']]);
} else {
$dates_month[$i]['times'] = [];
}
}
return array_values($dates_month);
}
private function mapTimeOfDate($timeOfSelectedDate)
{
$listTimes = Common::listTimes();
foreach ($listTimes as $k => $time) {
foreach ($timeOfSelectedDate as $appointment) {
$hasTime = $appointment->getStartAt()->format('H:i:s');
if ($hasTime == $time['value']) {
if (!$appointment->getReservation() || $appointment->getReservation() == Appointment::REVERVATION_CANCELED) {
$listTimes[$k]['status'] = 'create';
} else if ($appointment->getIsEnded()) {
$listTimes[$k]['status'] = 'ended';
} else if ($appointment->getReservation() == Appointment::REVERVATION_VALIDATING) {
$listTimes[$k]['status'] = 'validating';
$listTimes[$k]['id'] = isset($appointment) ? $appointment->getId() : '';
} else if ($appointment->getReservation() == Appointment::REVERVATION_CONFIRMED) {
$listTimes[$k]['status'] = 'confirm';
$listTimes[$k]['id'] = isset($appointment) ? $appointment->getId() : '';
}
}
}
}
return $listTimes;
}
}