Merge pull request #18779 from jaapjansma/dev_report_53
[civicrm-core.git] / CRM / Financial / Form / FrontEndPaymentFormTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class holds functionality shared between various front end forms.
20 */
21 trait CRM_Financial_Form_FrontEndPaymentFormTrait {
22
23 /**
24 * Is pay later enabled on this form?
25 *
26 * @var bool
27 */
28 protected $isPayLater = FALSE;
29
30 /**
31 * The label for the pay later pseudoprocessor option.
32 *
33 * @var string
34 */
35 protected $payLaterLabel;
36
37 /**
38 * Is this a back office form
39 *
40 * @var bool
41 */
42 public $isBackOffice = FALSE;
43
44 /**
45 * The payment mode that we are in ("live" or "test")
46 * This should be protected and retrieved via getPaymentMode() but it's accessed all over the place so we have to leave it public for now.
47 *
48 * @var string
49 */
50 public $_mode;
51
52 /**
53 * @return bool
54 */
55 public function isPayLater() {
56 return $this->isPayLater;
57 }
58
59 /**
60 * @param bool $isPayLater
61 */
62 public function setIsPayLater($isPayLater) {
63 $this->isPayLater = $isPayLater;
64 }
65
66 /**
67 * @return bool
68 */
69 public function getIsBackOffice() {
70 return $this->isBackOffice;
71 }
72
73 /**
74 * Get the payment mode ('live' or 'test')
75 *
76 * @return string
77 */
78 public function getPaymentMode() {
79 return $this->_mode;
80 }
81
82 /**
83 * Set the payment mode ('live' or 'test')
84 */
85 public function setPaymentMode() {
86 $this->_mode = ($this->_action === CRM_Core_Action::PREVIEW) ? 'test' : 'live';
87 }
88
89 /**
90 * @return string
91 */
92 public function getPayLaterLabel(): string {
93 if ($this->payLaterLabel) {
94 return $this->payLaterLabel;
95 }
96 return $this->get('payLaterLabel') ?? '';
97 }
98
99 /**
100 * @param string $payLaterLabel
101 */
102 public function setPayLaterLabel(string $payLaterLabel) {
103 $this->set('payLaterLabel', $payLaterLabel);
104 $this->payLaterLabel = $payLaterLabel;
105 }
106
107 /**
108 * Alter line items for template.
109 *
110 * This is an early cut of what will ideally eventually be a hooklike call to the
111 * CRM_Invoicing_Utils class with a potential end goal of moving this handling to an extension.
112 *
113 * @param $tplLineItems
114 */
115 protected function alterLineItemsForTemplate(&$tplLineItems) {
116 if (!CRM_Invoicing_Utils::isInvoicingEnabled()) {
117 return;
118 }
119 // @todo this should really be the first time we are determining
120 // the tax rates - we can calculate them from the financial_type_id
121 // & amount here so we didn't need a deeper function to semi-get
122 // them but not be able to 'format them right' because they are
123 // potentially being used for 'something else'.
124 // @todo invoicing code - please feel the hate. Also move this 'hook-like-bit'
125 // to the CRM_Invoicing_Utils class.
126 foreach ($tplLineItems as $key => $value) {
127 foreach ($value as $k => $v) {
128 if (isset($v['tax_rate']) && $v['tax_rate'] != '') {
129 // These only need assigning once, but code is more readable with them here
130 $this->assign('getTaxDetails', TRUE);
131 $this->assign('taxTerm', CRM_Invoicing_Utils::getTaxTerm());
132 // Cast to float to display without trailing zero decimals
133 $tplLineItems[$key][$k]['tax_rate'] = (float) $v['tax_rate'];
134 }
135 }
136 }
137 }
138
139 /**
140 * Assign line items to the template.
141 *
142 * @param $tplLineItems
143 */
144 protected function assignLineItemsToTemplate($tplLineItems) {
145 // @todo this should be a hook that invoicing code hooks into rather than a call to it.
146 $this->alterLineItemsForTemplate($tplLineItems);
147 $this->assign('lineItem', $tplLineItems);
148 }
149
150 /**
151 * Get the configured processors, including the pay later processor.
152 *
153 * @return array
154 */
155 protected function getProcessors(): array {
156 $pps = [];
157 if (!empty($this->_paymentProcessors)) {
158 foreach ($this->_paymentProcessors as $key => $processor) {
159 $pps[$key] = $this->getPaymentProcessorTitle($processor);
160 }
161 }
162 if ($this->getPayLaterLabel()) {
163 $pps[0] = $this->getPayLaterLabel();
164 }
165 return $pps;
166 }
167
168 /**
169 * Get the title of the payment processor to display to the user
170 * Note: There is an identical function in CRM_Core_Payment
171 *
172 * @param array $processor
173 *
174 * @return string
175 */
176 protected function getPaymentProcessorTitle($processor) {
177 return $processor['title'] ?? $processor['name'];
178 }
179
180 /**
181 * Adds in either a set of radio buttons or hidden fields to contain the payment processors on a front end form
182 */
183 protected function addPaymentProcessorFieldsToForm() {
184 $paymentProcessors = $this->getProcessors();
185 $optAttributes = [];
186 foreach ($paymentProcessors as $ppKey => $ppval) {
187 if ($ppKey > 0) {
188 $optAttributes[$ppKey]['class'] = 'payment_processor_' . strtolower($this->_paymentProcessors[$ppKey]['payment_processor_type']);
189 }
190 else {
191 $optAttributes[$ppKey]['class'] = 'payment_processor_paylater';
192 }
193 }
194 if (count($paymentProcessors) > 1) {
195 $this->addRadio('payment_processor_id', ts('Payment Method'), $paymentProcessors,
196 NULL, "&nbsp;", FALSE, $optAttributes
197 );
198 }
199 elseif (!empty($paymentProcessors)) {
200 $ppKeys = array_keys($paymentProcessors);
201 $currentPP = array_pop($ppKeys);
202 $this->addElement('hidden', 'payment_processor_id', $currentPP);
203 }
204 }
205
206 }