public function saveData()
 {
     $session = new Zend_Session_Namespace('referencing_global');
     $data = $this->getValues();
     $referenceManager = new Manager_Referencing_Reference();
     $reference = $referenceManager->getReference($session->referenceId);
     //Record the reference subject's personal details.
     if (empty($reference->referenceSubject->name)) {
         //If here then things are a bit weird - we should have the reference subject name
         //captured from an earlier form.
         $nameManager = new Manager_Core_Name();
         $reference->referenceSubject->name = $nameManager->createName();
     }
     $reference->referenceSubject->name->title = $data['personal_title'];
     $reference->referenceSubject->name->firstName = $data['first_name'];
     $reference->referenceSubject->name->middleName = $data['middle_name'];
     $reference->referenceSubject->name->lastName = $data['last_name'];
     $reference->referenceSubject->name->maidenName = $data['other_name'];
     //Reference subject contact details
     if (empty($reference->referenceSubject->contactDetails)) {
         $contactDetailsManager = new Manager_Core_ContactDetails();
         $reference->referenceSubject->contactDetails = $contactDetailsManager->createContactDetails();
     }
     $reference->referenceSubject->contactDetails->telephone1 = $data['telephone_day'];
     $reference->referenceSubject->contactDetails->telephone2 = $data['mobile_number'];
     $reference->referenceSubject->contactDetails->email1 = $data['email'];
     //Reference subject miscellaneous.
     $reference->referenceSubject->dob = new Zend_Date($data['tenancy_start_date'], Zend_Date::DATES);
     if ('Yes' == $data['has_adverse_credit']) {
         $reference->referenceSubject->hasAdverseCredit = true;
     } else {
         $reference->referenceSubject->hasAdverseCredit = false;
     }
     //Bank account details. Bank account details are optional.
     if (empty($data['bank_account_number']) || empty($data['bank_sortcode_number'])) {
         //No bank account details have been provided.
         if (!empty($reference->referenceSubject->bankAccount)) {
             //We have an existing bank account details record - delete this to reflect the
             //user input.
             $bankAccountManager = new Manager_Referencing_BankAccount();
             $bankAccountManager->deleteBankAccount($reference->referenceSubject->bankAccount);
         }
     } else {
         if (empty($reference->referenceSubject->bankAccount)) {
             $bankAccountManager = new Manager_Referencing_BankAccount();
             $reference->referenceSubject->bankAccount = $bankAccountManager->insertPlaceholder($session->referenceId);
         }
         $reference->referenceSubject->bankAccount->accountNumber = $data['bank_account_number'];
         $reference->referenceSubject->bankAccount->sortCode = $data['bank_sortcode_number'];
         //Run the bank account details through the validators.
         $reference->referenceSubject->bankAccount->isValidated = false;
         $bankManager = new Manager_Core_Bank();
         if ($bankManager->isSortCodeValid($data['bank_sortcode_number'])) {
             if ($bankManager->isAccountNumberValid($data['bank_sortcode_number'], $data['bank_account_number'])) {
                 $reference->referenceSubject->bankAccount->isValidated = true;
             }
         }
     }
     //Create a current residence record, if not already done so. Ensure the current residence record
     //reflects the residential status provided by the user.
     if (empty($reference->referenceSubject->residences)) {
         $residenceManager = new Manager_Referencing_Residence();
         $residence = $residenceManager->insertPlaceholder($session->referenceId, Model_Referencing_ResidenceChronology::CURRENT);
         $residence->status = $data['residential_status'];
         $reference->referenceSubject->residences = array();
         $reference->referenceSubject->residences[] = $residence;
     } else {
         //Locate the current residence record, and set the residential status accordingly.
         foreach ($reference->referenceSubject->residences as $residence) {
             if ($residence->chronology == Model_Referencing_ResidenceChronology::CURRENT) {
                 $residence->status = $data['residential_status'];
                 break;
             }
         }
     }
     //Create or locate the current occupation record.
     $occupationManager = new Manager_Referencing_Occupation();
     if (empty($reference->referenceSubject->occupations)) {
         $isNew = true;
         $currentOccupation = $occupationManager->createNewOccupation($session->referenceId, Model_Referencing_OccupationChronology::CURRENT, Model_Referencing_OccupationImportance::FIRST);
     } else {
         $isNew = false;
         //Locate the current primary occupation record, and set the occupation type accordingly.
         $currentOccupation = $occupationManager->findSpecificOccupation($reference->referenceSubject->occupations, Model_Referencing_OccupationChronology::CURRENT, Model_Referencing_OccupationImportance::FIRST);
     }
     //Update the current occupation record to reflect the user inputs.
     $currentOccupation->type = $data['occupational_type'];
     $currentOccupation->income = new Zend_Currency(array('value' => $data['total_annual_income'], 'precision' => 0));
     if ($data['occupation_will_change'] == 'No') {
         $currentOccupation->isPermanent = true;
     } else {
         $currentOccupation->isPermanent = false;
     }
     //Add the current occupation to the ReferenceSubject, if it is new.
     if ($isNew) {
         $reference->referenceSubject->occupations = array();
         $reference->referenceSubject->occupations[] = $currentOccupation;
     }
     //Identify if a future occupation record is required.
     if (isset($data['is_future_employment_secured'])) {
         if ('Yes' == $data['is_future_employment_secured']) {
             //See if a future occupation record exists already.
             $futureOccupation = $occupationManager->findSpecificOccupation($reference->referenceSubject->occupations, Model_Referencing_OccupationChronology::FUTURE, Model_Referencing_OccupationImportance::FIRST);
             if (empty($futureOccupation)) {
                 $createFutureOccupation = true;
             } else {
                 $createFutureOccupation = false;
             }
             if ($createFutureOccupation) {
                 $futureOccupation = $occupationManager->createNewOccupation($session->referenceId, Model_Referencing_OccupationChronology::FUTURE, Model_Referencing_OccupationImportance::FIRST);
                 $futureOccupation->type = Model_Referencing_OccupationTypes::EMPLOYMENT;
                 $reference->referenceSubject->occupations[] = $futureOccupation;
             }
         } else {
             //No future occupation record is required at this time, so ensure that
             //any existing are deleted.
             $futureOccupation = $occupationManager->findSpecificOccupation($reference->referenceSubject->occupations, Model_Referencing_OccupationChronology::FUTURE, Model_Referencing_OccupationImportance::FIRST);
             if (!empty($futureOccupation)) {
                 $occupationManager->deleteOccupation($futureOccupation);
             }
         }
     }
     //Write the updates to the datasources.
     $referenceManager->updateReference($reference);
 }
 /**
  * Controller for the direct debit payment page
  *
  * @return void
  */
 public function ddAction()
 {
     $pageForm = new LandlordsInsuranceQuote_Form_BankConfirmation();
     $pageSession = new Zend_Session_Namespace('landlords_insurance_quote');
     // Tell page NOT to use AJAX validation as we go
     $this->view->headScript()->appendScript('var ajaxValidate = false; var ajaxValidatePage = \'dd\';');
     // Look up bank details to show in view
     $bankManager = new Manager_Core_Bank();
     $this->view->branchDetails = $bankManager->getBranchDetail($pageSession->paymentSelectionDetails['bank_sortcode_number']);
     // Drop the branch address lines into an array for accessing on the
     // front-end.
     $this->view->branchLines = array($this->view->branchDetails->bankNameFull, $this->view->branchDetails->addressLine1, $this->view->branchDetails->addressLine2, $this->view->branchDetails->addressLine3, $this->view->branchDetails->addressLine4, $this->view->branchDetails->town, $this->view->branchDetails->county, $this->view->branchDetails->postCode);
     if ($this->getRequest()->isPost()) {
         $valid = $this->_formStepCommonValidate($pageForm, 'dd');
         if ($valid && isset($_POST['next'])) {
             // Form is valid and the user has confirmed the bank branch details
             $pageSession->completed['dd'] = true;
             // Mark page as valid, so user can progress
             $quoteManager = new Manager_Insurance_LandlordsPlus_Quote($this->_quoteID);
             // Save the direct debit details and redirect to confirmation page
             $ddData = new Model_Core_Directdebit();
             $ddData->refNo = $this->_customerReferenceNumber;
             $ddData->policyNumber = $quoteManager->getPolicyNumber();
             $ddData->accountName = $pageSession->paymentSelectionDetails['dd_accountname'];
             $ddData->accountNumber = $pageSession->paymentSelectionDetails['bank_account_number'];
             $ddData->sortCode = str_replace('-', '', $pageSession->paymentSelectionDetails['bank_sortcode_number']);
             $startDate = $quoteManager->getStartDate();
             $firstPayMonth = date('Y-m-d', strtotime("{$startDate} + 1 month"));
             $ddData->paymentDate = $firstPayMonth;
             $ddData->paymentFrequency = ucfirst(strtolower($quoteManager->getPayBy()));
             // Instantiate a DD manager
             $ddPayment = new Manager_Core_Directdebit();
             // Save the stuffs
             $ddPayment->save($ddData);
             $this->_formStepCommonNavigate('dd');
             return;
         }
         if (isset($_POST['back'])) {
             $this->_formStepCommonNavigate('dd');
             return;
         }
     }
     // Load the element data from the database if we can
     if ($this->_formStepCommonPopulate($pageForm, 'dd')) {
         // Render the page unless we have been redirected
         $this->view->form = $pageForm;
         $this->view->stepNum = 'dd';
         $this->render('step');
     }
 }
 /**
  * Controller for the direct debit payment page
  *
  * @return void
  * * array('sort' => '938611', 'acct' => '07806039', 'assert' => 'true'),
  */
 public function ddAction()
 {
     $pageForm = new TenantsInsuranceQuoteB_Form_DirectDebit();
     $pageSession = new Zend_Session_Namespace('tenants_insurance_quote');
     // Tell page NOT to use AJAX validation as we go
     $this->view->headScript()->appendScript('var ajaxValidate = true; var ajaxValidatePage = \'dd\';');
     if ($this->getRequest()->isPost()) {
         $valid = $this->_formStepCommonValidate($pageForm, 'dd');
         if ($valid && isset($_POST['dd_confirm'])) {
             // Form is valid and the user has confirmed the bank branch details
             $pageSession->completed['dd'] = true;
             // Mark page as valid, so user can progress
             // Save the direct debit details and redirect to confirmation page
             $formData = $pageForm->getValues();
             $ddData = new Model_Core_Directdebit();
             $ddData->refNo = $this->_customerReferenceNumber;
             $ddData->policyNumber = $this->_policyNumber;
             $ddData->accountName = $formData["subform_directdebit"]["dd_accountname"];
             $ddData->accountNumber = $formData["subform_directdebit"]["bank_account_number"];
             $ddData->sortCode = preg_replace('/-/', '', $formData["subform_directdebit"]["bank_sortcode_number"]);
             $quote = new Manager_Insurance_TenantsContentsPlus_Quote(null, null, $this->_policyNumber);
             $startDate = $quote->getStartDate();
             $firstPayMonth = date("Y-m-d", strtotime("{$startDate} + 1 month"));
             $ddData->paymentDate = $firstPayMonth;
             $ddData->paymentFrequency = ucfirst(strtolower($quote->getPayBy()));
             // Create the Manage Object
             $ddPayment = new Manager_Core_Directdebit();
             // Save the stuffs
             $ddPayment->save($ddData);
             $this->_formStepCommonNavigate('dd');
             return;
         } elseif ($valid && !isset($_POST['dd_confirm'])) {
             // Form data is valid but they haven't confirmed their branch details
             $formData = $pageForm->getValues();
             // Switch the form to the confirmation form (essentially the same but with hidden fields)
             $pageForm = new TenantsInsuranceQuoteB_Form_BankConfirmation();
             // Populate the confirmation form with the data from the main DD form and also fill in the branch details
             // This is to show the bank branch details in the confirmation screen
             $bankManager = new Manager_Core_Bank();
             $this->view->branchDetails = $bankManager->getBranchDetail($_POST['bank_sortcode_number']);
             $formData['subform_directdebit']['dd_confirm'] = "yes";
             // The form data is in the wrong subform now because we want to use the confirmation form
             // so we have to switch the key over
             $formData['subform_bankconfirmation'] = $formData['subform_directdebit'];
             unset($formData['subform_directdebit']);
             $pageForm->populate($formData);
         }
         if (isset($_POST['back'])) {
             $this->_formStepCommonNavigate('dd');
             return;
         }
     }
     // Load the element data from the database if we can
     if ($this->_formStepCommonPopulate($pageForm, 'dd')) {
         // Render the page unless we have been redirected
         $this->view->form = $pageForm;
         $this->render('step');
     }
 }
 /**
  * Validates a bank sort code and account number combination
  *
  * Returns True if valid, false otherwise.
  *
  * @param string $sort_code
  * @param string $acct_number
  * @return bool success
  */
 public function isValid($sort_code, $acct_number)
 {
     $core_bank = new Manager_Core_Bank();
     return $core_bank->isAccountNumberValid($sort_code, $acct_number);
 }