Merge pull request #22183 from eileenmcnaughton/smarty9
[civicrm-core.git] / CRM / Contribute / Form / ContributionView.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
95cdcc0f 19 * This class generates form components for Payment-Instrument.
6a488035
TO
20 */
21class CRM_Contribute_Form_ContributionView extends CRM_Core_Form {
22
23 /**
fe482240 24 * Set variables up before form is built.
6a488035
TO
25 */
26 public function preProcess() {
353ffa53 27 $id = $this->get('id');
b2095b9c
MW
28 if (empty($id)) {
29 throw new CRM_Core_Exception('Contribution ID is required');
30 }
be2fb01f 31 $params = ['id' => $id];
edc80cda 32 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035
TO
33 $this->assign('context', $context);
34
99cdd94d 35 $values = CRM_Contribute_BAO_Contribution::getValuesWithMappings($params);
36
7d238e35
JJ
37 $force_create_template = CRM_Utils_Request::retrieve('force_create_template', 'Boolean', $this, FALSE, FALSE);
38 if ($force_create_template && !empty($values['contribution_recur_id']) && empty($values['is_template'])) {
39 // Create a template contribution.
40 $templateContributionId = CRM_Contribute_BAO_ContributionRecur::ensureTemplateContributionExists($values['contribution_recur_id']);
41 if (!empty($templateContributionId)) {
42 $id = $templateContributionId;
43 $params = ['id' => $id];
44 $values = CRM_Contribute_BAO_Contribution::getValuesWithMappings($params);
45 }
46 }
47 $this->assign('is_template', $values['is_template']);
48
66af7c48 49 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && $this->_action & CRM_Core_Action::VIEW) {
c0f5a838 50 $financialTypeID = CRM_Contribute_PseudoConstant::financialType($values['financial_type_id']);
59ccc8cd
E
51 CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($id, 'view');
52 if (CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($id, 'edit', FALSE)) {
53 $this->assign('canEdit', TRUE);
54 }
55 if (CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($id, 'delete', FALSE)) {
56 $this->assign('canDelete', TRUE);
57 }
c0f5a838 58 if (!CRM_Core_Permission::check('view contributions of type ' . $financialTypeID)) {
e22ec653 59 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
c0f5a838
E
60 }
61 }
895d596d
E
62 elseif ($this->_action & CRM_Core_Action::VIEW) {
63 $this->assign('noACL', TRUE);
64 }
6a488035
TO
65 CRM_Contribute_BAO_Contribution::resolveDefaults($values);
66
a7488080 67 if (!empty($values['contribution_page_id'])) {
6a488035
TO
68 $contribPages = CRM_Contribute_PseudoConstant::contributionPage(NULL, TRUE);
69 $values['contribution_page_title'] = CRM_Utils_Array::value(CRM_Utils_Array::value('contribution_page_id', $values), $contribPages);
70 }
71
b44e3f84 72 // get received into i.e to_financial_account_id from last trxn
6a488035
TO
73 $financialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($values['contribution_id'], 'DESC');
74 $values['to_financial_account'] = '';
a7488080 75 if (!empty($financialTrxnId['financialTrxnId'])) {
eea16664 76 $values['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $financialTrxnId['financialTrxnId'], 'to_financial_account_id');
6a488035 77 if ($values['to_financial_account_id']) {
eea16664 78 $values['to_financial_account'] = CRM_Contribute_PseudoConstant::financialAccount($values['to_financial_account_id']);
6a488035 79 }
2cc6b631
DG
80 $values['payment_processor_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $financialTrxnId['financialTrxnId'], 'payment_processor_id');
81 if ($values['payment_processor_id']) {
82 $values['payment_processor_name'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', $values['payment_processor_id'], 'name');
83 }
6a488035 84 }
eea16664 85
a7488080 86 if (!empty($values['contribution_recur_id'])) {
353ffa53 87 $sql = "SELECT installments, frequency_interval, frequency_unit FROM civicrm_contribution_recur WHERE id = %1";
be2fb01f 88 $params = [1 => [$values['contribution_recur_id'], 'Integer']];
353ffa53 89 $dao = CRM_Core_DAO::executeQuery($sql, $params);
6a488035
TO
90 if ($dao->fetch()) {
91 $values['recur_installments'] = $dao->installments;
92 $values['recur_frequency_unit'] = $dao->frequency_unit;
93 $values['recur_frequency_interval'] = $dao->frequency_interval;
94 }
95 }
96
317103ab
CW
97 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Contribution', NULL, $id, 0, $values['financial_type_id'] ?? NULL,
98 NULL, TRUE, NULL, FALSE, CRM_Core_Permission::VIEW);
e34e6979 99 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $id);
6a488035
TO
100
101 $premiumId = NULL;
b2095b9c
MW
102 $dao = new CRM_Contribute_DAO_ContributionProduct();
103 $dao->contribution_id = $id;
104 if ($dao->find(TRUE)) {
105 $premiumId = $dao->id;
106 $productID = $dao->product_id;
6a488035
TO
107 }
108
109 if ($premiumId) {
110 $productDAO = new CRM_Contribute_DAO_Product();
111 $productDAO->id = $productID;
112 $productDAO->find(TRUE);
113
114 $this->assign('premium', $productDAO->name);
115 $this->assign('option', $dao->product_option);
116 $this->assign('fulfilled', $dao->fulfilled_date);
117 }
118
119 // Get Note
120 $noteValue = CRM_Core_BAO_Note::getNote(CRM_Utils_Array::value('id', $values), 'civicrm_contribution');
121 $values['note'] = array_values($noteValue);
122
123 // show billing address location details, if exists
a7488080 124 if (!empty($values['address_id'])) {
2cfe2cf4 125 $addressParams = ['id' => $values['address_id']];
6a488035
TO
126 $addressDetails = CRM_Core_BAO_Address::getValues($addressParams, FALSE, 'id');
127 $addressDetails = array_values($addressDetails);
128 $values['billing_address'] = $addressDetails[0]['display'];
129 }
130
b6545333 131 //assign soft credit record if exists.
132 $SCRecords = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($values['contribution_id'], TRUE);
00a1afe2 133 if (!empty($SCRecords['soft_credit'])) {
134 $this->assign('softContributions', $SCRecords['soft_credit']);
135 unset($SCRecords['soft_credit']);
136 }
b6545333 137
138 //assign pcp record if exists
139 foreach ($SCRecords as $name => $value) {
140 $this->assign($name, $value);
141 }
6a488035 142
b2095b9c 143 $lineItems = [CRM_Price_BAO_LineItem::getLineItemsByContributionID(($id))];
28b46c26 144 $this->assign('lineItem', $lineItems);
6a488035 145 $values['totalAmount'] = $values['total_amount'];
64525dae 146 $this->assign('displayLineItemFinancialType', TRUE);
6a488035
TO
147
148 //do check for campaigns
149 if ($campaignId = CRM_Utils_Array::value('campaign_id', $values)) {
150 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId);
151 $values['campaign'] = $campaigns[$campaignId];
152 }
99cdd94d 153 if ($values['contribution_status'] == 'Refunded') {
15ce8a6b 154 $this->assign('refund_trxn_id', CRM_Core_BAO_FinancialTrxn::getRefundTransactionTrxnID($id));
99cdd94d 155 }
6a488035
TO
156
157 // assign values to the template
b2095b9c 158 $this->assignVariables($values, array_keys($values));
d0df87f2 159 $invoicing = CRM_Invoicing_Utils::isInvoicingEnabled();
03b412ae 160 $this->assign('invoicing', $invoicing);
fc7106d2 161 $this->assign('isDeferred', Civi::settings()->get('deferred_revenue_enabled'));
03b412ae 162 if ($invoicing && isset($values['tax_amount'])) {
a32709be
PB
163 $this->assign('totalTaxAmount', $values['tax_amount']);
164 }
6a488035 165
7d238e35 166 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
6a488035
TO
167 $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']);
168 $this->assign('displayName', $displayName);
6a488035
TO
169 // Check if this is default domain contact CRM-10482
170 if (CRM_Contact_BAO_Contact::checkDomainContact($values['contact_id'])) {
171 $displayName .= ' (' . ts('default organization') . ')';
172 }
173
7d238e35 174 if (empty($values['is_template'])) {
7e2e2551 175 $this->setTitle(ts('View Contribution from') . ' ' . $displayName);
7d238e35
JJ
176 }
177 else {
7e2e2551 178 $this->setTitle(ts('View Template Contribution from') . ' ' . $displayName);
7d238e35 179 }
6a488035
TO
180
181 // add viewed contribution to recent items list
182 $url = CRM_Utils_System::url('civicrm/contact/view/contribution',
183 "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
184 );
185
230ea74e 186 $title = $displayName . ' - (' . CRM_Utils_Money::format($values['total_amount'], $values['currency']) . ' ' . ' - ' . $values['financial_type'] . ')';
6a488035 187
be2fb01f 188 $recentOther = [];
6a488035
TO
189 if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::UPDATE)) {
190 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution',
191 "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
192 );
193 }
194 if (CRM_Core_Permission::checkActionPermission('CiviContribute', CRM_Core_Action::DELETE)) {
195 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/contribution',
196 "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
197 );
198 }
199 CRM_Utils_Recent::add($title,
200 $url,
201 $values['id'],
202 'Contribution',
203 $values['contact_id'],
204 NULL,
205 $recentOther
206 );
231d9162 207 $statusOptionValueNames = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
208 $contributionStatus = $statusOptionValueNames[$values['contribution_status_id']];
be2fb01f 209 if (in_array($contributionStatus, ['Partially paid', 'Pending refund'])
685dc433
PN
210 || ($contributionStatus == 'Pending' && $values['is_pay_later'])
211 ) {
212 if ($contributionStatus == 'Pending refund') {
213 $this->assign('paymentButtonName', ts('Record Refund'));
214 }
215 else {
216 $this->assign('paymentButtonName', ts('Record Payment'));
217 }
218 $this->assign('addRecordPayment', TRUE);
219 $this->assign('contactId', $values['contact_id']);
220 $this->assign('componentId', $id);
221 $this->assign('component', 'contribution');
222 }
02fc97b3 223 $this->assignPaymentInfoBlock($id);
6a488035
TO
224 }
225
226 /**
fe482240 227 * Build the form object.
6a488035
TO
228 */
229 public function buildQuickForm() {
be2fb01f
CW
230 $this->addButtons([
231 [
c6554998
AH
232 'type' => 'cancel',
233 'name' => ts('Done'),
234 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
235 'isDefault' => TRUE,
be2fb01f
CW
236 ],
237 ]);
6a488035 238 }
96025800 239
02fc97b3 240 /**
241 * Assign the values to build the payment info block.
242 *
243 * @todo - this is a bit too much copy & paste from AbstractEditPayment
244 * (justifying on the basis it's 'pretty short' and in a different inheritance
245 * tree. I feel like traits are probably the longer term answer).
246 *
247 * @param int $id
248 *
1330f57a 249 * @return string
02fc97b3 250 * Block title.
251 */
252 protected function assignPaymentInfoBlock($id) {
253 // component is used in getPaymentInfo primarily to retrieve the contribution id, we
254 // already have that.
255 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($id, 'contribution', TRUE);
256 $title = ts('View Payment');
257 $this->assign('transaction', TRUE);
258 $this->assign('payments', $paymentInfo['transaction']);
259 $this->assign('paymentLinks', $paymentInfo['payment_links']);
260 return $title;
261 }
262
6a488035 263}