private function _doMail($data)
 {
     $pageSession = new Zend_Session_Namespace('portfolio_insurance_quote');
     $customerRefNo = $pageSession->CustomerRefNo;
     // Get Customer
     $customerManager = new Manager_Insurance_Portfolio_LegacyCustomer();
     $customerObject = new Model_Insurance_Portfolio_LegacyCustomer();
     $customerObject = $customerManager->fetchByRefNo($customerRefNo);
     // Get Properties
     $propertyManager = new Manager_Insurance_Portfolio_Property();
     $properties = array();
     // Fetch all the properties related to this customer refNo
     $properties = $propertyManager->fetchAllProperties($customerRefNo)->toArray();
     $propertyHtml = $this->view->partialLoop('portfolio-insurance-quote/partials/email-templates/property-details.phtml', $properties);
     // Fetch claims releted to this customer refNo
     $claimsManager = new Manager_Insurance_Portfolio_PreviousClaims();
     $claims = $claimsManager->fetchWithClaimTypes($customerRefNo);
     $claimsHtml = $this->view->partialLoop('portfolio-insurance-quote/partials/email-templates/claims.phtml', $claims);
     // Fetch bank interest related to this customer refNo
     $bankInterestManager = new Manager_Insurance_Portfolio_BankInterest();
     $bankInterest = $bankInterestManager->fetchAllInterests($customerRefNo);
     $bankInterestHtml = $this->view->partialLoop('portfolio-insurance-quote/partials/email-templates/bank-interest.phtml', $bankInterest);
     $uwManager = new Manager_Insurance_Portfolio_UnderwritingAnswers();
     $uwAnswers = $uwManager->fetchByRefNo($customerRefNo);
     // Merge the claim and Interest info into the UW template
     $uwQuestionsHtml = $this->view->partial('portfolio-insurance-quote/partials/email-templates/uw-questions.phtml', array('claimsHtml' => $claimsHtml, 'bankInterestHtml' => $bankInterestHtml, 'uwAnswers' => $uwAnswers->toArray()));
     // Merge all the html together
     $mailBody = $this->view->partial('portfolio-insurance-quote/partials/email-templates/emailQuote.phtml', array('theData' => $data, 'theCustomer' => $customerObject->toArray(), 'propertyHtml' => $propertyHtml, 'uwQuestionsHtml' => $uwQuestionsHtml));
     // Get some parameter stuffs
     $params = Zend_Registry::get('params');
     $emailArray = explode(",", $params->email->portfolioAdmin);
     $toAddress = $emailArray[0];
     $ccAddress = $emailArray[1];
     $fromAddress = $params->email->noreply;
     // Mail that bad boy
     if (isset($data['referred'])) {
         $referred = " - REFERRED";
     }
     $email = new Application_Core_Mail();
     $email->setFrom($fromAddress, "PORTFOLIO NEW BUSINESS {$referred}");
     $email->setTo($toAddress, "Underwriting");
     $email->setCC($ccAddress);
     $email->setSubject("Portfolio Website Query - ref: {$customerRefNo}");
     $email->setBodyHtml($mailBody);
     $email->send();
     return;
 }
 /**
  * RRP Inception Form
  */
 public function rentRecoveryPlusAction()
 {
     // Ensure address lookup JS is available
     $this->view->headScript()->appendFile('/assets/connect/js/rentguaranteeclaims/addressLookup.js');
     // Ensure date picker CSS and JS are available
     $this->view->headLink()->appendStylesheet('/assets/vendor/jquery-datepicker/css/datePicker.css');
     $this->view->headScript()->appendFile('/assets/vendor/jquery-date/js/date.js');
     $this->view->headScript()->appendFile('/assets/vendor/jquery-datepicker/js/jquery.datePicker.js');
     $request = $this->getSymfonyRequest();
     $data = null;
     $policyNumberManager = new Manager_Core_PolicyNumber();
     if ($request->isMethod('GET')) {
         $quoteNumber = $request->get('policyNumber');
         if ($policyNumberManager->isRentRecoveryPlusQuote($quoteNumber)) {
             $applicationDecoratorClass = $this->getContainer()->get('rrp.application.decorator.class');
             /** @var RRP\Application\Decorators\RentRecoveryPlusQuote $quote */
             $quote = $applicationDecoratorClass::getDecorator('RentRecoveryPlusQuote');
             $data = $quote->getApplicationData($quoteNumber);
         }
     }
     $form = $this->_getRentRecoveryPlusApplicationForm($data, array('currentAsn' => $this->_agentSchemeNumber));
     if ($request->isMethod('POST')) {
         $form->submit($request);
         if ($form->isValid()) {
             if ($request->isXmlHttpRequest()) {
                 $update = true;
             } else {
                 $update = $form->get('update')->isClicked();
             }
             if (!$update) {
                 /** @var RRP\Model\RentRecoveryPlusApplication $application */
                 $application = $form->getData();
                 $applicationDecoratorClass = $this->getContainer()->get('rrp.application.decorator.class');
                 /** @var RRP\Application\Decorators\RentRecoveryPlusQuote $quote */
                 $quote = $applicationDecoratorClass::getDecorator('RentRecoveryPlusQuote');
                 $quote->setFromApplication($application);
                 if ($quote->getAppData()->getRefNo() === null) {
                     $customerManager = new Manager_Core_Customer();
                     $sudoEmailAddress = $customerManager->generateAgentSudoEmailAddress($this->_agentSchemeNumber);
                     $customer = $customerManager->getCustomerByEmailAddress($sudoEmailAddress);
                     $isNewCustomer = false;
                     if (!$customer) {
                         $isNewCustomer = true;
                         $customer = $customerManager->createNewCustomer($sudoEmailAddress, Model_Core_Customer::AGENT, true);
                         $customer->setLastName($this->_agentObj->name);
                         if ($this->_agentObj->contact[0]->address->flatNumber) {
                             $line1 = $this->_agentObj->contact[0]->address->flatNumber . ' ';
                         } else {
                             if ($this->_agentObj->contact[0]->address->houseName) {
                                 $line1 = $this->_agentObj->contact[0]->address->houseName . ' ';
                             } else {
                                 if ($this->_agentObj->contact[0]->address->houseNumber) {
                                     $line1 = $this->_agentObj->contact[0]->address->houseNumber . ' ';
                                 } else {
                                     $line1 = '';
                                 }
                             }
                         }
                         if ($this->_agentObj->contact[0]->address->addressLine1 && $this->_agentObj->contact[0]->address->addressLine2) {
                             $line1 .= $this->_agentObj->contact[0]->address->addressLine1 . ', ' . $this->_agentObj->contact[0]->address->addressLine2;
                         } else {
                             if ($this->_agentObj->contact[0]->address->addressLine1) {
                                 $line1 .= $this->_agentObj->contact[0]->address->addressLine1;
                             } else {
                                 if ($this->_agentObj->contact[0]->address->addressLine2) {
                                     $line1 .= $this->_agentObj->contact[0]->address->addressLine2;
                                 }
                             }
                         }
                         $customer->setAddressLine(Model_Core_Customer::ADDRESSLINE1, $line1);
                         $customer->setAddressLine(Model_Core_Customer::ADDRESSLINE2, $this->_agentObj->contact[0]->address->town);
                         $customer->setAddressLine(Model_Core_Customer::ADDRESSLINE3, $this->_agentObj->contact[0]->address->county);
                         $customer->setPostCode($this->_agentObj->contact[0]->address->postCode);
                         $customer->setCountry($this->_agentObj->contact[0]->address->country);
                         $customerManager->updateCustomer($customer);
                     }
                     // Now get the reference number from the newly created customer
                     $refNo = $customer->getIdentifier(Model_Core_Customer::LEGACY_IDENTIFIER);
                     $quote->getAppData()->setRefNo($refNo);
                     if ($isNewCustomer) {
                         $customerManager->updateCustomerAgentSchemeNumber($this->_agentSchemeNumber, $refNo);
                     }
                     if ($application->getPolicyNumber()) {
                         $policyNumber = $application->getPolicyNumber();
                     } else {
                         $policyNumber = $policyNumberManager->generateApplicationNumber(Manager_Core_PolicyNumber::QUOTE_IDENTIFIER);
                     }
                     $quote->setDefaults($policyNumber, $application->getReferenceType(), $application->getIsContinuationOfExistingPolicy(), $application->getPropertyLetType(), $application->getPropertyDeposit(), Model_Insurance_RentRecoveryPlus_LegacyPolicy::STATUS_QUOTE)->getAppData()->setAgentSchemeNumber($this->_agentSchemeNumber)->setUnderwritingQuestionSetID($this->_params->connect->settings->rentRecoveryPlus->underwritingQuestionSetID)->setRiskArea($this->_params->connect->settings->rentRecoveryPlus->riskArea);
                 } else {
                     $policyNumber = $quote->getAppData()->getPolicyNumber();
                 }
                 $this->referralModel->setPolicyNumber($policyNumber);
                 $this->rateManager = $this->initialiseRateManager($application);
                 $quote->setPolicyOptions($application->getPropertyRental(), $this->rateManager->getPremium(), $this->rateManager->getNilExcessOption())->getAppData()->setPremium($this->rateManager->getPremium())->setIpt($this->rateManager->getIpt())->setQuote($this->rateManager->getQuote())->setRateSetID($this->rateManager->getRateSetID());
                 // Does the given reference satisfy the policy criteria for this particular reference?
                 $referralRequired = false;
                 $reference = $this->getReferenceFromSession($application->getReferenceNumber());
                 $referenceSatisfiesCriteria = $this->referenceSatisfiesCriteria($reference);
                 // Does the application satisfy the application criteria?
                 $this->referralModel->setFromApplication($application, $this->rateManager->getPremium());
                 $applicationSatisfiesCriteria = $this->applicationSatisfiesCriteria();
                 if (!$referenceSatisfiesCriteria || !$applicationSatisfiesCriteria) {
                     // Fire a POLICY_REFERRED event to handle all referral related actions.
                     $referredEvent = new ReferredEvent($this->referralModel);
                     $this->eventDispatcher->dispatch(RRPEvents::POLICY_REFERRED, $referredEvent);
                     // Mark this application as referred.
                     $quote->getAppData()->setPayStatus(Model_Insurance_RentRecoveryPlus_LegacyPolicy::PAY_STATUS_REFERRED);
                     $referralRequired = true;
                 }
                 if ($application->getIsContinuationOfExistingPolicy()) {
                     $mailManager = new Application_Core_Mail();
                     if ($application->getIsExistingPolicyToBeCancelled()) {
                         $subject = str_replace('{$existingPolicyRef}', $application->getExistingPolicyRef(), $this->_params->connect->settings->rentRecoveryPlus->cancelExisting->emailSubject);
                         $message = $this->getContainer()->get('twig')->render('rent-recovery-plus-cancel-existing-mail.plain.twig', array('agentName' => $this->_agentObj->name, 'agentSchemeNumber' => $this->_agentSchemeNumber, 'policyNumber' => $policyNumber));
                         $mailManager->setTo($this->_params->connect->settings->rentRecoveryPlus->cancelExisting->emailToAddress, $this->_params->connect->settings->rentRecoveryPlus->cancelExisting->emailToName)->setFrom($this->_params->connect->settings->rentRecoveryPlus->cancelExisting->emailFromAddress, $this->_params->connect->settings->rentRecoveryPlus->cancelExisting->emailFromName)->setSubject($subject)->setBodyText($message);
                     } else {
                         $subject = str_replace('{$existingPolicyRef}', $application->getExistingPolicyRef(), $this->_params->connect->settings->rentRecoveryPlus->cancelExisting->emailSubject);
                         $message = $this->getContainer()->get('twig')->render('rent-recovery-plus-keep-existing-mail.plain.twig', array('agentName' => $this->_agentObj->name, 'agentSchemeNumber' => $this->_agentSchemeNumber, 'existingPolicyRef' => $application->getExistingPolicyRef()));
                         $mailManager->setTo($this->_params->connect->settings->rentRecoveryPlus->keepExisting->emailToAddress, $this->_params->connect->settings->rentRecoveryPlus->keepExisting->emailToName)->setFrom($this->_params->connect->settings->rentRecoveryPlus->keepExisting->emailFromAddress, $this->_params->connect->settings->rentRecoveryPlus->keepExisting->emailFromName)->setSubject($subject)->setBodyText($message);
                         if (isset($this->_params->connect->settings->rentRecoveryPlus->keepExisting->emailCcAddress)) {
                             $mailManager->setCC($this->_params->connect->settings->rentRecoveryPlus->keepExisting->emailCcAddress);
                         }
                     }
                     $mailManager->send();
                 }
                 if (!$quote->save()) {
                     return $this->renderTwigView('/rentguarantee/rent-recovery-plus-error.html.twig');
                 }
                 if ($referralRequired) {
                     // Render the referral text to the user.
                     return $this->renderTwigView('/rentguarantee/rent-recovery-plus-referral.html.twig', array('policyNumber' => $policyNumber));
                 }
                 if ($application->getIsPayMonthly()) {
                     $paymentDetails = sprintf('This will appear on your invoices as %d monthly payments of' . ' £%.02f plus £%.02f (IPT at %d%%). Total monthly payment £%.02f. ', $application->getPolicyLength(), $this->rateManager->getPremium(), $this->rateManager->getIpt(), $this->rateManager->getIptPercent(), $this->rateManager->getQuote());
                 } else {
                     $paymentDetails = sprintf('This will appear on your next invoices as £%.02f plus £%.02f (IPT at %d%%). Total £%.02f. ', $this->rateManager->getPremium(), $this->rateManager->getIpt(), $this->rateManager->getIptPercent(), $this->rateManager->getQuote());
                 }
                 return $this->renderTwigView('/rentguarantee/rent-recovery-plus-quote.html.twig', array('policyNumber' => $policyNumber, 'paymentDetails' => $paymentDetails));
             }
         }
     }
     if ($this->getRequest()->isXmlHttpRequest()) {
         $this->_helper->viewRenderer->setNoRender(true);
         $this->_helper->layout->disableLayout();
     }
     $agent = new Datasource_Core_AgentUser();
     if ($agent->canDisplayRRPI($this->_agentSchemeNumber, $this->_agentUserName)) {
         $this->renderTwigView('/rentguarantee/rent-recovery-plus-application.html.twig', array('form' => $form->createView()));
     } else {
         $this->renderTwigView('/rentguarantee/rent-recovery-plus-information.html.twig');
     }
 }