/**
  * Cancel the policy
  */
 public function rentRecoveryPlusCancelAction()
 {
     $request = $this->getSymfonyRequest();
     $cancellationType = $this->getContainer()->get('rrp.form.cancellation');
     $policyNumber = null;
     $isConfirmed = false;
     if ($request->isMethod('GET')) {
         $policyNumber = $request->query->get('policyNumber');
     } else {
         if ($request->isMethod('POST')) {
             $isConfirmed = $request->request->get('isConfirmed', 0);
             $postedData = $request->request->get($cancellationType->getName());
             if (array_key_exists('policyNumber', $postedData)) {
                 $policyNumber = $postedData['policyNumber'];
             }
         }
     }
     if ($policyNumber) {
         // Ensure date picker CSS and JS are available
         $this->view->headLink()->appendStylesheet('/assets/vendor/jquery-datepicker/css/datePicker.css');
         $this->view->headScript()->appendFile('/assets/vendor/jquery-date/js/date.js');
         $this->view->headScript()->appendFile('/assets/vendor/jquery-datepicker/js/jquery.datePicker.js');
         $message = null;
         $applicationDecoratorClass = $this->getContainer()->get('rrp.application.decorator.class');
         /** @var RRP\Application\Decorators\RentRecoveryPlusPolicy $policy */
         $policy = $applicationDecoratorClass::getDecorator('RentRecoveryPlusPolicy');
         $policy->populateByPolicyNumber($policyNumber);
         if (Manager_Core_PolicyNumber::isQuote($policyNumber)) {
             $message = 'Quotes do not need cancelling';
             $isConfirmed = false;
         } else {
             if ($policy->getAppData()->getPayStatus() == Model_Insurance_RentRecoveryPlus_LegacyPolicy::PAY_STATUS_CANCELLED || strtotime($policy->getAppData()->getCancelledDate()) > 0) {
                 $message = 'This policy has already been cancelled';
                 $isConfirmed = false;
             } else {
                 $cancellationPeriod = $this->_params->connect->settings->rentRecoveryPlus->cancellationPeriod;
                 $now = new \DateTime();
                 $cancellationEndAt = new \DateTime($policy->getAppData()->getStartDate());
                 $cancellationEndAt = $cancellationEndAt->setTime(23, 59, 59)->add(new \DateInterval($cancellationPeriod));
                 if ($cancellationEndAt <= $now) {
                     $claimsDataSource = new Datasource_Insurance_Claims();
                     $claims = $claimsDataSource->getByPolicyNumber($policyNumber);
                     if ($claims) {
                         if ($claims->getClaimstatus() == $claims::CLAIM_STATUS_ACTIVE) {
                             $message = 'This policy has claims outstanding';
                             $isConfirmed = false;
                         }
                     }
                 }
             }
         }
         /** @var \RRP\Model\RentRecoveryPlusCancellation $cancellation */
         $cancellation = $this->getContainer()->get('rrp.model.cancellation');
         $cancellation->setPolicyNumber($policy->getAppData()->getPolicyNumber())->setPolicyExpiresAt($policy->getAppData()->getEndDate());
         $form = $this->getFormFactory()->create($cancellationType, $cancellation);
         if ($isConfirmed) {
             $form->submit($request);
             if (!$form->isValid()) {
                 $isConfirmed = false;
             }
         }
         if ($isConfirmed) {
             /** @var \RRP\Model\RentRecoveryPlusCancellation $cancellation */
             $cancellation = $form->getData();
             $refundValue = $policy->cancel($cancellation->getPolicyEndAt());
             if ($refundValue !== null) {
                 Manager_Insurance_Quote::sendCancellation($policyNumber, null, null, $this->_params->connect->settings->rentRecoveryPlus->systemCsuID, array('refundValue' => $refundValue));
                 $this->renderTwigView('/rentguarantee/rent-recovery-plus-cancellation.html.twig', array('form' => $form->createView(), 'isConfirmed' => $policyNumber, 'queryPhoneNumber' => $this->_params->connect->settings->rentRecoveryPlus->queryPhoneNumber));
                 return;
             }
         } else {
             if (!empty($policyNumber)) {
                 $cancellationDetails = array('queryPhoneNumber' => $this->_params->connect->settings->rentRecoveryPlus->queryPhoneNumber);
                 if ($message) {
                     $cancellationDetails['message'] = $message;
                 } else {
                     $cancellationDetails['form'] = $form->createView();
                 }
                 $this->renderTwigView('/rentguarantee/rent-recovery-plus-cancellation.html.twig', $cancellationDetails);
                 return;
             }
         }
     }
     $this->renderTwigView('/rentguarantee/rent-recovery-plus-error.html.twig');
 }