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