Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Accounting Batch | |
38 | * | |
39 | */ | |
40 | class CRM_Financial_Form_FinancialBatch extends CRM_Contribute_Form { | |
41 | ||
42 | /** | |
43 | * The financial batch id, used when editing the field | |
44 | * | |
45 | * @var int | |
46 | * @access protected | |
47 | */ | |
48 | protected $_id; | |
49 | ||
50 | /** | |
51 | * Function to set variables up before form is built | |
52 | * | |
53 | * @return void | |
54 | * @access public | |
55 | */ | |
56 | public function preProcess() { | |
57 | $context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
58 | $this->set("context", $context); | |
59 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
60 | parent::preProcess(); | |
61 | $session = CRM_Core_Session::singleton(); | |
62 | if ($this->_id) { | |
63 | $permissions = array( | |
64 | CRM_Core_Action::UPDATE => array( | |
65 | 'permission' => array( | |
66 | 'edit own manual batches', | |
67 | 'edit all manual batches', | |
68 | ), | |
69 | 'actionName' => 'edit'), | |
70 | CRM_Core_Action::DELETE => array( | |
71 | 'permission' => array( | |
72 | 'delete own manual batches', | |
73 | 'delete all manual batches', | |
74 | ), | |
75 | 'actionName' => 'delete'), | |
76 | ); | |
77 | ||
78 | $createdID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $this->_id, 'created_id'); | |
a7488080 | 79 | if (!empty($permissions[$this->_action])) { |
6a488035 TO |
80 | $this->checkPermissions($this->_action, $permissions[$this->_action]['permission'], $createdID, $session->get('userID'), $permissions[$this->_action]['actionName'] ); |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | /** | |
86 | * Function to build the form | |
87 | * | |
355ba699 | 88 | * @return void |
6a488035 TO |
89 | * @access public |
90 | */ | |
91 | public function buildQuickForm() { | |
92 | parent::buildQuickForm(); | |
93 | if (isset( $this->_id)) { | |
94 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $this->_id, 'title'); | |
95 | CRM_Utils_System::setTitle($this->_title .' - '.ts( 'Accounting Batch')); | |
96 | $this->assign('batchTitle', $this->_title); | |
97 | $contactID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $this->_id, 'created_id'); | |
98 | $contactName = CRM_Contact_BAO_Contact::displayName($contactID); | |
99 | $this->assign('contactName', $contactName); | |
100 | } | |
101 | ||
102 | $this->applyFilter('__ALL__', 'trim'); | |
103 | ||
104 | $this->addButtons( | |
105 | array( | |
106 | array( | |
107 | 'type' => 'next', | |
108 | 'name' => ts('Save'), | |
109 | 'isDefault' => true, | |
110 | ), | |
111 | array( | |
112 | 'type' => 'next', | |
113 | 'name' => ts('Save and New'), | |
114 | 'subName' => 'new', | |
115 | ), | |
116 | array( | |
117 | 'type' => 'cancel', | |
118 | 'name' => ts('Cancel'), | |
119 | ), | |
120 | ) | |
121 | ); | |
122 | ||
123 | if ($this->_action & CRM_Core_Action::UPDATE && $this->_id) { | |
f0ebbc90 | 124 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id'); |
03e04002 | 125 | |
6a488035 TO |
126 | //unset exported status |
127 | $exportedStatusId = CRM_Utils_Array::key('Exported', $batchStatus ); | |
128 | unset($batchStatus[$exportedStatusId]); | |
129 | $this->add('select', 'status_id', ts('Batch Status'), array('' => ts('- select -')) + $batchStatus, true); | |
130 | $this->freeze(array('status_id')); | |
131 | } | |
132 | ||
133 | $attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch'); | |
134 | ||
135 | $this->add('text', 'title', ts('Batch Name'), $attributes['name'], true); | |
136 | ||
137 | $this->add('textarea', 'description', ts('Description'), $attributes['description']); | |
138 | ||
139 | $this->add('select', 'payment_instrument_id', ts('Payment Instrument'), | |
140 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), | |
141 | false | |
142 | ); | |
143 | ||
144 | $this->add('text', 'total', ts('Total Amount'), $attributes['total']); | |
145 | ||
146 | $this->add('text', 'item_count', ts('Number of Transactions'), $attributes['item_count']); | |
147 | $this->addFormRule(array('CRM_Financial_Form_FinancialBatch', 'formRule'), $this); | |
148 | } | |
149 | ||
150 | /** | |
151 | * This function sets the default values for the form. Note that in edit/view mode | |
152 | * the default values are retrieved from the database | |
153 | * | |
154 | * @access public | |
155 | * | |
156 | * @return void | |
157 | */ | |
158 | function setDefaultValues() { | |
159 | $defaults = parent::setDefaultValues(); | |
160 | ||
161 | if ($this->_id) { | |
162 | $this->assign('modified_date', $defaults['modified_date']); | |
163 | $this->assign('created_date', $defaults['created_date']); | |
164 | } | |
165 | else { | |
166 | // set batch name default | |
167 | $defaults['title'] = CRM_Batch_BAO_Batch::generateBatchName(); | |
168 | } | |
169 | ||
170 | return $defaults; | |
171 | } | |
172 | ||
173 | /** | |
174 | * global validation rules for the form | |
175 | * | |
fd31fa4c EM |
176 | * @param $values |
177 | * @param $files | |
178 | * @param $self | |
179 | * | |
180 | * @internal param array $fields posted values of the form | |
6a488035 TO |
181 | * |
182 | * @return array list of errors to be posted back to the form | |
183 | * @static | |
184 | * @access public | |
185 | */ | |
186 | static function formRule($values, $files, $self) { | |
187 | $errors = array(); | |
a7488080 | 188 | if (!empty($values['contact_name']) && !is_numeric($values['created_id'])) { |
6a488035 TO |
189 | $errors['contact_name'] = ts('Please select a valid contact.'); |
190 | } | |
191 | if ($values['item_count'] && (!is_numeric($values['item_count']) || $values['item_count'] < 1)) { | |
192 | $errors['item_count'] = ts('Number of Transactions should a positive number'); | |
193 | } | |
194 | if ($values['total'] && (!is_numeric($values['total']) || $values['total'] <= 0)) { | |
195 | $errors['total'] = ts('Total Amount should be a positive number'); | |
196 | } | |
a7488080 | 197 | if (!empty($values['created_date']) && date('Y-m-d') < date('Y-m-d', strtotime($values['created_date']))) { |
6a488035 TO |
198 | $errors['created_date'] = ts('Created date cannot be greater than current date'); |
199 | } | |
200 | $batchName = $values['title']; | |
201 | if (!CRM_Core_DAO::objectExists($batchName, 'CRM_Batch_DAO_Batch', $self->_id)) { | |
202 | $errors['title'] = ts('This name already exists in database. Batch names must be unique.'); | |
203 | } | |
204 | return CRM_Utils_Array::crmIsEmptyArray($errors) ? true : $errors; | |
205 | } | |
206 | ||
207 | /** | |
208 | * Function to process the form | |
209 | * | |
210 | * @access public | |
355ba699 | 211 | * @return void |
6a488035 TO |
212 | */ |
213 | public function postProcess() { | |
214 | $session = CRM_Core_Session::singleton(); | |
215 | $ids = array(); | |
216 | $params = $this->exportValues(); | |
ca0641b5 | 217 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id'); |
6a488035 TO |
218 | if ($this->_id) { |
219 | $ids['batchID'] = $this->_id; | |
220 | $params['id'] = $this->_id; | |
221 | } | |
222 | ||
223 | // store the submitted values in an array | |
224 | $params['modified_date'] = date('YmdHis'); | |
225 | $params['modified_id'] = $session->get('userID'); | |
a7488080 | 226 | if (!empty($params['created_date'])) { |
6a488035 TO |
227 | $params['created_date'] = CRM_Utils_Date::processDate($params['created_date']); |
228 | } | |
229 | ||
230 | if ($this->_action & CRM_Core_Action::ADD) { | |
091fe2a8 | 231 | $batchMode = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'mode_id', array('labelColumn' => 'name')); |
6a488035 TO |
232 | $params['mode_id'] = CRM_Utils_Array::key('Manual Batch', $batchMode); |
233 | $params['status_id'] = CRM_Utils_Array::key('Open', $batchStatus); | |
234 | $params['created_date'] = date('YmdHis'); | |
235 | $params['created_id'] = $session->get('userID'); | |
236 | $details = "{$params['title']} batch has been created by this contact."; | |
03e04002 | 237 | $activityTypeName = 'Create Batch'; |
6a488035 TO |
238 | } |
239 | elseif ($this->_action & CRM_Core_Action::UPDATE && $this->_id) { | |
240 | $details = "{$params['title']} batch has been edited by this contact."; | |
241 | if (CRM_Utils_Array::value($params['status_id'], $batchStatus) == 'Closed') { | |
242 | $details = "{$params['title']} batch has been closed by this contact."; | |
243 | } | |
244 | $activityTypeName = 'Edit Batch'; | |
245 | } | |
246 | ||
247 | $batch = CRM_Batch_BAO_Batch::create($params, $ids, 'financialBatch'); | |
248 | ||
249 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); | |
250 | ||
251 | //create activity. | |
252 | $activityParams = array( | |
253 | 'activity_type_id' => array_search($activityTypeName, $activityTypes), | |
254 | 'subject' => $batch->title ."- Batch", | |
255 | 'status_id' => 2, | |
256 | 'priority_id' => 2, | |
257 | 'activity_date_time' => date('YmdHis'), | |
258 | 'source_contact_id' => $session->get('userID'), | |
259 | 'source_contact_qid' => $session->get('userID'), | |
260 | 'details' => $details, | |
261 | ); | |
262 | ||
263 | CRM_Activity_BAO_Activity::create($activityParams); | |
264 | ||
265 | $buttonName = $this->controller->getButtonName(); | |
266 | ||
267 | $context = $this->get("context"); | |
268 | if ($batch->title) { | |
269 | CRM_Core_Session::setStatus(ts("'%1' batch has been saved.", array(1 => $batch->title)), ts('Saved'), 'success'); | |
270 | } | |
271 | if ($buttonName == $this->getButtonName('next', 'new') & $this->_action == CRM_Core_Action::UPDATE) { | |
272 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/batch', | |
273 | "reset=1&action=add&context=1")); | |
274 | } | |
275 | elseif ($buttonName == $this->getButtonName('next', 'new')) { | |
276 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/batch', | |
277 | "reset=1&action=add")); | |
278 | } | |
279 | elseif (CRM_Utils_Array::value($batch->status_id, $batchStatus) == 'Closed') { | |
280 | $session->replaceUserContext(CRM_Utils_System::url('civicrm', 'reset=1')); | |
281 | } | |
282 | elseif (($buttonName == $this->getButtonName('next') & $this->_action == CRM_Core_Action::UPDATE) || | |
283 | ($buttonName == $this->getButtonName('next') & $this->_action == CRM_Core_Action::ADD & $context == 1 )) { | |
284 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches', | |
285 | "reset=1&batchStatus=1")); | |
286 | } | |
287 | else { | |
288 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/batchtransaction', | |
289 | "reset=1&bid={$batch->id}")); | |
290 | } | |
291 | } | |
292 | ||
293 | /** | |
294 | * global validation rules for the form | |
295 | * | |
da6b46f4 EM |
296 | * @param $action |
297 | * @param $permissions | |
298 | * @param $createdID | |
299 | * @param $userContactID | |
300 | * @param $actionName | |
301 | * | |
302 | * @internal param array $fields posted values of the form | |
6a488035 TO |
303 | * |
304 | * @return array list of errors to be posted back to the form | |
305 | * @static | |
306 | * @access public | |
307 | */ | |
308 | function checkPermissions($action, $permissions, $createdID, $userContactID, $actionName) { | |
309 | if ((CRM_Core_Permission::check($permissions[0]) || CRM_Core_Permission::check($permissions[1]))) { | |
310 | if (CRM_Core_Permission::check($permissions[0]) && $userContactID != $createdID && !CRM_Core_Permission::check($permissions[1])) { | |
311 | CRM_Core_Error::statusBounce(ts('You dont have permission to %1 this batch'), array(1 => $actionName)); | |
312 | } | |
313 | } | |
314 | else { | |
315 | CRM_Core_Error::statusBounce(ts('You dont have permission to %1 this batch'), array(1 => $actionName)); | |
316 | } | |
317 | } | |
318 | } | |
319 | ||
320 |