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