function run()
 {
     // Example: Set the page-title dynamically; alternatively, declare a static title in xml/Menu/*.xml
     CRM_Utils_System::setTitle(ts('Bank Transactions'));
     // look up the payment states
     $payment_states = banking_helper_optiongroup_id_name_mapping('civicrm_banking.bank_tx_status');
     if (!isset($_REQUEST['status_ids'])) {
         $_REQUEST['status_ids'] = $payment_states['new']['id'];
     }
     if (isset($_REQUEST['show']) && $_REQUEST['show'] == "payments") {
         // PAYMENT MODE REQUESTED
         $this->build_paymentPage($payment_states);
         $list_type = 'list';
     } else {
         // STATEMENT MODE REQUESTED
         $this->build_statementPage($payment_states);
         $list_type = 's_list';
     }
     // URLs
     global $base_url;
     $this->assign('base_url', $base_url);
     $this->assign('url_show_payments', banking_helper_buildURL('civicrm/banking/payments', array('show' => 'payments', $list_type => "__selected__"), array('status_ids')));
     $this->assign('url_show_statements', banking_helper_buildURL('civicrm/banking/payments', array('show' => 'statements'), array('status_ids')));
     $this->assign('url_show_payments_new', banking_helper_buildURL('civicrm/banking/payments', $this->_pageParameters(array('status_ids' => $payment_states['new']['id']))));
     $this->assign('url_show_payments_analysed', banking_helper_buildURL('civicrm/banking/payments', $this->_pageParameters(array('status_ids' => $payment_states['suggestions']['id']))));
     $this->assign('url_show_payments_completed', banking_helper_buildURL('civicrm/banking/payments', $this->_pageParameters(array('status_ids' => $payment_states['processed']['id'] . "," . $payment_states['ignored']['id']))));
     $this->assign('url_review_selected_payments', banking_helper_buildURL('civicrm/banking/review', array($list_type => "__selected__")));
     $this->assign('url_export_selected_payments', banking_helper_buildURL('civicrm/banking/export', array($list_type => "__selected__")));
     $this->assign('can_delete', CRM_Core_Permission::check('administer CiviCRM'));
     // status filter button styles
     if (isset($_REQUEST['status_ids']) && strlen($_REQUEST['status_ids']) > 0) {
         if ($_REQUEST['status_ids'] == $payment_states['new']['id']) {
             $this->assign('button_style_new', "color:green");
         } else {
             if ($_REQUEST['status_ids'] == $payment_states['suggestions']['id']) {
                 $this->assign('button_style_analysed', "color:green");
             } else {
                 if ($_REQUEST['status_ids'] == $payment_states['processed']['id'] . "," . $payment_states['ignored']['id']) {
                     $this->assign('button_style_completed', "color:green");
                 } else {
                     $this->assign('button_style_custom', "color:green");
                 }
             }
         }
     }
     parent::run();
 }
Example #2
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();
 }