From 82cc67759132ea48e725ec5a621288694ebfc344 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Sun, 31 Aug 2014 03:28:18 +0530 Subject: [PATCH] -- CRM-15055, handled creation of line item via membership api and import ---------------------------------------- * 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 | 13 +++++++++++-- CRM/Member/Form/Membership.php | 6 ------ CRM/Member/Import/Parser/Membership.php | 8 ++++++-- CRM/Price/BAO/LineItem.php | 21 +++++++++++++++++---- CRM/Price/BAO/PriceSet.php | 5 ++++- api/v3/Membership.php | 4 ++++ 6 files changed, 42 insertions(+), 15 deletions(-) diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 7192bf43aa..195e80d771 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -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 diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index c199493976..018978e6d1 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -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']); diff --git a/CRM/Member/Import/Parser/Membership.php b/CRM/Member/Import/Parser/Membership.php index 01c82f5104..09958ab631 100644 --- a/CRM/Member/Import/Parser/Membership.php +++ b/CRM/Member/Import/Parser/Membership.php @@ -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.'); diff --git a/CRM/Price/BAO/LineItem.php b/CRM/Price/BAO/LineItem.php index f87af91255..b8053645b1 100644 --- a/CRM/Price/BAO/LineItem.php +++ b/CRM/Price/BAO/LineItem.php @@ -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 { diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index baef902dc7..710044521a 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -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; diff --git a/api/v3/Membership.php b/api/v3/Membership.php index 29b4810f85..03582db73e 100644 --- a/api/v3/Membership.php +++ b/api/v3/Membership.php @@ -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)) { -- 2.25.1