Merge pull request #17431 from civicrm/5.26
[civicrm-core.git] / CRM / Financial / Form / FrontEndPaymentFormTrait.php
CommitLineData
97dc7b64 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
97dc7b64 5 | |
bc77d7c0
TO
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 |
97dc7b64 9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
97dc7b64 16 */
17
18/**
19 * This class holds functionality shared between various front end forms.
20 */
21trait CRM_Financial_Form_FrontEndPaymentFormTrait {
22
bf7ae813 23 /**
24 * The label for the pay later pseudoprocessor option.
25 *
26 * @var string
27 */
28 protected $payLaterLabel;
29
30 /**
31 * @return string
32 */
33 public function getPayLaterLabel(): string {
34 if ($this->payLaterLabel) {
35 return $this->payLaterLabel;
36 }
37 return $this->get('payLaterLabel') ?? '';
38 }
39
40 /**
41 * @param string $payLaterLabel
42 */
43 public function setPayLaterLabel(string $payLaterLabel) {
44 $this->set('payLaterLabel', $payLaterLabel);
45 $this->payLaterLabel = $payLaterLabel;
46 }
47
97dc7b64 48 /**
49 * Alter line items for template.
50 *
51 * This is an early cut of what will ideally eventually be a hooklike call to the
52 * CRM_Invoicing_Utils class with a potential end goal of moving this handling to an extension.
53 *
54 * @param $tplLineItems
97dc7b64 55 */
f1b56c48 56 protected function alterLineItemsForTemplate(&$tplLineItems) {
57 if (!CRM_Invoicing_Utils::isInvoicingEnabled()) {
58 return;
59 }
60 // @todo this should really be the first time we are determining
61 // the tax rates - we can calculate them from the financial_type_id
62 // & amount here so we didn't need a deeper function to semi-get
63 // them but not be able to 'format them right' because they are
64 // potentially being used for 'something else'.
65 // @todo invoicing code - please feel the hate. Also move this 'hook-like-bit'
66 // to the CRM_Invoicing_Utils class.
97dc7b64 67 foreach ($tplLineItems as $key => $value) {
68 foreach ($value as $k => $v) {
97288cdc 69 if (isset($v['tax_rate']) && $v['tax_rate'] != '') {
f1b56c48 70 // These only need assigning once, but code is more readable with them here
71 $this->assign('getTaxDetails', TRUE);
72 $this->assign('taxTerm', CRM_Invoicing_Utils::getTaxTerm());
97288cdc 73 // Cast to float to display without trailing zero decimals
74 $tplLineItems[$key][$k]['tax_rate'] = (float) $v['tax_rate'];
97dc7b64 75 }
76 }
77 }
f1b56c48 78 }
79
80 /**
81 * Assign line items to the template.
82 *
83 * @param $tplLineItems
84 */
85 protected function assignLineItemsToTemplate($tplLineItems) {
86 // @todo this should be a hook that invoicing code hooks into rather than a call to it.
87 $this->alterLineItemsForTemplate($tplLineItems);
88 $this->assign('lineItem', $tplLineItems);
97dc7b64 89 }
90
bf7ae813 91 /**
92 * Get the configured processors, including the pay later processor.
93 *
94 * @return array
95 */
96 protected function getProcessors(): array {
97 $pps = [];
98 if (!empty($this->_paymentProcessors)) {
99 foreach ($this->_paymentProcessors as $key => $processor) {
4e0ebeee 100 $pps[$key] = $this->getPaymentProcessorTitle($processor);
bf7ae813 101 }
102 }
103 if ($this->getPayLaterLabel()) {
104 $pps[0] = $this->getPayLaterLabel();
105 }
106 return $pps;
107 }
108
4e0ebeee
MW
109 /**
110 * Get the title of the payment processor to display to the user
111 * Note: There is an identical function in CRM_Core_Payment
112 *
113 * @param array $processor
114 *
115 * @return string
116 */
117 protected function getPaymentProcessorTitle($processor) {
118 return $processor['title'] ?? $processor['name'];
119 }
120
1421b010
SL
121 /**
122 * Adds in either a set of radio buttons or hidden fields to contain the payment processors on a front end form
123 */
124 protected function addPaymentProcessorFieldsToForm() {
125 $paymentProcessors = $this->getProcessors();
126 $optAttributes = [];
2aacd606 127 foreach ($paymentProcessors as $ppKey => $ppval) {
1421b010
SL
128 if ($ppKey > 0) {
129 $optAttributes[$ppKey]['class'] = 'payment_processor_' . strtolower($this->_paymentProcessors[$ppKey]['payment_processor_type']);
130 }
131 else {
132 $optAttributes[$ppKey]['class'] = 'payment_processor_paylater';
133 }
134 }
135 if (count($paymentProcessors) > 1) {
136 $this->addRadio('payment_processor_id', ts('Payment Method'), $paymentProcessors,
137 NULL, "&nbsp;", FALSE, $optAttributes
138 );
139 }
677fbbb0 140 elseif (!empty($paymentProcessors)) {
1421b010
SL
141 $ppKeys = array_keys($paymentProcessors);
142 $currentPP = array_pop($ppKeys);
143 $this->addElement('hidden', 'payment_processor_id', $currentPP);
144 }
145 }
146
97dc7b64 147}