Skip to content

Commit

Permalink
Merge pull request #18964 from eileenmcnaughton/trans
Browse files Browse the repository at this point in the history
dev/core#2155 Remove obscure broken handling for onlinePendingContribution
  • Loading branch information
seamuslee001 authored Nov 16, 2020
2 parents e60f1f4 + f8cb4d1 commit 6097f8d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 136 deletions.
170 changes: 39 additions & 131 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {

protected $_memType = NULL;

protected $_onlinePendingContributionId;

public $_mode;

public $_contributeMode = 'direct';
Expand Down Expand Up @@ -276,16 +274,6 @@ public function preProcess() {

// Add custom data to form
CRM_Custom_Form_CustomData::addToForm($this, $this->_memType);

// CRM-4395, get the online pending contribution id.
$this->_onlinePendingContributionId = NULL;
if (!$this->_mode && $this->_id && ($this->_action & CRM_Core_Action::UPDATE)) {
$this->_onlinePendingContributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($this->_id,
'Membership'
);
}
$this->assign('onlinePendingContributionId', $this->_onlinePendingContributionId);

$this->setPageTitle(ts('Membership'));
}

Expand All @@ -308,20 +296,15 @@ public function setDefaultValues() {
$defaults['num_terms'] = 1;

if (!empty($defaults['id'])) {
if ($this->_onlinePendingContributionId) {
$defaults['record_contribution'] = $this->_onlinePendingContributionId;
}
else {
$contributionId = CRM_Core_DAO::singleValueQuery("
SELECT contribution_id
FROM civicrm_membership_payment
WHERE membership_id = $this->_id
ORDER BY contribution_id
DESC limit 1");

if ($contributionId) {
$defaults['record_contribution'] = $contributionId;
}
$contributionId = CRM_Core_DAO::singleValueQuery("
SELECT contribution_id
FROM civicrm_membership_payment
WHERE membership_id = $this->_id
ORDER BY contribution_id
DESC limit 1");

if ($contributionId) {
$defaults['record_contribution'] = $contributionId;
}
}
else {
Expand Down Expand Up @@ -861,15 +844,6 @@ public static function formRule($params, $files, $self) {
}
}

// validate contribution status for 'Failed'.
if ($self->_onlinePendingContributionId && !empty($params['record_contribution']) &&
(CRM_Utils_Array::value('contribution_status_id', $params) ==
array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'))
)
) {
$errors['contribution_status_id'] = ts('Please select a valid payment status before updating.');
}

return empty($errors) ? TRUE : $errors;
}

Expand Down Expand Up @@ -975,9 +949,6 @@ public static function emailReceipt(&$form, &$formValues, &$membership, $customV
if (!empty($formValues['contribution_id'])) {
$form->assign('contributionID', $formValues['contribution_id']);
}
elseif (isset($form->_onlinePendingContributionId)) {
$form->assign('contributionID', $form->_onlinePendingContributionId);
}

if (!empty($formValues['contribution_status_id'])) {
$form->assign('contributionStatusID', $formValues['contribution_status_id']);
Expand Down Expand Up @@ -1252,16 +1223,14 @@ public function submit() {
$params[$f] = $formValues[$f] ?? NULL;
}

if (!$this->_onlinePendingContributionId) {
if (empty($formValues['source'])) {
$params['contribution_source'] = ts('%1 Membership: Offline signup (by %2)', [
1 => $membershipType,
2 => $userName,
]);
}
else {
$params['contribution_source'] = $formValues['source'];
}
if (empty($formValues['source'])) {
$params['contribution_source'] = ts('%1 Membership: Offline signup (by %2)', [
1 => $membershipType,
2 => $userName,
]);
}
else {
$params['contribution_source'] = $formValues['source'];
}

$completedContributionStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
Expand Down Expand Up @@ -1496,94 +1465,33 @@ public function submit() {
}
else {
$params['action'] = $this->_action;
if ($this->_onlinePendingContributionId && !empty($formValues['record_contribution'])) {

// update membership as well as contribution object, CRM-4395
$params['contribution_id'] = $this->_onlinePendingContributionId;
$params['componentId'] = $params['id'];
$params['componentName'] = 'contribute';
// Only available statuses are Pending and completed so cancel or failed is not possible here.
$result = CRM_Contribute_BAO_Contribution::transitionComponents($params, TRUE);
if (!empty($result) && !empty($params['contribution_id'])) {
$lineItem = [];
$lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($params['contribution_id']);
$itemId = key($lineItems);
$priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id');

$lineItems[$itemId]['unit_price'] = $params['total_amount'];
$lineItems[$itemId]['line_total'] = $params['total_amount'];
$lineItems[$itemId]['id'] = $itemId;
$lineItem[$priceSetId] = $lineItems;
$contributionBAO = new CRM_Contribute_BAO_Contribution();
$contributionBAO->id = $params['contribution_id'];
$contributionBAO->contact_id = $params['contact_id'];
$contributionBAO->find();
CRM_Price_BAO_LineItem::processPriceSet($params['contribution_id'], $lineItem, $contributionBAO, 'civicrm_membership');

//create new soft-credit record, CRM-13981
if ($softParams) {
$softParams['contribution_id'] = $params['contribution_id'];
while ($contributionBAO->fetch()) {
$softParams['currency'] = $contributionBAO->currency;
$softParams['amount'] = $contributionBAO->total_amount;
}
CRM_Contribute_BAO_ContributionSoft::add($softParams);
}
$count = 0;
foreach ($this->_memTypeSelected as $memType) {
if ($count && !empty($formValues['record_contribution']) &&
($relateContribution = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id))
) {
$membershipTypeValues[$memType]['relate_contribution_id'] = $relateContribution;
}

//carry updated membership object.
$membership = new CRM_Member_DAO_Membership();
$membership->id = $this->_id;
$membership->find(TRUE);

$cancelled = TRUE;
if ($membership->end_date) {
//display end date w/ status message.
$endDate = $membership->end_date;

if (!in_array($membership->status_id, [
// CRM-15475
array_search('Cancelled', CRM_Member_PseudoConstant::membershipStatus(NULL, " name = 'Cancelled' ", 'name', FALSE, TRUE)),
array_search('Expired', CRM_Member_PseudoConstant::membershipStatus()),
])
) {
$cancelled = FALSE;
}
// @todo figure out why recieve_date isn't being set right here.
if (empty($params['receive_date'])) {
$params['receive_date'] = date('Y-m-d H:i:s');
}
// suppress form values in template.
$this->assign('cancelled', $cancelled);

$createdMemberships[] = $membership;
}
else {
$count = 0;
foreach ($this->_memTypeSelected as $memType) {
if ($count && !empty($formValues['record_contribution']) &&
($relateContribution = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id))
) {
$membershipTypeValues[$memType]['relate_contribution_id'] = $relateContribution;
}

// @todo figure out why recieve_date isn't being set right here.
if (empty($params['receive_date'])) {
$params['receive_date'] = date('Y-m-d H:i:s');
}
$membershipParams = array_merge($params, $membershipTypeValues[$memType]);
$membershipParams = array_merge($params, $membershipTypeValues[$memType]);

if (!empty($softParams)) {
$membershipParams['soft_credit'] = $softParams;
}
// @todo stop passing $ids (membership and userId only are set above)
$membership = CRM_Member_BAO_Membership::create($membershipParams, $ids);
$params['contribution'] = $membershipParams['contribution'] ?? NULL;
unset($params['lineItems']);
// skip line item creation for next interation since line item(s) are already created.
$params['skipLineItem'] = TRUE;

$this->_membershipIDs[] = $membership->id;
$createdMemberships[$memType] = $membership;
$count++;
if (!empty($softParams)) {
$membershipParams['soft_credit'] = $softParams;
}
// @todo stop passing $ids (membership and userId only are set above)
$membership = CRM_Member_BAO_Membership::create($membershipParams, $ids);
$params['contribution'] = $membershipParams['contribution'] ?? NULL;
unset($params['lineItems']);
// skip line item creation for next interation since line item(s) are already created.
$params['skipLineItem'] = TRUE;

$this->_membershipIDs[] = $membership->id;
$createdMemberships[$memType] = $membership;
$count++;
}
}
$isRecur = $params['is_recur'] ?? NULL;
Expand Down
8 changes: 4 additions & 4 deletions templates/CRM/Member/Form/Membership.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@
<span class="description">{ts}When <strong>Status Override</strong> is active, the selected status will remain in force (it will NOT be subject to membership status rules) until it is cancelled or become inactive.{/ts}</span></td></tr>
{/if}

{if $accessContribution and !$membershipMode AND ($action neq 2 or (!$rows.0.contribution_id AND !$softCredit) or $onlinePendingContributionId)}
{if $accessContribution and !$membershipMode AND ($action neq 2 or (!$rows.0.contribution_id AND !$softCredit))}
<tr id="contri">
<td class="label">{if $onlinePendingContributionId}{ts}Update Payment Status{/ts}{else}{$form.record_contribution.label}{/if}</td>
<td class="label">{$form.record_contribution.label}</td>
<td>{$form.record_contribution.html}<br />
<span class="description">{ts}Check this box to enter or update payment information. You will also be able to generate a customized receipt.{/ts}</span></td>
</tr>
Expand Down Expand Up @@ -251,7 +251,7 @@
</div> <!-- end form-block -->

{if $action neq 8} {* Jscript additions not need for Delete action *}
{if $accessContribution and !$membershipMode AND ($action neq 2 or !$rows.0.contribution_id or $onlinePendingContributionId)}
{if $accessContribution and !$membershipMode AND ($action neq 2 or !$rows.0.contribution_id)}

{include file="CRM/common/showHideByFieldValue.tpl"
trigger_field_id ="record_contribution"
Expand All @@ -268,7 +268,7 @@
function setPaymentBlock(mode, checkboxEvent) {
var memType = parseInt(cj('#membership_type_id_1').val( ));
var isPriceSet = 0;
var existingAmount = {/literal}{if !empty($onlinePendingContributionId)}1{else}0{/if}{literal};
var existingAmount = 0;
if ( cj('#price_set_id').length > 0 && cj('#price_set_id').val() ) {
isPriceSet = 1;
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Member/Form/MembershipCommon.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{if !$membershipMode}
{if $accessContribution && ($action != 2 or (!$rows.0.contribution_id AND !$softCredit) or $onlinePendingContributionId)}
{if $accessContribution && ($action != 2 or (!$rows.0.contribution_id AND !$softCredit))}
<table>
<tr class="crm-{$formClass}-form-block-contribution-contact">
<td class="label">{$form.is_different_contribution_contact.label}</td>
Expand Down

0 comments on commit 6097f8d

Please sign in to comment.