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