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