/**
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  * @throws \Exception
  */
 public function quoteAction(Request $request)
 {
     $siteRequest = $this->get('sudoux.cms.site');
     $site = $siteRequest->getSite();
     $em = $this->getDoctrine()->getEntityManager();
     $lead = new Lead();
     $form = $this->createForm(new QuoteType($site), $lead);
     $showReferralSources = false;
     $referralSourceCount = $em->getRepository('SudouxCmsFormBundle:ReferralSource')->findAllActiveBySiteCount($site);
     if ($referralSourceCount > 0) {
         $showReferralSources = true;
     }
     if ($request->getMethod() == 'POST') {
         $form->bindRequest($request);
         if ($form->isValid()) {
             try {
                 $lead->setSite($site);
                 $status = $em->getRepository('SudouxCmsFormBundle:LeadStatus')->find($this->container->getParameter('default_lead_status'));
                 $lead->setLeadStatus($status);
                 $em->persist($lead);
                 $em->flush();
                 $this->get('session')->setFlash('success', 'Thank you for your submission');
                 $emailUtil = $this->get('sudoux.cms.message.email_util');
                 // lead confirmation
                 $email = new Email();
                 $email->setSubject(sprintf("Thank you for contacting %s.", $site->getName()));
                 $email->setMessage("Thank you for your submission. We will respond to your inquiry as soon as possible.");
                 $email->setRecipient($lead->getEmail());
                 $email->setSite($site);
                 $emailUtil->logAndSend($email);
                 // admin notification
                 $message = sprintf('First Name: %s<br />', $form['first_name']->getData());
                 $message .= sprintf('Last Name: %s<br />', $form['last_name']->getData());
                 $message .= sprintf('Home Phone: %s<br />', $form['home_phone']->getData());
                 $message .= sprintf('Email: %s<br />', $form['email']->getData());
                 $message .= sprintf('Loan Type: %s<br />', $form['subject']->getData());
                 $message .= sprintf('Loan Details: %s<br />', $form['message']->getData());
                 $referralSources = $lead->getReferralSourceAsString();
                 if (!empty($referralSources)) {
                     $message .= sprintf('Referral Sources: %s<br />', $referralSources);
                     $referralSourceDesc = $lead->getReferralSourceDesc();
                     if (!empty($referralSourceDesc)) {
                         $message .= sprintf('Referral Source Description:<br /> %s<br />', $referralSourceDesc);
                     }
                 }
                 $email = new Email();
                 $email->setSubject(sprintf('%s: Quote Form', $site->getName()));
                 $email->setMessage($message);
                 $email->setRecipient($site->getSettings()->getInheritedWebsiteEmail());
                 $email->setRecipientName($site->getName());
                 $email->setBcc($site->getSettings()->getInheritedWebsiteEmailBcc());
                 $email->setSite($site);
                 $emailUtil->logAndSend($email);
                 return $this->redirect($this->generateUrl('sudoux_mortgage_form_quote'));
             } catch (\Exception $e) {
                 $logger = $this->get('logger');
                 $logger->crit($e->getMessage());
                 throw $e;
             }
         }
     }
     return $this->render('SudouxMortgageBundle:FormFront:quote.html.twig', array('form' => $form->createView(), 'showReferralSources' => $showReferralSources));
 }
 /**
  * @param Request $request
  * @param $id
  * @return Response
  */
 public function applyStep7Action(Request $request, $id)
 {
     $securityContext = $this->container->get('security.context');
     $user = $securityContext->getToken()->getUser();
     $site = $this->get('sudoux.cms.site')->getSite();
     $em = $this->getDoctrine()->getEntityManager();
     $application = $em->getRepository('SudouxMortgageBundle:LoanApplication')->findOneBy(array('id' => $id, 'user' => $user));
     if (!isset($application)) {
         throw $this->createNotFoundException($this::LOAN_NOT_FOUND_MESSAGE);
     }
     $session = $request->getSession();
     if ($application->getLockStatus() > 0) {
         $session->getFlashBag()->add('error', $this::LOAN_LOCKED_MESSAGE);
         return $this->redirect($this->generateUrl('sudoux_mortgage_loan_member_detail', array('id' => $id)));
     }
     if (!$application->validate()) {
         $session->getFlashBag()->add('error', 'You must fill out all co-borrower information before you can submit the application.');
         return $this->redirect($this->generateUrl('sudoux_mortgage_loan_apply_step6', array('id' => $application->getId())));
     }
     $form = $this->createForm(new LoanApplicationType($site, $application), $application, array('validation_groups' => array('step7')));
     if ($request->getMethod() == 'POST') {
         $form->bindRequest($request);
         if ($form->isValid()) {
             try {
                 $application->setCompleted(true);
                 $application->setStatus(1);
                 $emailUtil = $this->get('sudoux.cms.message.email_util');
                 // @todo - add this to settings
                 $email = new Email();
                 $email->setSubject('Loan Application Complete');
                 $email->setMessage("Thank you for completing your loan application. We will contact you shortly.");
                 $email->setRecipient($application->getBorrower()->getEmail());
                 $email->setRecipientName($application->getBorrower()->getFullName());
                 $email->setSite($site);
                 $emailUtil->logAndSend($email);
                 $application->addEmail($email);
                 $email = new Email();
                 $email->setSubject('A borrower has completed a loan application');
                 $message = sprintf("<p>A borrower has completed a loan application. <a href=\"%s\">Click here to view</a>.</p>", $this->generateUrl('sudoux_mortgage_admin_loan_member', array('id' => $application->getId()), true));
                 $propertyLocation = $application->getPropertyLocation();
                 if (isset($propertyLocation)) {
                     $message .= sprintf("<p>Property Address:<br> %s</p>", $application->getPropertyLocation()->getAddressString());
                 }
                 $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);
                 $em->persist($application);
                 $em->flush();
                 // check and send data to the LOS
                 $job = new Job('sudoux:mortgage:loan', array('upsert_loan_to_los', 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()->getInheritedLoanCompleteUrl();
                 if (isset($redirectUrl)) {
                     if ($this->get('kernel')->getEnvironment() == 'dev') {
                         $redirectUrl = '/app_dev.php' . $redirectUrl;
                     }
                 } else {
                     $redirectUrl = $this->generateUrl('sudoux_mortgage_apply_complete');
                 }
                 return $this->redirect($redirectUrl);
             } catch (\Exception $e) {
                 $session->getFlashBag()->add('error', 'Sorry, there was an issue processing your request. Our support team has been notified and will update you as soon as the issue has been resolved.');
                 $logger = $this->get('logger');
                 $logger->crit(" Application ID: " . $application->getId() . ' ' . $e->getMessage());
             }
         }
     }
     return $this->render('SudouxMortgageBundle:LoanApplicationFront:applyStep7.html.twig', array('form' => $form->createView(), 'application' => $application));
 }