3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
50 * Set variables up before form is built
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);
59 $session = CRM_Core_Session
::singleton();
62 CRM_Core_Action
::UPDATE
=> array(
63 'permission' => array(
64 'edit own manual batches',
65 'edit all manual batches',
67 'actionName' => 'edit'),
68 CRM_Core_Action
::DELETE
=> array(
69 'permission' => array(
70 'delete own manual batches',
71 'delete all manual batches',
73 'actionName' => 'delete'),
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']);
84 * Build the form object
88 public function buildQuickForm() {
89 parent
::buildQuickForm();
90 $this->setPageTitle(ts('Financial Batch'));
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);
101 $this->applyFilter('__ALL__', 'trim');
107 'name' => ts('Save'),
112 'name' => ts('Save and New'),
117 'name' => ts('Cancel'),
122 if ($this->_action
& CRM_Core_Action
::UPDATE
&& $this->_id
) {
123 $batchStatus = CRM_Core_PseudoConstant
::get('CRM_Batch_DAO_Batch', 'status_id');
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'));
132 $attributes = CRM_Core_DAO
::getAttribute('CRM_Batch_DAO_Batch');
134 $this->add('text', 'title', ts('Batch Name'), $attributes['name'], TRUE);
136 $this->add('textarea', 'description', ts('Description'), $attributes['description']);
138 $this->add('select', 'payment_instrument_id', ts('Payment Instrument'),
139 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
143 $this->add('text', 'total', ts('Total Amount'), $attributes['total']);
145 $this->add('text', 'item_count', ts('Number of Transactions'), $attributes['item_count']);
146 $this->addFormRule(array('CRM_Financial_Form_FinancialBatch', 'formRule'), $this);
150 * Set default values for the form. Note that in edit/view mode
151 * the default values are retrieved from the database
156 public function setDefaultValues() {
157 $defaults = parent
::setDefaultValues();
160 $this->assign('modified_date', $defaults['modified_date']);
161 $this->assign('created_date', $defaults['created_date']);
164 // set batch name default
165 $defaults['title'] = CRM_Batch_BAO_Batch
::generateBatchName();
172 * Global validation rules for the form
174 * @param array $values
178 * @return array list of errors to be posted back to the form
181 public static function formRule($values, $files, $self) {
183 if (!empty($values['contact_name']) && !is_numeric($values['created_id'])) {
184 $errors['contact_name'] = ts('Please select a valid contact.');
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');
189 if ($values['total'] && (!is_numeric($values['total']) ||
$values['total'] <= 0)) {
190 $errors['total'] = ts('Total Amount should be a positive number');
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');
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.');
199 return CRM_Utils_Array
::crmIsEmptyArray($errors) ?
TRUE : $errors;
203 * Process the form submission
207 public function postProcess() {
208 $session = CRM_Core_Session
::singleton();
210 $params = $this->exportValues();
211 $batchStatus = CRM_Core_PseudoConstant
::get('CRM_Batch_DAO_Batch', 'status_id');
213 $ids['batchID'] = $this->_id
;
214 $params['id'] = $this->_id
;
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']);
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';
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.";
238 $activityTypeName = 'Edit Batch';
241 $batch = CRM_Batch_BAO_Batch
::create($params, $ids, 'financialBatch');
243 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, FALSE, FALSE, 'name');
246 $activityParams = array(
247 'activity_type_id' => array_search($activityTypeName, $activityTypes),
248 'subject' => $batch->title
. "- Batch",
251 'activity_date_time' => date('YmdHis'),
252 'source_contact_id' => $session->get('userID'),
253 'source_contact_qid' => $session->get('userID'),
254 'details' => $details,
257 CRM_Activity_BAO_Activity
::create($activityParams);
259 $buttonName = $this->controller
->getButtonName();
261 $context = $this->get("context");
263 CRM_Core_Session
::setStatus(ts("'%1' batch has been saved.", array(1 => $batch->title
)), ts('Saved'), 'success');
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"));
269 elseif ($buttonName == $this->getButtonName('next', 'new')) {
270 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/batch',
271 "reset=1&action=add"));
273 elseif (CRM_Utils_Array
::value($batch->status_id
, $batchStatus) == 'Closed') {
274 $session->replaceUserContext(CRM_Utils_System
::url('civicrm', 'reset=1'));
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"));
282 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/batchtransaction',
283 "reset=1&bid={$batch->id}"));
288 * Global validation rules for the form
291 * @param $permissions
292 * @param int $createdID
293 * @param int $userContactID
294 * @param string $actionName
296 * @return array list of errors to be posted back to the form
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));
306 CRM_Core_Error
::statusBounce(ts('You dont have permission to %1 this batch'), array(1 => $actionName));