--CRM-16015, Added form rule to restrict financial type change for having different...
authorPradeep Nayak <pradpnayak@gmail.com>
Sat, 14 Mar 2015 20:50:58 +0000 (02:20 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Thu, 19 Mar 2015 09:40:20 +0000 (15:10 +0530)
CRM/Contribute/BAO/Contribution.php
CRM/Contribute/Form/Contribution.php
api/v3/Contribution.php

index 76accef4136f25088da787ae89edc6c19076738e..5d80490419a6ad15037a568bbc22b65df6eb4c5b 100644 (file)
@@ -3675,4 +3675,35 @@ WHERE con.id = {$contributionId}
     return $params;
   }
 
+  /**
+   * Check financial type validation on update of a contribution.
+   *
+   * @param Integer $financialTypeId
+   *   Value of latest Financial Type.
+   *
+   * @param Integer
+   *   Contribution Id.
+   *
+   * @param array $errors
+   *   List of errors.
+   *
+   * @return bool
+   */
+  public static function checkFinancialTypeChange($financialTypeId, $contributionId, &$errors) {
+    if (!empty($financialTypeId)) {
+      $oldFinancialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id');
+      if ($oldFinancialTypeId == $financialTypeId) {
+        return FALSE;
+      }
+    }
+    $sql = 'SELECT financial_type_id FROM civicrm_line_item WHERE contribution_id = %1 GROUP BY financial_type_id;';
+    $params = array(
+      '1' => array($contributionId, 'Integer')
+    );
+    $result = CRM_Core_DAO::executeQuery($sql, $params);
+    if ($result->N > 1) {
+      $errors['financial_type_id'] = ts('One or more line items have a different financial type than the contribution. Editing the financial type is not yet supported in this situation.');
+    }
+  }
+
 }
index 1abeefd2f97c389967263f6470b303ddc62482e6..00de7c927a86cf9a3885509dc98793d7b7c0a6ab 100644 (file)
@@ -948,7 +948,10 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
     if ($self->_id && $self->_values['contribution_status_id'] != $fields['contribution_status_id']) {
       CRM_Contribute_BAO_Contribution::checkStatusValidation($self->_values, $fields, $errors);
     }
-
+    // CRM-16015, add form-rule to restrict change of financial type if using price field of different financial type
+    if ($self->_id && $self->_values['financial_type_id'] != $fields['financial_type_id']) {
+      CRM_Contribute_BAO_Contribution::checkFinancialTypeChange(NULL, $self->_id, $errors);
+    }
     //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
     if (!empty($fields['fee_amount']) && !empty($fields['financial_type_id']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($fields['financial_type_id'])) {
       $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
index 058f5bcc835c766d9101b61bd4b1fe389ca673e6..aa385a0fdaa430ce5cf2243138d70bb769e45675 100644 (file)
@@ -56,7 +56,13 @@ function civicrm_api3_contribution_create(&$params) {
       throw new API_Exception($error['contribution_status_id']);
     }
   }
-
+  if (!empty($params['id']) && !empty($params['financial_type_id'])) {
+    $error = array();
+    CRM_Contribute_BAO_Contribution::checkFinancialTypeChange($params['financial_type_id'], $params['id'], $error);
+    if (array_key_exists('financial_type_id', $error)) {
+      throw new API_Exception($error['financial_type_id']);
+    }
+  }
   _civicrm_api3_contribution_create_legacy_support_45($params);
 
   // Make sure tax calculation is handled via api.