Merge pull request #20995 from ixiam/dev-import-phone-extension
[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();
9c1bc317 41 $financialAccountType = $financialAccountType[$_GET['_value']] ?? NULL;
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
6a488035
TO
201 case 'delete':
202 $params = $recordID;
6a488035
TO
203 break;
204 }
205
206 if (method_exists($recordBAO, $methods[$op]) & !empty($params)) {
92e088c9 207 $updated = call_user_func_array(array($recordBAO, $methods[$op]), array(&$params));
6a488035 208 if ($updated) {
5de2e536
PN
209 $redirectStatus = $updated->status_id;
210 if ($batchStatus[$updated->status_id] == "Reopened") {
211 $redirectStatus = array_search("Open", $batchStatus);
212 }
be2fb01f 213 $response = [
5de2e536
PN
214 'status' => 'record-updated-success',
215 'status_id' => $redirectStatus,
be2fb01f 216 ];
6a488035
TO
217 }
218 }
219 }
220 }
ecdef330 221 CRM_Utils_JSON::output($response);
6a488035
TO
222 }
223
478c39cf 224 /**
9d311c8f
CW
225 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
226 * @deprecated
478c39cf 227 *
9d311c8f 228 * @return string|wtf??
478c39cf 229 */
73416ad0 230 public static function getFinancialTransactionsList() {
be2fb01f 231 $sortMapper = [
c301f76e 232 0 => '',
233 1 => '',
234 2 => 'sort_name',
235 3 => 'amount',
236 4 => 'trxn_id',
237 5 => 'transaction_date',
0a3dfa41
PN
238 6 => 'receive_date',
239 7 => 'payment_method',
240 8 => 'status',
241 9 => 'name',
be2fb01f 242 ];
6a488035 243
353ffa53 244 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
5b3fba71 245 $return = isset($_REQUEST['return']) ? CRM_Utils_Type::escape($_REQUEST['return'], 'Boolean') : FALSE;
353ffa53
TO
246 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
247 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
248 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
6a488035 249 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
edc80cda 250 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric');
353ffa53 251 $entityID = isset($_REQUEST['entityID']) ? CRM_Utils_Type::escape($_REQUEST['entityID'], 'String') : NULL;
6a488035 252 $notPresent = isset($_REQUEST['notPresent']) ? CRM_Utils_Type::escape($_REQUEST['notPresent'], 'String') : NULL;
353ffa53 253 $statusID = isset($_REQUEST['statusID']) ? CRM_Utils_Type::escape($_REQUEST['statusID'], 'String') : NULL;
f7dbf5d9 254 $search = isset($_REQUEST['search']);
6a488035
TO
255
256 $params = $_POST;
257 if ($sort && $sortOrder) {
258 $params['sortBy'] = $sort . ' ' . $sortOrder;
259 }
260
be2fb01f 261 $returnvalues = [
c301f76e 262 'civicrm_financial_trxn.payment_instrument_id as payment_method',
263 'civicrm_contribution.contact_id as contact_id',
264 'civicrm_contribution.id as contributionID',
265 'contact_a.sort_name',
266 'civicrm_financial_trxn.total_amount as amount',
267 'civicrm_financial_trxn.trxn_id as trxn_id',
268 'contact_a.contact_type',
269 'contact_a.contact_sub_type',
270 'civicrm_financial_trxn.trxn_date as transaction_date',
dbf605ab 271 'civicrm_contribution.receive_date as receive_date',
6579a0f5
PN
272 'civicrm_financial_type.name',
273 'civicrm_financial_trxn.currency as currency',
c301f76e 274 'civicrm_financial_trxn.status_id as status',
275 'civicrm_financial_trxn.check_number as check_number',
eb71b3f2 276 'civicrm_financial_trxn.card_type_id',
fe681d8d 277 'civicrm_financial_trxn.pan_truncation',
be2fb01f 278 ];
6a488035 279
be2fb01f 280 $columnHeader = [
c301f76e 281 'contact_type' => '',
282 'sort_name' => ts('Contact Name'),
283 'amount' => ts('Amount'),
284 'trxn_id' => ts('Trxn ID'),
dbf605ab
PN
285 'transaction_date' => ts('Transaction Date'),
286 'receive_date' => ts('Received'),
c301f76e 287 'payment_method' => ts('Payment Method'),
288 'status' => ts('Status'),
289 'name' => ts('Type'),
be2fb01f 290 ];
6a488035
TO
291
292 if ($sort && $sortOrder) {
293 $params['sortBy'] = $sort . ' ' . $sortOrder;
294 }
295
296 $params['page'] = ($offset / $rowCount) + 1;
297 $params['rp'] = $rowCount;
298
299 $params['context'] = $context;
353ffa53 300 $params['offset'] = ($params['page'] - 1) * $params['rp'];
6a488035 301 $params['rowCount'] = $params['rp'];
9c1bc317 302 $params['sort'] = $params['sortBy'] ?? NULL;
353ffa53 303 $params['total'] = 0;
6a488035
TO
304
305 // get batch list
306 if (isset($notPresent)) {
307 $financialItem = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, $notPresent, $params);
308 if ($search) {
309 $unassignedTransactions = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, $notPresent, $params, TRUE);
310 }
311 else {
312 $unassignedTransactions = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, $notPresent, NULL, TRUE);
313 }
314 while ($unassignedTransactions->fetch()) {
315 $unassignedTransactionsCount[] = $unassignedTransactions->id;
316 }
317 if (!empty($unassignedTransactionsCount)) {
318 $params['total'] = count($unassignedTransactionsCount);
319 }
320
321 }
322 else {
323 $financialItem = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, NULL, $params);
324 $assignedTransactions = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues);
325 while ($assignedTransactions->fetch()) {
326 $assignedTransactionsCount[] = $assignedTransactions->id;
327 }
328 if (!empty($assignedTransactionsCount)) {
329 $params['total'] = count($assignedTransactionsCount);
330 }
331 }
be2fb01f 332 $financialitems = [];
5de2e536 333 if ($statusID) {
be2fb01f 334 $batchStatuses = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', ['labelColumn' => 'name', 'condition' => " v.value={$statusID}"]);
5de2e536
PN
335 $batchStatus = $batchStatuses[$statusID];
336 }
6a488035 337 while ($financialItem->fetch()) {
be2fb01f 338 $row[$financialItem->id] = [];
6a488035
TO
339 foreach ($columnHeader as $columnKey => $columnValue) {
340 if ($financialItem->contact_sub_type && $columnKey == 'contact_type') {
341 $row[$financialItem->id][$columnKey] = $financialItem->contact_sub_type;
342 continue;
343 }
344 $row[$financialItem->id][$columnKey] = $financialItem->$columnKey;
e8b11533 345 if ($columnKey == 'sort_name' && $financialItem->$columnKey && $financialItem->contact_id) {
92fcb95f
TO
346 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=" . $financialItem->contact_id);
347 $row[$financialItem->id][$columnKey] = '<a href=' . $url . '>' . $financialItem->$columnKey . '</a>';
6a488035
TO
348 }
349 elseif ($columnKey == 'payment_method' && $financialItem->$columnKey) {
b75ec2eb 350 $row[$financialItem->id][$columnKey] = CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $financialItem->$columnKey);
6a488035 351 if ($row[$financialItem->id][$columnKey] == 'Check') {
6154e561 352 $checkNumber = $financialItem->check_number ? ' (' . $financialItem->check_number . ')' : '';
353 $row[$financialItem->id][$columnKey] = $row[$financialItem->id][$columnKey] . $checkNumber;
6a488035
TO
354 }
355 }
356 elseif ($columnKey == 'amount' && $financialItem->$columnKey) {
357 $row[$financialItem->id][$columnKey] = CRM_Utils_Money::format($financialItem->$columnKey, $financialItem->currency);
358 }
359 elseif ($columnKey == 'transaction_date' && $financialItem->$columnKey) {
045f52a3 360 $row[$financialItem->id][$columnKey] = CRM_Utils_Date::customFormat($financialItem->$columnKey);
6a488035 361 }
dbf605ab
PN
362 elseif ($columnKey == 'receive_date' && $financialItem->$columnKey) {
363 $row[$financialItem->id][$columnKey] = CRM_Utils_Date::customFormat($financialItem->$columnKey);
364 }
6a488035 365 elseif ($columnKey == 'status' && $financialItem->$columnKey) {
b75ec2eb 366 $row[$financialItem->id][$columnKey] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $financialItem->$columnKey);
6a488035
TO
367 }
368 }
be2fb01f 369 if (isset($batchStatus) && in_array($batchStatus, ['Open', 'Reopened'])) {
6a488035
TO
370 if (isset($notPresent)) {
371 $js = "enableActions('x')";
86bfa4f6 372 $row[$financialItem->id]['check'] = "<input type='checkbox' id='mark_x_" . $financialItem->id . "' name='mark_x_" . $financialItem->id . "' value='1' onclick={$js}></input>";
87dab4a4 373 $row[$financialItem->id]['action'] = CRM_Core_Action::formLink(
243a28d6 374 (new CRM_Financial_Form_BatchTransaction())->links(),
045f52a3 375 NULL,
be2fb01f 376 [
87dab4a4
AH
377 'id' => $financialItem->id,
378 'contid' => $financialItem->contributionID,
21dfd5f5 379 'cid' => $financialItem->contact_id,
be2fb01f 380 ],
87dab4a4
AH
381 ts('more'),
382 FALSE,
383 'financialItem.batch.row',
384 'FinancialItem',
385 $financialItem->id
386 );
6a488035
TO
387 }
388 else {
389 $js = "enableActions('y')";
86bfa4f6 390 $row[$financialItem->id]['check'] = "<input type='checkbox' id='mark_y_" . $financialItem->id . "' name='mark_y_" . $financialItem->id . "' value='1' onclick={$js}></input>";
87dab4a4 391 $row[$financialItem->id]['action'] = CRM_Core_Action::formLink(
243a28d6 392 (new CRM_Financial_Page_BatchTransaction())->links(),
045f52a3 393 NULL,
be2fb01f 394 [
87dab4a4
AH
395 'id' => $financialItem->id,
396 'contid' => $financialItem->contributionID,
21dfd5f5 397 'cid' => $financialItem->contact_id,
be2fb01f 398 ],
87dab4a4
AH
399 ts('more'),
400 FALSE,
401 'financialItem.batch.row',
402 'FinancialItem',
403 $financialItem->id
404 );
6a488035
TO
405 }
406 }
407 else {
408 $row[$financialItem->id]['check'] = NULL;
23f0202e 409 $tempBAO = new CRM_Financial_Page_BatchTransaction();
410 $links = $tempBAO->links();
6a488035 411 unset($links['remove']);
87dab4a4
AH
412 $row[$financialItem->id]['action'] = CRM_Core_Action::formLink(
413 $links,
045f52a3 414 NULL,
be2fb01f 415 [
87dab4a4
AH
416 'id' => $financialItem->id,
417 'contid' => $financialItem->contributionID,
21dfd5f5 418 'cid' => $financialItem->contact_id,
be2fb01f 419 ],
87dab4a4
AH
420 ts('more'),
421 FALSE,
422 'financialItem.batch.row',
423 'FinancialItem',
424 $financialItem->id
425 );
6a488035 426 }
e8b11533 427 if ($financialItem->contact_id) {
e71c1326 428 $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 429 }
243a28d6 430 // @todo: Is this right? Shouldn't it be adding to the array as we loop?
6a488035
TO
431 $financialitems = $row;
432 }
433
045f52a3 434 $iFilteredTotal = $iTotal = $params['total'];
be2fb01f 435 $selectorElements = [
c301f76e 436 'check',
437 'contact_type',
438 'sort_name',
439 'amount',
440 'trxn_id',
441 'transaction_date',
dbf605ab 442 'receive_date',
c301f76e 443 'payment_method',
444 'status',
445 'name',
446 'action',
be2fb01f 447 ];
6a488035 448
478c39cf 449 if ($return) {
450 return CRM_Utils_JSON::encodeDataTableSelector($financialitems, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
451 }
d42a224c 452 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
6a488035
TO
453 echo CRM_Utils_JSON::encodeDataTableSelector($financialitems, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
454 CRM_Utils_System::civiExit();
455 }
456
00be9182 457 public static function bulkAssignRemove() {
6a488035
TO
458 $checkIDs = $_REQUEST['ID'];
459 $entityID = CRM_Utils_Type::escape($_REQUEST['entityID'], 'String');
353ffa53 460 $action = CRM_Utils_Type::escape($_REQUEST['action'], 'String');
6a488035 461 foreach ($checkIDs as $key => $value) {
045f52a3
TO
462 if ((substr($value, 0, 7) == "mark_x_" && $action == 'Assign') || (substr($value, 0, 7) == "mark_y_" && $action == 'Remove')) {
463 $contributions = explode("_", $value);
6a488035
TO
464 $cIDs[] = $contributions[2];
465 }
466 }
467
468 $batchPID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $entityID, 'payment_instrument_id');
b7617307 469 $paymentInstrument = CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $batchPID);
6a488035
TO
470 foreach ($cIDs as $key => $value) {
471 $recordPID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $value, 'payment_instrument_id');
353ffa53 472 if ($action == 'Remove' || ($recordPID == $batchPID && $action == 'Assign') || !isset($batchPID)) {
be2fb01f 473 $params = [
c301f76e 474 'entity_id' => $value,
475 'entity_table' => 'civicrm_financial_trxn',
476 'batch_id' => $entityID,
be2fb01f 477 ];
6a488035 478 if ($action == 'Assign') {
ee20d7be 479 $updated = CRM_Batch_BAO_EntityBatch::create($params);
6a488035
TO
480 }
481 else {
99f76265
CW
482 $delete = \Civi\Api4\EntityBatch::delete(FALSE);
483 foreach ($params as $field => $val) {
484 $delete->addWhere($field, '=', $val);
485 }
486 $updated = $delete->execute()->count();
6a488035
TO
487 }
488 }
489 }
490 if ($updated) {
be2fb01f 491 $status = ['status' => 'record-updated-success'];
6a488035
TO
492 }
493 else {
be2fb01f 494 $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 495 }
ecdef330 496 CRM_Utils_JSON::output($status);
6a488035
TO
497 }
498
00be9182 499 public static function getBatchSummary() {
6a488035 500 $batchID = CRM_Utils_Type::escape($_REQUEST['batchID'], 'String');
be2fb01f 501 $params = ['id' => $batchID];
86b25ace 502
6c9111b5 503 $batchSummary = self::makeBatchSummary($batchID, $params);
973a1687
SB
504
505 CRM_Utils_JSON::output($batchSummary);
506 }
507
40c60cca
SB
508 /**
509 * Makes an array of the batch's summary and returns array to parent getBatchSummary() function.
510 *
511 * @param $batchID
512 * @param $params
513 *
514 * @return array
515 */
973a1687 516 public static function makeBatchSummary($batchID, $params) {
86b25ace 517 $batchInfo = CRM_Batch_BAO_Batch::retrieve($params, $value);
be2fb01f
CW
518 $batchTotals = CRM_Batch_BAO_Batch::batchTotals([$batchID]);
519 $batchSummary = [
c301f76e 520 'created_by' => CRM_Contact_BAO_Contact::displayName($batchInfo->created_id),
40c60cca 521 'status' => CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'status_id', $batchInfo->status_id),
c301f76e 522 'description' => $batchInfo->description,
973a1687 523 'payment_instrument' => CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $batchInfo->payment_instrument_id),
c301f76e 524 'item_count' => $batchInfo->item_count,
525 'assigned_item_count' => $batchTotals[$batchID]['item_count'],
526 'total' => CRM_Utils_Money::format($batchInfo->total),
527 'assigned_total' => CRM_Utils_Money::format($batchTotals[$batchID]['total']),
528 'opened_date' => CRM_Utils_Date::customFormat($batchInfo->created_date),
be2fb01f 529 ];
6a488035 530
973a1687 531 return $batchSummary;
6a488035 532 }
86b25ace 533
6a488035 534}