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