/**
  * Search in HRT.
  *
  * @param int $currentAgentSchemeNumber
  * @param SearchIndividualApplicationsCriteria $criteria
  * @param int $offset
  * @param int $limit
  * @return array Simple array of 'records' containing an array of ReferencingApplicationFindResult and
  *               'totalRecords' containing an integer of all possible records findable with the current criteria
  */
 private function searchInHrt($currentAgentSchemeNumber, SearchIndividualApplicationsCriteria $criteria, $offset, $limit)
 {
     if ($this->debugMode) {
         error_log('--- Search in HRT method invoked ---');
     }
     $records = array();
     $hrtReferenceManager = new \Manager_ReferencingLegacy_Munt();
     $hrtCriteria = array('refno' => $criteria->getReferenceNumber(), 'firstname' => $criteria->getApplicantFirstName(), 'lastname' => $criteria->getApplicantLastName(), 'address' => $criteria->getPropertyAddress(), 'town' => $criteria->getPropertyTown(), 'postcode' => $criteria->getPropertyPostcode(), 'state' => $criteria->getApplicationStatus(), 'type' => $criteria->getProductType());
     $hrtRows = $hrtReferenceManager->searchLegacyReferences($currentAgentSchemeNumber, $hrtCriteria, \Model_Referencing_SearchResult::STARTDATE_DESC, 0, $limit, $offset);
     foreach ($hrtRows->results as $hrtRow) {
         $status = self::APPLICATION_STATUS_INCOMPLETE;
         if ('Complete' == ucfirst(strtolower(trim($hrtRow['resulttx'])))) {
             $status = self::APPLICATION_STATUS_COMPLETE;
         }
         $model = new ReferencingApplicationFindResult();
         $model->setReferenceNumber($hrtRow['RefNo'])->setReferencingApplicationUuId($hrtRow['RefNo'])->setApplicantFirstName($hrtRow['firstname'])->setApplicantLastName($hrtRow['lastname'])->setStreet($hrtRow['address1'])->setCreatedAt($hrtRow['start_time'])->setStatusId($status)->setDataSource(self::DATA_SOURCE_HRT);
         $records[] = $model;
     }
     // Get total records by passing in a zero for both offset and limit
     $hrtRecordCount = $hrtReferenceManager->searchLegacyReferences($currentAgentSchemeNumber, $hrtCriteria, null, 0, 0, 0);
     $totalRecords = (int) $hrtRecordCount->totalNumberOfResults;
     return array('records' => $records, 'totalRecords' => $totalRecords);
 }
 /**
  * Displays the "ticks and crosses" reference summary + dynamic options for
  * a reference plucked from the legacy DB by its refno taken from a GET
  * parameter.
  *
  * @return void
  */
 public function summaryAction()
 {
     $this->view->headLink()->appendStylesheet('/assets/connect/css/referencingSummary.css');
     // See if there's a GET-based refno
     $request = $this->getRequest();
     if ($request->isGet() && !is_null($request->getParam('refno'))) {
         // Fetch reference by refno using the Referencing MUNT Manager class
         $refMuntManager = new Manager_ReferencingLegacy_Munt();
         $reference = $refMuntManager->getReference($request->getParam('refno'));
         // Check the owner of this reference
         if ($this->_agentSchemeNumber != $reference->customer->customerId) {
             // The session user is not the owner of this reference.
             // Renider auth failed status code and present error screen
             $this->getResponse()->setHttpResponseCode(401);
             throw new Exception('Authorisation failed when retreiving reference');
             return;
         }
         // Find related references
         $searchResult = $refMuntManager->searchLegacyReferences($this->_agentSchemeNumber, array('proprefno' => $reference->propertyLease->id), Model_Referencing_SearchResult::STARTDATE_ASC, 1, Model_Referencing_SearchResult::TWENTY_FIVE_ROWS);
         // Use the linked references array to determine if the current
         //   reference is complete
         // TODO: De-dirty this, shouldn't use the (even dirtier) linked refs
         //   array
         $refComplete = true;
         foreach ($searchResult->results as $linkedRef) {
             if ($linkedRef['RefNo'] == $reference->externalId) {
                 if (substr($linkedRef['refStatus'], 0, 10) == 'Incomplete') {
                     $refComplete = false;
                 }
             }
         }
         // Find if final report built or sent, and if so how long ago (to
         //   drive the availability of the "Add Guarantor" and "Print
         //   Guarantors Form" buttons)
         $interimReportBuiltItem = Manager_Referencing_Progress::findSpecificProgressItem($reference->progress, Model_Referencing_ProgressItemVariables::INTERIM_REPORT_BUILT);
         $finalReportBuilt = Manager_Referencing_Progress::findSpecificProgressItem($reference->progress, Model_Referencing_ProgressItemVariables::FINAL_REPORT_BUILT);
         // Fetch final-report-sent progress item
         $finalReportSent = Manager_Referencing_Progress::findSpecificProgressItem($reference->progress, Model_Referencing_ProgressItemVariables::FINAL_REPORT_SENT);
         $finalReportDaysAgo = null;
         $allowAddGuarantor = true;
         if (!is_null($finalReportSent) && $finalReportSent->itemState == Model_Referencing_ProgressItemStates::COMPLETE) {
             $date = new Zend_Date();
             $finalReportDaysAgo = $date->sub($finalReportSent->itemCompletionTimestamp)->toValue();
         } elseif (!is_null($finalReportBuilt) && $finalReportBuilt->itemState == Model_Referencing_ProgressItemStates::COMPLETE) {
             $date = new Zend_Date();
             $finalReportDaysAgo = $date->sub($finalReportBuilt->itemCompletionTimestamp)->toValue();
         }
         if (!is_null($finalReportDaysAgo)) {
             $finalReportDaysAgo = round($finalReportDaysAgo / (60 * 60 * 24));
             if ($finalReportDaysAgo > $this->_params->connect->referencing->disableGuarantorButtonAfterXDays) {
                 $allowAddGuarantor = false;
             }
         }
         // Instantiate security manager for generating MAC
         $securityManager = new Application_Core_Security($this->_params->connect->ref->security->securityString->agent);
         $macToken = $securityManager->generate(array($this->_agentSchemeNumber, $this->_agentId));
         // Pass vars into view
         $this->view->reference = $reference;
         $this->view->refComplete = $refComplete;
         $this->view->linkedRefs = $searchResult->results;
         $this->view->allowAddGuarantor = $allowAddGuarantor;
         // Set of URLs that link securely back to referencing
         $baseReferencingUrl = $this->_params->connect->baseUrl->referencing;
         $continueReferenceString = "{$baseReferencingUrl}frontEnd/referencingController.php?" . "refno={$reference->externalId}&" . "agentschemeno={$this->_agentSchemeNumber}&" . "agentid={$this->_agentId}&" . 'origin=connect&' . "agentToken={$macToken}=&" . 'refContinued=1';
         $applicationFormString = "{$baseReferencingUrl}frontEnd/displayForm.php?" . "agentid={$this->_agentId}&" . "refno={$reference->externalId}&" . 'form=application&' . "agentschemeno={$this->_agentSchemeNumber}&" . "agentToken={$macToken}";
         $addGuarantorString = "{$baseReferencingUrl}frontEnd/referencingController.php?" . "originalrefno={$reference->externalId}&" . 'isGuarantor=1&' . "agentschemeno={$this->_agentSchemeNumber}&" . "agentid={$this->_agentId}&" . 'origin=connect&' . "agentToken={$macToken}&" . 'newRefBegin=1';
         $addTenantString = "{$baseReferencingUrl}frontEnd/displayForm.php?" . "agentid={$this->_agentId}&" . 'isAddTenant=true&' . "startrefno={$reference->externalId}&" . 'form=product&' . "agentschemeno={$this->_agentSchemeNumber}&" . "agentToken={$macToken}";
         $viewCaseLogString = "{$baseReferencingUrl}frontEnd/displayForm.php?" . "agentid={$this->_agentId}&" . "refno={$reference->externalId}&" . 'form=caselog&' . "agentschemeno={$this->_agentSchemeNumber}&" . "agentToken={$macToken}";
         $emailAssessorString = "{$baseReferencingUrl}frontEnd/displayForm.php?" . "agentid={$this->_agentId}&" . "refno={$reference->externalId}&" . 'form=emailassessor&' . 'suppressStatus=1&' . "agentschemeno={$this->_agentSchemeNumber}&" . "agentToken={$macToken}";
         $printGuarantorFormString = "/referencing/agent-form?form=Agent-Guarantor&refno={$reference->externalId}";
         if ($reference->status->state == Model_Referencing_ReferenceStates::COMPLETE) {
             $tenantQuoteString = "{$baseReferencingUrl}cgi-bin/connect/tenantQuote.pl?" . "agentid={$this->_agentId}&" . "refno={$reference->externalId}&" . "form=tenantquote&" . "agentschemeno={$this->_agentSchemeNumber}&" . "agentToken={$macToken}";
         } else {
             $tenantQuoteString = '';
         }
         if ($interimReportBuiltItem != null && $interimReportBuiltItem->itemState == Model_Referencing_ProgressItemStates::COMPLETE) {
             /*
                         $retrieveReportString = "{$baseReferencingUrl}frontEnd/referencingController.php?"
                                 . "agentid={$this->_agentId}&"
                                 . "refno={$reference->externalId}&"
                                 . "agentschemeno={$this->_agentSchemeNumber}&"
                                 . "action=newReference&"
                                 . "brand=connect&"
                                 . "summaryConfirmed=1&"
                                 . "agentToken={$macToken}";
             */
             // Find if interim/final report built, and if so view the report via connect
             if ($interimReportBuiltItem != null && $interimReportBuiltItem->itemState === Model_Referencing_ProgressItemStates::COMPLETE) {
                 $repType = 'interim';
             }
             if ($finalReportBuilt != null && $finalReportBuilt->itemState === Model_Referencing_ProgressItemStates::COMPLETE) {
                 $repType = 'final';
             }
             $retrieveReportString = "{$this->_params->connectUrl->connectRootUrl}reports/view-report-pdf?refno={$reference->externalId}&repType={$repType}&contentDisposition=attachment";
             $this->view->refLinks = array('tenantQuote' => $tenantQuoteString, 'retrieveReport' => $retrieveReportString, 'applicationForm' => $applicationFormString, 'addGuarantor' => $addGuarantorString, 'printGuarantorForm' => $printGuarantorFormString, 'viewCaseLog' => $viewCaseLogString, 'addTenant' => $addTenantString, 'emailAssessor' => $emailAssessorString);
         } else {
             $this->view->refLinks = array('continueReference' => $continueReferenceString, 'applicationForm' => $applicationFormString, 'addGuarantor' => $addGuarantorString, 'printGuarantorForm' => $printGuarantorFormString, 'viewCaseLog' => $viewCaseLogString, 'addTenant' => $addTenantString, 'emailAssessor' => $emailAssessorString);
         }
     }
 }