private function saveStep1(Zend_Form $pageForm)
 {
     // Get sub forms
     $subFormPersonalDetails = $pageForm->getSubForm('subform_personaldetails');
     $subFormDataProtection = $pageForm->getSubForm('subform_dataprotection');
     $subFormContentsInsurance = $pageForm->getSubForm('subform_contentsinsurance');
     $subFormSharers = $pageForm->getSubForm('subform_sharers');
     $pageSession = new Zend_Session_Namespace('tenants_insurance_quote');
     $customerManager = new Manager_Core_Customer();
     $auth = Zend_Auth::getInstance();
     $auth->setStorage(new Zend_Auth_Storage_Session('homelet_customer'));
     // TODO: This should check reference number exists, not just property!
     if (!isset($this->_customerReferenceNumber)) {
         // Do we already have a legacy customer with this email address?
         $customer = $customerManager->getCustomerByEmailAddress($subFormPersonalDetails->getElement('email_address')->getValue());
         if (!$customer) {
             // We don't have a session so we need to create a legacy customer/quote entry to save against
             // All new customers are now of type Model_Core_Customer::CUSTOMER
             $customer = $customerManager->createNewCustomer($subFormPersonalDetails->getElement('email_address')->getValue(), Model_Core_Customer::CUSTOMER);
             $legacyCustomer = $customer;
             if (!isset($pageSession->IsNewCustomer)) {
                 $pageSession->IsNewCustomer = true;
             }
         } else {
             if (!isset($pageSession->IsNewCustomer)) {
                 $pageSession->IsNewCustomer = false;
             }
             $legacyCustomer = $customerManager->createNewCustomer($subFormPersonalDetails->getElement('email_address')->getValue(), Model_Core_Customer::CUSTOMER, true);
         }
         $customerManager->linkLegacyToNew($legacyCustomer->getIdentifier(Model_Core_Customer::LEGACY_IDENTIFIER), $customer->getIdentifier(Model_Core_Customer::IDENTIFIER));
         // Now get the reference number from the newly created customer
         $customerRefNo = $customer->getIdentifier(Model_Core_Customer::LEGACY_IDENTIFIER);
         $this->_customerReferenceNumber = $customerRefNo;
         $pageSession->CustomerRefNo = $customerRefNo;
         $pageSession->CustomerID = $customer->getIdentifier(Model_Core_Customer::IDENTIFIER);
     } else {
         // We are in session so just instantiate the customer manager with the existing reference number
         $customer = $customerManager->getCustomer(Model_Core_Customer::LEGACY_IDENTIFIER, $this->_customerReferenceNumber);
         $customerRefNo = $pageSession->CustomerRefNo;
         $pageSession->CustomerID = $customer->getIdentifier(Model_Core_Customer::IDENTIFIER);
         if (!isset($pageSession->IsNewCustomer)) {
             $pageSession->IsNewCustomer = false;
         }
     }
     $pageSession->CustomerEmail = $subFormPersonalDetails->getElement('email_address')->getValue();
     $pageSession->CustomerDob = $subFormPersonalDetails->getElement('date_of_birth_at')->getValue();
     if (!$auth->hasIdentity()) {
         // Update the customer record with the form data, but only if they are a new customer
         // Existing customers must phone us.
         // Note that no message stating this is given to the end user
         if ($subFormPersonalDetails->getElement('title')->getValue() != "Other") {
             $customer->setTitle($subFormPersonalDetails->getElement('title')->getValue());
         } else {
             $customer->setTitle($subFormPersonalDetails->getElement('other_title')->getValue());
         }
         $customer->setFirstName($subFormPersonalDetails->getElement('first_name')->getValue());
         $customer->setLastName($subFormPersonalDetails->getElement('last_name')->getValue());
         $customer->setTelephone(Model_Core_Customer::TELEPHONE1, $subFormPersonalDetails->getElement('phone_number')->getValue());
         $customer->setTelephone(Model_Core_Customer::TELEPHONE2, $subFormPersonalDetails->getElement('mobile_number')->getValue());
         $customer->setEmailAddress($subFormPersonalDetails->getElement('email_address')->getValue());
         $customer->setDateOfBirthAt(Application_Core_Utilities::ukDateToMysql($subFormPersonalDetails->getElement('date_of_birth_at')->getValue()));
         $customer->setIsForeignAddress(false);
     }
     // TODO: Make this all better...
     $customerManager->updateCustomer($customer);
     // Customer is logged in and starting a new quote - so we need to pre-populate the customers details from stored details
     $customerID = $auth->getStorage()->read()->id;
     $customerManager->linkLegacyToNew($customerRefNo, $customerID);
     // See if we have an agent scheme number, if so use it
     $session = new Zend_Session_Namespace('homelet_global');
     $agentSchemeNumber = $session->agentSchemeNumber;
     // Instantiate the quote manager - if this is a new customer it will automatically create a new quote
     // otherwise it will use the existing quote
     $quoteManager = new Manager_Insurance_TenantsContentsPlus_Quote($customerRefNo, $agentSchemeNumber, $pageSession->PolicyNumber);
     // 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);
     // Grab the policy number for the quote and save it to the session
     $policyNumber = $quoteManager->getPolicyNumber();
     $pageSession = new Zend_Session_Namespace('tenants_insurance_quote');
     $pageSession->PolicyNumber = $policyNumber;
     $this->_policyNumber = $policyNumber;
     // Set the contact preferences
     $contactPreferences = new Model_Core_CustomerContactPreferences();
     $contactPreferences->addPreference(Model_Core_CustomerContactPreferences::EMAIL_METHOD);
     $quoteManager->setContactPreference($contactPreferences);
     $quoteManager->setIssueDate(Zend_Date::now()->toString('YYYY-MM-dd'));
     //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;
     }
     //Record this WebLead, if not already done so. First create or
     //retrieve the WebLead summary.
     $webLeadManager = new Manager_Core_WebLead();
     if (empty($this->_webLeadSummaryId)) {
         $isNewWebLead = true;
         $webLeadSummary = $webLeadManager->createNewSummary();
     } else {
         $isNewWebLead = false;
         $webLeadSummary = $webLeadManager->getSummary($this->_webLeadSummaryId);
     }
     //Create or retrieve the step 1 blob.
     if (!$webLeadManager->getBlobExists($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP1)) {
         $webLeadBlob = $webLeadManager->createNewBlob($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP1);
     } else {
         $webLeadBlob = $webLeadManager->getBlob($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP1);
     }
     //Update the WebLead summary and store.
     $webLeadSummary->title = $subFormPersonalDetails->getElement('title')->getValue();
     $webLeadSummary->firstName = $subFormPersonalDetails->getElement('first_name')->getValue();
     $webLeadSummary->lastName = $subFormPersonalDetails->getElement('last_name')->getValue();
     if ($subFormPersonalDetails->getElement('phone_number')->getValue() != '') {
         $contactNumber = $subFormPersonalDetails->getElement('phone_number')->getValue();
     } else {
         $contactNumber = $subFormPersonalDetails->getElement('mobile_number')->getValue();
     }
     $webLeadSummary->contactNumber = $contactNumber;
     $webLeadSummary->emailAddress = $subFormPersonalDetails->getElement('email_address')->getValue();
     if ($isNewWebLead) {
         $webLeadSummary->startTime = $this->_offsetDate();
     }
     $webLeadSummary->lastUpdatedTime = $this->_offsetDate();
     $webLeadSummary->product = Model_Core_WebLeadProduct::TCIPLUS;
     $webLeadSummary->quoteNumber = $pageSession->PolicyNumber;
     $webLeadManager->updateSummary($webLeadSummary);
     //Update the blob, serialize and store.
     $webLeadBlob->blob = Zend_Json::encode($_POST);
     $webLeadBlob->blobChecksum = crc32($webLeadBlob->blob);
     $webLeadManager->updateBlob($webLeadBlob);
     //Record the WebLead identifier in the page session.
     $pageSession->webLeadSummaryId = $webLeadSummary->webLeadSummaryId;
     //Capture and store the insurance and WebLead data protections.
     $this->_saveDPA($customerRefNo, Model_Core_DataProtection_ItemEntityTypes::INSURANCE, $subFormDataProtection->getElement('dpa_phone_post')->getValue(), $subFormDataProtection->getElement('dpa_sms_email')->getValue(), $subFormDataProtection->getElement('dpa_resale')->getValue());
     $this->_saveDPA($webLeadSummary->webLeadSummaryId, Model_Core_DataProtection_ItemEntityTypes::WEBLEAD, $subFormDataProtection->getElement('dpa_phone_post')->getValue(), $subFormDataProtection->getElement('dpa_sms_email')->getValue(), $subFormDataProtection->getElement('dpa_resale')->getValue());
 }
 private function saveStep1(Zend_Form $pageForm)
 {
     // Get sub forms
     $subFormPersonalDetails = $pageForm->getSubForm('subform_personaldetails');
     $subFormInsuredAddress = $pageForm->getSubForm('subform_insuredaddress');
     $subFormCorrespondenceDetails = $pageForm->getSubForm('subform_correspondencedetails');
     $subFormDataProtection = $pageForm->getSubForm('subform_dataprotection');
     $pageSession = new Zend_Session_Namespace('tenants_insurance_quote');
     $customerManager = new Manager_Core_Customer();
     $auth = Zend_Auth::getInstance();
     $auth->setStorage(new Zend_Auth_Storage_Session('homelet_customer'));
     // TODO: This should check reference number exists, not just property!
     if (!isset($this->_customerReferenceNumber)) {
         // Do we already have a legacy customer with this email address?
         $customer = $customerManager->getCustomerByEmailAddress($subFormPersonalDetails->getElement('email_address')->getValue());
         if (!$customer) {
             // We don't have a session so we need to create a legacy customer/quote entry to save against
             // All new customers are now of type Model_Core_Customer::CUSTOMER
             $customer = $customerManager->createNewCustomer($subFormPersonalDetails->getElement('email_address')->getValue(), Model_Core_Customer::CUSTOMER);
             $legacyCustomer = $customer;
             if (!isset($pageSession->IsNewCustomer)) {
                 $pageSession->IsNewCustomer = true;
             }
         } else {
             if (!isset($pageSession->IsNewCustomer)) {
                 $pageSession->IsNewCustomer = false;
             }
             $legacyCustomer = $customerManager->createNewCustomer($subFormPersonalDetails->getElement('email_address')->getValue(), Model_Core_Customer::CUSTOMER, true);
         }
         $customerManager->linkLegacyToNew($legacyCustomer->getIdentifier(Model_Core_Customer::LEGACY_IDENTIFIER), $customer->getIdentifier(Model_Core_Customer::IDENTIFIER));
         // Now get the reference number from the newly created customer
         $customerRefNo = $customer->getIdentifier(Model_Core_Customer::LEGACY_IDENTIFIER);
         $this->_customerReferenceNumber = $customerRefNo;
         $pageSession->CustomerRefNo = $customerRefNo;
         $pageSession->CustomerID = $customer->getIdentifier(Model_Core_Customer::IDENTIFIER);
     } else {
         // We are in session so just instantiate the customer manager with the existing reference number
         $customer = $customerManager->getCustomer(Model_Core_Customer::LEGACY_IDENTIFIER, $this->_customerReferenceNumber);
         $customerRefNo = $pageSession->CustomerRefNo;
         $pageSession->CustomerID = $customer->getIdentifier(Model_Core_Customer::IDENTIFIER);
         if (!isset($pageSession->IsNewCustomer)) {
             $pageSession->IsNewCustomer = false;
         }
     }
     $pageSession->CustomerEmail = $subFormPersonalDetails->getElement('email_address')->getValue();
     $pageSession->CustomerDob = $subFormPersonalDetails->getElement('date_of_birth_at')->getValue();
     if (!$auth->hasIdentity()) {
         // Update the customer record with the form data, but only if they are a new customer
         // Existing customers must phone us.
         // Note that no message stating this is given to the end user
         if ($subFormPersonalDetails->getElement('title')->getValue() != "Other") {
             $customer->setTitle($subFormPersonalDetails->getElement('title')->getValue());
         } else {
             $customer->setTitle($subFormPersonalDetails->getElement('other_title')->getValue());
         }
         $customer->setFirstName($subFormPersonalDetails->getElement('first_name')->getValue());
         $customer->setLastName($subFormPersonalDetails->getElement('last_name')->getValue());
         $customer->setTelephone(Model_Core_Customer::TELEPHONE1, $subFormPersonalDetails->getElement('phone_number')->getValue());
         $customer->setTelephone(Model_Core_Customer::TELEPHONE2, $subFormPersonalDetails->getElement('mobile_number')->getValue());
         $customer->setEmailAddress($subFormPersonalDetails->getElement('email_address')->getValue());
         $customer->setIsForeignAddress(false);
     }
     // Whether a new customer or an existing customer without a DoB stored, allow the DoB supplied in the form to be
     // stored if what's there now is blank
     if (null === $customer->getDateOfBirthAt() || '0000-00-00' == $customer->getDateOfBirthAt()) {
         $customer->setDateOfBirthAt(Application_Core_Utilities::ukDateToMysql($subFormPersonalDetails->getElement('date_of_birth_at')->getValue()));
     }
     // TODO: Make this all better...
     $customerManager->updateCustomer($customer);
     // Customer is logged in and starting a new quote - so we need to pre-populate the customers details from stored details
     $customerID = $auth->getStorage()->read()->id;
     $customerManager->linkLegacyToNew($customerRefNo, $customerID);
     // See if we have an agent scheme number, if so use it
     $session = new Zend_Session_Namespace('homelet_global');
     $agentSchemeNumber = $session->agentSchemeNumber;
     // Instantiate the quote manager - if this is a new customer it will automatically create a new quote
     // otherwise it will use the existing quote
     $quoteManager = new Manager_Insurance_TenantsContentsPlus_Quote($customerRefNo, $agentSchemeNumber, $pageSession->PolicyNumber);
     // Grab the policy number for the quote and save it to the session
     $policyNumber = $quoteManager->getPolicyNumber();
     $pageSession = new Zend_Session_Namespace('tenants_insurance_quote');
     $pageSession->PolicyNumber = $policyNumber;
     $this->_policyNumber = $policyNumber;
     // Save the addresses
     // Create a postcode model
     $postcode = new Manager_Core_Postcode();
     // Get the addresses as arrays for insured and correspondence addresses
     $insuredAddressId = $subFormInsuredAddress->getElement('ins_address')->getValue();
     $insuredAddress = $postcode->getPropertyByID($insuredAddressId, false);
     $correspondenceAddressId = $subFormCorrespondenceDetails->getElement('cor_address')->getValue();
     $correspondenceAddress = $postcode->getPropertyByID($correspondenceAddressId, 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 the customer address in the customer record
     $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']);
     // Save the customer record details
     $customerManager->updateCustomer($customer);
     // Set the contact preferences
     $contactPreferences = new Model_Core_CustomerContactPreferences();
     $contactPreferences->addPreference(Model_Core_CustomerContactPreferences::EMAIL_METHOD);
     $quoteManager->setContactPreference($contactPreferences);
     $quoteManager->setIssueDate(Zend_Date::now()->toString('YYYY-MM-dd'));
     //Record this WebLead, if not already done so. First create or
     //retrieve the WebLead summary.
     $webLeadManager = new Manager_Core_WebLead();
     if (empty($this->_webLeadSummaryId)) {
         $isNewWebLead = true;
         $webLeadSummary = $webLeadManager->createNewSummary();
     } else {
         $isNewWebLead = false;
         $webLeadSummary = $webLeadManager->getSummary($this->_webLeadSummaryId);
     }
     //Create or retrieve the step 1 blob.
     if (!$webLeadManager->getBlobExists($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP1)) {
         $webLeadBlob = $webLeadManager->createNewBlob($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP1);
     } else {
         $webLeadBlob = $webLeadManager->getBlob($webLeadSummary->webLeadSummaryId, Model_Core_WebLeadStep::STEP1);
     }
     //Update the WebLead summary and store.
     $webLeadSummary->title = $subFormPersonalDetails->getElement('title')->getValue();
     $webLeadSummary->firstName = $subFormPersonalDetails->getElement('first_name')->getValue();
     $webLeadSummary->lastName = $subFormPersonalDetails->getElement('last_name')->getValue();
     if ($subFormPersonalDetails->getElement('phone_number')->getValue() != '') {
         $contactNumber = $subFormPersonalDetails->getElement('phone_number')->getValue();
     } else {
         $contactNumber = $subFormPersonalDetails->getElement('mobile_number')->getValue();
     }
     $webLeadSummary->contactNumber = $contactNumber;
     $webLeadSummary->emailAddress = $subFormPersonalDetails->getElement('email_address')->getValue();
     if ($isNewWebLead) {
         $webLeadSummary->startTime = $this->_offsetDate();
     }
     $webLeadSummary->lastUpdatedTime = $this->_offsetDate();
     $webLeadSummary->product = Model_Core_WebLeadProduct::TCIPLUS;
     $webLeadSummary->quoteNumber = $pageSession->PolicyNumber;
     $webLeadManager->updateSummary($webLeadSummary);
     //Update the blob, serialize and store.
     $webLeadBlob->blob = Zend_Json::encode($_POST);
     $webLeadBlob->blobChecksum = crc32($webLeadBlob->blob);
     $webLeadManager->updateBlob($webLeadBlob);
     //Record the WebLead identifier in the page session.
     $pageSession->webLeadSummaryId = $webLeadSummary->webLeadSummaryId;
     //Capture and store the insurance and WebLead data protections.
     $this->_saveDPA($customerRefNo, Model_Core_DataProtection_ItemEntityTypes::INSURANCE, $subFormDataProtection->getElement('dpa_phone_post')->getValue(), $subFormDataProtection->getElement('dpa_sms_email')->getValue(), $subFormDataProtection->getElement('dpa_resale')->getValue());
     $this->_saveDPA($webLeadSummary->webLeadSummaryId, Model_Core_DataProtection_ItemEntityTypes::WEBLEAD, $subFormDataProtection->getElement('dpa_phone_post')->getValue(), $subFormDataProtection->getElement('dpa_sms_email')->getValue(), $subFormDataProtection->getElement('dpa_resale')->getValue());
 }