/**
  * Show the HomeLet Verify page - used to be CheckRight
  */
 public function homeletVerifyAction()
 {
     $agent = new Datasource_Core_AgentUser();
     $canDisplayCheckRight = $agent->canDisplayCheckRight($this->_agentSchemeNumber, $this->_agentUserName);
     if (!$canDisplayCheckRight) {
         throw new Zend_Controller_Action_Exception('Page not found', 404);
     }
     $this->view->phoneNumber = $this->_params->connect->settings->checkRight->phoneNumber;
     $this->view->blogUrl = $this->_params->connect->settings->checkRight->blogUrl;
     $this->view->faqsUrl = $this->_params->connect->settings->checkRight->faqsUrl;
     $this->view->homeOfficeUrl = $this->_params->connect->settings->checkRight->homeOfficeUrl;
     $this->view->checkUrl = $this->_params->connect->settings->checkRight->checkUrl;
 }
 /**
  * Index action
  *
  * @return void
  */
 public function indexAction()
 {
     /** @var \Desarrolla2\Cache\Cache $cache */
     $cache = $this->getContainer()->get('iris.notifications_cache');
     $cacheKey = sha1($this->_agentSchemeNumber . 'notifications');
     // Get notifications (from cache is available)
     if ($cache->has($cacheKey)) {
         $reportNotifications = $cache->get($cacheKey);
     } else {
         try {
             $reportNotifications = $this->getIrisAgentContext()->getReferencingApplicationClient()->getReportNotifications();
         } catch (DefaultException $e) {
             $reportNotifications = array();
         }
         $cache->set($cacheKey, $reportNotifications);
     }
     $agent = new Datasource_Core_AgentUser();
     $canDisplayCheckRight = $agent->canDisplayCheckRight($this->_agentSchemeNumber, $this->_agentUserName);
     $this->renderTwigView('/iris-referencing/index.html.twig', array('rentAffordabilityForm' => $this->_getRentAffordabilityForm()->createView(), 'reportNotifications' => $reportNotifications, 'canDisplayCheckRight' => $canDisplayCheckRight, 'agentSchemeNumber' => $this->_agentSchemeNumber, 'disabledNewReferenceList' => array(1508996, 1506765, 1507004, 1507005, 1507006, 1507007, 1507008, 1507009, 1507010, 1507011, 1507012, 1507013, 1507014, 1508250, 1509271, 1510027, 1512889, 1507015, 1507016, 1508284, 1513311, 1513353, 1513354, 1513355, 1513356, 1513357, 1513358, 1513359, 1513360, 1513361)));
 }
 /**
  * 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');
     }
 }