Merge pull request #22290 from eileenmcnaughton/cont
[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 if (empty($id)) {
29 throw new CRM_Core_Exception('Contribution ID is required');
30 }
31 $params = ['id' => $id];
32 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
33 $this->assign('context', $context);
34
35 $values = CRM_Contribute_BAO_Contribution::getValuesWithMappings($params);
36
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
49 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && $this->_action & CRM_Core_Action::VIEW) {
50 $financialTypeID = CRM_Contribute_PseudoConstant::financialType($values['financial_type_id']);
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 }
58 if (!CRM_Core_Permission::check('view contributions of type ' . $financialTypeID)) {
59 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
60 }
61 }
62 elseif ($this->_action & CRM_Core_Action::VIEW) {
63 $this->assign('noACL', TRUE);
64 }
65 CRM_Contribute_BAO_Contribution::resolveDefaults($values);
66
67 if (!empty($values['contribution_page_id'])) {
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
72 // get received into i.e to_financial_account_id from last trxn
73 $financialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($values['contribution_id'], 'DESC');
74 $values['to_financial_account'] = '';
75 if (!empty($financialTrxnId['financialTrxnId'])) {
76 $values['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $financialTrxnId['financialTrxnId'], 'to_financial_account_id');
77 if ($values['to_financial_account_id']) {
78 $values['to_financial_account'] = CRM_Contribute_PseudoConstant::financialAccount($values['to_financial_account_id']);
79 }
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 }
84 }
85
86 if (!empty($values['contribution_recur_id'])) {
87 $sql = "SELECT installments, frequency_interval, frequency_unit FROM civicrm_contribution_recur WHERE id = %1";
88 $params = [1 => [$values['contribution_recur_id'], 'Integer']];
89 $dao = CRM_Core_DAO::executeQuery($sql, $params);
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
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);
99 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $id);
100
101 $premiumId = NULL;
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;
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
124 if (!empty($values['address_id'])) {
125 $addressParams = ['id' => $values['address_id']];
126 $addressDetails = CRM_Core_BAO_Address::getValues($addressParams, FALSE, 'id');
127 $addressDetails = array_values($addressDetails);
128 $values['billing_address'] = $addressDetails[0]['display'];
129 }
130
131 //assign soft credit record if exists.
132 $SCRecords = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($values['contribution_id'], TRUE);
133 if (!empty($SCRecords['soft_credit'])) {
134 $this->assign('softContributions', $SCRecords['soft_credit']);
135 unset($SCRecords['soft_credit']);
136 }
137
138 //assign pcp record if exists
139 foreach ($SCRecords as $name => $value) {
140 $this->assign($name, $value);
141 }
142
143 $lineItems = [CRM_Price_BAO_LineItem::getLineItemsByContributionID(($id))];
144 $this->assign('lineItem', $lineItems);
145 $values['totalAmount'] = $values['total_amount'];
146 $this->assign('displayLineItemFinancialType', TRUE);
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 }
153 if ($values['contribution_status'] == 'Refunded') {
154 $this->assign('refund_trxn_id', CRM_Core_BAO_FinancialTrxn::getRefundTransactionTrxnID($id));
155 }
156
157 // assign values to the template
158 $this->assignVariables($values, array_keys($values));
159 $invoicing = CRM_Invoicing_Utils::isInvoicingEnabled();
160 $this->assign('invoicing', $invoicing);
161 $this->assign('isDeferred', Civi::settings()->get('deferred_revenue_enabled'));
162 if ($invoicing && isset($values['tax_amount'])) {
163 $this->assign('totalTaxAmount', $values['tax_amount']);
164 }
165
166 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
167 $displayName = CRM_Contact_BAO_Contact::displayName($values['contact_id']);
168 $this->assign('displayName', $displayName);
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
174 if (empty($values['is_template'])) {
175 $this->setTitle(ts('View Contribution from') . ' ' . $displayName);
176 }
177 else {
178 $this->setTitle(ts('View Template Contribution from') . ' ' . $displayName);
179 }
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
186 $title = $displayName . ' - (' . CRM_Utils_Money::format($values['total_amount'], $values['currency']) . ' ' . ' - ' . $values['financial_type'] . ')';
187
188 $recentOther = [];
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 );
207 $statusOptionValueNames = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
208 $contributionStatus = $statusOptionValueNames[$values['contribution_status_id']];
209 if (in_array($contributionStatus, ['Partially paid', 'Pending refund'])
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 }
223 $this->assignPaymentInfoBlock($id);
224 }
225
226 /**
227 * Build the form object.
228 */
229 public function buildQuickForm() {
230 $this->addButtons([
231 [
232 'type' => 'cancel',
233 'name' => ts('Done'),
234 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
235 'isDefault' => TRUE,
236 ],
237 ]);
238 }
239
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 *
249 * @return string
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
263 }