Contribution WorkflowTrait updates to support event template changes
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 23 Jun 2023 23:41:27 +0000 (16:41 -0700)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 23 Jun 2023 23:41:27 +0000 (16:41 -0700)
CRM/Contribute/WorkflowMessage/ContributionTrait.php

index 2951c7480344831a68abd7894fd74c399f3e833d..433ef71d672129e1f0971777f65817edde3b921a 100644 (file)
@@ -4,7 +4,7 @@ use Civi\Api4\Membership;
 
 /**
  * @method array getContribution()
- * @method ?int getContributionID()
+ * @method int|null getContributionID()
  * @method $this setContributionID(?int $contributionId)
  */
 trait CRM_Contribute_WorkflowMessage_ContributionTrait {
@@ -70,6 +70,11 @@ trait CRM_Contribute_WorkflowMessage_ContributionTrait {
     if (!$this->order && $this->contributionID) {
       $this->order = new CRM_Financial_BAO_Order();
       $this->order->setTemplateContributionID($this->contributionID);
+      if (!empty($this->eventID)) {
+        // Temporary support for tests that are making a mess of this.
+        // It should always be possible to get this from the line items.
+        $this->order->setPriceSetIDByEventPageID($this->eventID);
+      }
     }
     return $this->order;
   }
@@ -147,11 +152,20 @@ trait CRM_Contribute_WorkflowMessage_ContributionTrait {
     }
     $this->taxRateBreakdown = [];
     foreach ($this->getLineItems() as $lineItem) {
-      $this->taxRateBreakdown[$lineItem['tax_rate']] = [
-        'amount' => $lineItem['tax_amount'] ?? 0,
-        'rate' => $lineItem['tax_rate'],
-        'percentage' => sprintf('%.2f', $lineItem['tax_rate']),
-      ];
+      if (!isset($this->taxRateBreakdown[$lineItem['tax_rate']])) {
+        $this->taxRateBreakdown[$lineItem['tax_rate']] = [
+          'amount' => 0,
+          'rate' => $lineItem['tax_rate'],
+          'percentage' => sprintf('%.2f', $lineItem['tax_rate']),
+        ];
+      }
+      $this->taxRateBreakdown[$lineItem['tax_rate']]['amount'] += $lineItem['tax_amount'] ?? 0;
+    }
+    // Remove the rates with no value.
+    foreach ($this->taxRateBreakdown as $rate => $details) {
+      if ($details['amount'] === 0.0) {
+        unset($this->taxRateBreakdown[$rate]);
+      }
     }
     if (array_keys($this->taxRateBreakdown) === [0]) {
       // If the only tax rate charged is 0% then no tax breakdown is returned.