Minor code cleanups around invoicing assignment
[civicrm-core.git] / CRM / Financial / Page / AJAX.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
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';
edc80cda 270 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric');
353ffa53 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',
eb71b3f2 296 'civicrm_financial_trxn.card_type_id',
fe681d8d 297 'civicrm_financial_trxn.pan_truncation',
c301f76e 298 );
6a488035 299
c301f76e 300 $columnHeader = array(
301 'contact_type' => '',
302 'sort_name' => ts('Contact Name'),
303 'amount' => ts('Amount'),
304 'trxn_id' => ts('Trxn ID'),
dbf605ab
PN
305 'transaction_date' => ts('Transaction Date'),
306 'receive_date' => ts('Received'),
c301f76e 307 'payment_method' => ts('Payment Method'),
308 'status' => ts('Status'),
309 'name' => ts('Type'),
310 );
6a488035
TO
311
312 if ($sort && $sortOrder) {
313 $params['sortBy'] = $sort . ' ' . $sortOrder;
314 }
315
316 $params['page'] = ($offset / $rowCount) + 1;
317 $params['rp'] = $rowCount;
318
319 $params['context'] = $context;
353ffa53 320 $params['offset'] = ($params['page'] - 1) * $params['rp'];
6a488035 321 $params['rowCount'] = $params['rp'];
353ffa53
TO
322 $params['sort'] = CRM_Utils_Array::value('sortBy', $params);
323 $params['total'] = 0;
6a488035
TO
324
325 // get batch list
326 if (isset($notPresent)) {
327 $financialItem = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, $notPresent, $params);
328 if ($search) {
329 $unassignedTransactions = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, $notPresent, $params, TRUE);
330 }
331 else {
332 $unassignedTransactions = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, $notPresent, NULL, TRUE);
333 }
334 while ($unassignedTransactions->fetch()) {
335 $unassignedTransactionsCount[] = $unassignedTransactions->id;
336 }
337 if (!empty($unassignedTransactionsCount)) {
338 $params['total'] = count($unassignedTransactionsCount);
339 }
340
341 }
342 else {
343 $financialItem = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues, NULL, $params);
344 $assignedTransactions = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityID, $returnvalues);
345 while ($assignedTransactions->fetch()) {
346 $assignedTransactionsCount[] = $assignedTransactions->id;
347 }
348 if (!empty($assignedTransactionsCount)) {
349 $params['total'] = count($assignedTransactionsCount);
350 }
351 }
352 $financialitems = array();
5de2e536
PN
353 if ($statusID) {
354 $batchStatuses = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name', 'condition' => " v.value={$statusID}"));
355 $batchStatus = $batchStatuses[$statusID];
356 }
6a488035
TO
357 while ($financialItem->fetch()) {
358 $row[$financialItem->id] = array();
359 foreach ($columnHeader as $columnKey => $columnValue) {
360 if ($financialItem->contact_sub_type && $columnKey == 'contact_type') {
361 $row[$financialItem->id][$columnKey] = $financialItem->contact_sub_type;
362 continue;
363 }
364 $row[$financialItem->id][$columnKey] = $financialItem->$columnKey;
e8b11533 365 if ($columnKey == 'sort_name' && $financialItem->$columnKey && $financialItem->contact_id) {
92fcb95f
TO
366 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=" . $financialItem->contact_id);
367 $row[$financialItem->id][$columnKey] = '<a href=' . $url . '>' . $financialItem->$columnKey . '</a>';
6a488035
TO
368 }
369 elseif ($columnKey == 'payment_method' && $financialItem->$columnKey) {
b75ec2eb 370 $row[$financialItem->id][$columnKey] = CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $financialItem->$columnKey);
6a488035 371 if ($row[$financialItem->id][$columnKey] == 'Check') {
6154e561 372 $checkNumber = $financialItem->check_number ? ' (' . $financialItem->check_number . ')' : '';
373 $row[$financialItem->id][$columnKey] = $row[$financialItem->id][$columnKey] . $checkNumber;
6a488035
TO
374 }
375 }
376 elseif ($columnKey == 'amount' && $financialItem->$columnKey) {
377 $row[$financialItem->id][$columnKey] = CRM_Utils_Money::format($financialItem->$columnKey, $financialItem->currency);
378 }
379 elseif ($columnKey == 'transaction_date' && $financialItem->$columnKey) {
045f52a3 380 $row[$financialItem->id][$columnKey] = CRM_Utils_Date::customFormat($financialItem->$columnKey);
6a488035 381 }
dbf605ab
PN
382 elseif ($columnKey == 'receive_date' && $financialItem->$columnKey) {
383 $row[$financialItem->id][$columnKey] = CRM_Utils_Date::customFormat($financialItem->$columnKey);
384 }
6a488035 385 elseif ($columnKey == 'status' && $financialItem->$columnKey) {
b75ec2eb 386 $row[$financialItem->id][$columnKey] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $financialItem->$columnKey);
6a488035
TO
387 }
388 }
5de2e536 389 if (isset($batchStatus) && in_array($batchStatus, array('Open', 'Reopened'))) {
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;
23f0202e 429 $tempBAO = new CRM_Financial_Page_BatchTransaction();
430 $links = $tempBAO->links();
6a488035 431 unset($links['remove']);
87dab4a4
AH
432 $row[$financialItem->id]['action'] = CRM_Core_Action::formLink(
433 $links,
045f52a3 434 NULL,
87dab4a4
AH
435 array(
436 'id' => $financialItem->id,
437 'contid' => $financialItem->contributionID,
21dfd5f5 438 'cid' => $financialItem->contact_id,
87dab4a4
AH
439 ),
440 ts('more'),
441 FALSE,
442 'financialItem.batch.row',
443 'FinancialItem',
444 $financialItem->id
445 );
6a488035 446 }
e8b11533
PN
447 if ($financialItem->contact_id) {
448 $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);
449 }
6a488035
TO
450 $financialitems = $row;
451 }
452
045f52a3 453 $iFilteredTotal = $iTotal = $params['total'];
c301f76e 454 $selectorElements = array(
455 'check',
456 'contact_type',
457 'sort_name',
458 'amount',
459 'trxn_id',
460 'transaction_date',
dbf605ab 461 'receive_date',
c301f76e 462 'payment_method',
463 'status',
464 'name',
465 'action',
466 );
6a488035 467
478c39cf 468 if ($return) {
469 return CRM_Utils_JSON::encodeDataTableSelector($financialitems, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
470 }
d42a224c 471 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
6a488035
TO
472 echo CRM_Utils_JSON::encodeDataTableSelector($financialitems, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
473 CRM_Utils_System::civiExit();
474 }
475
00be9182 476 public static function bulkAssignRemove() {
6a488035
TO
477 $checkIDs = $_REQUEST['ID'];
478 $entityID = CRM_Utils_Type::escape($_REQUEST['entityID'], 'String');
353ffa53 479 $action = CRM_Utils_Type::escape($_REQUEST['action'], 'String');
6a488035 480 foreach ($checkIDs as $key => $value) {
045f52a3
TO
481 if ((substr($value, 0, 7) == "mark_x_" && $action == 'Assign') || (substr($value, 0, 7) == "mark_y_" && $action == 'Remove')) {
482 $contributions = explode("_", $value);
6a488035
TO
483 $cIDs[] = $contributions[2];
484 }
485 }
486
487 $batchPID = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $entityID, 'payment_instrument_id');
b7617307 488 $paymentInstrument = CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $batchPID);
6a488035
TO
489 foreach ($cIDs as $key => $value) {
490 $recordPID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $value, 'payment_instrument_id');
353ffa53 491 if ($action == 'Remove' || ($recordPID == $batchPID && $action == 'Assign') || !isset($batchPID)) {
c301f76e 492 $params = array(
493 'entity_id' => $value,
494 'entity_table' => 'civicrm_financial_trxn',
495 'batch_id' => $entityID,
496 );
6a488035 497 if ($action == 'Assign') {
ee20d7be 498 $updated = CRM_Batch_BAO_EntityBatch::create($params);
6a488035
TO
499 }
500 else {
ee20d7be 501 $updated = CRM_Batch_BAO_EntityBatch::del($params);
6a488035
TO
502 }
503 }
504 }
505 if ($updated) {
506 $status = array('status' => 'record-updated-success');
507 }
508 else {
481a74f4 509 $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 510 }
ecdef330 511 CRM_Utils_JSON::output($status);
6a488035
TO
512 }
513
00be9182 514 public static function getBatchSummary() {
6a488035
TO
515 $batchID = CRM_Utils_Type::escape($_REQUEST['batchID'], 'String');
516 $params = array('id' => $batchID);
86b25ace 517
6c9111b5 518 $batchSummary = self::makeBatchSummary($batchID, $params);
973a1687
SB
519
520 CRM_Utils_JSON::output($batchSummary);
521 }
522
40c60cca
SB
523 /**
524 * Makes an array of the batch's summary and returns array to parent getBatchSummary() function.
525 *
526 * @param $batchID
527 * @param $params
528 *
529 * @return array
530 */
973a1687 531 public static function makeBatchSummary($batchID, $params) {
86b25ace 532 $batchInfo = CRM_Batch_BAO_Batch::retrieve($params, $value);
6a488035 533 $batchTotals = CRM_Batch_BAO_Batch::batchTotals(array($batchID));
86b25ace 534 $batchSummary = array(
c301f76e 535 'created_by' => CRM_Contact_BAO_Contact::displayName($batchInfo->created_id),
40c60cca 536 'status' => CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'status_id', $batchInfo->status_id),
c301f76e 537 'description' => $batchInfo->description,
973a1687 538 'payment_instrument' => CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $batchInfo->payment_instrument_id),
c301f76e 539 'item_count' => $batchInfo->item_count,
540 'assigned_item_count' => $batchTotals[$batchID]['item_count'],
541 'total' => CRM_Utils_Money::format($batchInfo->total),
542 'assigned_total' => CRM_Utils_Money::format($batchTotals[$batchID]['total']),
543 'opened_date' => CRM_Utils_Date::customFormat($batchInfo->created_date),
544 );
6a488035 545
973a1687 546 return $batchSummary;
6a488035 547 }
86b25ace 548
6a488035 549}