Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to gift aid batch validation via civi queue system #65

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 150 additions & 21 deletions CRM/Civigiftaid/Form/Task/AddToBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

class CRM_Civigiftaid_Form_Task_AddToBatch extends CRM_Contribute_Form_Task {

const VALIDATION_QUEUE_BATCH_LIMIT = 10;

protected $_id = NULL;

/**
Expand All @@ -53,33 +55,40 @@ class CRM_Civigiftaid_Form_Task_AddToBatch extends CRM_Contribute_Form_Task {
function preProcess() {
parent::preProcess();

require_once 'CRM/Civigiftaid/Utils/Contribution.php';
list($total, $added, $alreadyAdded, $notValid) =
CRM_Civigiftaid_Utils_Contribution::validateContributionToBatch($this->_contributionIds);
$isStatsDone = CRM_Utils_Request::retrieve('processed', 'Boolean', $this, FALSE, 0);
if (empty($isStatsDone)) {
$runner = $this->getRunner($this->_contributionIds);
if ($runner) {
$runner->runAllViaWeb();
}
}
list( $total, $added, $alreadyAdded, $notValid ) = $this->getValidationStats();
if (in_array($this->controller->getButtonName(), array('_qf_AddToBatch_back', '_qf_AddToBatch_next'))) {
// reset the flag, so it's revalidated next time.
$this->set('processed', 0);
}

$this->assign('selectedContributions', $total);
$this->assign('totalAddedContributions', count($added));
$this->assign('alreadyAddedContributions', count($alreadyAdded));
$this->assign('notValidContributions', count($notValid));

// get details of contribution that will be added to this batch.
$contributionsAddedRows =
CRM_Civigiftaid_Utils_Contribution::getContributionDetails($added);
$this->assign('contributionsAddedRows', $contributionsAddedRows);

// get details of contribution thatare already added to this batch.
$contributionsAlreadyAddedRows = array();
$contributionsAlreadyAddedRows =
CRM_Civigiftaid_Utils_Contribution::getContributionDetails($alreadyAdded);
$this->assign(
'contributionsAlreadyAddedRows',
$contributionsAlreadyAddedRows
);
$qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);

// URL to view contributions that will be added to this batch
$tobeAddedUrlParams = 'status=tobeadded&qfKey='.$qfKey;
$contributionsTobeAddedUrl = CRM_Utils_System::url('civicrm/addtobatch/summary', $tobeAddedUrlParams);
$this->assign('contributionsTobeAddedUrl', $contributionsTobeAddedUrl );

// URL to view contributions that are already added to this batch
$alreadyAddedUrlParams = 'status=alreadyadded&qfKey='.$qfKey;
$contributionsAlreadyAddedUrl = CRM_Utils_System::url('civicrm/addtobatch/summary', $alreadyAddedUrlParams);
$this->assign('contributionsAlreadyAddedUrl', $contributionsAlreadyAddedUrl );

// get details of contribution that are not valid for giftaid
$contributionsNotValid = array();
$contributionsNotValid =
CRM_Civigiftaid_Utils_Contribution::getContributionDetails($notValid);
$this->assign('contributionsNotValid', $contributionsNotValid);
// URL to view contributions that are not valid for giftaid
$invalidUrlParams = 'status=invalid&qfKey='.$qfKey;
$contributionsInvalidUrl = CRM_Utils_System::url('civicrm/addtobatch/summary', $invalidUrlParams);
$this->assign('contributionsInvalidUrl', $contributionsInvalidUrl );
}

/**
Expand Down Expand Up @@ -195,4 +204,124 @@ public function postProcess() {
$transaction->commit();
CRM_Core_Session::setStatus($status);
}//end of function

/**
* Build a queue of tasks by dividing contributions in sets.
*/
function getRunner($contributionIds) {
$queue = CRM_Queue_Service::singleton()->create(array(
'name' => 'ADD_TO_GIFTAID',
'type' => 'Sql',
'reset' => TRUE,
));
$qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
$total = count($contributionIds);
$batchLimit = self::VALIDATION_QUEUE_BATCH_LIMIT;
for ($i = 0; $i < ceil($total/$batchLimit); $i++) {
$start = $i * $batchLimit;
$contribIds = array_slice($contributionIds, $start, $batchLimit, TRUE);
$task = new CRM_Queue_Task(
array ('CRM_Civigiftaid_Form_Task_AddToBatch', 'validateContributionToBatchLimit'),
array($contribIds, $qfKey),
"Validated " . $i*$batchLimit . " contributions out of " . $total
);
$queue->createItem($task);
}
// Setup the Runner
$url = CRM_Utils_System::url(CRM_Utils_System::currentPath(), "_qf_AddToBatch_display=1&qfKey={$this->controller->_key}&processed=1", FALSE, NULL, FALSE);
$runner = new CRM_Queue_Runner(array(
'title' => ts('Validating Contributions..'),
'queue' => $queue,
'errorMode'=> CRM_Queue_Runner::ERROR_ABORT,
'onEndUrl' => $url
));
// reset stats
self::resetValidationStats($qfKey);

return $runner;
}

/**
* Get validation stats from cache.
*
* @return array
*/
function getValidationStats() {
$qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
$cache = self::getCache();
$stats = $cache->get(self::getCacheKey($qfKey));
return array(
empty($stats['total']) ? 0 : $stats['total'],
$stats['added'],
$stats['alreadyAdded'],
$stats['notValid']
);
}

/**
* Reset validation stats for giftaid
*
* @return array
*/
static function resetValidationStats($qfKey) {
$cache = self::getCache();
$key = self::getCacheKey($qfKey);
$cache->set($key, CRM_Core_DAO::$_nullArray);
}

/**
* Carry out batch validations.
*
* @param \CRM_Queue_TaskContext $ctx
* @param array $contributionIds
*
* @return int
*/
static function validateContributionToBatchLimit(CRM_Queue_TaskContext $ctx, $contributionIds, $qfKey) {
list( $total, $added, $alreadyAdded, $notValid ) =
CRM_Civigiftaid_Utils_Contribution::validateContributionToBatch($contributionIds);
$cache = self::getCache();
$key = self::getCacheKey($qfKey);
$stats = $cache->get($key);
if (empty($stats)) {
$stats = array(
'total' => $total,
'added' => $added,
'alreadyAdded' => $alreadyAdded,
'notValid' => $notValid,
);
$cache->set($key, $stats);
} else {
$stats = array(
'total' => $stats['total'] + $total,
'added' => array_merge($stats['added'], $added),
'alreadyAdded' => array_merge($stats['alreadyAdded'], $alreadyAdded),
'notValid' => array_merge($stats['notValid'], $notValid),
);
$cache->set($key, $stats);
}
return CRM_Queue_Task::TASK_SUCCESS;
}

/**
* Fetch cache object.
*
* @return object CRM_Utils_Cache_SqlGroup
*/
static function getCache() {
$cache = new CRM_Utils_Cache_SqlGroup(array(
'group' => 'Civigiftaid',
));
return $cache;
}

/**
* Fetch cache key.
*
* @return string cache key
*/
static function getCacheKey($qfKey) {
return "civigiftaid_addtobatch_stats_{$qfKey}";
}

}
54 changes: 54 additions & 0 deletions CRM/Civigiftaid/Page/AddToBatchSummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

