Merge pull request #13250 from francescbassas/patch-16
[civicrm-core.git] / CRM / Financial / Form / BatchTransaction.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
006389de 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 * This class generates form components for Financial Type
6a488035
TO
36 */
37class CRM_Financial_Form_BatchTransaction extends CRM_Contribute_Form {
38 static $_links = NULL;
39 static $_entityID;
40
41 /**
fe482240 42 * Batch status.
6a488035
TO
43 * @var
44 */
45 protected $_batchStatusId;
46
89d18ebd
PN
47 /**
48 * Batch status name.
49 * @string
50 */
51 protected $_batchStatus;
52
00be9182 53 public function preProcess() {
562fda4b
CW
54 // This reuses some styles from search forms
55 CRM_Core_Resources::singleton()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
56
0efdabe7 57 self::$_entityID = CRM_Utils_Request::retrieve('bid', 'Positive') ? CRM_Utils_Request::retrieve('bid', 'Positive') : CRM_Utils_Array::value('batch_id', $_POST);
6a488035
TO
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');
89d18ebd
PN
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];
6a488035 63 $this->assign('statusID', $this->_batchStatusId);
89d18ebd 64 $this->assign('batchStatus', $this->_batchStatus);
0bcbe589 65 $validStatus = FALSE;
89d18ebd 66 if (in_array($this->_batchStatus, array('Open', 'Reopened'))) {
0bcbe589 67 $validStatus = TRUE;
89d18ebd 68 }
0bcbe589 69 $this->assign('validStatus', $validStatus);
ca0e2dda 70 $this->_values = civicrm_api3('Batch', 'getSingle', array('id' => self::$_entityID));
6a488035
TO
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
408b79bf 74 $columnHeaders = array(
75 'created_by' => ts('Created By'),
76 'status' => ts('Status'),
77 'description' => ts('Description'),
536f0e02 78 'payment_instrument' => ts('Payment Method'),
a3933cca 79 'item_count' => ts('Expected Number of Items'),
80 'assigned_item_count' => ts('Actual Number of Items'),
81 'total' => ts('Expected Total Amount'),
82 'assigned_total' => ts('Actual Total Amount'),
408b79bf 83 'opened_date' => ts('Opened'),
84 );
6a488035
TO
85 $this->assign('columnHeaders', $columnHeaders);
86 }
87 }
353ffa53 88
6a488035 89 /**
fe482240 90 * Build the form object.
6a488035
TO
91 */
92 public function buildQuickForm() {
89d18ebd 93 if ($this->_batchStatus == 'Closed') {
03e04002 94 $this->add('submit', 'export_batch', ts('Export Batch'));
6a488035 95 }
03e04002 96
89d18ebd
PN
97 // do not build rest of form unless it is open/reopened batch
98 if (!in_array($this->_batchStatus, array('Open', 'Reopened'))) {
6a488035
TO
99 return;
100 }
101
102 parent::buildQuickForm();
f07fc4c9 103 if (CRM_Batch_BAO_Batch::checkBatchPermission('close', $this->_values['created_id'])) {
ca0e2dda
PN
104 $this->add('submit', 'close_batch', ts('Close Batch'));
105 if (CRM_Batch_BAO_Batch::checkBatchPermission('export', $this->_values['created_id'])) {
106 $this->add('submit', 'export_batch', ts('Close & Export Batch'));
107 }
108 }
6a488035
TO
109
110 // text for sort_name
111 $this->addElement('text',
112 'sort_name',
113 ts('Contributor Name or Email'),
114 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact',
115 'sort_name'
116 )
117 );
118
24431f7b 119 $this->_group = CRM_Core_PseudoConstant::nestedGroup();
03e04002 120
6a488035
TO
121 // multiselect for groups
122 if ($this->_group) {
123 $this->add('select', 'group', ts('Groups'), $this->_group, FALSE,
ab345ca5 124 array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')
6a488035
TO
125 );
126 }
127 $contactTags = CRM_Core_BAO_Tag::getTags();
03e04002 128
6a488035
TO
129 if ($contactTags) {
130 $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE,
ab345ca5 131 array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2')
6a488035
TO
132 );
133 }
134 CRM_Contribute_BAO_Query::buildSearchForm($this);
135 $this->addElement('checkbox', 'toggleSelects', NULL, NULL);
136
481a74f4 137 $this->add('select',
6a488035
TO
138 'trans_remove',
139 ts('Task'),
481a74f4 140 array('' => ts('- actions -')) + array('Remove' => ts('Remove from Batch')));
6a488035 141
045f52a3 142 $this->add('submit', 'rSubmit', ts('Go'),
6a488035 143 array(
97e557d7 144 'class' => 'crm-form-submit',
6a488035
TO
145 'id' => 'GoRemove',
146 ));
147
045f52a3 148 self::$_entityID = CRM_Utils_Request::retrieve('bid', 'Positive');
6a488035
TO
149
150 $this->addButtons(
151 array(
045f52a3 152 array(
353ffa53 153 'type' => 'submit',
6a488035
TO
154 'name' => ts('Search'),
155 'isDefault' => TRUE,
21dfd5f5 156 ),
6a488035
TO
157 )
158 );
03e04002 159
6a488035 160 $this->addElement('checkbox', 'toggleSelect', NULL, NULL);
481a74f4 161 $this->add('select',
6a488035
TO
162 'trans_assign',
163 ts('Task'),
481a74f4 164 array('' => ts('- actions -')) + array('Assign' => ts('Assign to Batch')));
6a488035 165
045f52a3 166 $this->add('submit', 'submit', ts('Go'),
6a488035 167 array(
97e557d7 168 'class' => 'crm-form-submit',
6a488035
TO
169 'id' => 'Go',
170 ));
171 $this->applyFilter('__ALL__', 'trim');
172
173 $this->addElement('hidden', 'batch_id', self::$_entityID);
174
175 $this->add('text', 'name', ts('Batch Name'));
176 }
177
f2ac86d1 178 /**
179 * Set the default values for the form.
180 */
00be9182 181 public function setDefaultValues() {
89d18ebd
PN
182 // do not setdefault unless it is open/reopened batch
183 if (!in_array($this->_batchStatus, array('Open', 'Reopened'))) {
6a488035
TO
184 return;
185 }
186 if (isset(self::$_entityID)) {
187 $paymentInstrumentID = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'payment_instrument_id');
4804d6ee 188 $defaults['contribution_payment_instrument_id'] = $paymentInstrumentID;
6a488035
TO
189 $this->assign('paymentInstrumentID', $paymentInstrumentID);
190 }
191 return $defaults;
192 }
193
e0ef6999 194 /**
23f0202e 195 * Get action links.
196 *
197 * @return array
e0ef6999 198 */
00be9182 199 public function &links() {
6a488035
TO
200 if (!(self::$_links)) {
201 self::$_links = array(
353ffa53
TO
202 'view' => array(
203 'name' => ts('View'),
204 'url' => 'civicrm/contact/view/contribution',
205 'qs' => 'reset=1&id=%%contid%%&cid=%%cid%%&action=view&context=contribution&selectedChild=contribute',
6a488035
TO
206 'title' => ts('View Contribution'),
207 ),
208 'assign' => array(
353ffa53
TO
209 'name' => ts('Assign'),
210 'ref' => 'disable-action',
6a488035
TO
211 'title' => ts('Assign Transaction'),
212 'extra' => 'onclick = "assignRemove( %%id%%,\'' . 'assign' . '\' );"',
21dfd5f5 213 ),
6a488035
TO
214 );
215 }
216 return self::$_links;
217 }
96025800 218
6a488035 219}