Merge pull request #2666 from kurund/test-fixes
[civicrm-core.git] / CRM / Price / BAO / LineItem.php
index 929012577241b0939f2759bfad5ad84897567e44..5fa1d3088df6e4171d0a30fbfa6e9fd9b22c298f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -92,6 +92,16 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem {
     return NULL;
   }
 
+  static function getLineTotal($entityId, $entityTable) {
+    $sqlLineItemTotal = "SELECT SUM(li.line_total)
+FROM civicrm_line_item li
+INNER JOIN civicrm_participant_payment pp ON ( li.entity_id = pp.participant_id
+AND li.entity_table = '{$entityTable}'
+AND li.entity_id = {$entityId})";
+    $lineItemTotal = CRM_Core_DAO::singleValueQuery($sqlLineItemTotal);
+    return $lineItemTotal;
+  }
+
   /**
    * Given a participant id/contribution id,
    * return contribution/fee line items
@@ -101,7 +111,7 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem {
    *
    * @return array of line items
    */
-  static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL) {
+  static function getLineItems($entityId, $entity = 'participant', $isQuick = NULL , $isQtyZero = TRUE) {
     $selectClause = $whereClause = $fromClause = NULL;
     $selectClause = "
       SELECT    li.id,
@@ -116,6 +126,7 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem {
       li.price_field_id,
       li.participant_count,
       li.price_field_value_id,
+      li.financial_type_id,
       pfv.description";
 
     $fromClause = "
@@ -130,6 +141,11 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem {
       $fromClause .= " LEFT JOIN civicrm_price_set cps on cps.id = pf.price_set_id ";
       $whereClause .= " and cps.is_quick_config = 0";
     }
+
+    if (!$isQtyZero) {
+      $whereClause .= " and li.qty != 0";
+    }
+
     $lineItems = array();
 
     if (!$entityId || !$entity || !$fromClause) {
@@ -158,6 +174,7 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem {
         'html_type' => $dao->html_type,
         'description' => $dao->description,
         'entity_id' => $entityId,
+        'financial_type_id' => $dao->financial_type_id,
         'membership_type_id' => $dao->membership_type_id,
         'membership_num_terms' => $dao->membership_num_terms,
       );
@@ -225,9 +242,9 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem {
         'membership_num_terms' => CRM_Utils_Array::value('membership_num_terms', $options[$oid]),
         'auto_renew' => CRM_Utils_Array::value('auto_renew', $options[$oid]),
         'html_type' => $fields['html_type'],
-        'financial_type_id' => CRM_Utils_Array::value( 'financial_type_id', $options[$oid]),
-
+        'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $options[$oid]),
       );
+
       if ($values[$oid]['membership_type_id'] && !isset($values[$oid]['auto_renew'])) {
         $values[$oid]['auto_renew'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $values[$oid]['membership_type_id'], 'auto_renew');
       }
@@ -286,7 +303,7 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem {
         $line['entity_id'] = $entityId;
         // if financial type is not set and if price field value is NOT NULL
         // get financial type id of price field value
-        if (CRM_Utils_Array::value('price_field_value_id', $line) && !CRM_Utils_Array::value('financial_type_id', $line)) {
+        if (!empty($line['price_field_value_id']) && empty($line['financial_type_id'])) {
           $line['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $line['price_field_value_id'], 'financial_type_id');
         }
         $lineItems = CRM_Price_BAO_LineItem::create($line);
@@ -379,11 +396,11 @@ class CRM_Price_BAO_LineItem extends CRM_Price_DAO_LineItem {
       foreach ($entityId as $id) {
         $lineItems = CRM_Price_BAO_LineItem::getLineItems($id, $entityTable);
         foreach ($lineItems as $key => $values) {
-          if (!$setID) {
+          if (!$setID && $values['price_field_id']) {
             $setID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $values['price_field_id'], 'price_set_id');
             $params['is_quick_config'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'is_quick_config');
           }
-          if (CRM_Utils_Array::value('is_quick_config', $params) && array_key_exists('total_amount', $params)
+          if (!empty($params['is_quick_config']) && array_key_exists('total_amount', $params)
             && $totalEntityId == 1) {
             $values['line_total'] = $values['unit_price'] = $params['total_amount'];
           }