/**
  * Test update of recurring payments after a deferred submission
  *
  * @author Björn Endres
  * @see https://github.com/Project60/sepa_dd/issues/190
  */
 public function testRCURGracePeriod_190()
 {
     $rcur_notice = 6;
     CRM_Sepa_Logic_Settings::setSetting('batching.RCUR.notice', $rcur_notice);
     CRM_Sepa_Logic_Settings::setSetting('batching.RCUR.grace', 2 * $rcur_notice);
     $contactId = $this->individualCreate();
     $collection_date = strtotime("+1 days");
     $deferred_collection_date = strtotime("+{$rcur_notice} days");
     // count the existing contributions
     $count = $this->callAPISuccess("Contribution", "getcount", array('version' => 3));
     // create a mandate, that's already late
     $parameters = array('version' => 3, 'type' => 'RCUR', 'status' => 'RCUR', 'contact_id' => $contactId, 'financial_type_id' => 1, 'amount' => '6.66', 'start_date' => date('YmdHis'), 'date' => date('YmdHis'), 'cycle_day' => date('d', $collection_date), 'frequency_interval' => 1, 'frequency_unit' => 'month', 'iban' => "BE68844010370034", 'bic' => "TESTTEST", 'creditor_id' => $this->getCreditor(), 'is_enabled' => 1);
     $this->callAPISuccess("SepaMandate", "createfull", $parameters);
     // batch it
     $this->callAPISuccess("SepaAlternativeBatching", "update", array("type" => "RCUR", 'version' => 3));
     // check contributions count again
     $newcount = $this->callAPISuccess("Contribution", "getcount", array('version' => 3));
     $this->assertEquals($count + 1, $newcount, "A contribution should have been created!");
     // adjust collection date, close the group and thus modify the contribution's receive date
     $txgroup = $this->callAPISuccess("SepaTransactionGroup", "getsingle", array('version' => 3));
     CRM_Sepa_BAO_SEPATransactionGroup::adjustCollectionDate($txgroup['id'], date('Y-m-d', $deferred_collection_date));
     CRM_Sepa_Logic_Group::close($txgroup['id']);
     // batch again
     $this->callAPISuccess("SepaAlternativeBatching", "update", array("type" => "RCUR", 'version' => 3));
     // verify, that NO new contribution is created
     $newcount = $this->callAPISuccess("Contribution", "getcount", array('version' => 3));
     $this->assertEquals($count + 1, $newcount, "Yet another contribution has been created. Issue #190 still active!");
 }
 function run()
 {
     if (isset($_REQUEST['group_id'])) {
         if (isset($_REQUEST['status']) && ($_REQUEST['status'] == "missed" || $_REQUEST['status'] == "invalid" || $_REQUEST['status'] == "closed")) {
             $this->assign('status', $_REQUEST['status']);
         } else {
             $_REQUEST['status'] = "";
         }
         $group_id = (int) $_REQUEST['group_id'];
         $this->assign('txgid', $group_id);
         // LOAD/CREATE THE TXFILE
         $group = civicrm_api('SepaTransactionGroup', 'getsingle', array('version' => 3, 'id' => $group_id));
         if (isset($group['is_error']) && $group['is_error']) {
             CRM_Core_Session::setStatus("Cannot load group #{$group_id}.<br/>Error was: " . $group['error_message'], ts('Error'), 'error');
         } else {
             $this->assign('txgroup', $group);
             // check whether this is a group created by a test creditor
             $creditor = civicrm_api('SepaCreditor', 'getsingle', array('version' => 3, 'id' => $group['sdd_creditor_id']));
             if (isset($creditor['is_error']) && $creditor['is_error']) {
                 CRM_Core_Session::setStatus("Cannot load creditor.<br/>Error was: " . $creditor['error_message'], ts('Error'), 'error');
             } else {
                 $isTestGroup = isset($creditor['category']) && $creditor['category'] == "TEST";
                 $this->assign('is_test_group', $isTestGroup);
                 if ($_REQUEST['status'] == "") {
                     // first adjust group's collection date if requested
                     if (!empty($_REQUEST['adjust'])) {
                         $result = CRM_Sepa_BAO_SEPATransactionGroup::adjustCollectionDate($group_id, $_REQUEST['adjust']);
                         if (is_string($result)) {
                             // that's an error -> stop here!
                             die($result);
                         } else {
                             // that went well, so result should be the update group data
                             $group = $result;
                         }
                     }
                     // delete old txfile
                     if (!empty($group['sdd_file_id'])) {
                         $result = civicrm_api('SepaSddFile', 'delete', array('id' => $group['sdd_file_id'], 'version' => 3));
                         if (isset($result['is_error']) && $result['is_error']) {
                             CRM_Core_Session::setStatus("Cannot delete file #" . $group['sdd_file_id'] . ".<br/>Error was: " . $result['error_message'], ts('Error'), 'error');
                         }
                     }
                     $xmlfile = civicrm_api('SepaAlternativeBatching', 'createxml', array('txgroup_id' => $group_id, 'override' => True, 'version' => 3));
                     if (isset($xmlfile['is_error']) && $xmlfile['is_error']) {
                         CRM_Core_Session::setStatus("Cannot load for group #" . $group_id . ".<br/>Error was: " . $xmlfile['error_message'], ts('Error'), 'error');
                     } else {
                         $file_id = $xmlfile['id'];
                         $this->assign('file_link', CRM_Utils_System::url('civicrm/sepa/xml', "id={$file_id}"));
                         $this->assign('file_name', $xmlfile['filename']);
                     }
                 }
                 if ($_REQUEST['status'] == "closed" && !$isTestGroup) {
                     // CLOSE THE GROUP:
                     $result = civicrm_api('SepaAlternativeBatching', 'close', array('version' => 3, 'txgroup_id' => $group_id));
                     if ($result['is_error']) {
                         CRM_Core_Session::setStatus("Cannot close group #{$group_id}.<br/>Error was: " . $result['error_message'], ts('Error'), 'error');
                     }
                 }
             }
         }
     }
     parent::run();
 }