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