Skip to content

Commit

Permalink
Add API to update the eligible_for_gift_aid flag on contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Jul 28, 2019
1 parent 421291d commit ee738bc
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
17 changes: 15 additions & 2 deletions CRM/Civigiftaid/Utils/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,20 @@ public static function addContributionToBatch($contributionIDs, $batchID) {

/**
* @param int $contributionID
* @param string $batchName - if this is set to NULL it will NOT be changed
* @param int $eligibleForGiftAid - if this is NULL if will NOT be set, otherwise set it to eg CRM_Civigiftaid_Utils_GiftAid::DECLARATION_IS_YES
*
* @throws \CRM_Extension_Exception
* @throws \CiviCRM_API3_Exception
*/
/**
* @param $contributionID
* @param string $batchName
*
* @throws \CRM_Extension_Exception
* @throws \CiviCRM_API3_Exception
*/
public static function updateGiftAidFields($contributionID, $batchName = '') {
public static function updateGiftAidFields($contributionID, $batchName = '', $eligibleForGiftAid = NULL) {
$totalAmount = civicrm_api3('Contribution', 'getvalue', [
'return' => "total_amount",
'id' => $contributionID,
Expand All @@ -118,8 +126,13 @@ public static function updateGiftAidFields($contributionID, $batchName = '') {
'id' => $contributionID,
CRM_Civigiftaid_Utils::getCustomByName('gift_aid_amount', $groupID) => $giftAidAmount,
CRM_Civigiftaid_Utils::getCustomByName('amount', $groupID) => $giftAidableContribAmt,
CRM_Civigiftaid_Utils::getCustomByName('batch_name', $groupID) => $batchName,
];
if ($batchName !== NULL) {
$contributionParams[CRM_Civigiftaid_Utils::getCustomByName('batch_name', $groupID)] = $batchName;
}
if ($eligibleForGiftAid) {
$contributionParams[CRM_Civigiftaid_Utils::getCustomByName('Eligible_for_Gift_Aid', $groupID)] = $eligibleForGiftAid;
}
civicrm_api3('Contribution', 'create', $contributionParams);
}

Expand Down
6 changes: 4 additions & 2 deletions CRM/Civigiftaid/Utils/GiftAid.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,13 @@ public static function isContributionEligible($contribution) {
'name' => "gift_aid",
]);

if (CRM_Utils_Array::value(CRM_Civigiftaid_Utils::getCustomByName('Eligible_for_Gift_Aid', $groupID), $contribution) == self::DECLARATION_IS_NO) {
$contributionEligible = CRM_Utils_Array::value(CRM_Civigiftaid_Utils::getCustomByName('Eligible_for_Gift_Aid', $groupID), $contribution);
// If it is not set ('') it's not the same as DECLARATION_IS_NO
if (!empty($contributionEligible) && ($contributionEligible == self::DECLARATION_IS_NO)) {
return FALSE;
}

foreach($declarations as $declaration) {
foreach ($declarations as $declaration) {
if($declaration['eligible_for_gift_aid'] == self::DECLARATION_IS_PAST_4_YEARS) {
$declaration['start_date'] = self::dateFourYearsAgo($declaration['start_date']);
}
Expand Down
58 changes: 56 additions & 2 deletions api/v3/GiftAid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function civicrm_api3_gift_aid_makepastyearsubmissions($params) {
// get all contacts with declarations
$contacts = CRM_Civigiftaid_Utils_GiftAid::getContactsWithDeclarations();

// get current declarations of contacts
$currentDeclarations = CRM_Civigiftaid_Utils_GiftAid::getCurrentDeclarations($contacts);

Expand All @@ -30,6 +30,60 @@ function civicrm_api3_gift_aid_makepastyearsubmissions($params) {
}

}

return civicrm_api3_create_success(1, $params);
}

function _civicrm_api3_gift_aid_updateeligiblecontributions_spec($params) {
$params['contribution_id']['title'] = 'Contribution ID';
$params['contribution_id']['type'] = CRM_Utils_Type::T_INT;
}

function civicrm_api3_gift_aid_updateeligiblecontributions($params) {
$groupID = civicrm_api3('CustomGroup', 'getvalue', [
'return' => "id",
'name' => "gift_aid",
]);
$contributionParams = [
'return' => [
'id',
'contact_id',
'contribution_status_id',
'receive_date',
CRM_Civigiftaid_Utils::getCustomByName('batch_name', $groupID),
CRM_Civigiftaid_Utils::getCustomByName('Eligible_for_Gift_Aid', $groupID)
],
CRM_Civigiftaid_Utils::getCustomByName('Eligible_for_Gift_Aid', $groupID) => ['IS NULL' => 1],
'options' => ['limit' => 0],
];
if (!empty($params['contribution_id'])) {
$contributionParams['id'] = $params['contribution_id'];
}
$contributions = civicrm_api3('Contribution', 'get', $contributionParams);
if (empty($contributions['count'])) {
return civicrm_api3_create_error('No contributions found or all have Eligible flag set!');
}
$contributions = $contributions['values'];

$updatedIDs = [];
foreach ($contributions as $key => $contribution) {
$contact = civicrm_api3('Contact', 'getsingle', [
'return' => ["contact_type"],
'id' => $contribution['contact_id'],
]);
if ($contact['contact_type'] !== 'Individual') {
unset($contributions[$key]);
continue;
}
if (!CRM_Civigiftaid_Utils_GiftAid::isEligibleForGiftAid($contribution)) {
unset($contributions[$key]);
continue;
}

// This must be an eligible contribution - update it
// We don't touch batchName
CRM_Civigiftaid_Utils_Contribution::updateGiftAidFields($contribution['id'], NULL, CRM_Civigiftaid_Utils_GiftAid::DECLARATION_IS_YES);
$updatedIDs[] = $contribution['id'];
}
return civicrm_api3_create_success(['updated' => $updatedIDs], $params, 'gift_aid', 'updateeligiblecontributions');
}

0 comments on commit ee738bc

Please sign in to comment.