Merge pull request #22487 from mattwire/repeattransactiontemplatecontribution
[civicrm-core.git] / ext / contributioncancelactions / contributioncancelactions.php
CommitLineData
c00edb21 1<?php
2
3require_once 'contributioncancelactions.civix.php';
4// phpcs:disable
5use CRM_Contributioncancelactions_ExtensionUtil as E;
6// phpcs:enable
6ff69e9c 7use Civi\Api4\LineItem;
a201e208 8use Civi\Api4\Participant;
c00edb21 9
10/**
11 * Implements hook_civicrm_preProcess().
12 *
6ff69e9c 13 * This enacts the following
14 * - find and cancel any related pending memberships
61470a1a 15 * - (not yet implemented) find and cancel any related pending participant
16 * records
17 * - (not yet implemented) find any related pledge payment records. Remove the
18 * contribution id.
6ff69e9c 19 *
c00edb21 20 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_post
61470a1a 21 *
22 * @throws \CiviCRM_API3_Exception
23 * @throws \API_Exception
c00edb21 24 */
25function contributioncancelactions_civicrm_post($op, $objectName, $objectId, $objectRef) {
26 if ($op === 'edit' && $objectName === 'Contribution') {
61470a1a 27 if (in_array(CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $objectRef->contribution_status_id),
28 ['Cancelled', 'Failed']
29 )) {
a201e208 30 contributioncancelactions_cancel_related_pending_memberships((int) $objectId);
31 contributioncancelactions_cancel_related_pending_participant_records((int) $objectId);
c00edb21 32 }
33 }
34}
a201e208 35
36/**
37 * Find and cancel any pending participant records.
38 *
39 * @param int $contributionID
40 * @throws CiviCRM_API3_Exception
41 */
42function contributioncancelactions_cancel_related_pending_participant_records($contributionID): void {
43 $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
44 $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
45 $cancellableParticipantRecords = civicrm_api3('ParticipantPayment', 'get', [
46 'contribution_id' => $contributionID,
47 'participant_id.status_id' => ['IN' => array_merge(array_keys($pendingStatuses), array_keys($waitingStatuses))],
48 ])['values'];
49 if (empty($cancellableParticipantRecords)) {
50 return;
51 }
52 Participant::update(FALSE)
53 ->addWhere('id', 'IN', array_keys($cancellableParticipantRecords))
54 ->setValues(['status_id:name' => 'Cancelled'])
55 ->execute();
56}
57
58/**
59 * Find and cancel any pending memberships.
60 *
61 * @param int $contributionID
62 * @throws API_Exception
63 * @throws CiviCRM_API3_Exception
64 */
65function contributioncancelactions_cancel_related_pending_memberships($contributionID): void {
66 $connectedMemberships = (array) LineItem::get(FALSE)->setWhere([
67 ['contribution_id', '=', $contributionID],
68 ['entity_table', '=', 'civicrm_membership'],
69 ])->execute()->indexBy('entity_id');
70
71 if (empty($connectedMemberships)) {
72 return;
73 }
74 // @todo we don't have v4 membership api yet so v3 for now.
75 $connectedMemberships = array_keys(civicrm_api3('Membership', 'get', [
76 'status_id' => 'Pending',
77 'id' => ['IN' => array_keys($connectedMemberships)],
78 ])['values']);
79 if (empty($connectedMemberships)) {
80 return;
81 }
82 foreach ($connectedMemberships as $membershipID) {
311218aa 83 civicrm_api3('Membership', 'create', ['status_id' => 'Cancelled', 'id' => $membershipID, 'is_override' => 1, 'status_override_end_date' => 'null']);
a201e208 84 }
85}