Exemplo n.º 1
0
 /**
  * will evaluate the 'list' (comma separated list of tx IDs) and 
  * 's_list' (comma separated list of tx_batch IDs), if given.
  *
  * @return an array('tx_batch_id' => array('tx_id'))
  */
 public static function getIdLists($params)
 {
     // first: extract all the IDs
     if (!empty($params['list'])) {
         $ids = explode(",", $params['list']);
     } else {
         $ids = array();
     }
     if (!empty($params['s_list'])) {
         $list = CRM_Banking_Page_Payments::getPaymentsForStatements($params['s_list']);
         $ids = array_merge(explode(",", $list), $ids);
     }
     // now create a (sane) SQL query
     $sane_ids = array();
     foreach ($ids as $tx_id) {
         if (is_numeric($tx_id)) {
             $sane_ids[] = (int) $tx_id;
         }
     }
     if (count($sane_ids) == 0) {
         return array();
     }
     $sane_ids_list = implode(',', $sane_ids);
     // query the DB
     $query_sql = "SELECT id, tx_batch_id FROM civicrm_bank_tx WHERE id IN ({$sane_ids_list});";
     $result = array();
     $query = CRM_Core_DAO::executeQuery($query_sql);
     while ($query->fetch()) {
         $result[$query->tx_batch_id][] = $query->id;
     }
     return $result;
 }
/**
 * extracts the individual transaction IDs from the parameter set
 * @param  list    comma separated list of bank_tx ids to process
 * @param  s_list  comma separated list of bank_tx_batch ids to process
 * 
 * @return array of IDs
 */
