Merge pull request #10699 from JMAConsulting/CRM-20321-1
[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 $defaults = $this->_values;
77 if (!empty($defaults['card_type_id'])) {
78 // See comments in getPaymentFields function about why this nastiness exists.
79 $defaults['credit_card_type'] = CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'card_type_id', $defaults['card_type_id']);
80 }
81
82 return $defaults;
83 }
84
85 /**
86 * Build quickForm.
87 */
88 public function buildQuickForm() {
89 CRM_Utils_System::setTitle(ts('Update Payment details'));
90
91 $paymentFields = $this->getPaymentFields();
92 $this->assign('paymentFields', $paymentFields);
93 foreach ($paymentFields as $name => $paymentField) {
94 $this->add($paymentField['htmlType'],
95 $paymentField['name'],
96 $paymentField['title'],
97 $paymentField['attributes'],
98 TRUE
99 );
100 if (!empty($paymentField['rules'])) {
101 foreach ($paymentField['rules'] as $rule) {
102 $this->addRule($name,
103 $rule['rule_message'],
104 $rule['rule_name'],
105 $rule['rule_parameters']
106 );
107 }
108 }
109 }
110
111 $this->assign('currency', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $this->_values['currency'], 'symbol', 'name'));
112 $this->addButtons(array(
113 array(
114 'type' => 'submit',
115 'name' => ts('Update'),
116 'isDefault' => TRUE,
117 ),
118 array(
119 'type' => 'cancel',
120 'name' => ts('Cancel'),
121 ),
122 ));
123 }
124
125 /**
126 * Process the form submission.
127 */
128 public function postProcess() {
129 $params = array(
130 'id' => $this->_id,
131 'check_number' => CRM_Utils_Array::value('check_number', $this->_submitValues),
132 'pan_truncation' => CRM_Utils_Array::value('pan_truncation', $this->_submitValues),
133 'trxn_date' => CRM_Utils_Array::value('trxn_date', $this->_submitValues, date('YmdHis')),
134 );
135 if (!empty($this->_submitValues['credit_card_type'])) {
136 // See comments in getPaymentFields function about why this nastiness exists.
137 $params['card_type_id'] = CRM_Core_PseudoConstant::getKey(
138 'CRM_Financial_DAO_FinancialTrxn',
139 'card_type_id',
140 $this->_submitValues['credit_card_type']
141 );
142 }
143 // update the financial trxn
144 civicrm_api3('FinancialTrxn', 'create', $params);
145 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath()));
146 }
147
148 /**
149 * Get payment fields
150 */
151 public function getPaymentFields() {
152 $paymentFields = array();
153 $paymentInstrument = CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $this->_values['payment_instrument_id']);
154 if ($paymentInstrument == 'Check') {
155 $paymentFields['check_number'] = array(
156 'htmlType' => 'text',
157 'name' => 'check_number',
158 'title' => ts('Check Number'),
159 'is_required' => FALSE,
160 'attributes' => NULL,
161 );
162 }
163 elseif ($paymentInstrument == 'Credit Card') {
164 // Ideally we would use $this->addField('card_type_id', array('entity' => 'FinancialTrxn'));
165 // to assign this field (& other fields on this form). However, the addCreditCardJs
166 // adds some 'pretty' to the credit card selection. The 'cost' of this is that because it
167 // was originally written to comply with front end needs (use of the word rather than the id)
168 // we have to do a lot of wrangling with our fields to make the BillingBlock.js code work.
169 // If you are reading this it means it's time to fix the BillingBlock.js to work with
170 // card_type_id and the pseudoconstant options & set this code free. No returns.
171 CRM_Financial_Form_Payment::addCreditCardJs(NULL, 'payment-edit-block');
172 $paymentFields['credit_card_type'] = array(
173 'htmlType' => 'select',
174 'name' => 'credit_card_type',
175 'title' => ts('Card Type'),
176 'attributes' => array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::creditCard(),
177 );
178 $paymentFields['pan_truncation'] = array(
179 'htmlType' => 'text',
180 'name' => 'pan_truncation',
181 'title' => ts('Last 4 digits of the card'),
182 'attributes' => array(
183 'size' => 4,
184 'maxlength' => 4,
185 'minlength' => 4,
186 'autocomplete' => 'off',
187 ),
188 'rules' => array(
189 array(
190 'rule_message' => ts('Please enter valid last 4 digit card number.'),
191 'rule_name' => 'numeric',
192 'rule_parameters' => NULL,
193 ),
194 ),
195 );
196 }
197 $paymentFields += array(
198 'trxn_date' => array(
199 'htmlType' => 'datepicker',
200 'name' => 'trxn_date',
201 'title' => ts('Transaction Date'),
202 'attributes' => array(
203 'date' => 'yyyy-mm-dd',
204 'time' => 24,
205 ),
206 ),
207 'total_amount' => array(
208 'htmlType' => 'text',
209 'name' => 'total_amount',
210 'title' => ts('Total Amount'),
211 'attributes' => array(
212 'readonly' => TRUE,
213 'size' => 6,
214 ),
215 ),
216 );
217
218 return $paymentFields;
219 }
220
221 }