avoid E_notice / refactor
authordemeritcowboy <demeritcowboy@hotmail.com>
Tue, 6 Apr 2021 17:26:17 +0000 (13:26 -0400)
committerdemeritcowboy <demeritcowboy@hotmail.com>
Tue, 6 Apr 2021 20:38:23 +0000 (16:38 -0400)
CRM/Contribute/Form/Contribution.php
CRM/Financial/BAO/FinancialAccount.php
tests/phpunit/CRM/Contribute/Form/ContributionTest.php

index 0f804d6c8540bc2008761bd9b1f925cf13af3be3..5796571d343a4556e052602160e1b5ce282c8cf0 100644 (file)
@@ -911,8 +911,14 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP
       }
     }
     // CRM-16189
+    $order = new CRM_Financial_BAO_Order();
+    $order->setPriceSelectionFromUnfilteredInput($fields);
+    if (isset($fields['total_amount'])) {
+      $order->setOverrideTotalAmount($fields['total_amount']);
+    }
+    $lineItems = $order->getLineItems();
     try {
-      CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($fields, $self->_id, $self->_priceSet['fields']);
+      CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($fields, $self->_id, $lineItems);
     }
     catch (CRM_Core_Exception $e) {
       $errors['financial_type_id'] = ' ';
index 32fe022f824e83f57586d373b948234064c2e8b8..0fc9b58d9b66ff769db7c10b32df9f8081804b20 100644 (file)
@@ -375,13 +375,13 @@ LIMIT 1";
    * @param int $contributionID
    *   Contribution ID
    *
-   * @param array $priceSetFields
-   *   Array of price fields of a price set.
+   * @param array $orderLineItems
+   *   The line items from the Order.
    *
    * @return bool
    *
    */
-  public static function checkFinancialTypeHasDeferred($params, $contributionID = NULL, $priceSetFields = NULL) {
+  public static function checkFinancialTypeHasDeferred($params, $contributionID = NULL, $orderLineItems = []) {
     if (!Civi::settings()->get('deferred_revenue_enabled')) {
       return FALSE;
     }
@@ -399,16 +399,7 @@ LIMIT 1";
       $financialTypeID = $params['prevContribution']->financial_type_id;
     }
     if (($contributionID || !empty($params['price_set_id'])) && empty($lineItems)) {
-      if (!$contributionID) {
-        CRM_Price_BAO_PriceSet::processAmount($priceSetFields,
-          $params, $items);
-      }
-      else {
-        $items = CRM_Price_BAO_LineItem::getLineItems($contributionID, 'contribution', TRUE, TRUE, TRUE);
-      }
-      if (!empty($items)) {
-        $lineItems[] = $items;
-      }
+      $lineItems[] = $orderLineItems;
     }
     $deferredFinancialType = self::getDeferredFinancialType();
     $isError = FALSE;
index b8b4fdee762465ef5a9baa2b01cfa5dfbaefca72..5c98e35c2adcee7d4864677ed2d3cb7f2d99580e 100644 (file)
@@ -2046,4 +2046,131 @@ Price Field - Price Field 1        1   $ 100.00      $ 100.00
     ];
   }
 
+  /**
+   * Test formRule
+   */
+  public function testContributionFormRule() {
+    $fields = [
+      'contact_id' => $this->_individualId,
+      'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
+      'currency' => 'USD',
+      'total_amount' => '10',
+      'price_set_id' => '',
+      'source' => '',
+      'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
+      'cancel_date' => '',
+      'cancel_reason' => '',
+      'receive_date' => date('Y-m-d H:i:s'),
+      'from_email_address' => key(CRM_Core_BAO_Email::getFromEmail()),
+      'receipt_date' => '',
+      'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
+      'trxn_id' => '',
+      'check_number' => '',
+      'soft_credit_contact_id' => [
+        1 => '',
+        2 => '',
+        3 => '',
+        4 => '',
+        5 => '',
+        6 => '',
+        7 => '',
+        8 => '',
+        9 => '',
+        10 => '',
+      ],
+      'soft_credit_amount' => [
+        1 => '',
+        2 => '',
+        3 => '',
+        4 => '',
+        5 => '',
+        6 => '',
+        7 => '',
+        8 => '',
+        9 => '',
+        10 => '',
+      ],
+      'soft_credit_type' => [
+        1 => '',
+        2 => '',
+        3 => '',
+        4 => '',
+        5 => '',
+        6 => '',
+        7 => '',
+        8 => '',
+        9 => '',
+        10 => '',
+      ],
+    ];
+
+    $form = new CRM_Contribute_Form_Contribution();
+    $this->assertSame([], $form->formRule($fields, [], $form));
+  }
+
+  /**
+   * Check that formRule validates you can only have one contribution with a
+   * given trxn_id.
+   */
+  public function testContributionFormRuleDuplicateTrxn() {
+    $contribution = $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['trxn_id' => '1234']));
+
+    $fields = [
+      'contact_id' => $this->_individualId,
+      'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
+      'currency' => 'USD',
+      'total_amount' => '10',
+      'price_set_id' => '',
+      'source' => '',
+      'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
+      'cancel_date' => '',
+      'cancel_reason' => '',
+      'receive_date' => date('Y-m-d H:i:s'),
+      'from_email_address' => key(CRM_Core_BAO_Email::getFromEmail()),
+      'receipt_date' => '',
+      'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
+      'trxn_id' => '1234',
+      'check_number' => '',
+      'soft_credit_contact_id' => [
+        1 => '',
+        2 => '',
+        3 => '',
+        4 => '',
+        5 => '',
+        6 => '',
+        7 => '',
+        8 => '',
+        9 => '',
+        10 => '',
+      ],
+      'soft_credit_amount' => [
+        1 => '',
+        2 => '',
+        3 => '',
+        4 => '',
+        5 => '',
+        6 => '',
+        7 => '',
+        8 => '',
+        9 => '',
+        10 => '',
+      ],
+      'soft_credit_type' => [
+        1 => '',
+        2 => '',
+        3 => '',
+        4 => '',
+        5 => '',
+        6 => '',
+        7 => '',
+        8 => '',
+        9 => '',
+        10 => '',
+      ],
+    ];
+
+    $form = new CRM_Contribute_Form_Contribution();
+    $this->assertEquals(['trxn_id' => "Transaction ID's must be unique. Transaction '1234' already exists in your database."], $form->formRule($fields, [], $form));
+  }
+
 }