/** * load the data to aggregate */ public function load() { $billrun_key = $this->getStamp(); $subscriber = Billrun_Factory::subscriber(); $filename = $billrun_key . '_leftover_aggregator_input'; Billrun_Factory::log()->log("Loading file " . $filename, Zend_Log::INFO); $billrun_end_time = Billrun_Util::getEndTime($billrun_key); $this->data = $subscriber->getListFromFile('files/' . $filename, $billrun_end_time); if (!count($this->data)) { Billrun_Factory::log()->log("No accounts were found for leftover aggregator", Zend_Log::ALERT); } if (is_array($this->data)) { $this->data = array_slice($this->data, $this->page * $this->size, $this->size, TRUE); } Billrun_Factory::log()->log("aggregator entities loaded: " . count($this->data), Zend_Log::INFO); Billrun_Factory::dispatcher()->trigger('afterAggregatorLoadData', array('aggregator' => $this)); }
/** * Removes lines from queue, reset added fields off lines and re-insert to queue first stage * @todo support update/removal of credit lines */ protected function resetLines() { $lines_coll = Billrun_Factory::db()->linesCollection(); $queue_coll = Billrun_Factory::db()->queueCollection(); if (!empty($this->sids) && !empty($this->billrun_key)) { $offset = 0; while ($update_count = count($update_sids = array_slice($this->sids, $offset, 10))) { Billrun_Factory::log()->log('Resetting lines of subscribers ' . implode(',', $update_sids), Zend_Log::INFO); $query = array('$or' => array(array('billrun' => $this->billrun_key), array('billrun' => array('$exists' => FALSE), 'urt' => array('$gte' => new MongoDate(Billrun_Util::getStartTime($this->billrun_key)), '$lte' => new MongoDate(Billrun_Util::getEndTime($this->billrun_key))))), 'sid' => array('$in' => $update_sids), 'type' => array('$ne' => 'credit'), 'process_time' => array('$lt' => date(Billrun_Base::base_dateformat, strtotime($this->process_time_offset . ' ago')))); $lines = $lines_coll->query($query); $stamps = array(); $queue_lines = array(); foreach ($lines as $line) { $stamps[] = $line['stamp']; $queue_line = array('calc_name' => false, 'calc_time' => false, 'stamp' => $line['stamp'], 'type' => $line['type'], 'urt' => $line['urt'], 'skip_fraud' => true); // todo: refactoring $advancedProperties = Billrun_Factory::config()->getConfigValue("queue.advancedProperties", array('imsi', 'msisdn', 'called_number', 'calling_number')); foreach ($advancedProperties as $property) { if (isset($line[$property]) && !isset($queue_line[$property])) { $queue_line[$property] = $line[$property]; } } $queue_lines[] = $queue_line; } $stamps_query = array('stamp' => array('$in' => $stamps)); $update = array('$unset' => array('apr' => 1, 'aprice' => 1, 'arate' => 1, 'arategroup' => 1, 'billrun' => 1, 'in_arate' => 1, 'in_group' => 1, 'in_plan' => 1, 'out_plan' => 1, 'over_arate' => 1, 'over_group' => 1, 'over_plan' => 1, 'plan' => 1, 'usagesb' => 1, 'usagev' => 1), '$set' => array('rebalance' => new MongoDate())); if ($stamps) { $ret = $queue_coll->remove($stamps_query, array('w' => 1)); // ok == 1, err null if (isset($ret['err']) && !is_null($ret['err'])) { return FALSE; } else { $ret = $this->resetBalances($update_sids); // err null if (isset($ret['err']) && !is_null($ret['err'])) { return FALSE; } else { if (Billrun_Factory::db()->compareServerVersion('2.6', '>=') === true) { $ret = $queue_coll->batchInsert($queue_lines, array('w' => 1)); // ok==true, nInserted==0 if w was 0 if (isset($ret['err']) && !is_null($ret['err'])) { return FALSE; } } else { foreach ($queue_lines as $qline) { $ret = $queue_coll->insert($qline, array('w' => 1)); // ok==1, err null if (isset($ret['err']) && !is_null($ret['err'])) { return FALSE; } } } $ret = $lines_coll->update($stamps_query, $update, array('multiple' => 1, 'w' => 1)); // err null if (isset($ret['err']) && !is_null($ret['err'])) { return FALSE; } } } } $offset += 10; } } return TRUE; }
/** * load the data to aggregate */ public function load() { $billrun_key = $this->getStamp(); $date = date(Billrun_Base::base_dateformat, Billrun_Util::getEndTime($billrun_key)); $subscriber = Billrun_Factory::subscriber(); Billrun_Factory::log()->log("Loading page " . $this->page . " of size " . $this->size, Zend_Log::INFO); if ($this->overrideAccountIds) { $this->data = array(); foreach ($this->overrideAccountIds as $account_id) { $this->data = $this->data + $subscriber->getList(0, 1, $date, $account_id); } } else { $this->data = $subscriber->getList($this->page, $this->size, $date); } Billrun_Factory::log()->log("aggregator entities loaded: " . count($this->data), Zend_Log::INFO); Billrun_Factory::dispatcher()->trigger('afterAggregatorLoadData', array('aggregator' => $this)); }
/** * Get the VAT value for some billing * @param string $billrun_key the billing period to get VAT for * @return float the VAT at the given time (0-1) */ public static function getVATByBillrunKey($billrun_key) { if (!isset(self::$vatsByBillrun[$billrun_key])) { $billrun_end_time = Billrun_Util::getEndTime($billrun_key); self::$vatsByBillrun[$billrun_key] = self::getVATAtDate($billrun_end_time); if (is_null(self::$vatsByBillrun[$billrun_key])) { self::$vatsByBillrun[$billrun_key] = floatval(Billrun_Factory::config()->getConfigValue('pricing.vat', 0.18)); } } return self::$vatsByBillrun[$billrun_key]; }
public function __construct($options = array()) { if (isset($options['autoload'])) { $autoload = $options['autoload']; } else { $autoload = true; } $options['autoload'] = false; parent::__construct($options); if (isset($options['calculator']['limit'])) { $this->limit = $options['calculator']['limit']; } if (isset($options['calculator']['vatable'])) { $this->vatable = $options['calculator']['vatable']; } if (isset($options['calculator']['months_limit'])) { $this->months_limit = $options['calculator']['months_limit']; } if (isset($options['calculator']['unlimited_to_balances'])) { $this->unlimited_to_balances = (bool) $options['calculator']['unlimited_to_balances']; } $this->billrun_lower_bound_timestamp = is_null($this->months_limit) ? 0 : strtotime($this->months_limit . " months ago"); // set months limit if ($autoload) { $this->load(); } $this->loadRates(); $this->loadPlans(); $this->balances = Billrun_Factory::db(array('name' => 'balances'))->balancesCollection()->setReadPreference('RP_PRIMARY'); $this->active_billrun = Billrun_Billrun::getActiveBillrun(); $this->active_billrun_end_time = Billrun_Util::getEndTime($this->active_billrun); $this->next_active_billrun = Billrun_Util::getFollowingBillrunKey($this->active_billrun); // max recursive retrues for value=oldValue tactic $this->concurrentMaxRetries = (int) Billrun_Factory::config()->getConfigValue('updateValueEqualOldValueMaxRetries', 8); $this->sidsQueuedForRebalance = array_flip(Billrun_Factory::db()->rebalance_queueCollection()->distinct('sid')); }
/** * method to update service row from API * @param array $service_row * @return $service_row after addition of fields */ public static function parseServiceRow($service_row, $billrun_key) { $service_row['source'] = 'api'; $service_row['usaget'] = $service_row['type'] = 'service'; $service_row['urt'] = new MongoDate(Billrun_Util::getEndTime($billrun_key)); ksort($service_row); $service_row['stamp'] = Billrun_Util::generateArrayStamp($service_row); return $service_row; }
/** * Gets all the account lines for this billrun from the db * @param int $aid the account id * @param int $start_time lower bound date to get lines from. A unix timestamp * @return Mongodloid_Cursor the mongo cursor used to iterate over the lines * @todo remove aid parameter */ protected function getAccountLines($aid, $start_time = 0, $include_flats = true) { $start_time = new MongoDate($start_time); $end_time = new MongoDate(Billrun_Util::getEndTime($this->billrun_key)); $query = array('aid' => $aid, 'urt' => array('$lte' => $end_time, '$gte' => $start_time)); if (!$include_flats) { $query['type'] = array('$ne' => 'flat'); } if ($this->allowOverride == 1) { $query['billrun']['$in'] = array('000000', $this->billrun_key); } else { if ($this->allowOverride == 2) { $query['billrun'] = $this->billrun_key; } else { $query['billrun'] = '000000'; } } $hint = array('aid' => 1, 'urt' => 1); $sort = array('aid' => 1, 'urt' => 1); Billrun_Factory::log()->log("Querying for account " . $aid . " lines", Zend_Log::INFO); $cursor = $this->lines->query($query)->cursor()->fields($this->filter_fields)->sort($sort)->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref'))->hint($hint); Billrun_Factory::log()->log("Finished querying for account " . $aid . " lines", Zend_Log::INFO); // $results = array(); // Billrun_Factory::log()->log("Saving account " . $aid . " lines to array", Zend_Log::DEBUG); // foreach ($cursor as $entity) { // $results[] = $entity; // } // Billrun_Factory::log()->log("Finished saving account " . $aid . " lines to array", Zend_Log::DEBUG); // return $results; return $cursor; }