CRM-16259 Add recordPartialPayment function and test.
authorDave Greenberg <dave@civicrm.org>
Tue, 22 Dec 2015 18:12:51 +0000 (10:12 -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/Core/BAO/FinancialTrxn.php
tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php

index 21eeaea4e01b0a1802b1b577364154f2f20b9d15..d4645599177026cf3d6be7ace7094df5621560f8 100644 (file)
@@ -520,4 +520,22 @@ WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFina
     return $value;
   }
 
+  /**
+   * @param int $contributionId
+   *
+   * @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') 
+      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 c93236bd0db00c104ccea46141153fc12c201b37..85e7d914be213d8fb8a1199b1e9689594bc27eb8 100644 (file)
@@ -66,4 +66,54 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
     $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
   }
 
+  /**
+   * Create() method (create and update modes).
+   */
+  public function testIsPaymentFlagForPending() {
+    require_once 'CiviTest/Contact.php';
+    $contactId = Contact::createIndividual();
+    $ids = array('contribution' => NULL);
+
+    $params = array(
+      'contact_id' => $contactId,
+      'currency' => 'USD',
+      'financial_type_id' => 1,
+      'contribution_status_id' => 2,
+      'payment_instrument_id' => 1,
+      'source' => 'STUDENT',
+      'is_pay_later' => 1,
+      'receive_date' => '20080522000000',
+      'receipt_date' => '20080522000000',
+      'non_deductible_amount' => 0.00,
+      'total_amount' => 200.00,
+      'fee_amount' => 5,
+      'net_amount' => 195,
+      'trxn_id' => '22ereerwwe4444yy',
+      'invoice_id' => '86ed39e9e9yy6ef6541621ce0eafe7eb81',
+      'thankyou_date' => '20080522',
+    );
+
+    $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
+
+    $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
+    $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id  creation.');
+
+    $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution->id);
+    $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
+    //update contribution amount
+    $ids = array('contribution' => $contribution->id);
+    $params['contribution_status_id'] = 1;
+
+    $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
+
+    $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.');
+
+    $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);
+  }
+
 }