Exemple #1
0
 public function load()
 {
     $this->date = date(Billrun_Base::base_dateformat, $this->now);
     $subscriber = Billrun_Factory::subscriber();
     $this->account_data = array();
     $res = $subscriber->getList(0, 1, $this->date, $this->aid);
     if (!empty($res)) {
         $this->account_data = current($res);
     }
     $billrun_params = array('aid' => $this->aid, 'billrun_key' => $this->stamp, 'autoload' => false);
     $billrun = Billrun_Factory::billrun($billrun_params);
     $manual_lines = array();
     $deactivated_subscribers = array();
     foreach ($this->account_data as $subscriber) {
         if (!Billrun_Factory::db()->rebalance_queueCollection()->query(array('sid' => $subscriber->sid), array('sid' => 1))->cursor()->current()->isEmpty()) {
             $subscriber_status = "REBALANCE";
             $billrun->addSubscriber($subscriber, $subscriber_status);
             continue;
         }
         if ($billrun->subscriberExists($subscriber->sid)) {
             Billrun_Factory::log()->log("Billrun " . $this->stamp . " already exists for subscriber " . $subscriber->sid, Zend_Log::ALERT);
             continue;
         }
         $next_plan_name = $subscriber->getNextPlanName();
         if (is_null($next_plan_name) || $next_plan_name == "NULL") {
             $subscriber_status = "closed";
             $current_plan_name = $subscriber->getCurrentPlanName();
             if (is_null($current_plan_name) || $current_plan_name == "NULL") {
                 $deactivated_subscribers[] = array("sid" => $subscriber->sid);
             }
         } else {
             $subscriber_status = "open";
             $flat_entry = $subscriber->getFlatEntry($this->stamp, true);
             $manual_lines = array_merge($manual_lines, array($flat_entry['stamp'] => $flat_entry));
         }
         $manual_lines = array_merge($manual_lines, $subscriber->getCredits($this->stamp, true));
         $billrun->addSubscriber($subscriber, $subscriber_status);
     }
     //		print_R($manual_lines);die;
     $this->lines = $billrun->addLines($manual_lines, $deactivated_subscribers);
     $billrun->filter_disconected_subscribers($deactivated_subscribers);
     $this->data = $billrun->getRawData();
 }
Exemple #2
0
 public function load()
 {
     $this->date = date(Billrun_Base::base_dateformat, $this->now);
     $subscriber = Billrun_Factory::subscriber();
     $this->account_data = array();
     $res = $subscriber->getList(0, 1, $this->date, $this->aid);
     if (!empty($res)) {
         $this->account_data = current($res);
     }
     $previous_billrun_key = Billrun_Util::getPreviousBillrunKey($this->stamp);
     if (Billrun_Billrun::exists($this->aid, $previous_billrun_key)) {
         $start_time = 0;
         // maybe some lines are late (e.g. tap3)
     } else {
         $start_time = Billrun_Util::getStartTime($this->stamp);
         // to avoid getting lines of previous billruns
     }
     $billrun_params = array('aid' => $this->aid, 'billrun_key' => $this->stamp, 'autoload' => false);
     $billrun = Billrun_Factory::billrun($billrun_params);
     $flat_lines = array();
     foreach ($this->account_data as $subscriber) {
         if ($billrun->subscriberExists($subscriber->sid)) {
             Billrun_Factory::log()->log("Billrun " . $this->stamp . " already exists for subscriber " . $subscriber->sid, Zend_Log::ALERT);
             continue;
         }
         $next_plan_name = $subscriber->getNextPlanName();
         if (is_null($next_plan_name) || $next_plan_name == "NULL") {
             $subscriber_status = "closed";
         } else {
             $subscriber_status = "open";
             $flat_lines[] = new Mongodloid_Entity($subscriber->getFlatEntry($this->stamp));
         }
         $billrun->addSubscriber($subscriber, $subscriber_status);
     }
     $this->lines = $billrun->addLines(false, $start_time, $flat_lines);
     $this->data = $billrun->getRawData();
 }
