/**
 * BankingAccountReference.check has two functions:
 *  - check if the reference is valid
 *  - normalize the reference
 *
 * @param $reference            reference to be checked
 * @param $reference_type       id (not value) of the reference type OptionValue
 * @param $reference_type_name  name of reference type, e.g. NBAN_DE
 * 
 * @return 'is_valid'   0/1: is the given reference valid? 0 could also mean "not checked", see below
 *         'checked'    0/1: could the value be checked? If 0, there is no implementation for this type
 *         'normalised' 0/1: was the reference normalised?
 *         'reference'  the normalised reference
 *         'original'   the reference as queried
 */
function civicrm_api3_banking_account_reference_check($params)
{
    if (empty($params['reference'])) {
        return civicrm_api3_create_error("No 'reference' parameter given.");
    }
    if (!empty($params['reference_type'])) {
        // look up reference_type_name
        $params['reference_type_name'] = civicrm_api3('OptionValue', 'getvalue', array('id' => (int) $params['reference_type'], 'return' => 'name'));
    }
    if (empty($params['reference_type_name'])) {
        return civicrm_api3_create_error("No 'reference_type_name' parameter given.");
    }
    $reference = $params['reference'];
    $reply = array('original' => $reference);
    $result = CRM_Banking_BAO_BankAccountReference::normalise($params['reference_type_name'], $reference);
    if ($result === FALSE) {
        $reply['is_valid'] = 0;
        $reply['checked'] = 0;
        $reply['normalised'] = 0;
    } elseif ($result === 0) {
        $reply['is_valid'] = 0;
        $reply['checked'] = 1;
        $reply['normalised'] = 0;
    } elseif ($result === 1) {
        $reply['is_valid'] = 1;
        $reply['checked'] = 1;
        $reply['normalised'] = 0;
    } elseif ($result === 2) {
        $reply['is_valid'] = 1;
        $reply['checked'] = 1;
        $reply['normalised'] = 1;
    } else {
        return civicrm_api3_create_error("Internal error");
    }
    $reply['reference'] = $reference;
    return civicrm_api3_create_success($reply);
}
Example #2
0
 public function getOrCreateBankAccount(&$coda_tx)
 {
     $refs = array();
     if (isset($coda_tx->iban) && !empty($coda_tx->iban)) {
         $refs['iban'] = $coda_tx->iban;
     }
     if (isset($coda_tx->bban) && !empty($coda_tx->bban)) {
         $refs['bban'] = $coda_tx->bban;
     }
     if (!array_key_exists('iban', $refs) || !array_key_exists('bban', $refs)) {
         //todo take the main ba
     }
     foreach ($refs as $type => $ref) {
         $bank_account_refs = array();
         $breftypeid = $this->_ba_ref_types[$type]['value'];
         $options = array('reference_type_id' => $breftypeid, 'reference' => $ref, 'version' => 3);
         $result = civicrm_api('banking_account_reference', 'get', $options);
         if ($result['count'] != 0) {
             $bank_account_refs[$type] = $result['values'][$result['id']];
         }
     }
     /*
         if(isset($coda_tx->bic) && !empty($coda_tx->bic)){
        $refs['bic'] = $coda_tx->bic;
         }   
     */
     if (empty($bank_account_refs)) {
         $bank_account = new CRM_Banking_BAO_BankAccount();
         $bank_account->description = $coda_tx->name;
         $data_raw = array('name' => $coda_tx->name, 'info_msg' => $coda_tx->info_message);
         $data_parsed = array('name' => $coda_tx->name, 'street_address' => trim($coda_tx->streetname . ' ' . $coda_tx->streetnumber), 'postal_code' => $coda_tx->postal_code, 'city' => $coda_tx->city, 'country_code' => $coda_tx->country_code, 'bic' => $coda_tx->bic);
         $bank_account->created_date = date('YmdHis');
         $bank_account->modified_date = date('YmdHis');
         $bank_account->data_raw = json_encode($data_raw);
         $bank_account->data_parsed = json_encode($data_parsed);
         $bank_account->save();
         $ma = new CRM_Banking_Helpers_MatchAddress($bank_account);
         if ($ma->findAddress()) {
             $ma->updateDataParsed();
         }
     } else {
         $ba_ref = each($bank_account_refs);
         $result = civicrm_api('banking_account', 'get', array('id' => $ba_ref['id'], 'version' => 3));
         $bank_account = (object) $result['values'][$result['id']];
     }
     foreach ($refs as $type => $ref) {
         if (!array_key_exists($type, $bank_account_refs)) {
             $bank_account_ref = new CRM_Banking_BAO_BankAccountReference();
             $bank_account_ref->reference = $coda_tx->{$type};
             $bank_account_ref->reference_type_id = $this->_ba_ref_types[$type]['value'];
             $bank_account_ref->ba_id = $bank_account->id;
             $bank_account_ref->save();
         }
     }
     return $bank_account->id;
 }
Example #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();
 }