524afb6cf3d07247552ff91a5f15c68deb732634
[civicrm-core.git] / CRM / Financial / Form / PaymentEdit.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33 class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form {
34
35 /**
36 * The id of the financial trxn.
37 *
38 * @var int
39 */
40 protected $_id;
41
42 /**
43 * The variable which holds the information of a financial transaction
44 *
45 * @var array
46 */
47 protected $_values;
48
49 /**
50 * Explicitly declare the form context.
51 */
52 public function getDefaultContext() {
53 return 'create';
54 }
55 /**
56 * Set variables up before form is built.
57 */
58 public function preProcess() {
59 $this->_action = CRM_Core_Action::UPDATE;
60 parent::preProcess();
61 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
62 $this->assign('id', $this->_id);
63
64 $this->_values = civicrm_api3('FinancialTrxn', 'getsingle', array('id' => $this->_id));
65 if (!empty($this->_values['payment_processor_id'])) {
66 CRM_Core_Error::statusBounce(ts('You cannot update this payment'));
67 }
68 }
69
70 /**
71 * Set default values.
72 *
73 * @return array
74 */
75 public function setDefaultValues() {
76 return $this->_values;
77 }
78
79 /**
80 * Build quickForm.
81 */
82 public function buildQuickForm() {
83 CRM_Utils_System::setTitle(ts('Update Payment details'));
84
85 $paymentFields = $this->getPaymentFields();
86 $this->assign('paymentFields', $paymentFields);
87 foreach ($paymentFields as $name => $paymentField) {
88 if (!empty($paymentField['add_field'])) {
89 $this->addField($name, $paymentField['attributes'], $paymentField['is_required']);
90 }
91 else {
92 $this->add($paymentField['htmlType'],
93 $name,
94 $paymentField['title'],
95 $paymentField['attributes'],
96 $paymentField['is_required']
97 );
98 }
99 }
100
101 $this->assign('currency', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $this->_values['currency'], 'symbol', 'name'));
102 $this->addFormRule(array(__CLASS__, 'formRule'), $this);
103
104 $this->addButtons(array(
105 array(
106 'type' => 'submit',
107 'name' => ts('Update'),
108 'isDefault' => TRUE,
109 ),
110 array(
111 'type' => 'cancel',
112 'name' => ts('Cancel'),
113 ),
114 ));
115 }
116
117 /**
118 * Global form rule.
119 *
120 * @param array $fields
121 * The input form values.
122 * @param array $files
123 * The uploaded files if any.
124 * @param $self
125 *
126 * @return bool|array
127 * true if no errors, else array of errors
128 */
129 public static function formRule($fields, $files, $self) {
130 $errors = array();
131
132 // if Credit Card is chosen and pan_truncation is not NULL ensure that it's value is numeric else throw validation error
133 if (CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $fields['payment_instrument_id']) == 'Credit Card' &&
134 !empty($fields['pan_truncation']) &&
135 !CRM_Utils_Rule::numeric($fields['pan_truncation'])
136 ) {
137 $errors['pan_truncation'] = ts('Please enter a valid Card Number');
138 }
139
140 return $errors;
141 }
142
143 /**
144 * Process the form submission.
145 */
146 public function postProcess() {
147 $params = array(
148 'id' => $this->_id,
149 'payment_instrument_id' => $this->_submitValues['payment_instrument_id'],
150 'trxn_id' => CRM_Utils_Array::value('trxn_id', $this->_submitValues),
151 'trxn_date' => CRM_Utils_Array::value('trxn_date', $this->_submitValues, date('YmdHis')),
152 );
153
154 $paymentInstrumentName = CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $params['payment_instrument_id']);
155 if ($paymentInstrumentName == 'Credit Card') {
156 $params['card_type_id'] = CRM_Utils_Array::value('card_type_id', $this->_submitValues);
157 $params['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $this->_submitValues);
158 }
159 elseif ($paymentInstrumentName == 'Check') {
160 $params['check_number'] = CRM_Utils_Array::value('check_number', $this->_submitValues);
161 }
162
163 // update the financial trxn
164 civicrm_api3('FinancialTrxn', 'create', $params);
165 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath()));
166 }
167
168 /**
169 * Get payment fields
170 */
171 public function getPaymentFields() {
172 $paymentFields = array(
173 'payment_instrument_id' => array(
174 'is_required' => TRUE,
175 'add_field' => TRUE,
176 'attributes' => array(
177 'entity' => 'FinancialTrxn',
178 'name' => 'payment_instrument_id',
179 ),
180 ),
181 'check_number' => array(
182 'is_required' => FALSE,
183 'add_field' => TRUE,
184 'attributes' => array(
185 'entity' => 'FinancialTrxn',
186 'name' => 'check_number',
187 ),
188 ),
189 // @TODO we need to show card type icon in place of select field
190 'card_type_id' => array(
191 'is_required' => FALSE,
192 'add_field' => TRUE,
193 'attributes' => array(
194 'entity' => 'FinancialTrxn',
195 'name' => 'card_type_id',
196 ),
197 ),
198 'pan_truncation' => array(
199 'is_required' => FALSE,
200 'add_field' => TRUE,
201 'attributes' => array(
202 'entity' => 'FinancialTrxn',
203 'name' => 'pan_truncation',
204 ),
205 ),
206 'trxn_id' => array(
207 'htmlType' => 'text',
208 'title' => ts('Transaction ID'),
209 'is_required' => FALSE,
210 'attributes' => array(
211 'size' => 6,
212 ),
213 ),
214 'trxn_date' => array(
215 'htmlType' => 'datepicker',
216 'name' => 'trxn_date',
217 'title' => ts('Transaction Date'),
218 'is_required' => TRUE,
219 'attributes' => array(
220 'date' => 'yyyy-mm-dd',
221 'time' => 24,
222 ),
223 ),
224 'total_amount' => array(
225 'htmlType' => 'text',
226 'name' => 'total_amount',
227 'title' => ts('Total Amount'),
228 'is_required' => TRUE,
229 'attributes' => array(
230 'readonly' => TRUE,
231 'size' => 6,
232 ),
233 ),
234 );
235
236 return $paymentFields;
237 }
238
239 }