4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
38 * This class contains all the function that are called using AJAX
40 class CRM_Financial_Page_AJAX
{
42 * Function to get financial accounts of required account relationship
43 * $financialAccountType array with key account relationship and value financial account type option groups
49 static function jqFinancial($config) {
50 if (!isset($_GET['_value']) ||
51 empty($_GET['_value'])) {
52 CRM_Utils_System
::civiExit();
55 if ($_GET['_value'] == 'select') {
56 $result = CRM_Contribute_PseudoConstant
::financialAccount();
59 $financialAccountType = array(
61 '3' => 1, //AR relation
64 '7' => 4, //cost of sales
65 '8' => 1, //premium inventory
66 '9' => 3, //discount account is
67 '10' => 2, //sales tax liability
69 $financialAccountType = CRM_Utils_Array
::value($_GET['_value'], $financialAccountType);
70 $result = CRM_Contribute_PseudoConstant
::financialAccount(NULL, $financialAccountType);
71 if ($financialAccountType) {
72 $defaultId = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = $financialAccountType");
77 'name' => ts('- select -'),
83 foreach ($result as $id => $name) {
84 $selectedArray = array();
85 if ($id == $defaultId) {
86 $selectedArray['selected'] = 'Selected';
94 CRM_Utils_JSON
::output($elements);
100 static function jqFinancialRelation($config) {
101 if (!isset($_GET['_value']) ||
102 empty($_GET['_value'])) {
103 CRM_Utils_System
::civiExit();
106 if ($_GET['_value'] == 'select') {
107 $result = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
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
116 $financialAccountTypeId = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', $_GET['_value'], 'financial_account_type_id');
117 $result = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
122 'name' => ts('- Select Financial Account Relationship -'),
127 $countResult = count($financialAccountType[$financialAccountTypeId]);
128 if (!empty($result)) {
129 foreach ($result as $id => $name) {
130 if (in_array($id, $financialAccountType[$financialAccountTypeId]) && $_GET['_value'] != 'select') {
131 if ($countResult != 1){
141 'selected' => 'Selected',
145 elseif ($_GET['_value'] == 'select'){
153 CRM_Utils_JSON
::output($elements);
159 static function jqFinancialType($config) {
160 if (! isset($_GET['_value']) ||
161 empty($_GET['_value'])) {
162 CRM_Utils_System
::civiExit();
165 $elements = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Product', $_GET['_value'], 'financial_type_id');
166 CRM_Utils_JSON
::output($elements);
170 * Callback to perform action on batch records.
172 static function assignRemove() {
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);
178 $records[] = $recordID;
182 $entityID = CRM_Utils_Array
::value('entityID', $_POST);
184 'assign' => 'addBatchEntity',
185 'remove' => 'removeBatchEntity',
186 'reopen' => 'create',
188 'delete' => 'deleteBatch',
190 if ($op == 'close') {
191 $totals = CRM_Batch_BAO_Batch
::batchTotals($records);
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) {
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');
208 $paymentInstrument = CRM_Core_OptionGroup
::getLabel('payment_instrument',$batchPID);
209 if ($op == 'remove' ||
($recordPID == $batchPID && $op == 'assign') ||
!isset($batchPID)) {
211 'entity_id' => $recordID,
212 'entity_table' => 'civicrm_financial_trxn',
213 'batch_id' => $entityID,
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)));
221 // Update totals when closing a batch
222 $params = $totals[$recordID];
224 $status = $op == 'close' ?
'Closed' : 'Reopened';
225 $ids['batchID'] = $recordID;
226 $batchStatus = CRM_Core_PseudoConstant
::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name'));
227 $params['status_id'] = CRM_Utils_Array
::key($status, $batchStatus);
228 $session = CRM_Core_Session
::singleton();
229 $params['modified_date'] = date('YmdHis');
230 $params['modified_id'] = $session->get('userID');
231 $params['id'] = $recordID;
232 $context = "financialBatch";
236 CRM_Utils_System
::redirect("civicrm/financial/batch/export?reset=1&id=$recordID");
241 $context = "financialBatch";
245 if (method_exists($recordBAO, $methods[$op]) & !empty($params)) {
246 if (isset($context)) {
247 $updated = call_user_func_array(array($recordBAO, $methods[$op]), array(&$params, $ids, $context));
250 $updated = call_user_func_array(array($recordBAO, $methods[$op]), array(&$params, $ids));
253 $response = array('status' => 'record-updated-success');
258 CRM_Utils_JSON
::output($response);
261 static function getFinancialTransactionsList() {
264 0 => '', 1 => '', 2 => 'sort_name',
265 3 => 'amount', 4 => 'trxn_id', 5 => 'transaction_date', 6 => 'payment_method', 7 => 'status', 8 => 'name',
268 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
269 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
270 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
271 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
272 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
273 $context = isset($_REQUEST['context']) ? CRM_Utils_Type
::escape($_REQUEST['context'], 'String') : NULL;
274 $entityID = isset($_REQUEST['entityID']) ? CRM_Utils_Type
::escape($_REQUEST['entityID'], 'String') : NULL;
275 $notPresent = isset($_REQUEST['notPresent']) ? CRM_Utils_Type
::escape($_REQUEST['notPresent'], 'String') : NULL;
276 $statusID = isset($_REQUEST['statusID']) ? CRM_Utils_Type
::escape($_REQUEST['statusID'], 'String') : NULL;
277 $search = isset($_REQUEST['search']) ?
TRUE : FALSE;
280 if ($sort && $sortOrder) {
281 $params['sortBy'] = $sort . ' ' . $sortOrder;
286 'civicrm_financial_trxn.payment_instrument_id as payment_method',
287 'civicrm_contribution.contact_id as contact_id',
288 'civicrm_contribution.id as contributionID',
289 'contact_a.sort_name',
290 'civicrm_financial_trxn.total_amount as amount',
291 'civicrm_financial_trxn.trxn_id as trxn_id',
292 'contact_a.contact_type',
293 'contact_a.contact_sub_type',
294 'civicrm_financial_trxn.trxn_date as transaction_date',
296 'civicrm_contribution.currency as currency',
297 'civicrm_financial_trxn.status_id as status',
298 'civicrm_financial_trxn.check_number as check_number',
303 'contact_type' => '',
304 'sort_name' => ts('Contact Name'),
305 'amount' => ts('Amount'),
306 'trxn_id' => ts('Trxn ID'),
307 'transaction_date' => ts('Received'),
308 'payment_method' => ts('Payment Method'),
309 'status' => ts('Status'),
310 'name' => ts('Type'),
313 if ($sort && $sortOrder) {
314 $params['sortBy'] = $sort . ' ' . $sortOrder;
317 $params['page'] = ($offset / $rowCount) +
1;
318 $params['rp'] = $rowCount;
320 $params['context'] = $context;
321 $params['offset'] = ($params['page'] - 1) * $params['rp'];
322 $params['rowCount'] = $params['rp'];
323 $params['sort'] = CRM_Utils_Array
::value('sortBy', $params);
324 $params['total'] = 0;
327 if (isset($notPresent)) {
328 $financialItem = CRM_Batch_BAO_Batch
::getBatchFinancialItems($entityID, $returnvalues, $notPresent, $params);
330 $unassignedTransactions = CRM_Batch_BAO_Batch
::getBatchFinancialItems($entityID, $returnvalues, $notPresent, $params, TRUE);
333 $unassignedTransactions = CRM_Batch_BAO_Batch
::getBatchFinancialItems($entityID, $returnvalues, $notPresent, NULL, TRUE);
335 while ($unassignedTransactions->fetch()) {
336 $unassignedTransactionsCount[] = $unassignedTransactions->id
;
338 if (!empty($unassignedTransactionsCount)) {
339 $params['total'] = count($unassignedTransactionsCount);
344 $financialItem = CRM_Batch_BAO_Batch
::getBatchFinancialItems($entityID, $returnvalues, NULL, $params);
345 $assignedTransactions = CRM_Batch_BAO_Batch
::getBatchFinancialItems($entityID, $returnvalues);
346 while ($assignedTransactions->fetch()) {
347 $assignedTransactionsCount[] = $assignedTransactions->id
;
349 if (!empty($assignedTransactionsCount)) {
350 $params['total'] = count($assignedTransactionsCount);
353 $financialitems = array();
354 while ($financialItem->fetch()) {
355 $row[$financialItem->id
] = array();
356 foreach ($columnHeader as $columnKey => $columnValue) {
357 if ($financialItem->contact_sub_type
&& $columnKey == 'contact_type') {
358 $row[$financialItem->id
][$columnKey] = $financialItem->contact_sub_type
;
361 $row[$financialItem->id
][$columnKey] = $financialItem->$columnKey;
362 if ($columnKey == 'sort_name' && $financialItem->$columnKey) {
363 $url = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid=".$financialItem->contact_id
);
364 $row[$financialItem->id
][$columnKey] = '<a href='.$url.'>'.$financialItem->$columnKey.'</a>';
366 elseif ($columnKey == 'payment_method' && $financialItem->$columnKey) {
367 $row[$financialItem->id
][$columnKey] = CRM_Core_OptionGroup
::getLabel('payment_instrument', $financialItem->$columnKey);
368 if ($row[$financialItem->id
][$columnKey] == 'Check') {
369 $row[$financialItem->id
][$columnKey] = $row[$financialItem->id
][$columnKey].' ('.$financialItem->check_number
.')';
372 elseif ($columnKey == 'amount' && $financialItem->$columnKey) {
373 $row[$financialItem->id
][$columnKey] = CRM_Utils_Money
::format($financialItem->$columnKey, $financialItem->currency
);
375 elseif ($columnKey == 'transaction_date' && $financialItem->$columnKey) {
376 $row[$financialItem->id
][$columnKey] = CRM_Utils_Date
::customFormat($financialItem->$columnKey);
378 elseif ($columnKey == 'status' && $financialItem->$columnKey) {
379 $row[$financialItem->id
][$columnKey] = CRM_Core_OptionGroup
::getLabel('contribution_status', $financialItem->$columnKey);
382 if ($statusID == CRM_Core_OptionGroup
::getValue('batch_status','Open')) {
383 if (isset($notPresent)) {
384 $js = "enableActions('x')";
385 $row[$financialItem->id
]['check'] = "<input type='checkbox' id='mark_x_". $financialItem->id
."' name='mark_x_". $financialItem->id
."' value='1' onclick={$js}></input>";
386 $row[$financialItem->id
]['action'] = CRM_Core_Action
::formLink(
387 CRM_Financial_Form_BatchTransaction
::links(),
390 'id' => $financialItem->id
,
391 'contid' => $financialItem->contributionID
,
392 'cid' => $financialItem->contact_id
396 'financialItem.batch.row',
402 $js = "enableActions('y')";
403 $row[$financialItem->id
]['check'] = "<input type='checkbox' id='mark_y_". $financialItem->id
."' name='mark_y_". $financialItem->id
."' value='1' onclick={$js}></input>";
404 $row[$financialItem->id
]['action'] = CRM_Core_Action
::formLink(
405 CRM_Financial_Page_BatchTransaction
::links(),
408 'id' => $financialItem->id
,
409 'contid' => $financialItem->contributionID
,
410 'cid' => $financialItem->contact_id
414 'financialItem.batch.row',
421 $row[$financialItem->id
]['check'] = NULL;
422 $links = CRM_Financial_Page_BatchTransaction
::links();
423 unset($links['remove']);
424 $row[$financialItem->id
]['action'] = CRM_Core_Action
::formLink(
428 'id' => $financialItem->id
,
429 'contid' => $financialItem->contributionID
,
430 'cid' => $financialItem->contact_id
434 'financialItem.batch.row',
439 $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
);
440 $financialitems = $row;
443 $iFilteredTotal = $iTotal = $params['total'];
446 'check', 'contact_type', 'sort_name',
447 'amount', 'trxn_id', 'transaction_date', 'payment_method', 'status', 'name', 'action',
450 echo CRM_Utils_JSON
::encodeDataTableSelector($financialitems, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
451 CRM_Utils_System
::civiExit();
454 static function bulkAssignRemove() {
455 $checkIDs = $_REQUEST['ID'];
456 $entityID = CRM_Utils_Type
::escape($_REQUEST['entityID'], 'String');
457 $action = CRM_Utils_Type
::escape($_REQUEST['action'], 'String');
458 foreach ($checkIDs as $key => $value) {
459 if ((substr($value,0,7) == "mark_x_" && $action == 'Assign') ||
(substr($value,0,7) == "mark_y_" && $action == 'Remove')) {
460 $contributions = explode("_",$value);
461 $cIDs[] = $contributions[2];
465 $batchPID = CRM_Core_DAO
::getFieldValue('CRM_Batch_DAO_Batch', $entityID, 'payment_instrument_id');
466 $paymentInstrument = CRM_Core_OptionGroup
::getLabel('payment_instrument',$batchPID);
467 foreach ($cIDs as $key => $value) {
468 $recordPID = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $value, 'payment_instrument_id');
469 if ($action == 'Remove' ||
($recordPID == $batchPID && $action == 'Assign') ||
!isset($batchPID)) {
472 'entity_id' => $value,
473 'entity_table' => 'civicrm_financial_trxn',
474 'batch_id' => $entityID,
476 if ($action == 'Assign') {
477 $updated = CRM_Batch_BAO_Batch
::addBatchEntity($params);
480 $updated = CRM_Batch_BAO_Batch
::removeBatchEntity($params);
485 $status = array('status' => 'record-updated-success');
488 $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)));
490 CRM_Utils_JSON
::output($status);
493 static function getBatchSummary() {
494 $batchID = CRM_Utils_Type
::escape($_REQUEST['batchID'], 'String');
495 $params = array('id' => $batchID);
496 $batchInfo = CRM_Batch_BAO_Batch
::retrieve($params, $value);
497 $batchTotals = CRM_Batch_BAO_Batch
::batchTotals(array($batchID));
500 'created_by' => CRM_Contact_BAO_Contact
::displayName($batchInfo->created_id
),
501 'status' => CRM_Core_OptionGroup
::getLabel('batch_status', $batchInfo->status_id
),
502 'description' => $batchInfo->description
,
503 'payment_instrument' => CRM_Core_OptionGroup
::getLabel('payment_instrument', $batchInfo->payment_instrument_id
),
504 'item_count' => $batchInfo->item_count
,
505 'assigned_item_count' => $batchTotals[$batchID]['item_count'],
506 'total' => CRM_Utils_Money
::format($batchInfo->total
),
507 'assigned_total' => CRM_Utils_Money
::format($batchTotals[$batchID]['total']),
508 'opened_date' => CRM_Utils_Date
::customFormat($batchInfo->created_date
),
511 CRM_Utils_JSON
::output($batchSummary);