3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Financial_Form_PaymentEdit
extends CRM_Core_Form
{
36 * The id of the financial trxn.
43 * The id of the related contribution ID
47 protected $_contributionID;
50 * The variable which holds the information of a financial transaction
57 * Explicitly declare the form context.
59 public function getDefaultContext() {
64 * Set variables up before form is built.
66 public function preProcess() {
67 $this->_action
= CRM_Core_Action
::UPDATE
;
69 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
70 $this->assign('id', $this->_id
);
71 $this->_contributionID
= CRM_Utils_Request
::retrieve('contribution_id', 'Positive', $this);
73 $this->_values
= civicrm_api3('FinancialTrxn', 'getsingle', ['id' => $this->_id
]);
74 if (!empty($this->_values
['payment_processor_id'])) {
75 CRM_Core_Error
::statusBounce(ts('You cannot update this payment as it is tied to a payment processor'));
84 public function setDefaultValues() {
85 $defaults = $this->_values
;
86 // Format money fields - localize for display
87 $moneyFields = ['total_amount', 'fee_amount', 'net_amount'];
88 foreach ($moneyFields as $field) {
89 $defaults[$field] = CRM_Utils_Money
::formatLocaleNumericRoundedForDefaultCurrency($this->_values
[$field]);
97 public function buildQuickForm() {
98 CRM_Utils_System
::setTitle(ts('Update Payment details'));
100 $paymentFields = $this->getPaymentFields();
101 $this->assign('paymentFields', $paymentFields);
102 foreach ($paymentFields as $name => $paymentField) {
103 if (!empty($paymentField['add_field'])) {
105 'entity' => 'FinancialTrxn',
108 $this->addField($name, $attributes, $paymentField['is_required']);
111 $this->add($paymentField['htmlType'],
113 $paymentField['title'],
114 $paymentField['attributes'],
115 $paymentField['is_required']
120 $this->assign('currency', CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_Currency', $this->_values
['currency'], 'symbol', 'name'));
121 $this->addFormRule([__CLASS__
, 'formRule'], $this);
126 'name' => ts('Update'),
131 'name' => ts('Cancel'),
139 * @param array $fields
140 * The input form values.
141 * @param array $files
142 * The uploaded files if any.
146 * true if no errors, else array of errors
148 public static function formRule($fields, $files, $self) {
151 // if Credit Card is chosen and pan_truncation is not NULL ensure that it's value is numeric else throw validation error
152 if (CRM_Core_PseudoConstant
::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $fields['payment_instrument_id']) == 'Credit Card' &&
153 !empty($fields['pan_truncation']) &&
154 !CRM_Utils_Rule
::numeric($fields['pan_truncation'])
156 $errors['pan_truncation'] = ts('Please enter a valid Card Number');
163 * Process the form submission.
165 public function postProcess() {
168 'payment_instrument_id' => $this->_submitValues
['payment_instrument_id'],
169 'trxn_id' => CRM_Utils_Array
::value('trxn_id', $this->_submitValues
),
170 'trxn_date' => CRM_Utils_Array
::value('trxn_date', $this->_submitValues
, date('YmdHis')),
173 $paymentInstrumentName = CRM_Core_PseudoConstant
::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $params['payment_instrument_id']);
174 if ($paymentInstrumentName == 'Credit Card') {
175 $params['card_type_id'] = CRM_Utils_Array
::value('card_type_id', $this->_submitValues
);
176 $params['pan_truncation'] = CRM_Utils_Array
::value('pan_truncation', $this->_submitValues
);
178 elseif ($paymentInstrumentName == 'Check') {
179 $params['check_number'] = CRM_Utils_Array
::value('check_number', $this->_submitValues
);
182 $this->submit($params);
184 CRM_Core_Session
::singleton()->pushUserContext(CRM_Utils_System
::url(CRM_Utils_System
::currentPath()));
188 * Wrapper function to process form submission
190 * @param array $submittedValues
193 protected function submit($submittedValues) {
194 // if payment instrument is changed then
195 // 1. Record a new reverse financial transaction with old payment instrument
196 // 2. Record a new financial transaction with new payment instrument
197 // 3. Add EntityFinancialTrxn records to relate with corresponding financial item and contribution
198 if ($submittedValues['payment_instrument_id'] != $this->_values
['payment_instrument_id']) {
199 $previousFinanciaTrxn = $this->_values
;
200 $newFinancialTrxn = $submittedValues;
201 unset($previousFinanciaTrxn['id'], $newFinancialTrxn['id']);
202 $previousFinanciaTrxn['trxn_date'] = CRM_Utils_Array
::value('trxn_date', $submittedValues, date('YmdHis'));
203 $previousFinanciaTrxn['total_amount'] = -$previousFinanciaTrxn['total_amount'];
204 $previousFinanciaTrxn['net_amount'] = -$previousFinanciaTrxn['net_amount'];
205 $previousFinanciaTrxn['fee_amount'] = -$previousFinanciaTrxn['fee_amount'];
206 $previousFinanciaTrxn['contribution_id'] = $newFinancialTrxn['contribution_id'] = $this->_contributionID
;
208 $newFinancialTrxn['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($submittedValues['payment_instrument_id']);
209 foreach (['total_amount', 'fee_amount', 'net_amount', 'currency', 'is_payment', 'status_id'] as $fieldName) {
210 $newFinancialTrxn[$fieldName] = $this->_values
[$fieldName];
213 foreach ([$previousFinanciaTrxn, $newFinancialTrxn] as $financialTrxnParams) {
214 $financialTrxn = civicrm_api3('FinancialTrxn', 'create', $financialTrxnParams);
216 'total_amount' => $financialTrxnParams['total_amount'],
217 'contribution_id' => $this->_contributionID
,
219 $contributionTotalAmount = CRM_Core_DAO
::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionID
, 'total_amount');
220 CRM_Contribute_BAO_Contribution
::assignProportionalLineItems($trxnParams, $financialTrxn['id'], $contributionTotalAmount);
224 // simply update the financial trxn
225 civicrm_api3('FinancialTrxn', 'create', $submittedValues);
228 self
::updateRelatedContribution($submittedValues, $this->_contributionID
);
232 * Wrapper for unit testing the post process submit function.
234 * @param array $params
236 public function testSubmit($params) {
237 $this->_id
= $params['id'];
238 $this->_contributionID
= $params['contribution_id'];
239 $this->_values
= civicrm_api3('FinancialTrxn', 'getsingle', ['id' => $params['id']]);
241 $this->submit($params);
245 * Function to update contribution's check_number and trxn_id by
246 * concatenating values from financial trxn's check_number and trxn_id respectively
248 * @param array $params
249 * @param int $contributionID
251 public static function updateRelatedContribution($params, $contributionID) {
252 $contributionDAO = new CRM_Contribute_DAO_Contribution();
253 $contributionDAO->id
= $contributionID;
254 $contributionDAO->find(TRUE);
256 foreach (['trxn_id', 'check_number'] as $fieldName) {
257 if (!empty($params[$fieldName])) {
258 if (!empty($contributionDAO->$fieldName)) {
259 $values = explode(',', $contributionDAO->$fieldName);
260 // if submitted check_number or trxn_id value is
261 // already present then ignore else add to $values array
262 if (!in_array($params[$fieldName], $values)) {
263 $values[] = $params[$fieldName];
265 $contributionDAO->$fieldName = implode(',', $values);
270 $contributionDAO->save();
276 public function getPaymentFields() {
278 'payment_instrument_id' => [
279 'is_required' => TRUE,
283 'is_required' => FALSE,
286 // @TODO we need to show card type icon in place of select field
288 'is_required' => FALSE,
291 'pan_truncation' => [
292 'is_required' => FALSE,
297 'is_required' => FALSE,
300 'htmlType' => 'datepicker',
301 'name' => 'trxn_date',
302 'title' => ts('Transaction Date'),
303 'is_required' => TRUE,
305 'date' => 'yyyy-mm-dd',
310 'htmlType' => 'text',
311 'name' => 'total_amount',
312 'title' => ts('Total Amount'),
313 'is_required' => TRUE,
321 return $paymentFields;