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