Merge pull request #19658 from eileenmcnaughton/member
[civicrm-core.git] / CRM / Financial / Form / PaymentEdit.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 class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form {
18
19 /**
20 * The id of the financial trxn.
21 *
22 * @var int
23 */
24 protected $_id;
25
26 /**
27 * The id of the related contribution ID
28 *
29 * @var int
30 */
31 protected $_contributionID;
32
33 /**
34 * The variable which holds the information of a financial transaction
35 *
36 * @var array
37 */
38 protected $_values;
39
40 /**
41 * Explicitly declare the form context.
42 */
43 public function getDefaultContext() {
44 return 'create';
45 }
46
47 /**
48 * Set variables up before form is built.
49 */
50 public function preProcess() {
51 $this->_action = CRM_Core_Action::UPDATE;
52 parent::preProcess();
53 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
54 $this->assign('id', $this->_id);
55 $this->_contributionID = CRM_Utils_Request::retrieve('contribution_id', 'Positive', $this);
56
57 $this->_values = civicrm_api3('FinancialTrxn', 'getsingle', ['id' => $this->_id]);
58 if (!empty($this->_values['payment_processor_id'])) {
59 CRM_Core_Error::statusBounce(ts('You cannot update this payment as it is tied to a payment processor'));
60 }
61 }
62
63 /**
64 * Set default values.
65 *
66 * @return array
67 */
68 public function setDefaultValues() {
69 $defaults = $this->_values;
70 $defaults['total_amount'] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($this->_values['total_amount']);
71 return $defaults;
72 }
73
74 /**
75 * Build quickForm.
76 */
77 public function buildQuickForm() {
78 CRM_Utils_System::setTitle(ts('Update Payment details'));
79
80 $paymentFields = $this->getPaymentFields();
81 $this->assign('paymentFields', $paymentFields);
82 foreach ($paymentFields as $name => $paymentField) {
83 if (!empty($paymentField['add_field'])) {
84 $attributes = [
85 'entity' => 'FinancialTrxn',
86 'name' => $name,
87 ];
88 $this->addField($name, $attributes, $paymentField['is_required']);
89 }
90 else {
91 $this->add($paymentField['htmlType'],
92 $name,
93 $paymentField['title'],
94 $paymentField['attributes'],
95 $paymentField['is_required']
96 );
97 }
98 }
99
100 $this->assign('currency', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $this->_values['currency'], 'symbol', 'name'));
101 $this->addFormRule([__CLASS__, 'formRule'], $this);
102
103 $this->addButtons([
104 [
105 'type' => 'submit',
106 'name' => ts('Update'),
107 'isDefault' => TRUE,
108 ],
109 [
110 'type' => 'cancel',
111 'name' => ts('Cancel'),
112 ],
113 ]);
114 }
115
116 /**
117 * Global form rule.
118 *
119 * @param array $fields
120 * The input form values.
121 * @param array $files
122 * The uploaded files if any.
123 * @param $self
124 *
125 * @return bool|array
126 * true if no errors, else array of errors
127 */
128 public static function formRule($fields, $files, $self) {
129 $errors = [];
130
131 // if Credit Card is chosen and pan_truncation is not NULL ensure that it's value is numeric else throw validation error
132 if (CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $fields['payment_instrument_id']) == 'Credit Card' &&
133 !empty($fields['pan_truncation']) &&
134 !CRM_Utils_Rule::numeric($fields['pan_truncation'])
135 ) {
136 $errors['pan_truncation'] = ts('Please enter a valid Card Number');
137 }
138
139 return $errors;
140 }
141
142 /**
143 * Process the form submission.
144 */
145 public function postProcess() {
146 $params = [
147 'id' => $this->_id,
148 'payment_instrument_id' => $this->_submitValues['payment_instrument_id'],
149 'trxn_id' => $this->_submitValues['trxn_id'] ?? NULL,
150 'trxn_date' => CRM_Utils_Array::value('trxn_date', $this->_submitValues, date('YmdHis')),
151 ];
152
153 $paymentInstrumentName = CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $params['payment_instrument_id']);
154 if ($paymentInstrumentName == 'Credit Card') {
155 $params['card_type_id'] = $this->_submitValues['card_type_id'] ?? NULL;
156 $params['pan_truncation'] = $this->_submitValues['pan_truncation'] ?? NULL;
157 }
158 elseif ($paymentInstrumentName == 'Check') {
159 $params['check_number'] = $this->_submitValues['check_number'] ?? NULL;
160 }
161
162 $this->submit($params);
163
164 $contactId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_contributionID, 'contact_id');
165 $url = CRM_Utils_System::url(
166 "civicrm/contact/view/contribution",
167 "reset=1&action=update&id={$this->_contributionID}&cid={$contactId}&context=contribution"
168 );
169 CRM_Core_Session::singleton()->pushUserContext($url);
170 }
171
172 /**
173 * Wrapper function to process form submission
174 *
175 * @param array $submittedValues
176 *
177 * @throws \CiviCRM_API3_Exception
178 */
179 protected function submit($submittedValues) {
180 // if payment instrument is changed then
181 // 1. Record a new reverse financial transaction with old payment instrument
182 // 2. Record a new financial transaction with new payment instrument
183 // 3. Add EntityFinancialTrxn records to relate with corresponding financial item and contribution
184 if ($submittedValues['payment_instrument_id'] != $this->_values['payment_instrument_id']) {
185 civicrm_api3('Payment', 'cancel', [
186 'id' => $this->_values['id'],
187 'trxn_date' => $submittedValues['trxn_date'],
188 ]);
189
190 $newFinancialTrxn = $submittedValues;
191 unset($newFinancialTrxn['id']);
192 $newFinancialTrxn['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($submittedValues['payment_instrument_id']);
193 $newFinancialTrxn['total_amount'] = $this->_values['total_amount'];
194 $newFinancialTrxn['currency'] = $this->_values['currency'];
195 civicrm_api3('Payment', 'create', $newFinancialTrxn);
196 }
197 else {
198 // simply update the financial trxn
199 civicrm_api3('FinancialTrxn', 'create', $submittedValues);
200 }
201
202 CRM_Financial_BAO_Payment::updateRelatedContribution($submittedValues, $this->_contributionID);
203 }
204
205 /**
206 * Wrapper for unit testing the post process submit function.
207 *
208 * @param array $params
209 *
210 * @throws \CiviCRM_API3_Exception
211 */
212 public function testSubmit(array $params): void {
213 $this->_id = $params['id'];
214 $this->_contributionID = $params['contribution_id'];
215 $this->_values = civicrm_api3('FinancialTrxn', 'getsingle', ['id' => $params['id']]);
216
217 $this->submit($params);
218 }
219
220 /**
221 * Get payment fields
222 */
223 public function getPaymentFields() {
224 $paymentFields = [
225 'payment_instrument_id' => [
226 'is_required' => TRUE,
227 'add_field' => TRUE,
228 ],
229 'check_number' => [
230 'is_required' => FALSE,
231 'add_field' => TRUE,
232 ],
233 // @TODO we need to show card type icon in place of select field
234 'card_type_id' => [
235 'is_required' => FALSE,
236 'add_field' => TRUE,
237 ],
238 'pan_truncation' => [
239 'is_required' => FALSE,
240 'add_field' => TRUE,
241 ],
242 'trxn_id' => [
243 'add_field' => TRUE,
244 'is_required' => FALSE,
245 ],
246 'trxn_date' => [
247 'htmlType' => 'datepicker',
248 'name' => 'trxn_date',
249 'title' => ts('Transaction Date'),
250 'is_required' => TRUE,
251 'attributes' => [
252 'date' => 'yyyy-mm-dd',
253 'time' => 24,
254 ],
255 ],
256 'total_amount' => [
257 'htmlType' => 'text',
258 'name' => 'total_amount',
259 'title' => ts('Total Amount'),
260 'is_required' => TRUE,
261 'attributes' => [
262 'readonly' => TRUE,
263 'size' => 6,
264 ],
265 ],
266 ];
267
268 return $paymentFields;
269 }
270
271 }