container = $container; $this->em = $em; } protected function configure() { $this ->setName('app:Breakday') ->setDescription('Récupérer les jours non travaillés de schedule') ->setHelp('Récupérer les jours non travaillés de schedule') ->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job') ->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron') ; } protected function execute(InputInterface $input, OutputInterface $output) { $this->output = $output; $this->filesystem = new Filesystem(); $this->rootlog = $this->container->get('kernel')->getLogDir()."/"; $alias = $this->container->getParameter('appAlias'); $this->writelnred(''); $this->writelnred('== app:Breakday'); $this->writelnred('=========================================================================================================='); // S'assurer que l'on doit ou non configurer le proxy $proxyUse = $this->container->getParameter('proxyUse'); $proxyHost = $this->container->getParameter('proxyHost'); $proxyPort = $this->container->getParameter('proxyPort'); $appWeburl= $this->container->getParameter('appWeburl'); $url = $this->container->getParameter('scheduleUrl')."/rest"; $key = $this->container->getParameter('scheduleKey'); if(stripos($url,"/")==0) { $url="https://".$appWeburl.$url; $proxyUse=false; } if(stripos($url,"https://".$appWeburl)===0) $proxyUse=false; if(stripos($url,"http://".$appWeburl)===0) $proxyUse=false; if($proxyUse) { $clientguzzle = new \GuzzleHttp\Client(array('timeout' => 3,'verify'=>false,'proxy' => ['http' => 'http://'.$proxyHost.':'.$proxyPort,'https' => 'http://'.$proxyHost.':'.$proxyPort])); } else $clientguzzle = new \GuzzleHttp\Client(['timeout' => 3,'verify'=>false]); try { $response = $clientguzzle->request('GET', $url); } catch (RequestException $e) { if ($e->hasResponse()) { return 0; } } $users=$this->em->getRepository("App:User")->findAll(); foreach($users as $user) { if(!$user->hasRole("ROLE_USER")) continue; $start=new \Datetime('00:00'); $start->modify("monday this week"); $end=new \Datetime('00:00'); $end->modify("last day of this month"); $end->modify("next sunday"); $response=$this->api("POST",$url."/getBreakdays",json_encode(["key"=>$key,"username"=>$user->getUsername()])); if($response&&$response->code=="200") { while($start<=$end) { $havebreakday=false; foreach($response->body->events as $event) { $manip=clone $start; if($event->eventallday) { $manip->setTime(8,0); if(new \Datetime($event->eventstart)<=$manip && $manip < new \Datetime($event->eventend) ) { $this->writeln($start->format("d/m/Y")." >> tallybreakday full day"); // On s'assure que la journée n'a pas été validé master $tallyday=$this->em->getRepository("App:Tallyday")->findOneBy(["user"=>$user,"dateof"=>$start,"validatemaster"=>true]); if(!$tallyday) { // Les pointages de la journée qui ne sont des congès sont supprimés $tallydays=$this->em->getRepository("App:Tallyday")->findBy(["user"=>$user,"dateof"=>$start,"isbreakday"=>false]); foreach($tallydays as $tallyday) { $this->em->remove($tallyday); $this->em->flush(); } // Les breakday am sont supprimés $manip=clone $start; $manip->setTime(9,0); $tallydays=$this->em->getRepository("App:Tallyday")->findBy(["user"=>$user,"dateof"=>$start,"datestart"=>$manip,"isbreakday"=>true]); foreach($tallydays as $tallyday) { $this->em->remove($tallyday); $this->em->flush(); } // Les breakday pm sont supprimés $manip=clone $start; $manip->setTime(13,30); $tallydays=$this->em->getRepository("App:Tallyday")->findBy(["user"=>$user,"dateof"=>$start,"datestart"=>$manip,"isbreakday"=>true]); foreach($tallydays as $tallyday) { $this->em->remove($tallyday); $this->em->flush(); } // Le breakday est généré s'il n'existe pas $manip=clone $start; $manip->setTime(10,00); $tallyday=$this->em->getRepository("App:Tallyday")->findOneBy(["user"=>$user,"dateof"=>$start,"datestart"=>$manip,"isbreakday"=>true]); if(!$tallyday) { $data = new Tallyday(); $data->setDateof($start); $data->setUser($user); $data->setValidateuser(false); $data->setValidatemaster(false); $data->setIsbreakday(true); $data->setName($event->taskname); $data->setDatestart(clone $manip); $manip->add(new \DateInterval("PT7H")); $data->setDateend(clone $manip); $this->em->persist($data); $this->em->flush(); } $havebreakday=true; } } } else { } } // Si la journée n'a pas de congès on supprime ceux qui peuvent existé non validé if(!$havebreakday) { $tallydays=$this->em->getRepository("App:Tallyday")->findBy(["user"=>$user,"dateof"=>$start,"validatemaster"=>false,"isbreakday"=>true]); foreach($tallydays as $tallyday) { $this->em->remove($tallyday); $this->em->flush(); } } $start->add(new \DateInterval('P1D')); } } } $this->writeln(''); return 1; } private function api($method,$url,$query,$token=null) { // Entete $headers = [ 'Accept' => 'application/json', 'Content-Type' => 'application/json', ]; if($token) $headers["Authorization"]="token ".$token; // Paramétrage unirest \Unirest\Request::verifyPeer(false); \Unirest\Request::verifyHost(false); \Unirest\Request::timeout(5); // Déclaration du proxy $proxyUse = $this->container->getParameter("proxyUse"); if($proxyUse) { $proxyHost = $this->container->getParameter("proxyHost"); $proxyPort = $this->container->getParameter("proxyPort"); \Unirest\Request::proxy($proxyHost, $proxyPort, CURLPROXY_HTTP, true); } $response = false; switch($method) { case "POST": try{ $response = \Unirest\Request::post($url,$headers,$query); } catch (\Exception $e) { return false; } break; case "GET": try{ $response = \Unirest\Request::get($url,$headers,$query); } catch (\Exception $e) { return false; } break; case "DELETE": try{ $response = \Unirest\Request::delete($url,$headers,$query); } catch (\Exception $e) { return false; } break; case "PATCH": try{ $response = \Unirest\Request::patch($url,$headers,$query); } catch (\Exception $e) { return false; } break; } return $response; } private function writelnred($string) { $this->output->writeln(''.$string.''); $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); } private function writeln($string) { $this->output->writeln($string); $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); } }