static function isEligibleForGiftAid($contactID, $date = null, $contributionID = null)
 {
     $charity = null;
     if ($contributionID && CRM_Core_DAO::checkFieldExists('civicrm_value_gift_aid_submission', 'charity')) {
         $charity = CRM_Core_DAO::singleValueQuery('SELECT charity FROM civicrm_value_gift_aid_submission WHERE entity_id = %1', array(1 => array($contributionID, 'Integer')));
     }
     $declaration = self::getDeclaration($contactID, $date, $charity);
     $isEligible = $declaration['eligible_for_gift_aid'] == 1;
     // hook can alter the eligibility if needed
     GiftAid_Utils_Hook::giftAidEligible($isEligible, $contactID, $date, $contributionID);
     return $isEligible;
 }
 /**
  * Given an array of contributionIDs, add them to a batch
  *
  * @param array  $contributionIDs (reference ) the array of contribution ids to be added
  * @param int    $batchID - the batchID to be added to
  *
  * @return array             (total, added, notAdded) ids of contributions added to the batch
  * @access public
  * @static
  */
 static function addContributionToBatch($contributionIDs, $batchID)
 {
     $date = date('YmdHis');
     $contributionsAdded = array();
     $contributionsNotAdded = array();
     require_once "GiftAid/Utils/GiftAid.php";
     require_once "CRM/Contribute/BAO/Contribution.php";
     require_once 'CRM/Core/DAO/EntityBatch.php';
     require_once "CRM/Core/BAO/Address.php";
     require_once "CRM/Contact/BAO/Contact.php";
     require_once "CRM/Utils/Address.php";
     $charityColumnExists = CRM_Core_DAO::checkFieldExists('civicrm_value_gift_aid_submission', 'charity');
     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, add to batchContribution else keep the count of contributions that are not added
         if ($batchContribution->find(true)) {
             $contributionsNotAdded[] = $contributionID;
             continue;
         }
         // get additional info
         // get contribution details from Contribution using contribution id
         $params = array('id' => $contributionID);
         CRM_Contribute_BAO_Contribution::retrieve($params, $contribution, $ids);
         $contactId = $contribution['contact_id'];
         // check if contribution is valid for gift aid
         if (GiftAid_Utils_GiftAid::isEligibleForGiftAid($contactId, $contribution['receive_date'], $contributionID)) {
             $batchContribution->batch_id = $batchID;
             $batchContribution->save();
             // get display name
             $displayName = CRM_Contact_BAO_Contact::displayName($contactId);
             // get Address & Postal Code from Address
             $params = array('contact_id' => $contactId, 'is_primary' => 1);
             $address = CRM_Core_BAO_Address::getValues($params);
             $address = $address[1];
             //adds all address lines to the report
             $fullFormatedAddress = CRM_Utils_Address::format($address);
             // get gift aid amount
             $giftAidAmount = self::_calculateGiftAidAmt($contribution['total_amount']);
             // FIXME: check if there is customTable method
             $query = "\nINSERT INTO civicrm_value_gift_aid_submission \n(entity_id, eligible_for_gift_aid, name, address, post_code, amount, gift_aid_amount) \nVALUES \n  ( %1, 1, %2, %3, %4, %5, %6 )\nON DUPLICATE KEY UPDATE \nname      = %2, \naddress   = %3,\npost_code = %4,\namount    = %5,\ngift_aid_amount = %6\n";
             $sqlParams = array(1 => array($contributionID, 'Integer'), 2 => array($displayName, 'String'), 3 => array($fullFormatedAddress, 'String'), 4 => array($address['postal_code'], 'String'), 5 => array($contribution['total_amount'], 'Money'), 6 => array($giftAidAmount, 'Money'));
             CRM_Core_DAO::executeQuery($query, $sqlParams);
             $contributionsAdded[] = $contributionID;
         } else {
             $contributionsNotAdded[] = $contributionID;
         }
     }
     if (!empty($contributionsAdded)) {
         // if there is any extra work required to be done for contributions that are batched,
         // should be done via hook
         GiftAid_Utils_Hook::batchContributions($batchID, $contributionsAdded);
     }
     return array(count($contributionIDs), count($contributionsAdded), count($contributionsNotAdded));
 }