-- CRM-15055, handled creation of line item via membership api and import
authorPradeep Nayak <pradeep.nayak@webaccess.co.in>
Sat, 30 Aug 2014 21:58:18 +0000 (03:28 +0530)
committerPradeep Nayak <pradeep.nayak@webaccess.co.in>
Wed, 3 Sep 2014 09:27:19 +0000 (14:57 +0530)
----------------------------------------
* CRM-15055: Line item for membership entity should be created even when Record Payment is not checked
  https://issues.civicrm.org/jira/browse/CRM-15055

CRM/Member/BAO/Membership.php
CRM/Member/Form/Membership.php
CRM/Member/Import/Parser/Membership.php
CRM/Price/BAO/LineItem.php
CRM/Price/BAO/PriceSet.php
api/v3/Membership.php

index 7192bf43aa4bfed86aa914a6602868fe41149d39..195e80d771b89aa417933a27b9ff7cd60fb8219e 100644 (file)
@@ -325,8 +325,17 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
       $params['contribution'] = self::recordMembershipContribution($memInfo, $ids);
     }
     
-    if (CRM_Utils_Array::value('lineItems', $params) && empty($params['contributionId'])) {
-      CRM_Price_BAO_LineItem::processPriceSet($membership->id, $params['lineItems'], CRM_Utils_Array::value('contribution', $params));
+    if (!empty($params['lineItems'])) {
+      $params['line_item'] = $params['lineItems'];
+    }
+
+    //do cleanup line  items if membership edit the Membership type.
+    if (empty($ids['contribution']) && !empty($ids['membership'])) {
+      CRM_Price_BAO_LineItem::deleteLineItems($ids['membership'], 'civicrm_membership');
+    }
+    
+    if (!empty($params['line_item']) && empty($ids['contribution'])) {
+      CRM_Price_BAO_LineItem::processPriceSet($membership->id, $params['line_item'], CRM_Utils_Array::value('contribution', $params));
     }
 
     //insert payment record for this membership
index c1994939760985cfc5bcfe32641bec10d183427a..018978e6d153406d18e17bbf9e9a55794a5ab0ff 100644 (file)
@@ -1245,12 +1245,6 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
     $membershipType = implode(', ', $membershipTypes);
     
-    //do cleanup line  items if membership edit the Membership Fee.
-    $params['contributionId'] = CRM_Utils_Array::value('contribution_id', $this->_defaultValues);
-    if (empty($params['contributionId']) && $this->_id) {
-      CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_membership');
-    }
-
     // Retrieve the name and email of the current user - this will be the FROM for the receipt email
     list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($ids['userId']);
 
index 01c82f51041d9aa3d7c2d37370defae39301d648..09958ab63128fdeb04474a4c78717014ff5f1ee3 100644 (file)
@@ -367,7 +367,7 @@ class CRM_Member_Import_Parser_Membership extends CRM_Member_Import_Parser {
           $dao->id = $formatValues['membership_id'];
           $dates   = array('join_date', 'start_date', 'end_date');
           foreach ($dates as $v) {
-                      if (empty($formatted[$v])) {
+            if (empty($formatted[$v])) {
               $formatted[$v] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $formatValues['membership_id'], $v);
             }
           }
@@ -382,7 +382,11 @@ class CRM_Member_Import_Parser_Membership extends CRM_Member_Import_Parser {
               'membership' => $formatValues['membership_id'],
               'userId' => $session->get('userID'),
             );
-
+            
+            if (empty($params['line_item']) && !empty($formatted['membership_type_id'])) {
+              CRM_Price_BAO_LineItem::getLineItemArray($formatted, NULL, 'membership', $formatted['membership_type_id']);
+            }
+            
             $newMembership = CRM_Member_BAO_Membership::create($formatted, $ids, TRUE);
             if (civicrm_error($newMembership)) {
               array_unshift($values, $newMembership['is_error'] . ' for Membership ID ' . $formatValues['membership_id'] . '. Row was skipped.');
index f87af912552ed8497b1d7f703185f6c4a3b2f233..b8053645b1a29a33a6181c7f8e42b41d2c835c0d 100644 (file)
@@ -432,17 +432,30 @@ AND li.entity_id = {$entityId}
   static function getLineItemArray(&$params, $entityId = NULL, $entityTable = 'contribution', $isRelatedID = FALSE) {
 
     if (!$entityId) {
-      $priceSetDetails = CRM_Price_BAO_PriceSet::getDefaultPriceSet();
+      $priceSetDetails = CRM_Price_BAO_PriceSet::getDefaultPriceSet($entityTable);
+      $totalAmount = CRM_Utils_Array::value('total_amount', $params);
+      $financialType = CRM_Utils_Array::value('financial_type_id', $params);
       foreach ($priceSetDetails as $values) {
+        if ($entityTable == 'membership') { 
+          if ($isRelatedID != $values['membership_type_id']) {
+            continue;
+          }
+          if (!$totalAmount) {
+            $totalAmount = $values['amount'];
+          }
+          $financialType = $values['financial_type_id'];
+        }
         $params['line_item'][$values['setID']][$values['priceFieldID']] = array(
           'price_field_id' => $values['priceFieldID'],
           'price_field_value_id' => $values['priceFieldValueID'],
           'label' => $values['label'],
           'qty' => 1,
-          'unit_price' => $params['total_amount'],
-          'line_total' => $params['total_amount'],
-          'financial_type_id' => $params['financial_type_id']
+          'unit_price' => $totalAmount,
+          'line_total' => $totalAmount,
+          'financial_type_id' => $financialType,
+          'membership_type_id' => $values['membership_type_id']
         );
+        break;
       }
     }
     else {
index baef902dc7258e1f2e0e1fb46e6884808c3a97c0..710044521a9585dc1202062b7c37d7010d8e326c 100644 (file)
@@ -121,7 +121,7 @@ class CRM_Price_BAO_PriceSet extends CRM_Price_DAO_PriceSet {
     }
 
     $sql = "
-SELECT      ps.id AS setID, pfv.price_field_id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.name, pfv.label
+SELECT      ps.id AS setID, pfv.price_field_id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.name, pfv.label, pfv.membership_type_id, pfv.amount, pfv.financial_type_id
 FROM        civicrm_price_set ps
 LEFT JOIN   civicrm_price_field pf ON pf.`price_set_id` = ps.id
 LEFT JOIN   civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
@@ -136,6 +136,9 @@ WHERE       ps.name = '{$entityName}'
       $defaultPriceSet[$dao->priceFieldValueID]['name'] = $dao->name;
       $defaultPriceSet[$dao->priceFieldValueID]['label'] = $dao->label;
       $defaultPriceSet[$dao->priceFieldValueID]['priceFieldValueID'] = $dao->priceFieldValueID;
+      $defaultPriceSet[$dao->priceFieldValueID]['membership_type_id'] = $dao->membership_type_id;
+      $defaultPriceSet[$dao->priceFieldValueID]['amount'] = $dao->amount;
+      $defaultPriceSet[$dao->priceFieldValueID]['financial_type_id'] = $dao->financial_type_id;
     }
 
     return $defaultPriceSet;
index 29b4810f85089113f6cacf670a5006a06a6e3f7e..03582db73e5d40700138fd89081e1604b1da33c3 100644 (file)
@@ -123,6 +123,10 @@ function civicrm_api3_membership_create($params) {
   //need to pass action to handle related memberships.
   $params['action'] = $action;
 
+  if (empty($params['line_item']) && !empty($params['membership_type_id'])) {
+    CRM_Price_BAO_LineItem::getLineItemArray($params, NULL, 'membership', $params['membership_type_id']);
+  }
+
   $membershipBAO = CRM_Member_BAO_Membership::create($params, $ids, TRUE);
 
   if (array_key_exists('is_error', $membershipBAO)) {