From ab6ba136d0b1dde086dec756cc5d1297f3cfbb02 Mon Sep 17 00:00:00 2001 From: eileenmcnaugton Date: Mon, 1 Aug 2016 12:56:44 +1200 Subject: [PATCH] CRM-18259 call pledge & pledgePayment specific functions for geting status options. The current config is to have a long list of contributions statuses which are shared as an option group with pledge & pledge_payment. The irrelevant ones are then hacked out. I believe that the correct way to do this is to split out the option groups into 3 separate ones. However, there is quite a lot of work in re-pointing the various places that get the statuses from contributions. So, I decided to limit this step to fixing the code to call the correct buildOptions function to get the list & moving the hacks into there. This does fix the original issue as the status was being hacked out for pledge payment when it should have been kept for pledge --- CRM/Pledge/BAO/Pledge.php | 43 +++++++++++++++++--------------- CRM/Pledge/BAO/PledgePayment.php | 19 ++++++++++++++ CRM/Pledge/BAO/Query.php | 9 ++----- CRM/Pledge/Form/Payment.php | 7 +++--- CRM/Pledge/Form/Pledge.php | 9 +++---- CRM/Pledge/Form/PledgeView.php | 2 +- CRM/Pledge/Form/Search.php | 6 ++--- CRM/Pledge/Selector/Search.php | 4 +-- 8 files changed, 54 insertions(+), 45 deletions(-) diff --git a/CRM/Pledge/BAO/Pledge.php b/CRM/Pledge/BAO/Pledge.php index 7ca42de78e..ecc923aa92 100644 --- a/CRM/Pledge/BAO/Pledge.php +++ b/CRM/Pledge/BAO/Pledge.php @@ -159,14 +159,11 @@ class CRM_Pledge_BAO_Pledge extends CRM_Pledge_DAO_Pledge { $params['amount'] = $params['installment_amount'] * $params['installments']; } - // get All Payments status types. - $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); - // update the pledge status only if it does NOT come from form if (!isset($params['pledge_status_id'])) { if (isset($params['contribution_id'])) { if ($params['installments'] > 1) { - $params['status_id'] = array_search('In Progress', $paymentStatusTypes); + $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'In Progress'); } } else { @@ -174,7 +171,7 @@ class CRM_Pledge_BAO_Pledge extends CRM_Pledge_DAO_Pledge { $params['status_id'] = CRM_Pledge_BAO_PledgePayment::calculatePledgeStatus($params['id']); } else { - $params['status_id'] = array_search('Pending', $paymentStatusTypes); + $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending'); } } } @@ -352,28 +349,16 @@ class CRM_Pledge_BAO_Pledge extends CRM_Pledge_DAO_Pledge { public static function getTotalAmountAndCount($status = NULL, $startDate = NULL, $endDate = NULL) { $where = array(); $select = $from = $queryDate = NULL; - // get all status - $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); - $statusId = array_search($status, $allStatus); + $statusId = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', $status); switch ($status) { case 'Completed': - $statusId = array_search('Cancelled', $allStatus); - $where[] = 'status_id != ' . $statusId; + $where[] = 'status_id != ' . CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Cancelled'); break; case 'Cancelled': - $where[] = 'status_id = ' . $statusId; - break; - case 'In Progress': - $where[] = 'status_id = ' . $statusId; - break; - case 'Pending': - $where[] = 'status_id = ' . $statusId; - break; - case 'Overdue': $where[] = 'status_id = ' . $statusId; break; @@ -413,7 +398,6 @@ GROUP BY currency ); $where = array(); - $statusId = array_search($status, $allStatus); switch ($status) { case 'Completed': $select = 'sum( total_amount ) as received_pledge , count( cd.id ) as received_count'; @@ -1290,4 +1274,23 @@ SELECT pledge.contact_id as contact_id, return $date; } + /** + * Override buildOptions to hack out some statuses. + * + * @todo instead of using & hacking the shared optionGroup contribution_status use a separate one. + * + * @param string $fieldName + * @param string $context + * @param array $props + * + * @return array|bool + */ + public static function buildOptions($fieldName, $context = NULL, $props = array()) { + $result = parent::buildOptions($fieldName, $context, $props); + if ($fieldName == 'status_id') { + $result = array_diff($result, array('Failed')); + } + return $result; + } + } diff --git a/CRM/Pledge/BAO/PledgePayment.php b/CRM/Pledge/BAO/PledgePayment.php index 08752df696..6c9e037f17 100644 --- a/CRM/Pledge/BAO/PledgePayment.php +++ b/CRM/Pledge/BAO/PledgePayment.php @@ -870,4 +870,23 @@ WHERE civicrm_pledge_payment.contribution_id = {$paymentContributionId} } } + /** + * Override buildOptions to hack out some statuses. + * + * @todo instead of using & hacking the shared optionGroup contribution_status use a separate one. + * + * @param string $fieldName + * @param string $context + * @param array $props + * + * @return array|bool + */ + public static function buildOptions($fieldName, $context = NULL, $props = array()) { + $result = parent::buildOptions($fieldName, $context, $props); + if ($fieldName == 'status_id') { + $result = array_diff($result, array('Failed', 'In Progress')); + } + return $result; + } + } diff --git a/CRM/Pledge/BAO/Query.php b/CRM/Pledge/BAO/Query.php index cd04c49b65..f2aec40dfd 100644 --- a/CRM/Pledge/BAO/Query.php +++ b/CRM/Pledge/BAO/Query.php @@ -534,13 +534,8 @@ class CRM_Pledge_BAO_Query { $form->add('text', 'pledge_amount_high', ts('To'), array('size' => 8, 'maxlength' => 8)); $form->addRule('pledge_amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money'); - $statusValues = CRM_Contribute_PseudoConstant::contributionStatus(); - // Remove status values that are only used for recurring contributions for now (Failed and In Progress). - unset($statusValues['4']); - // unset in progress for payment - unset($statusValues['5']); $form->add('select', 'pledge_status_id', - ts('Pledge Status'), $statusValues, + ts('Pledge Status'), CRM_Pledge_BAO_Pledge::buildOptions('status_id'), FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple') ); @@ -553,7 +548,7 @@ class CRM_Pledge_BAO_Query { $form->addRule('pledge_installments_high', ts('Please enter number.'), 'integer'); $form->add('select', 'pledge_payment_status_id', - ts('Pledge Payment Status'), $statusValues, + ts('Pledge Payment Status'), CRM_Pledge_BAO_PledgePayment::buildOptions('status_id'), FALSE, array('class' => 'crm-select2', 'multiple' => 'multiple') ); diff --git a/CRM/Pledge/Form/Payment.php b/CRM/Pledge/Form/Payment.php index 2d774ee5c4..72bb5cab44 100644 --- a/CRM/Pledge/Form/Payment.php +++ b/CRM/Pledge/Form/Payment.php @@ -70,7 +70,7 @@ class CRM_Pledge_Form_Payment extends CRM_Core_Form { if (isset($defaults['contribution_id'])) { $this->assign('pledgePayment', TRUE); } - $status = CRM_Contribute_PseudoConstant::contributionStatus($defaults['status_id']); + $status = CRM_Core_PseudoConstant::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']); $this->assign('status', $status); } $defaults['option_type'] = 1; @@ -129,13 +129,12 @@ class CRM_Pledge_Form_Payment extends CRM_Core_Form { $params['scheduled_date'] = CRM_Utils_Date::format($formValues['scheduled_date']); $params['currency'] = CRM_Utils_Array::value('currency', $formValues); $now = date('Ymd'); - $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($params['scheduled_date'], '%Y%m%d'), $now)) { - $params['status_id'] = array_search('Overdue', $contributionStatus); + $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue'); } else { - $params['status_id'] = array_search('Pending', $contributionStatus); + $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending'); } $params['id'] = $this->_id; diff --git a/CRM/Pledge/Form/Pledge.php b/CRM/Pledge/Form/Pledge.php index 764f98d2c7..b8ffbdab41 100644 --- a/CRM/Pledge/Form/Pledge.php +++ b/CRM/Pledge/Form/Pledge.php @@ -178,7 +178,7 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { $defaults['financial_type_id'] = array_search('Donation', CRM_Contribute_PseudoConstant::financialType()); } - $pledgeStatus = CRM_Contribute_PseudoConstant::contributionStatus(); + $pledgeStatus = CRM_Pledge_BAO_Pledge::buildOptions('status_id'); $pledgeStatusNames = CRM_Core_OptionGroup::values('contribution_status', FALSE, FALSE, FALSE, NULL, 'name', TRUE ); @@ -362,7 +362,7 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { } if (CRM_Utils_Array::value('status_id', $this->_values) != - array_search('Cancelled', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')) + CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Cancelled') ) { $this->addElement('checkbox', 'is_acknowledge', ts('Send Acknowledgment?'), NULL, @@ -486,9 +486,6 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { $session = CRM_Core_Session::singleton(); - // get All Payments status types. - $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); - $fields = array( 'frequency_unit', 'frequency_interval', @@ -511,7 +508,7 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form { $params['pledge_status_id'] = $params['status_id'] = $this->_values['status_id']; } else { - $params['pledge_status_id'] = $params['status_id'] = array_search('Pending', $paymentStatusTypes); + $params['pledge_status_id'] = $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending'); } // format amount $params['amount'] = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('amount', $formValues)); diff --git a/CRM/Pledge/Form/PledgeView.php b/CRM/Pledge/Form/PledgeView.php index 85d2df588e..fd1f15e923 100644 --- a/CRM/Pledge/Form/PledgeView.php +++ b/CRM/Pledge/Form/PledgeView.php @@ -73,7 +73,7 @@ class CRM_Pledge_Form_PledgeView extends CRM_Core_Form { $values['financial_type'] = CRM_Utils_Array::value($values['financial_type_id'], CRM_Contribute_PseudoConstant::financialType()); if ($values['status_id']) { - $values['pledge_status'] = CRM_Utils_Array::value($values['status_id'], CRM_Contribute_PseudoConstant::contributionStatus()); + $values['pledge_status'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', $values['status_id']); } $url = CRM_Utils_System::url('civicrm/contact/view/pledge', diff --git a/CRM/Pledge/Form/Search.php b/CRM/Pledge/Form/Search.php index be47cfdc08..8f48b5901e 100644 --- a/CRM/Pledge/Form/Search.php +++ b/CRM/Pledge/Form/Search.php @@ -353,11 +353,9 @@ class CRM_Pledge_Form_Search extends CRM_Core_Form_Search { // set pledge related fields $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String'); - if ($pledgeStatus) { - $statusValues = CRM_Contribute_PseudoConstant::contributionStatus(); - // Remove status values that are only used for recurring contributions for now (Failed). - unset($statusValues['4']); + if ($pledgeStatus) { + $statusValues = CRM_Pledge_BAO_Pledge::buildOptions('status_id'); // we need set all statuses except Cancelled unset($statusValues[$pledgeStatus]); diff --git a/CRM/Pledge/Selector/Search.php b/CRM/Pledge/Selector/Search.php index 5882970c04..5588fb19f5 100644 --- a/CRM/Pledge/Selector/Search.php +++ b/CRM/Pledge/Selector/Search.php @@ -290,9 +290,7 @@ class CRM_Pledge_Selector_Search extends CRM_Core_Selector_Base { $rows = array(); // get all pledge status - $pledgeStatuses = CRM_Core_OptionGroup::values('contribution_status', - FALSE, FALSE, FALSE, NULL, 'name', FALSE - ); + $pledgeStatuses = CRM_Pledge_BAO_Pledge::buildOptions('status_id'); // get all campaigns. $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); -- 2.25.1