Merge pull request #15815 from artfulrobot/issue-1108-fix-unsubscribe
[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 // @todo - I believe this cancelledStatus is unused - if someone reaches the same conclusion
52 // by grepping then the next few lines can go.
53 $cancelledStatus = TRUE;
54 $status = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
55 if (CRM_Utils_Array::value('contribution_status_id', $values) == array_search('Cancelled', $status)) {
56 $cancelledStatus = FALSE;
57 }
58 $this->assign('cancelledStatus', $cancelledStatus);
59
60 if (!empty($values['contribution_page_id'])) {
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
65 // get received into i.e to_financial_account_id from last trxn
66 $financialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($values['contribution_id'], 'DESC');
67 $values['to_financial_account'] = '';
68 if (!empty($financialTrxnId['financialTrxnId'])) {
69 $values['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $financialTrxnId['financialTrxnId'], 'to_financial_account_id');
70 if ($values['to_financial_account_id']) {
71 $values['to_financial_account'] = CRM_Contribute_PseudoConstant::financialAccount($values['to_financial_account_id']);
72 }
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 }
77 }
78
79 if (!empty($values['contribution_recur_id'])) {
80 $sql = "SELECT installments, frequency_interval, frequency_unit FROM civicrm_contribution_recur WHERE id = %1";
81 $params = [1 => [$values['contribution_recur_id'], 'Integer']];
82 $dao = CRM_Core_DAO::executeQuery($sql, $params);
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
90 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Contribution', NULL, $id, 0, CRM_Utils_Array::value('financial_type_id', $values));
91 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $id);
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
118 if (!empty($values['address_id'])) {
119 $addressParams = ['id' => CRM_Utils_Array::value('address_id', $values)];
120 $addressDetails = CRM_Core_BAO_Address::getValues($addressParams, FALSE, 'id');
121 $addressDetails = array_values($addressDetails);
122 $values['billing_address'] = $addressDetails[0]['display'];
123 }
124
125 //assign soft credit record if exists.
126 $SCRecords = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($values['contribution_id'], TRUE);
127 if (!empty($SCRecords['soft_credit'])) {
128 $this->assign('softContributions', $SCRecords['soft_credit']);
129 unset($SCRecords['soft_credit']);
130 }
131
132 //assign pcp record if exists
133 foreach ($SCRecords as $name => $value) {
134 $this->assign($name, $value);
135 }
136
137 $lineItems = [];
138 $displayLineItems = FALSE;
139 if ($id) {
140 $lineItems = [CRM_Price_BAO_LineItem::getLineItemsByContributionID(($id))];
141 $firstLineItem = reset($lineItems[0]);
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 {
149 $priceSet = civicrm_api3('PriceSet', 'getsingle', [
150 'id' => $firstLineItem['price_set_id'],
151 'return' => 'is_quick_config, id',
152 ]);
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 }
159 }
160 $this->assign('lineItem', $lineItems);
161 $this->assign('displayLineItems', $displayLineItems);
162 $values['totalAmount'] = $values['total_amount'];
163 $this->assign('displayLineItemFinancialType', TRUE);
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 }
170 if ($values['contribution_status'] == 'Refunded') {
171 $this->assign('refund_trxn_id', CRM_Core_BAO_FinancialTrxn::getRefundTransactionTrxnID($id));
172 }
173
174 // assign values to the template
175 $this->assign($values);
176 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
177 $invoicing = CRM_Invoicing_Utils::isInvoicingEnabled();
178 $this->assign('invoicing', $invoicing);
179 $this->assign('isDeferred', CRM_Utils_Array::value('deferred_revenue_enabled', $invoiceSettings));
180 if ($invoicing && isset($values['tax_amount'])) {
181 $this->assign('totalTaxAmount', $values['tax_amount']);
182 }
183
184 $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']);
185 $this->assign('displayName', $displayName);
186
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
193 CRM_Utils_System::setTitle(ts('View Contribution from') . ' ' . $displayName);
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
200 $title = $displayName . ' - (' . CRM_Utils_Money::format($values['total_amount'], $values['currency']) . ' ' . ' - ' . $values['financial_type'] . ')';
201
202 $recentOther = [];
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 );
221 $contributionStatus = $status[$values['contribution_status_id']];
222 if (in_array($contributionStatus, ['Partially paid', 'Pending refund'])
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 }
236 $this->assignPaymentInfoBlock($id);
237 }
238
239 /**
240 * Build the form object.
241 */
242 public function buildQuickForm() {
243 $this->addButtons([
244 [
245 'type' => 'cancel',
246 'name' => ts('Done'),
247 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
248 'isDefault' => TRUE,
249 ],
250 ]);
251 }
252
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 *
262 * @return string
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
276 }