<?php
namespace App\Controller;
use App\Entity\Roles;
use App\Entity\User;
use App\Helpers\Utils;
use App\Services\CashdeskService;
use App\Services\TimetableService;
use App\Services\WorkshiftService;
use App\Services\UserService;
use App\ValueObjects\Timetable\TimetableFilter;
use App\ValueObjects\Timetable\TimetablePersonalReport;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webmozart\Assert\Assert;
use PDO;
class IndexController extends AbstractController
{
/**
* @var CashdeskService
*/
private CashdeskService $cashdeskService;
/** @var UserService */
private UserService $userService;
private TranslatorInterface $translator;
private TimetableService $timetableService;
private WorkshiftService $workshiftService;
public function __construct(CashdeskService $cashdeskService, UserService $userService, TimetableService $timetableService, TranslatorInterface $translator, WorkshiftService $workshiftService)
{
$this->cashdeskService = $cashdeskService;
$this->userService = $userService;
$this->translator = $translator;
$this->timetableService = $timetableService;
$this->workshiftService = $workshiftService;
}
/**
* @Route(path="/", name="app_index")
*/
public function index(): Response
{
return $this->render('contentBlock.html.twig', [
"content" => "",
"cashdesks" => $this->cashdeskService->getAllowedCashdesksList()
]);
}
/**
* @Route("/chooseCashdesk", name="app_choose_cashdesk", methods={"POST"})
* @param Request $request
* @return RedirectResponse|Response
*/
public function chooseCashdesk(Request $request)
{
/** @var User $currentUser */
$currentUser = $this->getUser();
try {
if ($request->request->has("currentCashdeskId")) {
$currentCashdeskId = $request->request->getInt("currentCashdeskId");
$cashdesk = $this->cashdeskService->getById($currentCashdeskId);
Assert::notEmpty($cashdesk, $this->translator->trans('Cashdesk not found'));
if (!empty($currentCashdeskId) && in_array($currentCashdeskId, $currentUser->getAllowedCashdesksId())) {
$this->cashdeskService->setUserIntoCashdesk($currentUser, $currentCashdeskId);
$this->timetableService->saveClubToTimetable($currentUser, $cashdesk->getClubId());
if($currentUser->isTimetableEnabled()){
if($currentUser->getTimetables()->last()->getFinishDatetime()){
$this->timetableService->start($currentUser);
}
}
}
} else {
$this->userService->releaseCurrentCashdesk($currentUser);
}
return $this->redirectToRoute("app_index");
} catch (Exception $e) {
$error = $e->getMessage();
}
return $this->render('contentBlock.html.twig', [
"content" => $error,
"cashdesks" => $this->cashdeskService->getAllowedCashdesksList()
]);
}
/**
* @Route("/workShiftOpen", name="app_work_shift_open", methods={"POST"})
* @param Request $request
* @return RedirectResponse|Response
*/
public function workShiftOpen(Request $request)
{
/** @var User $currentUser */
$currentUser = $this->getUser();
try {
$currentCashdesk = $currentUser->getCurrentCashdesk();
Assert::notEmpty($currentCashdesk, $this->translator->trans('Please, choose your current cashdesk on top menu'));
Assert::true($currentCashdesk->isGranted(Roles::ROLE_ONLINE_CASHDESK_WORK_SHIFT), $this->translator->trans('You do not have permission'));
$this->workshiftService->open($currentCashdesk, $currentUser);
return $this->redirectToRoute("app_index");
} catch (Exception $e) {
$error = $e->getMessage();
}
return $this->render('contentBlock.html.twig', [
"content" => $error
]);
}
/**
* @Route("/workShiftClose", name="app_work_shift_close", methods={"POST"})
* @param Request $request
* @return RedirectResponse|Response
*/
public function workShiftClose(Request $request)
{
/** @var User $currentUser */
$currentUser = $this->getUser();
try {
$currentCashdesk = $currentUser->getCurrentCashdesk();
Assert::notEmpty($currentCashdesk, $this->translator->trans('Please, choose your current cashdesk on top menu'));
Assert::true($currentCashdesk->isGranted(Roles::ROLE_ONLINE_CASHDESK_WORK_SHIFT), $this->translator->trans('You do not have permission'));
$currentWorkshift = $currentCashdesk->getCurrentWorkshift();
Assert::notEmpty($currentWorkshift, $this->translator->trans('Cashdesk does not have open workshift'));
$this->workshiftService->close($currentWorkshift);
return $this->redirectToRoute("app_index");
} catch (Exception $e) {
$error = $e->getMessage();
}
return $this->render('contentBlock.html.twig', [
"content" => $error
]);
}
/**
* @Route(path="/getBalance", name="app_get_balance")
*/
public function getBalance(): JsonResponse
{
$balance = 0;
/** @var User $currentUser */
$currentUser = $this->getUser();
if ($currentUser) {
$balance = Utils::showMoney($currentUser->getBalance());
if ($this->isGranted(Roles::ROLE_CASHIER) && $currentUser->getCurrentCashdesk() && $currentUser->getCurrentCashdesk()->getBalance()) {
$balance = Utils::showMoney($currentUser->getCurrentCashdesk()->getBalance());
}
}
return new JsonResponse([
'balance' => $balance,
]);
}
/**
* @Route(path="/getScalary", name="app_get_scalary")
*/
// public function getScalary(): JsonResponse
// {
// /** @var User $currentUser */
// $currentUser = $this->getUser();
//
// $filter = new TimetableFilter();
// $filter
// ->setStartDate(strtotime(date("Y-m-01 06:00:00")))
// ->setFinishDate(strtotime(date("Y-m-d H:i:s")))
// ->setUserId($currentUser->getId());
//
// $personalReport = $this->timetableService->getPersonalReport($filter);
// if (sizeof($personalReport->getTimetables()) > 0) {
// foreach ($personalReport->getTimetables() as $timetable) {
//// $finish_date = date("Y-m-d H:i:s");
// if($timetable->getFinishDatetime()){
// $finish_date = $timetable->getFinishDatetime()->format('Y-m-d H:i:s');
//
// $info_user[] = [
// 'user_id' => $timetable->getUser()->getId(),
// 'start_datetime' => $timetable->getStartDatetime()->format('Y-m-d H:i:s'),
// 'finish_datetime' => $finish_date,
// 'hour_cost_0' => $timetable->getUser()->getHourCost_0(),
// 'hour_cost_1' => $timetable->getUser()->getHourCost_1(),
// 'hour_cost_2' => $timetable->getUser()->getHourCost_2(),
// ];
// }
// }
//
// $array_info = [
// "club_title" => "",
// ];
// foreach ($info_user as $row){
// if(date("d", strtotime($row['start_datetime'])) > 15){
// $half = "second_half_duration";
// }elseif(date("d", strtotime($row['start_datetime'])) <= 15){
// $half = "first_half_duration";
// }
//
// $duration = "";
//
//// $array_info["club_title"] = $row["club_title"];
//// $array_info["username"] = $row["username"];
//// $array_info["shift_duration"] = $row["username"];
//// $array_info["shift_cost"] = $row["shift_cost"];
// $array_info["hour_cost_0"] = $row["hour_cost_0"];
// $array_info["hour_cost_1"] = $row["hour_cost_1"];
// $array_info["hour_cost_2"] = $row["hour_cost_2"];
//// $array_info["ban"] = $row["ban"];
//// $array_info["bank_title"] = $row["bank_title"];
// $array_info["start_datetime"][] = $row["start_datetime"];
// $array_info["finish_datetime"][] = $row["finish_datetime"];
// $array_info["shift_datetime"][] = [$row["start_datetime"], $row["finish_datetime"]];
//
// $T1 = strtotime($row['start_datetime']);
// $T2 = strtotime($row['finish_datetime']);
//
// $t1_6am_8am = strtotime(date("Y-m-d 06:00:00", $T1));
// if(($T2-$T1) < 86400){
// $t2_6am_8am = strtotime(date("Y-m-d 08:00:00", $T2));
// }else{
// $t2_6am_8am = strtotime(date("Y-m-d 08:00:00", $T1));
// }
// $duration_6am_8am = $Lmin_6am_8am = $Lreal_6am_8am = 0;
// $Lmin_6am_8am = ($T2 - $T1) + ($t2_6am_8am - $t1_6am_8am);
// $Lreal_6am_8am = max($T2, $t2_6am_8am) - max($T1, $t1_6am_8am);
// if($Lmin_6am_8am >= $Lreal_6am_8am){
// if(($T1 <= $t1_6am_8am) && ($T2 >= $t2_6am_8am)){
// $duration_6am_8am = $t2_6am_8am - $t1_6am_8am;
// $duration = "duration_6am_8am";
// }
// if((($T1 >= $t1_6am_8am) && ($T1 < $t2_6am_8am)) && ($T2 >= $t2_6am_8am)){
// if(($t2_6am_8am - $T1) > 0){
// $duration_6am_8am = $t2_6am_8am - $T1;
// $duration = "duration_6am_8am";
// }
// }
// if(($T1 >= $t1_6am_8am) && ($T2 <= $t2_6am_8am)){
// $duration_6am_8am = $Lmin_6am_8am - ($t2_6am_8am - $t1_6am_8am);
// $duration = "duration_6am_8am";
// }
// if(($T1 <= $t1_6am_8am) && ($T2 <= $t2_6am_8am) && ($T2 > $t1_6am_8am)){
// $duration_6am_8am = $T2 - $t1_6am_8am;
// $duration = "duration_6am_8am";
// }
// }
// if(($T2-$T1) >= 86400){
// $koef = intval(($T2-$T1) / 86400);
// $duration_6am_8am = $duration_6am_8am * $koef;
// }
// if($half == "first_half_duration" && $duration == "duration_6am_8am" && $duration_6am_8am > 0){
// if(isset($array_info[$half][$duration])){
// $array_info[$half][$duration] = $array_info[$half][$duration] + $duration_6am_8am;
// }else{
// $array_info[$half][$duration] = $duration_6am_8am;
// }
// }
// if($half == "second_half_duration" && $duration == "duration_6am_8am" && $duration_6am_8am > 0){
// $a[] = $duration_6am_8am;
// if(isset($array_info[$half][$duration])){
// $array_info[$half][$duration] = $array_info[$half][$duration] + $duration_6am_8am;
// }else{
// $array_info[$half][$duration] = $duration_6am_8am;
// }
// }
//
// $t1_8am_7pm = strtotime(date("Y-m-d 08:00:00", $T1));
// if(($T2-$T1) < 86400){
// $t2_8am_7pm = strtotime(date("Y-m-d 19:00:00", $T2));
// }else{
// $t2_8am_7pm = strtotime(date("Y-m-d 19:00:00", $T1));
// }
// $duration_8am_7pm = $Lmin_8am_7pm = $Lreal_8am_7pm = 0;
// $Lmin_8am_7pm = ($T2 - $T1) + ($t2_8am_7pm - $t1_8am_7pm);
// $Lreal_8am_7pm = max($T2, $t2_8am_7pm) - max($T1, $t1_8am_7pm);
// if($Lmin_8am_7pm >= $Lreal_8am_7pm){
// if(($T1 <= $t1_8am_7pm) && ($T2 >= $t2_8am_7pm)){
// $duration_8am_7pm = $t2_8am_7pm - $t1_8am_7pm;
// $duration = "duration_8am_7pm";
// }
// if((($T1 >= $t1_8am_7pm) && $T1 < $t2_8am_7pm) && ($T2 >= $t2_8am_7pm)){
// if(($t2_8am_7pm - $T1) > 0){
// $duration_8am_7pm = $t2_8am_7pm - $T1;
// $duration = "duration_8am_7pm";
// }
// }
// if(($T1 <= $t1_8am_7pm) && ($T2 <= $t2_8am_7pm) && ($T2 > $t1_8am_7pm)){
// $duration_8am_7pm = $T2 - $t1_8am_7pm;
// $duration = "duration_8am_7pm";
// }
// if(($T1 >= $t1_8am_7pm) && ($T2 <= $t2_8am_7pm)){
// $duration_8am_7pm = $Lmin_8am_7pm - ($t2_8am_7pm - $t1_8am_7pm);
// $duration = "duration_8am_7pm";
// }
// }
// if(($T2-$T1) >= 86400){
// $koef = intval(($T2-$T1) / 86400);
// $duration_8am_7pm = $duration_8am_7pm * $koef;
// }
// if($half == "first_half_duration" && $duration == "duration_8am_7pm" && $duration_8am_7pm > 0){
// if(isset($array_info[$half][$duration])){
// $array_info[$half][$duration] = $array_info[$half][$duration] + $duration_8am_7pm;
// }else{
// $array_info[$half][$duration] = $duration_8am_7pm;
// }
// }
// if($half == "second_half_duration" && $duration == "duration_8am_7pm" && $duration_8am_7pm > 0){
// if(isset($array_info[$half][$duration])){
// $array_info[$half][$duration] = $array_info[$half][$duration] + $duration_8am_7pm;
// }else{
// $array_info[$half][$duration] = $duration_8am_7pm;
// }
// }
//
// $t1_7pm_10pm = strtotime(date("Y-m-d 19:00:00", $T1));
// if(($T2-$T1) < 86400){
// $t2_7pm_10pm = strtotime(date("Y-m-d 22:00:00", $T2));
// }else{
// $t2_7pm_10pm = strtotime(date("Y-m-d 22:00:00", $T1));
// }
// $duration_7pm_10pm = $Lmin_7pm_10pm = $Lreal_7pm_10pm = 0;
// $Lmin_7pm_10pm = ($T2 - $T1) + ($t2_7pm_10pm - $t1_7pm_10pm);
// $Lreal_7pm_10pm = max($T2, $t2_7pm_10pm) - max($T1, $t1_7pm_10pm);
// if($Lmin_7pm_10pm >= $Lreal_7pm_10pm){
// if(($T1 <= $t1_7pm_10pm) && ($T2 >= $t2_7pm_10pm)){
// $duration_7pm_10pm = $t2_7pm_10pm - $t1_7pm_10pm;
// $duration = "duration_7pm_10pm";
// }
// if((($T1 >= $t1_7pm_10pm) && $T1 < $t2_7pm_10pm) && ($T2 > $t2_7pm_10pm)){
// if(($t2_7pm_10pm - $T1) > 0){
// $duration_7pm_10pm = $t2_7pm_10pm - $T1;
// $duration = "duration_7pm_10pm";
// }
// }
// if(($T1 <= $t1_7pm_10pm) && ($T2 <= $t2_7pm_10pm) && ($T2 > $t1_7pm_10pm)){
// $duration_7pm_10pm = $T2 - $t1_7pm_10pm;
// $duration = "duration_7pm_10pm";
// }
// if(($T1 >= $t1_7pm_10pm) && ($T2 <= $t2_7pm_10pm)){
// $duration_7pm_10pm = $Lmin_7pm_10pm - ($t2_7pm_10pm - $t1_7pm_10pm);
// $duration = "duration_7pm_10pm";
// }
// }
// if(($T2-$T1) >= 86400){
// $koef = intval(($T2-$T1) / 86400);
// $duration_7pm_10pm = $duration_7pm_10pm * $koef;
// }
// if($half == "first_half_duration" && $duration == "duration_7pm_10pm" && $duration_7pm_10pm > 0){
// if(isset($array_info[$half][$duration])){
// $array_info[$half][$duration] = $array_info[$half][$duration] + $duration_7pm_10pm;
// }else{
// $array_info[$half][$duration] = $duration_7pm_10pm;
// }
// }
// if($half == "second_half_duration" && $duration == "duration_7pm_10pm" && $duration_7pm_10pm > 0){
// if(isset($array_info[$half][$duration])){
// $array_info[$half][$duration] = $array_info[$half][$duration] + $duration_7pm_10pm;
// }else{
// $array_info[$half][$duration] = $duration_7pm_10pm;
// }
// }
// }
//
// $array_sum_first_6am_8am = $array_sum_first_8am_7pm = $array_sum_first_7pm_10pm = 0;
// $array_sum_second_6am_8am = $array_sum_second_8am_7pm = $array_sum_second_7pm_10pm = 0;
// foreach ($info_user as $row) {
// if(isset($array_info["first_half_duration"]["duration_6am_8am"]))
// $array_sum_first_6am_8am = $array_info["first_half_duration"]["duration_6am_8am"];
// if(isset($array_info["first_half_duration"]["duration_8am_7pm"]))
// $array_sum_first_8am_7pm = $array_info["first_half_duration"]["duration_8am_7pm"];
// if(isset($array_info["first_half_duration"]["duration_7pm_10pm"]))
// $array_sum_first_7pm_10pm = intval($array_info["first_half_duration"]["duration_7pm_10pm"]);
//
// if(isset($array_info["second_half_duration"]["duration_6am_8am"]))
// $array_sum_second_6am_8am = $array_info["second_half_duration"]["duration_6am_8am"];
// if(isset($array_info["second_half_duration"]["duration_8am_7pm"]))
// $array_sum_second_8am_7pm = intval($array_info["second_half_duration"]["duration_8am_7pm"]);
// if(isset($array_info["second_half_duration"]["duration_7pm_10pm"]))
// $array_sum_second_7pm_10pm = intval($array_info["second_half_duration"]["duration_7pm_10pm"]);
//
// $result = [
// 'user_id' => intval($row['user_id']),
// 'hour_cost_0' => intval($row['hour_cost_0']),
// 'hour_cost_1' => intval($row['hour_cost_1']),
// 'hour_cost_2' => intval($row['hour_cost_2']),
// 'firstHalfDuration_6am_8am' => $array_sum_first_6am_8am,
// 'secondHalfDuration_6am_8am' => $array_sum_second_6am_8am,
// 'firstHalfDuration_8am_7pm' => $array_sum_first_8am_7pm,
// 'secondHalfDuration_8am_7pm' => $array_sum_second_8am_7pm,
// 'firstHalfDuration_7pm_10pm' => $array_sum_first_7pm_10pm,
// 'secondHalfDuration_7pm_10pm' => $array_sum_second_7pm_10pm,
// ];
// }
//
// $firstHalfShiftsCount_6am_8am = intval($result["firstHalfDuration_6am_8am"] / 60);
// $firstHalfShiftsCount_8am_7pm = intval($result["firstHalfDuration_8am_7pm"] / 60);
// $firstHalfShiftsCount_7pm_10pm = intval($result["firstHalfDuration_7pm_10pm"] / 60);
// $firstHalfSalary = $firstHalfShiftsCount_6am_8am * intval($result["hour_cost_0"] / 60) + $firstHalfShiftsCount_8am_7pm * intval($result["hour_cost_1"] / 60) + $firstHalfShiftsCount_7pm_10pm * intval($result["hour_cost_2"] / 60);
// $firstHalfSalary = intval($firstHalfSalary/100)*0.1;
// $firstHalfSalary = intval(round($firstHalfSalary)*1000);
// $days_1 = $firstHalfShiftsCount_6am_8am + $firstHalfShiftsCount_8am_7pm + $firstHalfShiftsCount_7pm_10pm;
// $secondHalfShiftsCount_6am_8am = intval($result["secondHalfDuration_6am_8am"] / 60);
// $secondHalfShiftsCount_8am_7pm = intval($result["secondHalfDuration_8am_7pm"] / 60);
// $secondHalfShiftsCount_7pm_10pm = intval($result["secondHalfDuration_7pm_10pm"] / 60);
// $secondHalfSalary = $secondHalfShiftsCount_6am_8am * intval($result["hour_cost_0"] / 60) + $secondHalfShiftsCount_8am_7pm * intval($result["hour_cost_1"] / 60) + $secondHalfShiftsCount_7pm_10pm * intval($result["hour_cost_2"] / 60);
// $secondHalfSalary = intval($secondHalfSalary/100)*0.1;
// $secondHalfSalary = intval(round($secondHalfSalary)*1000);
// $days_2 = $secondHalfShiftsCount_6am_8am + $secondHalfShiftsCount_8am_7pm + $secondHalfShiftsCount_7pm_10pm;
// $salaryGross = $firstHalfSalary + $secondHalfSalary;
// $annualSalaryGross = $salaryGross * 12;
// }
//
// function secToTimeArray($secs)
// {
// $res = array();
// $res['hours'] = floor($secs / 3600);
// $secs = $secs % 3600;
// $minutes = floor($secs / 60);
// if($minutes < 10)
// $minutes = "0" . $minutes;
// $res['minutes'] = $minutes;
// return $res;
// }
//
// $time_1 = secToTimeArray($days_1 * 60);
// $time_2 = secToTimeArray($days_2 * 60);
//
//
// return new JsonResponse([
// 'firstHalfSalary' => $firstHalfSalary / 100,
// 'secondHalfSalary' => $secondHalfSalary / 100,
// 'work_hours_1' => $time_1['hours'] . ":" . $time_1['minutes'],
// 'work_hours_2' => $time_2['hours'] . ":" . $time_2['minutes'],
// 'month' => date("F"),
// ]);
// }
}