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