public function match(CRM_Banking_BAO_BankTransaction $btx, CRM_Banking_Matcher_Context $context) { // this section will be refactored to use different conditions, but for now, this is hardcoded $suggestion = new CRM_Banking_Matcher_Suggestion($this, $btx); $config = $this->_plugin_config; // amount range if (isset($config->amount)) { $camount = $config->amount; $low = $camount->low; $high = $camount->high; $factor = $camount->prob or 1; $amount = $btx->amount; if ($low == null || $amount >= $low) { if ($high == null || $amount <= $high) { $message = ts('the transaction amount is in the range [ '); if ($low) { $message .= number_format($low, 2); } $message .= ' - '; if ($high) { $message .= number_format($high, 2); } $message .= ' ]'; $suggestion->addEvidence($factor, $message); } } } // date range if (isset($config->value_date)) { $cvdate = $config->value_date; $early = $cvdate->early; $late = $cvdate->late; $factor = $cvdate->prob or 1; $value_date = strtotime($btx->value_date); if ($early != '' && $value_date >= strtotime($early)) { if ($late != '' && $value_date <= strtotime($late)) { $message = ts('the transaction value date is in the range [ '); if ($early) { $message .= $early; } $message .= ' - '; if ($late) { $message .= $late; } $message .= ' ]'; $suggestion->addEvidence($factor, $message); } } } // regex if (isset($config->purpose)) { $cpurp = $config->purpose; $regex = $cpurp->regex; $factor = $cpurp->prob or 1; $parsed = json_decode($btx->data_parsed, true); $purpose = $parsed['purpose']; if ($regex != '' && preg_match("/{$regex}/", $purpose)) { $message = sprintf(ts('the transaction purpose matches the expression "%s"'), htmlentities($regex)); $suggestion->addEvidence($factor, $message); } } if ($suggestion->getProbability() > 0) { $btx->addSuggestion($suggestion); } // close up return empty($this->_suggestions) ? null : $this->_suggestions; }
public function match(CRM_Banking_BAO_BankTransaction $btx, CRM_Banking_Matcher_Context $context) { // get list of existing batches (cache in context) $existing_batches = $context->getCachedEntry('banking.pluginimpl.matcher.batch'); if ($existing_batches == NULL) { $existing_batches = $this->generateBatchList(); $context->setCachedEntry('banking.pluginimpl.matcher.batch', $existing_batches); } // look for a matching batch $config = $this->_plugin_config; $booking_date = strtotime($btx->booking_date); $matching_batches = array(); foreach ($existing_batches as $batch) { $total_amount = $batch['total']; if (!empty($batch['export_date'])) { $submission_date = strtotime($batch['export_date']); } elseif ($batch['modified_date']) { $submission_date = strtotime($batch['modified_date']); } else { $submission_date = strtotime($batch['created_date']); } // check amount if (abs(1 - $total_amount / $btx->amount) > $config->total_amount_tolerance) { continue; } // check export_date_to_payment_min / max if ($booking_date < strtotime($config->export_date_to_payment_min, $submission_date)) { continue; } if ($booking_date > strtotime($config->export_date_to_payment_max, $submission_date)) { continue; } // batch is accepted -> calculate probability: // first factor: expected income time $time_penalty_total = strtotime('-' . $config->export_date_to_payment_tolerance, abs($booking_date - $submission_date)); $time_penalty = min(1.0, 1 - $time_penalty_total / (strtotime($config->export_date_to_payment_max) - strtotime($config->export_date_to_payment_min))); // second factor: equal amount $amount_penalty = 1.0 - abs(1 - $total_amount / $btx->amount) / $config->total_amount_tolerance; // third factor: statmentes pending $status_penalty = 1.0 - count($this->getNonPendingContributionIDs($batch['id'])) / $batch['item_count']; $matching_batches[$batch['id']] = $time_penalty * $amount_penalty * $status_penalty; } // for each matched batch, create a suggestion foreach ($matching_batches as $batch_id => $batch_probability) { $suggestion = new CRM_Banking_Matcher_Suggestion($this, $btx); $suggestion->setTitle(ts("Settles a contribution batch")); $suggestion->setParameter('batch_id', $batch_id); $suggestion->setId("batch-" . $batch_id); $suggestion->setProbability($batch_probability); $btx->addSuggestion($suggestion); } // that's it... return empty($this->_suggestions) ? null : $this->_suggestions; }