/**
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  * @throws \Exception
  */
 public function prequalifyAction(Request $request)
 {
     $securityContext = $this->container->get('security.context');
     $user = $securityContext->getToken()->getUser();
     $siteRequest = $this->get('sudoux.cms.site');
     $site = $siteRequest->getSite();
     $em = $this->getDoctrine()->getEntityManager();
     $officers = $em->getRepository('SudouxMortgageBundle:LoanOfficer')->findBy(array('site' => $site, 'active' => true));
     $application = new LoanApplication();
     $application->setIsPrequal(true);
     $application->setSite($site);
     $siteLoanOfficer = $site->getSettings()->getLoanOfficer();
     if (isset($siteLoanOfficer)) {
         $application->setLoanOfficer($siteLoanOfficer);
     }
     if ($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
         // autofill user info
         $application->getBorrower()->setEmail($user->getEmail());
         $application->setUser($user);
     }
     $showReferralSources = false;
     $referralSourceCount = $em->getRepository('SudouxCmsFormBundle:ReferralSource')->findAllActiveBySiteCount($site);
     if ($referralSourceCount > 0) {
         $showReferralSources = true;
     }
     $form = $this->createForm(new LoanApplicationType($site), $application, array('validation_groups' => array('prequalify')));
     if ($request->getMethod() == 'POST') {
         $form->bindRequest($request);
         if ($form->isValid()) {
             try {
                 $em->persist($application);
                 $em->flush();
                 $emailUtil = $this->get('sudoux.cms.message.email_util');
                 // notify the borrrower
                 $email = new Email();
                 $email->setSubject('Pre-qualification Application Complete');
                 $email->setMessage($this::PREQUAL_SUCCESS_MESSAGE);
                 $email->setRecipient($application->getBorrower()->getEmail());
                 $email->setRecipientName($application->getBorrower()->getFullName());
                 $email->setSite($site);
                 $emailUtil->logAndSend($email);
                 // notifiy the site admin
                 $email = new Email();
                 $email->setSubject('A borrower has completed a pre-qualification application');
                 $message = sprintf("A borrower has completed a pre-qualification application. <a href=\"%s\">Click here</a> to view the application.", $this->generateUrl('sudoux_mortgage_admin_loan_step1', array('id' => $application->getId()), true));
                 $message .= sprintf('<p>Borrower: %s<br/>', $application->getBorrower()->getFullName());
                 $message .= sprintf('Email: %s<br/>', $application->getBorrower()->getEmail());
                 $message .= sprintf('Phone: %s</p>', $application->getBorrower()->getPhoneHome());
                 if ($application->getCoBorrower()->count() > 0) {
                     $coBorrower = $application->getCoBorrower()->get(0);
                     $message .= sprintf('<p>Co-Borrower: %s<br/>', $coBorrower->getFullName());
                     $message .= sprintf('Email: %s<br/>', $coBorrower->getEmail());
                     $message .= sprintf('Phone: %s</p>', $coBorrower->getPhoneHome());
                 }
                 $email->setMessage($message);
                 $email->setSite($site);
                 // notify the site admin or lo
                 $loanOfficer = $application->getLoanOfficer();
                 if (isset($loanOfficer)) {
                     $notificationEmail = $loanOfficer->getEmail();
                     $email->setRecipientName($loanOfficer->getFullName());
                 } else {
                     $notificationEmail = $site->getSettings()->getInheritedWebsiteEmail();
                     $email->setRecipientName('Site Administrator');
                 }
                 $email->setBcc($site->getSettings()->getInheritedWebsiteEmailBcc());
                 $email->setRecipient($notificationEmail);
                 $emailUtil->logAndSend($email);
                 $job = new Job('sudoux:mortgage:loan', array('add_loan_prospects', sprintf('--loan_id=%s', $application->getId()), '--env=' . $this->get('kernel')->getEnvironment(), '--no-debug'), true, 'loan_process_queue');
                 $em->persist($job);
                 $em->flush();
                 $redirectUrl = $site->getSettings()->getInheritedPrequalCompleteUrl();
                 if (isset($redirectUrl)) {
                     if ($this->get('kernel')->getEnvironment() == 'dev') {
                         $redirectUrl = '/app_dev.php' . $redirectUrl;
                     }
                 } else {
                     $redirectUrl = $this->generateUrl('sudoux_mortgage_loan_prequalify_complete');
                 }
                 return $this->redirect($redirectUrl);
             } catch (\Exception $e) {
                 $logger = $this->get('logger');
                 $logger->crit($e->getMessage());
             }
         }
     }
     return $this->render('SudouxMortgageBundle:LoanApplicationFront:prequalify.html.twig', array('form' => $form->createView(), 'application' => $application, 'officers' => $officers, 'showReferralSources' => $showReferralSources));
 }