Skip to content

Commit

Permalink
Be stricter checking eligible_for_gift_aid variable type
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Jun 28, 2019
1 parent e3972c7 commit 58865b0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 68 deletions.
135 changes: 70 additions & 65 deletions CRM/Civigiftaid/Utils/GiftAid.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public static function getDeclaration($contactID, $date = NULL, $charity = NULL)
CRM_Civigiftaid_Utils_Hook::alterDeclarationQuery($sql, $sqlParams);
$dao = CRM_Core_DAO::executeQuery($sql, $sqlParams);
if ($dao->fetch()) {
$currentDeclaration['id'] = $dao->id;
$currentDeclaration['entity_id'] = $dao->entity_id;
$currentDeclaration['eligible_for_gift_aid'] = $dao->eligible_for_gift_aid;
$currentDeclaration['id'] = (int) $dao->id;
$currentDeclaration['entity_id'] = (int) $dao->entity_id;
$currentDeclaration['eligible_for_gift_aid'] = (int) $dao->eligible_for_gift_aid;
$currentDeclaration['start_date'] = $dao->start_date;
$currentDeclaration['end_date'] = $dao->end_date;
$currentDeclaration['reason_ended'] = $dao->reason_ended;
Expand Down Expand Up @@ -136,8 +136,8 @@ public static function setDeclaration($params) {
2 => [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['id'] = (int) $dao->id;
$futureDeclaration['eligible_for_gift_aid'] = (int) $dao->eligible_for_gift_aid;
$futureDeclaration['start_date'] = $dao->start_date;
$futureDeclaration['end_date'] = $dao->end_date;
}
Expand All @@ -164,68 +164,73 @@ public static function setDeclaration($params) {
$endTimestamp = NULL;
}

if ($params['eligible_for_gift_aid'] == self::DECLARATION_IS_YES) {

if (!$currentDeclaration) {
// There is no current declaration so create new.
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);

} else if ($currentDeclaration['eligible_for_gift_aid'] == self::DECLARATION_IS_YES && $endTimestamp) {
// - if current positive, extend its end_date to new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => date('YmdHis', $endTimestamp),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);

} else if ($currentDeclaration['eligible_for_gift_aid'] == self::DECLARATION_IS_NO || $currentDeclaration['eligible_for_gift_aid'] == self::DECLARATION_IS_PAST_4_YEARS) {
// - if current negative, set its end_date to now and create new ending new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => CRM_Utils_Date::isoToMysql($params['start_date']),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);
}
switch ($params['eligible_for_gift_aid']) {
case self::DECLARATION_IS_YES:
if (!$currentDeclaration) {
// There is no current declaration so create new.
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);
}
elseif ($currentDeclaration['eligible_for_gift_aid'] === self::DECLARATION_IS_YES && $endTimestamp) {
// - if current positive, extend its end_date to new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => date('YmdHis', $endTimestamp),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);

}
elseif ($currentDeclaration['eligible_for_gift_aid'] === self::DECLARATION_IS_NO || $currentDeclaration['eligible_for_gift_aid'] === self::DECLARATION_IS_PAST_4_YEARS) {
// - if current negative, set its end_date to now and create new ending new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => CRM_Utils_Date::isoToMysql($params['start_date']),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);
}
break;

} else if ($params['eligible_for_gift_aid'] == self::DECLARATION_IS_PAST_4_YEARS) {

if (!$currentDeclaration) {
// There is no current declaration so create new.
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);

} else if ($currentDeclaration['eligible_for_gift_aid'] == self::DECLARATION_IS_PAST_4_YEARS && $endTimestamp) {
// - if current positive, extend its end_date to new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => date('YmdHis', $endTimestamp),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);

} else if ($currentDeclaration['eligible_for_gift_aid'] == self::DECLARATION_IS_NO || $currentDeclaration['eligible_for_gift_aid'] == self::DECLARATION_IS_YES) {
// - if current negative, set its end_date to now and create new ending new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => CRM_Utils_Date::isoToMysql($params['start_date']),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);
}
case self::DECLARATION_IS_PAST_4_YEARS:
if (!$currentDeclaration) {
// There is no current declaration so create new.
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);
}
elseif ($currentDeclaration['eligible_for_gift_aid'] === self::DECLARATION_IS_PAST_4_YEARS && $endTimestamp) {
// - if current positive, extend its end_date to new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => date('YmdHis', $endTimestamp),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);

}
elseif ($currentDeclaration['eligible_for_gift_aid'] === self::DECLARATION_IS_NO || $currentDeclaration['eligible_for_gift_aid'] === self::DECLARATION_IS_YES) {
// - if current negative, set its end_date to now and create new ending new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => CRM_Utils_Date::isoToMysql($params['start_date']),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);
}
break;

