3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Financial_Form_PaymentEdit
extends CRM_Core_Form
{
20 * The id of the financial trxn.
27 * The id of the related contribution ID
31 protected $_contributionID;
34 * The variable which holds the information of a financial transaction
41 * Explicitly declare the form context.
43 public function getDefaultContext() {
48 * Set variables up before form is built.
50 public function preProcess() {
51 $this->_action
= CRM_Core_Action
::UPDATE
;
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);
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'));
68 public function setDefaultValues() {
69 $defaults = $this->_values
;
70 $defaults['total_amount'] = CRM_Utils_Money
::formatLocaleNumericRoundedForDefaultCurrency($this->_values
['total_amount']);
77 public function buildQuickForm() {
78 CRM_Utils_System
::setTitle(ts('Update Payment details'));
80 $paymentFields = $this->getPaymentFields();
81 $this->assign('paymentFields', $paymentFields);
82 foreach ($paymentFields as $name => $paymentField) {
83 if (!empty($paymentField['add_field'])) {
85 'entity' => 'FinancialTrxn',
88 $this->addField($name, $attributes, $paymentField['is_required']);
91 $this->add($paymentField['htmlType'],
93 $paymentField['title'],
94 $paymentField['attributes'],
95 $paymentField['is_required']
100 $this->assign('currency', CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_Currency', $this->_values
['currency'], 'symbol', 'name'));
101 $this->addFormRule([__CLASS__
, 'formRule'], $this);
106 'name' => ts('Update'),
111 'name' => ts('Cancel'),
119 * @param array $fields
120 * The input form values.
121 * @param array $files
122 * The uploaded files if any.
126 * true if no errors, else array of errors
128 public static function formRule($fields, $files, $self) {
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'])
136 $errors['pan_truncation'] = ts('Please enter a valid Card Number');
143 * Process the form submission.
145 public function postProcess() {
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')),
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;
158 elseif ($paymentInstrumentName == 'Check') {
159 $params['check_number'] = $this->_submitValues
['check_number'] ??
NULL;
162 $this->submit($params);
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"
169 CRM_Core_Session
::singleton()->pushUserContext($url);
173 * Wrapper function to process form submission
175 * @param array $submittedValues
177 * @throws \CiviCRM_API3_Exception
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'],
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);
198 // simply update the financial trxn
199 civicrm_api3('FinancialTrxn', 'create', $submittedValues);
202 CRM_Financial_BAO_Payment
::updateRelatedContribution($submittedValues, $this->_contributionID
);
206 * Wrapper for unit testing the post process submit function.
208 * @param array $params
210 * @throws \CiviCRM_API3_Exception
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']]);
217 $this->submit($params);
223 public function getPaymentFields() {
225 'payment_instrument_id' => [
226 'is_required' => TRUE,
230 'is_required' => FALSE,
233 // @TODO we need to show card type icon in place of select field
235 'is_required' => FALSE,
238 'pan_truncation' => [
239 'is_required' => FALSE,
244 'is_required' => FALSE,
247 'htmlType' => 'datepicker',
248 'name' => 'trxn_date',
249 'title' => ts('Transaction Date'),
250 'is_required' => TRUE,
252 'date' => 'yyyy-mm-dd',
257 'htmlType' => 'text',
258 'name' => 'total_amount',
259 'title' => ts('Total Amount'),
260 'is_required' => TRUE,
268 return $paymentFields;