Пример #1
0
 /**
  * Validate and handle adding and removing possessions via AJAX
  *
  * @return void
  */
 public function possessionAction()
 {
     $output = array();
     $pageSession = new Zend_Session_Namespace('tenants_insurance_quote');
     // TODO: This needs to stop using datasources directly and use the quote manager
     // Add the bikes details to the database
     $possession = new Datasource_Insurance_Policy_SpecPossessions($pageSession->PolicyNumber);
     $ajaxForm = new TenantsInsuranceQuote_Form_Json_Possessions();
     // Ignore 'away_from_home' and 'above_x' fields for AJAX requests
     $ajaxForm->subform_possessions->getElement('away_from_home')->setRequired(false);
     $ajaxForm->subform_possessions->getElement('above_x')->setRequired(false);
     $request = $this->getRequest();
     $postdata = $request->getPost();
     // Filter to mirror the subform element definitions.
     $currencyFilterElements = array('possession_value');
     foreach ($currencyFilterElements as $filterElement) {
         if (isset($postdata[$filterElement])) {
             $postdata[$filterElement] = preg_replace(array('/[^\\d\\.]/'), array(''), $postdata[$filterElement]);
         }
     }
     // Force the elements to be required as this is an add item click - ugly :(
     if (isset($postdata['addPossession']) && $postdata['addPossession'] == 1) {
         $ajaxForm->subform_possessions->getElement('possession_categoryId')->setRequired(true);
         $ajaxForm->subform_possessions->getElement('possession_description')->setRequired(true);
         $ajaxForm->subform_possessions->getElement('possession_value')->setRequired(true);
     }
     $ajaxForm->populate($postdata);
     $quoteManager = new Manager_Insurance_TenantsContentsPlus_Quote(null, null, $pageSession->PolicyNumber);
     if ($ajaxForm->isValid($postdata)) {
         // Check if a new possession's details are being added and possessions is below max
         if (isset($postdata['addPossession']) && $postdata['addPossession'] == 1) {
             $cleanData = $ajaxForm->getValues();
             // According to the Zend manual these *should* be the clean values
             $possession->addNew($cleanData['subform_possessions']);
         }
         // Check if an existing possession's details are being removed
         if (isset($postdata['removePossession']) && $postdata['removePossession'] == 1) {
             $possession->remove($postdata['possessionNum']);
         }
         $totalValue = $possession->getTotalValue();
         // Now we need to update the total amounts covered in the quote manager
         $quoteManager->setCoverAmount($totalValue, Manager_Insurance_TenantsContentsPlus_Quote::SPECIFIEDPOSSESSIONS);
     }
     $errorMessages = $ajaxForm->getMessagesFlattened();
     $output['errorJs'] = $errorMessages;
     $output['errorCount'] = count($errorMessages);
     $output['errorHtml'] = $this->view->partial('partials/error-list.phtml', array('errors' => $errorMessages));
     // Tell page if max possessions reached
     $output['disableAdd'] = $possession->countPossessions() == $possession->maxPossessions ? 1 : 0;
     $output['html'] = $this->view->getHelper('yourPossessions')->yourPossessions();
     $premiums = $quoteManager->calculatePremiums();
     $fees = $quoteManager->getFees();
     $output['premiums'] = $premiums;
     $output['fees'] = $fees;
     echo Zend_Json::encode($output);
 }
 /**
  * Initialise the step 2 form
  *
  * @return void
  */
 public function step2Action()
 {
     $pageForm = new TenantsInsuranceQuoteB_Form_Step2();
     // Tell page to use AJAX validation as we go
     $this->view->headScript()->appendScript('var ajaxValidate = true; var ajaxValidatePage = 2;');
     if ($this->getRequest()->isPost()) {
         $valid = $this->_formStepCommonValidate($pageForm, 2);
         if ($valid) {
             $data = $pageForm->getValues();
             //Update the WebLead summary and create a STEP2 blob.
             $webLeadManager = new Manager_Core_WebLead();
             $webLeadSummary = $webLeadManager->getSummary($this->_webLeadSummaryId);
             $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 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');
     }
 }
 /**
  * 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');
     }
 }