public static function calculatePledgeStatus($pledgeId) {
$paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
+ //return if the pledge is cancelled.
+ $currentPledgeStatus = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeId, 'status_id', 'id', TRUE);
+ if ($currentPledgeStatus == array_search('Cancelled', $paymentStatusTypes)) {
+ return $currentPledgeStatus;
+ }
+
// retrieve all pledge payments for this particular pledge
$allPledgePayments = $allStatus = array();
$returnProperties = array('status_id');
$this->assertEquals(1, $result['count'], " in line " . __LINE__);
}
+ /**
+ * Test status of pledge on payments and cancellation.
+ */
+ public function testPledgeStatus() {
+ //Status should initially be Pending.
+ $checkStatus = $this->callAPISuccess('pledge', 'getsingle', array(
+ 'id' => $this->_pledgeID,
+ 'return' => 'pledge_status',
+ ));
+ $this->assertEquals('Pending', $checkStatus['pledge_status']);
+
+ //Make first payment.
+ $paymentParams = array(
+ 'contact_id' => $this->_individualId,
+ 'pledge_id' => $this->_pledgeID,
+ 'contribution_id' => $this->_contributionID,
+ 'status_id' => 1,
+ );
+ $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams);
+
+ //Status should be 'In Progress' after first payment.
+ $checkStatus = $this->callAPISuccess('pledge', 'getsingle', array(
+ 'id' => $this->_pledgeID,
+ 'return' => 'pledge_status',
+ ));
+ $this->assertEquals('In Progress', $checkStatus['pledge_status']);
+
+ //Cancel the Pledge.
+ $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
+ $updateParams = array(
+ 'id' => $this->_pledgeID,
+ 'status_id' => array_search('Cancelled', $paymentStatusTypes),
+ );
+ $this->callAPISuccess('pledge', 'create', $updateParams);
+
+ //Status should be calculated as Cancelled.
+ $pledgeStatus = CRM_Pledge_BAO_PledgePayment::calculatePledgeStatus($this->_pledgeID);
+ $this->assertEquals('Cancelled', $paymentStatusTypes[$pledgeStatus]);
+
+ //Already completed payments should not be cancelled.
+ $checkPaymentStatus = $this->callAPISuccess('pledge_payment', 'getsingle', array(
+ 'id' => $firstPayment['id'],
+ 'return' => 'status_id',
+ ));
+ $this->assertEquals(array_search('Completed', $paymentStatusTypes), $checkPaymentStatus['status_id']);
+ }
+
+
/**
* Test that passing in a single variable works:: status_id
*/