コード例 #1
0
 /**
  * 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);
 }