/** * Delete function addendum: update statement's count * * @see https://github.com/Project60/CiviBanking/issues/59 */ static function del($ba_id) { // get batch (statement) id $ba_bao = new CRM_Banking_BAO_BankTransaction(); $ba_bao->get('id', $ba_id); $batch_id = $ba_bao->tx_batch_id; // delete the transaction / payments $ba_bao->delete(); // if $batch exists, update count if (!empty($batch_id)) { $new_count_query = "SELECT COUNT(`id`) FROM `civicrm_bank_tx` WHERE `tx_batch_id`='{$batch_id}'"; CRM_Core_DAO::executeQuery("UPDATE `civicrm_bank_tx_batch` SET `tx_count` = ({$new_count_query}) WHERE `id`='{$batch_id}';"); } }
/** * Gather all information on the transaction / payment * * @return an array containing all values, keys prefixed with 'tx_' */ public function getTxData($tx_id) { $result = array(); $tx = array(); $tx_bao = new CRM_Banking_BAO_BankTransaction(); $tx_bao->get('id', $tx_id); CRM_Core_DAO::storeValues($tx_bao, $tx); // add all basic fields foreach ($tx as $key => $value) { $result['tx_' . $key] = $value; } // resolve status IDs $result['tx_status'] = CRM_Core_OptionGroup::getValue('civicrm_banking.bank_tx_status', $result['tx_status_id'], 'id', 'String', 'name'); $result['tx_status_name'] = CRM_Core_OptionGroup::getValue('civicrm_banking.bank_tx_status', $result['tx_status_id'], 'id', 'String', 'label'); // add all data_parsed $data_parsed = $tx_bao->getDataParsed(); foreach ($data_parsed as $key => $value) { $result['data_' . $key] = $value; } unset($result['tx_data_parsed']); unset($result['tx_suggestions']); // add execution info $suggestion_objects = $tx_bao->getSuggestionList(); foreach ($suggestion_objects as $suggestion) { if ($suggestion->isExecuted()) { $result['exec_date'] = $suggestion->isExecuted(); $result['exec_executed_by'] = $suggestion->getParameter('executed_by'); $result['exec_automatically'] = $suggestion->getParameter('executed_automatically'); // find contribtion IDs $contribution_ids = array(); $suggestion_contribution_id = $suggestion->getParameter('contribution_id'); if (!empty($suggestion_contribution_id)) { if ((int) $suggestion_contribution_id) { $contribution_ids[] = (int) $suggestion_contribution_id; } } $suggestion_contribution_ids = $suggestion->getParameter('contribution_ids'); if (!empty($suggestion_contribution_ids)) { foreach ($suggestion_contribution_ids as $id) { $id = (int) $id; if ($id) { $contribution_ids[] = $id; } } } $result['exec_contribution_count'] = count($contribution_ids); $result['exec_contribution_list'] = implode(',', $contribution_ids); // also, add individual contribution data $counter = 1; $total_sum = 0.0; $total_currency = ''; $total_non_deductible = 0.0; foreach ($contribution_ids as $contribution_id) { $contribution = civicrm_api('Contribution', 'getsingle', array('id' => $contribution_id, 'version' => 3)); if (!empty($contribtion['is_error'])) { error_log("org.project60.banking.exporter.csv: error while reading contribution [{$contribution_id}]: " . $contribution['error_message']); } else { $prefix = 'exec_contribution' . ($counter > 1 ? "_{$counter}_" : '_'); foreach ($contribution as $key => $value) { $result[$prefix . $key] = $value; } if (!empty($contribution['total_amount'])) { $total_sum += $contribution['total_amount']; } if (!empty($contribution['non_deductible_amount'])) { $total_non_deductible += $contribution['non_deductible_amount']; } if (!empty($contribution['currency'])) { if (empty($total_currency)) { $total_currency = $contribution['currency']; } elseif ($total_currency != $contribution['currency']) { $total_currency = 'MIX'; } } } $counter++; } $result['exec_total_amount'] = $total_sum; $result['exec_total_currency'] = $total_currency; $result['exec_total_non_deductible'] = $total_non_deductible; break; } } return $result; }
/** * Run this BTX through the matchers * * @param CRM_Banking_BAO_BankTransaction $btx * @param bool $override_processed Set this to TRUE if you want to re-match processed transactions. * This will destroy all records of the execution! */ public function match($btx_id, $override_processed = FALSE) { // TODO: timeout is 30s - do we need a setting here? $lock_timeout = 30.0; $lock = CRM_Utils_BankingSafeLock::acquireLock('org.project60.banking.tx' . '-' . $btx_id, $lock_timeout); if (empty($lock)) { error_log("org.project60.banking - couldn't acquire lock. Timeout is {$lock_timeout}."); return false; } // load btx $btx = new CRM_Banking_BAO_BankTransaction(); $btx->get('id', $btx_id); if (!$override_processed) { // don't match already executed transactions... $processed_status_id = banking_helper_optionvalueid_by_groupname_and_name('civicrm_banking.bank_tx_status', 'Processed'); $ignored_status_id = banking_helper_optionvalueid_by_groupname_and_name('civicrm_banking.bank_tx_status', 'Ignored'); if ($btx->status_id == $processed_status_id || $btx->status_id == $ignored_status_id) { // will not match already executed transactions $lock->release(); return true; } } // reset the BTX suggestion list $btx->resetSuggestions(); // reset the cache / context object $context = new CRM_Banking_Matcher_Context($btx); // run through the list of matchers if (empty($this->plugins)) { CRM_Core_Session::setStatus(ts("No matcher plugins configured!"), ts('No processors'), 'alert'); } else { foreach ($this->plugins as $weight => $plugins) { foreach ($plugins as $plugin) { try { // run matchers to generate suggestions $continue = $this->matchPlugin($plugin, $context); if (!$continue) { $lock->release(); return true; } // check if we can execute the suggestion right aways $abort = $this->checkAutoExecute($plugin, $btx); if ($abort) { $lock->release(); return false; } } catch (Exception $e) { $matcher_id = $plugin->getPluginID(); error_log("org.project60.banking - Exception during the execution of matcher [{$matcher_id}], error was: " . $e->getMessage()); $lock->release(); return false; } } } } $btx->saveSuggestions(); // set the status $newStatus = banking_helper_optionvalueid_by_groupname_and_name('civicrm_banking.bank_tx_status', 'Suggestions'); $btx->status_id = $newStatus; $btx->setStatus($newStatus); $lock->release(); return false; }
/** * Will trigger the execution of the given suggestion (identified by its hash) */ function execute_suggestion($suggestion_hash, $parameters, $btx_bao, $choices) { // load BTX object if not provided if (!$btx_bao) { $btx_bao = new CRM_Banking_BAO_BankTransaction(); $btx_bao->get('id', $parameters['execute']); } $suggestion = $btx_bao->getSuggestionByHash($suggestion_hash); if ($suggestion) { // update the parameters $suggestion->update_parameters($parameters); $btx_bao->saveSuggestions(); $suggestion->execute($btx_bao); // create a notification bubble for the user $text = $suggestion->visualize_execution($btx_bao); if ($btx_bao->status_id == $choices['processed']['id']) { CRM_Core_Session::setStatus(ts("The transaction was booked.") . "<br/>" . $text, ts("Transaction closed"), 'info'); } elseif ($btx_bao->status_id == $choices['ignored']['id']) { CRM_Core_Session::setStatus(ts("The transaction was ignored.") . "<br/>" . $text, ts("Transaction closed"), 'info'); } else { CRM_Core_Session::setStatus(ts("The transaction could not be closed."), ts("Error"), 'alert'); } } else { CRM_Core_Session::setStatus(ts("Selected suggestions disappeared. Suggestion NOT executed!"), ts("Internal Error"), 'error'); } }