/**
  * @param Request $request
  * @param null $id
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  * @throws \Exception
  */
 public function applyStep1Action(Request $request, $id = null)
 {
     $securityContext = $this->container->get('security.context');
     if (!$securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
         $session = $request->getSession();
         $session->getFlashBag()->add('error', $this::NOT_REGISTERED_MESSAGE);
         return $this->redirect($this->generateUrl('sudoux_cms_member_register'));
     }
     $siteRequest = $this->get('sudoux.cms.site');
     $site = $siteRequest->getSite();
     $em = $this->getDoctrine()->getEntityManager();
     $user = $securityContext->getToken()->getUser();
     $newApplication = true;
     if (isset($id)) {
         $application = $em->getRepository('SudouxMortgageBundle:LoanApplication')->findOneBy(array('id' => $id, 'user' => $user));
         if (!isset($application)) {
             throw $this->createNotFoundException($this::LOAN_NOT_FOUND_MESSAGE);
         }
         if ($application->getLockStatus() > 0) {
             $session = $request->getSession();
             $session->getFlashBag()->add('error', $this::LOAN_LOCKED_MESSAGE);
             return $this->redirect($this->generateUrl('sudoux_mortgage_loan_member_detail', array('id' => $id)));
         }
         $newApplication = false;
     } else {
         $application = new LoanApplication();
         $siteLoanOfficer = $site->getSettings()->getLoanOfficer();
         if (isset($siteLoanOfficer)) {
             $application->setLoanOfficer($siteLoanOfficer);
         }
     }
     if ($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
         // autofill user info
         $application->getBorrower()->setEmail($user->getEmail());
     }
     $showReferralSources = false;
     $referralSourceCount = $em->getRepository('SudouxCmsFormBundle:ReferralSource')->findAllActiveBySiteCount($site);
     if ($referralSourceCount > 0) {
         $showReferralSources = true;
     }
     $form = $this->createForm(new LoanApplicationType($site, $application), $application, array('validation_groups' => array('step1')));
     if ($request->getMethod() == 'POST') {
         $form->bindRequest($request);
         $this->validateChildEntity($application, $form, 'borrower');
         if ($form->isValid()) {
             try {
                 $application->setSite($site);
                 $application->setUser($user);
                 if ($newApplication) {
                     $application->setLastStepCompleted(1);
                     $em->persist($application);
                     $em->flush();
                     // set the defaults because the constructor is not called on prototype forms
                     $coBorrowers = $application->getCoBorrower();
                     if (count($coBorrowers) > 0) {
                         foreach ($coBorrowers as $coBorrower) {
                             $coBorrower->setDefaults();
                         }
                     }
                     $email = new Email();
                     $email->setSubject('A borrower has started a loan application');
                     $message = sprintf("A borrower has started a loan 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</p>', $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->setRecipient($notificationEmail);
                     $email->setBcc($site->getSettings()->getInheritedWebsiteEmailBcc());
                     $emailUtil = $this->get('sudoux.cms.message.email_util');
                     $emailUtil->logAndSend($email);
                 } else {
                     $em->persist($application);
                     $em->flush();
                 }
                 return $this->redirect($this->generateUrl('sudoux_mortgage_loan_apply_step2', array('id' => $application->getId())));
             } catch (\Exception $e) {
                 $logger = $this->get('logger');
                 $logger->crit($e->getMessage());
                 throw $e;
             }
         }
     }
     return $this->render('SudouxMortgageBundle:LoanApplicationFront:applyStep1.html.twig', array('form' => $form->createView(), 'application' => $application, 'showReferralSources' => $showReferralSources));
 }
 /**
  * @param Request $request
  * @param null $id
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  * @throws \Exception
  */
 public function applyStep1Action(Request $request, $id = null)
 {
     $siteRequest = $this->get('sudoux.cms.site');
     $site = $siteRequest->getSite();
     $em = $this->getDoctrine()->getEntityManager();
     $securityContext = $this->container->get('security.context');
     $user = $securityContext->getToken()->getUser();
     $newApplication = true;
     if (isset($id)) {
         $application = $em->getRepository('SudouxMortgageBundle:LoanApplication')->findOneBySite($site, $id);
         if (!isset($application)) {
             throw $this->createNotFoundException($this::LOAN_NOT_FOUND_MESSAGE);
         }
         $newApplication = false;
     } else {
         $application = new LoanApplication();
         $siteLoanOfficer = $site->getSettings()->getLoanOfficer();
         if (isset($siteLoanOfficer)) {
             $application->setLoanOfficer($siteLoanOfficer);
         }
     }
     if ($application->getLockStatus() > 0) {
         $session = $request->getSession();
         $session->getFlashBag()->add('error', $this::LOAN_LOCKED_MESSAGE);
         return $this->redirect($this->generateUrl('sudoux_mortgage_admin_loan_member', array('id' => $id)));
     }
     $showReferralSources = false;
     $referralSourceCount = $em->getRepository('SudouxCmsFormBundle:ReferralSource')->findAllActiveBySiteCount($site);
     if ($referralSourceCount > 0) {
         $showReferralSources = true;
     }
     $form = $this->createForm(new LoanApplicationType($site, $application), $application, array('validation_groups' => array('step1')));
     if ($request->getMethod() == 'POST') {
         $form->bindRequest($request);
         if ($form->isValid()) {
             try {
                 if ($newApplication) {
                     $application->setLastStepCompleted(1);
                     $application->setSite($site);
                     $application->setAdminUser($user);
                     // set the defaults because the constructor is not called on prototype forms
                     $coBorrowers = $application->getCoBorrower();
                     if (count($coBorrowers) > 0) {
                         foreach ($coBorrowers as $coBorrower) {
                             $coBorrower->setDefaults();
                         }
                     }
                 }
                 //echo 'Co-Borrowers: ' . count($application->getCoBorrower()); exit;
                 $em->persist($application);
                 $em->flush();
                 return $this->redirect($this->generateUrl('sudoux_mortgage_admin_loan_step2', array('id' => $application->getId())));
             } catch (\Exception $e) {
                 $logger = $this->get('logger');
                 $logger->crit($e->getMessage());
                 throw $e;
             }
         }
     }
     return $this->render('SudouxMortgageBundle:LoanApplicationAdmin:applyStep1.html.twig', array('form' => $form->createView(), 'application' => $application, 'showReferralSources' => $showReferralSources));
 }
 /**
  * @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));
 }