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