src/Controller/Finance/LeadController.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Finance;
  3. use App\Entity\Finance\Lead;
  4. use App\Form\Finance\LeadType;
  5. use App\Entity\Finance\Proposal;
  6. use App\Form\Finance\ProposalType;
  7. use DateTime;
  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 Symfony\Contracts\Translation\TranslatorInterface;
  13. use App\Entity\Kernel\ListItem;
  14. use App\Service\Kernel\KernelService;
  15. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. /**
  18.  * @Route("/{_locale}/lead")
  19.  */
  20. class LeadController extends AbstractController {
  21.     /**
  22.      * @Route("/index/{state}/{open}/{my}/{user}/{website}", name="lead_index", methods={"GET"})
  23.      */
  24.     public function index(int $state=0, int $open=2, int $my=0, int $user=0, int $website=0): Response {
  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.         return $this->render('finance/lead/index.html.twig', [
  30.            'state' => $state, 
  31.            'open' => $open,
  32.            'my' => $my,
  33.            'user' => $user,
  34.            'website' => $website,
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/table/{state}/{open}/{my}/{user?0}/{website?0}", name="lead_table", methods={"GET"})
  39.      */
  40.     public function table(int $state, int $open, int $my, int $user = 0, int $website=0, KernelService $kernelservice): Response {
  41.         $classname = explode("\\", get_class($this));
  42.         $arraysize = count($classname);
  43.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
  44.         $this->denyAccessUnlessGranted($attribute);
  45.         
  46.         $leadnewconsultant = $kernelservice->getApplicationInfoValue("Sales.LeadNewConsultant");
  47.         $leadnewproduct = $kernelservice->getApplicationInfoValue("Sales.LeadNewProduct");
  48.         // variable of button 
  49.         $button = $state + 3*$open + 9*$my;
  50.         return $this->render('finance/lead/table.html.twig', [
  51.            'button' => $button, 'state' => $state, 'open' => $open, 'my' => $my, 'user' => $user, 
  52.            'website' => $website,
  53.             'leadnewconsultant' => $leadnewconsultant, 'leadnewproduct' => $leadnewproduct
  54.         ]);
  55.     }
  56.     /**
  57.      * @Route("/tabledetails/{state}/{open}/{my}/{user}/{website}", name="lead_tabledetails", methods={"GET","POST"})
  58.      */
  59.      public function tabledetails(Request $request, int $state, int $open,int $my,int $user=0, int $website=0,TranslatorInterface $translator, KernelService $kernel_service) : Response {
  60.          $classname = explode("\\", get_class($this));
  61.          $arraysize = count($classname);
  62.          $attribute = str_replace("Controller", "", $classname[$arraysize - 1]).":".str_replace(
  63.                  "Action",
  64.                  "",
  65.                  __FUNCTION__
  66.              );
  67.          $this->denyAccessUnlessGranted($attribute);
  68.          $start = $request->get('start');
  69.          $length = $request->get('length');
  70.          $em = $this->getDoctrine()->getManager();
  71.          $organization = $this->getUser()->getOrganization();
  72.          if ($organization == null) {
  73.              $organizationid = 0;
  74.          } else {
  75.              $organizationid = $organization->getId();
  76.          }
  77.          
  78.          $openbasedondate = $kernel_service->getApplicationInfoValue('Kernel.OpenLeadBasedOnDate');
  79.          
  80.          $filter = $request->get('search')['value'];
  81.         $order = $request->get('order');
  82.         $orderColumn = $order[0]["column"];
  83.         $orderby = $this->orderColumns($orderColumn);
  84.          $direction = strtoupper($order[0]["dir"]);
  85.         $queryString = "SELECT s FROM App\Entity\Finance\Lead s LEFT JOIN s.vacancies t WHERE ((s.state = ?1) ";
  86.         if($open != 2){
  87.             if($website == 0){
  88.                 if ($openbasedondate == true)
  89.                 {
  90.                     $queryString = $queryString . "AND (s.created >= ?3) ";
  91.                 }
  92.                 else
  93.                 {
  94.                     $queryString = $queryString . "AND (s.open = ?3) ";
  95.                 }
  96.             }
  97.         }
  98.         if($my == 1){
  99.             $queryString = $queryString . "AND (s.owner = ?4 ) ";
  100.         }
  101.         if($user != 0){
  102.             $queryString = $queryString . "AND (s.owner = ?4 ) ";
  103.         }
  104.         if($website != 0){
  105.             $queryString = $queryString . "AND (t.state = 0 ) ";
  106.         }
  107.         $queryString = $queryString ."AND ((?2 = 0) or ((s.organization = ?2) or (s.organization is null)))) ";
  108.         //$queryString = $queryString . ")";
  109.         if(!empty($filter) == true){
  110.             $queryString = $queryString ."AND ((s.subject LIKE '%".$filter."%') "
  111.                     . "or (s.created LIKE '%".$filter."%')) ";
  112.             
  113.         } 
  114.         $queryString = $queryString ."ORDER BY ".$orderby." ".$direction;
  115.         $query = $em->createQuery($queryString);
  116.         $count = $query->setParameter(2, $organizationid)
  117.                        ->setParameter(1, $state);
  118.          if($open != 2){
  119.              if($website == 0){
  120.                 if ($openbasedondate == true)
  121.                 {
  122.                     $dateInTwoWeeks = new \DateTime();
  123.                     $dateInTwoWeeks->modify('-4 week');
  124.                     $count->setParameter(3, $dateInTwoWeeks);
  125.                 }
  126.                 else
  127.                 {
  128.                     $count->setParameter(3, $open);
  129.                 }
  130.              }
  131.          }
  132.          if($my == 1){
  133.              $count->setParameter(4, $this->getUser());
  134.          }
  135.          if($user != 0){
  136.              $count->setParameter(4, $user);
  137.          }
  138.         $total = count($count->getResult());
  139.         $query->setMaxResults($length)
  140.               ->setFirstResult($start);
  141.         $leads = $query->getResult();
  142.         $twig = clone $this->get('twig');
  143.         
  144.         
  145.         
  146.         
  147.         $datawithlinks = array();
  148.         foreach($leads as $row){
  149.             $i = 0;
  150.             $newrow = array();
  151.             $url =  $twig->createTemplate("{{ path('lead_show', {'id' : id,'state' : state }) }}")
  152.                     ->render(array("id" => $row->getId(),"state" => $row->getState()));
  153.             $newrow[$i] = $row->getId();
  154.             $i++;
  155.             $newrow[$i] = $url;
  156.             $i++;
  157.             if (false)
  158.             {
  159.                 $newrow[$i] = $translator->trans($row->getType());  
  160.                 $i++;
  161.             }
  162.             $newrow[$i] = "".$row;
  163.             $i++;
  164.             $newrow[$i] = "".$row->getClient();
  165.             $i++;
  166.             $newrow[$i] = "".$row->getendClient();
  167.             $i++;
  168.             $newrow[$i] = "".$row->getCreated()->format('d/m/Y');   
  169.             $i++;
  170.             $newrow[$i] = "".$row->getFunction();   
  171.             $i++;
  172.             $newrow[$i] = "".$row->getTechnology();   
  173.             $i++;
  174.             $newrow[$i] = "".$row->getOwner();
  175.             $i++;
  176.             $newrow[$i] = "".$this->giveAmountOfProposals($row);
  177.             $i++;
  178.             if($row->getOpen()){
  179.                 $state = "Open";
  180.             }else{
  181.                 $state = "Closed";
  182.             }
  183.             $newrow[$i] = $translator->trans($state);
  184.             $i++;
  185.             $newrow[$i] = "".$row->getChance();
  186.             $i++;
  187.             $newrow[$i] = "".$row->getOrganization();
  188.             $i++;
  189.             $datawithlinks[] = $newrow;
  190.         }
  191.         $return = array(
  192.             "draw" => intval($request->get('draw')),
  193.             "recordsTotal" => $total,
  194.             "recordsFiltered" => $total,
  195.             "aaData" => $datawithlinks
  196.         );
  197.         $return = json_encode($return);
  198.         return new Response($return,200,array('Content-Type' => 'application/json'));
  199.         
  200.      }
  201.      
  202.      
  203.          public function orderColumns($orderColumn){
  204.         $orderby = ""; 
  205.         switch($orderColumn){
  206.             case 0:
  207.                 $orderby = "s.id"; 
  208.                 break;
  209.             case 2: 
  210.                 $orderby = "s.subject";
  211.                 break;
  212.             case 3:
  213.                 $orderby = "s.client";
  214.                 break;
  215.             case 4:
  216.                 $orderby = "s.endClient";
  217.                 break;
  218.             case 5:
  219.                 $orderby = "s.created";
  220.                 break;
  221.             case 6:
  222.                 $orderby = "s.owner";
  223.                 break;
  224.             case 8:
  225.                 $orderby = "s.open";
  226.                 break;
  227.             case 9:
  228.                 $orderby = "s.chance";
  229.                 break;
  230.             case 10:
  231.                 $orderby = "s.organization";
  232.                 break;
  233.                 
  234.         }
  235.         return $orderby;
  236.     }
  237.     /**
  238.      * @Route("/{id}/newProposal", name="lead_newProposal", methods={"GET","POST"})
  239.      */
  240.     public function newProposal(Request $request, Lead $lead, KernelService $kernelservice): Response {
  241.         $classname = explode("\\", get_class($this));
  242.         $arraysize = count($classname);
  243.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
  244.         $this->denyAccessUnlessGranted($attribute);
  245.         $proposal = new Proposal();
  246.         $date = new DateTime();
  247.         $proposal->setLead($lead);
  248.         $proposal->setProposedOn($date);        
  249.         $proposal->setOpen($lead->getOpen());
  250.         $proposal->setSubject($lead->getSubject());
  251.         $proposal->setType($lead->getType());
  252.         $proposal->setClient($lead->getClient());
  253.         $proposal->setEndClient($lead->getEndClient());
  254.         $proposal->setOwner($lead->getOwner());
  255.         $proposal->setOrganization($lead->getOrganization());
  256.         
  257.         $simpleProposals = $kernelservice->getApplicationInfoValue("Sales.SimpleProposals");
  258.         //$proposal->setOrganization($lead->getOrganization());
  259.         $form = $this->createForm(ProposalType::class, $proposal,
  260.                 [
  261.                     'organization' => $this->getUser()->getOrganization(), 
  262.                     'action' => $this->generateUrl('proposal_new'), 
  263.                     'attr' => ['id' => $this->generateUrl('proposal_new')
  264.                 ]]);
  265.         
  266.         if($simpleProposals)
  267.         {
  268.             $required = true;
  269.             $form->add('employeeselect2',ChoiceType::class, array('priority' => 3, 'label' => 'Proposal.Employee',
  270.                     'required' => true,
  271.                     'multiple' => false,
  272.                     'mapped' => false,
  273.                     'attr' => array('class' => 'selectemployee')
  274.                 ));
  275.             $form->add('intakePrice', NumberType::class, array('priority' => 2, 'mapped' => false, 'label' => 'IntakePrice','required' => $required));
  276.             $form->add('unitPrice', NumberType::class, array('priority' => 1, 'mapped' => false, 'label' => 'Proposal.UnitPrice','required' => $required));
  277.         }
  278.         if ($this->getUser()->getOrganization() == null) {
  279.             $form->add('organization', null, array('label' => 'Organization',
  280.                 'query_builder' => function ($repository) {
  281.                     return $repository->createQueryBuilder('s')->where("s.state = 0");
  282.                 },
  283.                 'attr' => ['class' => 'select2']));
  284.         }
  285.         $form->handleRequest($request);
  286.         if ($form->isSubmitted() && $form->isValid()) {
  287.             $entityManager = $this->getDoctrine()->getManager();
  288.             $proposal->setState(0);
  289.             $entityManager->persist($proposal);
  290.             $entityManager->flush();
  291.             $this->denyAccessUnlessGranted($attribute, $proposal);
  292.             return $this->redirectToRoute('proposal_index');
  293.         }
  294.         return $this->render('finance/proposal/new.html.twig', [
  295.                     'proposal' => $proposal,
  296.                     'form' => $form->createView(),
  297.         ]);
  298.     }
  299.     /**
  300.      * @Route("/new/{type}", name="lead_new", methods={"GET","POST"})
  301.      */
  302.     public function new(Request $request, TranslatorInterface $translator, int $type, KernelService $kernelService): Response {
  303.         $classname = explode("\\", get_class($this));
  304.         $arraysize = count($classname);
  305.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
  306.         $this->denyAccessUnlessGranted($attribute);
  307.         $organization = $this->getUser()->getOrganization();
  308.         $lead = new Lead();
  309.         $lead->setOpen(true);
  310.         $lead->setChance(0);
  311.         $lead->setOwner($this->getUser());
  312.         if ($type == 0) {
  313.             $typeListItem = $kernelService->getListItem("Finance.LeadType", "LeadType.Product", $organization);
  314.         } else {
  315.             $typeListItem = $kernelService->getListItem("Finance.LeadType", "LeadType.Resource", $organization);
  316.         }
  317.         $form = $this->createForm(LeadType::class, $lead,
  318.                 ['organization' => $organization
  319.                     , 'action' => $this->generateUrl('lead_new', array('type' => $type)),
  320.                     'type' => $type,
  321.                     'attr' => ['id' => $this->generateUrl('lead_new', array('type' => $type))]]);
  322.         if ($this->getUser()->getOrganization() == null) {
  323.             $form->add('organization', null, array('label' => 'Organization',
  324.                 'query_builder' => function ($repository) {
  325.                     return $repository->createQueryBuilder('s')->where("s.state = 0");
  326.                 },
  327.                 'attr' => ['class' => 'select2']));
  328.         }
  329.         $form->handleRequest($request);
  330.         if ($form->isSubmitted() && $form->isValid()) {
  331.             $chance = $lead->getChance();
  332.             $lead->setType($typeListItem);
  333.             if ($this->checkBetween0and100($chance, $translator)) {
  334.                 $entityManager = $this->getDoctrine()->getManager();
  335.                 $lead->setState(0);
  336.                 if ($organization != null) {
  337.                     $lead->setOrganization($organization);
  338.                 }
  339.                 $entityManager->persist($lead);
  340.                 $entityManager->flush();
  341.                 $this->denyAccessUnlessGranted($attribute, $lead);
  342.                 return $this->redirectToRoute('lead_index');
  343.             }
  344.         }
  345.         return $this->render('finance/lead/new.html.twig', [
  346.                     'lead' => $lead,
  347.                     'form' => $form->createView(),
  348.                     'type' => $type
  349.         ]);
  350.     }
  351.     /**
  352.      * @Route("/{id}/showLead/{state}", name="show_lead", methods={"GET"})
  353.      */
  354.     public function showLead(Lead $lead, int $state, KernelService $kernelservice): Response {
  355.         $classname = explode("\\", get_class($this));
  356.         $arraysize = count($classname);
  357.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
  358.         $this->denyAccessUnlessGranted($attribute, $lead);
  359.         
  360.         $leadnewconsultant = $kernelservice->getApplicationInfoValue("Sales.LeadNewConsultant");
  361.         $leadnewproduct = $kernelservice->getApplicationInfoValue("Sales.LeadNewProduct");
  362.         return $this->render('finance/lead/lead_show.html.twig', [
  363.                     'lead' => $lead, 'state' => $state
  364.                 , 'leadnewconsultant' => $leadnewconsultant, 'leadnewproduct' => $leadnewproduct
  365.         ]);
  366.     }
  367.     /**
  368.      * @Route("/{id}/show/{state?0}", name="lead_show", methods={"GET"})
  369.      */
  370.     public function show(Lead $lead, int $state = 0, KernelService $kernelservice): Response {
  371.         $classname = explode("\\", get_class($this));
  372.         $arraysize = count($classname);
  373.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
  374.         $this->denyAccessUnlessGranted($attribute, $lead);
  375.         $proposals = $this->getDoctrine()
  376.                 ->getRepository(Proposal::class)
  377.                 ->findBy(array('state' => $state, 'lead' => $lead));
  378.         
  379.         
  380.         $leadnewconsultant = $kernelservice->getApplicationInfoValue("Sales.LeadNewConsultant");
  381.         $leadnewproduct = $kernelservice->getApplicationInfoValue("Sales.LeadNewProduct");
  382.         return $this->render('finance/lead/show.html.twig', [
  383.                     'lead' => $lead, 'proposals' => $proposals, 'state' => $state
  384.                 , 'leadnewconsultant' => $leadnewconsultant, 'leadnewproduct' => $leadnewproduct
  385.         ]);
  386.     }
  387.     /**
  388.      * @Route("/{id}/audit", name="lead_showaudit", methods={"GET"})
  389.      */
  390.     public function showaudit(Lead $lead): Response {
  391.         $classname = explode("\\", get_class($this));
  392.         $arraysize = count($classname);
  393.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
  394.         $this->denyAccessUnlessGranted($attribute, $lead);
  395.         return $this->render('finance/lead/showaudit.html.twig', [
  396.                     'lead' => $lead,
  397.         ]);
  398.     }
  399.     /**
  400.      * @Route("/{id}/restore", name="lead_restore", methods={"GET"})
  401.      */
  402.     public function restore(Lead $lead): Response {
  403.         $classname = explode("\\", get_class($this));
  404.         $arraysize = count($classname);
  405.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
  406.         $this->denyAccessUnlessGranted($attribute, $lead);
  407.         $entityManager = $this->getDoctrine()->getManager();
  408.         $lead->setState(0);
  409.         $entityManager->persist($lead);
  410.         $entityManager->flush();
  411.         return $this->redirectToRoute('show_lead', array('id' => $lead->getId(), 'state' => 0));
  412.     }
  413.     /**
  414.      * @Route("/{id}/archived", name="lead_archived", methods={"GET"})
  415.      */
  416.     public function archive(Lead $lead): Response
  417.     {
  418.         $classname = explode("\\", get_class($this));
  419.         $arraysize = count($classname);
  420.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]).":".str_replace(
  421.                 "Action",
  422.                 "",
  423.                 __FUNCTION__
  424.             );
  425.         $this->denyAccessUnlessGranted($attribute, $lead);
  426.         $entityManager = $this->getDoctrine()->getManager();
  427.         $lead->setState(1);
  428.         
  429.         foreach($lead->getVacancies() as $vancancy)
  430.         {
  431.             $vancancy->setState(1);
  432.             $entityManager->persist($vancancy);
  433.         }
  434.         
  435.         $entityManager->persist($lead);
  436.         $entityManager->flush();
  437.         return $this->redirectToRoute('show_lead', array('id' => $lead->getId(), 'state' => 1));
  438.     }
  439.     /**
  440.      * @Route("/{id}/delete", name="lead_delete", methods={"GET"})
  441.      */
  442.     public function delete(Lead $lead): Response {
  443.         $classname = explode("\\", get_class($this));
  444.         $arraysize = count($classname);
  445.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
  446.         $this->denyAccessUnlessGranted($attribute, $lead);
  447.         $entityManager = $this->getDoctrine()->getManager();
  448.         $lead->setState(2);
  449.         $entityManager->persist($lead);
  450.         $entityManager->flush();
  451.         return $this->redirectToRoute('lead_index');
  452.     }
  453.     /**
  454.      * @Route("/{id}/edit", name="lead_edit", methods={"GET","POST"})
  455.      */
  456.     public function edit(Request $request, Lead $lead): Response {
  457.         $classname = explode("\\", get_class($this));
  458.         $arraysize = count($classname);
  459.         $attribute = str_replace("Controller", "", $classname[$arraysize - 1]) . ":" . str_replace("Action", "", __FUNCTION__);
  460.         $this->denyAccessUnlessGranted($attribute, $lead);
  461.         
  462.         $type = 0;
  463.         if ($lead->getType()->getName() == "LeadType.Resource")
  464.         {
  465.             $type = 1;
  466.         }
  467.         $form = $this->createForm(LeadType::class, $lead,
  468.                 ['organization' => $lead->getOrganization(), 'type' => $type,
  469.                     'action' => $this->generateUrl('lead_edit', array('id' => $lead->getId())), 'attr' => ['id' => $this->generateUrl('lead_edit', array('id' => $lead->getId()))]]);
  470.         $form->handleRequest($request);
  471.         if ($form->isSubmitted() && $form->isValid()) {
  472.             $this->getDoctrine()->getManager()->flush();
  473.             return $this->redirectToRoute('lead_show', array('id' => $lead->getId()));
  474.         }
  475.         return $this->render('finance/lead/edit.html.twig', [
  476.                     'lead' => $lead,
  477.                     'form' => $form->createView(),
  478.         ]);
  479.     }
  480.     public function giveAmountOfProposals(Lead $lead) {
  481.         $amountOfProposals = $lead->giveAmountOfProposals();
  482.         return $amountOfProposals;
  483.     }
  484.     public function checkBetween0and100(float $chance, TranslatorInterface $translator) {
  485.         if ($chance > 1 || $chance < 0) {
  486.             $this->get('session')->getFlashBag()->add('error', $translator->trans('Lead.valuemustbebetween0and100'));
  487.             return false;
  488.         } else {
  489.             return true;
  490.         }
  491.     }
  492. }