Commit | Line | Data |
---|---|---|
9b2e3ee6 MD |
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 | ||
d8c01ab5 | 49 | /** |
50 | * Explicitly declare the form context. | |
51 | */ | |
52 | public function getDefaultContext() { | |
53 | return 'create'; | |
54 | } | |
9b2e3ee6 MD |
55 | /** |
56 | * Set variables up before form is built. | |
57 | */ | |
58 | public function preProcess() { | |
d8c01ab5 | 59 | $this->_action = CRM_Core_Action::UPDATE; |
9b2e3ee6 MD |
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() { | |
4ee75265 | 76 | return $this->_values; |
9b2e3ee6 MD |
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) { | |
4ee75265 | 88 | if (!empty($paymentField['add_field'])) { |
a7a8f17d | 89 | $attributes = array( |
90 | 'entity' => 'FinancialTrxn', | |
91 | 'name' => $name, | |
92 | ); | |
93 | $this->addField($name, $attributes, $paymentField['is_required']); | |
4ee75265 | 94 | } |
95 | else { | |
96 | $this->add($paymentField['htmlType'], | |
97 | $name, | |
98 | $paymentField['title'], | |
99 | $paymentField['attributes'], | |
100 | $paymentField['is_required'] | |
101 | ); | |
9b2e3ee6 MD |
102 | } |
103 | } | |
104 | ||
105 | $this->assign('currency', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $this->_values['currency'], 'symbol', 'name')); | |
4ee75265 | 106 | $this->addFormRule(array(__CLASS__, 'formRule'), $this); |
107 | ||
9b2e3ee6 MD |
108 | $this->addButtons(array( |
109 | array( | |
110 | 'type' => 'submit', | |
111 | 'name' => ts('Update'), | |
112 | 'isDefault' => TRUE, | |
113 | ), | |
114 | array( | |
115 | 'type' => 'cancel', | |
116 | 'name' => ts('Cancel'), | |
117 | ), | |
118 | )); | |
119 | } | |
120 | ||
4ee75265 | 121 | /** |
122 | * Global form rule. | |
123 | * | |
124 | * @param array $fields | |
125 | * The input form values. | |
126 | * @param array $files | |
127 | * The uploaded files if any. | |
128 | * @param $self | |
129 | * | |
130 | * @return bool|array | |
131 | * true if no errors, else array of errors | |
132 | */ | |
133 | public static function formRule($fields, $files, $self) { | |
134 | $errors = array(); | |
135 | ||
136 | // if Credit Card is chosen and pan_truncation is not NULL ensure that it's value is numeric else throw validation error | |
137 | if (CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $fields['payment_instrument_id']) == 'Credit Card' && | |
138 | !empty($fields['pan_truncation']) && | |
139 | !CRM_Utils_Rule::numeric($fields['pan_truncation']) | |
140 | ) { | |
141 | $errors['pan_truncation'] = ts('Please enter a valid Card Number'); | |
142 | } | |
143 | ||
144 | return $errors; | |
145 | } | |
146 | ||
9b2e3ee6 MD |
147 | /** |
148 | * Process the form submission. | |
149 | */ | |
150 | public function postProcess() { | |
151 | $params = array( | |
152 | 'id' => $this->_id, | |
4ee75265 | 153 | 'payment_instrument_id' => $this->_submitValues['payment_instrument_id'], |
154 | 'trxn_id' => CRM_Utils_Array::value('trxn_id', $this->_submitValues), | |
9b2e3ee6 MD |
155 | 'trxn_date' => CRM_Utils_Array::value('trxn_date', $this->_submitValues, date('YmdHis')), |
156 | ); | |
4ee75265 | 157 | |
158 | $paymentInstrumentName = CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $params['payment_instrument_id']); | |
159 | if ($paymentInstrumentName == 'Credit Card') { | |
160 | $params['card_type_id'] = CRM_Utils_Array::value('card_type_id', $this->_submitValues); | |
161 | $params['pan_truncation'] = CRM_Utils_Array::value('pan_truncation', $this->_submitValues); | |
162 | } | |
163 | elseif ($paymentInstrumentName == 'Check') { | |
164 | $params['check_number'] = CRM_Utils_Array::value('check_number', $this->_submitValues); | |
9b2e3ee6 | 165 | } |
4ee75265 | 166 | |
9b2e3ee6 MD |
167 | // update the financial trxn |
168 | civicrm_api3('FinancialTrxn', 'create', $params); | |
169 | CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath())); | |
170 | } | |
171 | ||
172 | /** | |
173 | * Get payment fields | |
174 | */ | |
175 | public function getPaymentFields() { | |
4ee75265 | 176 | $paymentFields = array( |
177 | 'payment_instrument_id' => array( | |
178 | 'is_required' => TRUE, | |
179 | 'add_field' => TRUE, | |
4ee75265 | 180 | ), |
181 | 'check_number' => array( | |
9b2e3ee6 | 182 | 'is_required' => FALSE, |
4ee75265 | 183 | 'add_field' => TRUE, |
4ee75265 | 184 | ), |
185 | // @TODO we need to show card type icon in place of select field | |
186 | 'card_type_id' => array( | |
187 | 'is_required' => FALSE, | |
188 | 'add_field' => TRUE, | |
4ee75265 | 189 | ), |
190 | 'pan_truncation' => array( | |
191 | 'is_required' => FALSE, | |
192 | 'add_field' => TRUE, | |
4ee75265 | 193 | ), |
194 | 'trxn_id' => array( | |
a7a8f17d | 195 | 'add_field' => TRUE, |
4ee75265 | 196 | 'is_required' => FALSE, |
4ee75265 | 197 | ), |
9b2e3ee6 MD |
198 | 'trxn_date' => array( |
199 | 'htmlType' => 'datepicker', | |
200 | 'name' => 'trxn_date', | |
201 | 'title' => ts('Transaction Date'), | |
4ee75265 | 202 | 'is_required' => TRUE, |
9b2e3ee6 MD |
203 | 'attributes' => array( |
204 | 'date' => 'yyyy-mm-dd', | |
205 | 'time' => 24, | |
206 | ), | |
207 | ), | |
208 | 'total_amount' => array( | |
209 | 'htmlType' => 'text', | |
210 | 'name' => 'total_amount', | |
211 | 'title' => ts('Total Amount'), | |
4ee75265 | 212 | 'is_required' => TRUE, |
9b2e3ee6 MD |
213 | 'attributes' => array( |
214 | 'readonly' => TRUE, | |
215 | 'size' => 6, | |
216 | ), | |
217 | ), | |
218 | ); | |
219 | ||
220 | return $paymentFields; | |
221 | } | |
222 | ||
223 | } |