Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class contains all the function that are called using AJAX | |
38 | */ | |
39 | class CRM_Financial_Page_AJAX { | |
d424ffde CW |
40 | |
41 | /** | |
fe482240 | 42 | * get financial accounts of required account relationship. |
6a488035 | 43 | * $financialAccountType array with key account relationship and value financial account type option groups |
d424ffde | 44 | * |
e0ef6999 EM |
45 | * @param $config |
46 | */ | |
00be9182 | 47 | public static function jqFinancial($config) { |
6a488035 | 48 | if (!isset($_GET['_value']) || |
353ffa53 TO |
49 | empty($_GET['_value']) |
50 | ) { | |
6a488035 TO |
51 | CRM_Utils_System::civiExit(); |
52 | } | |
7b3622bf | 53 | $defaultId = NULL; |
6a488035 TO |
54 | if ($_GET['_value'] == 'select') { |
55 | $result = CRM_Contribute_PseudoConstant::financialAccount(); | |
56 | } | |
57 | else { | |
58 | $financialAccountType = array( | |
59 | '5' => 5, //expense | |
60 | '3' => 1, //AR relation | |
61 | '1' => 3, //revenue | |
62 | '6' => 1, // asset | |
63 | '7' => 4, //cost of sales | |
64 | '8' => 1, //premium inventory | |
65 | '9' => 3, //discount account is | |
4d6769aa | 66 | '10' => 2, //sales tax liability |
6a488035 | 67 | ); |
b800df93 | 68 | $financialAccountType = CRM_Utils_Array::value($_GET['_value'], $financialAccountType); |
6a488035 | 69 | $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType); |
b800df93 PN |
70 | if ($financialAccountType) { |
71 | $defaultId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = $financialAccountType"); | |
72 | } | |
6a488035 TO |
73 | } |
74 | $elements = array( | |
75 | array( | |
353ffa53 | 76 | 'name' => ts('- select -'), |
6a488035 | 77 | 'value' => 'select', |
21dfd5f5 | 78 | ), |
6a488035 TO |
79 | ); |
80 | ||
9b873358 | 81 | if (!empty($result)) { |
6a488035 | 82 | foreach ($result as $id => $name) { |
7b3622bf PN |
83 | $selectedArray = array(); |
84 | if ($id == $defaultId) { | |
85 | $selectedArray['selected'] = 'Selected'; | |
86 | } | |
6a488035 | 87 | $elements[] = array( |
c301f76e | 88 | 'name' => $name, |
89 | 'value' => $id, | |
90 | ) + $selectedArray; | |
6a488035 TO |
91 | } |
92 | } | |
ecdef330 | 93 | CRM_Utils_JSON::output($elements); |
6a488035 TO |
94 | } |
95 | ||
e0ef6999 EM |
96 | /** |
97 | * @param $config | |
98 | */ | |
00be9182 | 99 | public static function jqFinancialRelation($config) { |
6a488035 | 100 | if (!isset($_GET['_value']) || |
353ffa53 TO |
101 | empty($_GET['_value']) |
102 | ) { | |
6a488035 TO |
103 | CRM_Utils_System::civiExit(); |
104 | } | |
105 | ||
106 | if ($_GET['_value'] == 'select') { | |
7611ae71 | 107 | $result = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); |
6a488035 TO |
108 | } |
109 | else { | |
110 | $financialAccountType = array( | |
111 | '5' => array(5), //expense | |
112 | '1' => array(3, 6, 8), //Asset | |
113 | '3' => array(1, 9), //revenue | |
114 | '4' => array(7), //cost of sales | |
115 | ); | |
7fa9167d | 116 | $financialAccountId = CRM_Utils_Request::retrieve('_value', 'Positive', CRM_Core_DAO::$_nullObject); |
117 | $financialAccountTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccountId, 'financial_account_type_id'); | |
7611ae71 | 118 | $result = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); |
6a488035 TO |
119 | } |
120 | ||
121 | $elements = array( | |
122 | array( | |
353ffa53 | 123 | 'name' => ts('- Select Financial Account Relationship -'), |
6a488035 | 124 | 'value' => 'select', |
21dfd5f5 | 125 | ), |
6a488035 TO |
126 | ); |
127 | ||
128 | $countResult = count($financialAccountType[$financialAccountTypeId]); | |
129 | if (!empty($result)) { | |
130 | foreach ($result as $id => $name) { | |
353ffa53 | 131 | if (in_array($id, $financialAccountType[$financialAccountTypeId]) && $_GET['_value'] != 'select') { |
9b873358 | 132 | if ($countResult != 1) { |
6a488035 | 133 | $elements[] = array( |
353ffa53 | 134 | 'name' => $name, |
6a488035 TO |
135 | 'value' => $id, |
136 | ); | |
137 | } | |
138 | else { | |
139 | $elements[] = array( | |
353ffa53 TO |
140 | 'name' => $name, |
141 | 'value' => $id, | |
6a488035 TO |
142 | 'selected' => 'Selected', |
143 | ); | |
144 | } | |
145 | } | |
9b873358 | 146 | elseif ($_GET['_value'] == 'select') { |
6a488035 | 147 | $elements[] = array( |
353ffa53 | 148 | 'name' => $name, |
6a488035 TO |
149 | 'value' => $id, |
150 | ); | |
151 | } | |
152 | } | |
153 | } | |
ecdef330 | 154 | CRM_Utils_JSON::output($elements); |
6a488035 TO |
155 | } |
156 | ||
e0ef6999 EM |
157 | /** |
158 | * @param $config | |
159 | */ | |
00be9182 | 160 | public static function jqFinancialType($config) { |
353ffa53 TO |
161 | if (!isset($_GET['_value']) || |
162 | empty($_GET['_value']) | |
163 | ) { | |
6a488035 TO |
164 | CRM_Utils_System::civiExit(); |
165 | } | |
7fa9167d | 166 | $productId = CRM_Utils_Request::retrieve('_value', 'Positive', CRM_Core_DAO::$_nullObject); |
167 | $elements = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Product', $productId, 'financial_type_id'); | |
ecdef330 | 168 | CRM_Utils_JSON::output($elements); |
6a488035 TO |
169 | } |
170 | ||
171 | /** | |
172 | * Callback to perform action on batch records. | |
173 | */ | |
00be9182 | 174 | public static function assignRemove() { |
6a488035 TO |
175 | $op = CRM_Utils_Type::escape($_POST['op'], 'String'); |
176 | $recordBAO = CRM_Utils_Type::escape($_POST['recordBAO'], 'String'); | |
177 | foreach ($_POST['records'] as $record) { | |
178 | $recordID = CRM_Utils_Type::escape($record, 'Positive', FALSE); | |
179 | if ($recordID) { | |
180 | $records[] = $recordID; | |
181 | } | |
182 | } | |
183 | ||
7fa9167d | 184 | $entityID = CRM_Utils_Request::retrieve('entityID', 'Positive', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'); |
6a488035 TO |
185 | $methods = array( |
186 | 'assign' => 'addBatchEntity', | |
187 | 'remove' => 'removeBatchEntity', | |
188 | 'reopen' => 'create', | |
189 | 'close' => 'create', | |
190 | 'delete' => 'deleteBatch', | |
191 | ); | |
192 | if ($op == 'close') { | |
193 | $totals = CRM_Batch_BAO_Batch::batchTotals($records); | |
194 | } | |
195 | $response = array('status' => 'record-updated-fail'); | |
196 | // first munge and clean the recordBAO and get rid of any non alpha numeric characters | |
197 | $recordBAO = CRM_Utils_String::munge($recordBAO); | |
198 | $recordClass = explode('_', $recordBAO); | |
199 | // make sure recordClass is in the CRM namespace and | |
200 | // at least 3 levels deep | |
201 | if ($recordClass[0] == 'CRM' && count($recordClass) >= 3) { | |
202 | foreach ($records as $recordID) { | |
203 | $params = array(); | |
045f52a3 | 204 | $ids = NULL; |
6a488035 TO |
205 | switch ($op) { |
206 | case 'assign': | |
207 | case 'remove': | |
208 | $recordPID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $recordID, 'payment_instrument_id'); | |
209 | $batchPID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $entityID, 'payment_instrument_id'); | |
045f52a3 | 210 | $paymentInstrument = CRM_Core_OptionGroup::getLabel('payment_instrument', $batchPID); |
6a488035 TO |
211 | if ($op == 'remove' || ($recordPID == $batchPID && $op == 'assign') || !isset($batchPID)) { |
212 | $params = array( | |
213 | 'entity_id' => $recordID, | |
214 | 'entity_table' => 'civicrm_financial_trxn', | |
215 | 'batch_id' => $entityID, | |
216 | ); | |
217 | } | |
218 | else { | |
481a74f4 | 219 | $response = array('status' => ts("This batch is configured to include only transactions using %1 payment method. If you want to include other transactions, please edit the batch first and modify the Payment Method.", array(1 => $paymentInstrument))); |
6a488035 TO |
220 | } |
221 | break; | |
045f52a3 | 222 | |
6a488035 TO |
223 | case 'close': |
224 | // Update totals when closing a batch | |
225 | $params = $totals[$recordID]; | |
226 | case 'reopen': | |
2d818e4a | 227 | $status = $op == 'close' ? 'Closed' : 'Reopened'; |
6a488035 | 228 | $ids['batchID'] = $recordID; |
2d818e4a | 229 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name')); |
6a488035 TO |
230 | $params['status_id'] = CRM_Utils_Array::key($status, $batchStatus); |
231 | $session = CRM_Core_Session::singleton(); | |
232 | $params['modified_date'] = date('YmdHis'); | |
233 | $params['modified_id'] = $session->get('userID'); | |
234 | $params['id'] = $recordID; | |
235 | $context = "financialBatch"; | |
236 | break; | |
237 | ||
238 | case 'export': | |
239 | CRM_Utils_System::redirect("civicrm/financial/batch/export?reset=1&id=$recordID"); | |
240 | break; | |
241 | ||
242 | case 'delete': | |
243 | $params = $recordID; | |
244 | $context = "financialBatch"; | |
245 | break; | |
246 | } | |
247 | ||
248 | if (method_exists($recordBAO, $methods[$op]) & !empty($params)) { | |
249 | if (isset($context)) { | |
250 | $updated = call_user_func_array(array($recordBAO, $methods[$op]), array(&$params, $ids, $context)); | |
251 | } | |
252 | else { | |
253 | $updated = call_user_func_array(array($recordBAO, $methods[$op]), array(&$params, $ids)); | |
254 | } | |
255 | if ($updated) { | |
256 | $response = array('status' => 'record-updated-success'); | |
257 | } | |
258 | } | |
259 | } | |
260 | } | |
ecdef330 | 261 | CRM_Utils_JSON::output($response); |
6a488035 TO |
262 | } |
263 | ||
00be9182 | 264 | public static function getFinancialTransactionsList() { |
c301f76e | 265 | $sortMapper = array( |
266 | 0 => '', | |
267 | 1 => '', | |
268 | 2 => 'sort_name', | |
269 | 3 => 'amount', | |
270 | 4 => 'trxn_id', | |
271 | 5 => 'transaction_date', | |
272 | 6 => 'payment_method', | |
273 | 7 => 'status', | |
274 | 8 => 'name', | |
275 | ); | |
6a488035 | 276 | |
353ffa53 TO |
277 | $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer'); |
278 | $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0; | |
279 | $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25; | |
280 | $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL; | |
6a488035 | 281 | $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc'; |
353ffa53 TO |
282 | $context = isset($_REQUEST['context']) ? CRM_Utils_Type::escape($_REQUEST['context'], 'String') : NULL; |
283 | $entityID = isset($_REQUEST['entityID']) ? CRM_Utils_Type::escape($_REQUEST['entityID'], 'String') : NULL; | |
6a488035 | 284 | $notPresent = isset($_REQUEST['notPresent']) ? CRM_Utils_Type::escape($_REQUEST['notPresent'], 'String') : NULL; |
353ffa53 TO |
285 | $statusID = isset($_REQUEST['statusID']) ? CRM_Utils_Type::escape($_REQUEST['statusID'], 'String') : NULL; |
286 | $search = isset($_REQUEST['search']) ? TRUE : FALSE; | |
6a488035 TO |
287 | |
288 | $params = $_POST; | |
289 | if ($sort && $sortOrder) { | |
290 | $params['sortBy'] = $sort . ' ' . $sortOrder; | |
291 | } | |
292 | ||
c301f76e | 293 | $returnvalues = array( |
294 | 'civicrm_financial_trxn.payment_instrument_id as payment_method', | |
295 | 'civicrm_contribution.contact_id as contact_id', | |
296 | 'civicrm_contribution.id as contributionID', | |
297 | 'contact_a.sort_name', | |
298 | 'civicrm_financial_trxn.total_amount as amount', | |
299 | 'civicrm_financial_trxn.trxn_id as trxn_id', | |
300 | 'contact_a.contact_type', | |
301 | 'contact_a.contact_sub_type', | |
302 | 'civicrm_financial_trxn.trxn_date as transaction_date', | |
303 | 'name', | |
304 | 'civicrm_contribution.currency as currency', | |
305 | 'civicrm_financial_trxn.status_id as status', | |
306 | 'civicrm_financial_trxn.check_number as check_number', | |
307 | ); | |
6a488035 | 308 | |
c301f76e | 309 | $columnHeader = array( |
310 | 'contact_type' => '', | |
311 | 'sort_name' => ts('Contact Name'), | |
312 | 'amount' => ts('Amount'), | |
313 | 'trxn_id' => ts('Trxn ID'), | |
314 | 'transaction_date' => ts('Received'), | |
315 | 'payment_method' => ts('Payment Method'), | |
316 | 'status' => ts('Status'), | |
317 | 'name' => ts('Type'), | |
318 | ); | |
6a488035 TO |
319 | |
320 | if ($sort && $sortOrder) { | |
321 | $params['sortBy'] = $sort . ' ' . $sortOrder; | |
322 | } | |
323 | ||
324 | $params['page'] = ($offset / $rowCount) + 1; | |
325 | $params['rp'] = $rowCount; | |
326 | ||
327 | $params['context'] = $context; | |
353ffa53 | 328 | $params['offset'] = ($params['page'] - 1) * $params['rp']; |
6a488035 | 329 | $params['rowCount'] = $params['rp']; |
353ffa53 TO |
330 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); |
331 | $params['total'] = 0; | |
6a488035 TO |
332 | |
333 | // get batch list | |
334 | if (isset($notPresent)) { | |
335 | $financialItem = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, $notPresent, $params); | |
336 | if ($search) { | |
337 | $unassignedTransactions = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, $notPresent, $params, TRUE); | |
338 | } | |
339 | else { | |
340 | $unassignedTransactions = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, $notPresent, NULL, TRUE); | |
341 | } | |
342 | while ($unassignedTransactions->fetch()) { | |
343 | $unassignedTransactionsCount[] = $unassignedTransactions->id; | |
344 | } | |
345 | if (!empty($unassignedTransactionsCount)) { | |
346 | $params['total'] = count($unassignedTransactionsCount); | |
347 | } | |
348 | ||
349 | } | |
350 | else { | |
351 | $financialItem = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, NULL, $params); | |
352 | $assignedTransactions = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues); | |
353 | while ($assignedTransactions->fetch()) { | |
354 | $assignedTransactionsCount[] = $assignedTransactions->id; | |
355 | } | |
356 | if (!empty($assignedTransactionsCount)) { | |
357 | $params['total'] = count($assignedTransactionsCount); | |
358 | } | |
359 | } | |
360 | $financialitems = array(); | |
361 | while ($financialItem->fetch()) { | |
362 | $row[$financialItem->id] = array(); | |
363 | foreach ($columnHeader as $columnKey => $columnValue) { | |
364 | if ($financialItem->contact_sub_type && $columnKey == 'contact_type') { | |
365 | $row[$financialItem->id][$columnKey] = $financialItem->contact_sub_type; | |
366 | continue; | |
367 | } | |
368 | $row[$financialItem->id][$columnKey] = $financialItem->$columnKey; | |
369 | if ($columnKey == 'sort_name' && $financialItem->$columnKey) { | |
92fcb95f TO |
370 | $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=" . $financialItem->contact_id); |
371 | $row[$financialItem->id][$columnKey] = '<a href=' . $url . '>' . $financialItem->$columnKey . '</a>'; | |
6a488035 TO |
372 | } |
373 | elseif ($columnKey == 'payment_method' && $financialItem->$columnKey) { | |
374 | $row[$financialItem->id][$columnKey] = CRM_Core_OptionGroup::getLabel('payment_instrument', $financialItem->$columnKey); | |
375 | if ($row[$financialItem->id][$columnKey] == 'Check') { | |
92fcb95f | 376 | $row[$financialItem->id][$columnKey] = $row[$financialItem->id][$columnKey] . ' (' . $financialItem->check_number . ')'; |
6a488035 TO |
377 | } |
378 | } | |
379 | elseif ($columnKey == 'amount' && $financialItem->$columnKey) { | |
380 | $row[$financialItem->id][$columnKey] = CRM_Utils_Money::format($financialItem->$columnKey, $financialItem->currency); | |
381 | } | |
382 | elseif ($columnKey == 'transaction_date' && $financialItem->$columnKey) { | |
045f52a3 | 383 | $row[$financialItem->id][$columnKey] = CRM_Utils_Date::customFormat($financialItem->$columnKey); |
6a488035 TO |
384 | } |
385 | elseif ($columnKey == 'status' && $financialItem->$columnKey) { | |
386 | $row[$financialItem->id][$columnKey] = CRM_Core_OptionGroup::getLabel('contribution_status', $financialItem->$columnKey); | |
387 | } | |
388 | } | |
045f52a3 | 389 | if ($statusID == CRM_Core_OptionGroup::getValue('batch_status', 'Open')) { |
6a488035 TO |
390 | if (isset($notPresent)) { |
391 | $js = "enableActions('x')"; | |
86bfa4f6 | 392 | $row[$financialItem->id]['check'] = "<input type='checkbox' id='mark_x_" . $financialItem->id . "' name='mark_x_" . $financialItem->id . "' value='1' onclick={$js}></input>"; |
87dab4a4 AH |
393 | $row[$financialItem->id]['action'] = CRM_Core_Action::formLink( |
394 | CRM_Financial_Form_BatchTransaction::links(), | |
045f52a3 | 395 | NULL, |
87dab4a4 AH |
396 | array( |
397 | 'id' => $financialItem->id, | |
398 | 'contid' => $financialItem->contributionID, | |
21dfd5f5 | 399 | 'cid' => $financialItem->contact_id, |
87dab4a4 AH |
400 | ), |
401 | ts('more'), | |
402 | FALSE, | |
403 | 'financialItem.batch.row', | |
404 | 'FinancialItem', | |
405 | $financialItem->id | |
406 | ); | |
6a488035 TO |
407 | } |
408 | else { | |
409 | $js = "enableActions('y')"; | |
86bfa4f6 | 410 | $row[$financialItem->id]['check'] = "<input type='checkbox' id='mark_y_" . $financialItem->id . "' name='mark_y_" . $financialItem->id . "' value='1' onclick={$js}></input>"; |
87dab4a4 AH |
411 | $row[$financialItem->id]['action'] = CRM_Core_Action::formLink( |
412 | CRM_Financial_Page_BatchTransaction::links(), | |
045f52a3 | 413 | NULL, |
87dab4a4 AH |
414 | array( |
415 | 'id' => $financialItem->id, | |
416 | 'contid' => $financialItem->contributionID, | |
21dfd5f5 | 417 | 'cid' => $financialItem->contact_id, |
87dab4a4 AH |
418 | ), |
419 | ts('more'), | |
420 | FALSE, | |
421 | 'financialItem.batch.row', | |
422 | 'FinancialItem', | |
423 | $financialItem->id | |
424 | ); | |
6a488035 TO |
425 | } |
426 | } | |
427 | else { | |
428 | $row[$financialItem->id]['check'] = NULL; | |
429 | $links = CRM_Financial_Page_BatchTransaction::links(); | |
430 | unset($links['remove']); | |
87dab4a4 AH |
431 | $row[$financialItem->id]['action'] = CRM_Core_Action::formLink( |
432 | $links, | |
045f52a3 | 433 | NULL, |
87dab4a4 AH |
434 | array( |
435 | 'id' => $financialItem->id, | |
436 | 'contid' => $financialItem->contributionID, | |
21dfd5f5 | 437 | 'cid' => $financialItem->contact_id, |
87dab4a4 AH |
438 | ), |
439 | ts('more'), | |
440 | FALSE, | |
441 | 'financialItem.batch.row', | |
442 | 'FinancialItem', | |
443 | $financialItem->id | |
444 | ); | |
6a488035 | 445 | } |
045f52a3 | 446 | $row[$financialItem->id]['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage(CRM_Utils_Array::value('contact_sub_type', $row[$financialItem->id]) ? CRM_Utils_Array::value('contact_sub_type', $row[$financialItem->id]) : CRM_Utils_Array::value('contact_type', $row[$financialItem->id]), FALSE, $financialItem->contact_id); |
6a488035 TO |
447 | $financialitems = $row; |
448 | } | |
449 | ||
045f52a3 | 450 | $iFilteredTotal = $iTotal = $params['total']; |
c301f76e | 451 | $selectorElements = array( |
452 | 'check', | |
453 | 'contact_type', | |
454 | 'sort_name', | |
455 | 'amount', | |
456 | 'trxn_id', | |
457 | 'transaction_date', | |
458 | 'payment_method', | |
459 | 'status', | |
460 | 'name', | |
461 | 'action', | |
462 | ); | |
6a488035 | 463 | |
70dd31e2 | 464 | header('Content-Type: application/json'); |
6a488035 TO |
465 | echo CRM_Utils_JSON::encodeDataTableSelector($financialitems, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); |
466 | CRM_Utils_System::civiExit(); | |
467 | } | |
468 | ||
00be9182 | 469 | public static function bulkAssignRemove() { |
6a488035 TO |
470 | $checkIDs = $_REQUEST['ID']; |
471 | $entityID = CRM_Utils_Type::escape($_REQUEST['entityID'], 'String'); | |
353ffa53 | 472 | $action = CRM_Utils_Type::escape($_REQUEST['action'], 'String'); |
6a488035 | 473 | foreach ($checkIDs as $key => $value) { |
045f52a3 TO |
474 | if ((substr($value, 0, 7) == "mark_x_" && $action == 'Assign') || (substr($value, 0, 7) == "mark_y_" && $action == 'Remove')) { |
475 | $contributions = explode("_", $value); | |
6a488035 TO |
476 | $cIDs[] = $contributions[2]; |
477 | } | |
478 | } | |
479 | ||
480 | $batchPID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $entityID, 'payment_instrument_id'); | |
045f52a3 | 481 | $paymentInstrument = CRM_Core_OptionGroup::getLabel('payment_instrument', $batchPID); |
6a488035 TO |
482 | foreach ($cIDs as $key => $value) { |
483 | $recordPID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $value, 'payment_instrument_id'); | |
353ffa53 | 484 | if ($action == 'Remove' || ($recordPID == $batchPID && $action == 'Assign') || !isset($batchPID)) { |
c301f76e | 485 | $params = array( |
486 | 'entity_id' => $value, | |
487 | 'entity_table' => 'civicrm_financial_trxn', | |
488 | 'batch_id' => $entityID, | |
489 | ); | |
6a488035 TO |
490 | if ($action == 'Assign') { |
491 | $updated = CRM_Batch_BAO_Batch::addBatchEntity($params); | |
492 | } | |
493 | else { | |
494 | $updated = CRM_Batch_BAO_Batch::removeBatchEntity($params); | |
495 | } | |
496 | } | |
497 | } | |
498 | if ($updated) { | |
499 | $status = array('status' => 'record-updated-success'); | |
500 | } | |
501 | else { | |
481a74f4 | 502 | $status = array('status' => ts("This batch is configured to include only transactions using %1 payment method. If you want to include other transactions, please edit the batch first and modify the Payment Method.", array(1 => $paymentInstrument))); |
6a488035 | 503 | } |
ecdef330 | 504 | CRM_Utils_JSON::output($status); |
6a488035 TO |
505 | } |
506 | ||
00be9182 | 507 | public static function getBatchSummary() { |
6a488035 TO |
508 | $batchID = CRM_Utils_Type::escape($_REQUEST['batchID'], 'String'); |
509 | $params = array('id' => $batchID); | |
510 | $batchInfo = CRM_Batch_BAO_Batch::retrieve($params, $value); | |
511 | $batchTotals = CRM_Batch_BAO_Batch::batchTotals(array($batchID)); | |
c301f76e | 512 | $batchSummary = array( |
513 | 'created_by' => CRM_Contact_BAO_Contact::displayName($batchInfo->created_id), | |
514 | 'status' => CRM_Core_OptionGroup::getLabel('batch_status', $batchInfo->status_id), | |
515 | 'description' => $batchInfo->description, | |
516 | 'payment_instrument' => CRM_Core_OptionGroup::getLabel('payment_instrument', $batchInfo->payment_instrument_id), | |
517 | 'item_count' => $batchInfo->item_count, | |
518 | 'assigned_item_count' => $batchTotals[$batchID]['item_count'], | |
519 | 'total' => CRM_Utils_Money::format($batchInfo->total), | |
520 | 'assigned_total' => CRM_Utils_Money::format($batchTotals[$batchID]['total']), | |
521 | 'opened_date' => CRM_Utils_Date::customFormat($batchInfo->created_date), | |
522 | ); | |
6a488035 | 523 | |
ecdef330 | 524 | CRM_Utils_JSON::output($batchSummary); |
6a488035 | 525 | } |
96025800 | 526 | |
6a488035 | 527 | } |