3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
35 * This class generates form components for Accounting Batch
37 class CRM_Financial_Form_FinancialBatch
extends CRM_Contribute_Form
{
40 * The financial batch id, used when editing the field
47 * Set variables up before form is built.
49 public function preProcess() {
50 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
51 $this->set("context", $context);
52 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
54 $session = CRM_Core_Session
::singleton();
57 CRM_Core_Action
::UPDATE
=> array(
58 'permission' => array(
59 'edit own manual batches',
60 'edit all manual batches',
62 'actionName' => 'edit',
64 CRM_Core_Action
::DELETE
=> array(
65 'permission' => array(
66 'delete own manual batches',
67 'delete all manual batches',
69 'actionName' => 'delete',
73 $createdID = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $this->_id
, 'created_id');
74 if (!empty($permissions[$this->_action
])) {
75 $this->checkPermissions($this->_action
, $permissions[$this->_action
]['permission'], $createdID, $session->get('userID'), $permissions[$this->_action
]['actionName']);
81 * Build the form object.
83 public function buildQuickForm() {
84 parent
::buildQuickForm();
85 $this->setPageTitle(ts('Financial Batch'));
86 if (!empty($this->_id
)) {
87 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $this->_id
, 'title');
88 CRM_Utils_System
::setTitle($this->_title
. ' - ' . ts('Accounting Batch'));
89 $this->assign('batchTitle', $this->_title
);
90 $contactID = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $this->_id
, 'created_id');
91 $contactName = CRM_Contact_BAO_Contact
::displayName($contactID);
92 $this->assign('contactName', $contactName);
95 $this->applyFilter('__ALL__', 'trim');
101 'name' => ts('Save'),
106 'name' => ts('Save and New'),
111 'name' => ts('Cancel'),
116 if ($this->_action
& CRM_Core_Action
::UPDATE
&& $this->_id
) {
117 $batchStatus = CRM_Core_PseudoConstant
::get('CRM_Batch_BAO_Batch', 'status_id');
119 // unset exported status
120 $exportedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Exported');
121 unset($batchStatus[$exportedStatusId]);
122 $this->add('select', 'status_id', ts('Batch Status'), array('' => ts('- select -')) +
$batchStatus, TRUE);
123 $this->freeze(array('status_id'));
126 $attributes = CRM_Core_DAO
::getAttribute('CRM_Batch_DAO_Batch');
128 $this->add('text', 'title', ts('Batch Name'), $attributes['name'], TRUE);
130 $this->add('textarea', 'description', ts('Description'), $attributes['description']);
132 $this->add('select', 'payment_instrument_id', ts('Payment Method'),
133 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
137 $this->add('text', 'total', ts('Total Amount'), $attributes['total']);
139 $this->add('text', 'item_count', ts('Number of Transactions'), $attributes['item_count']);
140 $this->addFormRule(array('CRM_Financial_Form_FinancialBatch', 'formRule'), $this);
144 * Set default values for the form. Note that in edit/view mode
145 * the default values are retrieved from the database.
147 public function setDefaultValues() {
148 $defaults = parent
::setDefaultValues();
151 $this->assign('modified_date', $defaults['modified_date']);
152 $this->assign('created_date', $defaults['created_date']);
155 // set batch name default
156 $defaults['title'] = CRM_Batch_BAO_Batch
::generateBatchName();
163 * Global validation rules for the form.
165 * @param array $values
170 * list of errors to be posted back to the form
172 public static function formRule($values, $files, $self) {
174 if (!empty($values['contact_name']) && !is_numeric($values['created_id'])) {
175 $errors['contact_name'] = ts('Please select a valid contact.');
177 if ($values['item_count'] && (!is_numeric($values['item_count']) ||
$values['item_count'] < 1)) {
178 $errors['item_count'] = ts('Number of Transactions should a positive number');
180 if ($values['total'] && (!is_numeric($values['total']) ||
$values['total'] <= 0)) {
181 $errors['total'] = ts('Total Amount should be a positive number');
183 if (!empty($values['created_date']) && date('Y-m-d') < date('Y-m-d', strtotime($values['created_date']))) {
184 $errors['created_date'] = ts('Created date cannot be greater than current date');
186 $batchName = $values['title'];
187 if (!CRM_Core_DAO
::objectExists($batchName, 'CRM_Batch_DAO_Batch', $self->_id
)) {
188 $errors['title'] = ts('This name already exists in database. Batch names must be unique.');
190 return CRM_Utils_Array
::crmIsEmptyArray($errors) ?
TRUE : $errors;
194 * Process the form submission.
196 public function postProcess() {
197 $session = CRM_Core_Session
::singleton();
198 $params = $this->exportValues();
199 $closedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Closed');
201 $params['id'] = $this->_id
;
204 // store the submitted values in an array
205 $params['modified_date'] = date('YmdHis');
206 $params['modified_id'] = $session->get('userID');
207 if (!empty($params['created_date'])) {
208 $params['created_date'] = CRM_Utils_Date
::processDate($params['created_date']);
211 if ($this->_action
& CRM_Core_Action
::ADD
) {
212 $params['mode_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Batch_BAO_Batch', 'mode_id', 'Manual Batch');
213 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Open');
214 $params['created_date'] = date('YmdHis');
215 if (empty($params['created_id'])) {
216 $params['created_id'] = $session->get('userID');
218 $details = "{$params['title']} batch has been created by this contact.";
219 $activityTypeName = 'Create Batch';
221 elseif ($this->_action
& CRM_Core_Action
::UPDATE
&& $this->_id
) {
222 $details = "{$params['title']} batch has been edited by this contact.";
223 if ($params['status_id'] === $closedStatusId) {
224 $details = "{$params['title']} batch has been closed by this contact.";
226 $activityTypeName = 'Edit Batch';
229 // FIXME: What happens if we get to here and no activityType is defined?
231 $batch = CRM_Batch_BAO_Batch
::create($params);
234 $this->_id
= $batch->id
;
237 $activityParams = array(
238 'activity_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_DAO_Activity', 'activity_type_id', $activityTypeName),
239 'subject' => $batch->title
. "- Batch",
240 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_DAO_Activity', 'activity_status_id', 'Completed'),
241 'priority_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_DAO_Activity', 'priority_id', 'Normal'),
242 'activity_date_time' => date('YmdHis'),
243 'source_contact_id' => $session->get('userID'),
244 'source_contact_qid' => $session->get('userID'),
245 'details' => $details,
248 CRM_Activity_BAO_Activity
::create($activityParams);
250 $buttonName = $this->controller
->getButtonName();
252 $context = $this->get("context");
254 CRM_Core_Session
::setStatus(ts("'%1' batch has been saved.", array(1 => $batch->title
)), ts('Saved'), 'success');
256 if ($buttonName == $this->getButtonName('next', 'new') & $this->_action
== CRM_Core_Action
::UPDATE
) {
257 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/batch',
258 "reset=1&action=add&context=1"));
260 elseif ($buttonName == $this->getButtonName('next', 'new')) {
261 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/batch',
262 "reset=1&action=add"));
264 elseif ($batch->status_id
=== $closedStatusId) {
265 $session->replaceUserContext(CRM_Utils_System
::url('civicrm', 'reset=1'));
267 elseif (($buttonName == $this->getButtonName('next') & $this->_action
== CRM_Core_Action
::UPDATE
) ||
268 ($buttonName == $this->getButtonName('next') & $this->_action
== CRM_Core_Action
::ADD
& $context == 1)
270 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/financialbatches',
271 "reset=1&batchStatus=1"));
274 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/batchtransaction',
275 "reset=1&bid={$batch->id}"));
280 * Global validation rules for the form.
283 * @param $permissions
284 * @param int $createdID
285 * @param int $userContactID
286 * @param string $actionName
288 * list of errors to be posted back to the form
290 public function checkPermissions($action, $permissions, $createdID, $userContactID, $actionName) {
291 if ((CRM_Core_Permission
::check($permissions[0]) || CRM_Core_Permission
::check($permissions[1]))) {
292 if (CRM_Core_Permission
::check($permissions[0]) && $userContactID != $createdID && !CRM_Core_Permission
::check($permissions[1])) {
293 CRM_Core_Error
::statusBounce(ts('You dont have permission to %1 this batch'), array(1 => $actionName));
297 CRM_Core_Error
::statusBounce(ts('You dont have permission to %1 this batch'), array(1 => $actionName));