Skip to content

Commit

Permalink
Fix updatePledgeStatus execution to use pledge_status og values
Browse files Browse the repository at this point in the history
  • Loading branch information
Jitendra Purohit committed Jul 25, 2017
1 parent 3ec9410 commit 8b0d660
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
33 changes: 14 additions & 19 deletions CRM/Pledge/BAO/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -853,20 +853,15 @@ public static function updatePledgeStatus($params) {

$sendReminders = CRM_Utils_Array::value('send_reminders', $params, FALSE);

$allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');

// unset statues that we never use for pledges
foreach (array(
'Completed',
'Cancelled',
'Failed',
) as $statusKey) {
if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
unset($allStatus[$key]);
}
}
$allStatus = array_flip(CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'));
$allPledgeStatus = CRM_Core_OptionGroup::values('pledge_status',
TRUE, FALSE, FALSE, NULL, 'name', TRUE
);
unset($allPledgeStatus['Completed'], $allPledgeStatus['Cancelled']);
unset($allStatus['Completed'], $allStatus['Cancelled'], $allStatus['Failed']);

$statusIds = implode(',', array_keys($allStatus));
$statusIds = implode(',', $allStatus);
$pledgeStatusIds = implode(',', $allPledgeStatus);
$updateCnt = 0;

$query = "
Expand Down Expand Up @@ -895,7 +890,7 @@ public static function updatePledgeStatus($params) {
) as amount_paid
FROM civicrm_pledge pledge, civicrm_pledge_payment payment
WHERE pledge.id = payment.pledge_id
AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$statusIds} )
AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$pledgeStatusIds} )
GROUP By payment.id
";

Expand Down Expand Up @@ -933,23 +928,23 @@ public static function updatePledgeStatus($params) {

if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($dao->scheduled_date, '%Y%m%d'),
$now
) && $dao->payment_status != array_search('Overdue', $allStatus)
) && $dao->payment_status != $allStatus['Overdue']
) {
$pledgePayments[$dao->pledge_id][$dao->payment_id] = $dao->payment_id;
}
}
$allPledgeStatus = array_flip($allPledgeStatus);

// process the updating script...

foreach ($pledgePayments as $pledgeId => $paymentIds) {
// 1. update the pledge /pledge payment status. returns new status when an update happens
$returnMessages[] = "Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allStatus[$pledgeStatus[$pledgeId]]})";
$returnMessages[] = "Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allPledgeStatus[$pledgeStatus[$pledgeId]]})";

$newStatus = CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIds,
array_search('Overdue', $allStatus), NULL, 0, FALSE, TRUE
$allStatus['Overdue'], NULL, 0, FALSE, TRUE
);
if ($newStatus != $pledgeStatus[$pledgeId]) {
$returnMessages[] = "- status updated to: {$allStatus[$newStatus]}";
$returnMessages[] = "- status updated to: {$allPledgeStatus[$newStatus]}";
$updateCnt += 1;
}
}
Expand Down
4 changes: 3 additions & 1 deletion CRM/Pledge/BAO/PledgePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ public static function updatePledgePaymentStatus(
$editScheduled = FALSE;

// get all statuses
$allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$allStatus = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'name', TRUE
);

// if we get do not get contribution id means we are editing the scheduled payment.
if (!empty($paymentIDs)) {
Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/api/v3/PledgePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,41 @@ public function testGetSinglePledgePayment() {
$this->assertEquals(1, $result['count'], " in line " . __LINE__);
}

/**
* Test process_pledge job log.
*/
public function testProcessPledgeJob() {
$pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'name'
);
//Make first payment.
$paymentParams = array(
'contact_id' => $this->_individualId,
'pledge_id' => $this->_pledgeID,
'contribution_id' => $this->_contributionID,
'scheduled_date' => date('Ymd',strtotime("-1 days")),
'status_id' => array_search('Pending', $pledgeStatuses),
);
$firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams);
//Status should be 'Pending' after first incomplete payment.
$checkStatus = $this->callAPISuccess('pledge', 'getsingle', array(
'id' => $this->_pledgeID,
'return' => 'pledge_status',
));
$this->assertEquals('Pending', $checkStatus['pledge_status']);

//Execute process_pledge job log.
$result = $this->callAPISuccess('Job', 'process_pledge', array());
$this->assertEquals("Checking if status update is needed for Pledge Id: {$this->_pledgeID} (current status is Pending)\n\r- status updated to: Overdue\n\r1 records updated.", $result['values']);

//Status should be 'Overdue' after processing.
$statusAfterProcessing = $this->callAPISuccess('pledge', 'getsingle', array(
'id' => $this->_pledgeID,
'return' => 'pledge_status',
));
$this->assertEquals('Overdue', $statusAfterProcessing['pledge_status']);
}

/**
* Test status of pledge on payments and cancellation.
*/
Expand Down

0 comments on commit 8b0d660

Please sign in to comment.