[REF] Cleanup code to determine financial_type_id
[civicrm-core.git] / CRM / Financial / BAO / Order.php
index 66cfdc210467c30801212e1e4e2bb40bdc8c80a7..f1d0c771db3775be05406777876597fa5ebbc630 100644 (file)
@@ -78,6 +78,13 @@ class CRM_Financial_BAO_Order {
    */
   protected $priceFieldMetadata = [];
 
+  /**
+   * Metadata for price sets.
+   *
+   * @var array
+   */
+  protected $priceSetMetadata = [];
+
   /**
    * Get form object.
    *
@@ -245,7 +252,7 @@ class CRM_Financial_BAO_Order {
    */
   public function getPriceFieldsMetadata(): array {
     if (empty($this->priceFieldMetadata)) {
-      $this->priceFieldMetadata = CRM_Price_BAO_PriceSet::getCachedPriceSetDetail($this->getPriceSetID())['fields'];
+      $this->getPriceSetMetadata();
       if ($this->getForm()) {
         CRM_Utils_Hook::buildAmount($this->form->getFormContext(), $this->form, $this->priceFieldMetadata);
       }
@@ -253,6 +260,32 @@ class CRM_Financial_BAO_Order {
     return $this->priceFieldMetadata;
   }
 
+  /**
+   * Get the metadata for the fields in the price set.
+   *
+   * @return array
+   */
+  public function getPriceSetMetadata(): array {
+    if (empty($this->priceSetMetadata)) {
+      $priceSetMetadata = CRM_Price_BAO_PriceSet::getCachedPriceSetDetail($this->getPriceSetID());
+      $this->priceFieldMetadata = $priceSetMetadata['fields'];
+      unset($priceSetMetadata['fields']);
+      $this->priceSetMetadata = $priceSetMetadata;
+    }
+    return $this->priceSetMetadata;
+  }
+
+  /**
+   * Get the financial type id for the order.
+   *
+   * This may differ to the line items....
+   *
+   * @return int
+   */
+  public function getFinancialTypeID(): int {
+    return (int) $this->getOverrideFinancialTypeID() ?: $this->getPriceSetMetadata()['financial_type_id'];
+  }
+
   /**
    * Set the price field selection from an array of params containing price fields.
    *