Update copyright date for 2020
[civicrm-core.git] / CRM / Financial / Form / FrontEndPaymentFormTrait.php
CommitLineData
97dc7b64 1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
97dc7b64 7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 *
30 * @package CRM
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
97dc7b64 32 */
33
34/**
35 * This class holds functionality shared between various front end forms.
36 */
37trait CRM_Financial_Form_FrontEndPaymentFormTrait {
38
bf7ae813 39 /**
40 * The label for the pay later pseudoprocessor option.
41 *
42 * @var string
43 */
44 protected $payLaterLabel;
45
46 /**
47 * @return string
48 */
49 public function getPayLaterLabel(): string {
50 if ($this->payLaterLabel) {
51 return $this->payLaterLabel;
52 }
53 return $this->get('payLaterLabel') ?? '';
54 }
55
56 /**
57 * @param string $payLaterLabel
58 */
59 public function setPayLaterLabel(string $payLaterLabel) {
60 $this->set('payLaterLabel', $payLaterLabel);
61 $this->payLaterLabel = $payLaterLabel;
62 }
63
97dc7b64 64 /**
65 * Alter line items for template.
66 *
67 * This is an early cut of what will ideally eventually be a hooklike call to the
68 * CRM_Invoicing_Utils class with a potential end goal of moving this handling to an extension.
69 *
70 * @param $tplLineItems
97dc7b64 71 */
f1b56c48 72 protected function alterLineItemsForTemplate(&$tplLineItems) {
73 if (!CRM_Invoicing_Utils::isInvoicingEnabled()) {
74 return;
75 }
76 // @todo this should really be the first time we are determining
77 // the tax rates - we can calculate them from the financial_type_id
78 // & amount here so we didn't need a deeper function to semi-get
79 // them but not be able to 'format them right' because they are
80 // potentially being used for 'something else'.
81 // @todo invoicing code - please feel the hate. Also move this 'hook-like-bit'
82 // to the CRM_Invoicing_Utils class.
97dc7b64 83 foreach ($tplLineItems as $key => $value) {
84 foreach ($value as $k => $v) {
97288cdc 85 if (isset($v['tax_rate']) && $v['tax_rate'] != '') {
f1b56c48 86 // These only need assigning once, but code is more readable with them here
87 $this->assign('getTaxDetails', TRUE);
88 $this->assign('taxTerm', CRM_Invoicing_Utils::getTaxTerm());
97288cdc 89 // Cast to float to display without trailing zero decimals
90 $tplLineItems[$key][$k]['tax_rate'] = (float) $v['tax_rate'];
97dc7b64 91 }
92 }
93 }
f1b56c48 94 }
95
96 /**
97 * Assign line items to the template.
98 *
99 * @param $tplLineItems
100 */
101 protected function assignLineItemsToTemplate($tplLineItems) {
102 // @todo this should be a hook that invoicing code hooks into rather than a call to it.
103 $this->alterLineItemsForTemplate($tplLineItems);
104 $this->assign('lineItem', $tplLineItems);
97dc7b64 105 }
106
bf7ae813 107 /**
108 * Get the configured processors, including the pay later processor.
109 *
110 * @return array
111 */
112 protected function getProcessors(): array {
113 $pps = [];
114 if (!empty($this->_paymentProcessors)) {
115 foreach ($this->_paymentProcessors as $key => $processor) {
116 $pps[$key] = $processor['title'] ?? $processor['name'];
117 }
118 }
119 if ($this->getPayLaterLabel()) {
120 $pps[0] = $this->getPayLaterLabel();
121 }
122 return $pps;
123 }
124
97dc7b64 125}