From 1019b2fe8e5e506f34cf338d6e4e77f601b408c4 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 18 Oct 2016 07:37:02 +1100 Subject: [PATCH] CRM-19517 allow disabled financial types to show --- .../Form/Checkout/ParticipantsAndPrices.php | 6 ++- CRM/Event/Form/Registration/Register.php | 27 ++++++------ CRM/Price/BAO/PriceField.php | 44 ++++++++++++------- CRM/Price/BAO/PriceFieldValue.php | 25 ++++++++--- CRM/Price/BAO/PriceSet.php | 18 ++++---- CRM/Price/Form/Preview.php | 4 +- CRM/Price/Page/Field.php | 6 +-- CRM/Price/Page/Option.php | 4 +- CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl | 35 +++++++++++++++ tests/phpunit/api/v3/PriceFieldValueTest.php | 17 +++++++ 10 files changed, 132 insertions(+), 54 deletions(-) diff --git a/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php b/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php index ffe3b7f563..c47306a840 100644 --- a/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php +++ b/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php @@ -98,8 +98,10 @@ class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_ if (CRM_Utils_Array::value('visibility', $field) == 'public' || (CRM_Utils_Array::value('visibility', $field) == 'admin' && $adminFieldVisible == TRUE)) { $field_name = "event_{$event->id}_price_{$field['id']}"; - CRM_Price_BAO_PriceField::addQuickFormElement($this, $field_name, $field['id'], FALSE); - $price_fields_for_event[] = $field_name; + if (!empty($field['options'])) { + CRM_Price_BAO_PriceField::addQuickFormElement($this, $field_name, $field['id'], FALSE); + $price_fields_for_event[] = $field_name; + } } } } diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index 798c3edc0e..82a726eb31 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -593,17 +593,18 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { if (!empty($optionFullIds) && (count($options) == count($optionFullIds))) { $isRequire = FALSE; } - - //build the element. - CRM_Price_BAO_PriceField::addQuickFormElement($form, - $elementName, - $fieldId, - FALSE, - $isRequire, - NULL, - $options, - $optionFullIds - ); + if (!empty($options)) { + //build the element. + CRM_Price_BAO_PriceField::addQuickFormElement($form, + $elementName, + $fieldId, + FALSE, + $isRequire, + NULL, + $options, + $optionFullIds + ); + } } } $form->assign('priceSet', $form->_priceSet); @@ -838,8 +839,8 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { // @todo - can we remove the 'is_monetary' concept? if ($self->_values['event']['is_monetary']) { - if (empty($self->_requireApproval) && !empty($fields['amount']) && $fields['amount'] > 0 && !isset - ($fields['payment_processor_id'])) { + if (empty($self->_requireApproval) && !empty($fields['amount']) && $fields['amount'] > 0 && + !isset($fields['payment_processor_id'])) { $errors['payment_processor_id'] = ts('Please select a Payment Method'); } diff --git a/CRM/Price/BAO/PriceField.php b/CRM/Price/BAO/PriceField.php index 969402bd73..8e1829be01 100644 --- a/CRM/Price/BAO/PriceField.php +++ b/CRM/Price/BAO/PriceField.php @@ -94,22 +94,15 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { } $optionsIds = array(); $maxIndex = CRM_Price_Form_Field::NUM_OPTION; - - if ($priceField->html_type == 'Text') { + $priceField2 = civicrm_api3('price_field', 'getsingle', array('id' => $priceField->id)); + if ($priceField2['html_type'] == 'Text') { $maxIndex = 1; - - $fieldValue = new CRM_Price_DAO_PriceFieldValue(); - $fieldValue->price_field_id = $priceField->id; - - // update previous field values( if any ) - if ($fieldValue->find(TRUE)) { - $optionsIds['id'] = $fieldValue->id; - - //Update price_field_value label when edited inline. - if (!empty($params['id']) && $priceField->label != $fieldValue->label) { - $fieldValue->label = $priceField->label; - $fieldValue->save(); - } + $fieldOptions = civicrm_api3('price_field_value', 'get', array( + 'price_field_id' => $priceField->id, + 'sequential' => 1, + )); + foreach ($fieldOptions['values'] as $option) { + $optionsIds['id'] = $option['id']; } } $defaultArray = array(); @@ -161,7 +154,6 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { elseif (!empty($params['financial_type_id'])) { $options['financial_type_id'] = $params['financial_type_id']; } - if ($opIds = CRM_Utils_Array::value('option_id', $params)) { if ($opId = CRM_Utils_Array::value($index, $opIds)) { $optionsIds['id'] = $opId; @@ -170,7 +162,25 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { $optionsIds['id'] = NULL; } } - CRM_Price_BAO_PriceFieldValue::create($options, $optionsIds); + try { + CRM_Price_BAO_PriceFieldValue::create($options, $optionsIds); + } + catch (Exception $e) { + $transaction->rollback(); + throw new CRM_Core_Exception($e->getMessage()); + } + } + elseif (!empty($optionIds)) { + $optionsLoad = civicrm_api3('price_field_value', 'get', array('id' => $optionIds['id'])); + $options = $optionsLoad['values'][$option['id']]; + $options['is_active'] = CRM_Utils_Array::value('is_active', $params, 1); + try { + CRM_Price_BAO_PriceFieldValue::create($options, $optionIds); + } + catch (Exception $e) { + $transaction->rollback(); + throw new CRM_Core_Exception($e->getMessage()); + } } } diff --git a/CRM/Price/BAO/PriceFieldValue.php b/CRM/Price/BAO/PriceFieldValue.php index 2f337386d5..f6b8b3687b 100644 --- a/CRM/Price/BAO/PriceFieldValue.php +++ b/CRM/Price/BAO/PriceFieldValue.php @@ -111,7 +111,6 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { if ($id) { $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'weight', 'id'); } - $fieldValues = array('price_field_id' => CRM_Utils_Array::value('price_field_id', $params, 0)); $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceFieldValue', $oldWeight, $params['weight'], $fieldValues); } @@ -123,6 +122,15 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { } } } + + $financialType = CRM_Utils_Array::value('financial_type_id', $params, NULL); + if (!$financialType && $id) { + $financialType = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $id, 'financial_type_id', 'id'); + } + CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); + if (!empty($financialType) && !array_key_exists($financialType, $financialTypes) && $params['is_active']) { + throw new CRM_Core_Exception("Financial Type for Price Field Option is either disabled or does not exist"); + } return self::add($params, $ids); } @@ -164,30 +172,35 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { * @param string $orderBy * For order by, default weight. * @param bool|int $isActive is_active, default false + * @param bool $admin is this loading it for use on an admin page. * * @return array * */ - public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE) { + public static function getValues($fieldId, &$values, $orderBy = 'weight', $isActive = FALSE, $admin = FALSE) { $sql = "SELECT cs.id FROM civicrm_price_set cs INNER JOIN civicrm_price_field cp ON cp.price_set_id = cs.id WHERE cs.name IN ('default_contribution_amount', 'default_membership_type_amount') AND cp.id = {$fieldId} "; $setId = CRM_Core_DAO::singleValueQuery($sql); $fieldValueDAO = new CRM_Price_DAO_PriceFieldValue(); $fieldValueDAO->price_field_id = $fieldId; + $addWhere = ''; if (!$setId) { CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); - $addWhere = "financial_type_id IN (0)"; - if (!empty($financialTypes)) { + if (!$admin) { + $addWhere = "financial_type_id IN (0)"; + } + if (!empty($financialTypes) && !$admin) { $addWhere = "financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ")"; } - $fieldValueDAO->whereAdd($addWhere); + if (!empty($addWhere)) { + $fieldValueDAO->whereAdd($addWhere); + } } $fieldValueDAO->orderBy($orderBy, 'label'); if ($isActive) { $fieldValueDAO->is_active = 1; } $fieldValueDAO->find(); - while ($fieldValueDAO->fetch()) { CRM_Core_DAO::storeValues($fieldValueDAO, $values[$fieldValueDAO->id]); } diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index af7518f3ec..531372bc6f 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -1054,14 +1054,16 @@ WHERE id = %1"; if (!is_array($options) || !in_array($id, $validPriceFieldIds)) { continue; } - CRM_Price_BAO_PriceField::addQuickFormElement($form, - 'price_' . $field['id'], - $field['id'], - FALSE, - CRM_Utils_Array::value('is_required', $field, FALSE), - NULL, - $options - ); + if (!empty($options)) { + CRM_Price_BAO_PriceField::addQuickFormElement($form, + 'price_' . $field['id'], + $field['id'], + FALSE, + CRM_Utils_Array::value('is_required', $field, FALSE), + NULL, + $options + ); + } } } } diff --git a/CRM/Price/Form/Preview.php b/CRM/Price/Form/Preview.php index 60ca0c9638..70d4b3c986 100644 --- a/CRM/Price/Form/Preview.php +++ b/CRM/Price/Form/Preview.php @@ -132,7 +132,9 @@ class CRM_Price_Form_Preview extends CRM_Core_Form { foreach ($group['fields'] as $field) { $fieldId = $field['id']; $elementName = 'price_' . $fieldId; - CRM_Price_BAO_PriceField::addQuickFormElement($this, $elementName, $fieldId, FALSE, $field['is_required']); + if (!empty($field['options'])) { + CRM_Price_BAO_PriceField::addQuickFormElement($this, $elementName, $fieldId, FALSE, $field['is_required']); + } } } } diff --git a/CRM/Price/Page/Field.php b/CRM/Price/Page/Field.php index c8b1de506a..536195af8f 100644 --- a/CRM/Price/Page/Field.php +++ b/CRM/Price/Page/Field.php @@ -142,12 +142,8 @@ class CRM_Price_Page_Field extends CRM_Core_Page { $params = array('price_field_id' => $priceFieldBAO->id); CRM_Price_BAO_PriceFieldValue::retrieve($params, $optionValues); - $financialTypeId = $optionValues['financial_type_id']; - if (!array_key_exists($financialTypeId, $financialTypes)) { - unset($priceField[$priceFieldBAO->id]); - continue; - } $priceField[$priceFieldBAO->id]['price'] = CRM_Utils_Array::value('amount', $optionValues); + $financialTypeId = $optionValues['financial_type_id']; if ($invoicing && isset($taxRate[$financialTypeId])) { $priceField[$priceFieldBAO->id]['tax_rate'] = $taxRate[$financialTypeId]; $getTaxDetails = TRUE; diff --git a/CRM/Price/Page/Option.php b/CRM/Price/Page/Option.php index 5e3daafb18..ba409c9a38 100644 --- a/CRM/Price/Page/Option.php +++ b/CRM/Price/Page/Option.php @@ -156,8 +156,8 @@ class CRM_Price_Page_Option extends CRM_Core_Page { $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($customOption[$id]['amount'], $customOption[$id]['tax_rate']); $customOption[$id]['tax_amount'] = $taxAmount['tax_amount']; } - if (!empty($values['financial_type_id'])) { - $customOption[$id]['financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($values['financial_type_id']); + if (!empty($values['financial_type_id']) && !empty($financialType[$values['financial_type_id']])) { + $customOption[$id]['financial_type_id'] = $financialType[$values['financial_type_id']]; } // update enable/disable links depending on price_field properties. if ($this->_isSetReserved) { diff --git a/CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl index 4052097c03..aa0c0e6f4c 100644 --- a/CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.7.15.mysql.tpl @@ -2,3 +2,38 @@ -- CRM-19685 (fix for inconsistencies) UPDATE civicrm_contact SET preferred_mail_format = 'Both' WHERE preferred_mail_format IS NULL; + +-- CRM-19723 add icons +SELECT @option_group_id_act := max(id) from civicrm_option_group where name = 'activity_type'; +UPDATE civicrm_option_value SET icon = 'fa-slideshare' WHERE option_group_id = @option_group_id_act AND name = 'Meeting'; +UPDATE civicrm_option_value SET icon = 'fa-phone' WHERE option_group_id = @option_group_id_act AND name = 'Phone Call'; +UPDATE civicrm_option_value SET icon = 'fa-envelope-o' WHERE option_group_id = @option_group_id_act AND name = 'Email'; +UPDATE civicrm_option_value SET icon = 'fa-mobile' WHERE option_group_id = @option_group_id_act AND name = 'SMS'; +UPDATE civicrm_option_value SET icon = 'fa-file-pdf-o' WHERE option_group_id = @option_group_id_act AND name = 'Print PDF Letter'; +UPDATE civicrm_option_value SET icon = 'fa-folder-open-o' WHERE option_group_id = @option_group_id_act AND name = 'Open Case'; +UPDATE civicrm_option_value SET icon = 'fa-share-square-o' WHERE option_group_id = @option_group_id_act AND name = 'Follow up'; +UPDATE civicrm_option_value SET icon = 'fa-random' WHERE option_group_id = @option_group_id_act AND name = 'Change Case Type'; +UPDATE civicrm_option_value SET icon = 'fa-pencil-square-o' WHERE option_group_id = @option_group_id_act AND name = 'Change Case Status'; +UPDATE civicrm_option_value SET icon = 'fa-calendar' WHERE option_group_id = @option_group_id_act AND name = 'Change Case Start Date'; +UPDATE civicrm_option_value SET icon = 'fa-user-plus' WHERE option_group_id = @option_group_id_act AND name = 'Assign Case Role'; +UPDATE civicrm_option_value SET icon = 'fa-user-times' WHERE option_group_id = @option_group_id_act AND name = 'Remove Case Role'; +UPDATE civicrm_option_value SET icon = 'fa-file-pdf-o' WHERE option_group_id = @option_group_id_act AND name = 'Print PDF Letter'; +UPDATE civicrm_option_value SET icon = 'fa-compress' WHERE option_group_id = @option_group_id_act AND name = 'Merge Case'; +UPDATE civicrm_option_value SET icon = 'fa-user-circle-o' WHERE option_group_id = @option_group_id_act AND name = 'Reassigned Case'; +UPDATE civicrm_option_value SET icon = 'fa-link' WHERE option_group_id = @option_group_id_act AND name = 'Link Cases'; +UPDATE civicrm_option_value SET icon = 'fa-tags' WHERE option_group_id = @option_group_id_act AND name = 'Change Case Tags'; +UPDATE civicrm_option_value SET icon = 'fa-users' WHERE option_group_id = @option_group_id_act AND name = 'Add Client To Case'; + +-- CRM-19517 Disable all price fields and price field options that use disabled fianancial types +UPDATE civicrm_price_field_value cpfv +INNER JOIN civicrm_financial_type cft ON cft.id = cpfv.financial_type_id +SET cpfv.is_active = 0 +WHERE cft.is_active = 0; + +UPDATE civicrm_price_field cpf +LEFT JOIN (SELECT DISTINCT price_field_id AS price_field_id + FROM civicrm_price_field_value + WHERE is_active = 1) AS price_field +ON price_field.price_field_id = cpf.id +SET cpf.is_active = 0 +WHERE price_field.price_field_id IS NULL; diff --git a/tests/phpunit/api/v3/PriceFieldValueTest.php b/tests/phpunit/api/v3/PriceFieldValueTest.php index 5aff59d644..2d581f7c4b 100644 --- a/tests/phpunit/api/v3/PriceFieldValueTest.php +++ b/tests/phpunit/api/v3/PriceFieldValueTest.php @@ -216,4 +216,21 @@ class api_v3_PriceFieldValueTest extends CiviUnitTestCase { $this->callAPISuccess($this->_entity, 'delete', array('id' => $result2['id'])); } + public function testCreatePriceFieldValueWithDisabledFinancialType() { + $financialTypeParams = array( + 'is_active' => 0, + 'name' => 'Disabled Donations', + ); + $financialType = $this->callAPISuccess('financial_type', 'create', $financialTypeParams); + $params = array( + 'price_field_id' => $this->priceFieldID, + 'name' => 'DonType1', + 'label' => 'DonType1', + 'amount' => 90, + 'is_active' => 1, + 'financial_type_id' => $financialType['id'], + ); + $this->callAPIFailure($this->_entity, 'create', $params); + } + } -- 2.25.1