3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for Accounting Batch
40 class CRM_Financial_Form_FinancialBatch
extends CRM_Contribute_Form
{
43 * The financial batch id, used when editing the field
51 * Function to set variables up before form is built
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);
61 $session = CRM_Core_Session
::singleton();
64 CRM_Core_Action
::UPDATE
=> array(
65 'permission' => array(
66 'edit own manual batches',
67 'edit all manual batches',
69 'actionName' => 'edit'),
70 CRM_Core_Action
::DELETE
=> array(
71 'permission' => array(
72 'delete own manual batches',
73 'delete all manual batches',
75 'actionName' => 'delete'),
78 $createdID = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $this->_id
, 'created_id');
79 if (!empty($permissions[$this->_action
])) {
80 $this->checkPermissions($this->_action
, $permissions[$this->_action
]['permission'], $createdID, $session->get('userID'), $permissions[$this->_action
]['actionName'] );
86 * Function to build the form
91 public function buildQuickForm() {
92 parent
::buildQuickForm();
93 $this->setPageTitle(ts('Financial Batch'));
95 if (isset( $this->_id
)) {
96 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $this->_id
, 'title');
97 CRM_Utils_System
::setTitle($this->_title
.' - '.ts( 'Accounting Batch'));
98 $this->assign('batchTitle', $this->_title
);
99 $contactID = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $this->_id
, 'created_id');
100 $contactName = CRM_Contact_BAO_Contact
::displayName($contactID);
101 $this->assign('contactName', $contactName);
104 $this->applyFilter('__ALL__', 'trim');
110 'name' => ts('Save'),
115 'name' => ts('Save and New'),
120 'name' => ts('Cancel'),
125 if ($this->_action
& CRM_Core_Action
::UPDATE
&& $this->_id
) {
126 $batchStatus = CRM_Core_PseudoConstant
::get('CRM_Batch_DAO_Batch', 'status_id');
128 //unset exported status
129 $exportedStatusId = CRM_Utils_Array
::key('Exported', $batchStatus );
130 unset($batchStatus[$exportedStatusId]);
131 $this->add('select', 'status_id', ts('Batch Status'), array('' => ts('- select -')) +
$batchStatus, true);
132 $this->freeze(array('status_id'));
135 $attributes = CRM_Core_DAO
::getAttribute('CRM_Batch_DAO_Batch');
137 $this->add('text', 'title', ts('Batch Name'), $attributes['name'], true);
139 $this->add('textarea', 'description', ts('Description'), $attributes['description']);
141 $this->add('select', 'payment_instrument_id', ts('Payment Instrument'),
142 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
146 $this->add('text', 'total', ts('Total Amount'), $attributes['total']);
148 $this->add('text', 'item_count', ts('Number of Transactions'), $attributes['item_count']);
149 $this->addFormRule(array('CRM_Financial_Form_FinancialBatch', 'formRule'), $this);
153 * This function sets the default values for the form. Note that in edit/view mode
154 * the default values are retrieved from the database
160 function setDefaultValues() {
161 $defaults = parent
::setDefaultValues();
164 $this->assign('modified_date', $defaults['modified_date']);
165 $this->assign('created_date', $defaults['created_date']);
168 // set batch name default
169 $defaults['title'] = CRM_Batch_BAO_Batch
::generateBatchName();
176 * global validation rules for the form
182 * @internal param array $fields posted values of the form
184 * @return array list of errors to be posted back to the form
188 static function formRule($values, $files, $self) {
190 if (!empty($values['contact_name']) && !is_numeric($values['created_id'])) {
191 $errors['contact_name'] = ts('Please select a valid contact.');
193 if ($values['item_count'] && (!is_numeric($values['item_count']) ||
$values['item_count'] < 1)) {
194 $errors['item_count'] = ts('Number of Transactions should a positive number');
196 if ($values['total'] && (!is_numeric($values['total']) ||
$values['total'] <= 0)) {
197 $errors['total'] = ts('Total Amount should be a positive number');
199 if (!empty($values['created_date']) && date('Y-m-d') < date('Y-m-d', strtotime($values['created_date']))) {
200 $errors['created_date'] = ts('Created date cannot be greater than current date');
202 $batchName = $values['title'];
203 if (!CRM_Core_DAO
::objectExists($batchName, 'CRM_Batch_DAO_Batch', $self->_id
)) {
204 $errors['title'] = ts('This name already exists in database. Batch names must be unique.');
206 return CRM_Utils_Array
::crmIsEmptyArray($errors) ?
true : $errors;
210 * Function to process the form
215 public function postProcess() {
216 $session = CRM_Core_Session
::singleton();
218 $params = $this->exportValues();
219 $batchStatus = CRM_Core_PseudoConstant
::get('CRM_Batch_DAO_Batch', 'status_id');
221 $ids['batchID'] = $this->_id
;
222 $params['id'] = $this->_id
;
225 // store the submitted values in an array
226 $params['modified_date'] = date('YmdHis');
227 $params['modified_id'] = $session->get('userID');
228 if (!empty($params['created_date'])) {
229 $params['created_date'] = CRM_Utils_Date
::processDate($params['created_date']);
232 if ($this->_action
& CRM_Core_Action
::ADD
) {
233 $batchMode = CRM_Core_PseudoConstant
::get('CRM_Batch_DAO_Batch', 'mode_id', array('labelColumn' => 'name'));
234 $params['mode_id'] = CRM_Utils_Array
::key('Manual Batch', $batchMode);
235 $params['status_id'] = CRM_Utils_Array
::key('Open', $batchStatus);
236 $params['created_date'] = date('YmdHis');
237 $params['created_id'] = $session->get('userID');
238 $details = "{$params['title']} batch has been created by this contact.";
239 $activityTypeName = 'Create Batch';
241 elseif ($this->_action
& CRM_Core_Action
::UPDATE
&& $this->_id
) {
242 $details = "{$params['title']} batch has been edited by this contact.";
243 if (CRM_Utils_Array
::value($params['status_id'], $batchStatus) == 'Closed') {
244 $details = "{$params['title']} batch has been closed by this contact.";
246 $activityTypeName = 'Edit Batch';
249 $batch = CRM_Batch_BAO_Batch
::create($params, $ids, 'financialBatch');
251 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, FALSE, FALSE, 'name');
254 $activityParams = array(
255 'activity_type_id' => array_search($activityTypeName, $activityTypes),
256 'subject' => $batch->title
."- Batch",
259 'activity_date_time' => date('YmdHis'),
260 'source_contact_id' => $session->get('userID'),
261 'source_contact_qid' => $session->get('userID'),
262 'details' => $details,
265 CRM_Activity_BAO_Activity
::create($activityParams);
267 $buttonName = $this->controller
->getButtonName();
269 $context = $this->get("context");
271 CRM_Core_Session
::setStatus(ts("'%1' batch has been saved.", array(1 => $batch->title
)), ts('Saved'), 'success');
273 if ($buttonName == $this->getButtonName('next', 'new') & $this->_action
== CRM_Core_Action
::UPDATE
) {
274 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/batch',
275 "reset=1&action=add&context=1"));
277 elseif ($buttonName == $this->getButtonName('next', 'new')) {
278 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/batch',
279 "reset=1&action=add"));
281 elseif (CRM_Utils_Array
::value($batch->status_id
, $batchStatus) == 'Closed') {
282 $session->replaceUserContext(CRM_Utils_System
::url('civicrm', 'reset=1'));
284 elseif (($buttonName == $this->getButtonName('next') & $this->_action
== CRM_Core_Action
::UPDATE
) ||
285 ($buttonName == $this->getButtonName('next') & $this->_action
== CRM_Core_Action
::ADD
& $context == 1 )) {
286 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/financialbatches',
287 "reset=1&batchStatus=1"));
290 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/batchtransaction',
291 "reset=1&bid={$batch->id}"));
296 * global validation rules for the form
299 * @param $permissions
301 * @param $userContactID
304 * @internal param array $fields posted values of the form
306 * @return array list of errors to be posted back to the form
310 function checkPermissions($action, $permissions, $createdID, $userContactID, $actionName) {
311 if ((CRM_Core_Permission
::check($permissions[0]) || CRM_Core_Permission
::check($permissions[1]))) {
312 if (CRM_Core_Permission
::check($permissions[0]) && $userContactID != $createdID && !CRM_Core_Permission
::check($permissions[1])) {
313 CRM_Core_Error
::statusBounce(ts('You dont have permission to %1 this batch'), array(1 => $actionName));
317 CRM_Core_Error
::statusBounce(ts('You dont have permission to %1 this batch'), array(1 => $actionName));