Merge pull request #24117 from civicrm/5.52
[civicrm-core.git] / CRM / Contribute / WorkflowMessage / ContributionTrait.php
1 <?php
2
3 /**
4 * @method array getContribution()
5 * @method ?int getContributionID()
6 * @method $this setContributionID(?int $contributionId)
7 */
8 trait CRM_Contribute_WorkflowMessage_ContributionTrait {
9 /**
10 * The contribution.
11 *
12 * @var array|null
13 *
14 * @scope tokenContext as contribution
15 */
16 public $contribution;
17
18 /**
19 * @var int
20 * @scope tokenContext as contributionId
21 */
22 public $contributionId;
23
24 /**
25 * Is the site configured such that tax should be displayed.
26 *
27 * @var bool
28 */
29 public $isShowTax;
30
31 /**
32 * Line items associated with the contribution.
33 *
34 * @var array
35 *
36 * @scope tplParams
37 */
38 public $lineItems;
39
40 /**
41 * Tax rates paid.
42 *
43 * Generally this would look like
44 *
45 * ['10.00%' => 100]
46 *
47 * To indicate that $100 was changed for 10% tax.
48 *
49 * @var array
50 *
51 * @scope tplParams
52 */
53 public $taxRateBreakdown;
54
55 /**
56 * @var CRM_Financial_BAO_Order
57 */
58 private $order;
59
60 /**
61 * Get order, if available.
62 *
63 * The order is used within the class to calculate line items etc.
64 *
65 * @return \CRM_Financial_BAO_Order|null
66 */
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);
71 }
72 return $this->order;
73 }
74
75 /**
76 * Should line items be displayed for the contribution.
77 *
78 * This determination is based on whether the price set is quick config.
79 *
80 * @var bool
81 *
82 * @scope tplParams
83 */
84 public $isShowLineItems;
85
86 /**
87 * Get bool for whether a line item breakdown be displayed.
88 *
89 * @return bool
90 */
91 public function getIsShowLineItems(): bool {
92 if (isset($this->isShowLineItems)) {
93 return $this->isShowLineItems;
94 }
95
96 $order = $this->getOrder();
97 if (!$order) {
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.
101 return FALSE;
102 }
103 return !$this->order->getPriceSetMetadata()['is_quick_config'];
104 }
105
106 /**
107 * Get the line items.
108 *
109 * @return array
110 */
111 public function getLineItems(): array {
112 if (isset($this->lineItems)) {
113 return $this->lineItems;
114 }
115 $order = $this->getOrder();
116 if (!$order) {
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.
120 return [];
121 }
122 return $order->getLineItems();
123 }
124
125 /**
126 * Get the line items.
127 *
128 * @return array
129 */
130 public function getTaxRateBreakdown(): array {
131 if (isset($this->taxRateBreakdown)) {
132 return $this->taxRateBreakdown;
133 }
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']),
140 ];
141 }
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 = [];
145 }
146 return $this->taxRateBreakdown;
147 }
148
149 /**
150 * Set contribution object.
151 *
152 * @param array $contribution
153 *
154 * @return $this
155 */
156 public function setContribution(array $contribution): self {
157 $this->contribution = $contribution;
158 if (!empty($contribution['id'])) {
159 $this->contributionId = $contribution['id'];
160 }
161 return $this;
162 }
163
164 /**
165 * Set order object.
166 *
167 * Note this is only supported for core use (specifically in example work flow)
168 * as the contract might change.
169 *
170 * @param CRM_Financial_BAO_Order $order
171 *
172 * @return $this
173 */
174 public function setOrder(CRM_Financial_BAO_Order $order): self {
175 $this->order = $order;
176 return $this;
177 }
178
179 /**
180 * Extra variables to be exported to smarty based on being calculated.
181 *
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.
185 *
186 * @param array $export
187 */
188 protected function exportExtraTplParams(array &$export): void {
189 $export['isShowTax'] = (bool) Civi::settings()->get('invoicing');
190 }
191
192 }