Exemple #1
0
 /**
  * method to execute the refund
  * it's called automatically by the api main controller
  */
 public function execute()
 {
     Billrun_Factory::log()->log("Execute credit", Zend_Log::INFO);
     $request = $this->getRequest()->getRequest();
     // supports GET / POST requests
     $parsed_row = $this->parseRow($request);
     if (is_null($parsed_row)) {
         return;
     }
     $linesCollection = Billrun_Factory::db()->linesCollection();
     if ($linesCollection->query('stamp', $parsed_row['stamp'])->count() > 0) {
         return $this->setError('Transaction already exists in the DB', $request);
     }
     $parsed_row['process_time'] = date(Billrun_Base::base_dateformat);
     $entity = new Mongodloid_Entity($parsed_row);
     if ($entity->save($linesCollection) === false) {
         return $this->setError('failed to store into DB lines', $request);
     }
     if ($this->insertToQueue($entity) === false) {
         return $this->setError('failed to store into DB queue', $request);
     } else {
         $this->getController()->setOutput(array(array('status' => 1, 'desc' => 'success', 'stamp' => $entity['stamp'], 'input' => $request)));
         Billrun_Factory::log()->log("Added credit line " . $entity['stamp'], Zend_Log::INFO);
         return true;
     }
 }
Exemple #2
0
 public function equals(Mongodloid_Entity $obj)
 {
     $data1 = $this->getRawData();
     $data2 = $obj->getRawData();
     unset($data1['_id'], $data2['_id']);
     return $data1 == $data2;
 }
 /**
  * method to log the processing
  * 
  * @todo refactoring this method
  */
 protected function logDB($path, $remoteHost = null, $extraData = false)
 {
     $log = Billrun_Factory::db()->logCollection();
     $log_data = array('source' => static::$type, 'path' => $path, 'file_name' => basename($path));
     if (!is_null($remoteHost)) {
         $log_data['retrieved_from'] = $remoteHost;
     }
     if ($extraData) {
         $log_data['extra_data'] = $extraData;
     }
     $log_data['stamp'] = md5(serialize($log_data));
     $log_data['received_time'] = date(self::base_dateformat);
     Billrun_Factory::dispatcher()->trigger('beforeLogReceiveFile', array(&$log_data, $this));
     $entity = new Mongodloid_Entity($log_data);
     if ($log->query('stamp', $entity->get('stamp'))->count() > 0) {
         Billrun_Factory::log()->log("Billrun_Receiver::logDB - DUPLICATE! trying to insert duplicate log file " . $log_data['file_name'] . " with stamp of : {$entity->get('stamp')}", Zend_Log::NOTICE);
         return FALSE;
     }
     return $entity->save($log, true);
 }
 public function save(Mongodloid_Entity $entity, $save = false, $w = null)
 {
     $data = $entity->getRawData();
     if (is_null($w)) {
         $w = $this->w;
     }
     $result = $this->_collection->save($data, array('save' => $save, 'w' => $w));
     if (!$result) {
         return false;
     }
     $entity->setRawData($data);
     return true;
 }
Exemple #5
0
 protected function addToQueue($queue_data)
 {
     $queue = Billrun_Factory::db()->queueCollection();
     foreach ($queue_data as $row) {
         try {
             $entity = new Mongodloid_Entity($row);
             $entity->save($queue);
         } catch (Exception $e) {
             Billrun_Factory::log()->log("Processor store " . basename($this->filePath) . " to queue failed on stamp : " . $row['stamp'] . " with the next message: " . $e->getCode() . ": " . $e->getMessage(), Zend_Log::NOTICE);
             continue;
         }
     }
 }
Exemple #6
0
 public function valid()
 {
     return !$this->entity->isEmpty();
 }
Exemple #7
0
 /**
  * update the billing line with stamp to avoid another aggregation
  *
  * @param int $sid the subscriber id to update
  * @param Mongodloid_Entity $line the billing line to update
  *
  * @return boolean true on success else false
  */
 protected function updateBillingLine($subscriber, $line)
 {
     if (isset($subscriber->id)) {
         $sid = $subscriber->id;
     } else {
         // todo: alert to log
         return false;
     }
     $current = $line->getRawData();
     $added_values = array('sid' => $sid, 'billrun' => $this->getStamp());
     if (isset($subscriber->aid)) {
         $added_values['aid'] = $subscriber->aid;
     }
     $newData = array_merge($current, $added_values);
     $line->setRawData($newData);
     return true;
 }
 /**
  * Save a modified line to the lines collection.
  * @param Mongodloid_Entity $line the line to write
  * @param mixed $dataKey the line key in the calculator's data container
  */
 public function writeLine($line, $dataKey)
 {
     Billrun_Factory::dispatcher()->trigger('beforeCalculatorWriteLine', array('data' => $line));
     $line->save(Billrun_Factory::db()->linesCollection());
     Billrun_Factory::dispatcher()->trigger('afterCalculatorWriteLine', array('data' => $line));
     if (!isset($line['usagev']) || $line['usagev'] === 0) {
         $this->removeLineFromQueue($line);
         unset($this->data[$dataKey]);
     }
 }
