Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | |
6a488035 TO |
46 | */ |
47 | protected $_id; | |
48 | ||
49 | /** | |
100fef9d | 50 | * Set variables up before form is built |
6a488035 TO |
51 | * |
52 | * @return void | |
6a488035 TO |
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'); | |
a7488080 | 77 | if (!empty($permissions[$this->_action])) { |
481a74f4 | 78 | $this->checkPermissions($this->_action, $permissions[$this->_action]['permission'], $createdID, $session->get('userID'), $permissions[$this->_action]['actionName']); |
6a488035 TO |
79 | } |
80 | } | |
81 | } | |
82 | ||
83 | /** | |
c490a46a | 84 | * Build the form object |
6a488035 | 85 | * |
355ba699 | 86 | * @return void |
6a488035 TO |
87 | */ |
88 | public function buildQuickForm() { | |
89 | parent::buildQuickForm(); | |
e2046b33 CW |
90 | $this->setPageTitle(ts('Financial Batch')); |
91 | ||
481a74f4 | 92 | if (isset($this->_id)) { |
6a488035 | 93 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $this->_id, 'title'); |
86bfa4f6 | 94 | CRM_Utils_System::setTitle($this->_title . ' - ' . ts('Accounting Batch')); |
6a488035 TO |
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'), | |
045f52a3 | 108 | 'isDefault' => TRUE, |
6a488035 TO |
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) { | |
f0ebbc90 | 123 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id'); |
03e04002 | 124 | |
6a488035 | 125 | //unset exported status |
481a74f4 | 126 | $exportedStatusId = CRM_Utils_Array::key('Exported', $batchStatus); |
6a488035 | 127 | unset($batchStatus[$exportedStatusId]); |
045f52a3 | 128 | $this->add('select', 'status_id', ts('Batch Status'), array('' => ts('- select -')) + $batchStatus, TRUE); |
6a488035 TO |
129 | $this->freeze(array('status_id')); |
130 | } | |
131 | ||
132 | $attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch'); | |
133 | ||
045f52a3 | 134 | $this->add('text', 'title', ts('Batch Name'), $attributes['name'], TRUE); |
6a488035 TO |
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(), | |
045f52a3 | 140 | FALSE |
6a488035 TO |
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 | /** | |
c490a46a | 150 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
151 | * the default values are retrieved from the database |
152 | * | |
6a488035 TO |
153 | * |
154 | * @return void | |
155 | */ | |
00be9182 | 156 | public function setDefaultValues() { |
6a488035 TO |
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 | /** | |
100fef9d | 172 | * Global validation rules for the form |
6a488035 | 173 | * |
c490a46a | 174 | * @param array $values |
fd31fa4c EM |
175 | * @param $files |
176 | * @param $self | |
177 | * | |
6a488035 TO |
178 | * @return array list of errors to be posted back to the form |
179 | * @static | |
6a488035 | 180 | */ |
00be9182 | 181 | public static function formRule($values, $files, $self) { |
6a488035 | 182 | $errors = array(); |
a7488080 | 183 | if (!empty($values['contact_name']) && !is_numeric($values['created_id'])) { |
6a488035 TO |
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 | } | |
a7488080 | 192 | if (!empty($values['created_date']) && date('Y-m-d') < date('Y-m-d', strtotime($values['created_date']))) { |
6a488035 TO |
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 | } | |
045f52a3 | 199 | return CRM_Utils_Array::crmIsEmptyArray($errors) ? TRUE : $errors; |
6a488035 TO |
200 | } |
201 | ||
202 | /** | |
c490a46a | 203 | * Process the form submission |
6a488035 | 204 | * |
355ba699 | 205 | * @return void |
6a488035 TO |
206 | */ |
207 | public function postProcess() { | |
208 | $session = CRM_Core_Session::singleton(); | |
209 | $ids = array(); | |
210 | $params = $this->exportValues(); | |
ca0641b5 | 211 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id'); |
6a488035 TO |
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'); | |
a7488080 | 220 | if (!empty($params['created_date'])) { |
6a488035 TO |
221 | $params['created_date'] = CRM_Utils_Date::processDate($params['created_date']); |
222 | } | |
223 | ||
224 | if ($this->_action & CRM_Core_Action::ADD) { | |
091fe2a8 | 225 | $batchMode = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'mode_id', array('labelColumn' => 'name')); |
6a488035 TO |
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."; | |
03e04002 | 231 | $activityTypeName = 'Create Batch'; |
6a488035 TO |
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), | |
86bfa4f6 | 248 | 'subject' => $batch->title . "- Batch", |
6a488035 TO |
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) || | |
481a74f4 | 277 | ($buttonName == $this->getButtonName('next') & $this->_action == CRM_Core_Action::ADD & $context == 1)) { |
6a488035 TO |
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 | /** | |
100fef9d | 288 | * Global validation rules for the form |
6a488035 | 289 | * |
da6b46f4 EM |
290 | * @param $action |
291 | * @param $permissions | |
100fef9d CW |
292 | * @param int $createdID |
293 | * @param int $userContactID | |
294 | * @param string $actionName | |
da6b46f4 | 295 | * |
6a488035 TO |
296 | * @return array list of errors to be posted back to the form |
297 | * @static | |
6a488035 | 298 | */ |
00be9182 | 299 | public function checkPermissions($action, $permissions, $createdID, $userContactID, $actionName) { |
6a488035 TO |
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 | } | |
e2046b33 | 309 | |
6a488035 | 310 | } |