From 691345e96b762fbb5d4cb89eaebdedb626c21dd4 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 15 Jun 2022 15:44:52 +1200 Subject: [PATCH] Fix breakage in message preview on workflow contributions --- .../Contribution/BasicContribution.ex.php | 4 +++ .../WorkflowMessage/ContributionTrait.php | 25 ++++++++++++++++--- CRM/Financial/BAO/Order.php | 7 ++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CRM/Contribute/WorkflowMessage/Contribution/BasicContribution.ex.php b/CRM/Contribute/WorkflowMessage/Contribution/BasicContribution.ex.php index 23c035710532..954385ac82e7 100644 --- a/CRM/Contribute/WorkflowMessage/Contribution/BasicContribution.ex.php +++ b/CRM/Contribute/WorkflowMessage/Contribution/BasicContribution.ex.php @@ -54,6 +54,10 @@ public function build(array &$example): void { private function addExampleData(GenericWorkflowMessage $messageTemplate): void { $messageTemplate->setContact(\Civi\Test::example('entity/Contact/Barb')); $messageTemplate->setContribution(\Civi\Test::example('entity/Contribution/Euro5990/completed')); + $mockOrder = new CRM_Financial_BAO_Order(); + $mockOrder->setTemplateContributionID(50); + $mockOrder->setPriceSetToDefault('contribution'); + $messageTemplate->setOrder($mockOrder); } } diff --git a/CRM/Contribute/WorkflowMessage/ContributionTrait.php b/CRM/Contribute/WorkflowMessage/ContributionTrait.php index 4ecf20565bb2..2692957a035c 100644 --- a/CRM/Contribute/WorkflowMessage/ContributionTrait.php +++ b/CRM/Contribute/WorkflowMessage/ContributionTrait.php @@ -17,7 +17,7 @@ trait CRM_Contribute_WorkflowMessage_ContributionTrait { /** * @var int - * @scope tokenContext as contribution_id + * @scope tokenContext as contributionId */ public $contributionId; @@ -41,7 +41,7 @@ trait CRM_Contribute_WorkflowMessage_ContributionTrait { * @return \CRM_Financial_BAO_Order|null */ private function getOrder(): ?CRM_Financial_BAO_Order { - if ($this->contributionId) { + if (!$this->order && $this->contributionId) { $this->order = new CRM_Financial_BAO_Order(); $this->order->setTemplateContributionID($this->contributionId); } @@ -65,6 +65,10 @@ private function getOrder(): ?CRM_Financial_BAO_Order { * @return bool */ public function getIsShowLineItems(): bool { + if (isset($this->isShowLineItems)) { + return $this->isShowLineItems; + } + $order = $this->getOrder(); if (!$order) { // This would only be the case transitionally. @@ -72,7 +76,7 @@ public function getIsShowLineItems(): bool { // always have the contribution ID available as well as migrated ones. return FALSE; } - return $this->order->getPriceSetMetadata()['is_quick_config']; + return !$this->order->getPriceSetMetadata()['is_quick_config']; } /** @@ -90,6 +94,21 @@ public function setContribution(array $contribution): self { return $this; } + /** + * Set order object. + * + * Note this is only supported for core use (specifically in example work flow) + * as the contract might change. + * + * @param CRM_Financial_BAO_Order $order + * + * @return $this + */ + public function setOrder(CRM_Financial_BAO_Order $order): self { + $this->order = $order; + return $this; + } + /** * Extra variables to be exported to smarty based on being calculated. * diff --git a/CRM/Financial/BAO/Order.php b/CRM/Financial/BAO/Order.php index 7374dce22293..66ac91b5f1b6 100644 --- a/CRM/Financial/BAO/Order.php +++ b/CRM/Financial/BAO/Order.php @@ -451,6 +451,12 @@ public function getPriceSetID(): int { foreach ($this->getPriceOptions() as $fieldID => $valueID) { $this->setPriceSetIDFromSelectedField($fieldID); } + if (!$this->priceSetID && $this->getTemplateContributionID()) { + // Load the line items from the contribution. + foreach ($this->getLineItems() as $lineItem) { + return $lineItem['price_field_id.price_set_id']; + } + } } return $this->priceSetID; } @@ -1099,6 +1105,7 @@ protected function getLinesForContribution(): array { 'entity_id', 'entity_table', 'price_field_id', + 'price_field_id.price_set_id', 'price_field_value_id', 'financial_type_id', 'label',