/**
  * Retrieve TCI+ or LI+ quote.
  */
 public function retrieveQuoteAction()
 {
     $this->_setBreadcrumbs(array('/' => 'Home', '/my-homelet' => 'My HomeLet', '/my-homelet/retrieve-quote' => 'Retrieve Quote'));
     $params = Zend_Registry::get('params');
     $form = new Account_Form_RetrieveQuote();
     $quoteManager = new Manager_Insurance_LegacyQuote();
     $customerManager = new Manager_Core_Customer();
     // If there's a quote number in the GET vars then sanitise and uppercase it, and place it in the form
     if (isset($_GET['number'])) {
         // Sanitise and uppercase supplied quote number, place in form
         $quoteNumber = strtoupper(preg_replace('/[^\\w\\/]/', '', $_GET['number']));
         $form->quote_number->setValue($quoteNumber);
         // Also pre-populate the form with first name and last name if the quote number is valid
         $quote = $quoteManager->getQuoteByPolicyNumber($quoteNumber);
         if ($quote) {
             // Get customer details from quote refNo
             $quoteRefNo = $quote->refNo;
             $customer = $customerManager->getCustomer(Model_Core_Customer::LEGACY_IDENTIFIER, $quoteRefNo);
             if ($customer) {
                 $form->first_name->setValue($customer->getFirstName());
                 $form->last_name->setValue($customer->getLastName());
             }
         }
     }
     // Get email address, place in form if it looks valid
     if (isset($_GET['email'])) {
         $getEmail = $_GET['email'];
         $emailValidator = new Zend_Validate_EmailAddress();
         if ($emailValidator->isValid($getEmail)) {
             $form->email->setValue($getEmail);
         }
     }
     $request = $this->getRequest();
     $postData = $request->getPost();
     // Handle retrieve attempts
     if ($request->isPost()) {
         if ($form->isValid($postData)) {
             // Are we looking up by quote number or by e-mail address?  If a quote number is present it takes
             // precedence
             $quotes = array();
             $customer = null;
             $quoteNumber = $form->quote_number->getValue();
             $email = $form->email->getValue();
             if ('' != $quoteNumber) {
                 $quote = $quoteManager->getQuoteByPolicyNumber($quoteNumber);
                 if ($quote) {
                     // Look up customer from quote retrieved
                     $quoteRefNo = $quote->refNo;
                     $customer = $customerManager->getCustomer(Model_Core_Customer::LEGACY_IDENTIFIER, $quoteRefNo);
                     $quotes = array($quote);
                 }
             } else {
                 // Get all legacy quote IDs by customer e-mail address
                 $legacyIDs = array();
                 // Try to look up a customer record's quotes' IDs by the e-mail provided
                 $newCustomer = $customerManager->getCustomerByEmailAddress($email);
                 if ($newCustomer) {
                     $legacyCustomerMap = new Datasource_Core_CustomerMaps();
                     $legacyIDs = $legacyCustomerMap->getLegacyIDs($newCustomer->getIdentifier(Model_Core_Customer::IDENTIFIER));
                 }
                 // Also check in the legacy DB only to ensure landlords quotes are found
                 $customer = $customerManager->getLegacyCustomerByEmailAddress($email);
                 if ($customer) {
                     $legacyCustomerId = $customer->getIdentifier(Model_Core_Customer::LEGACY_IDENTIFIER);
                     if (!in_array($legacyCustomerId, $legacyIDs)) {
                         $legacyIDs[] = $legacyCustomerId;
                     }
                 }
                 // Retrieve all quotes for the linked customer reference numbers
                 $quoteDatasource = new Datasource_Insurance_LegacyQuotes();
                 $quotes = $quoteDatasource->getActiveQuotes($legacyIDs, '', array('policynumber', 'startdate'));
             }
             // Do we have at least one quote and the customer details
             if (count($quotes) > 0 && $customer) {
                 // Check that the security requirements are met (matching email, first name, last name, postcode and
                 // DOB)
                 if (trim($customer->getEmailAddress()) == trim($form->email->getValue()) && Application_Core_Utilities::simplifiedStringCompare($customer->getFirstName(), $form->first_name->getValue()) && Application_Core_Utilities::simplifiedStringCompare($customer->getLastName(), $form->last_name->getValue()) && Application_Core_Utilities::simplifiedStringCompare($customer->getPostCode(), $form->cor_postcode->getValue()) && Application_Core_Utilities::mysqlDateToUk($customer->getDateOfBirthAt()) == trim($form->date_of_birth_at->getValue())) {
                     // If this is a single quote then generate an auth token and bounce them on
                     if (count($quotes) == 1) {
                         // Ensure there's a quote number to use in the security token (because there won't be one if
                         // it was a single match based only on an e-mail address)
                         if ('' == $quoteNumber) {
                             $quoteNumber = $quotes[0]->policyNumber;
                         }
                         // Generate an authentication token for a single policy
                         $securityManager = new Application_Core_Security($params->myhomelet->retrieveWithoutAccount->macSecret, $params->myhomelet->retrieveWithoutAccount->macTimestampVariance != 0, $params->myhomelet->retrieveWithoutAccount->macTimestampVariance);
                         $securityData = array('quoteNumber' => $quoteNumber);
                         $authToken = $securityManager->generate($securityData);
                         // Bounce to the right Q&B depending on quote type
                         if ($quotes[0]->getProductName() == 'tenants') {
                             $this->_helper->redirector->gotoUrl('/tenants/insurance-quote/retrieve?auth=' . $authToken);
                             return;
                         } elseif ($quotes[0]->getProductName() == 'landlords') {
                             $this->_helper->redirector->gotoUrl('/landlords/insurance-quote/retrieve?auth=' . $authToken);
                             return;
                         } else {
                             $form->setDescription('Sorry, we don\'t yet allow resuming the type of quote you have - please call us.');
                         }
                     } else {
                         // Generate an authentication token for the customer email
                         $securityManager = new Application_Core_Security($params->myhomelet->retrieveWithoutAccount->macSecret, $params->myhomelet->retrieveWithoutAccount->macTimestampVariance != 0, $params->myhomelet->retrieveWithoutAccount->macTimestampVariance);
                         $securityData = array('customerEmail' => $email);
                         $authToken = $securityManager->generate($securityData);
                         $this->_helper->redirector->gotoUrl('/my-homelet/retrieve-multiple-quotes?auth=' . $authToken);
                         return;
                     }
                 } else {
                     // Security check failed, show error
                     $form->setDescription('Sorry, we could not find your quote from the details provided - please check them and try again.');
                 }
             } else {
                 // Lookup failed, show error
                 $form->setDescription('Sorry, we could not find your quote from the details provided - please check them and try again.');
             }
         }
     }
     $this->view->form = $form;
 }