/**
  * Accept the RRP Quote
  *
  * @return array
  */
 public function rentRecoveryPlusAcceptAction()
 {
     $request = $this->getSymfonyRequest();
     $quoteNumber = null;
     if ($request->isMethod('POST')) {
         $quoteNumber = $request->request->get('policyNumber');
     } else {
         if ($request->isMethod('GET')) {
             $quoteNumber = $request->query->get('policyNumber');
         }
     }
     if ($quoteNumber) {
         $applicationDecoratorClass = $this->getContainer()->get('rrp.application.decorator.class');
         /** @var RRP\Application\Decorators\RentRecoveryPlusQuote $quote */
         $quote = $applicationDecoratorClass::getDecorator('RentRecoveryPlusQuote');
         if ($quote->populateByPolicyNumber($quoteNumber)) {
             $referred = false;
             if ($quote->getAppData()->getPayStatus() == Model_Insurance_RentRecoveryPlus_LegacyPolicy::PAY_STATUS_REFERRED) {
                 $referred = true;
             } else {
                 $policyOptionsManagerClass = $this->getContainer()->get('rrp.utility.policy_options_manager.class');
                 $propertyRental = $policyOptionsManagerClass::getOption($quote->getAppData()->getPolicyOptions(), Model_Insurance_RentRecoveryPlus_LegacyPolicy::POLICY_OPTION_RRP, $quote->getAppData()->getAmountsCovered());
                 // Does the given reference satisfy the policy criteria?
                 $application = $quote->getApplicationData($quoteNumber);
                 $reference = $this->getReferenceFromSession($application->getReferenceNumber());
                 $referenceSatisfiesCriteria = $this->referenceSatisfiesCriteria($reference);
                 $this->referralModel->setFromRrpPolicy($quote, $propertyRental);
                 $applicationSatisfiesCriteria = $this->applicationSatisfiesCriteria();
                 if (!$referenceSatisfiesCriteria || !$applicationSatisfiesCriteria) {
                     // Need to set the policy number on the referral object so that listeners can access it for whatever reason.
                     $this->referralModel->setPolicyNumber($quoteNumber);
                     // Fire a POLICY_REFERRED event to handle all referral related actions.
                     $referredEvent = new ReferredEvent($this->referralModel);
                     $this->eventDispatcher->dispatch(RRPEvents::POLICY_REFERRED, $referredEvent);
                     // Mark this application as referred.
                     $quote->getAppData()->setPayStatus(Model_Insurance_RentRecoveryPlus_LegacyPolicy::PAY_STATUS_REFERRED);
                     $quote->save();
                     $referred = true;
                 }
             }
             if ($referred) {
                 return $this->renderTwigView('/rentguarantee/rent-recovery-plus-referral.html.twig', array('policyNumber' => $quoteNumber));
             }
             if ($this->_params->connect->settings->rentRecoveryPlus->manualReferenceCheck) {
                 $quote->getAppData()->setPayStatus(Model_Insurance_RentRecoveryPlus_LegacyPolicy::PAY_STATUS_REFERRED);
                 $quote->save();
                 $policyNote = new Datasource_Insurance_PolicyNotes();
                 $note = "Policy incepted by agent, passed to RG team to validate reference and place on risk.";
                 $policyNote->addNote($quoteNumber, $note);
                 // Send referral email
                 $subject = str_replace('{$policyNumber}', $quoteNumber, $this->_params->connect->settings->rentRecoveryPlus->referral->emailSubject);
                 $message = $this->getContainer()->get('twig')->render('rent-recovery-plus-manual-reference-mail.plain.twig', array('agentName' => $this->_agentObj->name, 'agentSchemeNumber' => $this->_agentSchemeNumber));
                 $mailManager = new Application_Core_Mail();
                 $mailManager->setTo($this->_params->connect->settings->rentRecoveryPlus->referral->emailToAddress, $this->_params->connect->settings->rentRecoveryPlus->referral->emailToName)->setFrom($this->_params->connect->settings->rentRecoveryPlus->referral->emailFromAddress, $this->_params->connect->settings->rentRecoveryPlus->referral->emailFromName)->setSubject($subject)->setBodyText($message);
                 $mailManager->send();
                 return $this->renderTwigView('/rentguarantee/rent-recovery-plus-manual-reference.html.twig', array('policyNumber' => $quoteNumber));
             }
             $policyNumber = $quote->acceptQuote($quoteNumber);
             if (!$policyNumber) {
                 $subject = str_replace('{$policyNumber}', $policyNumber, $this->_params->connect->settings->rentRecoveryPlus->referral->emailSubject);
                 $message = $this->getContainer()->get('twig')->render('rent-recovery-plus-error-mail.plain.twig', array('agentName' => $this->_agentObj->name, 'agentSchemeNumber' => $this->_agentSchemeNumber, 'dateEntered' => date('Y-m-d H:i:s'), 'policyNumber' => $policyNumber));
                 $mailManager = new Application_Core_Mail();
                 $mailManager->setTo($this->_params->connect->settings->rentRecoveryPlus->referral->emailToAddress, $this->_params->connect->settings->rentRecoveryPlus->referral->emailToName)->setFrom($this->_params->connect->settings->rentRecoveryPlus->referral->emailFromAddress, $this->_params->connect->settings->rentRecoveryPlus->referral->emailFromName)->setSubject($subject)->setBodyText($message);
                 $mailManager->send();
                 $this->renderTwigView('/rentguarantee/rent-recovery-plus-error.html.twig');
             } else {
                 Manager_Insurance_Quote::sendInception($policyNumber, null, null, $this->_params->connect->settings->rentRecoveryPlus->systemCsuID);
                 return $this->renderTwigView('/rentguarantee/rent-recovery-plus-confirmation.html.twig', array('policyNumber' => $policyNumber, 'emailQueryAddress' => $this->_params->connect->settings->rentRecoveryPlus->queryEmailAddress));
             }
         }
     }
     $this->renderTwigView('/rentguarantee/rent-recovery-plus-error.html.twig');
 }
 /**
  * Runs the migration action
  */
 public function run()
 {
     $this->legacyContainer = new LegacyContainer();
     $searchClient = $this->legacyContainer->get('rrp.legacy.datasource.search');
     $policyNumberManager = $this->legacyContainer->get('rrp.legacy.manager.policy_number');
     $this->policyNotes = $this->legacyContainer->get('rrp.legacy.datasource.policy_notes');
     $this->rates = $this->legacyContainer->get('rrp.legacy.datasource.rrp_rates');
     $renewalInvitePeriod = $this->legacyContainer->get('rrp.config.renewal_invite_period');
     $insightStatusInsight = $this->legacyContainer->get('rrp.legacy.const.insight_status_insight');
     $insightStatusException = $this->legacyContainer->get('rrp.legacy.const.insight_status_exception');
     $policyOptionRrp = $this->legacyContainer->get('rrp.legacy.const.policy_option_rrp');
     $policyOptionRrpNilExcess = $this->legacyContainer->get('rrp.legacy.const.policy_option_rrp-nilexcess');
     $this->customerAddress1 = $this->legacyContainer->get('rrp.legacy.const.customer_address1');
     $this->customerAddress2 = $this->legacyContainer->get('rrp.legacy.const.customer_address2');
     $this->customerAddress3 = $this->legacyContainer->get('rrp.legacy.const.customer_address3');
     $this->customerAgent = $this->legacyContainer->get('rrp.legacy.const.customer_agent');
     $this->customerLegacyIdentifier = $this->legacyContainer->get('rrp.legacy.const.customer_legacy_identifier');
     $this->statusPolicy = $this->legacyContainer->get('rrp.legacy.const.status_policy');
     $this->underwritingQuestionSetID = $this->legacyContainer->get('rrp.config.underwriting_question_set_id');
     $this->riskArea = $this->legacyContainer->get('rrp.config.risk_area');
     $this->iptPercent = $this->legacyContainer->get('rrp.config.ipt_percent');
     $this->payStatusReferred = $this->legacyContainer->get('rrp.legacy.const.pay_status_referred');
     $this->payStatusUpToDate = $this->legacyContainer->get('rrp.legacy.const.pay_status_up-to-date');
     $this->rrpContainer = new RRPContainer();
     $now = new \DateTime();
     $renewalDate = $now;
     $renewalDate->add(new \DateInterval($renewalInvitePeriod));
     $dateString = $now->format('Y-m-d H:i:s');
     $referral = new RentRecoveryPlusReferral();
     // Initialise the report data
     $this->initialiseReportData();
     // Send assumptive renewals
     $isRenewalInvite = false;
     $policyList = $searchClient->searchForInsightByEndDate($renewalDate, $insightStatusInsight, $isRenewalInvite);
     /** @var \RRP\Application\Decorators\RentRecoveryPlusInsight $insightPolicy */
     $insightPolicy = ApplicationDecoratorFactory::getDecorator('RentRecoveryPlusInsight');
     foreach ($policyList as $rrpPolicyNumber) {
         if (!$insightPolicy->populateByPolicyNumber($rrpPolicyNumber)) {
             $this->addToReport(self::POLICY_TYPE_EXCEPTION, $rrpPolicyNumber);
         } else {
             $newPolicyNumber = $policyNumberManager->generateApplicationNumber($policyNumberManager::POLICY_IDENTIFIER);
             if ($insightPolicy->getAppData()->getAgentRateSetID() === null) {
                 $isReferralRequired = true;
             } else {
                 $propertyRental = PolicyOptionsManager::getOption($insightPolicy->getAppData()->getPolicyOptions(), $policyOptionRrp, $insightPolicy->getAppData()->getAmountsCovered());
                 $isNilExcess = PolicyOptionsManager::isOptionSet($insightPolicy->getAppData()->getPolicyOptions(), $policyOptionRrpNilExcess, $insightPolicy->getAppData()->getAmountsCovered());
                 $rateDate = \DateTime::createFromFormat('Y-m-d', $insightPolicy->getAppData()->getEndDate());
                 $rateDate->add(new \DateInterval('P1D'));
                 try {
                     $rateManager = RateDecoratorFactory::getDecorator('RentRecoveryPlus', $insightPolicy->getAppData()->getAgentRateSetID(), $this->riskArea, $this->iptPercent, $propertyRental, $isNilExcess, $insightPolicy->getRrpData()->getReferenceType(), $insightPolicy->getAppData()->getPolicyLength(), $insightPolicy->getAppData()->isPayMonthly(), $rateDate);
                     $insightPolicy->setPolicyOptions($propertyRental, $rateManager->getPremium(), $rateManager->getNilExcessOption())->getAppData()->setPremium($rateManager->getPremium())->setIpt($rateManager->getIpt())->setQuote($rateManager->getQuote())->setRateSetID($rateManager->getRateSetID());
                     $referral->setFromRrpPolicy($insightPolicy->getRrpData(), $insightPolicy->getAllUnderwritingAnswers(), $now->format('Y-m-d'), $rateManager->getPremium(), $propertyRental, true);
                     $isReferralRequired = $referral->isReferralRequired();
                 } catch (\Exception $ex) {
                     error_log($ex->getMessage());
                     $isReferralRequired = true;
                 }
             }
             $policyNote = "Policy invite sent on renewal for previous Insight policy number {$rrpPolicyNumber}\n" . "Underwriting answers for 'Rent in advance', 'No claims logged' and 'Deposit sum'" . " are all assumed for migration from Insight but please check if a claim arises\n";
             try {
                 $this->migratePolicy($insightPolicy, $rrpPolicyNumber, $newPolicyNumber, $policyNumberManager::WHITE_LABEL_HOMELET, $dateString, $isReferralRequired, $referral->getReferralReason(), $policyNote, $isRenewalInvite);
             } catch (\Exception $ex) {
                 error_log($ex->getMessage());
                 $isReferralRequired = true;
             }
             if ($isReferralRequired) {
                 $this->addToReport(self::POLICY_TYPE_REFERRED, $rrpPolicyNumber, $newPolicyNumber);
             } else {
                 $this->addToReport(self::POLICY_TYPE_MIGRATED, $rrpPolicyNumber, $newPolicyNumber);
             }
         }
     }
     // Now do exceptions
     $policyList = $searchClient->searchForInsightByEndDate($now, $insightStatusException);
     foreach ($policyList as $rrpPolicyNumber) {
         $insightPolicy->populateByPolicyNumber($rrpPolicyNumber);
         $newPolicyNumber = $policyNumberManager->generateApplicationNumber($policyNumberManager::POLICY_IDENTIFIER);
         $insightPolicy->getAppData()->setPayStatus($this->legacyContainer->get('rrp.legacy.const.pay_status_referred'));
         $policyNote = 'Policy referred as it has changed in Insight since the data was migrated from previous Insight policy number ' . $rrpPolicyNumber;
         try {
             $this->migratePolicy($insightPolicy, $rrpPolicyNumber, $newPolicyNumber, $policyNumberManager::WHITE_LABEL_HOMELET, $dateString, true, $policyNote, $policyNote);
         } catch (\Exception $ex) {
             error_log($ex->getMessage());
         }
         $this->addToReport(self::POLICY_TYPE_EXCEPTION, $rrpPolicyNumber, $newPolicyNumber);
     }
     // Now send a report
     $this->sendReport();
 }