3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Accounting Batch
21 class CRM_Financial_Form_FinancialBatch
extends CRM_Contribute_Form
{
24 * The financial batch id, used when editing the field
31 * Set variables up before form is built.
33 public function preProcess() {
34 $context = CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
35 $this->set("context", $context);
36 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
38 $session = CRM_Core_Session
::singleton();
41 CRM_Core_Action
::UPDATE
=> [
43 'edit own manual batches',
44 'edit all manual batches',
46 'actionName' => 'edit',
48 CRM_Core_Action
::DELETE
=> [
50 'delete own manual batches',
51 'delete all manual batches',
53 'actionName' => 'delete',
57 $createdID = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $this->_id
, 'created_id');
58 if (!empty($permissions[$this->_action
])) {
59 $this->checkPermissions($this->_action
, $permissions[$this->_action
]['permission'], $createdID, $session->get('userID'), $permissions[$this->_action
]['actionName']);
65 * Build the form object.
67 public function buildQuickForm() {
68 parent
::buildQuickForm();
69 $this->setPageTitle(ts('Financial Batch'));
70 if (!empty($this->_id
)) {
71 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $this->_id
, 'title');
72 CRM_Utils_System
::setTitle($this->_title
. ' - ' . ts('Accounting Batch'));
73 $this->assign('batchTitle', $this->_title
);
74 $contactID = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $this->_id
, 'created_id');
75 $contactName = CRM_Contact_BAO_Contact
::displayName($contactID);
76 $this->assign('contactName', $contactName);
79 $this->applyFilter('__ALL__', 'trim');
90 'name' => ts('Save and New'),
95 'name' => ts('Cancel'),
100 if ($this->_action
& CRM_Core_Action
::UPDATE
&& $this->_id
) {
101 $batchStatus = CRM_Core_PseudoConstant
::get('CRM_Batch_BAO_Batch', 'status_id');
103 // unset exported status
104 $exportedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Exported');
105 unset($batchStatus[$exportedStatusId]);
106 $this->add('select', 'status_id', ts('Batch Status'), ['' => ts('- select -')] +
$batchStatus, TRUE);
107 $this->freeze(['status_id']);
110 $attributes = CRM_Core_DAO
::getAttribute('CRM_Batch_DAO_Batch');
112 $this->add('text', 'title', ts('Batch Name'), $attributes['name'], TRUE);
114 $this->add('textarea', 'description', ts('Description'), $attributes['description']);
116 $this->add('select', 'payment_instrument_id', ts('Payment Method'),
117 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant
::paymentInstrument(),
121 $this->add('text', 'total', ts('Total Amount'), $attributes['total']);
123 $this->add('text', 'item_count', ts('Number of Transactions'), $attributes['item_count']);
124 $this->addFormRule(['CRM_Financial_Form_FinancialBatch', 'formRule'], $this);
128 * Set default values for the form. Note that in edit/view mode
129 * the default values are retrieved from the database.
131 public function setDefaultValues() {
132 $defaults = parent
::setDefaultValues();
135 $this->assign('modified_date', $defaults['modified_date']);
136 $this->assign('created_date', $defaults['created_date']);
139 // set batch name default
140 $defaults['title'] = CRM_Batch_BAO_Batch
::generateBatchName();
147 * Global validation rules for the form.
149 * @param array $values
154 * list of errors to be posted back to the form
156 public static function formRule($values, $files, $self) {
158 if (!empty($values['contact_name']) && !is_numeric($values['created_id'])) {
159 $errors['contact_name'] = ts('Please select a valid contact.');
161 if ($values['item_count'] && (!is_numeric($values['item_count']) ||
$values['item_count'] < 1)) {
162 $errors['item_count'] = ts('Number of Transactions should a positive number');
164 if ($values['total'] && (!is_numeric($values['total']) ||
$values['total'] <= 0)) {
165 $errors['total'] = ts('Total Amount should be a positive number');
167 if (!empty($values['created_date']) && date('Y-m-d') < date('Y-m-d', strtotime($values['created_date']))) {
168 $errors['created_date'] = ts('Created date cannot be greater than current date');
170 $batchName = $values['title'];
171 if (!CRM_Core_DAO
::objectExists($batchName, 'CRM_Batch_DAO_Batch', $self->_id
)) {
172 $errors['title'] = ts('This name already exists in database. Batch names must be unique.');
174 return CRM_Utils_Array
::crmIsEmptyArray($errors) ?
TRUE : $errors;
178 * Process the form submission.
180 public function postProcess() {
181 $session = CRM_Core_Session
::singleton();
182 $params = $this->exportValues();
183 $closedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Closed');
185 $params['id'] = $this->_id
;
188 // store the submitted values in an array
189 $params['modified_date'] = date('YmdHis');
190 $params['modified_id'] = $session->get('userID');
191 if (!empty($params['created_date'])) {
192 $params['created_date'] = CRM_Utils_Date
::processDate($params['created_date']);
195 if ($this->_action
& CRM_Core_Action
::ADD
) {
196 $params['mode_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Batch_BAO_Batch', 'mode_id', 'Manual Batch');
197 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Open');
198 $params['created_date'] = date('YmdHis');
199 if (empty($params['created_id'])) {
200 $params['created_id'] = $session->get('userID');
202 $details = "{$params['title']} batch has been created by this contact.";
203 $activityTypeName = 'Create Batch';
205 elseif ($this->_action
& CRM_Core_Action
::UPDATE
&& $this->_id
) {
206 $details = "{$params['title']} batch has been edited by this contact.";
207 if ($params['status_id'] === $closedStatusId) {
208 $details = "{$params['title']} batch has been closed by this contact.";
210 $activityTypeName = 'Edit Batch';
213 // FIXME: What happens if we get to here and no activityType is defined?
215 $batch = CRM_Batch_BAO_Batch
::create($params);
218 $this->_id
= $batch->id
;
222 // activityTypeName - dev/core#1116-unknown-if-ok
223 'activity_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_DAO_Activity', 'activity_type_id', $activityTypeName),
224 'subject' => $batch->title
. "- Batch",
225 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_DAO_Activity', 'activity_status_id', 'Completed'),
226 'priority_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_DAO_Activity', 'priority_id', 'Normal'),
227 'activity_date_time' => date('YmdHis'),
228 'source_contact_id' => $session->get('userID'),
229 'source_contact_qid' => $session->get('userID'),
230 'details' => $details,
233 CRM_Activity_BAO_Activity
::create($activityParams);
235 $buttonName = $this->controller
->getButtonName();
237 $context = $this->get("context");
239 CRM_Core_Session
::setStatus(ts("'%1' batch has been saved.", [1 => $batch->title
]), ts('Saved'), 'success');
241 if ($buttonName == $this->getButtonName('next', 'new') & $this->_action
== CRM_Core_Action
::UPDATE
) {
242 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/batch',
243 "reset=1&action=add&context=1"));
245 elseif ($buttonName == $this->getButtonName('next', 'new')) {
246 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/batch',
247 "reset=1&action=add"));
249 elseif ($batch->status_id
=== $closedStatusId) {
250 $session->replaceUserContext(CRM_Utils_System
::url('civicrm', 'reset=1'));
252 elseif (($buttonName == $this->getButtonName('next') & $this->_action
== CRM_Core_Action
::UPDATE
) ||
253 ($buttonName == $this->getButtonName('next') & $this->_action
== CRM_Core_Action
::ADD
& $context == 1)
255 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/financial/financialbatches',
256 "reset=1&batchStatus=1"));
259 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/batchtransaction',
260 "reset=1&bid={$batch->id}"));
265 * Global validation rules for the form.
268 * @param $permissions
269 * @param int $createdID
270 * @param int $userContactID
271 * @param string $actionName
273 * list of errors to be posted back to the form
275 public function checkPermissions($action, $permissions, $createdID, $userContactID, $actionName) {
276 if ((CRM_Core_Permission
::check($permissions[0]) || CRM_Core_Permission
::check($permissions[1]))) {
277 if (CRM_Core_Permission
::check($permissions[0]) && $userContactID != $createdID && !CRM_Core_Permission
::check($permissions[1])) {
278 CRM_Core_Error
::statusBounce(ts('You dont have permission to %1 this batch'), [1 => $actionName]);
282 CRM_Core_Error
::statusBounce(ts('You dont have permission to %1 this batch'), [1 => $actionName]);