src/Controller/HR/WageSimulationController.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Controller\HR;
  3. use App\Entity\HR\WageSimulation;
  4. use App\Entity\HR\WageSimulationInput;
  5. use App\Entity\HR\Employee;
  6. use App\Entity\Kernel\ListItem;
  7. use App\Form\HR\WageSimulationType;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use App\Service\Kernel\DMSService;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use App\Service\Kernel\KernelService;
  15. /**
  16.  * @Route("/{_locale}/wagesimulation")
  17.  */
  18. class WageSimulationController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/", name="wage_simulation_index", methods={"GET"})
  22.      */
  23.     public function index(): Response
  24.     {
  25.         $classname =  explode("\\", get_class($this));
  26.         $arraysize = count($classname);
  27.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  28.         $this->denyAccessUnlessGranted($attribute);
  29.     
  30.         $wageSimulations = $this->getDoctrine()
  31.             ->getRepository(WageSimulation::class)
  32.             ->findBy(array('state' => 0));
  33.         return $this->render('hr/wage_simulation/index.html.twig', [
  34.             'wage_simulations' => $wageSimulations,
  35.         ]);
  36.     }
  37.     
  38.     /**
  39.      * @Route("/table/{state}/{id}", name="wage_simulation_table", methods={"GET"})
  40.      */
  41.     public function table(int $state,int $id = 0): Response
  42.     {
  43.         $classname =  explode("\\", get_class($this));
  44.         $arraysize = count($classname);
  45.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  46.         $this->denyAccessUnlessGranted($attribute);
  47.                 
  48.         $organization =$this->getUser()->getOrganization();
  49.         $em = $this->getDoctrine()->getManager();
  50.         
  51.         if ($id == 0)
  52.         {
  53.             $wageSimulations = $em->createQuery("SELECT s from App\Entity\HR\WageSimulation s WHERE ((s.state = ?1) AND ((s.organization = ?2) or (?2 is null)))")
  54.                     ->setParameter(1, $state)
  55.                     ->setParameter(2,$organization)
  56.                     ->getResult();
  57.             $employee = null;
  58.         }
  59.         else
  60.         {
  61.             $employee = $this->getDoctrine()
  62.                 ->getRepository(Employee::class)
  63.                 ->findOneBy(array('id' => $id));
  64.             $wageSimulations = $em->createQuery("SELECT s from App\Entity\HR\WageSimulation s WHERE (((s.state = ?1) AND (s.employee = ?3)) AND ((s.organization = ?2) or (?2 is null)))")
  65.                     ->setParameter(1, $state)
  66.                     ->setParameter(2,$organization)
  67.                     ->setParameter(3,$employee)
  68.                     ->getResult();
  69.         }
  70.         return $this->render('hr/wage_simulation/table.html.twig', [
  71.             'state' => $state,
  72.             'wage_simulations' => $wageSimulations,
  73.             'employee' => $employee,
  74.             'employee_id' => $id,
  75.         ]);
  76.     }
  77.     /**
  78.      * @Route("/new/{id}", name="wage_simulation_new", methods={"GET","POST"})
  79.      */
  80.     public function new(Request $request,int $id = 0, KernelService $kernel_service, TranslatorInterface $translator, DMSService $dmsservice): Response
  81.     {
  82.         $classname =  explode("\\", get_class($this));
  83.         $arraysize = count($classname);
  84.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  85.         $this->denyAccessUnlessGranted($attribute);
  86.     
  87.         $wageSimulation = new WageSimulation();
  88.         $wageSimulation->setRepresentationallowance(floatval($kernel_service->getApplicationInfoValue('HR.DefaultRepresentationAllowance')));
  89.         $wageSimulation->setMargin(floatval($kernel_service->getApplicationInfoValue('HR.DefaultMargin')));
  90.         $wageSimulation->setAdv(true);
  91.         $wageSimulation->setHospitalisation(true);
  92.         $wageSimulation->setWorkregime($kernel_service->getListItem("HR.Workregime", "Workregime.40h"));
  93.         
  94.         $employees = array();
  95.         $employee = "";
  96.             
  97.         if ($id != 0)
  98.         {
  99.             $employee = $this->getDoctrine()
  100.                 ->getRepository(Employee::class)
  101.                 ->findOneBy(array('id' => $id));
  102.             
  103.             $employees["".$employee] = "SU".$employee->getId();
  104.             $employee = "SU".$employee->getId();
  105.         }
  106.         
  107.         $form = $this->createForm(WageSimulationType::class, $wageSimulation, 
  108.                 ['employees' => $employees, "employee" => $employee, 'action' => $this->generateUrl('wage_simulation_new'), 
  109.                     'attr' => ['id' => $this->generateUrl('wage_simulation_new')]]);
  110.         if ($this->getUser()->getOrganization() == null) {
  111.             $form->add('organization', null, array('label' => 'Organization',
  112.                 'query_builder' => function ($repository) {
  113.                     return $repository->createQueryBuilder('s')->where("s.state = 0");
  114.                 },
  115.                 'attr' => ['class' => 'select2']));
  116.         }
  117.         $form->handleRequest($request);
  118.         
  119.         $continue = true;
  120.         $requestdata = $request->request->all("wage_simulation");
  121.         
  122.         if ($form->isSubmitted() && $form->isValid()) {
  123.             
  124.             $maxrepresentationallowance = $kernel_service->getApplicationInfoValue('HR.MaxRepresentationAllowance');
  125.             $minmargin = $kernel_service->getApplicationInfoValue('HR.MinMargin');
  126.             
  127.             if (($wageSimulation->getRepresentationallowance() != null) && ($wageSimulation->getRepresentationallowance() > $maxrepresentationallowance))
  128.             {
  129.                 $continue = false;
  130.                 $this->get('session')->getFlashBag()->add('error', $translator->trans("WageSimulation.Representationallowancetohigh"));
  131.             }
  132.             
  133.             if (($wageSimulation->getMargin() != null) && ($wageSimulation->getMargin() < $minmargin))
  134.             {
  135.                 $continue = false;
  136.                 $this->get('session')->getFlashBag()->add('error', $translator->trans("WageSimulation.Margintolow"));
  137.             }
  138.             
  139.             if (isset($requestdata['employeeselect2']) != true) {
  140.                 $continue = false;
  141.                 $this->get('session')->getFlashBag()->add('error', $translator->trans("WageSimulation.EmployeeIsMandatory"));
  142.             }
  143.         }
  144.         if ($form->isSubmitted() && $continue == true) {
  145.             $entityManager = $this->getDoctrine()->getManager();
  146.             $wageSimulation->setState(0);
  147.             $wageSimulation->setHomedesk(true);
  148.             if ($this->getUser()->getOrganization() != null) {
  149.                 $wageSimulation->setOrganization($this->getUser()->getOrganization());
  150.             }
  151.             $calcresult = $this->docalc($wageSimulation->getGrosssalary()->getId(), $wageSimulation->getKids(), $wageSimulation->getRepresentationallowance(), $wageSimulation->getMargin(),
  152.                     $wageSimulation->getCar(), $wageSimulation->getFuel(), $wageSimulation->getTrain(), $wageSimulation->getHomedesk(), $kernel_service);
  153.             $wageSimulation->setInternalcost($calcresult["internalcost"]);
  154.             $wageSimulation->setSalesprice($calcresult["salesprice"]);
  155.             $wageSimulation->setNetwage($calcresult["netwage"]);
  156.             $wageSimulation->setTotalnetwage($calcresult["totalnetwage"]);
  157.             $wageSimulation->setHomedeskallowance($calcresult["homedeskallowance"]);
  158.             $wageSimulation->setHomedesk(true);
  159.             
  160.             if (isset($requestdata['employeeselect2']) == true) {
  161.                 $employeeselect2 = $requestdata['employeeselect2'];
  162.                 $employee = null;
  163.                 $employeetype = substr($employeeselect2, 0, 2);
  164.                 $employeeid = substr($employeeselect2, 2);
  165.                 if ($employeetype == "SU") {
  166.                     
  167.                     
  168.                     $wageSimulation
  169.                             ->setEmployee(
  170.                                $entityManager->getRepository(Employee::class)
  171.                                     ->findOneById($employeeid));
  172.                 }
  173.             }
  174.             
  175.             $entityManager->persist($wageSimulation);
  176.             $entityManager->flush();
  177.             
  178.             $applicationfolder = $dmsservice->createFolderIfNeeded("System");
  179.             $wagesimulationfolder = $dmsservice->createFolderIfNeeded("WageSimulation", $applicationfolder);
  180.             //$folder = $dmsservice->createFolderIfNeeded("".$wageSimulation->getId(), $wagesimulationfolder);
  181.             
  182.             $filename = "Loonsvoorstel - ";
  183.             if ($wageSimulation->getLanguage()->getName() == "fr-FR")
  184.             {
  185.                     $filename = "Proposition de Salaire - ";
  186.             }
  187.             $filename = $filename . $wageSimulation->getEmployee() . " ";
  188.             $filename = $filename . date("d/m/Y");
  189.             
  190.             $pdf = $dmsservice->createPDF(
  191.                 $filename,
  192.                 $wagesimulationfolder,
  193.                 "TemplateType.WageSimulation",
  194.                 $wageSimulation->getLanguage(),
  195.                 $wageSimulation,
  196.                 null,
  197.                 $wageSimulation->getPdf(),
  198.                 $wageSimulation->getOrganization()
  199.             );
  200.             $wageSimulation->setPdf($pdf);
  201.             $entityManager->persist($wageSimulation);
  202.             $entityManager->flush();
  203.             $this->denyAccessUnlessGranted($attribute, $wageSimulation);
  204.             return $this->redirectToRoute('wage_simulation_show', array('id' => $wageSimulation->getId()));
  205.         }
  206.         return $this->render('hr/wage_simulation/new.html.twig', [
  207.             'wage_simulation' => $wageSimulation,
  208.             'form' => $form->createView(),
  209.         ]);
  210.     }
  211.     
  212.      public function docalc($bruto, $kids, $representation, $margin, $car, $fuel, $train, $homedesk, KernelService $kernel_service)
  213.     {
  214.         $em = $this->getDoctrine()->getManager();
  215.         
  216.         $entity = $em->getRepository(WageSimulationInput::class)->findOneById($bruto);
  217.         $kids = $em->getRepository(ListItem::class)->findOneById($kids)->getName();
  218.         $netto = 0;
  219.         $totalnetto = 0;
  220.         if($kids == "0")
  221.         {
  222.             $netto = $entity->getNetwage();
  223.         }
  224.         else
  225.         {
  226.             if($kids == "1")
  227.             {
  228.                 $netto = $entity->getNetwage1child();
  229.             }
  230.             else
  231.             {
  232.                 if($kids == "2")
  233.                 {
  234.                     $netto = $entity->getNetwage2childs();
  235.                 }
  236.                 else
  237.                 {
  238.                     $netto = $entity->getNetwage3childs();
  239.                 }
  240.             }
  241.         }
  242.         $totalnetto = $netto;
  243.         
  244.         $internalcost = 0;
  245.         $homedeskallowance = 0;
  246.         $internalcost = $internalcost + $entity->getInternalcost();
  247.         if($car == true)
  248.         {
  249.             $internalcost = $internalcost + floatval($kernel_service->getApplicationInfoValue("HR.CarCost"));
  250.         }
  251.             
  252.         if($fuel == true)
  253.         {
  254.             $internalcost = $internalcost + floatval($kernel_service->getApplicationInfoValue("HR.FuelCost"));
  255.         }
  256.             
  257.         if($train == true)
  258.         {
  259.             $internalcost = $internalcost + floatval($kernel_service->getApplicationInfoValue("HR.TrainCost"));
  260.         }
  261.         
  262.         if($homedesk == true)
  263.         {
  264.             
  265.             $homedeskallowance = floatval($kernel_service->getApplicationInfoValue("HR.CompensationHomeOffice"));
  266.             $totalnetto = $totalnetto + $homedeskallowance;
  267.         }
  268.         
  269.         if($representation != null)
  270.         {
  271.             $totalnetto = $totalnetto + $representation;
  272.             $internalcost = $internalcost + intval($representation)*12/208;
  273.         }
  274.             
  275.            $internalcost = round($internalcost);
  276.           $salesprice = $internalcost + $margin;
  277.         
  278.         return array("internalcost" => $internalcost , "salesprice" => $salesprice, "netwage" => $netto, "totalnetwage" => $totalnetto, "homedeskallowance" => $homedeskallowance);
  279.     }
  280.     
  281.     /**
  282.      * @Route("/calc", name="wage_simulation_calc", methods={"POST"})
  283.      */
  284.     public function calc(Request $request, KernelService $kernel_service): Response
  285.     {
  286.         
  287.     
  288.         $bruto = $request->get('bruto');
  289.         $kids = $request->get('kids');
  290.         $representation = $request->get('representation');
  291.         $margin = $request->get('margin');
  292.         $car = $request->get('car');
  293.         if ($car == "true") {
  294.             $car = true;
  295.         } else {
  296.             $car = false;
  297.         }
  298.         $fuel = $request->get('fuel');
  299.         if ($fuel == "true") {
  300.             $fuel = true;
  301.         } else {
  302.             $fuel = false;
  303.         }
  304.         $train = $request->get('train');
  305.         if ($train == "true") {
  306.             $train = true;
  307.         } else {
  308.             $train = false;
  309.         }
  310.         $homedesk = true;
  311.         
  312.         $return = $this->docalc($bruto, $kids, $representation, $margin, $car, $fuel, $train, $homedesk, $kernel_service);
  313.         $return = json_encode($return);
  314.         return new Response($return, 200, array('Content-Type' => 'application/json'));
  315.     }
  316.     /**
  317.      * @Route("/{id}", name="wage_simulation_show", methods={"GET"})
  318.      */
  319.     public function show(WageSimulation $wageSimulation): Response
  320.     {
  321.         $classname =  explode("\\", get_class($this));
  322.         $arraysize = count($classname);
  323.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  324.         $this->denyAccessUnlessGranted($attribute, $wageSimulation);  
  325.         
  326.         return $this->render('hr/wage_simulation/show.html.twig', [
  327.             'wage_simulation' => $wageSimulation,
  328.         ]);
  329.     }
  330.     
  331.     /**
  332.      * @Route("/{id}/audit", name="wage_simulation_showaudit", methods={"GET"})
  333.      */
  334.     public function showaudit(WageSimulation $wageSimulation): Response
  335.     {
  336.         $classname =  explode("\\", get_class($this));
  337.         $arraysize = count($classname);
  338.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  339.         $this->denyAccessUnlessGranted($attribute, $wageSimulation);  
  340.         
  341.         return $this->render('hr/wage_simulation/showaudit.html.twig', [
  342.             'wage_simulation' => $wageSimulation,
  343.         ]);
  344.     }
  345.     
  346.     /**
  347.      * @Route("/{id}/archive", name="wage_simulation_archive", methods={"GET"})
  348.      */
  349.     public function archive(WageSimulation $wageSimulation): Response
  350.     {
  351.         $classname =  explode("\\", get_class($this));
  352.         $arraysize = count($classname);
  353.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  354.         $this->denyAccessUnlessGranted($attribute, $wageSimulation); 
  355.         
  356.                 
  357.         $entityManager = $this->getDoctrine()->getManager();
  358.         $wageSimulation->setState(1);
  359.         $entityManager->persist($wageSimulation);
  360.         $entityManager->flush();
  361.         
  362.         return $this->redirectToRoute('wage_simulation_show', array('id' => $wageSimulation->getId()));
  363.     }
  364.     
  365.     /**
  366.      * @Route("/{id}/restore", name="wage_simulation_restore", methods={"GET"})
  367.      */
  368.     public function restore(WageSimulation $wageSimulation): Response
  369.     {
  370.         $classname =  explode("\\", get_class($this));
  371.         $arraysize = count($classname);
  372.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  373.         $this->denyAccessUnlessGranted($attribute, $wageSimulation); 
  374.         
  375.                 
  376.         $entityManager = $this->getDoctrine()->getManager();
  377.         $wageSimulation->setState(0);
  378.         $entityManager->persist($wageSimulation);
  379.         $entityManager->flush();
  380.         
  381.         return $this->redirectToRoute('wage_simulation_show', array('id' => $wageSimulation->getId()));
  382.     }
  383.     
  384.     /**
  385.      * @Route("/{id}/delete", name="wage_simulation_logicaldelete", methods={"GET"})
  386.      */
  387.     public function logicaldelete(WageSimulation $wageSimulation): Response
  388.     {
  389.         $classname =  explode("\\", get_class($this));
  390.         $arraysize = count($classname);
  391.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  392.         $this->denyAccessUnlessGranted($attribute, $wageSimulation); 
  393.         
  394.                 
  395.         $entityManager = $this->getDoctrine()->getManager();
  396.         $wageSimulation->setState(2);
  397.         $entityManager->persist($wageSimulation);
  398.         $entityManager->flush();
  399.         
  400.         return $this->redirectToRoute('wage_simulation_index');
  401.     }
  402.     /**
  403.      * @Route("/{id}/edit", name="wage_simulation_edit", methods={"GET","POST"})
  404.      */
  405.     public function edit(Request $request, WageSimulation $wageSimulation): Response
  406.     {
  407.         $classname =  explode("\\", get_class($this));
  408.         $arraysize = count($classname);
  409.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  410.         $this->denyAccessUnlessGranted($attribute, $wageSimulation); 
  411.     
  412.         $form = $this->createForm(WageSimulationType::class, $wageSimulation, 
  413.                 ['action' => $this->generateUrl('wage_simulation_edit', array('id' => $wageSimulation->getId())), 'attr' => ['id' => $this->generateUrl('wage_simulation_edit', array('id' => $wageSimulation->getId()))]]);
  414.         $form->handleRequest($request);
  415.         if ($form->isSubmitted() && $form->isValid()) {
  416.             $this->getDoctrine()->getManager()->flush();
  417.             return $this->redirectToRoute('wage_simulation_show', array('id' => $wageSimulation->getId()));
  418.         }
  419.         return $this->render('hr/wage_simulation/edit.html.twig', [
  420.             'wage_simulation' => $wageSimulation,
  421.             'form' => $form->createView(),
  422.         ]);
  423.     }
  424.     /**
  425.      * @Route("/{id}", name="wage_simulation_delete", methods={"DELETE"})
  426.      */
  427.     public function delete(Request $request, WageSimulation $wageSimulation): Response
  428.     {
  429.         $classname =  explode("\\", get_class($this));
  430.         $arraysize = count($classname);
  431.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  432.         $this->denyAccessUnlessGranted($attribute, $wageSimulation); 
  433.     
  434.         if ($this->isCsrfTokenValid('delete'.$wageSimulation->getId(), $request->request->get('_token'))) {
  435.             $entityManager = $this->getDoctrine()->getManager();
  436.             $entityManager->remove($wageSimulation);
  437.             $entityManager->flush();
  438.         }
  439.         return $this->redirectToRoute('wage_simulation_index');
  440.     }
  441. }