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