/**
  * Evaluate the application against business rules and return the result.
  *
  * @return bool
  */
 private function applicationSatisfiesCriteria()
 {
     $applicationSatisfiesCriteria = !$this->referralModel->isReferralRequired();
     if (!$applicationSatisfiesCriteria) {
         $this->applicationBasedReferralReasons = $this->referralModel->getReferralReason();
     }
     return $applicationSatisfiesCriteria;
 }
 /**
  * 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;
 }