CRM-17034 : PriceField tax_amount should not be rounded
[civicrm-core.git] / CRM / Price / BAO / PriceField.php
index 859063d959e311965923e4d722a514e5a48cc927..cc0aee8609b3eb3275c69f794fe6b68abd593e81 100644 (file)
@@ -3,7 +3,7 @@
   +--------------------------------------------------------------------+
   | CiviCRM version 4.6                                                |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2014                                |
+  | Copyright CiviCRM LLC (c) 2004-2015                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
   | GNU Affero General Public License or the licensing of CiviCRM,     |
   | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
   +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
@@ -42,7 +42,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
   protected $_options;
 
   /**
-   * Takes an associative array and creates a price field object
+   * Takes an associative array and creates a price field object.
    *
    * the function extract all the params it needs to initialize the create a
    * price field object. the params array could contain additional unused name/value
@@ -67,7 +67,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
   }
 
   /**
-   * Takes an associative array and creates a price field object
+   * Takes an associative array and creates a price field object.
    *
    * This function is invoked from within the web form layer and also from the api layer
    *
@@ -168,7 +168,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
   }
 
   /**
-   * Fetch object based on array of properties
+   * Fetch object based on array of properties.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
@@ -182,7 +182,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
   }
 
   /**
-   * Update the is_active flag in the db
+   * Update the is_active flag in the db.
    *
    * @param int $id
    *   Id of the database record.
@@ -190,13 +190,20 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
    *   Value we want to set the is_active field.
    *
    * @return Object
-   *   DAO object on sucess, null otherwise
-   *
+   *   DAO object on success, null otherwise.
    */
   public static function setIsActive($id, $is_active) {
     return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceField', $id, 'is_active', $is_active);
   }
 
+  /**
+   * Freeze form if the event is full.
+   *
+   * @param $element
+   * @param $fieldOptions
+   *
+   * @return null
+   */
   public static function freezeIfEnabled(&$element, $fieldOptions) {
     if (!empty($fieldOptions['is_full'])) {
       $element->freeze();
@@ -219,7 +226,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
   }
 
   /**
-   * This function for building custom fields
+   * This function for building custom fields.
    *
    * @param CRM_Core_Form $qf
    *   Form object (reference).
@@ -313,22 +320,17 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           ));
 
         $extra = array();
-        if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount)) {
-          foreach ($fieldOptions as &$fieldOption) {
-            if ($fieldOption['name'] == 'other_amount') {
-              $fieldOption['label'] = $fieldOption['label'] . '  ' . $currencySymbol;
-            }
-          }
-          $qf->assign('priceset', $elementName);
-          $extra = array('onclick' => 'useAmountOther();');
-        }
-
         if (!empty($qf->_membershipBlock) && !empty($qf->_quickConfig) && $field->name == 'other_amount' && empty($qf->_contributionAmount)) {
           $useRequired = 0;
         }
         elseif (!empty($fieldOptions[$optionKey]['label'])) {
           //check for label.
           $label = $fieldOptions[$optionKey]['label'];
+          if (!empty($qf->_quickConfig) && !empty($qf->_contributionAmount) && strtolower($fieldOptions[$optionKey]['name']) == 'other_amount') {
+            $label .= '  ' . $currencySymbol;
+            $qf->assign('priceset', $elementName);
+            $extra = array('onclick' => 'useAmountOther();');
+          }
         }
 
         $element = &$qf->add('text', $elementName, $label,
@@ -346,7 +348,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
 
         // CRM-6902 - Add "max" option for a price set field
         if (in_array($optionKey, $freezeOptions)) {
-          self::freezeIfEnabled($element, $freezeOptions);
+          self::freezeIfEnabled($element, $fieldOptions[$optionKey]);
           // CRM-14696 - Improve display for sold out price set options
           $element->setLabel($label . '&nbsp;<span class="sold-out-option">' . ts('Sold out') . '</span>');
         }
@@ -399,6 +401,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
             'price' => json_encode(array($elementName, $priceVal)),
             'data-amount' => $opt[$valueFieldName],
             'data-currency' => $currencyName,
+            'data-price-field-values' => json_encode($customOption),
           );
           if (!empty($qf->_quickConfig) && $field->name == 'contribution_amount') {
             $extra += array('onclick' => 'clearAmountOther();');
@@ -481,7 +484,6 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
             }
           }
 
-          $selectOption[$opt['id']] = $opt['label'];
           $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
 
           if (!in_array($opt['id'], $freezeOptions)) {
@@ -489,6 +491,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           }
           // CRM-14696 - Improve display for sold out price set options
           else {
+            $opt['id'] = 'crm_disabled_opt-' . $opt['id'];
             $opt['label'] = $opt['label'] . ' (' . ts('Sold out') . ')';
           }
 
@@ -504,7 +507,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
             '' => ts('- select -'),
           ) + $selectOption,
           $useRequired && $field->is_required,
-          array('price' => json_encode($priceVal))
+          array('price' => json_encode($priceVal), 'class' => 'crm-select2', 'data-price-field-values' => json_encode($customOption))
         );
 
         // CRM-6902 - Add "max" option for a price set field
@@ -562,7 +565,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
   }
 
   /**
-   * Retrieve a list of options for the specified field
+   * Retrieve a list of options for the specified field.
    *
    * @param int $fieldId
    *   Price field ID.
@@ -589,7 +592,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
         if (isset($priceFieldValues['financial_type_id']) && array_key_exists($priceFieldValues['financial_type_id'], $taxRates)) {
           $options[$fieldId][$priceFieldId]['tax_rate'] = $taxRates[$priceFieldValues['financial_type_id']];
           $taxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($priceFieldValues['amount'], $options[$fieldId][$priceFieldId]['tax_rate']);
-          $options[$fieldId][$priceFieldId]['tax_amount'] = round($taxAmount['tax_amount'], 2);
+          $options[$fieldId][$priceFieldId]['tax_amount'] = $taxAmount['tax_amount'];
         }
       }
     }
@@ -677,7 +680,7 @@ WHERE
   }
 
   /**
-   * Validate the priceset
+   * Validate the priceset.
    *
    * @param int $priceSetId
    *   , array $fields.
@@ -803,4 +806,5 @@ WHERE  id IN (" . implode(',', array_keys($priceFields)) . ')';
 
     return $label;
   }
+
 }