Revert "Schema - Fix boolean fields in various tables"
[civicrm-core.git] / CRM / Financial / Form / BatchTransaction.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for Financial Type
20 */
21 class CRM_Financial_Form_BatchTransaction extends CRM_Contribute_Form_Search {
22 public static $_links = NULL;
23 public static $_entityID;
24
25 /**
26 * Batch status.
27 * @var int
28 */
29 protected $_batchStatusId;
30
31 /**
32 * Batch status name.
33 *
34 * @var string
35 */
36 protected $_batchStatus = 'open';
37
38 public function preProcess() {
39 // This reuses some styles from search forms
40 CRM_Core_Resources::singleton()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
41 self::$_entityID = CRM_Utils_Request::retrieve('bid', 'Positive') ? CRM_Utils_Request::retrieve('bid', 'Positive') : CRM_Utils_Array::value('batch_id', $_POST);
42 $this->assign('entityID', self::$_entityID);
43 if (isset(self::$_entityID)) {
44 $this->_batchStatusId = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'status_id');
45 $batchStatuses = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', ['labelColumn' => 'name', 'condition' => " v.value={$this->_batchStatusId}"]);
46 $this->_batchStatus = $batchStatuses[$this->_batchStatusId];
47 $this->assign('statusID', $this->_batchStatusId);
48 $validStatus = FALSE;
49 if (in_array($this->_batchStatus, ['Open', 'Reopened'])) {
50 $validStatus = TRUE;
51 }
52 $this->assign('validStatus', $validStatus);
53 $this->_values = civicrm_api3('Batch', 'getSingle', ['id' => self::$_entityID]);
54 $batchTitle = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'title');
55 $this->setTitle(ts('Accounting Batch - %1', [1 => $batchTitle]));
56
57 $columnHeaders = [
58 'created_by' => ts('Created By'),
59 'status' => ts('Status'),
60 'description' => ts('Description'),
61 'payment_instrument' => ts('Payment Method'),
62 'item_count' => ts('Expected Number of Items'),
63 'assigned_item_count' => ts('Actual Number of Items'),
64 'total' => ts('Expected Total Amount'),
65 'assigned_total' => ts('Actual Total Amount'),
66 'opened_date' => ts('Opened'),
67 ];
68 $this->assign('columnHeaders', $columnHeaders);
69 }
70 $this->assign('batchStatus', $this->_batchStatus);
71 }
72
73 /**
74 * Build the form object.
75 */
76 public function buildQuickForm() {
77 if ($this->_batchStatus == 'Closed') {
78 $this->add('xbutton', 'export_batch', ts('Export Batch'), ['type' => 'submit']);
79 }
80
81 // do not build rest of form unless it is open/reopened batch
82 if (!in_array($this->_batchStatus, ['Open', 'Reopened'])) {
83 return;
84 }
85
86 parent::buildQuickForm();
87 if (CRM_Batch_BAO_Batch::checkBatchPermission('close', $this->_values['created_id'])) {
88 $this->add('xbutton', 'close_batch', ts('Close Batch'), ['type' => 'submit']);
89 if (CRM_Batch_BAO_Batch::checkBatchPermission('export', $this->_values['created_id'])) {
90 $this->add('xbutton', 'export_batch', ts('Close and Export Batch'), ['type' => 'submit']);
91 }
92 }
93
94 CRM_Contribute_BAO_Query::buildSearchForm($this);
95 $this->addElement('checkbox', 'toggleSelects', NULL, NULL);
96
97 $this->add('select',
98 'trans_remove',
99 ts('Task'),
100 ['' => ts('- actions -')] + ['Remove' => ts('Remove from Batch')]);
101
102 $this->add('xbutton', 'rSubmit', ts('Go'),
103 [
104 'type' => 'submit',
105 'class' => 'crm-form-submit',
106 'id' => 'GoRemove',
107 ]);
108
109 self::$_entityID = CRM_Utils_Request::retrieve('bid', 'Positive');
110
111 $this->addButtons(
112 [
113 [
114 'type' => 'submit',
115 'name' => ts('Search'),
116 'isDefault' => TRUE,
117 ],
118 ]
119 );
120
121 $this->addElement('checkbox', 'toggleSelect', NULL, NULL);
122 $this->add('select',
123 'trans_assign',
124 ts('Task'),
125 ['' => ts('- actions -')] + ['Assign' => ts('Assign to Batch')]);
126
127 $this->add('xbutton', 'submit', ts('Go'),
128 [
129 'type' => 'submit',
130 'class' => 'crm-form-submit',
131 'id' => 'Go',
132 ]);
133 $this->applyFilter('__ALL__', 'trim');
134
135 $this->addElement('hidden', 'batch_id', self::$_entityID);
136
137 $this->add('text', 'name', ts('Batch Name'));
138 }
139
140 /**
141 * Set the default values for the form.
142 */
143 public function setDefaultValues() {
144 // do not setdefault unless it is open/reopened batch
145 if (!in_array($this->_batchStatus, ['Open', 'Reopened'])) {
146 return;
147 }
148 if (isset(self::$_entityID)) {
149 $paymentInstrumentID = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'payment_instrument_id');
150 $defaults['contribution_payment_instrument_id'] = $paymentInstrumentID;
151 $this->assign('paymentInstrumentID', $paymentInstrumentID);
152 }
153 return $defaults;
154 }
155
156 /**
157 * Get action links.
158 *
159 * @return array
160 */
161 public function &links() {
162 if (!(self::$_links)) {
163 self::$_links = [
164 'view' => [
165 'name' => ts('View'),
166 'url' => 'civicrm/contact/view/contribution',
167 'qs' => 'reset=1&id=%%contid%%&cid=%%cid%%&action=view&context=contribution&selectedChild=contribute',
168 'title' => ts('View Contribution'),
169 ],
170 'assign' => [
171 'name' => ts('Assign'),
172 'ref' => 'disable-action',
173 'title' => ts('Assign Transaction'),
174 'extra' => 'onclick = "assignRemove( %%id%%,\'' . 'assign' . '\' );"',
175 ],
176 ];
177 }
178 return self::$_links;
179 }
180
181 }