예제 #1
0
 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;
 }
 /**
  * Will get a the set of contributions of a given contact
  * 
  * caution: will only the contributions of the last year
  *
  * @return an array with contributions
  */
 public function getPotentialContributionsForContact($contact_id, CRM_Banking_Matcher_Context $context)
 {
     $config = $this->_plugin_config;
     // check in cache
     $cache_key = "_contributions_{$contact_id}_{$range_back}_{$config->received_date_check}";
     $contributions = $context->getCachedEntry($cache_key);
     if ($contributions != NULL) {
         return $contributions;
     }
     $contributions = array();
     if ($config->received_date_check) {
         $range_back = (int) $config->received_range_days;
         $date_restriction = " AND receive_date > (NOW() - INTERVAL {$range_back} DAY)";
     } else {
         $date_restriction = "";
     }
     $sql = "SELECT * FROM civicrm_contribution WHERE contact_id={$contact_id} AND is_test = 0 {$date_restriction};";
     $contribution = CRM_Contribute_DAO_Contribution::executeQuery($sql);
     while ($contribution->fetch()) {
         array_push($contributions, $contribution->toArray());
     }
     // cache result and return
     $context->setCachedEntry($cache_key, $contributions);
     return $contributions;
 }