/**
  * Constructor and getter for the singleton instance
  * @return instance of $config->userHookClass
  */
 static function singleton()
 {
     if (self::$_singleton == null) {
         $config = CRM_Core_Config::singleton();
         $class = $config->userHookClass;
         require_once str_replace('_', DIRECTORY_SEPARATOR, $config->userHookClass) . '.php';
         self::$_singleton = new $class();
     }
     return self::$_singleton;
 }
 /**
  * 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
  */
 public static function addContributionToBatch($contributionIDs, $batchID)
 {
     $date = date('YmdHis');
     $contributionsAdded = array();
     $contributionsNotAdded = array();
     require_once "CRM/Civigiftaid/Utils/GiftAid.php";
     require_once "CRM/Contribute/BAO/Contribution.php";
     require_once 'CRM/Batch/DAO/EntityBatch.php';
     require_once "CRM/Core/BAO/Address.php";
     require_once "CRM/Contact/BAO/Contact.php";
     require_once "CRM/Utils/Address.php";
     // Get the batch name
     require_once 'CRM/Batch/DAO/Batch.php';
     $batch = new CRM_Batch_DAO_Batch();
     $batch->id = $batchID;
     $batch->find(TRUE);
     $batchName = $batch->title;
     $batchNameGroup = civicrm_api('OptionGroup', 'getsingle', array('version' => 3, 'sequential' => 1, 'name' => 'giftaid_batch_name'));
     if ($batchNameGroup['id']) {
         $groupId = $batchNameGroup['id'];
         $params = array('version' => 3, 'sequential' => 1, 'option_group_id' => $groupId, 'value' => $batchName, 'label' => $batchName);
         $result = civicrm_api('OptionValue', 'create', $params);
     }
     $charityColumnExists = CRM_Core_DAO::checkFieldExists('civicrm_value_gift_aid_submission', 'charity');
     foreach ($contributionIDs as $contributionID) {
         //$batchContribution =& new CRM_Core_DAO_EntityBatch( );
         $batchContribution =& new CRM_Batch_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 (CRM_Civigiftaid_Utils_GiftAid::isEligibleForGiftAid($contactId, $contribution['receive_date'], $contributionID) and $contribution['contribution_status_id'] == 1) {
             $batchContribution->batch_id = $batchID;
             $batchContribution->save();
             $giftAidableContribAmt = self::getGiftAidableContribAmt($contribution['total_amount'], $contributionID);
             // get gift aid amount
             $giftAidAmount = static::calculateGiftAidAmt($giftAidableContribAmt);
             // FIXME: check if there is customTable method
             $query = "\n                          INSERT INTO civicrm_value_gift_aid_submission\n                          (entity_id, eligible_for_gift_aid, gift_aid_amount , amount , batch_name)\n                          VALUES\n                            ( %1, 1, %2, %3 , %4 )\n                          ON DUPLICATE KEY UPDATE\n                          gift_aid_amount = %2 ,\n                          amount = %3 ,\n                          batch_name = %4\n                          ";
             $sqlParams = array(1 => array($contributionID, 'Integer'), 2 => array($giftAidAmount, 'Money'), 3 => array($contribution['total_amount'], 'Money'), 4 => array($batchName, 'String'));
             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
         CRM_Civigiftaid_Utils_Hook::batchContributions($batchID, $contributionsAdded);
     }
     return array(count($contributionIDs), count($contributionsAdded), count($contributionsNotAdded));
 }
 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);
     if (isset($declaration['eligible_for_gift_aid'])) {
         $isEligible = $declaration['eligible_for_gift_aid'] == 1;
     }
     // hook can alter the eligibility if needed
     CRM_Civigiftaid_Utils_Hook::giftAidEligible($isEligible, $contactID, $date, $contributionID);
     return $isEligible;
 }