--fixed for CRM-12498, added condition to check valid contribution status change...
authorPradeep Nayak <pradeep@pradeep.(none)>
Mon, 20 May 2013 10:59:34 +0000 (16:29 +0530)
committerPradeep Nayak <pradeep@pradeep.(none)>
Mon, 20 May 2013 10:59:34 +0000 (16:29 +0530)
----------------------------------------
* CRM-12498: API contribution.create/update DB Error: constraint violation
  http://issues.civicrm.org/jira/browse/CRM-12498

CRM/Contribute/BAO/Contribution.php
api/v3/Contribution.php
tests/phpunit/api/v3/ContributionTest.php

index 3a143bf5b815479f788f70391efa872263218daf..7dbc1e89279e313d4c85085cfe94e767096d52c4 100644 (file)
@@ -2779,11 +2779,11 @@ WHERE  contribution_id = %1 ";
         $financialTxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams);
         $params['entity_id'] = $financialTxn->id;
       }
-    }
-    // record line items and finacial items
+      // record line items and finacial items
 
-    if (!CRM_Utils_Array::value('skipLineItem', $params)) {
-      CRM_Price_BAO_LineItem::processPriceSet($entityId, CRM_Utils_Array::value('line_item', $params), $params['contribution'], $entityTable, $update);
+      if (!CRM_Utils_Array::value('skipLineItem', $params)) {
+        CRM_Price_BAO_LineItem::processPriceSet($entityId, CRM_Utils_Array::value('line_item', $params), $params['contribution'], $entityTable, $update);
+      }
     }
 
     // create batch entry if batch_id is passed
@@ -2974,6 +2974,12 @@ WHERE  contribution_id = %1 ";
    * @static
    */
   static function checkStatusValidation($values, &$fields, &$errors) {
+    if (CRM_Utils_System::isNull($values) && CRM_Utils_Array::value('id', $fields)) {
+      $values['contribution_status_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $fields['id'], 'contribution_status_id');
+      if ($values['contribution_status_id'] == $fields['contribution_status_id']) {
+        return FALSE;
+      }
+    }
     $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     $checkStatus = array(
       'Cancelled' => array('Completed', 'Refunded'),
index 71b42849edf954d882f6dd75471fd9ce4ced99c5..0d774014158b61f893a2720119d306173d4c4b94 100644 (file)
@@ -61,6 +61,15 @@ function civicrm_api3_contribution_create(&$params) {
   $ids = array();
   if (CRM_Utils_Array::value('id', $params)) {
     $ids['contribution'] = $params['id'];
+    // CRM-12498
+    if (CRM_Utils_Array::value('contribution_status_id', $params)) {
+      $error = array(); 
+      //throw error for invalid status change
+      CRM_Contribute_BAO_Contribution::checkStatusValidation(NULL, $params, $error);
+      if (array_key_exists('contribution_status_id', $error)) {
+        return civicrm_api3_create_error($error['contribution_status_id']);
+      }
+    }
   }
 
   $contribution = CRM_Contribute_BAO_Contribution::create($values, $ids);
index e0cb803dc4e24035c097a8305bcb3dafb6a7ee06..18893ba05015af72364c2063caf77110dc95fda0 100644 (file)
@@ -946,15 +946,16 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       'total_amount' => 100.00,
       'financial_type_id' => $this->_contributionTypeId,
       'payment_instrument_id' => 1,
-      'contribution_status_id' => 2,
+      'contribution_status_id' => 2,  
+      'is_pay_later' => 1,                                           
       'version' => $this->_apiversion,
     );
     $contribution = civicrm_api('contribution', 'create', $contribParams);
 
     $newParams = array_merge($contribParams, array(
-                                                   'id' => $contribution['id'],
-                                                   'contribution_status_id' => 1,)
-                             );
+      'id' => $contribution['id'],
+      'contribution_status_id' => 1,)
+    );
     $contribution = civicrm_api('contribution', 'update', $newParams);
     $contribution = $contribution['values'][$contribution['id']];
     $this->assertEquals($contribution['contribution_status_id'],'1');
@@ -1009,6 +1010,31 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $contribution = civicrm_api('contribution', 'update', $newParams);
     $this->_checkFinancialTrxn($contribution, 'refund');
     $this->_checkFinancialItem($contribution['id'], 'refund');
+  } 
+
+  /*
+   * Function tests invalid contribution status change 
+   */
+  function testCreateUpdateContributionInValidStatusChange() {
+    $contribParams = array(
+      'contact_id' => 1,
+      'receive_date' => '2012-01-01',
+      'total_amount' => 100.00,
+      'financial_type_id' => 1,
+      'payment_instrument_id' => 1,
+      'contribution_status_id' => 1,
+      'version' => 3,
+    );
+    $contribution = civicrm_api('contribution', 'create', $contribParams);
+    $newParams = array_merge($contribParams, array(
+     'id' => $contribution['id'],
+     'contribution_status_id' => 2,
+      )
+    );
+    $contribution = civicrm_api('contribution', 'update', $newParams);
+    $this->assertTrue(!empty($contribution['is_error']), 'In line ' . __LINE__);
+    $this->assertEquals($contribution['error_message'], ts('Cannot change contribution status from Completed to Pending.'), 'In line ' . __LINE__);
+
   }
 
   /*
@@ -1022,6 +1048,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       'financial_type_id' => $this->_contributionTypeId,
       'payment_instrument_id' => 1,
       'contribution_status_id' => 2,
+      'is_pay_later' => 1,
       'version' => $this->_apiversion,
     );
     $contribution = civicrm_api('contribution', 'create', $contribParams);