/** * @dataProvider dataProvider */ public function testDeclarations($input, $output) { $count = 0; $params = $tableRows = array(); $declarationFields = array('entity_id', 'eligible_for_gift_aid', 'start_date', 'end_date'); foreach ($declarationFields as $field) { $params[$field] = $input[$count]; $count++; } $result = GiftAid_Utils_GiftAid::setDeclaration($params); $sql = "select * from civicrm_value_gift_aid_declaration"; $dao =& CRM_Core_DAO::executeQuery($sql); $count = 0; while ($dao->fetch()) { foreach ($declarationFields as $field) { $tableRows[$count][] = $dao->{$field}; } $count++; } $this->assertEquals($output, $tableRows, 'In line ' . __LINE__); }
/** * Create / update Gift Aid declaration records on Individual when * "Eligible for Gift Aid" field on Contribution is updated. * See http://wiki.civicrm.org/confluence/display/CRM/Gift+aid+implementation * * TODO change arguments to single $param array * @param array $params - fields to store in declaration: * - entity_id: the Individual for whom we will create/update declaration * - eligible_for_gift_aid: 1 for positive declaration, 0 for negative * - start_date: start date of declaration (in ISO date format) * - end_date: end date of declaration (in ISO date format) * * @return array TODO * @access public * @static */ static function setDeclaration($params) { if (!CRM_Utils_Array::value('entity_id', $params)) { return array('is_error' => 1, 'error_message' => 'entity_id is required'); } // Retrieve existing declarations for this user. $currentDeclaration = GiftAid_Utils_GiftAid::getDeclaration($params['entity_id'], $params['start_date']); // Get future declarations: start_date in future, end_date in future or null // - if > 1, pick earliest start_date $futureDeclaration = array(); $sql = "\n SELECT id, eligible_for_gift_aid, start_date, end_date\n FROM civicrm_value_gift_aid_declaration\n WHERE entity_id = %1 AND start_date > %2 AND (end_date > %2 OR end_date IS NULL)\n ORDER BY start_date"; $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($params['entity_id'], 'Integer'), 2 => array(CRM_Utils_Date::isoToMysql($params['start_date']), 'Timestamp'))); if ($dao->fetch()) { $futureDeclaration['id'] = $dao->id; $futureDeclaration['eligible_for_gift_aid'] = $dao->eligible_for_gift_aid; $futureDeclaration['start_date'] = $dao->start_date; $futureDeclaration['end_date'] = $dao->end_date; } #CRM_Core_Error::debug('futureDeclaration', $futureDeclaration); $specifiedEndTimestamp = null; if (CRM_Utils_Array::value('end_date', $params)) { $specifiedEndTimestamp = strtotime(CRM_Utils_Array::value('end_date', $params)); } // Calculate new_end_date for negative declaration // - new_end_date = // if end_date specified then (specified end_date) // else (start_date of first future declaration if any, else null) $futureTimestamp = null; if (CRM_Utils_Array::value('start_date', $futureDeclaration)) { $futureTimestamp = strtotime(CRM_Utils_Array::value('start_date', $futureDeclaration)); } if ($specifiedEndTimestamp) { $endTimestamp = $specifiedEndTimestamp; } else { if ($futureTimestamp) { $endTimestamp = $futureTimestamp; } else { $endTimestamp = null; } } if ($params['eligible_for_gift_aid'] == 1) { if (!$currentDeclaration) { // There is no current declaration so create new. GiftAid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp); } else { if ($currentDeclaration['eligible_for_gift_aid'] == 1) { // - if current positive, extend its end_date to new_end_date. $updateParams = array('id' => $currentDeclaration['id'], 'end_date' => date('YmdHis', $endTimestamp)); GiftAid_Utils_GiftAid::_updateDeclaration($updateParams); } else { if ($currentDeclaration['eligible_for_gift_aid'] == 0) { // - if current negative, set its end_date to now and create new ending new_end_date. $updateParams = array('id' => $currentDeclaration['id'], 'end_date' => CRM_Utils_Date::isoToMysql($params['start_date'])); GiftAid_Utils_GiftAid::_updateDeclaration($updateParams); GiftAid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp); } } } } else { if ($params['eligible_for_gift_aid'] == 0) { if (!$currentDeclaration) { // There is no current declaration so create new. GiftAid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp); } else { if ($currentDeclaration['eligible_for_gift_aid'] == 1) { // - if current positive, set its end_date to now and create new ending new_end_date. $updateParams = array('id' => $currentDeclaration['id'], 'end_date' => CRM_Utils_Date::isoToMysql($params['start_date'])); GiftAid_Utils_GiftAid::_updateDeclaration($updateParams); GiftAid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp); } } // - if current negative, leave as is. } } return array('is_error' => 0); }
static function _validateContributionToBatch(&$contributionIDs) { $contributionsAdded = array(); $contributionsAlreadyAdded = array(); $contributionsNotValid = array(); require_once "GiftAid/Utils/GiftAid.php"; require_once "CRM/Core/DAO/EntityBatch.php"; require_once "CRM/Contribute/BAO/Contribution.php"; foreach ($contributionIDs as $contributionID) { $batchContribution =& new CRM_Core_DAO_EntityBatch(); $batchContribution->entity_table = 'civicrm_contribution'; $batchContribution->entity_id = $contributionID; // check if the selected contribution id already in a batch // if not, increment $numContributionsAdded else keep the count of contributions that are already added if (!$batchContribution->find(true)) { // get contact_id, & contribution receive date from Contribution using contribution id $params = array('id' => $contributionID); CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $ids); // check if contribution is not valid for gift aid, increment $numContributionsNotValid if (GiftAid_Utils_GiftAid::isEligibleForGiftAid($defaults['contact_id'], $defaults['receive_date'], $contributionID)) { $contributionsAdded[] = $contributionID; } else { $contributionsNotValid[] = $contributionID; } } else { $contributionsAlreadyAdded[] = $contributionID; } } return array(count($contributionIDs), $contributionsAdded, $contributionsAlreadyAdded, $contributionsNotValid); }