From edb4c74db23ebfe7456da68ee379bcc6cfac2dd8 Mon Sep 17 00:00:00 2001 From: Eileen Date: Wed, 16 Jul 2014 04:21:41 -0400 Subject: [PATCH] CRM-14995 contributions disappearing on pledge edit Conflicts: CRM/Pledge/Form/Pledge.php --- CRM/Pledge/BAO/Pledge.php | 51 ++++++++++++++++++++++++++++++++++++++ CRM/Pledge/Form/Pledge.php | 33 ++---------------------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/CRM/Pledge/BAO/Pledge.php b/CRM/Pledge/BAO/Pledge.php index f4c92d8478..cd6319df1e 100644 --- a/CRM/Pledge/BAO/Pledge.php +++ b/CRM/Pledge/BAO/Pledge.php @@ -1073,5 +1073,56 @@ SELECT pledge.contact_id as contact_id, } return $paymentIDs; } + + /** + * Is this pledge free from financial transactions (this is important to know as we allow editing + * when no transactions have taken place - the editing process currently involves deleting all pledge payments & contributions + * & recreating so we want to block that if appropriate + * + * @param integer $pledgeID + * @return bool do financial transactions exist for this pledge? + */ + static function pledgeHasFinancialTransactions($pledgeID, $pledgeStatusID) { + if (empty($pledgeStatusID)) { + //why would this happen? If we can see where it does then we can see if we should look it up + //but assuming from form code it CAN be empty + return TRUE; + } + if (self::isTransactedStatus($pledgeStatusID)) { + return TRUE; + } + + return civicrm_api3('pledge_payment', 'getcount', array('pledge_id' => $pledgeID, 'status_id' => array('IN' => self::getTransactionalStatus()))); + } + + /** + * Does this pledge / pledge payment status mean that a financial transaction has taken place? + * @param int $statusID pledge status id + * + * @return bool is it a transactional status? + */ + protected static function isTransactedStatus($statusID) { + if (!in_array($statusID, self::getNonTransactionalStatus())) { + return TRUE; + } + } + + /** + * Get array of non transactional statuses + * @return array non transactional status ids + */ + protected static function getNonTransactionalStatus() { + $paymentStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + return array_flip(array_intersect($paymentStatus, array('Overdue', 'Pending'))); + } + + /** + * Get array of non transactional statuses + * @return array non transactional status ids + */ + protected static function getTransactionalStatus() { + $paymentStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + return array_diff(array_flip($paymentStatus), self::getNonTransactionalStatus()); + } } diff --git a/CRM/Pledge/Form/Pledge.php b/CRM/Pledge/Form/Pledge.php index 97ba708dbf..a847c6556c 100644 --- a/CRM/Pledge/Form/Pledge.php +++ b/CRM/Pledge/Form/Pledge.php @@ -115,7 +115,6 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { //build custom data CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'Pledge', $this->_id); - $this->_values = array(); // current pledge id if ($this->_id) { @@ -126,36 +125,7 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { $params = array('id' => $this->_id); CRM_Pledge_BAO_Pledge::getValues($params, $this->_values); - $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); - - //check for pending pledge. - if (CRM_Utils_Array::value('status_id', $this->_values) == - array_search('Pending', $paymentStatusTypes) - ) { - $this->_isPending = TRUE; - } - elseif (CRM_Utils_Array::value('status_id', $this->_values) == - array_search('Overdue', $paymentStatusTypes) - ) { - - $allPledgePayments = array(); - CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', - 'pledge_id', - $this->_id, - $allPledgePayments, - array('status_id') - ); - - foreach ($allPledgePayments as $key => $value) { - $allStatus[$value['id']] = $paymentStatusTypes[$value['status_id']]; - } - - if (count(array_count_values($allStatus)) <= 2) { - if (CRM_Utils_Array::value('Pending', array_count_values($allStatus))) { - $this->_isPending = TRUE; - } - } - } + $this->_isPending = (CRM_Pledge_BAO_Pledge::pledgeHasFinancialTransactions($this->_id, CRM_Utils_Array::value('status_id', $this->_values))) ? FALSE : T; } //get the pledge frequency units. @@ -164,6 +134,7 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); } + /** * This function sets the default values for the form. * the default values are retrieved from the database -- 2.25.1