function _civicrm_api3_banking_transaction_getTxIDs($params)
{
    // extract payment IDs from parameters
    $list_string = "";
    if (!empty($params['list'])) {
        $list_string .= $params['list'];
    }
    if (!empty($params['s_list'])) {
        if (!empty($list_string)) {
            $list_string .= ',';
        }
        $list_string .= CRM_Banking_Page_Payments::getPaymentsForStatements($params['s_list']);
    }
    // clean up the list
    $list = explode(',', $list_string);
    $tx_ids = array();
    foreach ($list as $tx_id) {
        $tx_id = (int) $tx_id;
        if (!empty($tx_id)) {
            $tx_ids[] = $tx_id;
        }
    }
    return $tx_ids;
}
Exemplo n.º 3
0
 function run()
 {
     // set this variable to request a redirect
     $url_redirect = NULL;
     // Get the current ID
     if (isset($_REQUEST['list'])) {
         $list = explode(",", $_REQUEST['list']);
     } else {
         if (isset($_REQUEST['s_list'])) {
             $list = CRM_Banking_Page_Payments::getPaymentsForStatements($_REQUEST['s_list']);
             $list = explode(",", $list);
         } else {
             $list = array();
             array_push($list, $_REQUEST['id']);
         }
     }
     if (isset($_REQUEST['id'])) {
         $pid = $_REQUEST['id'];
     } else {
         $pid = $list[0];
     }
     // find position in the list
     $index = array_search($pid, $list);
     if ($index >= 0) {
         if (isset($list[$index + 1])) {
             $next_pid = $list[$index + 1];
         }
         if (isset($list[$index - 1])) {
             $prev_pid = $list[$index - 1];
         }
     }
     $btx_bao = new CRM_Banking_BAO_BankTransaction();
     $btx_bao->get('id', $pid);
     // read the list of BTX statuses
     $choices = banking_helper_optiongroup_id_name_mapping('civicrm_banking.bank_tx_status');
     // If the exercution was triggered, run that first
     if (isset($_REQUEST['execute'])) {
         $execute_bao = $_REQUEST['execute'] == $pid ? $btx_bao : NULL;
         $this->execute_suggestion($_REQUEST['execute_suggestion'], $_REQUEST, $execute_bao, $choices);
         // after execution -> exit if this was the last in the list
         if (!isset($next_pid) && $_REQUEST['execute'] == $pid) {
             $url_redirect = banking_helper_buildURL('civicrm/banking/payments', $this->_pageParameters());
         }
     }
     // look up some stuff regarding this btx
     $my_bao = new CRM_Banking_BAO_BankAccount();
     $my_bao->get('id', $btx_bao->ba_id);
     if ($btx_bao->party_ba_id) {
         $ba_bao = new CRM_Banking_BAO_BankAccount();
         $ba_bao->get('id', $btx_bao->party_ba_id);
         $this->assign('party_ba', $ba_bao);
         $this->assign('party_ba_data_parsed', json_decode($ba_bao->data_parsed, true));
         $this->assign('party_ba_references', $ba_bao->getReferences());
         // deprecated: contact can also be indetified via other means, see below
         if ($ba_bao->contact_id) {
             $contact = civicrm_api('Contact', 'getsingle', array('version' => 3, 'id' => $ba_bao->contact_id));
         }
     }
     // parse structured data
     $this->assign('btxstatus', $choices[$btx_bao->status_id]);
     $this->assign('payment', $btx_bao);
     $this->assign('my_bao', $my_bao);
     $this->assign('payment_data_raw', json_decode($btx_bao->data_raw, true));
     $data_parsed = json_decode($btx_bao->data_parsed, true);
     $this->assign('payment_data_parsed', $data_parsed);
     if (!empty($data_parsed['iban'])) {
         $data_parsed['iban'] = CRM_Banking_BAO_BankAccountReference::format('iban', $data_parsed['iban']);
     }
     if (empty($contact) && !empty($data_parsed['contact_id'])) {
         // convention: the contact was identified with acceptable precision
         $contact = civicrm_api('Contact', 'getsingle', array('version' => 3, 'id' => $data_parsed['contact_id']));
     }
     if (!empty($contact)) {
         $this->assign('contact', $contact);
     } else {
         $this->assign('contact', NULL);
     }
     $extra_data = array();
     $_data_raw = json_decode($btx_bao->data_raw, true);
     if (is_array($_data_raw)) {
         $extra_data = $_data_raw;
     } else {
         $extra_data['raw'] = $btx_bao->data_raw;
     }
     if (is_array($btx_bao->getDataParsed())) {
         $extra_data = array_merge($extra_data, $btx_bao->getDataParsed());
     }
     $this->assign('extra_data', $extra_data);
     // check if closed ('processed' or 'ignored')
     if ($choices[$btx_bao->status_id]['name'] == 'processed' || $choices[$btx_bao->status_id]['name'] == 'ignored') {
         // this is a closed BTX, generate execution information
         $execution_info = array();
         $execution_info['status'] = $choices[$btx_bao->status_id]['name'];
         $suggestion_objects = $btx_bao->getSuggestionList();
         foreach ($suggestion_objects as $suggestion) {
             if ($suggestion->isExecuted()) {
                 $execution_info['date'] = $suggestion->isExecuted();
                 $execution_info['visualization'] = $suggestion->visualize_execution($btx_bao);
                 $execution_info['executed_by'] = $suggestion->getParameter('executed_by');
                 $execution_info['executed_automatically'] = $suggestion->getParameter('executed_automatically');
                 break;
             }
         }
         $this->assign('execution_info', $execution_info);
         // generate message
         if (!empty($execution_info['date'])) {
             $execution_date = CRM_Utils_Date::customFormat($execution_info['date'], CRM_Core_Config::singleton()->dateformatFull);
         } else {
             $execution_date = ts("<i>unknown date</i>");
         }
         if (!empty($execution_info['executed_by'])) {
             // visualize more info, see https://github.com/Project60/CiviBanking/issues/71
             // try to load contact
             $user_id = $execution_info['executed_by'];
             $user = civicrm_api('Contact', 'getsingle', array('id' => $user_id, 'version' => 3));
             if (empty($user['is_error'])) {
                 $user_link = CRM_Utils_System::url("civicrm/contact/view", "&reset=1&cid={$user_id}");
                 $user_string = "<a href='{$user_link}'>" . $user['display_name'] . "</a>";
             } else {
                 $user_string = ts('Unknown User') . ' [' . $user_id . ']';
             }
             if (empty($execution_info['executed_automatically'])) {
                 $automated = '';
             } else {
                 $automated = ts('automatically');
             }
             if ($choices[$btx_bao->status_id]['name'] == 'processed') {
                 $message = sprintf(ts("This transaction was <b>%s processed</b> on %s by %s."), $automated, $execution_date, $user_string);
             } else {
                 $message = sprintf(ts("This transaction was <b>%s ignored</b> on %s by %s."), $automated, $execution_date, $user_string);
             }
         } else {
             // visualize the previous, reduced information
             if ($choices[$btx_bao->status_id]['name'] == 'processed') {
                 $message = sprintf(ts("This transaction was <b>processed</b> on %s."), $execution_date);
             } else {
                 $message = sprintf(ts("This transaction was marked to be <b>ignored</b> on %s."), $execution_date);
             }
         }
         $this->assign('status_message', $message);
     } else {
         // this is an open (new or analysed) BTX:  create suggestion list
         $suggestions = array();
         $suggestion_objects = $btx_bao->getSuggestionList();
         foreach ($suggestion_objects as $suggestion) {
             $color = $this->translateProbability($suggestion->getProbability() * 100);
             array_push($suggestions, array('hash' => $suggestion->getHash(), 'probability' => sprintf('%d&nbsp;%%', $suggestion->getProbability() * 100), 'color' => $color, 'visualization' => $suggestion->visualize($btx_bao), 'title' => $suggestion->getTitle(), 'actions' => $suggestion->getActions()));
         }
         $this->assign('suggestions', $suggestions);
     }
     // URLs & stats
     $unprocessed_count = 0;
     $this->assign('url_back', banking_helper_buildURL('civicrm/banking/payments', $this->_pageParameters()));
     if (isset($next_pid)) {
         $this->assign('url_skip_forward', banking_helper_buildURL('civicrm/banking/review', $this->_pageParameters(array('id' => $next_pid))));
         $this->assign('url_execute', banking_helper_buildURL('civicrm/banking/review', $this->_pageParameters(array('id' => $next_pid, 'execute' => $pid))));
         $unprocessed_info = $this->getUnprocessedInfo($list, $next_pid, $choices);
         if ($unprocessed_info) {
             $this->assign('url_skip_processed', banking_helper_buildURL('civicrm/banking/review', $this->_pageParameters(array('id' => $unprocessed_info['next_unprocessed_pid']))));
             $unprocessed_count = $unprocessed_info['unprocessed_count'];
         }
     } else {
         $this->assign('url_execute', banking_helper_buildURL('civicrm/banking/review', $this->_pageParameters(array('execute' => $pid))));
     }
     if (isset($prev_pid)) {
         $this->assign('url_skip_back', banking_helper_buildURL('civicrm/banking/review', $this->_pageParameters(array('id' => $prev_pid))));
     }
     $this->assign('url_show_payments', banking_helper_buildURL('civicrm/banking/payments', array('show' => 'payments')));
     global $base_url;
     $this->assign('base_url', $base_url);
     // Set the page-title dynamically
     if (count($list) > 1) {
         CRM_Utils_System::setTitle(ts("Review Bank Transaction %1 of %2 (%3 unprocessed ahead)", array(1 => $index + 1, 2 => count($list), 3 => $unprocessed_count)));
     } else {
         CRM_Utils_System::setTitle(ts("Review Bank Transaction"));
     }
     // perform redirect, if requested
     if ($url_redirect) {
         CRM_Utils_System::redirect($url_redirect);
     }
     parent::run();
 }