Beispiel #1
0
 /**
  * @see Billrun_Calculator_Rate::getLineRate
  */
 protected function getLineRate($row, $usage_type)
 {
     if ($this->shouldLineBeRated($row)) {
         $matchedRate = $this->rates['UNRATED'];
         $called_number = $this->extractNumber($row);
         $line_time = $row['urt'];
         $called_number_prefixes = Billrun_Util::getPrefixes($called_number);
         foreach ($called_number_prefixes as $prefix) {
             if (isset($this->rates[$prefix])) {
                 foreach ($this->rates[$prefix] as $rate) {
                     if (isset($rate['rates'][$usage_type]) && (!isset($rate['params']['fullEqual']) || $prefix == $called_number)) {
                         if ($rate['from'] <= $line_time && $rate['to'] >= $line_time) {
                             $matchedRate = $rate;
                             break 2;
                         }
                     }
                 }
             }
         }
         return $matchedRate;
     } else {
         return false;
     }
 }
Beispiel #2
0
 /**
  * @see Billrun_Calculator_Rate::getLineRate
  */
 protected function getLineRate($row, $usage_type)
 {
     $line_time = $row['urt'];
     $serving_network = $row['serving_network'];
     $sender = isset($row['sending_source']) ? $row['sending_source'] : false;
     $matchedRate = false;
     $prefix_length_matched = 0;
     if (!is_null($serving_network)) {
         $call_number = $this->number_to_rate($row);
         if ($call_number) {
             $call_number = preg_replace("/^[^1-9]*/", "", $call_number);
             $call_number_prefixes = Billrun_Util::getPrefixes($call_number);
         }
         $potential_rates = array();
         if (isset($this->rates['by_names'][$serving_network])) {
             foreach ($this->rates['by_names'][$serving_network] as $named_rate) {
                 if (is_array($named_rate['params']['sending_sources'])) {
                     if (!$sender || isset($named_rate['params']['sending_sources']) && in_array($sender, $named_rate['params']['sending_sources'])) {
                         $potential_rates[] = $named_rate;
                     }
                 } else {
                     if (is_string($named_rate['params']['sending_sources'])) {
                         if (!$sender || isset($named_rate['params']['sending_sources']) && preg_match($named_rate['params']['sending_sources'], $sender)) {
                             $potential_rates[] = $named_rate;
                         }
                     }
                 }
             }
         }
         if (!empty($this->rates['by_regex'])) {
             foreach ($this->rates['by_regex'] as $regex => $regex_rates) {
                 if (preg_match($regex, $serving_network)) {
                     foreach ($regex_rates as $regex_rate) {
                         if (is_array($regex_rate['params']['sending_sources'])) {
                             if (!$sender || isset($regex_rate['params']['sending_sources']) && in_array($sender, $regex_rate['params']['sending_sources'])) {
                                 $potential_rates[] = $regex_rate;
                             }
                         } else {
                             if (is_string($regex_rate['params']['sending_sources'])) {
                                 if (!$sender || isset($regex_rate['params']['sending_sources']) && preg_match($regex_rate['params']['sending_sources'], $sender)) {
                                     $potential_rates[] = $regex_rate;
                                 }
                             }
                         }
                     }
                 }
             }
         }
         foreach ($potential_rates as $rate) {
             if (isset($rate['rates'][$usage_type])) {
                 if ($rate['from'] <= $line_time && $rate['to'] >= $line_time) {
                     if (!$matchedRate && empty($rate['params']['prefix']) || is_array($rate['params']['serving_networks']) && !$prefix_length_matched) {
                         // array of serving networks is stronger then regex of serving_networks
                         $matchedRate = $rate;
                     }
                     if (isset($call_number_prefixes) && !empty($rate['params']['prefix'])) {
                         if (!isset($rate['params']['fullEqual'])) {
                             foreach ($call_number_prefixes as $prefix) {
                                 if (in_array($prefix, $rate['params']['prefix']) && strlen($prefix) > $prefix_length_matched) {
                                     $prefix_length_matched = strlen($prefix);
                                     $matchedRate = $rate;
                                 }
                             }
                         } else {
                             if (in_array($call_number, $rate['params']['prefix']) && strlen($call_number) > $prefix_length_matched) {
                                 $prefix_length_matched = strlen($call_number);
                                 $matchedRate = $rate;
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($matchedRate === FALSE && !in_array($usage_type, $this->optional_usage_types)) {
         $matchedRate = $this->rates['UNRATED'];
     }
     return $matchedRate;
 }
Beispiel #3
0
 /**
  * Get a matching rate by the supplied params
  * @param string $called_number the number called
  * @param string $usage_type the usage type (call / sms ...)
  * @param MongoDate $urt the time of the event
  * @param string $ocg the out circuit group of the event. If not supplied, ocg will be ignored in the search.
  * @return Mongodloid_Entity the matched rate or UNRATED rate if none found
  */
 protected function getRateByParams($called_number, $usage_type, $urt, $ocg = null)
 {
     $matchedRate = $this->rates['UNRATED'];
     $called_number_prefixes = Billrun_Util::getPrefixes($called_number);
     foreach ($called_number_prefixes as $prefix) {
         if (isset($this->rates[$prefix])) {
             foreach ($this->rates[$prefix] as $rate) {
                 if (isset($rate['rates'][$usage_type]) && (!isset($rate['params']['fullEqual']) || $prefix == $called_number)) {
                     if ($rate['from'] <= $urt && $rate['to'] >= $urt) {
                         if (is_null($ocg)) {
                             $matchedRate = $rate;
                             break 2;
                         } else {
                             foreach ($rate['params']['out_circuit_group'] as $groups) {
                                 if ($groups['from'] <= $ocg && $groups['to'] >= $ocg) {
                                     $matchedRate = $rate;
                                     break 3;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $matchedRate;
 }
Beispiel #4
0
 public function getRateByVLR($vlr)
 {
     $prefixes = Billrun_Util::getPrefixes($vlr);
     $match = array('$match' => array('params.serving_networks' => array('$exists' => true), 'kt_prefixes' => array('$in' => $prefixes)));
     $unwind = array('$unwind' => '$kt_prefixes');
     $sort = array('$sort' => array('kt_prefixes' => -1));
     $limit = array('$limit' => 1);
     $rate = $this->collection->aggregate(array($match, $unwind, $sort, $limit));
     if ($rate) {
         return $rate[0];
     } else {
         return NULL;
     }
 }