Skip to content

Commit

Permalink
Merge pull request #22698 from eileenmcnaughton/matt
Browse files Browse the repository at this point in the history
Cleanup on contribution view
  • Loading branch information
mattwire authored Feb 18, 2022
2 parents c8a7531 + 9fa586c commit d6ea2c5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 103 deletions.
27 changes: 15 additions & 12 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -3646,7 +3646,7 @@ public static function getPaymentInfo($id, $component = 'contribution', $getTrxn
$info['transaction'] = self::getContributionTransactionInformation($contributionId, $contribution['financial_type_id']);
}

$info['payment_links'] = self::getContributionPaymentLinks($id, $paymentBalance, $info['contribution_status']);
$info['payment_links'] = self::getContributionPaymentLinks($id, $info['contribution_status']);
return $info;
}

Expand Down Expand Up @@ -4287,15 +4287,16 @@ public static function updateMembershipBasedOnCompletionOfContribution($contribu
* then a refund link.
*
* @param int $id
* @param float $balance
* @param string $contributionStatus
*
* @return array
* $actionLinks Links array containing:
* -url
* -title
*
* @internal - not supported for use outside of core.
*/
protected static function getContributionPaymentLinks($id, $balance, $contributionStatus) {
public static function getContributionPaymentLinks(int $id, string $contributionStatus): array {
if ($contributionStatus === 'Failed' || !CRM_Core_Permission::check('edit contributions')) {
// In general the balance is the best way to determine if a payment can be added or not,
// but not for Failed contributions, where we don't accept additional payments at the moment.
Expand Down Expand Up @@ -4326,15 +4327,17 @@ protected static function getContributionPaymentLinks($id, $balance, $contributi
'title' => ts('Submit Credit Card payment'),
];
}
$actionLinks[] = [
'url' => CRM_Utils_System::url('civicrm/payment', [
'action' => 'add',
'reset' => 1,
'id' => $id,
'is_refund' => 1,
]),
'title' => ts('Record Refund'),
];
if ($contributionStatus !== 'Pending') {
$actionLinks[] = [
'url' => CRM_Utils_System::url('civicrm/payment', [
'action' => 'add',
'reset' => 1,
'id' => $id,
'is_refund' => 1,
]),
'title' => ts('Record Refund'),
];
}
return $actionLinks;
}

Expand Down
110 changes: 84 additions & 26 deletions CRM/Contribute/Form/ContributionView.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class CRM_Contribute_Form_ContributionView extends CRM_Core_Form {

/**
* Set variables up before form is built.
*
* @throws \CRM_Core_Exception
* @throws \API_Exception
*/
public function preProcess() {
$id = $this->getID();
Expand Down Expand Up @@ -55,22 +58,6 @@ public function preProcess() {
}
$this->assign('is_template', $values['is_template']);

if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && $this->_action & CRM_Core_Action::VIEW) {
$financialTypeID = CRM_Contribute_PseudoConstant::financialType($values['financial_type_id']);
CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($id, 'view');
if (CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($id, 'edit', FALSE)) {
$this->assign('canEdit', TRUE);
}
if (CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($id, 'delete', FALSE)) {
$this->assign('canDelete', TRUE);
}
if (!CRM_Core_Permission::check('view contributions of type ' . $financialTypeID)) {
CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
}
}
elseif ($this->_action & CRM_Core_Action::VIEW) {
$this->assign('noACL', TRUE);
}
CRM_Contribute_BAO_Contribution::resolveDefaults($values);

if (!empty($values['contribution_page_id'])) {
Expand Down Expand Up @@ -215,20 +202,74 @@ public function preProcess() {
);
$statusOptionValueNames = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$contributionStatus = $statusOptionValueNames[$values['contribution_status_id']];
if (in_array($contributionStatus, ['Partially paid', 'Pending refund'])
|| ($contributionStatus == 'Pending' && $values['is_pay_later'])
) {
if ($contributionStatus == 'Pending refund') {
$this->assign('paymentButtonName', ts('Record Refund'));
$this->assign('addRecordPayment', in_array($contributionStatus, ['Partially paid', 'Pending refund', 'Pending']));
$this->assignPaymentInfoBlock($id);

$searchKey = NULL;
if ($this->controller->_key) {
$searchKey = $this->controller->_key;
}

if ($this->isHasAccess('update')) {
$urlParams = "reset=1&id={$id}&cid={$values['contact_id']}&action=update&context={$context}";
if (($context === 'fulltext' || $context === 'search') && $searchKey) {
$urlParams = "reset=1&id={$id}&cid={$values['contact_id']}&action=update&context={$context}&key={$searchKey}";
}
if (!$contribution['is_template']) {
foreach (CRM_Contribute_BAO_Contribution::getContributionPaymentLinks($this->getID(), $contributionStatus) as $paymentButton) {
$paymentButton['icon'] = 'fa-plus-circle';
$linkButtons[] = $paymentButton;
}
}
$linkButtons[] = [
'title' => ts('Edit'),
'url' => 'civicrm/contact/view/contribution',
'qs' => $urlParams,
'icon' => 'fa-pencil',
'accesskey' => 'e',
];
}

if ($this->isHasAccess('delete')) {
$urlParams = "reset=1&id={$id}&cid={$values['contact_id']}&action=delete&context={$context}";
if (($context === 'fulltext' || $context === 'search') && $searchKey) {
$urlParams = "reset=1&id={$id}&cid={$values['contact_id']}&action=delete&context={$context}&key={$searchKey}";
}
$linkButtons[] = [
'title' => ts('Delete'),
'url' => 'civicrm/contact/view/contribution',
'qs' => $urlParams,
'icon' => 'fa-trash',
];
}

$pdfUrlParams = "reset=1&id={$id}&cid={$values['contact_id']}";
$emailUrlParams = "reset=1&id={$id}&cid={$values['contact_id']}&select=email";
if (Civi::settings()->get('invoicing') && !$contribution['is_template']) {
if (($values['contribution_status'] !== 'Refunded') && ($values['contribution_status'] !== 'Cancelled')) {
$invoiceButtonText = ts('Download Invoice');
}
else {
$this->assign('paymentButtonName', ts('Record Payment'));
$invoiceButtonText = ts('Download Invoice and Credit Note');
}
$this->assign('addRecordPayment', TRUE);
$this->assign('contactId', $values['contact_id']);
$this->assign('componentId', $id);
$this->assign('component', 'contribution');
$linkButtons[] = [
'title' => $invoiceButtonText,
'url' => 'civicrm/contribute/invoice',
'qs' => $pdfUrlParams,
'icon' => 'fa-download',
];
$linkButtons[] = [
'title' => ts('Email Invoice'),
'url' => 'civicrm/contribute/invoice/email',
'qs' => $emailUrlParams,
'icon' => 'fa-paper-plane',
];
}
$this->assign('linkButtons', $linkButtons ?? []);
// These next 3 parameters are used to construct a url in PaymentInfo.tpl
$this->assign('contactId', $values['contact_id']);
$this->assign('componentId', $id);
$this->assign('component', 'contribution');
$this->assignPaymentInfoBlock($id);
}

Expand Down Expand Up @@ -269,6 +310,23 @@ protected function assignPaymentInfoBlock($id) {
return $title;
}

/**
* @param string $action
*
* @return bool
*/
private function isHasAccess(string $action): bool {
try {
return Contribution::checkAccess()
->setAction($action)
->addValue('id', $this->getID())
->execute()->first()['access'];
}
catch (API_Exception $e) {
return FALSE;
}
}

/**
* Get the contribution ID.
*
Expand Down
70 changes: 5 additions & 65 deletions templates/CRM/Contribute/Form/ContributionView.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,6 @@
<div class="crm-block crm-content-block crm-contribution-view-form-block">
<div class="action-link">
<div class="crm-submit-buttons">
{if (call_user_func(array('CRM_Core_Permission','check'), 'edit contributions') && call_user_func(array('CRM_Core_Permission', 'check'), "edit contributions of type $financial_type") && !empty($canEdit)) ||
(call_user_func(array('CRM_Core_Permission','check'), 'edit contributions') && $noACL)}
{assign var='urlParams' value="reset=1&id=$id&cid=$contact_id&action=update&context=$context"}
{if ( $context eq 'fulltext' || $context eq 'search' ) && $searchKey}
{assign var='urlParams' value="reset=1&id=$id&cid=$contact_id&action=update&context=$context&key=$searchKey"}
{/if}
<a class="button" href="{crmURL p='civicrm/contact/view/contribution' q=$urlParams}" accesskey="e"><span>
<i class="crm-i fa-pencil" aria-hidden="true"></i> {ts}Edit{/ts}</span>
</a>
{if !empty($paymentButtonName)}
<a class="button" href='{crmURL p="civicrm/payment" q="action=add&reset=1&component=`$component`&id=`$id`&cid=`$contact_id`"}'><i class="crm-i fa-plus-circle" aria-hidden="true"></i> {ts}{$paymentButtonName}{/ts}</a>
{/if}
{/if}
{if (call_user_func(array('CRM_Core_Permission','check'), 'delete in CiviContribute') && call_user_func(array('CRM_Core_Permission', 'check'), "delete contributions of type $financial_type") && !empty($canDelete)) || (call_user_func(array('CRM_Core_Permission','check'), 'delete in CiviContribute') && $noACL)}
{assign var='urlParams' value="reset=1&id=$id&cid=$contact_id&action=delete&context=$context"}
{if ( $context eq 'fulltext' || $context eq 'search' ) && $searchKey}
{assign var='urlParams' value="reset=1&id=$id&cid=$contact_id&action=delete&context=$context&key=$searchKey"}
{/if}
<a class="button" href="{crmURL p='civicrm/contact/view/contribution' q=$urlParams}"><span>
<i class="crm-i fa-trash" aria-hidden="true"></i> {ts}Delete{/ts}</span>
</a>
{/if}
{assign var='pdfUrlParams' value="reset=1&id=$id&cid=$contact_id"}
{assign var='emailUrlParams' value="reset=1&id=$id&cid=$contact_id&select=email"}
{if $invoicing && empty($is_template)}
<div class="css_right">
<a class="button no-popup" href="{crmURL p='civicrm/contribute/invoice' q=$pdfUrlParams}">
<i class="crm-i fa-download" aria-hidden="true"></i>
{if $contribution_status != 'Refunded' && $contribution_status != 'Cancelled' }
{ts}Download Invoice{/ts}</a>
{else}
{ts}Download Invoice and Credit Note{/ts}</a>
{/if}
<a class="button" href="{crmURL p='civicrm/contribute/invoice/email' q=$emailUrlParams}">
<i class="crm-i fa-paper-plane" aria-hidden="true"></i>
{ts}Email Invoice{/ts}</a>
</div>
{/if}
{include file="CRM/common/formButtons.tpl" location="top"}
</div>
</div>
Expand Down Expand Up @@ -216,18 +178,16 @@
<td>{$thankyou_date|crmDate}</td>
</tr>
{/if}
<tr>
<td class='label'>{ts}Payment Summary{/ts}</td>
<td id='payment-info'></td>
</tr>
{if empty($is_template)}
<tr>
<td class="label">{ts}Payment Details{/ts}</td>
<td>{include file="CRM/Contribute/Form/PaymentInfoBlock.tpl"}</td>
</tr>
{/if}
{if $addRecordPayment}
<tr>
<td class='label'>{ts}Payment Summary{/ts}</td>
<td id='payment-info'></td>
</tr>
{/if}
</table>

{if $softContributions && count($softContributions)} {* We show soft credit name with PCP section if contribution is linked to a PCP. *}
Expand Down Expand Up @@ -323,29 +283,9 @@
</div>
</fieldset>
{/if}
{if $addRecordPayment}
{include file="CRM/Contribute/Page/PaymentInfo.tpl" show='payments'}
{/if}
{include file="CRM/Contribute/Page/PaymentInfo.tpl" show='payments'}

<div class="crm-submit-buttons">
{if (call_user_func(array('CRM_Core_Permission','check'), 'edit contributions') && call_user_func(array('CRM_Core_Permission', 'check'), "edit contributions of type $financial_type") && $canEdit) ||
(call_user_func(array('CRM_Core_Permission','check'), 'edit contributions') && $noACL)}
{assign var='urlParams' value="reset=1&id=$id&cid=$contact_id&action=update&context=$context"}
{if ( $context eq 'fulltext' || $context eq 'search' ) && $searchKey}
{assign var='urlParams' value="reset=1&id=$id&cid=$contact_id&action=update&context=$context&key=$searchKey"}
{/if}
<a class="button" href="{crmURL p='civicrm/contact/view/contribution' q=$urlParams}" accesskey="e"><span><i class="crm-i fa-pencil" aria-hidden="true"></i> {ts}Edit{/ts}</span></a>
{if $paymentButtonName}
<a class="button" href='{crmURL p="civicrm/payment" q="action=add&reset=1&component=`$component`&id=`$id`&cid=`$contact_id`"}'><i class="crm-i fa-plus-circle" aria-hidden="true"></i> {ts}{$paymentButtonName}{/ts}</a>
{/if}
{/if}
{if (call_user_func(array('CRM_Core_Permission','check'), 'delete in CiviContribute') && call_user_func(array('CRM_Core_Permission', 'check'), "delete contributions of type $financial_type") && $canDelete) || (call_user_func(array('CRM_Core_Permission','check'), 'delete in CiviContribute') && $noACL)}
{assign var='urlParams' value="reset=1&id=$id&cid=$contact_id&action=delete&context=$context"}
{if ( $context eq 'fulltext' || $context eq 'search' ) && $searchKey}
{assign var='urlParams' value="reset=1&id=$id&cid=$contact_id&action=delete&context=$context&key=$searchKey"}
{/if}
<a class="button" href="{crmURL p='civicrm/contact/view/contribution' q=$urlParams}"><span><i class="crm-i fa-trash" aria-hidden="true"></i> {ts}Delete{/ts}</span></a>
{/if}
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
</div>
Expand Down

0 comments on commit d6ea2c5

Please sign in to comment.