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);
 }
Пример #2
0
 public function saveData()
 {
     $session = new Zend_Session_Namespace('referencing_global');
     $data = $this->getValues();
     $referenceManager = new Manager_Referencing_Reference();
     $reference = $referenceManager->getReference($session->referenceId);
     //Derive the residence chronology from the current flow item, so that we can locate
     //the relevant residence to update.
     switch ($session->currentFlowItem) {
         case Model_Referencing_DataEntry_FlowItems::FIRST_RESIDENCE:
             $chronology = Model_Referencing_ResidenceChronology::CURRENT;
             break;
         case Model_Referencing_DataEntry_FlowItems::SECOND_RESIDENCE:
             $chronology = Model_Referencing_ResidenceChronology::FIRST_PREVIOUS;
             break;
         case Model_Referencing_DataEntry_FlowItems::THIRD_RESIDENCE:
             $chronology = Model_Referencing_ResidenceChronology::SECOND_PREVIOUS;
             break;
     }
     //Attept to locate the relevant residence.
     $residenceManager = new Manager_Referencing_Residence();
     $thisResidence = $residenceManager->findSpecificResidence($reference->referenceSubject->residences, $chronology);
     if (empty($thisResidence)) {
         //The residence to process does not exist, so create it first.
         $thisResidence = $residenceManager->insertPlaceholder($session->referenceId, $chronology);
         if (empty($reference->referenceSubject->residences)) {
             $reference->referenceSubject->residences = array();
         }
         $reference->referenceSubject->residences[] = $thisResidence;
     }
     //Update thisResidence to reflect the user inputs.
     if (isset($data['is_foreign_address']) && 'Yes' == $data['is_foreign_address']) {
         // Needed by the HRT system to recognise foreign addresses
         $thisResidence->address->addressLine1 = 'Abroad';
         $thisResidence->address->town = 'Abroad';
         $thisResidence->address->postCode = '1001';
         $thisResidence->address->isOverseasAddress = true;
     } else {
         //Format the property details.
         $postcodeManager = new Manager_Core_Postcode();
         $propertyAddress = $postcodeManager->getPropertyByID($data['property_address'], false);
         $addressLine1 = ($propertyAddress['organisation'] != '' ? "{$propertyAddress['organisation']}, " : '') . ($propertyAddress['houseNumber'] != '' ? "{$propertyAddress['houseNumber']} " : '') . ($propertyAddress['buildingName'] != '' ? "{$propertyAddress['buildingName']}, " : '') . $propertyAddress['address2'];
         $addressLine2 = $propertyAddress['address4'];
         $town = $propertyAddress['address5'];
         $postCode = $data['property_postcode'];
         if (empty($thisResidence->address)) {
             $addressManager = new Manager_Core_Address();
             $thisResidence->address = $addressManager->createAddress();
         }
         $thisResidence->address->addressLine1 = $addressLine1;
         $thisResidence->address->addressLine2 = $addressLine2;
         $thisResidence->address->town = $town;
         $thisResidence->address->postCode = $postCode;
         $thisResidence->address->isOverseasAddress = false;
     }
     $thisResidence->durationAtAddress = $data['duration_at_address'];
     //months
     //Finally, identify if the ReferenceSubject should be classed as a foreign national,
     //which is when they have spent the 6 months or more abroad.
     if ($this->_isOverseas($reference)) {
         $reference->referenceSubject->isForeignNational = true;
     } else {
         $reference->referenceSubject->isForeignNational = false;
     }
     //Update the datasources.
     $referenceManager->updateReference($reference);
 }