/** * Generate a set of suggestions for the given bank transaction * * @return array(match structures) */ public function match(CRM_Banking_BAO_BankTransaction $btx, CRM_Banking_Matcher_Context $context) { $config = $this->_plugin_config; $threshold = $this->getThreshold(); $data_parsed = $btx->getDataParsed(); // find potential contacts $contacts_found = $context->findContacts($threshold, $data_parsed['name'], $config->lookup_contact_by_name); // with the identified contacts, look up matching memberships $memberships = $this->findMemberships($contacts_found, $btx, $context); // transform all memberships into suggestions foreach ($memberships as $membership) { $suggestion = new CRM_Banking_Matcher_Suggestion($this, $btx); if (isset($contact->general_options->suggestion_title)) { $suggestion->setTitle($contact->general_options->suggestion_title); } else { $suggestion->setTitle(ts("Record as Membership Fee")); } $suggestion->setId("existing-{$contribution_id}"); $suggestion->setParameter('membership_id', $membership['id']); $suggestion->setParameter('last_fee_id', $membership['last_fee_id']); $suggestion->setProbability($membership['probability']); $btx->addSuggestion($suggestion); } // that's it... return empty($this->_suggestions) ? null : $this->_suggestions; }
/** * Generate a set of suggestions for the given bank transaction * * @return array(match structures) */ public function match(CRM_Banking_BAO_BankTransaction $btx, CRM_Banking_Matcher_Context $context) { $config = $this->_plugin_config; $threshold = $this->getThreshold(); $penalty = $this->getPenalty($btx); $data_parsed = $btx->getDataParsed(); // first see if all the required values are there if (!$this->requiredValuesPresent($btx)) { return null; } // then look up potential contacts $contacts_found = $context->findContacts($threshold, $data_parsed['name'], $config->lookup_contact_by_name); // finally generate suggestions foreach ($contacts_found as $contact_id => $contact_probability) { $suggestion = new CRM_Banking_Matcher_Suggestion($this, $btx); $suggestion->setTitle(ts("Create a new contribution")); $suggestion->setId("create-{$contact_id}"); $suggestion->setParameter('contact_id', $contact_id); // set probability manually, I think the automatic calculation provided by ->addEvidence might not be what we need here $contact_probability -= $penalty; if ($contact_probability >= $threshold) { $suggestion->setProbability($contact_probability); $btx->addSuggestion($suggestion); } } // that's it... return empty($this->_suggestions) ? null : $this->_suggestions; }
/** * If the user has modified the input fields provided by the "visualize" html code, * the new values will be passed here BEFORE execution * * CAUTION: there might be more parameters than provided. Only process the ones that * 'belong' to your suggestion. */ public function update_parameters(CRM_Banking_Matcher_Suggestion $match, $parameters) { if ($match->getId() === "manual") { if (isset($parameters["manual_match_contributions"])) { $contributions = explode(",", $parameters["manual_match_contributions"]); $match->setParameter('contribution_ids', $contributions); } } }
/** * create suggestions for matching recurring contributions */ function createRecurringContributionSuggestions($query, $probability, $btx, $context) { $config = $this->_plugin_config; $threshold = $this->getThreshold(); $data_parsed = $btx->getDataParsed(); $suggestions = array(); // don't waste your time contacts below the threshold... if ($probability < $threshold) { return $suggestions; } $rcur_result = civicrm_api3('ContributionRecur', 'get', $query); foreach ($rcur_result['values'] as $rcur_id => $rcur) { // find the next expected date for the recurring contribution $expected_date = self::getExpectedDate($rcur, $btx, $config->recurring_mode); if ($expected_date == NULL) { continue; } // create a suggestion $suggestion = new CRM_Banking_Matcher_Suggestion($this, $btx); $suggestion->setId("recurring-{$rcur_id}"); $suggestion->setParameter('recurring_contribution_id', $rcur_id); $suggestion->setParameter('contact_id', $rcur['contact_id']); $suggestion->setParameter('expected_date', date('Y-m-d', $expected_date)); $suggestion->setParameter('expected_amount', $rcur['amount']); if (!empty($config->suggestion_title)) { $suggestion->setTitle($config->suggestion_title); } if ($probability < 1.0) { $suggestion->addEvidence(1.0 - $probability, ts("The contact could not be uniquely identified.")); } // CHECK AMOUNT if ($config->amount_check) { // calculate the amount penalties (equivalent to CRM_Banking_PluginImpl_Matcher_ExistingContribution) $transaction_amount = $btx->amount; $expected_amount = $rcur['amount']; $amount_delta = $transaction_amount - $expected_amount; if ($transaction_amount < $expected_amount * $config->amount_relative_minimum && $amount_delta < $config->amount_absolute_minimum) { continue; } if ($transaction_amount > $expected_amount * $config->amount_relative_maximum && $amount_delta > $config->amount_absolute_maximum) { continue; } $amount_range_rel = $transaction_amount * ($config->amount_relative_maximum - $config->amount_relative_minimum); $amount_range_abs = $config->amount_absolute_maximum - $config->amount_absolute_minimum; $amount_range = max($amount_range_rel, $amount_range_abs); if ($amount_range) { $penalty = $config->amount_penalty * (abs($amount_delta) / $amount_range); if ($penalty) { $suggestion->addEvidence($penalty, ts("The amount of the transaction differs from the expected amount.")); $probability -= $penalty; } } } // CHECK CURRENCY if ($context->btx->currency != $rcur['currency']) { $suggestion->addEvidence($config->currency_penalty, ts("The currency of the transaction is not as expected.")); $probability -= $config->currency_penalty; } // CHECK EXPECTED DATE if ($config->received_date_check) { // use date only $transaction_date = strtotime(date('Y-m-d', strtotime($context->btx->value_date))); // only apply penalties, if the offset is outside the accepted range $date_offset = $transaction_date - $expected_date; if ($date_offset < strtotime($config->acceptable_date_offset_from, 0) || $date_offset > strtotime($config->acceptable_date_offset_to, 0)) { // check if the payment is completely out of bounds if ($date_offset < strtotime($config->date_offset_minimum, 0)) { continue; } if ($date_offset > strtotime($config->date_offset_maximum, 0)) { continue; } // calculate the date penalties $date_range = strtotime($config->date_offset_maximum) - strtotime($config->date_offset_minimum) - (strtotime($config->acceptable_date_offset_to) - strtotime($config->acceptable_date_offset_from)); if ($date_offset < 0) { $date_delta = abs($date_offset - strtotime($config->acceptable_date_offset_from, 0)); } else { $date_delta = abs($date_offset - strtotime($config->acceptable_date_offset_to, 0)); } if ($date_range) { $penalty = $config->date_penalty * ($date_delta / $date_range); if ($penalty) { $suggestion->addEvidence($penalty, ts("The date of the transaction deviates too much from the expected date.")); $probability -= $penalty; } } } } // CHECK FOR OTHER PAYMENTS if ($config->existing_check) { $other_contributions_id_list = array(); $expected_date_string = date('Y-m-d', $expected_date); if (empty($config->existing_status_list)) { $config->existing_status_list = array(1, 2); } $existing_status_list = implode(',', $config->existing_status_list); // determine date range // TODO: use date_offset_minimum/maximum if (preg_match("/[0-9]+%/", $config->existing_precision)) { $cycle_length_seconds = strtotime("+{$rcur['frequency_interval']} {$rcur['frequency_unit']}", 0); $date_range = (int) ((100.0 - (double) $config->existing_precision) / 100.0 * ((double) $cycle_length_seconds / (double) (60 * 60 * 24))); } else { $date_range = (int) $config->existing_precision; } $sql = "\n SELECT id AS contribution_id\n FROM civicrm_contribution \n WHERE contribution_recur_id = {$rcur_id}\n AND contribution_status_id IN ({$existing_status_list})\n AND (receive_date BETWEEN ('{$expected_date_string}' - INTERVAL {$date_range} DAY)\n AND ('{$expected_date_string}' + INTERVAL {$date_range} DAY) );"; $sql_query = CRM_Core_DAO::executeQuery($sql); while ($sql_query->fetch()) { $other_contributions_id_list[] = $sql_query->contribution_id; } if (!empty($other_contributions_id_list)) { $links = array(); if (count($other_contributions_id_list) == 1) { $message = ts("There is already another contribution recorded for this interval: "); } else { $message = ts("There are already multiple contributions recorded for this interval: "); } foreach ($other_contributions_id_list as $other_contributions_id) { $links[] = "<a href='" . CRM_Utils_System::url('civicrm/contact/view/contribution', "reset=1&id={$other_contributions_id}&cid={$rcur['contact_id']}&action=view") . "'>[{$other_contributions_id}]</a>"; } $message .= implode(', ', $links); $suggestion->addEvidence($config->existing_penalty, $message); $probability -= $config->existing_penalty; } } $suggestion->setProbability($probability); $suggestions[] = $suggestion; } return $suggestions; }
/** * If the user has modified the input fields provided by the "visualize" html code, * the new values will be passed here BEFORE execution * * CAUTION: there might be more parameters than provided. Only process the ones that * 'belong' to your suggestion. */ public function update_parameters(CRM_Banking_Matcher_Suggestion $match, $parameters) { $config = $this->_plugin_config; if ($config->mode == 'cancellation') { // store potentially modified extended cancellation values if ($config->cancellation_cancel_reason) { $match->setParameter('cancel_reason', $parameters['cancel_reason']); } if ($config->cancellation_cancel_fee) { $match->setParameter('cancel_fee', (double) $parameters['cancel_fee']); } } }
/** * If the user has modified the input fields provided by the "visualize" html code, * the new values will be passed here BEFORE execution * * CAUTION: there might be more parameters than provided. Only process the ones that * 'belong' to your suggestion. */ public function update_parameters(CRM_Banking_Matcher_Suggestion $match, $parameters) { // record the override parameter $batch_id = $match->getParameter('batch_id'); if ($parameters["batches_override_{$batch_id}"]) { $match->setParameter('override_status', 1); } else { $match->setParameter('override_status', 0); } }