/**
  * Cancel the policy
  *
  * @param string $policyEndDate
  * @return null|float
  */
 public function cancel($policyEndDate)
 {
     $cancelledDate = date('Y-m-d');
     $this->appData->setCancelledDate($cancelledDate);
     $this->appData->setEndDate($policyEndDate);
     $this->appData->setPayStatus($this->container->get('rrp.legacy.const.pay_status_policy'));
     $propertyRental = PolicyOptionsManager::getOption($this->appData->getPolicyOptions(), $this->getContainer()->get('rrp.legacy.const.policy_option_rrp'), $this->appData->getAmountscovered());
     $termData = $this->policyTerm->getPolicyTerm($this->appData->getPolicyNumber(), $this->appData->getStartDate());
     if ($termData) {
         if ($this->application->save($this->appData)) {
             if ($propertyRental < RentRecoveryPlus::BAND_A_LIMIT) {
                 $transBand = RentRecoveryPlus::BAND_A;
             } else {
                 $transBand = RentRecoveryPlus::BAND_B;
             }
             $cancellationPeriod = $this->container->get('rrp.config.cancellation_period');
             if ($termData->term > 1) {
                 $refundEndAt = \DateTime::createFromFormat('Y-m-d', $this->appData->getStartDate());
             } else {
                 $refundEndAt = \DateTime::createFromFormat('Y-m-d', $this->appData->getIssueDate());
             }
             $refundEndAt = $refundEndAt->setTime(23, 59, 59)->add(new \DateInterval($cancellationPeriod));
             $policyEndAt = \DateTime::createFromFormat('Y-m-d', $policyEndDate);
             $refundValue = 0.0;
             $cancelFromDate = null;
             if ($policyEndAt < $refundEndAt) {
                 $cancelFromDate = $this->appData->getIssueDate();
             } else {
                 $cancelFromDate = $policyEndDate;
             }
             if ($cancelFromDate !== null) {
                 $transaction = TransactionDecoratorFactory::getDecorator(TransactionDecoratorFactory::TRANSACTION_CANCEL, 0, 0, $termData->id, 0, $transBand, $cancelFromDate, $this->appData);
                 $transaction->processAllTransactions();
                 $refundValue = $transaction->getTransactionAmount();
             }
             return $refundValue;
         }
     }
     return null;
 }
 /**
  * Gets the application data using the policy number
  *
  * @param $policyNumber
  * @return RentRecoveryPlusApplication|null
  */
 public function getApplicationData($policyNumber)
 {
     if (!$this->populateByPolicyNumber($policyNumber)) {
         return null;
     } else {
         if ($this->appData->getPayStatus() == $this->container->get('rrp.legacy.const.pay_status_referred')) {
             return null;
         }
     }
     $referenceNumbers = array();
     /** @var \Model_Insurance_RentRecoveryPlus_RrpTenantReference $rrpTenantReference */
     foreach ($this->rrpTenantReferenceData as $rrpTenantReference) {
         $referenceNumbers[] = $rrpTenantReference->getReferenceNumber();
     }
     $tenancyStartAt = \DateTime::createFromFormat('Y-m-d', $this->rrpData->getTenancyStartAt());
     $applicationData = new RentRecoveryPlusApplication();
     $applicationData->setPolicyNumber($policyNumber)->setPolicyLength($this->appData->getPolicyLength())->setReferenceType($this->rrpData->getReferenceType())->setOtherProvider($this->rrpData->getOtherProvider())->setReferenceNumber(implode("\n", $referenceNumbers))->setIsNilExcess(PolicyOptionsManager::isOptionSet($this->appData->getPolicyOptions(), $this->container->get('rrp.legacy.const.policy_option_rrp-nilexcess'), $this->appData->getAmountsCovered()))->setIsContinuationOfExistingPolicy($this->underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_CONTINUATION])->setExistingPolicyRef($this->rrpData->getExistingPolicyRef())->setIsExistingPolicyToBeCancelled($this->rrpData->getIsExistingPolicyToBeCancelled())->setCancellationObjections($this->rrpData->getCancellationObjections())->setPropertyLetType($this->rrpData->getPropertyLetType())->setHasLandlordPermission($this->rrpData->getHasLandlordPermission())->setPropertyAddress1($this->combineAddress($this->appData->getPropertyAddress1(), $this->appData->getPropertyAddress2()))->setPropertyAddress2($this->appData->getPropertyAddress3())->setPropertyPostcode($this->appData->getPropertyPostcode())->setPropertyDeposit($this->rrpData->getPropertyDeposit())->setPropertyRental(PolicyOptionsManager::getOption($this->appData->getPolicyOptions(), $this->container->get('rrp.legacy.const.policy_option_rrp'), $this->appData->getAmountsCovered()))->setHasPaidRentInAdvance($this->underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_RENT_IN_ADVANCE])->setTenancyStartAt($tenancyStartAt)->setPolicyStartAt(\DateTime::createFromFormat('Y-m-d', $this->appData->getStartDate()))->setLandlordTitle($this->lliData->getTitle())->setLandlordFirstName($this->lliData->getFirstName())->setLandlordLastName($this->lliData->getLastName())->setLandlordEmailAddress($this->lliData->getEmailAddress())->setLandlordPhoneNumber($this->lliData->getPhoneNumber())->setLandlordAddress1($this->combineAddress($this->lliData->getAddress1(), $this->lliData->getAddress2()))->setLandlordAddress2($this->lliData->getAddress3())->setLandlordPostcode($this->lliData->getPostcode())->setLandlordCountry($this->lliData->getCountry())->setHasPossibleClaimCircumstances($this->underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_CLAIM_CIRCUMSTANCES])->setClaimInfo($this->rrpData->getClaimInfo())->setHasPermittedOccupiersOnly($this->underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_PERMITTED_OCCUPIERS])->setHasTenancyDisputes($this->underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_TENANCY_DISPUTES])->setTenancyAgreementType(TenancyAgreementTypes::getIsAssuredShortholdTenancy($this->underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_TENANCY_AST]))->setHasPriorClaims($this->underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_PRIOR_CLAIMS])->setIsDepositSufficient($this->underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_DEPOSIT_SUFFICIENT])->setIsPayMonthly($this->appData->getPayBy() == $this->container->get('rrp.legacy.const.pay_by_monthly'));
     $this->isNewApplication = false;
     return $applicationData;
 }
 /**
  * Renew a policy from the 'temporary' insight_rrp_policy to the policy table
  *
  * @param string $rrpPolicyNumber
  * @param string $newPolicyNumber
  * @param string $termNumber
  * @return bool
  * @throws \RuntimeException
  */
 public function migratePolicy($rrpPolicyNumber, $newPolicyNumber, $termNumber)
 {
     if ($this->policyNumberManager->isRentRecoveryPlusInsightPolicy($rrpPolicyNumber)) {
         $this->setPolicyNumber($newPolicyNumber);
         $disbData = $this->disbursement->getDisbursement($this->appData->getWhiteLabelID(), $this->appData->getRateSetID());
         if (!$disbData) {
             $message = sprintf('Disbursement data not found (WhiteLabelId = %s, RateSetID = %d)', $this->appData->getWhiteLabelID(), $this->appData->getRateSetID());
             throw new \RuntimeException($message);
         }
         $getter = sprintf('getRrpi%s%dm', ReferenceTypes::isFullReference($this->rrpData->getReferenceType()) ? 'FullRef' : 'CreditCheck', $this->appData->getPolicyLength());
         if (PolicyOptionsManager::isOptionSet($this->appData->getPolicyOptions(), $this->container->get('rrp.legacy.const.policy_option_rrp-nilexcess'), $this->appData->getAmountsCovered())) {
             $getter .= '0xs';
         }
         if (!method_exists($disbData, $getter)) {
             $message = sprintf('Unknown disbursement type (%s)', $getter);
             throw new \RuntimeException($message);
         }
         $disbursement = $disbData->{$getter}();
         if (empty($disbursement)) {
             $message = sprintf('Disbursement data not set (WhiteLabelId = %s, RateSetID = %d, %s)', $this->appData->getWhiteLabelID(), $this->appData->getRateSetID(), $getter);
             throw new \RuntimeException($message);
         }
         $propertyRental = PolicyOptionsManager::getOption($this->appData->getPolicyOptions(), $this->getContainer()->get('rrp.legacy.const.policy_option_rrp'), $this->appData->getAmountsCovered());
         if ($propertyRental < RentRecoveryPlus::BAND_A_LIMIT) {
             $band = $disbData::BAND_A;
         } else {
             $band = $disbData::BAND_B;
         }
         $insuranceNetAmount = $disbData->extractBandDisbursement($disbursement, $band);
         // Policy Term
         $termId = $this->policyTerm->updatePolicyTerm($this->appData, $termNumber, array('netPremium' => $insuranceNetAmount, 'collectedPremium' => $this->appData->premium));
         if ($termId) {
             // Write the policy to the datasource
             $this->getAppData()->setStatus($this->container->get('rrp.legacy.const.status_policy'))->setTermId($termId);
             $legacyPolicy = $this->container->get('rrp.legacy.datasource.policy');
             $legacyPolicy->save($this->appData);
             // Delete the legacy insight record.
             $policyConstType = $this->container->get('rrp.legacy.const.quote_policy_number');
             $this->application->remove(array($policyConstType => $rrpPolicyNumber));
             $insightStatusIas = $this->container->get('rrp.legacy.const.insight_status_ias');
             // Now move the RRP / LL Interest parts
             if ($this->rrp->accept($rrpPolicyNumber, $newPolicyNumber, $insightStatusIas)) {
                 if ($this->llInterest->accept($rrpPolicyNumber, $newPolicyNumber)) {
                     /** @var RentRecoveryPlusAnswers $underwriting */
                     $underwriting = UnderwritingDecoratorFactory::getDecorator(UnderwritingDecoratorFactory::UNDERWRITING_RENT_RECOVERY_PLUS, $this->container->get('rrp.config.underwriting_question_set_id'), $this->appData->getIssueDate(), $rrpPolicyNumber);
                     $answers = $underwriting->getAllAnswers();
                     $answers[RentRecoveryPlusAnswers::QUESTION_ID_RENT_IN_ADVANCE] = true;
                     $answers[RentRecoveryPlusAnswers::QUESTION_ID_PRIOR_CLAIMS] = false;
                     $answers[RentRecoveryPlusAnswers::QUESTION_ID_DEPOSIT_SUFFICIENT] = true;
                     $underwriting->setAnswers($answers)->saveAnswers()->changeQuoteToPolicy($newPolicyNumber);
                     $coverOptions = array();
                     foreach ($this->appData->getValidPolicyOptionNames() as $optionName) {
                         if (PolicyOptionsManager::isOptionSet($this->appData->getPolicyOptions(), $optionName, $this->appData->getAmountsCovered())) {
                             $optionId = $this->policyOptions->fetchOptionsByName($optionName);
                             $sumInsured = PolicyOptionsManager::getOption($this->appData->getPolicyOptions(), $optionName, $this->appData->getAmountsCovered());
                             $premium = PolicyOptionsManager::getOption($this->appData->getPolicyOptions(), $optionName, $this->appData->getOptionPremiums());
                             $coverOptions[] = array('policyNumber' => $newPolicyNumber, 'policyOptionID' => $optionId, 'sumInsured' => $sumInsured, 'premium' => $premium);
                         }
                     }
                     $this->policyCover->setCover($newPolicyNumber, $coverOptions);
                     return $newPolicyNumber;
                 }
             }
         }
     }
     return false;
 }
 /**
  * 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();
 }
 /**
  * @inheritdoc
  */
 public function create($policy, $mta, $premium, $quote, $ipt, $nilExcessOption)
 {
     $this->legacyMtaData->setPolicyNumber($policy->getAppData()->getPolicyNumber());
     $now = new \DateTime();
     $isNilExcess = PolicyOptionsManager::isOptionSet($policy->getAppData()->getPolicyOptions(), $this->getContainer()->get('rrp.legacy.const.policy_option_rrp-nilexcess'), $policy->getAppData()->getAmountsCovered());
     $proRataCalcs = new ProRataCalculations($mta->getPolicyStartedAt(), $mta->getMtaEffectiveAt());
     $premiumAdjustment = $proRataCalcs->getAdjustment($policy->getAppData()->getPolicyLength(), $policy->getAppData()->getPolicyLength(), $premium, $policy->getAppData()->getPremium());
     $quoteAdjustment = $proRataCalcs->getAdjustment($policy->getAppData()->getPolicyLength(), $policy->getAppData()->getPolicyLength(), $quote, $policy->getAppData()->getQuote());
     $iptAdjustment = $proRataCalcs->getAdjustment($policy->getAppData()->getPolicyLength(), $policy->getAppData()->getPolicyLength(), $ipt, $policy->getAppData()->getIpt());
     $amountToPay = $quoteAdjustment;
     $monthsRemaining = $proRataCalcs->getMonthsRemaining($policy->getAppData()->getPolicyLength());
     $policyOptions = PolicyOptionsManager::addPolicyOption($this->container->get('rrp.legacy.const.policy_option_rrp'));
     $amountsCovered = PolicyOptionsManager::addPolicyOption($mta->getPropertyRental());
     $policyOptionPremium = PolicyOptionsManager::getOption($policy->getAppData()->getPolicyOptions(), $this->container->get('rrp.legacy.const.policy_option_rrp'), $policy->getAppData()->getOptionPremiums());
     $policyOptionNilExcess = 0.0;
     if ($isNilExcess) {
         $policyOptionNilExcess = PolicyOptionsManager::getOption($policy->getAppData()->getPolicyOptions(), $this->container->get('rrp.legacy.const.policy_option_rrp-nilexcess'), $policy->getAppData()->getOptionPremiums());
     }
     $policyOptionAdjustment = $proRataCalcs->getAdjustment($policy->getAppData()->getPolicyLength(), $policy->getAppData()->getPolicyLength(), $premium - $nilExcessOption, $policyOptionPremium);
     $optionPremiums = PolicyOptionsManager::addPolicyOption($policyOptionAdjustment);
     if ($isNilExcess) {
         $nilExcessAdjustment = $proRataCalcs->getAdjustment($policy->getAppData()->getPolicyLength(), $policy->getAppData()->getPolicyLength(), $nilExcessOption, $policyOptionNilExcess);
         $policyOptions = PolicyOptionsManager::addPolicyOption($this->container->get('rrp.legacy.const.policy_option_rrp-nilexcess'), $policyOptions);
         $amountsCovered = PolicyOptionsManager::addPolicyOption($mta->getPropertyRental(), $amountsCovered);
         $optionPremiums = PolicyOptionsManager::addPolicyOption($nilExcessAdjustment, $optionPremiums);
     }
     $this->legacyMtaData->setPolicyOptions($policyOptions)->setAmountsCovered($amountsCovered)->setOptionPremiums($optionPremiums)->setDateAdded($now->format('Y-m-d'))->setStatus('pending')->setPremium($premiumAdjustment)->setQuote($quoteAdjustment)->setIpt($iptAdjustment)->setAmountToPay($amountToPay)->setMonthsRemaining($monthsRemaining)->setAdminCharge(0.0)->setPropAddress1('')->setPropAddress3('')->setPropAddress5('')->setPropPostcode('')->setRiskArea('')->setRiskAreaB('')->setChangeCorrespondenceAndPersonal('')->setParagonMortgageNumber('');
     $propertyRental = PolicyOptionsManager::getOption($policy->getAppData()->getPolicyOptions(), $this->getContainer()->get('rrp.legacy.const.policy_option_rrp'), $policy->getAppData()->getAmountsCovered());
     $notes = $now->format('d/m/Y') . "\n" . "MTA by Agent via Connect\n";
     if ($mta->getPropertyRental() != $propertyRental) {
         $notes .= "Property rental changed from £" . round($propertyRental, 2) . " to £" . round($mta->getPropertyRental(), 2) . "\n";
     }
     $referral = new RentRecoveryPlusReferral();
     $referral->setFromMta($mta, $premium);
     if ($referral->isReferralRequired()) {
         $notes .= "\nThis policy has been referred for the following reason(s)\n\n" . implode("\n", $referral->getReferralReason());
     }
     $this->legacyMtaData->SetDisplayNotes($notes);
     $endTime = new \DateTime($policy->getAppData()->getEndDate());
     $monthsRemaining = (int) $endTime->format('m') - (int) $now->format('m');
     if ($monthsRemaining < 0) {
         $monthsRemaining += 13;
     } else {
         $monthsRemaining += 1;
     }
     $this->legacyMtaData->setMonthsRemaining($monthsRemaining)->setPaidNet('no')->setDateAdded($now->format('Y-m-d'))->setDateOnRisk($mta->getMtaEffectiveAt())->setDateOffRisk(null);
     /** @var RentRecoveryPlusAnswers $underwriting */
     $underwriting = UnderwritingDecoratorFactory::getDecorator(UnderwritingDecoratorFactory::UNDERWRITING_RENT_RECOVERY_PLUS, $this->container->get('rrp.config.underwriting_question_set_id'), $now->format('Y-m-d'), $policy->getAppData()->getPolicyNumber());
     $underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_CONTINUATION] = null;
     $underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_RENT_IN_ADVANCE] = null;
     $underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_CLAIM_CIRCUMSTANCES] = $mta->getHasPossibleClaimCircumstances();
     $underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_PERMITTED_OCCUPIERS] = $mta->getHasPermittedOccupiersOnly();
     $underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_TENANCY_DISPUTES] = $mta->getHasTenancyDisputes();
     $underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_TENANCY_AST] = TenancyAgreementTypes::isAssuredShortholdTenancy($mta->getTenancyAgreementType());
     $underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_DEPOSIT_SUFFICIENT] = $mta->getIsDepositSufficient();
     $underwritingAnswers[RentRecoveryPlusAnswers::QUESTION_ID_PRIOR_CLAIMS] = $mta->getHasPriorClaims();
     $underwriting->setAnswers($underwritingAnswers)->saveAnswers();
     $mtaID = $this->legacyMta->create($this->legacyMtaData);
     if ($mtaID) {
         if ($this->rrpMta->create($mta, $mtaID)) {
             return $mtaID;
         }
     }
     return false;
 }
 /**
  * Gets the property rental
  *
  * @return float
  */
 public function getPropertyRental()
 {
     return (double) PolicyOptionsManager::getOption($this->policyOptions, self::POLICY_OPTION_RRP, $this->amountsCovered);
 }
 /**
  * Accept the quote and change it to a policy
  *
  * @param string $quoteNumber
  * @return null|string
  * @throws \RuntimeException
  */
 public function acceptQuote($quoteNumber)
 {
     /** @var \Manager_Core_PolicyNumber $policyNumberManager */
     $policyNumberManager = $this->getContainer()->get('rrp.legacy.manager.policy_number');
     if ($policyNumberManager->isRentRecoveryPlusQuote($quoteNumber)) {
         $this->isNewApplication = false;
         $policyNumber = $policyNumberManager::convertQuoteToPolicyNumber($quoteNumber);
         if ($this->populateByPolicyNumber($quoteNumber)) {
             //Set the issueDate and timecompleted fields (which oddly record the same value but
             // in different formats).
             $timeCompleted = time();
             $issueDate = new \DateTime();
             $issueDate->setTimestamp($timeCompleted);
             $this->setPolicyNumber($policyNumber)->appData->setStatus($this->getContainer()->get('rrp.legacy.const.status_policy'))->setPayStatus($this->getContainer()->get('rrp.legacy.const.pay_status_up-to-date'))->setIssueDate($issueDate)->setTimeCompleted($timeCompleted);
             $disbData = $this->disbursement->getDisbursement($this->appData->getWhiteLabelID(), $this->appData->getRateSetID());
             if (!$disbData) {
                 $message = sprintf('Disbursement data not found (WhiteLabelId = %s, RateSetID = %d)', $this->appData->getWhiteLabelID(), $this->appData->getRateSetID());
                 throw new \RuntimeException($message);
             }
             $getter = sprintf('getRrpi%s%dm', ReferenceTypes::isFullReference($this->rrpData->getReferenceType()) ? 'FullRef' : 'CreditCheck', $this->appData->getPolicyLength());
             if (PolicyOptionsManager::isOptionSet($this->appData->getPolicyOptions(), $this->container->get('rrp.legacy.const.policy_option_rrp-nilexcess'), $this->appData->getAmountsCovered())) {
                 $getter .= '0xs';
             }
             if (!method_exists($disbData, $getter)) {
                 $message = sprintf('Unknown disbursement type (%s)', $getter);
                 throw new \RuntimeException($message);
             }
             $disbursement = $disbData->{$getter}();
             if (empty($disbursement)) {
                 $message = sprintf('Disbursement data not set (WhiteLabelId = %s, RateSetID = %d, %s)', $this->appData->getWhiteLabelID(), $this->appData->getRateSetID(), $getter);
                 throw new \RuntimeException($message);
             }
             $propertyRental = PolicyOptionsManager::getOption($this->appData->getPolicyOptions(), $this->getContainer()->get('rrp.legacy.const.policy_option_rrp'), $this->appData->getAmountscovered());
             if ($propertyRental < RentRecoveryPlus::BAND_A_LIMIT) {
                 $band = $disbData::BAND_A;
                 $transBand = RentRecoveryPlus::BAND_A;
             } else {
                 $band = $disbData::BAND_B;
                 $transBand = RentRecoveryPlus::BAND_B;
             }
             $insuranceNetAmount = $disbData->extractBandDisbursement($disbursement, $band);
             $termId = $this->policyTerm->insertPolicyTerm($this->appData, $insuranceNetAmount);
             if ($termId) {
                 $policy = $this->container->get('rrp.legacy.datasource.policy');
                 if ($policy->save($this->appData)) {
                     //Delete the legacy quote.
                     $policyConstType = $this->container->get('rrp.legacy.const.quote_policy_number');
                     $this->application->remove(array($policyConstType => $quoteNumber));
                     // Now move the RRP / LL Interest parts
                     if ($this->rrp->accept($quoteNumber, $policyNumber)) {
                         if ($this->rrpTenantReference->accept($quoteNumber, $policyNumber, $termId)) {
                             if ($this->llInterest->accept($quoteNumber, $policyNumber)) {
                                 $this->policyNotes->changeQuoteToPolicy($quoteNumber, $policyNumber);
                                 $transaction = TransactionDecoratorFactory::getDecorator(TransactionDecoratorFactory::TRANSACTION_ADD, $this->getAppData()->getPremium(), $insuranceNetAmount, $termId, 0, $transBand, $this->appData->getStartDate(), $this->appData);
                                 $transaction->processAllTransactions();
                                 $underwriting = UnderwritingDecoratorFactory::getDecorator(UnderwritingDecoratorFactory::UNDERWRITING_RENT_RECOVERY_PLUS, $this->container->get('rrp.config.underwriting_question_set_id'), $this->appData->getIssueDate(), $quoteNumber);
                                 $underwriting->changeQuoteToPolicy();
                                 /** @var \Datasource_Insurance_Policy_Cover $policyCover */
                                 $policyCover = $this->container->get('rrp.legacy.datasource.policy_cover');
                                 $policyCover->changeQuoteToPolicy($quoteNumber, $policyNumber);
                                 return $policyNumber;
                             }
                         }
                     }
                 }
             }
         }
     }
     return null;
 }