case self::DECLARATION_IS_NO:
if (!$currentDeclaration) {
// There is no current declaration so create new.
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);
}
elseif ($currentDeclaration['eligible_for_gift_aid'] === self::DECLARATION_IS_YES || $currentDeclaration['eligible_for_gift_aid'] === self::DECLARATION_IS_PAST_4_YEARS) {
// - if current positive, set its end_date to now and create new ending new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => CRM_Utils_Date::isoToMysql($params['start_date']),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);
}
break;

} else if ($params['eligible_for_gift_aid'] == self::DECLARATION_IS_NO) {
if (!$currentDeclaration) {
// There is no current declaration so create new.
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);

} else if ($currentDeclaration['eligible_for_gift_aid'] == self::DECLARATION_IS_YES || $currentDeclaration['eligible_for_gift_aid'] == self::DECLARATION_IS_PAST_4_YEARS) {
// - if current positive, set its end_date to now and create new ending new_end_date.
$updateParams = [
'id' => $currentDeclaration['id'],
'end_date' => CRM_Utils_Date::isoToMysql($params['start_date']),
];
CRM_Civigiftaid_Utils_GiftAid::_updateDeclaration($updateParams);
CRM_Civigiftaid_Utils_GiftAid::_insertDeclaration($params, $endTimestamp);
}
// - if current negative, leave as is.
}

Expand Down
11 changes: 8 additions & 3 deletions civigiftaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,27 @@ function civigiftaid_update_declaration_amount($contributionID) {
'return' => ['contact_id', 'receive_date', 'custom_' . $customFieldID]
]);

// Get the gift aid eligible status
// If it's not a valid number don't do any further processing
$giftAidEligibleStatus = $contribution['custom_' . $customFieldID];
if (is_null($giftAidEligibleStatus)) {
if (!is_numeric($giftAidEligibleStatus)) {
return;
}
else {
$giftAidEligibleStatus = (int) $giftAidEligibleStatus;
}

list($addressDetails, $postCode) = _civigiftaid_civicrm_custom_get_address_and_postal_code($contribution['contact_id'], 1);

$params = [
'entity_id' => $contribution['contact_id'],
'eligible_for_gift_aid' => $giftAidEligibleStatus,
'eligible_for_gift_aid' => (int) $giftAidEligibleStatus,
'start_date' => $contribution['receive_date'],
'address' => $addressDetails,
'post_code' => $postCode,
];
CRM_Civigiftaid_Utils_GiftAid::setDeclaration($params);
if ($giftAidEligibleStatus == CRM_Civigiftaid_Utils_GiftAid::DECLARATION_IS_PAST_4_YEARS || $giftAidEligibleStatus == CRM_Civigiftaid_Utils_GiftAid::DECLARATION_IS_YES) {
if ($giftAidEligibleStatus === CRM_Civigiftaid_Utils_GiftAid::DECLARATION_IS_PAST_4_YEARS || $giftAidEligibleStatus === CRM_Civigiftaid_Utils_GiftAid::DECLARATION_IS_YES) {
CRM_Civigiftaid_Utils_Contribution::updateGiftAidFields($contributionID);
}
}
Expand Down

0 comments on commit 58865b0

Please sign in to comment.