Merge pull request #10625 from mickadoo/CRM-20833-update-api4-entity-namespace
[civicrm-core.git] / CRM / Financial / Form / BatchTransaction.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33
34 /**
35 * This class generates form components for Financial Type
36 */
37 class CRM_Financial_Form_BatchTransaction extends CRM_Contribute_Form {
38 static $_links = NULL;
39 static $_entityID;
40
41 /**
42 * Batch status.
43 * @var
44 */
45 protected $_batchStatusId;
46
47 /**
48 * Batch status name.
49 * @string
50 */
51 protected $_batchStatus;
52
53 public function preProcess() {
54 // This reuses some styles from search forms
55 CRM_Core_Resources::singleton()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
56
57 self::$_entityID = CRM_Utils_Request::retrieve('bid', 'Positive') ? CRM_Utils_Request::retrieve('bid', 'Positive') : $_POST['batch_id'];
58 $this->assign('entityID', self::$_entityID);
59 if (isset(self::$_entityID)) {
60 $this->_batchStatusId = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'status_id');
61 $batchStatuses = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name', 'condition' => " v.value={$this->_batchStatusId}"));
62 $this->_batchStatus = $batchStatuses[$this->_batchStatusId];
63 $this->assign('statusID', $this->_batchStatusId);
64 $this->assign('batchStatus', $this->_batchStatus);
65 $validStatus = FALSE;
66 if (in_array($this->_batchStatus, array('Open', 'Reopened'))) {
67 $validStatus = TRUE;
68 }
69 $this->assign('validStatus', $validStatus);
70
71 $batchTitle = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'title');
72 CRM_Utils_System::setTitle(ts('Accounting Batch - %1', array(1 => $batchTitle)));
73
74 $columnHeaders = array(
75 'created_by' => ts('Created By'),
76 'status' => ts('Status'),
77 'description' => ts('Description'),
78 'payment_instrument' => ts('Payment Method'),
79 'item_count' => ts('Entered Transactions'),
80 'assigned_item_count' => ts('Assigned Transactions'),
81 'total' => ts('Entered Total'),
82 'assigned_total' => ts('Assigned Total'),
83 'opened_date' => ts('Opened'),
84 );
85 $this->assign('columnHeaders', $columnHeaders);
86 }
87 }
88
89 /**
90 * Build the form object.
91 */
92 public function buildQuickForm() {
93 if ($this->_batchStatus == 'Closed') {
94 $this->add('submit', 'export_batch', ts('Export Batch'));
95 }
96
97 // do not build rest of form unless it is open/reopened batch
98 if (!in_array($this->_batchStatus, array('Open', 'Reopened'))) {
99 return;
100 }
101
102 parent::buildQuickForm();
103 $this->add('submit', 'close_batch', ts('Close Batch'));
104 $this->add('submit', 'export_batch', ts('Close & Export Batch'));
105
106 // text for sort_name
107 $this->addElement('text',
108 'sort_name',
109 ts('Contributor Name or Email'),
110 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact',
111 'sort_name'
112 )
113 );
114
115 $this->_group = CRM_Core_PseudoConstant::nestedGroup();
116
117 // multiselect for groups
118 if ($this->_group) {
119 $this->add('select', 'group', ts('Groups'), $this->_group, FALSE,
120 array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')
121 );
122 }
123 $contactTags = CRM_Core_BAO_Tag::getTags();
124
125 if ($contactTags) {
126 $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
127 array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
128 );
129 }
130 CRM_Contribute_BAO_Query::buildSearchForm($this);
131 $this->addElement('checkbox', 'toggleSelects', NULL, NULL);
132
133 $this->add('select',
134 'trans_remove',
135 ts('Task'),
136 array('' => ts('- actions -')) + array('Remove' => ts('Remove from Batch')));
137
138 $this->add('submit', 'rSubmit', ts('Go'),
139 array(
140 'class' => 'crm-form-submit',
141 'id' => 'GoRemove',
142 ));
143
144 self::$_entityID = CRM_Utils_Request::retrieve('bid', 'Positive');
145
146 $this->addButtons(
147 array(
148 array(
149 'type' => 'submit',
150 'name' => ts('Search'),
151 'isDefault' => TRUE,
152 ),
153 )
154 );
155
156 $this->addElement('checkbox', 'toggleSelect', NULL, NULL);
157 $this->add('select',
158 'trans_assign',
159 ts('Task'),
160 array('' => ts('- actions -')) + array('Assign' => ts('Assign to Batch')));
161
162 $this->add('submit', 'submit', ts('Go'),
163 array(
164 'class' => 'crm-form-submit',
165 'id' => 'Go',
166 ));
167 $this->applyFilter('__ALL__', 'trim');
168
169 $this->addElement('hidden', 'batch_id', self::$_entityID);
170
171 $this->add('text', 'name', ts('Batch Name'));
172 }
173
174 /**
175 * Set the default values for the form.
176 */
177 public function setDefaultValues() {
178 // do not setdefault unless it is open/reopened batch
179 if (!in_array($this->_batchStatus, array('Open', 'Reopened'))) {
180 return;
181 }
182 if (isset(self::$_entityID)) {
183 $paymentInstrumentID = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'payment_instrument_id');
184 $defaults['payment_instrument_id'] = $paymentInstrumentID;
185 $this->assign('paymentInstrumentID', $paymentInstrumentID);
186 }
187 return $defaults;
188 }
189
190 /**
191 * Get action links.
192 *
193 * @return array
194 */
195 public function &links() {
196 if (!(self::$_links)) {
197 self::$_links = array(
198 'view' => array(
199 'name' => ts('View'),
200 'url' => 'civicrm/contact/view/contribution',
201 'qs' => 'reset=1&id=%%contid%%&cid=%%cid%%&action=view&context=contribution&selectedChild=contribute',
202 'title' => ts('View Contribution'),
203 ),
204 'assign' => array(
205 'name' => ts('Assign'),
206 'ref' => 'disable-action',
207 'title' => ts('Assign Transaction'),
208 'extra' => 'onclick = "assignRemove( %%id%%,\'' . 'assign' . '\' );"',
209 ),
210 );
211 }
212 return self::$_links;
213 }
214
215 }