3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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() {
63 * Set variables up before form is built.
65 public function preProcess() {
66 $this->_action
= CRM_Core_Action
::UPDATE
;
68 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
69 $this->assign('id', $this->_id
);
70 $this->_contributionID
= CRM_Utils_Request
::retrieve('contribution_id', 'Positive', $this);
72 $this->_values
= civicrm_api3('FinancialTrxn', 'getsingle', array('id' => $this->_id
));
73 if (!empty($this->_values
['payment_processor_id'])) {
74 CRM_Core_Error
::statusBounce(ts('You cannot update this payment as it is tied to a payment processor'));
83 public function setDefaultValues() {
84 return $this->_values
;
90 public function buildQuickForm() {
91 CRM_Utils_System
::setTitle(ts('Update Payment details'));
93 $paymentFields = $this->getPaymentFields();
94 $this->assign('paymentFields', $paymentFields);
95 foreach ($paymentFields as $name => $paymentField) {
96 if (!empty($paymentField['add_field'])) {
98 'entity' => 'FinancialTrxn',
101 $this->addField($name, $attributes, $paymentField['is_required']);
104 $this->add($paymentField['htmlType'],
106 $paymentField['title'],
107 $paymentField['attributes'],
108 $paymentField['is_required']
113 $this->assign('currency', CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_Currency', $this->_values
['currency'], 'symbol', 'name'));
114 $this->addFormRule(array(__CLASS__
, 'formRule'), $this);
116 $this->addButtons(array(
119 'name' => ts('Update'),
124 'name' => ts('Cancel'),
132 * @param array $fields
133 * The input form values.
134 * @param array $files
135 * The uploaded files if any.
139 * true if no errors, else array of errors
141 public static function formRule($fields, $files, $self) {
144 // if Credit Card is chosen and pan_truncation is not NULL ensure that it's value is numeric else throw validation error
145 if (CRM_Core_PseudoConstant
::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $fields['payment_instrument_id']) == 'Credit Card' &&
146 !empty($fields['pan_truncation']) &&
147 !CRM_Utils_Rule
::numeric($fields['pan_truncation'])
149 $errors['pan_truncation'] = ts('Please enter a valid Card Number');
156 * Process the form submission.
158 public function postProcess() {
161 'payment_instrument_id' => $this->_submitValues
['payment_instrument_id'],
162 'trxn_id' => CRM_Utils_Array
::value('trxn_id', $this->_submitValues
),
163 'trxn_date' => CRM_Utils_Array
::value('trxn_date', $this->_submitValues
, date('YmdHis')),
166 $paymentInstrumentName = CRM_Core_PseudoConstant
::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $params['payment_instrument_id']);
167 if ($paymentInstrumentName == 'Credit Card') {
168 $params['card_type_id'] = CRM_Utils_Array
::value('card_type_id', $this->_submitValues
);
169 $params['pan_truncation'] = CRM_Utils_Array
::value('pan_truncation', $this->_submitValues
);
171 elseif ($paymentInstrumentName == 'Check') {
172 $params['check_number'] = CRM_Utils_Array
::value('check_number', $this->_submitValues
);
175 $this->submit($params);
177 CRM_Core_Session
::singleton()->pushUserContext(CRM_Utils_System
::url(CRM_Utils_System
::currentPath()));
181 * Wrapper function to process form submission
183 * @param array $submittedValues
186 protected function submit($submittedValues) {
187 // if payment instrument is changed then
188 // 1. Record a new reverse financial transaction with old payment instrument
189 // 2. Record a new financial transaction with new payment instrument
190 // 3. Add EntityFinancialTrxn records to relate with corresponding financial item and contribution
191 if ($submittedValues['payment_instrument_id'] != $this->_values
['payment_instrument_id']) {
192 $previousFinanciaTrxn = $this->_values
;
193 $newFinancialTrxn = $submittedValues;
194 unset($previousFinanciaTrxn['id'], $newFinancialTrxn['id']);
195 $previousFinanciaTrxn['trxn_date'] = CRM_Utils_Array
::value('trxn_date', $submittedValues, date('YmdHis'));
196 $previousFinanciaTrxn['total_amount'] = -$previousFinanciaTrxn['total_amount'];
197 $previousFinanciaTrxn['net_amount'] = -$previousFinanciaTrxn['net_amount'];
198 $previousFinanciaTrxn['fee_amount'] = -$previousFinanciaTrxn['fee_amount'];
199 $previousFinanciaTrxn['contribution_id'] = $newFinancialTrxn['contribution_id'] = $this->_contributionID
;
201 $newFinancialTrxn['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($submittedValues['payment_instrument_id']);
202 foreach (array('total_amount', 'fee_amount', 'net_amount', 'currency', 'is_payment', 'status_id') as $fieldName) {
203 $newFinancialTrxn[$fieldName] = $this->_values
[$fieldName];
206 foreach (array($previousFinanciaTrxn, $newFinancialTrxn) as $financialTrxnParams) {
207 civicrm_api3('FinancialTrxn', 'create', $financialTrxnParams);
209 'total_amount' => $financialTrxnParams['total_amount'],
210 'contribution_id' => $this->_contributionID
,
212 $contributionTotalAmount = CRM_Core_DAO
::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionID
, 'total_amount');
213 CRM_Contribute_BAO_Contribution
::assignProportionalLineItems($trxnParams, $submittedValues['id'], $contributionTotalAmount);
217 // simply update the financial trxn
218 civicrm_api3('FinancialTrxn', 'create', $submittedValues);
221 self
::updateRelatedContribution($submittedValues, $this->_contributionID
);
225 * Wrapper for unit testing the post process submit function.
227 * @param array $params
229 public function testSubmit($params) {
230 $this->_id
= $params['id'];
231 $this->_contributionID
= $params['contribution_id'];
232 $this->_values
= civicrm_api3('FinancialTrxn', 'getsingle', array('id' => $params['id']));
234 $this->submit($params);
238 * Function to update contribution's check_number and trxn_id by
239 * concatenating values from financial trxn's check_number and trxn_id respectively
241 * @param array $params
242 * @param int $contributionID
244 public static function updateRelatedContribution($params, $contributionID) {
245 $contributionDAO = new CRM_Contribute_DAO_Contribution();
246 $contributionDAO->id
= $contributionID;
247 $contributionDAO->find(TRUE);
249 foreach (array('trxn_id', 'check_number') as $fieldName) {
250 if (!empty($params[$fieldName])) {
251 if (!empty($contributionDAO->$fieldName)) {
252 $values = explode(',', $contributionDAO->$fieldName);
253 // if submitted check_number or trxn_id value is
254 // already present then ignore else add to $values array
255 if (!in_array($params[$fieldName], $values)) {
256 $values[] = $params[$fieldName];
258 $contributionDAO->$fieldName = implode(',', $values);
263 $contributionDAO->save();
269 public function getPaymentFields() {
270 $paymentFields = array(
271 'payment_instrument_id' => array(
272 'is_required' => TRUE,
275 'check_number' => array(
276 'is_required' => FALSE,
279 // @TODO we need to show card type icon in place of select field
280 'card_type_id' => array(
281 'is_required' => FALSE,
284 'pan_truncation' => array(
285 'is_required' => FALSE,
290 'is_required' => FALSE,
292 'trxn_date' => array(
293 'htmlType' => 'datepicker',
294 'name' => 'trxn_date',
295 'title' => ts('Transaction Date'),
296 'is_required' => TRUE,
297 'attributes' => array(
298 'date' => 'yyyy-mm-dd',
302 'total_amount' => array(
303 'htmlType' => 'text',
304 'name' => 'total_amount',
305 'title' => ts('Total Amount'),
306 'is_required' => TRUE,
307 'attributes' => array(
314 return $paymentFields;