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