CRM-16259 - Addtional fixes from Eileen to deal with failure of new unit test.
authorDave Greenberg <dave@civicrm.org>
Fri, 25 Dec 2015 00:44:23 +0000 (16:44 -0800)
committereileenmcnaughton <eileen@fuzion.co.nz>
Wed, 6 Jan 2016 07:52:46 +0000 (07:52 +0000)
----------------------------------------
* CRM-16259: Create Payment API
  https://issues.civicrm.org/jira/browse/CRM-16259

CRM/Contribute/BAO/Contribution.php
CRM/Core/BAO/FinancialTrxn.php
CRM/Core/OptionGroup.php
api/v3/Contribution.php
tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php

index a784ba2c2c800fb7854334c192d05846a42ea68b..1fa916f38dfbf5725a7f0f63fa5ee3676ee1abe5 100644 (file)
@@ -141,6 +141,11 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
         }
       }
     }
+    else {
+      // Since the fee amount is expecting this (later on) ensure it is always set.
+      // It would only not be set for an update where it is unchanged.
+      $params['contribution_status_id'] = civicrm_api3('Contribution', 'getvalue', array('id' => $params['id'], 'return' => 'contribution_status_id'));
+    }
 
     //set defaults in create mode
     if (!$contributionID) {
index d4645599177026cf3d6be7ace7094df5621560f8..04de7293433eb0be882e1ace14285eaf29e37521 100644 (file)
@@ -421,7 +421,7 @@ WHERE ceft.entity_id = %1";
     $params['trxnParams']['to_financial_account_id'] = $financialAccount;
     $params['trxnParams']['total_amount'] = $amount;
     $params['trxnParams']['fee_amount'] = $params['trxnParams']['net_amount'] = 0;
-    $params['trxnParams']['status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
+    $params['trxnParams']['status_id'] = $params['contribution_status_id'];
     $params['trxnParams']['contribution_id'] = $contributionId;
     $trxn = self::create($params['trxnParams']);
     if (empty($params['entity_id'])) {
@@ -526,15 +526,17 @@ WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFina
    * @return array
    */
   public static function getTotalPayments($contributionId) {
-    $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
-    $sql = "SELECT SUM(ft.total_amount) FROM civicrm_financial_trxn ft 
-      INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') 
+    $statusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
+
+    $sql = "SELECT SUM(ft.total_amount) FROM civicrm_financial_trxn ft
+      INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution')
       WHERE eft.entity_id = %1 AND ft.is_payment = 1 AND ft.status_id = %2";
 
     $params = array(
       1 => array($contributionId, 'Integer'),
       2 => array($statusId, 'Integer'),
     );
+
     return CRM_Core_DAO::singleValueQuery($sql, $params);
   }
 
index dcddadd583006af50de93af4ac9b4ae264592e8e..55efe9cfd292efc628c8855605cc7181c3826d5a 100644 (file)
@@ -363,6 +363,10 @@ WHERE  v.option_group_id = g.id
   }
 
   /**
+   * @deprecated
+   *
+   * This function is not cached.
+   *
    * @param string $groupName
    * @param $label
    * @param string $labelField
index 1c7f9d4d3598d42a8f24e559f7f29e03ff55e954..1ac600a5b69b60fc800cac97bb44ed2e4d5fd59f 100644 (file)
@@ -84,6 +84,7 @@ function civicrm_api3_contribution_create(&$params) {
   _civicrm_api3_contribution_create_legacy_support_45($params);
 
   // Make sure tax calculation is handled via api.
+  // @todo this belongs in the BAO NOT the api.
   $params = CRM_Contribute_BAO_Contribution::checkTaxAmount($params);
 
   return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Contribution');
index 85e7d914be213d8fb8a1199b1e9689594bc27eb8..b36858bba8cbf34e932f260c24b0c3cf70dd9f16 100644 (file)
@@ -67,12 +67,10 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
   }
 
   /**
-   * Create() method (create and update modes).
+   * Test getTotalPayments function.
    */
-  public function testIsPaymentFlagForPending() {
-    require_once 'CiviTest/Contact.php';
-    $contactId = Contact::createIndividual();
-    $ids = array('contribution' => NULL);
+  public function testGetTotalPayments() {
+    $contactId = $this->individualCreate();
 
     $params = array(
       'contact_id' => $contactId,
@@ -93,27 +91,24 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
       'thankyou_date' => '20080522',
     );
 
-    $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
+    $contribution = CRM_Contribute_BAO_Contribution::create($params);
 
-    $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
-    $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id  creation.');
+    $this->assertEquals($params['trxn_id'], $contribution->trxn_id);
+    $this->assertEquals($contactId, $contribution->contact_id);
 
     $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution->id);
     $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
     //update contribution amount
-    $ids = array('contribution' => $contribution->id);
+    $params['id'] = $contribution->id;
     $params['contribution_status_id'] = 1;
 
-    $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
+    $contribution = CRM_Contribute_BAO_Contribution::create($params);
 
-    $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
-    $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id, 'Check for status updation.');
+    $this->assertEquals($params['trxn_id'], $contribution->trxn_id);
+    $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id);
 
     $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution->id);
     $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
-    //Delete Contribution
-    $this->contributionDelete($contribution->id);
-    Contact::delete($contactId);
   }
 
 }