src/Controller/Clients/ClientController.php line 97

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Clients;
  3. use App\Domain;
  4. use App\Domain\Entities;
  5. use App\Security\Voter\ClientVoter;
  6. use DateTime;
  7. use Doctrine\DBAL;
  8. use Doctrine\ORM;
  9. use Exception;
  10. use Symfony\Bundle\FrameworkBundle;
  11. use Symfony\Component\HttpFoundation;
  12. class ClientController extends FrameworkBundle\Controller\AbstractController
  13. {
  14.     private DBAL\Connection $connection;
  15.     private ORM\EntityManagerInterface $entityManager;
  16.     private Domain\Repositories\ApplicationRepository $applicationRepository;
  17.     public function __construct(
  18.         DBAL\Connection $connection,
  19.         ORM\EntityManagerInterface $entityManager,
  20.         Domain\Repositories\ApplicationRepository $applicationRepository,
  21.     ) {
  22.         $this->connection $connection;
  23.         $this->entityManager $entityManager;
  24.         $this->applicationRepository $applicationRepository;
  25.     }
  26.     public function index(): HttpFoundation\Response
  27.     {
  28.         return $this->render(
  29.             'clients/client/index.html.twig',
  30.             array(
  31.                 'adminlevel' => $this->get('session')->get('adminlevel'),
  32.             )
  33.         );
  34.     }
  35.     public function search(HttpFoundation\Request $request): HttpFoundation\Response
  36.     {
  37.         $clientName $request->get('criteria');
  38.         $criteria preg_replace('/  +/'' 'preg_replace('/[^\'a-z ]/'' 'strtolower($clientName)));
  39.         $sql "
  40.             SELECT *
  41.             FROM clients
  42.             WHERE (CONCAT_WS(' ', lower(clients.lname), lower(clients.fname)) LIKE lower(:criteria) OR CONCAT_WS(' ', lower(clients.fname), lower(clients.lname)) LIKE lower(:criteria))
  43.             ORDER BY lname";
  44.         $statement $this->connection->prepare($sql);
  45.         $statement->bindValue(':criteria'$criteria '%');
  46.         $result $statement->executeQuery();
  47.         $clients $result->fetchAllAssociative();
  48.         return $this->render(
  49.             'clients/client/search.html.twig',
  50.             array(
  51.                 'clients' => $clients,
  52.                 'criteria' => $clientName
  53.             )
  54.         );
  55.     }
  56.     public function add(HttpFoundation\Request $request): HttpFoundation\Response
  57.     {
  58.         $allow $request->get('allow');
  59.         if ($this->get('session')->get('adminlevel') == && empty($allow)) {
  60.             return $this->redirect('/clients/client');
  61.         }
  62.         $client = new Entities\Client();
  63.         if ($request->getMethod() == 'POST' && $this->setClient($client$request)) {
  64.             $redirectTo '/clients/client/edit/';
  65.             if (!empty($this->get('session')->get('redirecttype')) && $this->get('session')->get('redirecttype') == 'newclient' && !empty($this->get('session')->get('redirect'))) {
  66.                 $redirectTo $this->get('session')->get('redirect');
  67.             }
  68.             return $this->redirect($redirectTo $client->getId());
  69.         }
  70.         return $this->render(
  71.             'clients/client/form.html.twig',
  72.             array(
  73.                 'client' => $client,
  74.                 'county' => '',
  75.                 'applications' => $this->applicationRepository->applicationsOfClientIdentity($client->getId()),
  76.                 'carriers' => $this->entityManager->getRepository(Entities\Carrier::class)->findAll([], ['name' => 'ASC']),
  77.                 'showProtectedFields' => true,
  78.             )
  79.         );
  80.     }
  81.     public function edit(HttpFoundation\Request $request): HttpFoundation\Response
  82.     {
  83.         $id $request->get('id');
  84.         $client $this->entityManager->find(Entities\Client::class, $id);
  85.         if (!$client) {
  86.             return $this->redirect('/clients/client');
  87.         }
  88.         if ($request->getMethod() == "POST" && $this->setClient($client$request)) {
  89.             $redirectTo '/clients/client/edit/';
  90.             if (!empty($this->get('session')->get('redirecttype')) && $this->get('session')->get('redirecttype') == 'newclient' && !empty($this->get('session')->get('redirect'))) {
  91.                 $redirectTo $this->get('session')->get('redirect');
  92.             }
  93.             return $this->redirect($redirectTo $client->getId());
  94.         }
  95.         return $this->render(
  96.             'clients/client/form.html.twig',
  97.             array(
  98.                 'client' => $client,
  99.                 'county' => $this->lookupCounty($client->getCity(), $client->getState()),
  100.                 'applications' => $this->applicationRepository->applicationsOfClientIdentity($client->getId()),
  101.                 'carriers' => $this->entityManager->getRepository(Entities\Carrier::class)->findAll([], ['name' => 'ASC']),
  102.                 'showProtectedFields' => $this->showProtectedFields($client->getId()),
  103.             )
  104.         );
  105.     }
  106.     public function deleteComment(HttpFoundation\Request $request): HttpFoundation\Response
  107.     {
  108.         $id $request->get('id');
  109.         $commentId $request->get('commentId');
  110.         $client $this->entityManager->find(Entities\Client::class, $id);
  111.         if (!$client) {
  112.             throw new Exception("Invalid client id specified");
  113.         }
  114.         $comment $client->removeCommentById($commentId);
  115.         $this->entityManager->remove($comment);
  116.         $this->entityManager->flush();
  117.         return new HttpFoundation\Response();
  118.     }
  119.     public function loadApplications(HttpFoundation\Request $request)
  120.     {
  121.         $id $request->get('id', -1);
  122.         $client $this->entityManager->find(Entities\Client::class, $id);
  123.         if (!$client) {
  124.             throw new Exception("Invalid client id specified");
  125.         }
  126.         return $this->render(
  127.             'clients/client/applications.html.twig',
  128.             array(
  129.                 'applications' => $this->entityManager->getRepository(Entities\Application::class)->findBy(['client' => $client], ['effectivedate' => 'desc']),
  130.                 'agent' => $this->entityManager->getRepository(Entities\Agent::class)->find($this->get('session')->get('idagent')),
  131.                 'isAdmin' => $this->get('session')->get('adminlevel') == 0,
  132.             )
  133.         );
  134.         return new HttpFoundation\Response();
  135.     }
  136.     public function checkForDuplicate(HttpFoundation\Request $request): HttpFoundation\Response
  137.     {
  138.         $id $request->get('id', -1);
  139.         $firstName $request->get('firstName');
  140.         $lastName $request->get('lastName');
  141.         $clients $this->entityManager->getRepository(Entities\Client::class)
  142.             ->findBy([
  143.                 'firstName' => $firstName,
  144.                 'lastName' => $lastName,
  145.             ]);
  146.         $hasDuplicate false;
  147.         foreach ($clients as $client) {
  148.             if ($client->getId() == $id) {
  149.                 continue;
  150.             }
  151.             $hasDuplicate true;
  152.             break;
  153.         }
  154.         return $this->json([
  155.             'hasDuplicate' => $hasDuplicate,
  156.             'canOverride' => $this->get('session')->get('adminlevel') == 0
  157.         ]);
  158.     }
  159.     protected function setClient(Entities\Client &$client$request)
  160.     {
  161.         $firstName $request->get('firstName');
  162.         $middle $request->get('middle');
  163.         $lastName $request->get('lastName');
  164.         $email $request->get('email');
  165.         $suffix $request->get('suffix');
  166.         $primaryPhone $request->get('primaryPhone');
  167.         $altPhone $request->get('altPhone');
  168.         $dateOfBirth $request->get('dateOfBirth');
  169.         $dateOfDeath $request->get('dateOfDeath');
  170.         $medicareNumber $request->get('medicareNumber'null);
  171.         $medicaidNumber $request->get('medicaidNumber'null);
  172.         $medicareAEffectiveDate $request->get('medicareAEffectiveDate');
  173.         $medicareBEffectiveDate $request->get('medicareBEffectiveDate');
  174.         $social $request->get('social');
  175.         $smoker $request->get('smoker');
  176.         $gender $request->get('gender');
  177.         $street $request->get('street');
  178.         $city $request->get('city');
  179.         $state $request->get('state');
  180.         $zip $request->get('zip');
  181.         $poBoxStreet $request->get('poBoxStreet');
  182.         $poBoxCity $request->get('poBoxCity');
  183.         $poBoxState $request->get('poBoxState');
  184.         $poBoxZip $request->get('poBoxZip');
  185.         $spouseName $request->get('spouseName');
  186.         $spouseDateOfBirth $request->get('spouseDateOfBirth');
  187.         $spouseSmoker $request->get('spouseSmoker'0);
  188.         $comments $request->get('comments');
  189.         if (!$firstName) {
  190.             $this->addFlash('error''You must specify a first name');
  191.             return false;
  192.         } else if (!$lastName) {
  193.             $this->addFlash('error''You must specify a last name');
  194.             return false;
  195.         } else if (!$dateOfBirth) {
  196.             $this->addFlash('error''You must specify a date of birth');
  197.             return false;
  198.         }
  199.         $client->setFirstName($firstName)
  200.             ->setMiddle($middle)
  201.             ->setLastName($lastName)
  202.             ->setEmail($email)
  203.             ->setSuffix($suffix)
  204.             ->setDateOfBirth(new DateTime($dateOfBirth))
  205.             ->setDateOfDeath(empty($dateOfDeath) ? null : new DateTime($dateOfDeath))
  206.             ->setMedicareAEffectiveDate(empty($medicareAEffectiveDate) ? null : new DateTime($medicareAEffectiveDate))
  207.             ->setMedicareBEffectiveDate(empty($medicareBEffectiveDate) ? null : new DateTime($medicareBEffectiveDate))
  208.             ->setMedicareNumber($medicareNumber)
  209.             ->setMedicaidNumber($medicaidNumber)
  210.             ->setSmoker($smoker == 'null' null $smoker)
  211.             ->setGender($gender == 'null' '' $gender)
  212.             ->setStreet($street)
  213.             ->setCity($city)
  214.             ->setState($state)
  215.             ->setZip($zip)
  216.             ->setPoBoxStreet($poBoxStreet)
  217.             ->setPoBoxCity($poBoxCity)
  218.             ->setPoBoxState($poBoxState)
  219.             ->setPoBoxZip($poBoxZip)
  220.             ->setSpouseName($spouseName)
  221.             ->setSpouseDateOfBirth(empty($spouseDateOfBirth) ? null : new DateTime($spouseDateOfBirth))
  222.             ->setSpouseSmoker($spouseSmoker);
  223.         // Agents can only set this when the client is initially created but
  224.         // after that they cannot modify it.
  225.         if ($this->showProtectedFields($client->getId())) {
  226.             $client->setPrimaryPhone($primaryPhone)
  227.                 ->setAltPhone($altPhone)
  228.                 ->setSocial($social);
  229.         }
  230.         if (!empty($comments)) {
  231.             $comment = new Entities\ClientComment($client$comments$this->get('session')->get('userstamp'));
  232.             $client->addComment($comment);
  233.         }
  234.         try {
  235.             $this->entityManager->persist($client);
  236.             $this->entityManager->flush();
  237.         } catch (Exception $e) {
  238.             $this->addFlash('error''Unable to save client: ' $e->getMessage());
  239.             return false;
  240.         }
  241.         $this->addFlash('success''Client saved successfully');
  242.         return true;
  243.     }
  244.     protected function lookupCounty(?string $city, ?string $state): string
  245.     {
  246.         if (empty($city) || empty($state)) {
  247.             return '';
  248.         }
  249.         $result $this->connection->executeQuery(
  250.             'SELECT DISTINCT county FROM county_areas WHERE city = :city AND state = :state ORDER BY county',
  251.             ['city' => $city'state' => $state]
  252.         );
  253.         return implode(' / 'array_column($result->fetchAllAssociative(), 'county'));
  254.     }
  255.     protected function showProtectedFields($idclient): bool
  256.     {
  257.         if (empty($idclient)) {
  258.             return true;
  259.         }
  260.         return $this->isGranted(ClientVoter::UPDATE$idclient);
  261.     }
  262. }