Exemple #3
0
 /**
  * execute aggregate
  */
 public function aggregate()
 {
     Billrun_Factory::dispatcher()->trigger('beforeAggregate', array($this->data, &$this));
     $account_billrun = false;
     $billrun_key = $this->getStamp();
     $billruns_count = 0;
     $skipped_billruns_count = 0;
     if ($this->bulkAccountPreload) {
         Billrun_Factory::log('loading accounts that will be needed to be preloaded...', Zend_log::INFO);
         $dataKeys = array_keys($this->data);
         //$existingAccounts = array();
         foreach ($dataKeys as $key => $aid) {
             if (!$this->overrideAccountIds && Billrun_Billrun::exists($aid, $billrun_key)) {
                 unset($dataKeys[$key]);
                 //$existingAccounts[$aid]  = $this->data[$aid];
             }
         }
     }
     foreach ($this->data as $accid => $account) {
         if ($this->memory_limit > -1 && memory_get_usage() > $this->memory_limit) {
             Billrun_Factory::log('Customer aggregator memory limit of ' . $this->memory_limit / 1048576 . 'M has reached. Exiting (page: ' . $this->page . ', size: ' . $this->size . ').', Zend_log::ALERT);
             break;
         }
         //pre-load  account lines
         if ($this->bulkAccountPreload && !($billruns_count % $this->bulkAccountPreload) && count($dataKeys) > $billruns_count) {
             $aidsToLoad = array_slice($dataKeys, $billruns_count, $this->bulkAccountPreload);
             Billrun_Billrun::preloadAccountsLines($aidsToLoad, $billrun_key);
         }
         Billrun_Factory::dispatcher()->trigger('beforeAggregateAccount', array($accid, $account, &$this));
         Billrun_Factory::log('Current account index: ' . ++$billruns_count, Zend_log::INFO);
         //			if (!Billrun_Factory::config()->isProd()) {
         //				if ($this->testAcc && is_array($this->testAcc) && !in_array($accid, $this->testAcc)) {//TODO : remove this??
         //					//Billrun_Factory::log(" Moving on nothing to see here... , account Id : $accid");
         //					continue;
         //				}
         //			}
         if (!$this->overrideAccountIds && Billrun_Billrun::exists($accid, $billrun_key)) {
             Billrun_Factory::log()->log("Billrun " . $billrun_key . " already exists for account " . $accid, Zend_Log::ALERT);
             $skipped_billruns_count++;
             continue;
         }
         $params = array('aid' => $accid, 'billrun_key' => $billrun_key, 'autoload' => !empty($this->overrideAccountIds));
         $account_billrun = Billrun_Factory::billrun($params);
         if ($this->overrideAccountIds) {
             $account_billrun->resetBillrun();
         }
         $manual_lines = array();
         $deactivated_subscribers = array();
         foreach ($account as $subscriber) {
             Billrun_Factory::dispatcher()->trigger('beforeAggregateSubscriber', array($subscriber, $account_billrun, &$this));
             $sid = $subscriber->sid;
             if ($account_billrun->subscriberExists($sid)) {
                 Billrun_Factory::log()->log("Billrun " . $billrun_key . " already exists for subscriber " . $sid, Zend_Log::ALERT);
                 continue;
             }
             $next_plan_name = $subscriber->getNextPlanName();
             if (is_null($next_plan_name) || $next_plan_name == "NULL") {
                 $subscriber_status = "closed";
                 $current_plan_name = $subscriber->getCurrentPlanName();
                 if (is_null($current_plan_name) || $current_plan_name == "NULL") {
                     Billrun_Factory::log()->log("Subscriber " . $sid . " has current plan null and next plan null", Zend_Log::INFO);
                     $deactivated_subscribers[] = array("sid" => $sid);
                 }
             } else {
                 $subscriber_status = "open";
                 Billrun_Factory::log("Getting flat price for subscriber {$sid}", Zend_log::INFO);
                 $flat_price = $subscriber->getFlatPrice();
                 Billrun_Factory::log("Finished getting flat price for subscriber {$sid}", Zend_log::INFO);
                 if (is_null($flat_price)) {
                     Billrun_Factory::log()->log("Couldn't find flat price for subscriber " . $sid . " for billrun " . $billrun_key, Zend_Log::ALERT);
                     continue;
                 }
                 Billrun_Factory::log('Adding flat line to subscriber ' . $sid, Zend_Log::INFO);
                 $flat = $this->saveFlatLine($subscriber, $billrun_key);
                 $manual_lines = array_merge($manual_lines, array($flat['stamp'] => $flat));
                 Billrun_Factory::log('Finished adding flat line to subscriber ' . $sid, Zend_Log::INFO);
             }
             $manual_lines = array_merge($manual_lines, $this->saveCreditLines($subscriber, $billrun_key));
             $manual_lines = array_merge($manual_lines, $this->saveServiceLines($subscriber, $billrun_key));
             $account_billrun->addSubscriber($subscriber, $subscriber_status);
             Billrun_Factory::dispatcher()->trigger('afterAggregateSubscriber', array($subscriber, $account_billrun, &$this));
         }
         $lines = $account_billrun->addLines($manual_lines, $deactivated_subscribers);
         $account_billrun->filter_disconected_subscribers($deactivated_subscribers);
         //save the billrun
         if ($account_billrun->is_deactivated() === true) {
             Billrun_Factory::log('deactivated account, no need for invoice ' . $accid, Zend_Log::DEBUG);
             continue;
         }
         Billrun_Factory::log('Saving account ' . $accid, Zend_Log::INFO);
         if ($account_billrun->save() === false) {
             Billrun_Factory::log('Error saving account ' . $accid, Zend_Log::ALERT);
             continue;
         }
         $this->successfulAccounts[] = $accid;
         Billrun_Factory::log('Finished saving account ' . $accid, Zend_Log::INFO);
         Billrun_Factory::dispatcher()->trigger('aggregateBeforeCloseAccountBillrun', array($accid, $account, $account_billrun, $lines, &$this));
         Billrun_Factory::log("Closing billrun {$billrun_key} for account {$accid}", Zend_log::INFO);
         $account_billrun->close($this->min_invoice_id);
         Billrun_Factory::log("Finished closing billrun {$billrun_key} for account {$accid}", Zend_log::INFO);
         Billrun_Factory::dispatcher()->trigger('afterAggregateAccount', array($accid, $account, $account_billrun, $lines, &$this));
         if ($this->bulkAccountPreload) {
             Billrun_Billrun::clearPreLoadedLines(array($accid));
         }
     }
     if ($billruns_count == count($this->data)) {
         $end_msg = "Finished iterating page {$this->page} of size {$this->size}. Memory usage is " . memory_get_usage() / 1048576 . " MB\n";
         $end_msg .= "Processed " . ($billruns_count - $skipped_billruns_count) . " accounts, Skipped over {$skipped_billruns_count} accounts, out of a total of {$billruns_count} accounts";
         Billrun_Factory::log($end_msg, Zend_log::INFO);
         $this->sendEndMail($end_msg);
     }
     // @TODO trigger after aggregate
     Billrun_Factory::dispatcher()->trigger('afterAggregate', array($this->data, &$this));
     return $this->successfulAccounts;
 }
 /**
  * execute aggregate
  */
 public function aggregate()
 {
     if ($this->write_stamps_to_file) {
         if (!$this->initStampsDir()) {
             Billrun_Factory::log()->log("Could not create stamps file for page " . $this->page, Zend_Log::ALERT);
             return false;
         }
     }
     // @TODO trigger before aggregate
     Billrun_Factory::dispatcher()->trigger('beforeAggregate', array($this->data, &$this));
     $account_billrun = false;
     $billrun_key = $this->getStamp();
     $billruns_count = 0;
     foreach ($this->data as $accid => $account) {
         if ($this->memory_limit > -1 && memory_get_usage() > $this->memory_limit) {
             Billrun_Factory::log('Customer aggregator memory limit of ' . $this->memory_limit / 1048576 . 'M has reached. Exiting (page: ' . $this->page . ', size: ' . $this->size . ').', Zend_log::ALERT);
             break;
         }
         Billrun_Factory::dispatcher()->trigger('beforeAggregateAccount', array($accid, $account, &$this));
         Billrun_Factory::log('Current account index: ' . ++$billruns_count, Zend_log::INFO);
         if (!Billrun_Factory::config()->isProd()) {
             if ($this->testAcc && is_array($this->testAcc) && !in_array($accid, $this->testAcc)) {
                 //Billrun_Factory::log("Moving on nothing to see here... , account Id : $accid");
                 continue;
             }
         }
         if (Billrun_Billrun::exists($accid, $billrun_key)) {
             Billrun_Factory::log()->log("Billrun " . $billrun_key . " already exists for account " . $accid, Zend_Log::ALERT);
             continue;
         }
         $params = array('aid' => $accid, 'billrun_key' => $billrun_key, 'autoload' => false);
         $account_billrun = Billrun_Factory::billrun($params);
         $flat_lines = array();
         foreach ($account as $subscriber) {
             Billrun_Factory::dispatcher()->trigger('beforeAggregateSubscriber', array($subscriber, $account_billrun, &$this));
             $sid = $subscriber->sid;
             if ($account_billrun->subscriberExists($sid)) {
                 Billrun_Factory::log()->log("Billrun " . $billrun_key . " already exists for subscriber " . $sid, Zend_Log::ALERT);
                 continue;
             }
             $next_plan_name = $subscriber->getNextPlanName();
             if (is_null($next_plan_name) || $next_plan_name == "NULL") {
                 $subscriber_status = "closed";
             } else {
                 $subscriber_status = "open";
                 Billrun_Factory::log("Getting flat price for subscriber {$sid}", Zend_log::INFO);
                 $flat_price = $subscriber->getFlatPrice();
                 Billrun_Factory::log("Finished getting flat price for subscriber {$sid}", Zend_log::INFO);
                 if (is_null($flat_price)) {
                     Billrun_Factory::log()->log("Couldn't find flat price for subscriber " . $sid . " for billrun " . $billrun_key, Zend_Log::ALERT);
                     continue;
                 }
                 Billrun_Factory::log('Adding flat line to subscriber ' . $sid, Zend_Log::INFO);
                 $flat_lines[] = $this->saveFlatLine($subscriber, $billrun_key);
                 Billrun_Factory::log('Finished adding flat line to subscriber ' . $sid, Zend_Log::INFO);
             }
             $account_billrun->addSubscriber($subscriber, $subscriber_status);
             Billrun_Factory::dispatcher()->trigger('afterAggregateSubscriber', array($subscriber, $account_billrun, &$this));
         }
         if ($this->write_stamps_to_file) {
             $lines = $account_billrun->addLines(false, 0, $flat_lines);
             if (!empty($lines)) {
                 $stamps_str = implode("\n", array_keys($lines)) . "\n";
                 file_put_contents($this->file_path, $stamps_str, FILE_APPEND);
             }
         } else {
             $lines = $account_billrun->addLines(true, 0, $flat_lines);
         }
         //save the billrun
         Billrun_Factory::log('Saving account ' . $accid, Zend_Log::INFO);
         if ($account_billrun->save() === false) {
             Billrun_Factory::log('Error saving account ' . $accid, Zend_Log::ALERT);
             continue;
         }
         Billrun_Factory::log('Finished saving account ' . $accid, Zend_Log::INFO);
         Billrun_Factory::dispatcher()->trigger('aggregateBeforeCloseAccountBillrun', array($accid, $account, $account_billrun, $lines, &$this));
         Billrun_Factory::log("Closing billrun {$billrun_key} for account {$accid}", Zend_log::INFO);
         $account_billrun->close($this->min_invoice_id);
         Billrun_Factory::log("Finished closing billrun {$billrun_key} for account {$accid}", Zend_log::INFO);
         Billrun_Factory::dispatcher()->trigger('afterAggregateAccount', array($accid, $account, $account_billrun, $lines, &$this));
     }
     if ($billruns_count == count($this->data)) {
         $end_msg = "Finished iterating page {$this->page} of size {$this->size}. Memory usage is " . memory_get_usage() / 1048576 . " MB";
         Billrun_Factory::log($end_msg, Zend_log::INFO);
         $this->sendEndMail($end_msg);
     }
     // @TODO trigger after aggregate
     Billrun_Factory::dispatcher()->trigger('afterAggregate', array($this->data, &$this));
 }