4 * @method array getContribution()
5 * @method ?int getContributionID()
6 * @method $this setContributionID(?int $contributionId)
8 trait CRM_Contribute_WorkflowMessage_ContributionTrait
{
14 * @scope tokenContext as contribution
20 * @scope tokenContext as contributionId
22 public $contributionId;
25 * Is the site configured such that tax should be displayed.
32 * Line items associated with the contribution.
43 * Generally this would look like
47 * To indicate that $100 was changed for 10% tax.
53 public $taxRateBreakdown;
56 * @var CRM_Financial_BAO_Order
61 * Get order, if available.
63 * The order is used within the class to calculate line items etc.
65 * @return \CRM_Financial_BAO_Order|null
67 private function getOrder(): ?CRM_Financial_BAO_Order
{
68 if (!$this->order
&& $this->contributionId
) {
69 $this->order
= new CRM_Financial_BAO_Order();
70 $this->order
->setTemplateContributionID($this->contributionId
);
76 * Should line items be displayed for the contribution.
78 * This determination is based on whether the price set is quick config.
84 public $isShowLineItems;
87 * Get bool for whether a line item breakdown be displayed.
91 public function getIsShowLineItems(): bool {
92 if (isset($this->isShowLineItems
)) {
93 return $this->isShowLineItems
;
96 $order = $this->getOrder();
98 // This would only be the case transitionally.
99 // Since this is a trait it is used by templates which don't (yet)
100 // always have the contribution ID available as well as migrated ones.
103 return !$this->order
->getPriceSetMetadata()['is_quick_config'];
107 * Get the line items.
111 public function getLineItems(): array {
112 if (isset($this->lineItems
)) {
113 return $this->lineItems
;
115 $order = $this->getOrder();
117 // This would only be the case transitionally.
118 // Since this is a trait it is used by templates which don't (yet)
119 // always have the contribution ID available as well as migrated ones.
122 return $order->getLineItems();
126 * Get the line items.
130 public function getTaxRateBreakdown(): array {
131 if (isset($this->taxRateBreakdown
)) {
132 return $this->taxRateBreakdown
;
134 $this->taxRateBreakdown
= [];
135 foreach ($this->getLineItems() as $lineItem) {
136 $this->taxRateBreakdown
[$lineItem['tax_rate']] = [
137 'amount' => $lineItem['tax_amount'] ??
0,
138 'rate' => $lineItem['tax_rate'],
139 'percentage' => sprintf('%.2f', $lineItem['tax_rate']),
142 if (array_keys($this->taxRateBreakdown
) === [0]) {
143 // If the only tax rate charged is 0% then no tax breakdown is returned.
144 $this->taxRateBreakdown
= [];
146 return $this->taxRateBreakdown
;
150 * Set contribution object.
152 * @param array $contribution
156 public function setContribution(array $contribution): self
{
157 $this->contribution
= $contribution;
158 if (!empty($contribution['id'])) {
159 $this->contributionId
= $contribution['id'];
167 * Note this is only supported for core use (specifically in example work flow)
168 * as the contract might change.
170 * @param CRM_Financial_BAO_Order $order
174 public function setOrder(CRM_Financial_BAO_Order
$order): self
{
175 $this->order
= $order;
180 * Extra variables to be exported to smarty based on being calculated.
182 * We export isShowTax to denote whether invoicing is enabled but
183 * hopefully at some point we will separate the assumption that invoicing
184 * and tax are a package.
186 * @param array $export
188 protected function exportExtraTplParams(array &$export): void
{
189 $export['isShowTax'] = (bool) Civi
::settings()->get('invoicing');