/**
  * Initialise the step 3 form [Important Information Form]
  *
  * @return void
  */
 public function step3Action()
 {
     $pageForm = new TenantsInsuranceQuoteB_Form_Step3();
     // Tell page to use AJAX validation as we go
     $this->view->headScript()->appendScript('var ajaxValidate = true; var ajaxValidatePage = 3;');
     // Get customer details
     $customerManager = new Manager_Core_Customer();
     $customer = $customerManager->getCustomer(Model_Core_Customer::LEGACY_IDENTIFIER, $this->_customerReferenceNumber);
     // Hydrate registration form
     if (isset($pageForm->subform_register) || isset($pageForm->subform_login)) {
         // Grab a new customer to populate the form
         $pageSession = new Zend_Session_Namespace('tenants_insurance_quote');
         $newCust = $customerManager->getCustomer(Model_Core_Customer::IDENTIFIER, $pageSession->CustomerID);
         if (isset($pageForm->subform_register)) {
             if ($newCust) {
                 $pageForm->subform_register->email->setValue($newCust->getEmailAddress());
                 $pageForm->subform_register->security_question->setValue($newCust->getSecurityQuestion());
                 $pageForm->subform_register->security_answer->setValue($newCust->getSecurityAnswer());
                 $emailAddress = $newCust->getEmailAddress();
             } else {
                 $pageForm->subform_register->email->setValue($customer->getEmailAddress());
                 $emailAddress = $customer->getEmailAddress();
             }
             if (!$emailAddress) {
                 $emailAddress = $newCust->getEmailAddress();
             }
         } else {
             if ($newCust) {
                 $pageForm->subform_login->email->setValue($newCust->getEmailAddress());
             }
         }
     }
     if ($this->getRequest()->isPost()) {
         $valid = $this->_formStepCommonValidate($pageForm, 3);
         if (isset($pageForm->subform_register)) {
             $pageForm->subform_register->getElement('email')->setValue($emailAddress);
         }
         if ($valid) {
             $pageSession = new Zend_Session_Namespace('tenants_insurance_quote');
             $pageSession->IsNewCustomer = false;
             $data = $pageForm->getValues();
             //Update the WebLead summary and create a STEP3 blob.
             $webLeadManager = new Manager_Core_WebLead();
             $webLeadSummary = $webLeadManager->getSummary($this->_webLeadSummaryId);
             $webLeadSummary->lastUpdatedTime = $this->_offsetDate();
             $webLeadSummary->promotionCode = $data["subform_howhear"]['campaign_code'];
             $webLeadManager->updateSummary($webLeadSummary);
             //Determine if a new STEP3 blob needs to be created, or an existing one retrieved.
             if ($webLeadManager->getBlobExists($this->_webLeadSummaryId, Model_Core_WebLeadStep::STEP3)) {
                 $webLeadBlob = $webLeadManager->getBlob($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP3);
             } else {
                 $webLeadBlob = $webLeadManager->createNewBlob($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP3);
             }
             //Update the blob and store
             $webLeadBlob->blob = Zend_Json::encode($_POST);
             $webLeadBlob->blobChecksum = crc32($webLeadBlob->blob);
             $webLeadManager->updateBlob($webLeadBlob);
             // Instantiate the quote manager
             $quoteManager = new Manager_Insurance_TenantsContentsPlus_Quote(null, null, $this->_policyNumber);
             // Save new ASN if there is one
             // Create a postcode model
             $postcode = new Manager_Core_Postcode();
             // Get the address as array for Insured and correspondance address
             $insuredAddress = $postcode->getPropertyByID($data['subform_insuredaddress']['ins_address'], false);
             $correspondenceAddress = $postcode->getPropertyByID($data['subform_correspondencedetails']['cor_address'], false);
             // Update the property address in the quote
             $quoteManager->setPropertyAddress(($insuredAddress['organisation'] != '' ? "{$insuredAddress['organisation']}, " : '') . ($insuredAddress['buildingName'] != '' ? "{$insuredAddress['buildingName']}, " : '') . ($insuredAddress['houseNumber'] != '' ? "{$insuredAddress['houseNumber']} " : '') . $insuredAddress['address2'], $insuredAddress['address4'], $insuredAddress['address5'], $insuredAddress['postcode']);
             // Update start and end dates
             $startDate = $data['subform_policydetails']['policy_start'];
             $startDate = substr($startDate, 6, 4) . '-' . substr($startDate, 3, 2) . '-' . substr($startDate, 0, 2);
             $endDate = date('Y-m-d', strtotime(date('Y-m-d', strtotime($startDate)) . ' +1 year -1 day'));
             $quoteManager->setStartAndEndDates($startDate, $endDate);
             //Update the customer in the DataStore and the LegacyDataStore. Use the CustomerManager
             //to do this.
             //$customerManager = new Manager_Core_Customer();
             //First get the existing customer details.
             //$customer = $customerManager->getCustomer(Model_Core_Customer::LEGACY_IDENTIFIER, $this->_customerReferenceNumber);
             //Now modify the details.
             $customer->setAddressLine(Model_Core_Customer::ADDRESSLINE1, ($correspondenceAddress['organisation'] != '' ? "{$correspondenceAddress['organisation']}, " : '') . ($correspondenceAddress['houseNumber'] != '' ? "{$correspondenceAddress['houseNumber']} " : '') . ($correspondenceAddress['buildingName'] != '' ? "{$correspondenceAddress['buildingName']}, " : '') . $correspondenceAddress['address2']);
             $customer->setAddressLine(Model_Core_Customer::ADDRESSLINE2, $correspondenceAddress['address4']);
             $customer->setAddressLine(Model_Core_Customer::ADDRESSLINE3, $correspondenceAddress['address5']);
             $customer->setPostCode($correspondenceAddress['postcode']);
             $customer->setDateOfBirthAt(Application_Core_Utilities::ukDateToMysql($pageSession->CustomerDob));
             //Finally, save the details back to both DataStores.
             $customerManager->updateCustomer($customer);
             $premiums = $quoteManager->calculatePremiums();
             // Save MI information - how did you hear about us
             $marketQuestion = new Manager_Core_ManagementInformation();
             $marketQuestion->saveMarketingAnswers($this->_policyNumber, $this->_customerReferenceNumber, $data["subform_howhear"]["how_hear"]);
             // Perform login/register procedure
             $auth = Zend_Auth::getInstance();
             $auth->setStorage(new Zend_Auth_Storage_Session('homelet_customer'));
             if (isset($data['subform_register'])) {
                 // Process registration
                 $params = Zend_Registry::get('params');
                 $newCustomer = $customerManager->getCustomerByEmailAddress($data['subform_register']['email']);
                 if (!$newCustomer) {
                     $newCustomer = $customerManager->createCustomerFromLegacy($data['subform_register']['email'], $this->_customerReferenceNumber);
                 }
                 // Update customer with password and security data
                 $newCustomer->setTitle($customer->getTitle());
                 $newCustomer->setFirstName($customer->getFirstName());
                 $newCustomer->setLastName($customer->getLastName());
                 $newCustomer->setAddressLine(Model_Core_Customer::ADDRESSLINE1, ($correspondenceAddress['organisation'] != '' ? "{$correspondenceAddress['organisation']}, " : '') . ($correspondenceAddress['houseNumber'] != '' ? "{$correspondenceAddress['houseNumber']} " : '') . ($correspondenceAddress['buildingName'] != '' ? "{$correspondenceAddress['buildingName']}, " : '') . $correspondenceAddress['address2']);
                 $newCustomer->setAddressLine(Model_Core_Customer::ADDRESSLINE2, $correspondenceAddress['address4']);
                 $newCustomer->setAddressLine(Model_Core_Customer::ADDRESSLINE3, $correspondenceAddress['address5']);
                 $newCustomer->setPostCode($correspondenceAddress['postcode']);
                 $newCustomer->setDateOfBirthAt(Application_Core_Utilities::ukDateToMysql($pageSession->CustomerDob));
                 //                    assuming that the email is already set and so won't require setting again.
                 //                    $newCustomer->setEmailAddress($data['subform_register']['email']);
                 $newCustomer->setSecurityQuestion($data['subform_register']['security_question']);
                 $newCustomer->setSecurityAnswer($data['subform_register']['security_answer']);
                 $newCustomer->setPassword($data['subform_register']['password']);
                 $newCustomer->setAccountLoadComplete(true);
                 $newCustomer->typeID = Model_Core_Customer::CUSTOMER;
                 $customerManager->updateCustomer($newCustomer);
                 // Create sign-up completion email
                 $mail = new Application_Core_Mail();
                 $mail->setTo($data['subform_register']['email'], null);
                 $mail->setFrom('*****@*****.**', 'HomeLet');
                 $mail->setSubject('My HomeLet account validation');
                 //  Generate activation link
                 $mac = new Application_Core_Security($params->myhomelet->activation_mac_secret, false);
                 $digest = $mac->generate(array('email' => $data['subform_register']['email']));
                 $activationLink = 'email=' . $data['subform_register']['email'] . '&' . 'mac=' . $digest;
                 // Apply template
                 $mail->applyTemplate('core/account-validation', array('activationLink' => $activationLink, 'homeletWebsite' => $params->homelet->domain, 'firstname' => $newCustomer->getFirstName(), 'templateId' => 'HL2442 12-12', 'heading' => 'Validating your My HomeLet account'), false, '/email-branding/homelet/portal-footer.phtml', '/email-branding/homelet/portal-header.phtml');
                 $mail->applyTextTemplate('core/account-validationtxt', array('activationLink' => $activationLink, 'homeletWebsite' => $params->homelet->domain, 'firstname' => $newCustomer->getFirstName(), 'templateId' => 'HL2442 12-12', 'heading' => 'Validating your My HomeLet account'), false, '/email-branding/homelet/portal-footer-txt.phtml', '/email-branding/homelet/portal-header-txt.phtml');
                 // Send email
                 $mail->send();
                 // Everything has been saved ok so navigate to next step
                 $this->_formStepCommonNavigate(3);
             } elseif ($auth->hasIdentity()) {
                 $this->_formStepCommonNavigate(3);
             }
             //return;
         } elseif (isset($_POST['back'])) {
             $this->_formStepCommonNavigate(3);
             return;
         }
     }
     // Load the element data from the database if we can
     if ($this->_formStepCommonPopulate($pageForm, 3)) {
         // Render the page unless we have been redirected
         $this->view->form = $pageForm;
         $this->render('step');
     }
 }
 /**
  * Initialise the step 2 form
  *
  * @return void
  */
 public function step2Action()
 {
     $pageForm = new TenantsInsuranceQuote_Form_Step2();
     // Tell page to use AJAX validation as we go
     $this->view->headScript()->appendScript('var ajaxValidate = true; var ajaxValidatePage = 2;');
     //Update the WebLead summary and create a STEP2 blob.
     $webLeadManager = new Manager_Core_WebLead();
     $webLeadSummary = $webLeadManager->getSummary($this->_webLeadSummaryId);
     $this->view->qhli = $webLeadSummary->quoteNumber;
     if ($this->getRequest()->isPost()) {
         $valid = $this->_formStepCommonValidate($pageForm, 2);
         if ($valid) {
             $data = $pageForm->getValues();
             $webLeadSummary->lastUpdatedTime = $this->_offsetDate();
             $webLeadManager->updateSummary($webLeadSummary);
             //Determine if a new STEP2 blob needs to be created, or an existing one retrieved.
             if ($webLeadManager->getBlobExists($this->_webLeadSummaryId, Model_Core_WebLeadStep::STEP2)) {
                 $webLeadBlob = $webLeadManager->getBlob($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP2);
             } else {
                 $webLeadBlob = $webLeadManager->createNewBlob($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP2);
             }
             //Update the blob and store
             $webLeadBlob->blob = Zend_Json::encode($_POST);
             $webLeadBlob->blobChecksum = crc32($webLeadBlob->blob);
             $webLeadManager->updateBlob($webLeadBlob);
             // Instantiate the quote manager
             $quoteManager = new Manager_Insurance_TenantsContentsPlus_Quote(null, null, $this->_policyNumber);
             // Update start and end dates
             $startDate = $data['subform_policydetails']['policy_start'];
             $startDate = substr($startDate, 6, 4) . '-' . substr($startDate, 3, 2) . '-' . substr($startDate, 0, 2);
             $endDate = date('Y-m-d', strtotime(date('Y-m-d', strtotime($startDate)) . ' +1 year -1 day'));
             $quoteManager->setStartAndEndDates($startDate, $endDate);
             // Get sub forms that were originally on step 1
             $subFormContentsInsurance = $pageForm->getSubForm('subform_contentsinsurance');
             $subFormSharers = $pageForm->getSubForm('subform_sharers');
             // Set the contents SI - this does an automatic save
             if (is_numeric($subFormContentsInsurance->getElement('contents_cover_a')->getValue())) {
                 $contentsSI = $subFormContentsInsurance->getElement('contents_cover_a')->getValue();
             } else {
                 $contentsSI = $subFormContentsInsurance->getElement('contents_cover_b')->getValue();
             }
             $quoteManager->setCoverAmount($contentsSI, Manager_Insurance_TenantsContentsPlus_Quote::CONTENTS);
             //Save the sharer details.
             $contentsSICurrency = new Zend_Currency(array('value' => $contentsSI, 'precision' => 2));
             $sharersManager = new Manager_Insurance_TenantsContentsPlus_Sharers();
             $noOfSharersAllowed = $sharersManager->getNoOfSharersAllowed($contentsSICurrency);
             switch ($noOfSharersAllowed) {
                 case 1:
                     $sharers = new Model_Insurance_TenantsContentsPlus_Sharers($this->_policyNumber);
                     $sharers->setSharerOccupation(Model_Insurance_TenantsContentsPlus_Sharers::SHARER_01, $subFormSharers->getElement('policy_sharer1_occupation')->getValue());
                     $sharersManager->insertSharers($sharers);
                     break;
                 case 2:
                     $sharers = new Model_Insurance_TenantsContentsPlus_Sharers($this->_policyNumber);
                     $sharers->setSharerOccupation(Model_Insurance_TenantsContentsPlus_Sharers::SHARER_01, $subFormSharers->getElement('policy_sharer1_occupation')->getValue());
                     $sharers->setSharerOccupation(Model_Insurance_TenantsContentsPlus_Sharers::SHARER_02, $subFormSharers->getElement('policy_sharer2_occupation')->getValue());
                     $sharersManager->insertSharers($sharers);
                     break;
             }
             // Update the unspecified possessions SI
             $unspecPossessionsSI = $data['subform_possessions']['possessions_cover'];
             $unspecPossessionsSI = is_numeric($unspecPossessionsSI) ? $unspecPossessionsSI : 0;
             $quoteManager->setCoverAmount($unspecPossessionsSI, Manager_Insurance_TenantsContentsPlus_Quote::UNSPECIFIEDPOSSESSIONS);
             // Everything has been saved ok so navigate to next step
             $this->_formStepCommonNavigate(2);
             return;
         } elseif (isset($_POST['back'])) {
             $this->_formStepCommonNavigate(2);
             return;
         }
     }
     // Load the element data from the database if we can
     if ($this->_formStepCommonPopulate($pageForm, 2)) {
         // Render the page unless we have been redirected
         $this->view->form = $pageForm;
         $this->render('step');
     }
 }