public function __construct() { // load the config data from db $this->collection = Billrun_Factory::db()->configCollection(); $this->options = array('receive', 'process', 'calculate'); $this->loadConfig(); }
/** * method to send * * @param type $message * @param type $recipients * @return \Billrun_Sms|boolean */ public function send($message, $recipients) { if (empty($message) || empty($recipients)) { Billrun_Factory::log()->log("can not send the sms, there are missing params - txt: " . $this->data['message'] . " recipients: " . print_r($this->data['recipients'], TRUE) . " from: " . $this->data['from'], Zend_Log::WARN); return false; } $unicode_text = $this->sms_unicode($message); if (!empty($message) && empty($unicode_text)) { $language = '1'; } else { $language = '2'; } // Temporary - make sure is not 23 chars long $text = str_pad($message, 24, '+'); $period = 120; foreach ($recipients as $recipient) { $send_params = array('message' => $text, 'to' => $recipient, 'from' => $this->data['from'], 'language' => $language, 'username' => $this->data['user'], 'password' => $this->data['pwd'], 'acknowledge' => "false", 'period' => $period, 'channel' => "SRV"); $url = $this->data['provisioning'] . "?" . http_build_query($send_params); $sms_result = Billrun_Util::sendRequest($url); $exploded = explode(',', $sms_result); $response = array('error-code' => empty($exploded[0]) ? 'error' : 'success', 'cause-code' => $exploded[1], 'error-description' => $exploded[2], 'tid' => $exploded[3]); Billrun_Factory::log()->log("phone: " . $recipient . " encoded_text: " . $message . " url: " . $url . " result" . print_R($response, 1), Zend_Log::INFO); } return $response['error-code'] == 'success' ? true : false; }
public function indexAction() { $this->redirect('admin'); $this->getView()->title = "BillRun | The best open source billing system"; $this->getView()->content = "Open Source Last Forever!"; $this->getView()->favicon = Billrun_Factory::config()->getConfigValue('favicon'); }
/** * method that outputs account, subscribers and usage of requested accounts and requested date usage * it's called automatically by the api main controller */ public function execute() { Billrun_Factory::log()->log("Execute data triggers", Zend_Log::INFO); $request = $this->getRequest()->getRequest(); // supports GET / POST requests $params = array('plan', 'data_usage', 'from_account_id', 'to_account_id', 'billrun'); foreach ($params as $param) { if (!isset($request[$param])) { $msg = 'Missing required parameter: ' . $param; Billrun_Factory::log()->log($msg, Zend_Log::ERR); $this->getController()->setOutput(array(array('status' => 0, 'desc' => 'failed', 'output' => $msg))); return; } } Billrun_Factory::log()->log("Request params Received: plan-" . $request['plan'] . ", data_usage-" . $request['data_usage'] . ", from_account_id-" . $request['from_account_id'] . ", to_account_id-" . $request['to_account_id'] . ", billrun-" . $request['billrun'], Zend_Log::INFO); $balances = new BalancesModel(array('size' => Billrun_Factory::config()->getConfigValue('balances.accounts.limit', 50000))); $results = $balances->getBalancesVolume($request['plan'], $request['data_usage'], $request['from_account_id'], $request['to_account_id'], $request['billrun']); if (empty($results)) { Billrun_Factory::log()->log('Some error happen, no result, received parameters: ' . print_r($request, true), Zend_Log::ERR); return; } $counter = 0; $accounts = array(); foreach ($results as $result) { $accounts['aid'][$result['aid']]['subs'][$result['sid']] = Billrun_Util::byteFormat($result['balance']['totals']['data']['usagev'], 'MB', 2, false, '.', ''); $counter++; } $this->getController()->setOutput(array(array('status' => 1, 'desc' => 'success', 'subscribers_count' => $counter, 'output' => $accounts))); return true; }
/** * method to execute remove of billing lines (only credit and active) * it's called automatically by the api main controller */ public function execute() { Billrun_Factory::log()->log("Execute api remove", Zend_Log::INFO); $request = $this->getRequest()->getRequest(); // supports GET / POST requests Billrun_Factory::log()->log("Input: " . print_R($request, 1), Zend_Log::INFO); $stamps = array(); foreach ($request['stamps'] as $line_stamp) { $clear_stamp = Billrun_Util::filter_var($line_stamp, FILTER_SANITIZE_STRING, FILTER_FLAG_ALLOW_HEX); if (!empty($clear_stamp)) { $stamps[] = $clear_stamp; } } if (empty($stamps)) { Billrun_Factory::log()->log("remove action failed; no correct stamps", Zend_Log::INFO); $this->getController()->setOutput(array(array('status' => false, 'desc' => 'failed - invalid stamps input', 'input' => $request))); return true; } $model = new LinesModel(); $query = array('source' => 'api', 'stamp' => array('$in' => $stamps), '$or' => array(array('billrun' => array('$gte' => Billrun_Billrun::getActiveBillrun())), array('billrun' => array('$exists' => false)))); $ret = $model->remove($query); if (!isset($ret['ok']) || !$ret['ok'] || !isset($ret['n'])) { Billrun_Factory::log()->log("remove action failed pr miscomplete", Zend_Log::INFO); $this->getController()->setOutput(array(array('status' => false, 'desc' => 'remove failed', 'input' => $request))); return true; } Billrun_Factory::log()->log("remove success", Zend_Log::INFO); $this->getController()->setOutput(array(array('status' => $ret['n'], 'desc' => 'success', 'input' => $request))); }
public function calc() { Billrun_Factory::log()->log("Execute reset", Zend_Log::INFO); $rebalance_queue = Billrun_Factory::db()->rebalance_queueCollection(); $limit = Billrun_Config::getInstance()->getConfigValue('resetlines.limit', 10); $offset = Billrun_Config::getInstance()->getConfigValue('resetlines.offset', '1 hour'); $query = array('creation_date' => array('$lt' => new MongoDate(strtotime($offset . ' ago')))); $sort = array('creation_date' => 1); $results = $rebalance_queue->find($query)->sort($sort)->limit($limit); $billruns = array(); $all_sids = array(); foreach ($results as $result) { $billruns[$result['billrun_key']][] = $result['sid']; $all_sids[] = $result['sid']; } foreach ($billruns as $billrun_key => $sids) { $model = new ResetLinesModel($sids, $billrun_key); try { $ret = $model->reset(); if (isset($ret['err']) && !is_null($ret['err'])) { return FALSE; } $rebalance_queue->remove(array('sid' => array('$in' => $sids))); } catch (Exception $exc) { Billrun_Factory::log()->log('Error resetting sids ' . implode(',', $sids) . ' of billrun ' . $billrun_key . '. Error was ' . $exc->getTraceAsString(), Zend_Log::ALERT); return $this->setError($exc->getTraceAsString(), array('sids' => $sids, 'billrun_key' => $billrun_key)); } } Billrun_Factory::log()->log("Success resetting sids " . implode(',', $all_sids), Zend_Log::INFO); return true; }
/** * load the container the need to be generate */ public function load() { $this->data = $this->collection->aggregate($this->aggregation_array); //TODO how to perform it on the secondaries? Billrun_Factory::log()->log("generator entities loaded: " . count($this->data), Zend_Log::INFO); Billrun_Factory::dispatcher()->trigger('afterGeneratorLoadData', array('generator' => $this)); }
/** * Get the data resource * * @return Mongo Cursor */ public function getData($filter_query = array()) { $cursor = $this->collection->query($filter_query)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref')); $this->_count = $cursor->count(); $resource = $cursor->sort($this->sort)->skip($this->offset())->limit($this->size); return $resource; }
/** * Process a given file and create a temporary response file to it. * @param type $filePath the location of the file that need to be proceesed * @param type $logLine the log line that associated with the file to process. * @return boolean|string return the temporary file path if the file should be responded to. * or false if the file wasn't processed into the DB yet. */ protected function processFileForResponse($filePath, $logLine) { $logLine = $logLine->getRawData(); $this->linesCount = $this->linesErrors = $this->totalChargeAmount = 0; $linesCollection = Billrun_Factory::db()->linesCollection(); $dbLines = $linesCollection->query()->equals('file', $logLine['file']); //run only after the lines were processed by the billrun. if ($dbLines->count() == 0 || $linesCollection->query()->equals('file', $logLine['file'])->exists('billrun')->count() == 0) { return false; } //save file to a temporary location $responsePath = $this->workspace . rand(); $srcFile = fopen($filePath, "r+"); $file = fopen($responsePath, "w"); $lines = ""; foreach ($dbLines as $dbLine) { //alter data line $line = $this->updateLine($dbLine->getRawData(), $logLine); if ($line) { $this->linesCount++; $this->totalChargeAmount += floatval($dbLine->get('call_charge')); $lines .= $line . "\n"; } } //alter lines fputs($file, $this->updateHeader(fgets($srcFile), $logLine) . "\n"); fputs($file, $lines); //alter trailer fputs($file, $this->updateTrailer($logLine) . "\n"); fclose($file); return $responsePath; }
public function byimsitestAction() { $working_dir = "/home/shani/Documents/S.D.O.C/BillRun/Files/Docs/Tests/"; $row = 1; if (($handle = fopen($working_dir . "billing_crm_diff_with_sid_and_imsi-1.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 0, "\t")) !== FALSE) { error_log($row); if ($row++ == 1) { continue; } $this->subscriber = Billrun_Factory::subscriber(); $params['time'] = "2013-10-24 23:59:59"; $params['IMSI'] = $data[5]; $params_arr[] = array('time' => $params['time'], 'DATETIME' => $params['time'], 'IMSI' => $params['IMSI']); $details = $this->subscriber->load($params); $data['normal_plan'] = $details->plan; $newCsvData[$data[4]] = $data; } fclose($handle); $list = Subscriber_Golan::requestList($params_arr); foreach ($list as $arr) { $newCsvData[$arr['subscriber_id']]['bulk_plan'] = $arr['plan']; } $handle = fopen('/tmp/result.csv', 'w'); foreach ($newCsvData as $line) { fputcsv($handle, $line); } fclose($handle); } }
protected function isOn() { if (Billrun_Factory::config()->getConfigValue($this->getRequest()->action)) { return true; } return false; }
/** * method to execute the query * it's called automatically by the api main controller */ public function execute() { Billrun_Factory::log()->log("Execute api query billrun", Zend_Log::INFO); $request = $this->getRequest()->getRequest(); // supports GET / POST requests Billrun_Factory::log()->log("Input: " . print_R($request, 1), Zend_Log::INFO); if (!isset($request['aid'])) { $this->setError('Require to supply aid or sid', $request); return true; } $find = array(); $max_list = 1000; if (isset($request['aid'])) { $aids = Billrun_Util::verify_array($request['aid'], 'int'); if (count($aids) > $max_list) { $this->setError('Maximum of aid is ' . $max_list, $request); return true; } $find['aid'] = array('$in' => $aids); } if (isset($request['billrun'])) { $find['billrun_key'] = $this->getBillrunQuery($request['billrun']); } $options = array('sort' => array('aid', 'billrun_key')); $cacheParams = array('fetchParams' => array('options' => $options, 'find' => $find)); $this->setCacheLifeTime(604800); // 1 week $results = $this->cache($cacheParams); Billrun_Factory::log()->log("query success", Zend_Log::INFO); $ret = array(array('status' => 1, 'desc' => 'success', 'input' => $request, 'details' => $results)); $this->getController()->setOutput($ret); }
protected function initThings() { $config_path = Billrun_Factory::config()->getConfigValue('compare.config_path', '/var/www/billrun/conf/compare/config.ini'); $config = (new Yaf_Config_Ini($config_path))->toArray(); $this->included_accounts = array_unique(isset($config['include_accounts']) ? $config['include_accounts'] : array()); $this->excluded_accounts = array_unique(isset($config['exclude_accounts']) ? $config['exclude_accounts'] : array()); $this->excluded_ndcsns = array_unique(isset($config['exclude_ndcsns']) ? $config['exclude_ndcsns'] : array()); }
protected function respondAFile($responseFilePath, $fileName, $logLine) { Billrun_Factory::log()->log("Responding on : {$fileName}", Zend_Log::DEBUG); $data = $logLine->getRawData(); $data['response_time'] = time(); $logLine->setRawData($data); $logLine->save(); return $responseFilePath; }
/** * Add data that is needed to use the event object/DB document later * @param Array|Object $event the event to add fields to. * @return Array|Object the event object with added fields */ protected function addAlertData(&$newEvent) { $newEvent['units'] = 'MIN'; $newEvent['value'] = $newEvent['total']; $newEvent['threshold'] = Billrun_Factory::config()->getConfigValue('ilds.threshold', 100); $newEvent['event_type'] = 'ILDS'; $newEvent['msisdn'] = $newEvent['caller_phone_no']; return $newEvent; }
/** * @see Billrun_Processor::parse() */ protected function parse() { if (!is_resource($this->fileHandler)) { Billrun_Factory::log()->log('Resource is not configured well', Zend_Log::ERR); return FALSE; } $this->data['data'] = json_decode(stream_get_contents($this->fileHandler), true); return $this->processData(); }
/** * method to receive the balances lines that over requested date usage * * @return Mongodloid_Cursor Mongo cursor for iteration */ public function getBalancesVolume($plan, $data_usage, $from_account_id, $to_account_id, $billrun) { $params = array('name' => $plan, 'time' => Billrun_Util::getStartTime($billrun)); $plan_id = Billrun_Factory::plan($params); $id = $plan_id->get('_id')->getMongoID(); $data_usage_bytes = Billrun_Util::megabytesToBytesFormat((int) $data_usage); $query = array('aid' => array('$gte' => (int) $from_account_id, '$lte' => (int) $to_account_id), 'billrun_month' => $billrun, 'balance.totals.data.usagev' => array('$gt' => (double) $data_usage_bytes), 'current_plan' => Billrun_Factory::db()->plansCollection()->createRef($id)); // print_R($query);die; return $this->collection->query($query)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref'))->hint(array('aid' => 1, 'billrun_month' => 1))->limit($this->size); }
public function __construct(array $params = array()) { if (isset($params['collection'])) { unset($params['collection']); } parent::__construct($params); $this->collection = Billrun_Factory::db(Billrun_Factory::config()->getConfigValue('fraud.db'))->eventsCollection(); $this->collection_name = 'events'; $this->search_key = "stamp"; }
protected function insertToQueue($entity) { $queue = Billrun_Factory::db()->queueCollection(); if (!is_object($queue)) { Billrun_Factory::log()->log('Queue collection is not defined', Zend_Log::ALERT); return false; } else { return $queue->insert(array('stamp' => $entity['stamp'], 'type' => $entity['type'], 'urt' => $entity['urt'], 'calc_name' => false, 'calc_time' => false), array('w' => 1)); } }
public function _initLayout(Yaf_Dispatcher $dispatcher) { // Enable template layout only on admin // TODO: make this more accurate if (strpos($dispatcher->getRequest()->getRequestUri(), "admin") !== FALSE && strpos($dispatcher->getRequest()->getRequestUri(), "edit") === FALSE && strpos($dispatcher->getRequest()->getRequestUri(), "confirm") === FALSE && strpos($dispatcher->getRequest()->getRequestUri(), "logDetails") === FALSE) { $path = Billrun_Factory::config()->getConfigValue('application.directory'); $view = new Yaf_View_Simple($path . '/views/layout'); $dispatcher->setView($view); } }
protected function processFileForResponse($filePath, $logLine) { $tmpLogLine = $logLine->getRawData(); $unprocessDBLines = Billrun_Factory::db()->linesCollection()->query()->notExists('billrun')->equals('file', $tmpLogLine['file']); //run only if theres promlematic lines in the file. if ($unprocessDBLines->count() == 0) { return false; } return parent::processFileForResponse($filePath, $logLine); }
/** * method to execute the query * it's called automatically by the api main controller */ public function execute() { Billrun_Factory::log()->log("Execute api query aggregate", Zend_Log::INFO); $request = $this->getRequest()->getRequest(); // supports GET / POST requests Billrun_Factory::log()->log("Input: " . print_R($request, 1), Zend_Log::DEBUG); if (!isset($request['aid']) && !isset($request['sid'])) { $this->setError('Require to supply aid or sid', $request); return true; } $find = array(); $max_list = 1000; if (isset($request['aid'])) { $aids = Billrun_Util::verify_array($request['aid'], 'int'); if (count($aids) > $max_list) { $this->setError('Maximum of aid is ' . $max_list, $request); return true; } $find['aid'] = array('$in' => $aids); } if (isset($request['sid'])) { $sids = Billrun_Util::verify_array($request['sid'], 'int'); if (count($sids) > $max_list) { $this->setError('Maximum of sid is ' . $max_list, $request); return true; } $find['sid'] = array('$in' => $sids); } if (isset($request['billrun'])) { $find['billrun'] = $this->getBillrunQuery($request['billrun']); } if (isset($request['query'])) { $query = $this->getArrayParam($request['query']); $find = array_merge($find, (array) $query); } if (isset($request['groupby'])) { $groupby = array('_id' => $this->getArrayParam($request['groupby'])); } else { $groupby = array('_id' => null); } if (isset($request['aggregate'])) { $aggregate = $this->getArrayParam($request['aggregate']); } else { $aggregate = array('count' => array('$sum' => 1)); } $group = array_merge($groupby, $aggregate); $options = array('sort' => array('urt'), 'page' => isset($request['page']) && $request['page'] > 0 ? (int) $request['page'] : 0, 'size' => isset($request['size']) && $request['size'] > 0 ? (int) $request['size'] : 1000); $cacheParams = array('fetchParams' => array('options' => $options, 'find' => $find, 'group' => $group, 'groupby' => $groupby)); $this->setCacheLifeTime(604800); // 1 week $results = $this->cache($cacheParams); Billrun_Factory::log()->log("Aggregate query success", Zend_Log::INFO); $ret = array(array('status' => 1, 'desc' => 'success', 'input' => $request, 'details' => $results)); $this->getController()->setOutput($ret); }
/** * Caches the rates in the memory for fast computations */ protected function loadRates() { $rates_coll = Billrun_Factory::db()->ratesCollection(); $query = array('key' => array('$in' => array('CREDIT_VATABLE', 'CREDIT_VAT_FREE'))); $rates = $rates_coll->query($query)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref')); $this->rates = array(); foreach ($rates as $rate) { $rate->collection($rates_coll); $this->rates[$rate['key']] = $rate; } }
/** * Caches the rates in the memory for fast computations */ protected function loadRates() { $rates_coll = Billrun_Factory::db()->ratesCollection(); $query = array('rates.service' => array('$exists' => 1)); $rates = $rates_coll->query($query)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref')); $this->rates = array(); foreach ($rates as $rate) { $rate->collection($rates_coll); $this->rates[] = $rate; } }
public function __construct($options = array()) { parent::__construct($options); $configPath = Billrun_Factory::config()->getConfigValue($this->getType() . '.billrun.config_path'); if ($configPath) { $config = new Yaf_Config_Ini($configPath); if (isset($config->billrun->exclude)) { $this->excludes = $config->billrun->exclude->toArray(); } } }
/** * @see Billrun_Calculator_Rate::getLineRate */ protected function getLineRate($row, $usage_type) { $line_time = $row['urt']; foreach ($this->rates as $rate) { if (preg_match($rate['params']['sgsn_addresses'], $row['sgsn_address']) && $rate['from'] <= $line_time && $line_time <= $rate['to']) { return $rate; } } Billrun_Factory::log()->log("Couldn't find rate for row : " . print_r($row['stamp'], 1), Zend_Log::DEBUG); return FALSE; }
/** * method to markdown all the lines that triggered the event * * @param array $items the lines * @param string $pluginName the plugin name which triggered the event * * @return array affected lines */ public function handlerMarkDown(&$items, $pluginName) { if ($pluginName != $this->getName() || !$items) { return; } $ret = array(); $lines = Billrun_Factory::db()->linesCollection(); foreach ($items as &$item) { $ret[] = $lines->update(array('stamp' => array('$in' => $item['lines_stamps'])), array('$set' => array('event_stamp' => $item['event_stamp'])), array('multiple' => 1)); } return $ret; }
protected static function initCollection($collection_name) { if (isset(self::$keys[$collection_name])) { $coll = Billrun_Factory::db()->{$collection_name . "Collection"}(); $resource = $coll->query()->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref')); foreach ($resource as $entity) { $entity->collection($coll); self::$entities[$collection_name]['by_id'][strval($entity->getId())] = $entity; self::$entities[$collection_name]['by_key'][$entity[self::$keys[$collection_name]]][] = $entity; } } }
public function prev_calc($value) { $val = $value[0]; $names = Billrun_Factory::config()->getConfigValue('queue.calculators'); if ($val != $names[0]) { $key = array_search($val, $names); $prev = $names[$key - 1]; } else { $prev = false; } return array($prev); }
protected function loadSidLines($sid, $limits, $plan, $groupSelected, $dayKey) { $line_year = date('Y', strtotime($this->line_time)); $from = date('YmdHis', strtotime(str_replace('%Y', $line_year, $limits['period']['from']) . ' 00:00:00')); $to = date('YmdHis', strtotime(str_replace('%Y', $line_year, $limits['period']['to']) . ' 23:59:59')); $match = array('$match' => array('sid' => $sid, 'type' => 'tap3', 'plan' => $plan->getData()->get('name'), 'basicCallInformation.CallEventStartTimeStamp.localTimeStamp' => array('$gte' => $from, '$lte' => $to), 'arategroup' => $groupSelected, 'in_group' => array('$gt' => 0), 'billrun' => array('$exists' => true))); $group = array('$group' => array('_id' => array('day_key' => array('$substr' => array('$basicCallInformation.CallEventStartTimeStamp.localTimeStamp', 0, 8))))); $match2 = array('$match' => array('_id.day_key' => array('$lte' => $dayKey))); $results = Billrun_Factory::db()->linesCollection()->aggregate($match, $group, $match2); return array_map(function ($res) { return $res['_id']['day_key']; }, $results); }