Merge pull request #22888 from civicrm/5.47
[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
2d4bdc58
EM
18use Civi\Api4\Batch;
19
6a488035
TO
20/**
21 * This class contains all the function that are called using AJAX
22 */
23class CRM_Financial_Page_AJAX {
d424ffde
CW
24
25 /**
fe482240 26 * get financial accounts of required account relationship.
6a488035 27 * $financialAccountType array with key account relationship and value financial account type option groups
d424ffde 28 *
e0ef6999
EM
29 * @param $config
30 */
00be9182 31 public static function jqFinancial($config) {
6a488035 32 if (!isset($_GET['_value']) ||
353ffa53
TO
33 empty($_GET['_value'])
34 ) {
6a488035
TO
35 CRM_Utils_System::civiExit();
36 }
7b3622bf 37 $defaultId = NULL;
6a488035
TO
38 if ($_GET['_value'] == 'select') {
39 $result = CRM_Contribute_PseudoConstant::financialAccount();
40 }
41 else {
48a29690 42 $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
9c1bc317 43 $financialAccountType = $financialAccountType[$_GET['_value']] ?? NULL;
6a488035 44 $result = CRM_Contribute_PseudoConstant::financialAccount(NULL, $financialAccountType);
b800df93
PN
45 if ($financialAccountType) {
46 $defaultId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = $financialAccountType");
47 }
6a488035 48 }
be2fb01f
CW
49 $elements = [
50 [
353ffa53 51 'name' => ts('- select -'),
6a488035 52 'value' => 'select',
be2fb01f
CW
53 ],
54 ];
6a488035 55
9b873358 56 if (!empty($result)) {
6a488035 57 foreach ($result as $id => $name) {
be2fb01f 58 $selectedArray = [];
7b3622bf
PN
59 if ($id == $defaultId) {
60 $selectedArray['selected'] = 'Selected';
61 }
be2fb01f 62 $elements[] = [
c301f76e 63 'name' => $name,
64 'value' => $id,
be2fb01f 65 ] + $selectedArray;
6a488035
TO
66 }
67 }
ecdef330 68 CRM_Utils_JSON::output($elements);
6a488035
TO
69 }
70
e0ef6999
EM
71 /**
72 * @param $config
73 */
00be9182 74 public static function jqFinancialRelation($config) {
6a488035 75 if (!isset($_GET['_value']) ||
353ffa53
TO
76 empty($_GET['_value'])
77 ) {
6a488035
TO
78 CRM_Utils_System::civiExit();
79 }
80
48a29690
PN
81 if ($_GET['_value'] != 'select') {
82 $financialAccountType = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(TRUE);
a3d827a7 83 $financialAccountId = CRM_Utils_Request::retrieve('_value', 'Positive');
7fa9167d 84 $financialAccountTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccountId, 'financial_account_type_id');
6a488035 85 }
48a29690
PN
86 $params['orderColumn'] = 'label';
87 $result = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
6d02eb56 88
be2fb01f
CW
89 $elements = [
90 [
353ffa53 91 'name' => ts('- Select Financial Account Relationship -'),
6a488035 92 'value' => 'select',
be2fb01f
CW
93 ],
94 ];
6a488035
TO
95
96 $countResult = count($financialAccountType[$financialAccountTypeId]);
97 if (!empty($result)) {
98 foreach ($result as $id => $name) {
353ffa53 99 if (in_array($id, $financialAccountType[$financialAccountTypeId]) && $_GET['_value'] != 'select') {
9b873358 100 if ($countResult != 1) {
be2fb01f 101 $elements[] = [
353ffa53 102 'name' => $name,
6a488035 103 'value' => $id,
be2fb01f 104 ];
6a488035
TO
105 }
106 else {
be2fb01f 107 $elements[] = [
353ffa53
TO
108 'name' => $name,
109 'value' => $id,
6a488035 110 'selected' => 'Selected',
be2fb01f 111 ];
6a488035
TO
112 }
113 }
9b873358 114 elseif ($_GET['_value'] == 'select') {
be2fb01f 115 $elements[] = [
353ffa53 116 'name' => $name,
6a488035 117 'value' => $id,
be2fb01f 118 ];
6a488035
TO
119 }
120 }
121 }
ecdef330 122 CRM_Utils_JSON::output($elements);
6a488035
TO
123 }
124
e0ef6999
EM
125 /**
126 * @param $config
127 */
00be9182 128 public static function jqFinancialType($config) {
353ffa53
TO
129 if (!isset($_GET['_value']) ||
130 empty($_GET['_value'])
131 ) {
6a488035
TO
132 CRM_Utils_System::civiExit();
133 }
a3d827a7 134 $productId = CRM_Utils_Request::retrieve('_value', 'Positive');
7fa9167d 135 $elements = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Product', $productId, 'financial_type_id');
ecdef330 136 CRM_Utils_JSON::output($elements);
6a488035
TO
137 }
138
139 /**
140 * Callback to perform action on batch records.
141 */
00be9182 142 public static function assignRemove() {
6a488035
TO
143 $op = CRM_Utils_Type::escape($_POST['op'], 'String');
144 $recordBAO = CRM_Utils_Type::escape($_POST['recordBAO'], 'String');
145 foreach ($_POST['records'] as $record) {
146 $recordID = CRM_Utils_Type::escape($record, 'Positive', FALSE);
147 if ($recordID) {
148 $records[] = $recordID;
149 }
150 }
151
7fa9167d 152 $entityID = CRM_Utils_Request::retrieve('entityID', 'Positive', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
be2fb01f 153 $methods = [
ee20d7be
PN
154 'assign' => 'create',
155 'remove' => 'del',
6a488035
TO
156 'reopen' => 'create',
157 'close' => 'create',
158 'delete' => 'deleteBatch',
be2fb01f 159 ];
6a488035
TO
160 if ($op == 'close') {
161 $totals = CRM_Batch_BAO_Batch::batchTotals($records);
162 }
be2fb01f 163 $response = ['status' => 'record-updated-fail'];
6a488035
TO
164 // first munge and clean the recordBAO and get rid of any non alpha numeric characters
165 $recordBAO = CRM_Utils_String::munge($recordBAO);
166 $recordClass = explode('_', $recordBAO);
167 // make sure recordClass is in the CRM namespace and
168 // at least 3 levels deep
169 if ($recordClass[0] == 'CRM' && count($recordClass) >= 3) {
170 foreach ($records as $recordID) {
be2fb01f 171 $params = [];
6a488035
TO
172 switch ($op) {
173 case 'assign':
174 case 'remove':
df33c0cd 175 if ($op == 'remove' || $op == 'assign') {
be2fb01f 176 $params = [
6a488035
TO
177 'entity_id' => $recordID,
178 'entity_table' => 'civicrm_financial_trxn',
179 'batch_id' => $entityID,
be2fb01f 180 ];
6a488035 181 }
6a488035 182 break;
045f52a3 183
6a488035
TO
184 case 'close':
185 // Update totals when closing a batch
186 $params = $totals[$recordID];
187 case 'reopen':
2d818e4a 188 $status = $op == 'close' ? 'Closed' : 'Reopened';
be2fb01f 189 $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', ['labelColumn' => 'name']);
6a488035
TO
190 $params['status_id'] = CRM_Utils_Array::key($status, $batchStatus);
191 $session = CRM_Core_Session::singleton();
192 $params['modified_date'] = date('YmdHis');
193 $params['modified_id'] = $session->get('userID');
194 $params['id'] = $recordID;
6a488035
TO
195 break;
196
6a488035
TO
197 case 'delete':
198 $params = $recordID;
6a488035
TO
199 break;
200 }
201
202 if (method_exists($recordBAO, $methods[$op]) & !empty($params)) {
df33c0cd
JG
203 try {
204 $updated = call_user_func_array(array($recordBAO, $methods[$op]), array(&$params));
205 }
206 catch (\CRM_Core_Exception $e) {
207 $errorMessage = $e->getMessage();
208 }
6a488035 209 if ($updated) {
5de2e536
PN
210 $redirectStatus = $updated->status_id;
211 if ($batchStatus[$updated->status_id] == "Reopened") {
212 $redirectStatus = array_search("Open", $batchStatus);
213 }
be2fb01f 214 $response = [
5de2e536
PN
215 'status' => 'record-updated-success',
216 'status_id' => $redirectStatus,
be2fb01f 217 ];
6a488035 218 }
df33c0cd
JG
219 if ($errorMessage ?? FALSE) {
220 $response = ['status' => $errorMessage];
221 }
6a488035
TO
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 257 $statusID = isset($_REQUEST['statusID']) ? CRM_Utils_Type::escape($_REQUEST['statusID'], 'String') : NULL;
f7dbf5d9 258 $search = isset($_REQUEST['search']);
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'];
9c1bc317 306 $params['sort'] = $params['sortBy'] ?? NULL;
353ffa53 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 }
adc25b11
EM
360 elseif ($columnKey === 'amount' && $financialItem->$columnKey) {
361 $row[$financialItem->id][$columnKey] = Civi::format()->money($financialItem->$columnKey, $financialItem->currency);
6a488035 362 }
adc25b11 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 377 $row[$financialItem->id]['action'] = CRM_Core_Action::formLink(
243a28d6 378 (new 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 395 $row[$financialItem->id]['action'] = CRM_Core_Action::formLink(
243a28d6 396 (new 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 431 if ($financialItem->contact_id) {
e71c1326 432 $row[$financialItem->id]['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage(!empty($row[$financialItem->id]['contact_sub_type']) ? $row[$financialItem->id]['contact_sub_type'] : CRM_Utils_Array::value('contact_type', $row[$financialItem->id]), FALSE, $financialItem->contact_id);
e8b11533 433 }
243a28d6 434 // @todo: Is this right? Shouldn't it be adding to the array as we loop?
6a488035
TO
435 $financialitems = $row;
436 }
437
045f52a3 438 $iFilteredTotal = $iTotal = $params['total'];
be2fb01f 439 $selectorElements = [
c301f76e 440 'check',
441 'contact_type',
442 'sort_name',
443 'amount',
444 'trxn_id',
445 'transaction_date',
dbf605ab 446 'receive_date',
c301f76e 447 'payment_method',
448 'status',
449 'name',
450 'action',
be2fb01f 451 ];
6a488035 452
478c39cf 453 if ($return) {
454 return CRM_Utils_JSON::encodeDataTableSelector($financialitems, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
455 }
d42a224c 456 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
6a488035
TO
457 echo CRM_Utils_JSON::encodeDataTableSelector($financialitems, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
458 CRM_Utils_System::civiExit();
459 }
460
00be9182 461 public static function bulkAssignRemove() {
6a488035
TO
462 $checkIDs = $_REQUEST['ID'];
463 $entityID = CRM_Utils_Type::escape($_REQUEST['entityID'], 'String');
353ffa53 464 $action = CRM_Utils_Type::escape($_REQUEST['action'], 'String');
6a488035 465 foreach ($checkIDs as $key => $value) {
045f52a3
TO
466 if ((substr($value, 0, 7) == "mark_x_" && $action == 'Assign') || (substr($value, 0, 7) == "mark_y_" && $action == 'Remove')) {
467 $contributions = explode("_", $value);
6a488035
TO
468 $cIDs[] = $contributions[2];
469 }
470 }
471
df33c0cd
JG
472 foreach ($cIDs as $value) {
473 if ($action == 'Remove' || $action == 'Assign') {
be2fb01f 474 $params = [
c301f76e 475 'entity_id' => $value,
476 'entity_table' => 'civicrm_financial_trxn',
477 'batch_id' => $entityID,
be2fb01f 478 ];
df33c0cd
JG
479 try {
480 if ($action == 'Assign') {
481 CRM_Batch_BAO_EntityBatch::create($params);
482 }
483 else {
484 $delete = \Civi\Api4\EntityBatch::delete(FALSE);
485 foreach ($params as $field => $val) {
486 $delete->addWhere($field, '=', $val);
487 }
488 $delete->execute()->count();
99f76265 489 }
df33c0cd
JG
490 }
491 catch (\CRM_Core_Exception $e) {
492 $errorMessage = $e->getMessage();
6a488035
TO
493 }
494 }
495 }
df33c0cd
JG
496 if ($errorMessage ?? FALSE) {
497 $status = ['status' => $errorMessage];
6a488035
TO
498 }
499 else {
df33c0cd 500 $status = ['status' => 'record-updated-success'];
6a488035 501 }
ecdef330 502 CRM_Utils_JSON::output($status);
6a488035
TO
503 }
504
2d4bdc58
EM
505 /**
506 * @throws \API_Exception
507 * @throws \CRM_Core_Exception
508 */
509 public static function getBatchSummary(): void {
510 CRM_Utils_JSON::output(self::makeBatchSummary(CRM_Utils_Type::escape($_REQUEST['batchID'], 'Integer')));
973a1687
SB
511 }
512
40c60cca 513 /**
2d4bdc58 514 * Get a summary of the batch..
40c60cca
SB
515 *
516 * @param $batchID
40c60cca
SB
517 *
518 * @return array
2d4bdc58
EM
519 * @throws \API_Exception
520 * @throws \CRM_Core_Exception
40c60cca 521 */
2d4bdc58
EM
522 public static function makeBatchSummary(int $batchID): array {
523 // We use permissions false because the old function did that & we
524 // have not tested to ensure permissions are correct - but ideally
525 // we would setCheckPermissions = TRUE.
526 $batchInfo = Batch::get(FALSE)
527 ->addWhere('id', '=', $batchID)
528 ->addSelect(
529 'description',
530 'item_count',
531 'total',
532 'created_date',
533 'created_id.display_name',
534 'status_id:label',
535 'payment_instrument_id:label'
536 )
537 ->execute()->first();
be2fb01f 538 $batchTotals = CRM_Batch_BAO_Batch::batchTotals([$batchID]);
2d4bdc58
EM
539 return [
540 'created_by' => $batchInfo['created_id.display_name'],
541 'status' => $batchInfo['status_id:label'],
542 'description' => $batchInfo['description'],
543 'payment_instrument' => $batchInfo['payment_instrument_id:label'],
544 'item_count' => $batchInfo['item_count'],
c301f76e 545 'assigned_item_count' => $batchTotals[$batchID]['item_count'],
2d4bdc58
EM
546 'total' => Civi::format()->money($batchInfo['total']),
547 'assigned_total' => Civi::format()->money($batchTotals[$batchID]['total']),
548 'opened_date' => CRM_Utils_Date::customFormat($batchInfo['created_date']),
be2fb01f 549 ];
6a488035 550 }
86b25ace 551
6a488035 552}