class CRM_Civigiftaid_Page_AddToBatchSummary extends CRM_Core_Page {

public function run() {

$status = CRM_Utils_Array::value('status', $_GET);

// get validated stats
list( $total, $added, $alreadyAdded, $notValid ) = CRM_Civigiftaid_Form_Task_AddToBatch::getValidationStats();

// get title & contributionIds depends on the status
switch ($status) {
case 'tobeadded':
$contributionIds = $added;
$title = 'Contributions that will be added to this batch';
break;

case 'alreadyadded':
$contributionIds = $alreadyAdded;
$title = 'Contributions already in a batch';
break;

case 'invalid':
$contributionIds = $notValid;
$title = 'Contributions not valid for gift aid';
break;

default:
$contributionIds = array();
$title = '';
break;
}

CRM_Utils_System::setTitle(ts($title));

$contributionsRows = array();
$errorMessage = '';
if (!empty($contributionIds)) {

$contributionsRows = CRM_Civigiftaid_Utils_Contribution::getContributionDetails ($contributionIds);

} else {
$errorMessage = 'No contribution records to display. Please contact admin!';
CRM_Core_Error::debug_var('No contribution Ids received in CRM_Civigiftaid_Page_AddToBatchSummary : ', $_GET);
}

$this->assign('contributionsRows', $contributionsRows);
$this->assign('errorMessage', $errorMessage);

parent::run();
}

}
11 changes: 6 additions & 5 deletions CRM/Civigiftaid/Utils/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public static function addContributionToBatch($contributionIDs, $batchID) {
);

foreach ($contributionIDs as $contributionID) {
//$batchContribution =& new CRM_Core_DAO_EntityBatch( );
$batchContribution =& new CRM_Batch_DAO_EntityBatch();
//$batchContribution = new CRM_Core_DAO_EntityBatch( );
$batchContribution = new CRM_Batch_DAO_EntityBatch();
$batchContribution->entity_table = 'civicrm_contribution';
$batchContribution->entity_id = $contributionID;

Expand Down Expand Up @@ -187,7 +187,7 @@ public static function removeContributionFromBatch($contributionIDs) {
foreach ($contributions as $contribution) {
if (!empty($contribution['batch_id'])) {

$batchContribution =& new CRM_Batch_DAO_EntityBatch();
$batchContribution = new CRM_Batch_DAO_EntityBatch();
$batchContribution->entity_table = 'civicrm_contribution';
$batchContribution->entity_id = $contribution['contribution_id'];
$batchContribution->batch_id = $contribution['batch_id'];
Expand Down Expand Up @@ -343,7 +343,7 @@ public static function validationRemoveContributionFromBatch(&$contributionIDs)

foreach ($contributionIDs as $contributionID) {

$batchContribution =& new CRM_Batch_DAO_EntityBatch();
$batchContribution = new CRM_Batch_DAO_EntityBatch();
$batchContribution->entity_table = 'civicrm_contribution';
$batchContribution->entity_id = $contributionID;

Expand Down Expand Up @@ -395,7 +395,7 @@ public static function validateContributionToBatch(&$contributionIDs) {
require_once "CRM/Contribute/BAO/Contribution.php";

foreach ($contributionIDs as $contributionID) {
$batchContribution =& new CRM_Batch_DAO_EntityBatch();
$batchContribution = new CRM_Batch_DAO_EntityBatch();
$batchContribution->entity_table = 'civicrm_contribution';
$batchContribution->entity_id = $contributionID;

Expand Down Expand Up @@ -423,6 +423,7 @@ public static function validateContributionToBatch(&$contributionIDs) {
}
}

CRM_Core_DAO::freeResult();
return array(
count($contributionIDs),
$contributionsAdded,
Expand Down
Loading