Exemple #9
0
 public function update($params)
 {
     //		if (method_exists($this, $coll . 'BeforeDataSave')) {
     //			call_user_func_array(array($this, $coll . 'BeforeDataSave'), array($collection, &$newEntity));
     //		}
     if (isset($params['_id'])) {
         $entity = $this->collection->findOne($params['_id']);
         $protected_keys = $this->getProtectedKeys($entity, "update");
         $hidden_keys = $this->getHiddenKeys($entity, "update");
         $raw_data = $entity->getRawData();
         $new_data = array();
         foreach ($protected_keys as $value) {
             if (isset($raw_data[$value])) {
                 $new_data[$value] = $raw_data[$value];
             }
         }
         foreach ($hidden_keys as $value) {
             $new_data[$value] = $raw_data[$value];
         }
         foreach ($params as $key => $value) {
             $new_data[$key] = $value;
         }
         $entity->setRawData($new_data);
     } else {
         $entity = new Mongodloid_Entity($params);
     }
     $entity->save($this->collection);
     //		if (method_exists($this, $coll . 'AfterDataSave')) {
     //			call_user_func_array(array($this, $coll . 'AfterDataSave'), array($collection, &$newEntity));
     //		}
     return $entity;
 }
Exemple #10
0
 protected function importRates()
 {
     $updated_keys = array();
     $missing_categories = array();
     $old_or_not_exist = array();
     $rates_coll = Billrun_Factory::db()->ratesCollection();
     $future_keys = $this->model->getFutureRateKeys(array_unique(array_keys($this->rules)));
     foreach ($future_keys as $key) {
         unset($this->rules[$key]);
     }
     if ($this->rules) {
         $active_rates = $this->model->getActiveRates(array_unique(array_keys($this->rules)));
         foreach ($active_rates as $old_rate) {
             $new_raw_data = $old_raw_data = $old_rate->getRawData();
             $rate_key = $old_raw_data['key'];
             $new_from_date = new Zend_Date($this->rules[$rate_key]['from'], 'yyyy-MM-dd HH:mm:ss');
             $old_to_date = clone $new_from_date;
             $old_raw_data['to'] = new MongoDate($old_to_date->subSecond(1)->getTimestamp());
             $old_rate->setRawData($old_raw_data);
             unset($new_raw_data['_id']);
             $new_raw_data['from'] = new MongoDate($new_from_date->getTimestamp());
             if ($this->remove_non_existing_usage_types) {
                 $new_usage_types = array_unique(array_keys($this->rules[$rate_key]['usage_type_rates']));
                 $old_usage_types = array_unique(array_keys($old_rate['rates']));
                 $usage_types_to_remove = array_diff($old_usage_types, $new_usage_types);
                 foreach ($usage_types_to_remove as $usage_types) {
                     unset($new_raw_data['rates'][$usage_types]);
                 }
             }
             $new_prefix = explode(',', $this->rules[$rate_key]['prefix']);
             if (!$this->remove_non_existing_prefix) {
                 $additional_prefix = array_diff($new_prefix, $new_raw_data['params']['prefix']);
                 $combined_prefix = array_merge($new_raw_data['params']['prefix'], $additional_prefix);
                 $new_raw_data['params']['prefix'] = $combined_prefix;
             } else {
                 $new_raw_data['params']['prefix'] = $new_prefix;
             }
             foreach ($this->rules[$rate_key]['usage_type_rates'] as $usage_type => $usage_type_rate) {
                 if (!isset($new_raw_data['rates'][$usage_type]) && (!isset($usage_type_rate['category']) || !$usage_type_rate['category'])) {
                     $missing_categories[] = $rate_key;
                     continue 2;
                 }
                 $new_raw_data['rates'][$usage_type]['unit'] = $this->model->getUnit($usage_type);
                 $new_raw_data['rates'][$usage_type]['rate'] = $this->model->getRateArrayByRules($usage_type_rate['rules']);
                 if ($usage_type_rate['category']) {
                     $new_raw_data['rates'][$usage_type]['category'] = $usage_type_rate['category'];
                 }
                 $new_raw_data['rates'][$usage_type]['access'] = floatval($usage_type_rate['access']);
             }
             $updated_keys[] = $rate_key;
             $new_rate = new Mongodloid_Entity($new_raw_data);
             $old_rate->save($rates_coll);
             $new_rate->save($rates_coll);
         }
     }
     if (count($updated_keys) + count($future_keys) + count($missing_categories) != count($this->rules)) {
         $all_keys = array_unique(array_keys($this->rules));
         $old_or_not_exist = array_diff($all_keys, $updated_keys, $missing_categories);
     }
     return array('updated' => $updated_keys, 'future' => $future_keys, 'missing_category' => $missing_categories, 'old' => $old_or_not_exist);
 }
Exemple #11
0
 /**
  * Add plan reference to line
  * @param Mongodloid_Entity $row
  * @param string $plan
  */
 protected function addPlanRef($row, $plan)
 {
     $planObj = Billrun_Factory::plan(array('name' => $plan, 'time' => $row['urt']->sec, 'disableCache' => true));
     if (!$planObj->get('_id')) {
         Billrun_Factory::log("Couldn't get plan for CDR line : {$row['stamp']} with plan {$plan}", Zend_Log::ALERT);
         return;
     }
     $row['plan_ref'] = $planObj->createRef();
     return $row->get('plan_ref', true);
 }