src/Controller/SymulationController.php line 344

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Posrednik\HPracy;
  4. use App\Entity\Swieta;
  5. use App\Repository\HPracyRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class SymulationController extends AbstractController
  11. {
  12.     protected $requestStack;
  13.     public function __construct(RequestStack $requestStacknull)
  14.     {
  15.         $this->requestStack $requestStack;
  16.     }
  17.     /**
  18.      * @Route("/symulation", name="symulation")
  19.      *
  20.      *
  21.      */
  22.     public function indexAction(Request $request){
  23.         $emi $this->getDoctrine()->getManager();
  24.         $repositorySym $emi->getRepository('App:Symulation');
  25.         $repositoryObjectDef $emi->getRepository('App:ObjectDef');
  26.         $repositorySymDef $emi->getRepository('App:SymulationDef');
  27.         $querySym $repositorySym
  28.             ->createQueryBuilder('s')
  29.             ->select('s')
  30.             ->where('s.active =1 ')
  31.             ->setMaxResults(10)
  32.             ->getQuery();
  33.         $symulation $querySym->execute();
  34.         $workTime 0;
  35.         foreach ($symulation as $sym) {
  36.             $symID $sym->getId();
  37.             $obiekt_id $sym->getObject()->getId();
  38.             if ($symID <>''){
  39.                 $symDef $repositorySymDef->findby(array('symulation'=> $symID));
  40.                 $employment_arr[$symID] =  $this->getEmployment$symDef);
  41.                 $object_arr[$symID] =  $this->getObjectDef($symDef);
  42.                 $object_hour_arr[$symID] =  $this->getObjectPlanWork($this->getObjectDef($symDef));
  43.             }
  44.         }
  45.         $monthCost $this->getMonthCost($symulation$object_arr$object_hour_arr$employment_arr);
  46.         return $this->render('symulation/index.html.twig',[
  47.             'object' => $object_arr,
  48.             'employer' => $employment_arr,
  49.             'symulation' => $symulation,
  50.             'monthCost' => $monthCost,
  51.             'activityByDay' => $object_hour_arr,
  52.             'monthWorkTime' => $workTime,
  53.         ]);
  54.     }
  55.     /**
  56.      * Konwersja czasu na string
  57.      */
  58.     public function getFirstDay($date){
  59.         return  date("Y-m-d",strtotime($date) );
  60.     }
  61.     public function getTimetoHour($date){
  62.         if($date == null)
  63.             return null;
  64.         $godziny $date->format("H");
  65.         if($date->format("i") == 59)
  66.             $godziny $godziny +1;
  67.         return  ($godziny);
  68.     }
  69.     public function getTimeMinute($date){
  70.         if($date == null)
  71.             return null;
  72.         $godziny $date->format("i");
  73.         if($date->format("i") == 59)
  74.             $godziny 0;
  75.         return  ($godziny);
  76.     }
  77.     public function getTimetoMinute($date){
  78.         if($date == null)
  79.             return 0;
  80.         $godziny $date->format("H");
  81.         $minuty $date->format("i");
  82.         return  ($godziny*60)+$minuty;
  83.     }
  84.     public function getTimetoSec($date){
  85.         if($date == null)
  86.             return 0;
  87.         $godziny $date->format("H");
  88.         $minuty $date->format("i");
  89.         return  ($godziny*60*60)+($minuty*60);
  90.     }
  91.     public function getLastDayForOneMonth($month$rok=2022){
  92.             $arr $this->getLastDayInt($rok.'-'.$month'-01');
  93.         return $arr;
  94.     }
  95.     public function getLastDayForMonth($month$rok=2022){
  96.         $arr=[];
  97.             for ($m=1$m <= $month$m++) {
  98.                 //ToDo rok z konfigu
  99.                 $arr[$m] = $this->getLastDayInt($rok.'-'.$m'-01');
  100.             }
  101.             return $arr;
  102.     }
  103.     public function getLastDay($date){
  104.         return  date("Y-m-t",strtotime($date) );
  105.     }
  106.     public function getLastDayInt($date){
  107.         return  date("t",strtotime($date) );
  108.     }
  109.     public function getTimeDiff($timeStart$timeEnd){
  110.         $hourEnd date_format($timeEnd,'H');
  111.         if ($hourEnd =="00"){
  112.             $hourEnd 24;
  113.         }
  114.         $hourStart date_format($timeStart,'H');
  115.         $h = ($hourEnd $hourStart)*60;
  116.         //$h = (date_diff($timeStart, $timeEnd)->h) * 60;
  117.         $m = (date_diff($timeStart$timeEnd)->i);
  118.         return ($h+$m);
  119.     }
  120.     /**
  121.      * Konwersja czasu na string
  122.      */
  123.     public function getTime($time){
  124.         return $time->format('H:i');
  125.     }
  126.     /**
  127.      * Zwraca plan pracy
  128.      *
  129.      */
  130.     public function getObjectPlanWork($obj)
  131.     {
  132.         $idObject null;
  133.         $arr null;
  134.         if(is_array($obj)) {
  135.             $idObject $obj["id"];
  136.             $emi $this->getDoctrine()->getManager();
  137.             $repositorySym $emi->getRepository('App:ObjectDefMonth');
  138.             $queryAll $repositorySym
  139.                 ->createQueryBuilder('o')
  140.                 ->select('o')
  141.                 ->where('o.defId = :id')
  142.                 ->setParameter('id'$idObject)
  143.                 ->getQuery();
  144.             $workDay $queryAll->execute();
  145.             $querySum $repositorySym
  146.                 ->createQueryBuilder('o')
  147.                 ->select('o.defId,
  148.                 o.year, o.month, 
  149.                 sum(CASE 
  150.                         WHEN (o.hour >=22 or o.hour < 6) 
  151.                         THEN o.employers * o.work_time 
  152.                         ELSE 
  153.                         0
  154.                         END 
  155.                     ) as nightHours,
  156.                 sum(CASE 
  157.                         WHEN (o.hour <22 and o.hour >= 6) 
  158.                         THEN o.employers * o.work_time 
  159.                         ELSE 
  160.                         0
  161.                         END 
  162.                 ) as dayHours,
  163.                
  164.                 sum(CASE 
  165.                         WHEN (o.day_num = \'swieto\') 
  166.                         THEN o.employers * o.work_time 
  167.                         ELSE 
  168.                         0
  169.                         END 
  170.                 ) as hollidayHours,
  171.                  sum(CASE 
  172.                         WHEN (o.day_num = \'sobota\') 
  173.                         THEN o.employers * o.work_time 
  174.                         ELSE 
  175.                         0
  176.                         END 
  177.                 ) as sobotaHours,
  178.                 
  179.                  sum(CASE 
  180.                         WHEN (o.day_num = \'niedziela\') 
  181.                         THEN o.employers * o.work_time 
  182.                         ELSE 
  183.                         0
  184.                         END 
  185.                 ) as niedzielaHours
  186.                 ')
  187.                 ->where('o.defId = :id')
  188.                 ->groupBy('o.month, o.year, o.defId')
  189.                 ->setParameter('id'$idObject)
  190.                 ->getQuery();
  191.             $sumMonth $querySum->execute();
  192.            $arr = [
  193.                 "sumHour" => $sumMonth,
  194.                 "allHour" => $workDay,
  195.            ];
  196.         }
  197.         return $arr  ;
  198.     }
  199.     /**
  200.      * Zwraca definicje obiektu do symulacji
  201.      *
  202.      */
  203.     public function getObjectDef($obj)
  204.     {
  205.         $arr null;
  206.         if (is_array($obj)){
  207.                 $object $obj[0]->getSymulation()->getObject();
  208.                 $arr = [
  209.                         "id" => $object->getId(),
  210.                         "name" => $object->getName(),
  211.                         "hoursMonthly" => $object->getHoursMonth(),
  212.                         "employerMaxHour" => $object->getEmployerMaxHour(),
  213.                         "objectId" => $object->getObjectId(),
  214.                         "hourStart" => $object->getHourStart(),
  215.                         "hourEnd" => $object->getHourEnd(), //format('H:i');
  216.                         "hourDiff" => 12,
  217.                 ];
  218.             }
  219.         return $arr;
  220.     }
  221.     /**
  222.      * Zwraca definicje zatrudnienia do symulacji
  223.      *
  224.      */
  225.     public function getEmployment($sym)
  226.     {
  227.         $arr null;
  228.             if (is_array($sym)){
  229.                 $numAll=0;
  230.                 foreach ($sym as $s){
  231.                    $arr[$s->getEmployer()->getId()] = [
  232.                        "name" => $s->getEmployer()->getName(),
  233.                        "nameType" => $s->getEmployer()->getNameType(),
  234.                        "costBase" => $s->getEmployer()->getCostBase(),
  235.                        "costEmployer" => $s->getEmployer()->getCostEmployer(),
  236.                        "nightHours" => $s->getEmployer()->getNightHours(),
  237.                        "overtime" => $s->getEmployer()->getOvertime(),
  238.                        "norm" => $s->getEmployer()->getNorm(),
  239.                        "holiday" => $s->getEmployer()->getHoliday(),
  240.                        "num" => $s->getEmployerNum(),
  241.                    ];
  242.                 $numAll $numAll $s->getEmployerNum();
  243.                 }
  244.                 $arr = [
  245.                     $arr,
  246.                     "numAll" => $numAll,
  247.                 ];
  248.             }
  249.         return $arr;
  250.     }
  251.     public function getDayNum($date$em){
  252.         if($date == null)
  253.             return null;
  254.         if($em == null)
  255.             $em $this->getDoctrine()->getManager();
  256.         $day_num 'normalny';
  257.         $w $date->format("w");
  258.         if ($w == 6)
  259.             $day_num 'sobota';
  260.         if ($w == 0)
  261.             $day_num 'niedziela';
  262.             if(in_array($date->format("Y-m-d"), $this->getHollidays(2022,null$em), true)){
  263.                 $day_num 'swieto';
  264.             }
  265.         return $day_num;
  266.     }
  267.     public function getHollidays($year=2022$month null$em){
  268.         //ToDo zapytanie zmienić by uwzględniało rok i miesiąc
  269.         $holidays = array();
  270.         if($em == null)
  271.             $em $this->getDoctrine()->getManager();
  272.         $holidays_ $em->getRepository(Swieta::class)->findAll();
  273.         foreach ($holidays_ as $h){
  274.             $holidays[$h->getData()->format("Y-m-d")] = $h->getData()->format("Y-m-d");
  275.         }
  276.         return $holidays;
  277.     }
  278.     public function getMonthCost($symulation$object$object_hour$employment$year=2022 ){
  279.         $arrnull;
  280.         $em_posrednik $this->getDoctrine()->getManager('posrednik');
  281.         $em $this->getDoctrine()->getManager();
  282.         $holidays $this->getHollidays($yearnull$em);
  283.         if($symulation) {
  284.             $objetID $symulation[0]->getObject()->getObjectId()->getId();
  285.             $hpracyRepository$em_posrednik->getRepository(HPracy::class);
  286.         }
  287.         // generowanie  symulacji dla poszczególnych obiektów
  288.         foreach ($symulation as $sym) {
  289.             $symId =$sym->getId();
  290.             if ($symId <>''){
  291.                 $a=0;
  292.                 $month_arr $object_hour[$symId]['sumHour'];
  293.                 //generowanie symulacji dla poszczególnych miesiecy zdefiniowanych dla obiektu
  294.                 while ($a count($month_arr) ) {
  295.                     $month $month_arr[$a]["month"];
  296.                     $a_date $year.'-'.$month.'-01';
  297.                     $firstDay $this->getFirstDay($a_date);
  298.                     $lastDay $this->getLastDay($a_date);
  299.                     $year_1 $year 1;
  300.                     $a_date_1 $year_1.'-'.$month.'-01';
  301.                     $a_date_1 $year_1.'-'.$month.'-01';
  302.                     $firstDay_1 $this->getFirstDay($a_date_1);
  303.                     $lastDay_1 $this->getLastDay($a_date_1);
  304.                     $lastDay_ $this->getLastDayInt($a_date);
  305.                     $workingDay $this->getWorkingDays($firstDay$lastDay$holidays);
  306.                     $object_ $object[$symId];
  307.                     $objectHour $month_arr[$a]["dayHours"]+ $month_arr[$a]["nightHours"];
  308.                     $objectHourNight $month_arr[$a]["nightHours"];
  309.                     $objectHourHolliday $month_arr[$a]["hollidayHours"];
  310.                     $objectHourSobota $month_arr[$a]["sobotaHours"];
  311.                     $objectHourNiedziela $month_arr[$a]["niedzielaHours"];
  312.                     $objectHourDiff $object_['hourDiff'];
  313.                     $hpracyResult $hpracyRepository
  314.                         ->createQueryBuilder('h')
  315.                         ->select('h.rodzaj, sum(h.czasMinuty/60/60) as czas')
  316.                         ->where('h.obiektyId = :obiekt')
  317.                         ->andWhere('h.dzien between :firstday AND :lastday ')
  318.                         ->andWhere('h.rodzaj <> \'NZ\' ' )
  319.                         ->setParameter('obiekt'$objetID)
  320.                         ->setParameter('firstday'$firstDay)
  321.                         ->setParameter('lastday'$lastDay)
  322.                         ->groupBy('h.rodzaj')
  323.                         ->getQuery()
  324.                         ->getScalarResult()
  325.                     ;
  326.                     $hpracyResult_1 $hpracyRepository
  327.                         ->createQueryBuilder('h')
  328.                         ->select('h.rodzaj, sum(h.czasMinuty/60/60) as czas')
  329.                         ->where('h.obiektyId = :obiekt')
  330.                         ->andWhere('h.dzien between :firstday AND :lastday ')
  331.                         ->andWhere('h.rodzaj <> \'NZ\' ' )
  332.                         ->setParameter('obiekt'$objetID)
  333.                         ->setParameter('firstday'$firstDay_1)
  334.                         ->setParameter('lastday'$lastDay_1)
  335.                         ->groupBy('h.rodzaj')
  336.                         ->getQuery()
  337.                         ->getScalarResult()
  338.                     ;
  339.                     $arr[$symId][$month]= [
  340.                                 "firstDay"=> $firstDay,
  341.                                 "lastDay"=> $lastDay,
  342.                                 "dniRoboczych" => $workingDay,
  343.                                 "objectHour" => $objectHour,
  344.                                 "objectHourNight" => $objectHourNight,
  345.                                 "objectHourHolliday" => $objectHourHolliday,
  346.                                 "objectHourSoboty" => $objectHourSobota,
  347.                                 "objectHourNiedziele" => $objectHourNiedziela,
  348.                                 "month" => $month,
  349.                                 "idSym" => $symId,
  350.                                 "hp" => $hpracyResult,
  351.                                 "hp_1" => $hpracyResult_1,
  352.                     ];
  353.                     $a++;
  354.                 }
  355.             }
  356.         }
  357.     return $arr;
  358.     }
  359.     /**
  360.      * @Route("/activity", name="activity")
  361.      *
  362.      *
  363.      */
  364.     public function activityAction(Request $request){
  365.         return $this->render('symulation/modal.html.twig',[
  366.             "day" => $request->query->get('id'),
  367.             "request"=> $request
  368.         ]);
  369.     }
  370. public function getMonthWorkTime($objectID$month$year){
  371.     $sqlDay "SELECT day, sum(employers*work_time) FROM `object_def_month` where month=".$month." and year=".$year." and def_id = ".$objectID." group by day";
  372.     dump($sqlDay);
  373. }
  374.     public function getWorkingDays($startDate,$endDate,$holidays){
  375.         $endDate strtotime($endDate);
  376.         $startDate strtotime($startDate);
  377.         $days = ($endDate $startDate) / 86400 1;
  378.         $no_full_weeks floor($days 7);
  379.         $no_remaining_days fmod($days7);
  380.         $the_first_day_of_week date("N"$startDate);
  381.         $the_last_day_of_week date("N"$endDate);
  382.         if ($the_first_day_of_week <= $the_last_day_of_week) {
  383.             if ($the_first_day_of_week <= && <= $the_last_day_of_week$no_remaining_days--;
  384.             if ($the_first_day_of_week <= && <= $the_last_day_of_week$no_remaining_days--;
  385.         }
  386.         else {
  387.             if ($the_first_day_of_week == 7) {
  388.                 $no_remaining_days--;
  389.                 if ($the_last_day_of_week == 6) {
  390.                     $no_remaining_days--;
  391.                }
  392.             }
  393.             else {
  394.                 $no_remaining_days -= 2;
  395.             }
  396.         }
  397.         $workingDays $no_full_weeks 5;
  398.         if ($no_remaining_days )
  399.         {
  400.             $workingDays += $no_remaining_days;
  401.         }
  402.         foreach($holidays as $holiday){
  403.             $time_stamp=strtotime($holiday);
  404.             if ($startDate <= $time_stamp && $time_stamp <= $endDate && date("N",$time_stamp) != && date("N",$time_stamp) != 7)
  405.                 $workingDays--;
  406.         }
  407.         return (int)round($workingDays,0);
  408.     }
  409. }