Commit c9053672 authored by Monish Deb's avatar Monish Deb Committed by GitHub
Browse files

Merge pull request #10370 from JMAConsulting/CRM-20321

CRM-20321, CRM-20652 : Changing membership type should change related contribution and generalise changeFeeSelection function to be used for any entity
parents 64e9e192 2ced2272
......@@ -37,6 +37,7 @@
class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences {
protected $_settings = array(
'cvv_backoffice_required' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
'update_contribution_on_membership_type_change' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
'acl_financial_type' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
'always_post_to_accounts_receivable' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
'deferred_revenue_enabled' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
......@@ -196,6 +197,9 @@ class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences {
unset($params['qfKey']);
unset($params['entryURL']);
Civi::settings()->set('contribution_invoice_settings', $params);
Civi::settings()->set('update_contribution_on_membership_type_change',
CRM_Utils_Array::value('update_contribution_on_membership_type_change', $params)
);
// to set default value for 'Invoices / Credit Notes' checkbox on display preferences
$values = CRM_Core_BAO_Setting::getItem("CiviCRM Preferences");
......
......@@ -4085,6 +4085,11 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']})
}
}
}
elseif ($component == 'membership') {
$entity = $component;
$entityTable = 'civicrm_membership';
$contributionId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', $id, 'contribution_id', 'membership_id');
}
else {
$contributionId = $id;
$entity = 'contribution';
......
......@@ -466,6 +466,9 @@ WHERE ceft.entity_id = %1";
if ($entityName == 'participant') {
$contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $entityId, 'contribution_id', 'participant_id');
}
elseif ($entityName == 'membership') {
$contributionId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', $entityId, 'contribution_id', 'membership_id');
}
else {
$contributionId = $entityId;
}
......
......@@ -1880,323 +1880,6 @@ WHERE civicrm_participant.contact_id = {$contactID} AND
}
}
/**
* @param array $params
* @param int $participantId
* @param int $contributionId
* @param $feeBlock
* @param array $lineItems
* @param $paidAmount
* @param int $priceSetId
*/
public static function changeFeeSelections($params, $participantId, $contributionId, $feeBlock, $lineItems, $paidAmount, $priceSetId) {
$contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses);
$pendingRefundStatusId = array_search('Pending refund', $contributionStatuses);
$previousLineItems = CRM_Price_BAO_LineItem::getLineItems($participantId, 'participant');
CRM_Price_BAO_PriceSet::processAmount($feeBlock,
$params, $lineItems
);
// get the submitted
foreach ($feeBlock as $id => $values) {
CRM_Price_BAO_LineItem::format($id, $params, $values, $submittedLineItems);
$submittedFieldId[] = CRM_Utils_Array::retrieveValueRecursive($submittedLineItems, 'price_field_id');
}
if (!empty($submittedLineItems)) {
$insertLines = $submittedLineItems;
$submittedFieldValueIds = array_keys($submittedLineItems);
$updateLines = array();
foreach ($previousLineItems as $id => $previousLineItem) {
// check through the submitted items if the previousItem exists,
// if found in submitted items, do not use it for new item creations
if (in_array($previousLineItem['price_field_value_id'], $submittedFieldValueIds)) {
// if submitted line items are existing don't fire INSERT query
if ($previousLineItem['line_total'] != 0) {
unset($insertLines[$previousLineItem['price_field_value_id']]);
}
else {
$insertLines[$previousLineItem['price_field_value_id']]['skip'] = TRUE;
}
// for updating the line items i.e. use-case - once deselect-option selecting again
if (($previousLineItem['line_total'] != $submittedLineItems[$previousLineItem['price_field_value_id']]['line_total']) ||
($submittedLineItems[$previousLineItem['price_field_value_id']]['line_total'] == 0 && $submittedLineItems[$previousLineItem['price_field_value_id']]['qty'] == 1) ||
($previousLineItem['qty'] != $submittedLineItems[$previousLineItem['price_field_value_id']]['qty'])
) {
$updateLines[$previousLineItem['price_field_value_id']] = $submittedLineItems[$previousLineItem['price_field_value_id']];
$updateLines[$previousLineItem['price_field_value_id']]['id'] = $id;
}
}
}
$submittedFields = implode(', ', $submittedFieldId);
$submittedFieldValues = implode(', ', $submittedFieldValueIds);
}
$financialItemsArray = array();
if (!empty($submittedFields) && !empty($submittedFieldValues)) {
$updateLineItem = "UPDATE civicrm_line_item li
INNER JOIN civicrm_financial_item fi
ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item')
SET li.qty = 0,
li.line_total = 0.00,
li.tax_amount = NULL
WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId}) AND
(price_field_value_id NOT IN ({$submittedFieldValues}))
";
CRM_Core_DAO::executeQuery($updateLineItem);
// gathering necessary info to record negative (deselected) financial_item
$updateFinancialItem = "
SELECT fi.*, SUM(fi.amount) as differenceAmt, price_field_value_id, financial_type_id, tax_amount
FROM civicrm_financial_item fi LEFT JOIN civicrm_line_item li ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item')
WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId})
GROUP BY li.entity_table, li.entity_id, price_field_value_id, fi.id
";
$updateFinancialItemInfoDAO = CRM_Core_DAO::executeQuery($updateFinancialItem);
$trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contributionId, 'DESC', TRUE);
$trxnId['id'] = $trxn['financialTrxnId'];
$invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
$taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
$updateFinancialItemInfoValues = array();
while ($updateFinancialItemInfoDAO->fetch()) {
$updateFinancialItemInfoValues = (array) $updateFinancialItemInfoDAO;
$updateFinancialItemInfoValues['transaction_date'] = date('YmdHis');
// the below params are not needed
unset($updateFinancialItemInfoValues['id']);
unset($updateFinancialItemInfoValues['created_date']);
// if not submitted and difference is not 0 make it negative
if (!in_array($updateFinancialItemInfoValues['price_field_value_id'], $submittedFieldValueIds) && $updateFinancialItemInfoValues['differenceAmt'] != 0) {
// INSERT negative financial_items
$updateFinancialItemInfoValues['amount'] = -$updateFinancialItemInfoValues['amount'];
if ($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']) {
$updateFinancialItemInfoValues['tax']['amount'] = -($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']);
$updateFinancialItemInfoValues['tax']['description'] = $taxTerm;
if ($updateFinancialItemInfoValues['financial_type_id']) {
$updateFinancialItemInfoValues['tax']['financial_account_id'] = CRM_Contribute_BAO_Contribution::getFinancialAccountId($updateFinancialItemInfoValues['financial_type_id']);
}
}
// INSERT negative financial_items for tax amount
$financialItemsArray[] = $updateFinancialItemInfoValues;
}
// if submitted and difference is 0 add a positive entry again
elseif (in_array($updateFinancialItemInfoValues['price_field_value_id'], $submittedFieldValueIds) && $updateFinancialItemInfoValues['differenceAmt'] == 0) {
$updateFinancialItemInfoValues['amount'] = $updateFinancialItemInfoValues['amount'];
// INSERT financial_items for tax amount
if ($updateFinancialItemInfoValues['entity_id'] == $updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['id'] &&
isset($updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['tax_amount'])
) {
$updateFinancialItemInfoValues['tax']['amount'] = $updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['tax_amount'];
$updateFinancialItemInfoValues['tax']['description'] = $taxTerm;
if ($updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['financial_type_id']) {
$updateFinancialItemInfoValues['tax']['financial_account_id'] = CRM_Contribute_BAO_Contribution::getFinancialAccountId($updateLines[$updateFinancialItemInfoValues['price_field_value_id']]['financial_type_id']);
}
}
$financialItemsArray[] = $updateFinancialItemInfoValues;
}
}
}
elseif (empty($submittedFields) && empty($submittedFieldValues)) {
$updateLineItem = "UPDATE civicrm_line_item li
INNER JOIN civicrm_financial_item fi
ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item')
SET li.qty = 0,
li.line_total = 0.00,
li.tax_amount = NULL
WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId})";
CRM_Core_DAO::executeQuery($updateLineItem);
}
$amountLevel = array();
$totalParticipant = $participantCount = 0;
if (!empty($updateLines)) {
foreach ($updateLines as $valueId => $vals) {
$taxAmount = "NULL";
if (isset($vals['tax_amount'])) {
$taxAmount = $vals['tax_amount'];
}
$amountLevel[] = $vals['label'] . ' - ' . (float) $vals['qty'];
if (isset($vals['participant_count'])) {
$participantCount = $vals['participant_count'];
$totalParticipant += $vals['participant_count'];
}
$updateLineItem = "
UPDATE civicrm_line_item li
SET li.qty = {$vals['qty']},
li.line_total = {$vals['line_total']},
li.tax_amount = {$taxAmount},
li.unit_price = {$vals['unit_price']},
li.participant_count = {$participantCount},
li.label = %1
WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId}) AND
(price_field_value_id = {$valueId})
";
CRM_Core_DAO::executeQuery($updateLineItem, array(1 => array($vals['label'], 'String')));
}
}
// insert new 'adjusted amount' transaction entry and update contribution entry.
// ensure entity_financial_trxn table has a linking of it.
// insert new line items
if (!empty($insertLines)) {
foreach ($insertLines as $valueId => $lineParams) {
if (!array_key_exists('skip', $lineParams)) {
$lineParams['entity_table'] = 'civicrm_participant';
$lineParams['entity_id'] = $participantId;
$lineParams['contribution_id'] = $contributionId;
$lineObj = CRM_Price_BAO_LineItem::create($lineParams);
}
}
}
// the recordAdjustedAmt code would execute over here
$ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
if (count($ids) > 1) {
$updatedAmount = CRM_Price_BAO_LineItem::getLineTotal($contributionId);
}
else {
$updatedAmount = $params['amount'];
}
if (strlen($params['tax_amount']) != 0) {
$taxAmount = $params['tax_amount'];
}
else {
$taxAmount = "NULL";
}
$displayParticipantCount = '';
if ($totalParticipant > 0) {
$displayParticipantCount = ' Participant Count -' . $totalParticipant;
}
$updateAmountLevel = NULL;
if (!empty($amountLevel)) {
$updateAmountLevel = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amountLevel) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR;
}
$trxn = self::recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount, $updateAmountLevel);
$trxnId = array();
if ($trxn) {
$trxnId['id'] = $trxn->id;
foreach ($financialItemsArray as $updateFinancialItemInfoValues) {
CRM_Financial_BAO_FinancialItem::create($updateFinancialItemInfoValues, NULL, $trxnId);
if (!empty($updateFinancialItemInfoValues['tax'])) {
$updateFinancialItemInfoValues['tax']['amount'] = $updateFinancialItemInfoValues['amount'];
$updateFinancialItemInfoValues['tax']['description'] = $updateFinancialItemInfoValues['description'];
if (!empty($updateFinancialItemInfoValues['financial_account_id'])) {
$updateFinancialItemInfoValues['financial_account_id'] = $updateFinancialItemInfoValues['tax']['financial_account_id'];
}
CRM_Financial_BAO_FinancialItem::create($updateFinancialItemInfoValues, NULL, $trxnId);
}
}
}
$fetchCon = array('id' => $contributionId);
$updatedContribution = CRM_Contribute_BAO_Contribution::retrieve($fetchCon, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray);
// insert financial items
if (!empty($insertLines)) {
foreach ($insertLines as $valueId => $lineParams) {
$lineParams['entity_table'] = 'civicrm_participant';
$lineParams['entity_id'] = $participantId;
$lineObj = CRM_Price_BAO_LineItem::retrieve($lineParams, CRM_Core_DAO::$_nullArray);
// insert financial items
// ensure entity_financial_trxn table has a linking of it.
$prevItem = CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution, NULL, $trxnId);
if (isset($lineObj->tax_amount)) {
CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution, TRUE, $trxnId);
}
}
}
// update participant fee_amount column
$partUpdateFeeAmt['id'] = $participantId;
$getUpdatedLineItems = "SELECT *
FROM civicrm_line_item
WHERE (entity_table = 'civicrm_participant' AND entity_id = {$participantId} AND qty > 0)";
$getUpdatedLineItemsDAO = CRM_Core_DAO::executeQuery($getUpdatedLineItems);
$line = array();
while ($getUpdatedLineItemsDAO->fetch()) {
$line[$getUpdatedLineItemsDAO->price_field_value_id] = $getUpdatedLineItemsDAO->label . ' - ' . (float) $getUpdatedLineItemsDAO->qty;
}
$partUpdateFeeAmt['fee_level'] = implode(', ', $line);
$partUpdateFeeAmt['fee_amount'] = $params['amount'];
self::add($partUpdateFeeAmt);
//activity creation
self::addActivityForSelection($participantId, 'Change Registration');
}
/**
* Record adjusted amount.
*
* @param int $updatedAmount
* @param int $paidAmount
* @param int $contributionId
*
* @param int $taxAmount
* @param bool $updateAmountLevel
*
* @return bool|\CRM_Core_BAO_FinancialTrxn
*/
public static function recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount = NULL, $updateAmountLevel = NULL) {
$pendingAmount = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId);
$pendingAmount = CRM_Utils_Array::value('total_amount', $pendingAmount, 0);
$balanceAmt = $updatedAmount - $paidAmount;
if ($paidAmount != $pendingAmount) {
$balanceAmt -= $pendingAmount;
}
$contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses);
$pendingRefundStatusId = array_search('Pending refund', $contributionStatuses);
$completedStatusId = array_search('Completed', $contributionStatuses);
$updatedContributionDAO = new CRM_Contribute_BAO_Contribution();
$adjustedTrxn = $skip = FALSE;
if ($balanceAmt) {
if ($balanceAmt > 0 && $paidAmount != 0) {
$contributionStatusVal = $partiallyPaidStatusId;
}
elseif ($balanceAmt < 0 && $paidAmount != 0) {
$contributionStatusVal = $pendingRefundStatusId;
}
elseif ($paidAmount == 0) {
//skip updating the contribution status if no payment is made
$skip = TRUE;
$updatedContributionDAO->cancel_date = 'null';
$updatedContributionDAO->cancel_reason = NULL;
}
// update contribution status and total amount without trigger financial code
// as this is handled in current BAO function used for change selection
$updatedContributionDAO->id = $contributionId;
if (!$skip) {
$updatedContributionDAO->contribution_status_id = $contributionStatusVal;
}
$updatedContributionDAO->total_amount = $updatedContributionDAO->net_amount = $updatedAmount;
$updatedContributionDAO->fee_amount = 0;
$updatedContributionDAO->tax_amount = $taxAmount;
if (!empty($updateAmountLevel)) {
$updatedContributionDAO->amount_level = $updateAmountLevel;
}
$updatedContributionDAO->save();
// adjusted amount financial_trxn creation
$updatedContribution = CRM_Contribute_BAO_Contribution::getValues(
array('id' => $contributionId),
CRM_Core_DAO::$_nullArray,
CRM_Core_DAO::$_nullArray
);
$toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($updatedContribution->financial_type_id, 'Accounts Receivable Account is');
$adjustedTrxnValues = array(
'from_financial_account_id' => NULL,
'to_financial_account_id' => $toFinancialAccount,
'total_amount' => $balanceAmt,
'net_amount' => $balanceAmt,
'status_id' => $completedStatusId,
'payment_instrument_id' => $updatedContribution->payment_instrument_id,
'contribution_id' => $updatedContribution->id,
'trxn_date' => date('YmdHis'),
'currency' => $updatedContribution->currency,
);
$adjustedTrxn = CRM_Core_BAO_FinancialTrxn::create($adjustedTrxnValues);
}
return $adjustedTrxn;
}
/**
* @param int $participantId
* @param $activityType
......
......@@ -239,7 +239,7 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
$feeBlock = $this->_values['fee'];
$lineItems = $this->_values['line_items'];
CRM_Event_BAO_Participant::changeFeeSelections($params, $this->_participantId, $this->_contributionId, $feeBlock, $lineItems, $this->_paidAmount, $params['priceSetId']);
CRM_Price_BAO_LineItem::changeFeeSelections($params, $this->_participantId, 'participant', $this->_contributionId, $feeBlock, $lineItems, $this->_paidAmount);
$this->contributionAmt = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'total_amount');
// email sending
if (!empty($params['send_receipt'])) {
......
......@@ -1723,10 +1723,91 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
}
$isRecur = CRM_Utils_Array::value('is_recur', $params);
$this->updateContributionOnMembershipTypeChange($params, $membership);
$this->setStatusMessage($membership, $endDate, $receiptSent, $membershipTypes, $createdMemberships, $isRecur, $calcDates, $mailSend);
return $createdMemberships;
}
/**
* Update related contribution of a membership if update_contribution_on_membership_type_change
* contribution setting is enabled and type is changed on edit
*
* @param array $inputParams
* submitted form values
* @param CRM_Member_DAO_Membership $membership
* Updated membership object
*
*/
protected function updateContributionOnMembershipTypeChange($inputParams, $membership) {
if (Civi::settings()->get('update_contribution_on_membership_type_change') &&
($this->_action & CRM_Core_Action::UPDATE) && // on update
$this->_id && // if ID is present
!in_array($this->_memType, $this->_memTypeSelected) // if selected membership doesn't match with earlier membership
) {
if (CRM_Utils_Array::value('is_recur', $inputParams)) {
CRM_Core_Session::setStatus(ts('Associated recurring contribution cannot be updated on membership type change.', ts('Error'), 'error'));
return;
}
// fetch lineitems by updated membership ID
$lineItems = CRM_Price_BAO_LineItem::getLineItems($membership->id, 'membership');
// retrieve the related contribution ID
$contributionID = CRM_Core_DAO::getFieldValue(
'CRM_Member_DAO_MembershipPayment',
$membership->id,
'contribution_id',
'membership_id'
);
// get price fields of chosen price-set
$priceSetDetails = CRM_Utils_Array::value(
$this->_priceSetId,
CRM_Price_BAO_PriceSet::getSetDetail(
$this->_priceSetId,
TRUE,
TRUE
)
);
// add price field information in $inputParams
self::addPriceFieldByMembershipType($inputParams, $priceSetDetails['fields'], $membership->membership_type_id);
// paid amount
$paidAmount = CRM_Utils_Array::value('paid', CRM_Contribute_BAO_Contribution::getPaymentInfo($membership->id, 'membership'));
// update related contribution and financial records
CRM_Price_BAO_LineItem::changeFeeSelections(
$inputParams,
$membership->id,
'membership',
$contributionID,
$priceSetDetails['fields'],
$lineItems, $paidAmount
);
CRM_Core_Session::setStatus(ts('Associated contribution is updated on membership type change.'), ts('Success'), 'success');
}
}
/**
* Add selected price field information in $formValues
*
* @param array $formValues
* submitted form values
* @param array $priceFields
* Price fields of selected Priceset ID
* @param int $membershipTypeID
* Selected membership type ID
*
*/
public static function addPriceFieldByMembershipType(&$formValues, $priceFields, $membershipTypeID) {
foreach ($priceFields as $priceFieldID => $priceField) {
if (isset($priceField['options']) && count($priceField['options'])) {
foreach ($priceField['options'] as $option) {
if ($option['membership_type_id'] == $membershipTypeID) {
$formValues["price_{$priceFieldID}"] = $option['id'];
break;
}
}
}
}
}
/**
* Set context in session.
*/
......
This diff is collapsed.
......@@ -240,6 +240,21 @@ function setting_getfields_expectedresult() {
'description' => '',
'help_text' => '',
),
'update_contribution_on_membership_type_change' => array(
'group_name' => 'Contribute Preferences',
'group' => 'contribute',
'name' => 'update_contribution_on_membership_type_change',
'type' => 'Integer',
'html_type' => 'checkbox',
'quick_form_type' => 'Element',
'default' => 0,
'add' => '4.7',
'title' => 'Automatically update related contributions when Membership Type is changed',
'is_domain' => 1,
'is_contact' => 0,
'description' => 'Enabling this setting will update related contribution of membership(s) except if the membership is paid for with a recurring contribution.',
'help_text' => '',
),
'contact_view_options' => array(
'group_name' => 'CiviCRM Preferences',
'group' => 'core',
......
......@@ -150,4 +150,19 @@ return array(
'description' => NULL,
'help_text' => NULL,
),
'update_contribution_on_membership_type_change' => array(
'group_name' => 'Contribute Preferences',
'group' => 'contribute',
'name' => 'update_contribution_on_membership_type_change',
'type' => 'Integer',
'html_type' => 'checkbox',
'quick_form_type' => 'Element',
'default' => 0,
'add' => '4.7',
'title' => 'Automatically update related contributions when Membership Type is changed',
'is_domain' => 1,
'is_contact' => 0,
'description' => 'Enabling this setting will update related contribution of membership(s) except if the membership is paid for with a recurring contribution.',
'help_text' => NULL,
),
);
......@@ -176,7 +176,7 @@ class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
//Change selection to a lower amount.
$params['price_2'] = 50;
CRM_Event_BAO_Participant::changeFeeSelections($params, $participant['id'], $contribution->id, $feeBlock, $lineItem, $feeAmt, $priceSetId);
CRM_Price_BAO_LineItem::changeFeeSelections($params, $participant['id'], 'participant', $contribution->id, $feeBlock, $lineItem, $feeAmt);
//Record a refund of the remaining amount.
$submittedValues['total_amount'] = 50;
......
......@@ -227,21 +227,20 @@ class CRM_Event_BAO_CRM19273 extends CiviUnitTestCase {
}
public function testCRM19273() {
$PSparams['price_1'] = 2;
$lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
CRM_Event_BAO_Participant::changeFeeSelections($PSparams, $this->participantID, $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee, $this->_priceSetID);
CRM_Price_BAO_LineItem::changeFeeSelections($PSparams, $this->participantID, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
$this->balanceCheck($this->_cheapFee);
$PSparams['price_1'] = 1;
$lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
CRM_Event_BAO_Participant::changeFeeSelections($PSparams, $this->participantID, $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee, $this->_priceSetID);
CRM_Price_BAO_LineItem::changeFeeSelections($PSparams, $this->participantID, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
$this->balanceCheck($this->_expensiveFee);
$PSparams['price_1'] = 3;
$lineItem = CRM_Price_BAO_LineItem::getLineItems($this->participantID, 'participant');
CRM_Event_BAO_Participant::changeFeeSelections($PSparams, $this->participantID, $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee, $this->_priceSetID);
CRM_Price_BAO_LineItem::changeFeeSelections($PSparams, $this->participantID, 'participant', $this->_contributionId, $this->_feeBlock, $lineItem, $this->_expensiveFee);
$this->balanceCheck($this->_veryExpensive);
}
......
......@@ -110,6 +110,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
'name' => "AnnualFixed",
'member_of_contact_id' => 23,
'duration_unit' => "year",
'minimum_fee' => 50,
'duration_interval' => 1,
'period_type' => "fixed",
'fixed_period_start_day' => "101",
......@@ -512,6 +513,100 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
$this->mut->stop();
}
/**
* Test the submit function of the membership form on membership type change.
* Check if the related contribuion is also updated if the minimum_fee didn't match
*/
public function testContributionUpdateOnMembershipTypeChange() {
// Step 1: Create a Membership via backoffice whose with 50.00 payment
$form = $this->getForm();
$form->preProcess();
$this->mut = new CiviMailUtils($this, TRUE);
$this->createLoggedInUser();
$priceSet = $this->callAPISuccess('PriceSet', 'Get', array("extends" => "CiviMember"));
$form->set('priceSetId', $priceSet['id']);
CRM_Price_BAO_PriceSet::buildPriceSet($form);
$params = array(
'cid' => $this->_individualId,
'join_date' => date('m/d/Y', time()),
'start_date' => '',
'end_date' => '',
// This format reflects the 23 being the organisation & the 25 being the type.
'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
'record_contribution' => 1,
'total_amount' => 50,
'receive_date' => date('m/d/Y', time()),
'receive_date_time' => '08:36PM',