Skip to content

Commit

Permalink
Update online_event_template to use workflow message
Browse files Browse the repository at this point in the history
This updates the default online event template to use the variables assigned by
the workflow template

This works to standardise the variables previously in dataArray
, lineItem, totalAmount, taxAmount to reflect the
approach in other templates per
https://docs.civicrm.org/user/en/latest/email/message-templates/#variables-and-tokens-in-workflow-message-templates

However, for participants, per the helpful discussion at
civicrm#24576

the expectation is that the primaryParticipant gets the values for
all participants whereas the others only get line items,
tax breakdowns, totals that relate to them.

Hence I have assigned an array participants that holds detials for all participants
and the template iterates through them showing all or just the
one that relates to the assigned participant id depending on
whether it is primary.

This allows the template to display in the Message preview and should
also mean that those values that I have addressed will always reflect
the participantID being used. This also addresses some notices
and incompatibility with secure smarty.

However, there are still values I haven't made sense of, or otherwise left
out of the scope of this PR in the template.

Also note I updated the taxBreakdown in the contribution trait - I decided it
was cloberring the amount as it iterated through the line items - rather than
doing a running total.
  • Loading branch information
eileenmcnaughton committed Jun 24, 2023
1 parent beb8871 commit 198b3cc
Show file tree
Hide file tree
Showing 6 changed files with 5,317 additions and 5,372 deletions.
41 changes: 38 additions & 3 deletions CRM/Event/WorkflowMessage/ParticipantTrait.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

use Civi\Api4\LineItem;
use Civi\Api4\Participant;

/**
* Trait for participant workflow classes.
*
* @method int getParticipantID()
* @method int getEventID()
*/
trait CRM_Event_WorkflowMessage_ParticipantTrait {

Expand Down Expand Up @@ -83,6 +87,34 @@ public function setEventID(int $eventID): self {
return $this;
}

/**
* @throws \Civi\API\Exception\UnauthorizedException
* @throws \CRM_Core_Exception
*/
public function setParticipantID(int $participantID) {
$this->participantID = $participantID;
if (!$this->getContributionID()) {
$lineItem = LineItem::get(FALSE)
->addWhere('entity_table', '=', 'civicrm_participant')
->addWhere('id', '=', $participantID)
->addSelect('contribution_id')
->execute()->first();
if (!empty($lineItem)) {
$this->setContributionID($lineItem['contribution_id']);
}
// It might be bad data on the site - let's do a noisy fall back to participant payment
// (the relationship between contribution & participant should be in the line item but
// some integrations might mess this up - if they are not using the order api).
$participantPayment = civicrm_api3('ParticipantPayment', 'get', ['participant_id' => $participantID])['values'];
if (!empty($participantPayment)) {
$participantPayment = reset($participantPayment);
$this->setContributionID((int) $participantPayment['contribution_id']);
CRM_Core_Session::setStatus('There might be a data problem, contribution id could not be loaded from the line item');
}
}
return $this;
}

/**
* Is the participant the primary participant.
*
Expand Down Expand Up @@ -110,7 +142,7 @@ public function getPrimaryParticipantID(): int {
public function setParticipant(array $participant): self {
$this->participant = $participant;
if (!empty($participant['id'])) {
$this->participantID = $participant['id'];
$this->setParticipantID($participant['id']);
}
if (!empty($participant['event_id'])) {
$this->eventID = $participant['event_id'];
Expand Down Expand Up @@ -149,7 +181,7 @@ public function getParticipants(): array {
return [];
}
// Initiate with the current participant to ensure they are first.
$participants = [$this->participantID => ['id' => $this->participantID]];
$participants = [$this->participantID => ['id' => $this->participantID, 'tax_rate_breakdown' => []]];
foreach ($this->getLineItems() as $lineItem) {
if ($lineItem['entity_table'] === 'civicrm_participant') {
$participantID = $lineItem['entity_id'];
Expand All @@ -167,6 +199,9 @@ public function getParticipants(): array {
$participants[$participantID]['totals']['total_amount_exclusive'] += $lineItem['line_total'];
$participants[$participantID]['totals']['tax_amount'] += $lineItem['tax_amount'];
$participants[$participantID]['totals']['total_amount_inclusive'] += ($lineItem['line_total'] + $lineItem['tax_amount']);
if (!isset($participants[$participantID]['tax_rate_breakdown'])) {
$participants[$participantID]['tax_rate_breakdown'] = [];
}
if (!isset($participants[$participantID]['tax_rate_breakdown'][$lineItem['tax_rate']])) {
$participants[$participantID]['tax_rate_breakdown'][$lineItem['tax_rate']] = [
'amount' => 0,
Expand All @@ -182,7 +217,7 @@ public function getParticipants(): array {
$participant['id'] = $participantID;
$participant['index'] = $count;
$participant['contact'] = $this->getParticipantContact($participantID);
foreach ($participant['tax_rate_breakdown'] as $rate => $details) {
foreach ($participant['tax_rate_breakdown'] ?? [] as $rate => $details) {
if ($details['amount'] === 0.0) {
unset($participant['tax_rate_breakdown'][$rate]);
}
Expand Down
Loading

0 comments on commit 198b3cc

Please sign in to comment.