From e7212d8609c52dd70ec35fb1f3735da51260fc29 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Tue, 25 Jul 2017 12:49:02 +0530 Subject: [PATCH] Fix updatePledgeStatus execution to use pledge_status og values --- CRM/Pledge/BAO/Pledge.php | 33 +++++++++----------- CRM/Pledge/BAO/PledgePayment.php | 4 ++- tests/phpunit/api/v3/PledgePaymentTest.php | 35 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/CRM/Pledge/BAO/Pledge.php b/CRM/Pledge/BAO/Pledge.php index ead46a0a07..4cde98bb67 100644 --- a/CRM/Pledge/BAO/Pledge.php +++ b/CRM/Pledge/BAO/Pledge.php @@ -853,20 +853,15 @@ GROUP BY currency $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 = " @@ -895,7 +890,7 @@ SELECT pledge.contact_id as contact_id, ) 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 "; @@ -933,23 +928,23 @@ SELECT pledge.contact_id as contact_id, 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; } } diff --git a/CRM/Pledge/BAO/PledgePayment.php b/CRM/Pledge/BAO/PledgePayment.php index 2319d0b3d8..fd44137756 100644 --- a/CRM/Pledge/BAO/PledgePayment.php +++ b/CRM/Pledge/BAO/PledgePayment.php @@ -349,7 +349,9 @@ WHERE pledge_id = %1 $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)) { diff --git a/tests/phpunit/api/v3/PledgePaymentTest.php b/tests/phpunit/api/v3/PledgePaymentTest.php index 8b03ae5d18..acda41b2a7 100644 --- a/tests/phpunit/api/v3/PledgePaymentTest.php +++ b/tests/phpunit/api/v3/PledgePaymentTest.php @@ -87,6 +87,41 @@ class api_v3_PledgePaymentTest extends CiviUnitTestCase { $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. */ -- 2.25.1