Skip to content
Snippets Groups Projects
Commit f6ef9827 authored by mattwire's avatar mattwire
Browse files

Support cancelling memberships when issuing refunds

parent 0888c2c2
No related branches found
No related tags found
No related merge requests found
<?php
use Civi\Api4\LineItem;
use Civi\Api4\Membership;
use Civi\Api4\Participant;
use Civi\Api4\PaymentProcessor;
use Civi\Payment\Exception\PaymentProcessorException;
......@@ -84,6 +85,7 @@ class CRM_Mjwshared_Form_PaymentRefund extends CRM_Core_Form {
case 'civicrm_membership':
$membershipIDs[] = $lineItemDetails['entity_id'];
break;
}
}
if (!empty($participantIDs)) {
......@@ -103,6 +105,23 @@ class CRM_Mjwshared_Form_PaymentRefund extends CRM_Core_Form {
}
$this->assign('participants', $participantsForAssign ?? NULL);
if (!empty($membershipIDs)) {
$membershipsForAssign = [];
$this->set('membership_ids', $membershipIDs);
$memberships = Membership::get(FALSE)
->addSelect('*', 'membership_type_id:label', 'status_id:label', 'contact_id.display_name')
->addWhere('id', 'IN', $membershipIDs)
->execute();
foreach ($memberships->getArrayCopy() as $membership) {
$membership['status'] = $membership['status_id:label'];
$membership['type'] = $membership['membership_type_id:label'];
$membership['display_name'] = $membership['contact_id.display_name'];
$membershipsForAssign[] = $membership;
}
$this->addYesNo('cancel_memberships', E::ts('Do you want to cancel these memberships when you refund the payment?'), NULL, TRUE);
}
$this->assign('memberships', $membershipsForAssign ?? NULL);
$this->addMoney('refund_amount',
ts('Refund Amount'),
TRUE,
......@@ -173,6 +192,8 @@ class CRM_Mjwshared_Form_PaymentRefund extends CRM_Core_Form {
$paymentID = $this->get('payment_id');
$participantIDs = $this->get('participant_ids');
$cancelParticipants = $formValues['cancel_participants'] ?? FALSE;
$membershipIDs = $this->get('membership_ids');
$cancelMemberships = $formValues['cancel_memberships'] ?? FALSE;
try {
$payment = reset(civicrm_api3('Mjwpayment', 'get_payment', ['id' => $paymentID])['values']);
......@@ -230,6 +251,15 @@ class CRM_Mjwshared_Form_PaymentRefund extends CRM_Core_Form {
}
$message .= ' ' . E::ts('Cancelled %1 participant registration(s).', [1 => count($participantIDs)]);
}
if ($cancelMemberships && !empty($membershipIDs)) {
Membership::update(FALSE)
->addValue('status_id.name', 'Cancelled')
->addWhere('id', 'IN', $membershipIDs)
->execute();
$message .= ' ' . E::ts('Cancelled %1 membership(s).', [1 => count($membershipIDs)]);
}
CRM_Core_Session::setStatus($message, 'Refund processed', 'success');
}
else {
......
......@@ -3,38 +3,56 @@
<div class="crm-section crm-mjwshared-paymentrefund-paymentinfo">
<div class="label">{ts}Amount{/ts}</div><div class="content">{$paymentInfo.total_amount|crmMoney:$paymentInfo.currency}</div>
<div class="label">{ts}Payment date{/ts}</div><div class="content">{$paymentInfo.trxn_date|crmDate}</div>
{if $paymentInfo.trxn_id}<div class="label">{ts}Transaction ID{/ts}</div><div class="content">{$paymentInfo.trxn_id}</div>{/if}
{if $paymentInfo.order_reference}<div class="label">{ts}Order Reference{/ts}</div><div class="content">{$paymentInfo.order_reference}</div>{/if}
{if $paymentInfo.payment_processor_title}<div class="label">{ts}Payment Processor{/ts}</div><div class="content">{$paymentInfo.payment_processor_title}</div>{/if}
{if $paymentInfo.trxn_id}<div class="label">{ts}Transaction ID{/ts}</div><div class="content">{$paymentInfo.trxn_id}</div>{/if}
{if $paymentInfo.order_reference}<div class="label">{ts}Order Reference{/ts}</div><div class="content">{$paymentInfo.order_reference}</div>{/if}
{if $paymentInfo.payment_processor_title}<div class="label">{ts}Payment Processor{/ts}</div><div class="content">{$paymentInfo.payment_processor_title}</div>{/if}
</div>
{if $participants}
<h3>{ts}This payment was used to register the following participants:{/ts}</h3>
<div class="crm-section crm-mjwshared-paymentrefund-participants">
<br />
<ul>
{foreach from=$participants item=participant}
<li>{$participant.display_name}: {$participant.event_title} (<em>{$participant.status}</em>)</li>
{/foreach}
</ul>
<ul>
{foreach from=$participants item=participant}
<li>{$participant.display_name}: {$participant.event_title} (<em>{$participant.status}</em>)</li>
{/foreach}
</ul>
</div>
<br />
<div class="crm-section crm-mjwshared-paymentrefund-canceloption">
<div class="label">{$form.cancel_participants.label}</div>
<div class="content">{$form.cancel_participants.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section crm-mjwshared-paymentrefund-participant-canceloption">
<div class="label">{$form.cancel_participants.label}</div>
<div class="content">{$form.cancel_participants.html}</div>
<div class="clear"></div>
</div>
{/if}
<div class="crm-section crm-mjwshared-paymentrefund-canceloption">
<div class="label">{$form.refund_amount.label}</div>
<div class="content">
<span id='totalAmount'>{$form.currency.html|crmAddClass:eight}&nbsp;{$form.refund_amount.html|crmAddClass:eight}</span>
{if $memberships}
<h3>{ts}This payment was used for the following memberships:{/ts}</h3>
<div class="crm-section crm-mjwshared-paymentrefund-memberships">
<br />
<ul>
{foreach from=$memberships item=membership}
<li>{$membership.display_name}: {$membership.type} (<em>{$membership.status}</em>)</li>
{/foreach}
</ul>
</div>
<div class="clear"></div>
<br />
<div class="crm-section crm-mjwshared-paymentrefund-membership-canceloption">
<div class="label">{$form.cancel_memberships.label}</div>
<div class="content">{$form.cancel_memberships.html}</div>
<div class="clear"></div>
</div>
{/if}
<div class="crm-section crm-mjwshared-paymentrefund-refundamount">
<div class="label">{$form.refund_amount.label}</div>
<div class="content">
<span id='totalAmount'>{$form.currency.html|crmAddClass:eight}&nbsp;{$form.refund_amount.html|crmAddClass:eight}</span>
</div>
<div class="clear"></div>
</div>
<div class="help">{ts}Click "refund" to refund this payment{/ts}</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
{/crmScope}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment