src/Controller/CRM/ClientController.php line 174

Open in your IDE?
  1. <?php
  2. namespace App\Controller\CRM;
  3. use App\Entity\CRM\Client;
  4. use App\Form\CRM\ClientType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use App\Entity\Kernel\ListItem;
  11. use App\Service\Kernel\KernelService;
  12. use App\Entity\Kernel\ConfigurableList;
  13. use App\Entity\Kernel\Setting;
  14. use App\Service\Kernel\DMSService;
  15. use App\Service\CRM\ClientService;
  16. /**
  17.  * @Route("/{_locale}/client")
  18.  */
  19. class ClientController extends AbstractController
  20. {
  21.     
  22.     /**
  23.      * @Route("/", name="client_index", methods={"GET"})
  24.      */
  25.     public function index(): Response
  26.     {
  27.         $classname =  explode("\\", get_class($this));
  28.         $arraysize = count($classname);
  29.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  30.         $this->denyAccessUnlessGranted($attribute);
  31.     
  32.         /*$clients = $this->getDoctrine()
  33.             ->getRepository(Client::class)
  34.             ->findBy(array('state' => 0));*/
  35.         /*$em = $this->getDoctrine()->getManager();
  36.         if ($this->getUser()->getOrganization() == null)
  37.     {
  38.             $query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (s.state = 0)");
  39.             $clients = $query->getResult();
  40.     }
  41.     else
  42.     {
  43.             $query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE ((s.state = 0) AND ((s.organization = ?2) or (s.organization is null)))");
  44.         $query = $query->setParameter(2, $this->getUser()->getOrganization()->getId());
  45.             $clients = $query->getResult();
  46.     }*/
  47.         
  48.         
  49.         return $this->render('crm/client/index.html.twig', [
  50.         ]);
  51.     }
  52.     
  53.     /**
  54.      * @Route("/table/{state}", name="client_table", methods={"GET"})
  55.      */
  56.     public function table(int $state, KernelService $kernelservice): Response
  57.     {
  58.         $classname =  explode("\\", get_class($this));
  59.         $arraysize = count($classname);
  60.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  61.         $this->denyAccessUnlessGranted($attribute);
  62.         
  63.         $newindividuals = $kernelservice->getApplicationInfoValue("CRM.NewIndividuals");
  64.     
  65.         //State 3 = my clients
  66.         $em = $this->getDoctrine()->getManager();
  67.         if ($this->getUser()->getOrganization() == null)
  68.     {
  69.             if ($state != 3)
  70.             {
  71.                 $query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (s.state = ?1)");
  72.                 $query = $query->setParameter(1, $state);
  73.                 $clients = $query->getResult();
  74.             }
  75.             else
  76.             {
  77.                 $query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (s.state = 0) AND s.owner = ?1");
  78.                 $query = $query->setParameter(1, $this->getUser()->getId());
  79.                 $clients = $query->getResult();
  80.             }
  81.     }
  82.     else
  83.     {
  84.             if ($state != 3)
  85.             {
  86.                 $query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE ((s.state = ?1) AND ((s.organization = ?2) or (s.organization is null)))");
  87.                 $query = $query->setParameter(1, $state);
  88.                 $query = $query->setParameter(2, $this->getUser()->getOrganization()->getId());
  89.                 $clients = $query->getResult();
  90.             }
  91.             else
  92.             {
  93.                 $query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE ((s.state = 0) AND ((s.organization = ?2) or (s.organization is null)))  AND s.owner = ?1");
  94.                 $query = $query->setParameter(2, $this->getUser()->getOrganization()->getId());
  95.                 $query = $query->setParameter(1, $this->getUser()->getId());
  96.                 $clients = $query->getResult();
  97.             }
  98.     }
  99.         
  100.         return $this->render('crm/client/table.html.twig', [
  101.             'state' => $state,
  102.             'clients' => $clients,
  103.             'newindividuals' => $newindividuals
  104.         ]);
  105.     }
  106.     
  107.     
  108.     
  109.     /**
  110.      * Lists all building entities.
  111.      *
  112.      * @Route("/select", name="client_select", methods={"GET"})
  113.      */
  114.     public function select(Request $request, TranslatorInterface $translator): Response
  115.     {
  116.         $classname =  explode("\\", get_class($this));
  117.         $arraysize = count($classname);
  118.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  119.         $this->denyAccessUnlessGranted($attribute);
  120.         
  121.         $em = $this->getDoctrine()->getManager();
  122.         $id = $request->get('id');
  123.         
  124.         $organization = $this->getUser()->getOrganization();
  125.         $organization_id = $request->get('organization');
  126.         if (empty($organization_id) or ($organization_id == 0))
  127.         {
  128.             //$organisation = null;
  129.         }
  130.         else
  131.         {
  132.             $organization = $em->getRepository('App\Entity\Kernel\Organization')->findOneById($organization_id);
  133.         }
  134.         if ($organization == null) {
  135.             $query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (s.state = 0) AND ((s.company LIKE ?1 OR s.firstname LIKE ?1) OR s.lastname LIKE ?1)");
  136.             $query = $query->setParameter(1, '%'.$id.'%');
  137.             $clients = $query->getResult();
  138.         } else {
  139.             $query = $em->createQuery("SELECT s FROM App\Entity\CRM\Client s WHERE (((s.state = 0)  AND ((s.company LIKE ?1 OR s.firstname LIKE ?1) OR s.lastname LIKE ?1)) AND ((s.organization = ?2) or (s.organization is null)))");
  140.             $query = $query->setParameter(1, '%'.$id.'%');
  141.             $query = $query->setParameter(2, $organization->getId());
  142.             $clients = $query->getResult();
  143.         }
  144.         $clientsfound = array();
  145.         foreach ($clients as $client) {
  146.             $clientsfound[] = array("id" => "SU" . $client->getId(), "name" => "" . $client);
  147.         }
  148.         $newcompany = $translator->trans("CRM.Newcompany");
  149.         $newclient = $translator->trans("CRM.NewindividualLastname");
  150.         if (empty($id) == false) {
  151.             $clientsfound[] = array("id" => "NC" . $id, "name" => $id . " (" . $newcompany . ")");
  152.             $clientsfound[] = array("id" => "NI" . $id, "name" => $id . " (" . $newclient . ")");
  153.         }
  154.         
  155.         //CRM.Newindividual
  156.         $return = array("items" => $clientsfound);
  157.         $return = json_encode($return);
  158.         return new Response($return, 200, array('Content-Type' => 'application/json'));
  159.     }
  160.     /**
  161.      * @Route("/new/{type?0}", name="client_new", methods={"GET","POST"})
  162.      */
  163.     public function new(Request $request, int $type = 0, TranslatorInterface $translator, DMSService $dmsservice, ClientService $clientService): Response
  164.     {
  165.         $classname =  explode("\\", get_class($this));
  166.         $arraysize = count($classname);
  167.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  168.         $this->denyAccessUnlessGranted($attribute);
  169.         
  170.         $entityManager = $this->getDoctrine()->getManager();
  171.     
  172.         $postcodes = array();
  173.         $municipalities = array();
  174.         $organization = $this->getUser()->getOrganization();
  175.         if ($organization != null)
  176.         {
  177.             $organization = $organization->getId();
  178.         }
  179.         else
  180.         {
  181.             $organization = -1;
  182.         }
  183.         if ($request->isMethod('POST'))
  184.         {
  185.             $requestdata = $request->request->all("client");
  186.             $postcodeid = $requestdata['postcode'];
  187.             $postcodeidvalue = $requestdata['postcode'];
  188.             if (empty($postcodeid) == false)
  189.             {
  190.                 
  191.                 $type2 = substr($postcodeid, 0, 8);
  192.                 $postcodeidsub = substr($postcodeid, 8);
  193.                 if ($type2 == "INTERNAL")
  194.                 {
  195.                     $item  = $entityManager->getRepository(ListItem::class)->findOneById($postcodeidsub);
  196.                     $postcodeidvalue = $item->getString1();
  197.                 }
  198.             }
  199.             $postcodes[$postcodeidvalue] = $postcodeid;
  200.         }
  201.         if ($request->isMethod('POST'))
  202.         {
  203.                 $requestdata = $request->request->all("client");
  204.                 $municipalityid = $requestdata['municipality'];
  205.                 $municipalityidvalue = $requestdata['municipality'];
  206.             if (empty($municipalityid) == false)
  207.             {
  208.                     $type2 = substr($municipalityid, 0, 8);
  209.                     $municipalityidsub = substr($municipalityid, 8);
  210.                     if ($type2 == "INTERNAL")
  211.                     {
  212.                             $item  = $entityManager->getRepository(ListItem::class)->findOneById($municipalityidsub);
  213.                             $municipalityidvalue = $item->getName();
  214.                     }
  215.             }
  216.             $municipalities[$municipalityidvalue] = $municipalityid;
  217.         }
  218.         
  219.         $client = new Client();
  220.         $client->setContactByEmail(true);
  221.         $form = $this->createForm(ClientType::class, $client, 
  222.                     ['postcode' => $postcodes, 'municipality' => $municipalities, 'organization' => $organization,
  223.                         'type' => $type,
  224.                         'action' => $this->generateUrl('client_new', array('type' => $type)),
  225.                         'attr' => ['id' => $this->generateUrl('client_new', array('type' => $type))]]);
  226.         
  227.         if ($this->getUser()->getOrganization() == null)
  228.         {
  229.             $form->add('organization', null, array('label' => 'Organization',
  230.                     'query_builder'=>function ($repository){return $repository->createQueryBuilder('s')->where("s.state = 0");},
  231.                     'attr' => ['class' => 'select2']));
  232.         }
  233.         $form->handleRequest($request);
  234.         if ($form->isSubmitted() && $form->isValid()) {
  235.             
  236.             $organization = $client->getOrganization();
  237.             if ($this->getUser()->getOrganization() != null) {
  238.                 $organization = $this->getUser()->getOrganization();
  239.             }
  240.             
  241.             $taxnumberok = true;
  242.             if (empty($client->getTax()) == false) {
  243.                 $clients = $entityManager->getRepository(Client::class)->findBy(array('organization' => $client->getOrganization(), 'state' => 0, 'tax' => $client->getTax()));
  244.                 foreach ($clients as $checkclient) {
  245.                     if ($checkclient->getId() != $client->getId()) {
  246.                         $taxnumberok = false;
  247.                     }
  248.                 }
  249.             }
  250.             if ($taxnumberok == false) {
  251.                 $this->get('session')->getFlashBag()->add(
  252.                         'error',
  253.                         $translator->trans('Client.Taxnumberalreadyinuse')
  254.                 );
  255.             } else {
  256.                 $stateRegistrationNumberok = true;
  257.                 if (empty($client->getStateRegistrationNumber()) == false) {
  258.                     $clients = $entityManager->getRepository(Client::class)->findBy(array('organization' => $client->getOrganization(), 'state' => 0, 'stateRegistrationNumber' => $client->getStateRegistrationNumber()));
  259.                     foreach ($clients as $checkclient) {
  260.                         if ($checkclient->getId() != $client->getId()) {
  261.                             $stateRegistrationNumberok = false;
  262.                         }
  263.                     }
  264.                 }
  265.                 if ($stateRegistrationNumberok == false) {
  266.                     $this->get('session')->getFlashBag()->add(
  267.                             'error',
  268.                             $translator->trans('Client.Stateregistrationnumberalreadyinuse')
  269.                     );
  270.                 } else {
  271.                     $postcode = null;
  272.                     $municipality = null;
  273.                     $requestdata = $request->request->all("client");
  274.                     $postcodeid = $requestdata['postcode'];
  275.                     if (empty($postcodeid) == false) {
  276.                         $type2 = substr($postcodeid, 0, 8);
  277.                         $postcodeid = substr($postcodeid, 8);
  278.                         if ($type2 == "INTERNAL") {
  279.                             $item = $entityManager->getRepository(ListItem::class)->findOneById($postcodeid);
  280.                             $postcode = $item->getString1();
  281.                         } else {
  282.                             $postcode = $requestdata['postcode'];
  283.                         }
  284.                     }
  285.                     $municipalityid = $requestdata['municipality'];
  286.                     if (empty($municipalityid) == false) {
  287.                         $type2 = substr($municipalityid, 0, 8);
  288.                         $municipalityid = substr($municipalityid, 8);
  289.                         if ($type2 == "INTERNAL") {
  290.                             $item = $entityManager->getRepository(ListItem::class)->findOneById($municipalityid);
  291.                             $municipality = $item->getName();
  292.                         } else {
  293.                             $municipality = $requestdata['municipality'];
  294.                         }
  295.                     }
  296.                     $weblist = $entityManager->getRepository(ConfigurableList::class)->findOneByIdentifier("CRM.ClientType"); 
  297.                     if ($type == 0)
  298.                     {
  299.                         $clienttype = $entityManager->getRepository(ListItem::class)->findOneBy(array('configurablelist' => $weblist->getId(), 'name' => 'CRM.Company'));
  300.                     }
  301.                     else
  302.                     {
  303.                         $clienttype = $entityManager->getRepository(ListItem::class)->findOneBy(array('configurablelist' => $weblist->getId(), 'name' => 'CRM.Individual'));
  304.                         
  305.                     }
  306.                     
  307.                     $input = $request->files->get('client');
  308.                     $uploadfile = null;
  309.                     if ($input != null) {
  310.                         $uploadfile = $input['image'];
  311.                     }
  312.                     
  313.                     $client = $clientService->CreateClient($clienttype, $client->getCompany(), $client->getTax(), $client->getLastname(), $client->getFirstname(), 
  314.                             $client->getStateRegistrationNumber(), $client->getPhone(), $client->getPhone2(), $client->getEmail(), 
  315.                             $client->getLanguage(), $client->getStreet(), $client->getHousenumber(), $postcode, $municipality, $client->getCountry(), 
  316.                             $client->getIban(), $client->getBic(), $client->getOwner(), $client->getClienttags(), $client->getTaxrate(), 
  317.                             $client->getAdditionalInvoiceText(), $client->getAcronym(), $client->getExternalid(), $client->getDescription(), $client->getContactByEmail(),
  318.                             $uploadfile, $organization);
  319.                     
  320.                     /*$client->setClienttype($clienttype);                   
  321.                     
  322.                     $entityManager->persist($client);
  323.                     $entityManager->flush();
  324.                     $input = $request->files->get('client');
  325.                     $uploadfile = null;
  326.                     if ($input != null) {
  327.                         $uploadfile = $input['image'];
  328.                     }
  329.                     $applicationfolder = $dmsservice->createFolderIfNeeded("System");
  330.                     $clientfolder = $dmsservice->createFolderIfNeeded("Client", $applicationfolder);
  331.                     $folder = $dmsservice->createFolderIfNeeded("".$client->getId(), $clientfolder);
  332.                     if ($uploadfile == null) {
  333.                         $file = $dmsservice->copyfile($entityManager->getRepository(Setting::class)->findOneByIdentifier("Kernel.Newclientimage")->getFileValue(), $clientfolder);
  334.                     } else {
  335.                         $file = $dmsservice->uploadfile($uploadfile, $clientfolder);
  336.                     }
  337.                     $newname = "" . $client->getId();
  338.                     $newname = $newname . ".png";
  339.                     $file->setName($newname);
  340.                     $entityManager->persist($file);
  341.                     $entityManager->flush();
  342.                     $client->setImage($file);
  343.                     $entityManager->persist($client);
  344.                     $entityManager->flush();*/
  345.                     
  346.                     $this->denyAccessUnlessGranted($attribute, $client);
  347.                     return $this->redirectToRoute('client_show', array('id' => $client->getId()));
  348.                 }
  349.             }
  350.         }
  351.         return $this->render('crm/client/new.html.twig', [
  352.             'client' => $client,
  353.             'type' => $type,
  354.             'form' => $form->createView(),
  355.         ]);
  356.     }
  357.     /**
  358.      * @Route("/{id}", name="client_show", methods={"GET"})
  359.      */
  360.     public function show(Client $client, DMSService $dmsservice): Response
  361.     {
  362.         $classname =  explode("\\", get_class($this));
  363.         $arraysize = count($classname);
  364.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  365.         $this->denyAccessUnlessGranted($attribute, $client); 
  366.         
  367.         $entityManager = $this->getDoctrine()->getManager();
  368.         
  369.         $applicationfolder = $dmsservice->createFolderIfNeeded("System");
  370.         $clientfolder = $dmsservice->createFolderIfNeeded("Client", $applicationfolder);
  371.         $folder = $dmsservice->createFolderIfNeeded("".$client->getId(), $clientfolder);
  372.         if ($client->getImage() == null)
  373.         {
  374.             $file = $dmsservice->copyfile($entityManager->getRepository(Setting::class)->findOneByIdentifier("Kernel.Newclientimage")->getFileValue(), $clientfolder);
  375.         
  376.             $newname = "" . $client->getId();
  377.             $newname = $newname . ".png";
  378.             $file->setName($newname);
  379.             $entityManager->persist($file);
  380.             $entityManager->flush();
  381.             $client->setImage($file);
  382.             
  383.             $entityManager->persist($client);
  384.             $entityManager->flush();
  385.         }
  386.         
  387.         return $this->render('crm/client/show.html.twig', [
  388.             'client' => $client,
  389.             'folder' => $folder,
  390.         ]);
  391.     }
  392.     
  393.     /**
  394.      * @Route("/{id}/audit", name="client_showaudit", methods={"GET"})
  395.      */
  396.     public function showaudit(Client $client): Response
  397.     {
  398.         $classname =  explode("\\", get_class($this));
  399.         $arraysize = count($classname);
  400.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  401.         $this->denyAccessUnlessGranted($attribute, $client);  
  402.         
  403.         return $this->render('crm/client/showaudit.html.twig', [
  404.             'client' => $client,
  405.         ]);
  406.     }
  407.     
  408.     /**
  409.      * @Route("/{id}/archive", name="client_archive", methods={"GET"})
  410.      */
  411.     public function archive(Client $client): Response
  412.     {
  413.         $classname =  explode("\\", get_class($this));
  414.         $arraysize = count($classname);
  415.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  416.         $this->denyAccessUnlessGranted($attribute, $client); 
  417.         
  418.                 
  419.         $entityManager = $this->getDoctrine()->getManager();
  420.         $client->setState(1);
  421.         $entityManager->persist($client);
  422.         $entityManager->flush();
  423.         
  424.         return $this->redirectToRoute('client_show', array('id' => $client->getId()));
  425.     }
  426.     
  427.     /**
  428.      * @Route("/{id}/restore", name="client_restore", methods={"GET"})
  429.      */
  430.     public function restore(Client $client): Response
  431.     {
  432.         $classname =  explode("\\", get_class($this));
  433.         $arraysize = count($classname);
  434.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  435.         $this->denyAccessUnlessGranted($attribute, $client); 
  436.         
  437.                 
  438.         $entityManager = $this->getDoctrine()->getManager();
  439.         $client->setState(0);
  440.         $entityManager->persist($client);
  441.         $entityManager->flush();
  442.         
  443.         return $this->redirectToRoute('client_show', array('id' => $client->getId()));
  444.     }
  445.     
  446.     /**
  447.      * @Route("/{id}/delete", name="client_logicaldelete", methods={"GET"})
  448.      */
  449.     public function logicaldelete(Client $client): Response
  450.     {
  451.         $classname =  explode("\\", get_class($this));
  452.         $arraysize = count($classname);
  453.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  454.         $this->denyAccessUnlessGranted($attribute, $client); 
  455.         
  456.                 
  457.         $entityManager = $this->getDoctrine()->getManager();
  458.         $client->setState(2);
  459.         $entityManager->persist($client);
  460.         $entityManager->flush();
  461.         
  462.         return $this->redirectToRoute('client_index');
  463.     }
  464.     /**
  465.      * @Route("/{id}/edit", name="client_edit", methods={"GET","POST"})
  466.      */
  467.     public function edit(Request $request, Client $client, TranslatorInterface $translator, DMSService $dmsservice): Response
  468.     {
  469.         $classname =  explode("\\", get_class($this));
  470.         $arraysize = count($classname);
  471.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  472.         $this->denyAccessUnlessGranted($attribute, $client); 
  473.         
  474.         $entityManager = $this->getDoctrine()->getManager();
  475.     
  476.         $postcodes = array();
  477.         $municipalities = array();
  478.         if (empty($client->getPostcode())==false)
  479.         {
  480.             $postcodes[$client->getPostcode()] = $client->getPostcode();
  481.         }
  482.         if ($request->isMethod('POST'))
  483.         {
  484.             $requestdata = $request->request->all("client");
  485.             $postcodeid = $requestdata['postcode'];
  486.             $postcodeidvalue = $requestdata['postcode'];
  487.             if (empty($postcodeid) == false)
  488.             {
  489.                 $type = substr($postcodeid, 0, 8);
  490.                 $postcodeidsub = substr($postcodeid, 8);
  491.                 if ($type == "INTERNAL")
  492.                 {
  493.                     $item  = $entityManager->getRepository(ListItem::class)->findOneById($postcodeidsub);
  494.                     $postcodeidvalue = $item->getString1();
  495.                 }
  496.             }
  497.             $postcodes[$postcodeidvalue] = $postcodeid;
  498.         }
  499.         
  500.         if (empty($client->getMunicipality())==false)
  501.         {
  502.             $municipalities[$client->getMunicipality()] = $client->getMunicipality();
  503.         }
  504.         if ($request->isMethod('POST'))
  505.         {
  506.             $requestdata = $request->request->all("client");
  507.             $municipalityid = $requestdata['municipality'];
  508.             $municipalityidvalue = $requestdata['municipality'];
  509.             if (empty($municipalityid) == false)
  510.             {
  511.                     $type = substr($municipalityid, 0, 8);
  512.                     $municipalityidsub = substr($municipalityid, 8);
  513.                     if ($type == "INTERNAL")
  514.                     {
  515.                             $item  = $entityManager->getRepository(ListItem::class)->findOneById($municipalityidsub);
  516.                             $municipalityidvalue = $item->getName();
  517.                     }
  518.             }
  519.             $municipalities[$municipalityidvalue] = $municipalityid;
  520.         }
  521.         
  522.         $type=0;
  523.         if($client->getClientType() != null)
  524.         {
  525.             if ($client->getClientType()->getName() == "CRM.Individual")
  526.             {
  527.                 $type = 1;
  528.             }
  529.         }
  530.     
  531.         $form = $this->createForm(ClientType::class, $client, 
  532.                 ['postcode' => $postcodes, 'municipality' => $municipalities, 'organization' => $client->getOrganization(),
  533.                     'type' => $type, 'action' => $this->generateUrl('client_edit', array('id' => $client->getId())), 'attr' => ['id' => $this->generateUrl('client_edit', array('id' => $client->getId()))]]);
  534.         $form->handleRequest($request);
  535.         if ($form->isSubmitted() && $form->isValid()) {
  536.             $taxnumberok = true;
  537.             if (empty($client->getTax()) == false) {
  538.                 $clients = $entityManager->getRepository(Client::class)->findBy(array('organization' => $client->getOrganization(), 'state' => 0, 'tax' => $client->getTax()));
  539.                 foreach ($clients as $checkclient) {
  540.                     if ($checkclient->getId() != $client->getId()) {
  541.                         $taxnumberok = false;
  542.                     }
  543.                 }
  544.             }
  545.             if ($taxnumberok == false) {
  546.                 $this->get('session')->getFlashBag()->add(
  547.                         'error',
  548.                         $translator->trans('Client.Taxnumberalreadyinuse')
  549.                 );
  550.             } else {
  551.                 $stateRegistrationNumberok = true;
  552.                 if (empty($client->getStateRegistrationNumber()) == false) {
  553.                     $clients = $entityManager->getRepository(Client::class)->findBy(array('organization' => $client->getOrganization(), 'state' => 0, 'stateRegistrationNumber' => $client->getStateRegistrationNumber()));
  554.                     foreach ($clients as $checkclient) {
  555.                         if ($checkclient->getId() != $client->getId()) {
  556.                             $stateRegistrationNumberok = false;
  557.                         }
  558.                     }
  559.                 }
  560.                 if ($stateRegistrationNumberok == false) {
  561.                     $this->get('session')->getFlashBag()->add(
  562.                             'error',
  563.                             $translator->trans('Client.Stateregistrationnumberalreadyinuse')
  564.                     );
  565.                 } else {
  566.                     $requestdata = $request->request->all("client");
  567.                     $postcodeid = $requestdata['postcode'];
  568.                     if (empty($postcodeid) == false) {
  569.                         $type = substr($postcodeid, 0, 8);
  570.                         $postcodeid = substr($postcodeid, 8);
  571.                         if ($type == "INTERNAL") {
  572.                             $item = $entityManager->getRepository(ListItem::class)->findOneById($postcodeid);
  573.                             $client->setPostcode($item->getString1());
  574.                         } else {
  575.                             $client->setPostcode($requestdata['postcode']);
  576.                         }
  577.                     }
  578.                     $municipalityid = $requestdata['municipality'];
  579.                     if (empty($municipalityid) == false) {
  580.                         $type = substr($municipalityid, 0, 8);
  581.                         $municipalityid = substr($municipalityid, 8);
  582.                         if ($type == "INTERNAL") {
  583.                             $item = $entityManager->getRepository(ListItem::class)->findOneById($municipalityid);
  584.                             $client->setMunicipality($item->getName());
  585.                         } else {
  586.                             $client->setMunicipality($requestdata['municipality']);
  587.                         }
  588.                     }
  589.                     $entityManager->persist($client);
  590.                     $entityManager->flush();
  591.                     $input = $request->files->get('client');
  592.                     $uploadfile = null;
  593.                     if ($input != null) {
  594.                         $uploadfile = $input['image'];
  595.                         if ($uploadfile != null) {
  596.                             $clientimage = $client->getImage();
  597.                             $dmsservice->addVersion($clientimage, $uploadfile);
  598.                         }
  599.                     }
  600.                     
  601.                     $this->denyAccessUnlessGranted($attribute, $client);
  602.                     return $this->redirectToRoute('client_show', array('id' => $client->getId()));
  603.                 }
  604.             }
  605.             return $this->redirectToRoute('client_show', array('id' => $client->getId()));
  606.         }
  607.         return $this->render('crm/client/edit.html.twig', [
  608.             'client' => $client,
  609.             'form' => $form->createView(),
  610.         ]);
  611.     }
  612.     /**
  613.      * @Route("/{id}", name="client_delete", methods={"DELETE"})
  614.      */
  615.     public function delete(Request $request, Client $client): Response
  616.     {
  617.         $classname =  explode("\\", get_class($this));
  618.         $arraysize = count($classname);
  619.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  620.         $this->denyAccessUnlessGranted($attribute, $client); 
  621.     
  622.         if ($this->isCsrfTokenValid('delete'.$client->getId(), $request->request->get('_token'))) {
  623.             $entityManager = $this->getDoctrine()->getManager();
  624.             $entityManager->remove($client);
  625.             $entityManager->flush();
  626.         }
  627.         return $this->redirectToRoute('client_index');
  628.     }
  629. }