From f182074ec6867516ab195217737f1822d532b206 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Thu, 4 Jul 2013 13:13:34 +0530 Subject: [PATCH] -- fixed for CRM-12929 ---------------------------------------- * CRM-12929: Do not allow Permanent Delete for contacts who are linked to live financial transactions http://issues.civicrm.org/jira/browse/CRM-12929 --- CRM/Contact/BAO/Contact.php | 7 ++++ CRM/Contact/Form/Task/Delete.php | 22 +++++++++++ CRM/Financial/BAO/FinancialItem.php | 43 +++++++++++++++++++++ CRM/Upgrade/Incremental/sql/4.3.5.mysql.tpl | 9 ++++- api/v3/Contact.php | 7 ++++ settings/Core.setting.php | 14 +++++++ 6 files changed, 101 insertions(+), 1 deletion(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 4742ec1cde..097499a1b1 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -723,6 +723,13 @@ WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); ) { return FALSE; } + + // CRM-12929 + // Restrict contact to be delete if contact has financial trxns + $error = NULL; + if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($id), $error)) { + return FALSE; + } // make sure this contact_id does not have any membership types $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', diff --git a/CRM/Contact/Form/Task/Delete.php b/CRM/Contact/Form/Task/Delete.php index 7e9451d7f3..d9aa493540 100644 --- a/CRM/Contact/Form/Task/Delete.php +++ b/CRM/Contact/Form/Task/Delete.php @@ -169,6 +169,28 @@ class CRM_Contact_Form_Task_Delete extends CRM_Contact_Form_Task { else { $this->addDefaultButtons($label, 'done'); } + + $this->addFormRule(array('CRM_Contact_Form_Task_Delete', 'formRule'), $this); + } + + /** + * global form rule + * + * @param array $fields the input form values + * @param array $files the uploaded files if any + * @param object $self form object + * + * @return true if no errors, else array of errors + * @access public + * @static + */ + static function formRule($fields, $files, $self) { + // CRM-12929 + $error = array(); + if ($self->_skipUndelete) { + CRM_Financial_BAO_FinancialItem::checkContactPresent($self->_contactIds, $error); + } + return $error; } /** diff --git a/CRM/Financial/BAO/FinancialItem.php b/CRM/Financial/BAO/FinancialItem.php index 79f7f61d03..5d184a4e51 100644 --- a/CRM/Financial/BAO/FinancialItem.php +++ b/CRM/Financial/BAO/FinancialItem.php @@ -204,4 +204,47 @@ class CRM_Financial_BAO_FinancialItem extends CRM_Financial_DAO_FinancialItem { return null; } } + + /** + * check if contact is present in financial_item table + * + * CRM-12929 + * + * @param array $contactIds an array contact id's + * + * @param array $error error to display + * + * @return array + * @access public + * @static + */ + static function checkContactPresent($contactIds, &$error) { + if (empty($contactIds)) { + return FALSE; + } + + $allowPermDelete = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'allowPermDeleteFinancial'); + + if (!$allowPermDelete) { + $sql = 'SELECT DISTINCT(cc.id), cc.display_name FROM civicrm_contact cc +INNER JOIN civicrm_contribution con ON con.contact_id = cc.id +WHERE cc.id IN (' . implode (',', $contactIds) . ') AND con.is_test = 0'; + $dao = CRM_Core_DAO::executeQuery($sql); + if ($dao->N) { + while ($dao->fetch()) { + $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$dao->id"); + $not_deleted[$dao->id] = "$dao->display_name"; + } + + $errorStatus = ''; + if (is_array($error)) { + $errorStatus = ''; + } + + $error['_qf_default'] = $errorStatus . ts('This contact(s) can not be permanently deleted because the contact record is linked to one or more live financial transactions. Deleting this contact would result in the loss of financial data.'); + return $error; + } + } + return FALSE; + } } diff --git a/CRM/Upgrade/Incremental/sql/4.3.5.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.3.5.mysql.tpl index 6c9db72982..bac8a19058 100644 --- a/CRM/Upgrade/Incremental/sql/4.3.5.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.3.5.mysql.tpl @@ -1,4 +1,11 @@ {* file to handle db changes in 4.3.5 during upgrade*} {include file='../CRM/Upgrade/4.3.5.msg_template/civicrm_msg_template.tpl'} -- CRM-12799 -DROP TABLE IF EXISTS civicrm_payment; \ No newline at end of file +DROP TABLE IF EXISTS civicrm_payment; + +-- CRM-12929 + +INSERT INTO civicrm_setting +(domain_id, contact_id, is_domain, group_name, name, value) +VALUES +({$domainID}, NULL, 1, 'CiviCRM Preferences', 'allowPermDeleteFinancial', '{serialize}0{/serialize}'); diff --git a/api/v3/Contact.php b/api/v3/Contact.php index 0a51db9865..957e2bbf5e 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -295,6 +295,13 @@ function civicrm_api3_contact_delete($params) { } $restore = CRM_Utils_Array::value('restore', $params) ? $params['restore'] : FALSE; $skipUndelete = CRM_Utils_Array::value('skip_undelete', $params) ? $params['skip_undelete'] : FALSE; + + // CRM-12929 + // restrict permanent delete if a contact has financial trxn associated with it + $error = NULL; + if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($contactID), $error)) { + return civicrm_api3_create_error($error['_qf_default']); + } if (CRM_Contact_BAO_Contact::deleteContact($contactID, $restore, $skipUndelete)) { return civicrm_api3_create_success(); } diff --git a/settings/Core.setting.php b/settings/Core.setting.php index e6aa893444..4c8067f22d 100644 --- a/settings/Core.setting.php +++ b/settings/Core.setting.php @@ -323,6 +323,20 @@ return array ( 'description' => 'If enabled, deleted contacts will be moved to trash (instead of being destroyed). Users with the proper permission are able to search for the deleted contacts and restore them (or delete permanently).', 'help_text' => null, ), + 'allowPermDeleteFinancial' => array( + 'group_name' => 'CiviCRM Preferences', + 'group' => 'core', + 'name' => 'allowPermDeleteFinancial', + 'type' => 'Boolean', + 'quick_form_type' => 'YesNo', + 'default' => FALSE, + 'add' => '4.3', + 'title' => 'Contact Permanent Delete', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => 'Allow Permanent Delete for contacts who are linked to live financial transactions', + 'help_text' => null, + ), 'versionAlert' => array( 'group_name' => 'CiviCRM Preferences', 'group' => 'core', -- 2.25.1