<?php
namespace App\Controller;
use App\Entity\Posrednik\HPracy;
use App\Entity\Swieta;
use App\Repository\HPracyRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Annotation\Route;
class SymulationController extends AbstractController
{
protected $requestStack;
public function __construct(RequestStack $requestStack= null)
{
$this->requestStack = $requestStack;
}
/**
* @Route("/symulation", name="symulation")
*
*
*/
public function indexAction(Request $request){
$emi = $this->getDoctrine()->getManager();
$repositorySym = $emi->getRepository('App:Symulation');
$repositoryObjectDef = $emi->getRepository('App:ObjectDef');
$repositorySymDef = $emi->getRepository('App:SymulationDef');
$querySym = $repositorySym
->createQueryBuilder('s')
->select('s')
->where('s.active =1 ')
->setMaxResults(10)
->getQuery();
$symulation = $querySym->execute();
$workTime = 0;
foreach ($symulation as $sym) {
$symID = $sym->getId();
$obiekt_id = $sym->getObject()->getId();
if ($symID <>''){
$symDef = $repositorySymDef->findby(array('symulation'=> $symID));
$employment_arr[$symID] = $this->getEmployment( $symDef);
$object_arr[$symID] = $this->getObjectDef($symDef);
$object_hour_arr[$symID] = $this->getObjectPlanWork($this->getObjectDef($symDef));
}
}
$monthCost = $this->getMonthCost($symulation, $object_arr, $object_hour_arr, $employment_arr);
return $this->render('symulation/index.html.twig',[
'object' => $object_arr,
'employer' => $employment_arr,
'symulation' => $symulation,
'monthCost' => $monthCost,
'activityByDay' => $object_hour_arr,
'monthWorkTime' => $workTime,
]);
}
/**
* Konwersja czasu na string
*/
public function getFirstDay($date){
return date("Y-m-d",strtotime($date) );
}
public function getTimetoHour($date){
if($date == null)
return null;
$godziny = $date->format("H");
if($date->format("i") == 59)
$godziny = $godziny +1;
return ($godziny);
}
public function getTimeMinute($date){
if($date == null)
return null;
$godziny = $date->format("i");
if($date->format("i") == 59)
$godziny = 0;
return ($godziny);
}
public function getTimetoMinute($date){
if($date == null)
return 0;
$godziny = $date->format("H");
$minuty = $date->format("i");
return ($godziny*60)+$minuty;
}
public function getTimetoSec($date){
if($date == null)
return 0;
$godziny = $date->format("H");
$minuty = $date->format("i");
return ($godziny*60*60)+($minuty*60);
}
public function getLastDayForOneMonth($month, $rok=2022){
$arr = $this->getLastDayInt($rok.'-'.$month, '-01');
return $arr;
}
public function getLastDayForMonth($month, $rok=2022){
$arr=[];
for ($m=1; $m <= $month; $m++) {
//ToDo rok z konfigu
$arr[$m] = $this->getLastDayInt($rok.'-'.$m, '-01');
}
return $arr;
}
public function getLastDay($date){
return date("Y-m-t",strtotime($date) );
}
public function getLastDayInt($date){
return date("t",strtotime($date) );
}
public function getTimeDiff($timeStart, $timeEnd){
$hourEnd = date_format($timeEnd,'H');
if ($hourEnd =="00"){
$hourEnd = 24;
}
$hourStart = date_format($timeStart,'H');
$h = ($hourEnd - $hourStart)*60;
//$h = (date_diff($timeStart, $timeEnd)->h) * 60;
$m = (date_diff($timeStart, $timeEnd)->i);
return ($h+$m);
}
/**
* Konwersja czasu na string
*/
public function getTime($time){
return $time->format('H:i');
}
/**
* Zwraca plan pracy
*
*/
public function getObjectPlanWork($obj)
{
$idObject = null;
$arr = null;
if(is_array($obj)) {
$idObject = $obj["id"];
$emi = $this->getDoctrine()->getManager();
$repositorySym = $emi->getRepository('App:ObjectDefMonth');
$queryAll = $repositorySym
->createQueryBuilder('o')
->select('o')
->where('o.defId = :id')
->setParameter('id', $idObject)
->getQuery();
$workDay = $queryAll->execute();
$querySum = $repositorySym
->createQueryBuilder('o')
->select('o.defId,
o.year, o.month,
sum(CASE
WHEN (o.hour >=22 or o.hour < 6)
THEN o.employers * o.work_time
ELSE
0
END
) as nightHours,
sum(CASE
WHEN (o.hour <22 and o.hour >= 6)
THEN o.employers * o.work_time
ELSE
0
END
) as dayHours,
sum(CASE
WHEN (o.day_num = \'swieto\')
THEN o.employers * o.work_time
ELSE
0
END
) as hollidayHours,
sum(CASE
WHEN (o.day_num = \'sobota\')
THEN o.employers * o.work_time
ELSE
0
END
) as sobotaHours,
sum(CASE
WHEN (o.day_num = \'niedziela\')
THEN o.employers * o.work_time
ELSE
0
END
) as niedzielaHours
')
->where('o.defId = :id')
->groupBy('o.month, o.year, o.defId')
->setParameter('id', $idObject)
->getQuery();
$sumMonth = $querySum->execute();
$arr = [
"sumHour" => $sumMonth,
"allHour" => $workDay,
];
}
return $arr ;
}
/**
* Zwraca definicje obiektu do symulacji
*
*/
public function getObjectDef($obj)
{
$arr = null;
if (is_array($obj)){
$object = $obj[0]->getSymulation()->getObject();
$arr = [
"id" => $object->getId(),
"name" => $object->getName(),
"hoursMonthly" => $object->getHoursMonth(),
"employerMaxHour" => $object->getEmployerMaxHour(),
"objectId" => $object->getObjectId(),
"hourStart" => $object->getHourStart(),
"hourEnd" => $object->getHourEnd(), //format('H:i');
"hourDiff" => 12,
];
}
return $arr;
}
/**
* Zwraca definicje zatrudnienia do symulacji
*
*/
public function getEmployment($sym)
{
$arr = null;
if (is_array($sym)){
$numAll=0;
foreach ($sym as $s){
$arr[$s->getEmployer()->getId()] = [
"name" => $s->getEmployer()->getName(),
"nameType" => $s->getEmployer()->getNameType(),
"costBase" => $s->getEmployer()->getCostBase(),
"costEmployer" => $s->getEmployer()->getCostEmployer(),
"nightHours" => $s->getEmployer()->getNightHours(),
"overtime" => $s->getEmployer()->getOvertime(),
"norm" => $s->getEmployer()->getNorm(),
"holiday" => $s->getEmployer()->getHoliday(),
"num" => $s->getEmployerNum(),
];
$numAll = $numAll + $s->getEmployerNum();
}
$arr = [
$arr,
"numAll" => $numAll,
];
}
return $arr;
}
public function getDayNum($date, $em){
if($date == null)
return null;
if($em == null)
$em = $this->getDoctrine()->getManager();
$day_num = 'normalny';
$w = $date->format("w");
if ($w == 6)
$day_num = 'sobota';
if ($w == 0)
$day_num = 'niedziela';
if(in_array($date->format("Y-m-d"), $this->getHollidays(2022,null, $em), true)){
$day_num = 'swieto';
}
return $day_num;
}
public function getHollidays($year=2022, $month = null, $em){
//ToDo zapytanie zmienić by uwzględniało rok i miesiąc
$holidays = array();
if($em == null)
$em = $this->getDoctrine()->getManager();
$holidays_ = $em->getRepository(Swieta::class)->findAll();
foreach ($holidays_ as $h){
$holidays[$h->getData()->format("Y-m-d")] = $h->getData()->format("Y-m-d");
}
return $holidays;
}
public function getMonthCost($symulation, $object, $object_hour, $employment, $year=2022 ){
$arr= null;
$em_posrednik = $this->getDoctrine()->getManager('posrednik');
$em = $this->getDoctrine()->getManager();
$holidays = $this->getHollidays($year, null, $em);
if($symulation) {
$objetID = $symulation[0]->getObject()->getObjectId()->getId();
$hpracyRepository= $em_posrednik->getRepository(HPracy::class);
}
// generowanie symulacji dla poszczególnych obiektów
foreach ($symulation as $sym) {
$symId =$sym->getId();
if ($symId <>''){
$a=0;
$month_arr = $object_hour[$symId]['sumHour'];
//generowanie symulacji dla poszczególnych miesiecy zdefiniowanych dla obiektu
while ($a < count($month_arr) ) {
$month = $month_arr[$a]["month"];
$a_date = $year.'-'.$month.'-01';
$firstDay = $this->getFirstDay($a_date);
$lastDay = $this->getLastDay($a_date);
$year_1 = $year - 1;
$a_date_1 = $year_1.'-'.$month.'-01';
$a_date_1 = $year_1.'-'.$month.'-01';
$firstDay_1 = $this->getFirstDay($a_date_1);
$lastDay_1 = $this->getLastDay($a_date_1);
$lastDay_ = $this->getLastDayInt($a_date);
$workingDay = $this->getWorkingDays($firstDay, $lastDay, $holidays);
$object_ = $object[$symId];
$objectHour = $month_arr[$a]["dayHours"]+ $month_arr[$a]["nightHours"];
$objectHourNight = $month_arr[$a]["nightHours"];
$objectHourHolliday = $month_arr[$a]["hollidayHours"];
$objectHourSobota = $month_arr[$a]["sobotaHours"];
$objectHourNiedziela = $month_arr[$a]["niedzielaHours"];
$objectHourDiff = $object_['hourDiff'];
$hpracyResult = $hpracyRepository
->createQueryBuilder('h')
->select('h.rodzaj, sum(h.czasMinuty/60/60) as czas')
->where('h.obiektyId = :obiekt')
->andWhere('h.dzien between :firstday AND :lastday ')
->andWhere('h.rodzaj <> \'NZ\' ' )
->setParameter('obiekt', $objetID)
->setParameter('firstday', $firstDay)
->setParameter('lastday', $lastDay)
->groupBy('h.rodzaj')
->getQuery()
->getScalarResult()
;
$hpracyResult_1 = $hpracyRepository
->createQueryBuilder('h')
->select('h.rodzaj, sum(h.czasMinuty/60/60) as czas')
->where('h.obiektyId = :obiekt')
->andWhere('h.dzien between :firstday AND :lastday ')
->andWhere('h.rodzaj <> \'NZ\' ' )
->setParameter('obiekt', $objetID)
->setParameter('firstday', $firstDay_1)
->setParameter('lastday', $lastDay_1)
->groupBy('h.rodzaj')
->getQuery()
->getScalarResult()
;
$arr[$symId][$month]= [
"firstDay"=> $firstDay,
"lastDay"=> $lastDay,
"dniRoboczych" => $workingDay,
"objectHour" => $objectHour,
"objectHourNight" => $objectHourNight,
"objectHourHolliday" => $objectHourHolliday,
"objectHourSoboty" => $objectHourSobota,
"objectHourNiedziele" => $objectHourNiedziela,
"month" => $month,
"idSym" => $symId,
"hp" => $hpracyResult,
"hp_1" => $hpracyResult_1,
];
$a++;
}
}
}
return $arr;
}
/**
* @Route("/activity", name="activity")
*
*
*/
public function activityAction(Request $request){
return $this->render('symulation/modal.html.twig',[
"day" => $request->query->get('id'),
"request"=> $request
]);
}
public function getMonthWorkTime($objectID, $month, $year){
$sqlDay = "SELECT day, sum(employers*work_time) FROM `object_def_month` where month=".$month." and year=".$year." and def_id = ".$objectID." group by day";
dump($sqlDay);
}
public function getWorkingDays($startDate,$endDate,$holidays){
$endDate = strtotime($endDate);
$startDate = strtotime($startDate);
$days = ($endDate - $startDate) / 86400 + 1;
$no_full_weeks = floor($days / 7);
$no_remaining_days = fmod($days, 7);
$the_first_day_of_week = date("N", $startDate);
$the_last_day_of_week = date("N", $endDate);
if ($the_first_day_of_week <= $the_last_day_of_week) {
if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
}
else {
if ($the_first_day_of_week == 7) {
$no_remaining_days--;
if ($the_last_day_of_week == 6) {
$no_remaining_days--;
}
}
else {
$no_remaining_days -= 2;
}
}
$workingDays = $no_full_weeks * 5;
if ($no_remaining_days > 0 )
{
$workingDays += $no_remaining_days;
}
foreach($holidays as $holiday){
$time_stamp=strtotime($holiday);
if ($startDate <= $time_stamp && $time_stamp <= $endDate && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
$workingDays--;
}
return (int)round($workingDays,0);
}
}