<?php
namespace App\Controller\HR;
use App\Entity\HR\WageSimulation;
use App\Entity\HR\WageSimulationInput;
use App\Entity\HR\Employee;
use App\Entity\Kernel\ListItem;
use App\Form\HR\WageSimulationType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Service\Kernel\DMSService;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Service\Kernel\KernelService;
/**
* @Route("/{_locale}/wagesimulation")
*/
class WageSimulationController extends AbstractController
{
/**
* @Route("/", name="wage_simulation_index", methods={"GET"})
*/
public function index(): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$wageSimulations = $this->getDoctrine()
->getRepository(WageSimulation::class)
->findBy(array('state' => 0));
return $this->render('hr/wage_simulation/index.html.twig', [
'wage_simulations' => $wageSimulations,
]);
}
/**
* @Route("/table/{state}/{id}", name="wage_simulation_table", methods={"GET"})
*/
public function table(int $state,int $id = 0): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$organization =$this->getUser()->getOrganization();
$em = $this->getDoctrine()->getManager();
if ($id == 0)
{
$wageSimulations = $em->createQuery("SELECT s from App\Entity\HR\WageSimulation s WHERE ((s.state = ?1) AND ((s.organization = ?2) or (?2 is null)))")
->setParameter(1, $state)
->setParameter(2,$organization)
->getResult();
$employee = null;
}
else
{
$employee = $this->getDoctrine()
->getRepository(Employee::class)
->findOneBy(array('id' => $id));
$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)))")
->setParameter(1, $state)
->setParameter(2,$organization)
->setParameter(3,$employee)
->getResult();
}
return $this->render('hr/wage_simulation/table.html.twig', [
'state' => $state,
'wage_simulations' => $wageSimulations,
'employee' => $employee,
'employee_id' => $id,
]);
}
/**
* @Route("/new/{id}", name="wage_simulation_new", methods={"GET","POST"})
*/
public function new(Request $request,int $id = 0, KernelService $kernel_service, TranslatorInterface $translator, DMSService $dmsservice): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute);
$wageSimulation = new WageSimulation();
$wageSimulation->setRepresentationallowance(floatval($kernel_service->getApplicationInfoValue('HR.DefaultRepresentationAllowance')));
$wageSimulation->setMargin(floatval($kernel_service->getApplicationInfoValue('HR.DefaultMargin')));
$wageSimulation->setAdv(true);
$wageSimulation->setHospitalisation(true);
$wageSimulation->setWorkregime($kernel_service->getListItem("HR.Workregime", "Workregime.40h"));
$employees = array();
$employee = "";
if ($id != 0)
{
$employee = $this->getDoctrine()
->getRepository(Employee::class)
->findOneBy(array('id' => $id));
$employees["".$employee] = "SU".$employee->getId();
$employee = "SU".$employee->getId();
}
$form = $this->createForm(WageSimulationType::class, $wageSimulation,
['employees' => $employees, "employee" => $employee, 'action' => $this->generateUrl('wage_simulation_new'),
'attr' => ['id' => $this->generateUrl('wage_simulation_new')]]);
if ($this->getUser()->getOrganization() == null) {
$form->add('organization', null, array('label' => 'Organization',
'query_builder' => function ($repository) {
return $repository->createQueryBuilder('s')->where("s.state = 0");
},
'attr' => ['class' => 'select2']));
}
$form->handleRequest($request);
$continue = true;
$requestdata = $request->request->all("wage_simulation");
if ($form->isSubmitted() && $form->isValid()) {
$maxrepresentationallowance = $kernel_service->getApplicationInfoValue('HR.MaxRepresentationAllowance');
$minmargin = $kernel_service->getApplicationInfoValue('HR.MinMargin');
if (($wageSimulation->getRepresentationallowance() != null) && ($wageSimulation->getRepresentationallowance() > $maxrepresentationallowance))
{
$continue = false;
$this->get('session')->getFlashBag()->add('error', $translator->trans("WageSimulation.Representationallowancetohigh"));
}
if (($wageSimulation->getMargin() != null) && ($wageSimulation->getMargin() < $minmargin))
{
$continue = false;
$this->get('session')->getFlashBag()->add('error', $translator->trans("WageSimulation.Margintolow"));
}
if (isset($requestdata['employeeselect2']) != true) {
$continue = false;
$this->get('session')->getFlashBag()->add('error', $translator->trans("WageSimulation.EmployeeIsMandatory"));
}
}
if ($form->isSubmitted() && $continue == true) {
$entityManager = $this->getDoctrine()->getManager();
$wageSimulation->setState(0);
$wageSimulation->setHomedesk(true);
if ($this->getUser()->getOrganization() != null) {
$wageSimulation->setOrganization($this->getUser()->getOrganization());
}
$calcresult = $this->docalc($wageSimulation->getGrosssalary()->getId(), $wageSimulation->getKids(), $wageSimulation->getRepresentationallowance(), $wageSimulation->getMargin(),
$wageSimulation->getCar(), $wageSimulation->getFuel(), $wageSimulation->getTrain(), $wageSimulation->getHomedesk(), $kernel_service);
$wageSimulation->setInternalcost($calcresult["internalcost"]);
$wageSimulation->setSalesprice($calcresult["salesprice"]);
$wageSimulation->setNetwage($calcresult["netwage"]);
$wageSimulation->setTotalnetwage($calcresult["totalnetwage"]);
$wageSimulation->setHomedeskallowance($calcresult["homedeskallowance"]);
$wageSimulation->setHomedesk(true);
if (isset($requestdata['employeeselect2']) == true) {
$employeeselect2 = $requestdata['employeeselect2'];
$employee = null;
$employeetype = substr($employeeselect2, 0, 2);
$employeeid = substr($employeeselect2, 2);
if ($employeetype == "SU") {
$wageSimulation
->setEmployee(
$entityManager->getRepository(Employee::class)
->findOneById($employeeid));
}
}
$entityManager->persist($wageSimulation);
$entityManager->flush();
$applicationfolder = $dmsservice->createFolderIfNeeded("System");
$wagesimulationfolder = $dmsservice->createFolderIfNeeded("WageSimulation", $applicationfolder);
//$folder = $dmsservice->createFolderIfNeeded("".$wageSimulation->getId(), $wagesimulationfolder);
$filename = "Loonsvoorstel - ";
if ($wageSimulation->getLanguage()->getName() == "fr-FR")
{
$filename = "Proposition de Salaire - ";
}
$filename = $filename . $wageSimulation->getEmployee() . " ";
$filename = $filename . date("d/m/Y");
$pdf = $dmsservice->createPDF(
$filename,
$wagesimulationfolder,
"TemplateType.WageSimulation",
$wageSimulation->getLanguage(),
$wageSimulation,
null,
$wageSimulation->getPdf(),
$wageSimulation->getOrganization()
);
$wageSimulation->setPdf($pdf);
$entityManager->persist($wageSimulation);
$entityManager->flush();
$this->denyAccessUnlessGranted($attribute, $wageSimulation);
return $this->redirectToRoute('wage_simulation_show', array('id' => $wageSimulation->getId()));
}
return $this->render('hr/wage_simulation/new.html.twig', [
'wage_simulation' => $wageSimulation,
'form' => $form->createView(),
]);
}
public function docalc($bruto, $kids, $representation, $margin, $car, $fuel, $train, $homedesk, KernelService $kernel_service)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository(WageSimulationInput::class)->findOneById($bruto);
$kids = $em->getRepository(ListItem::class)->findOneById($kids)->getName();
$netto = 0;
$totalnetto = 0;
if($kids == "0")
{
$netto = $entity->getNetwage();
}
else
{
if($kids == "1")
{
$netto = $entity->getNetwage1child();
}
else
{
if($kids == "2")
{
$netto = $entity->getNetwage2childs();
}
else
{
$netto = $entity->getNetwage3childs();
}
}
}
$totalnetto = $netto;
$internalcost = 0;
$homedeskallowance = 0;
$internalcost = $internalcost + $entity->getInternalcost();
if($car == true)
{
$internalcost = $internalcost + floatval($kernel_service->getApplicationInfoValue("HR.CarCost"));
}
if($fuel == true)
{
$internalcost = $internalcost + floatval($kernel_service->getApplicationInfoValue("HR.FuelCost"));
}
if($train == true)
{
$internalcost = $internalcost + floatval($kernel_service->getApplicationInfoValue("HR.TrainCost"));
}
if($homedesk == true)
{
$homedeskallowance = floatval($kernel_service->getApplicationInfoValue("HR.CompensationHomeOffice"));
$totalnetto = $totalnetto + $homedeskallowance;
}
if($representation != null)
{
$totalnetto = $totalnetto + $representation;
$internalcost = $internalcost + intval($representation)*12/208;
}
$internalcost = round($internalcost);
$salesprice = $internalcost + $margin;
return array("internalcost" => $internalcost , "salesprice" => $salesprice, "netwage" => $netto, "totalnetwage" => $totalnetto, "homedeskallowance" => $homedeskallowance);
}
/**
* @Route("/calc", name="wage_simulation_calc", methods={"POST"})
*/
public function calc(Request $request, KernelService $kernel_service): Response
{
$bruto = $request->get('bruto');
$kids = $request->get('kids');
$representation = $request->get('representation');
$margin = $request->get('margin');
$car = $request->get('car');
if ($car == "true") {
$car = true;
} else {
$car = false;
}
$fuel = $request->get('fuel');
if ($fuel == "true") {
$fuel = true;
} else {
$fuel = false;
}
$train = $request->get('train');
if ($train == "true") {
$train = true;
} else {
$train = false;
}
$homedesk = true;
$return = $this->docalc($bruto, $kids, $representation, $margin, $car, $fuel, $train, $homedesk, $kernel_service);
$return = json_encode($return);
return new Response($return, 200, array('Content-Type' => 'application/json'));
}
/**
* @Route("/{id}", name="wage_simulation_show", methods={"GET"})
*/
public function show(WageSimulation $wageSimulation): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $wageSimulation);
return $this->render('hr/wage_simulation/show.html.twig', [
'wage_simulation' => $wageSimulation,
]);
}
/**
* @Route("/{id}/audit", name="wage_simulation_showaudit", methods={"GET"})
*/
public function showaudit(WageSimulation $wageSimulation): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $wageSimulation);
return $this->render('hr/wage_simulation/showaudit.html.twig', [
'wage_simulation' => $wageSimulation,
]);
}
/**
* @Route("/{id}/archive", name="wage_simulation_archive", methods={"GET"})
*/
public function archive(WageSimulation $wageSimulation): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $wageSimulation);
$entityManager = $this->getDoctrine()->getManager();
$wageSimulation->setState(1);
$entityManager->persist($wageSimulation);
$entityManager->flush();
return $this->redirectToRoute('wage_simulation_show', array('id' => $wageSimulation->getId()));
}
/**
* @Route("/{id}/restore", name="wage_simulation_restore", methods={"GET"})
*/
public function restore(WageSimulation $wageSimulation): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $wageSimulation);
$entityManager = $this->getDoctrine()->getManager();
$wageSimulation->setState(0);
$entityManager->persist($wageSimulation);
$entityManager->flush();
return $this->redirectToRoute('wage_simulation_show', array('id' => $wageSimulation->getId()));
}
/**
* @Route("/{id}/delete", name="wage_simulation_logicaldelete", methods={"GET"})
*/
public function logicaldelete(WageSimulation $wageSimulation): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $wageSimulation);
$entityManager = $this->getDoctrine()->getManager();
$wageSimulation->setState(2);
$entityManager->persist($wageSimulation);
$entityManager->flush();
return $this->redirectToRoute('wage_simulation_index');
}
/**
* @Route("/{id}/edit", name="wage_simulation_edit", methods={"GET","POST"})
*/
public function edit(Request $request, WageSimulation $wageSimulation): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $wageSimulation);
$form = $this->createForm(WageSimulationType::class, $wageSimulation,
['action' => $this->generateUrl('wage_simulation_edit', array('id' => $wageSimulation->getId())), 'attr' => ['id' => $this->generateUrl('wage_simulation_edit', array('id' => $wageSimulation->getId()))]]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('wage_simulation_show', array('id' => $wageSimulation->getId()));
}
return $this->render('hr/wage_simulation/edit.html.twig', [
'wage_simulation' => $wageSimulation,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="wage_simulation_delete", methods={"DELETE"})
*/
public function delete(Request $request, WageSimulation $wageSimulation): Response
{
$classname = explode("\\", get_class($this));
$arraysize = count($classname);
$attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
$this->denyAccessUnlessGranted($attribute, $wageSimulation);
if ($this->isCsrfTokenValid('delete'.$wageSimulation->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($wageSimulation);
$entityManager->flush();
}
return $this->redirectToRoute('wage_simulation_index');
}
}