/** * 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); }
/** * method to log the processing * * @todo refactoring this method */ protected function logDB() { if (!isset($this->data['trailer']) && !isset($this->data['header'])) { Billrun_Factory::log()->log("Billrun_Processor:logDB " . $this->filePath . " no header nor trailer to log", Zend_Log::ERR); return false; } $log = Billrun_Factory::db()->logCollection(); $header = array(); if (isset($this->data['header'])) { $header = $this->data['header']; } $trailer = array(); if (isset($this->data['trailer'])) { $trailer = $this->data['trailer']; } if (empty($header) && empty($trailer)) { Billrun_Factory::log()->log("Billrun_Processor::logDB - trailer and header are empty", Zend_Log::ERR); return FALSE; } $header['linesStats']['queue'] = count($this->queue_data); $header['linesStats']['good'] = count($this->data['data']) - $header['linesStats']['queue']; $current_stamp = $this->getStamp(); // mongo id in new version; else string if ($current_stamp instanceof Mongodloid_Entity || $current_stamp instanceof Mongodloid_Id) { $resource = $log->findOne($current_stamp); if (!empty($header)) { $resource->set('header', $header, true); } if (!empty($trailer)) { $resource->set('trailer', $trailer, true); } $resource->set('process_hostname', Billrun_Util::getHostName(), true); $resource->set('process_time', date(self::base_dateformat), true); return $resource->save($log); } else { // backward compatibility // old method of processing => receiver did not logged, so it's the first time the file logged into DB $entity = new Mongodloid_Entity($trailer); if ($log->query('stamp', $entity->get('stamp'))->count() > 0) { Billrun_Factory::log()->log("Billrun_Processor::logDB - DUPLICATE! trying to insert duplicate log file with stamp of : {$entity->get('stamp')}", Zend_Log::NOTICE); return FALSE; } return $entity->save($log); } }
/** * method to update the billrun by the billing line (row) * @param Mongodloid_Entity $billrun the billrun line * @param Mongodloid_Entity $line the billing line * * @return boolean true on success else false */ protected function updateBillrun($billrun, $line) { // @TODO trigger before update row $current = $billrun->getRawData(); $added_charge = $line->get('aprice'); if (!is_numeric($added_charge)) { //raise an error return false; } $type = $line->get('type'); $subscriberId = $line->get('sid'); if (!isset($current['subscribers'][$subscriberId])) { $current['subscribers'][$subscriberId] = array('cost' => array()); } if (!isset($current['cost'][$type])) { $current['cost'][$type] = $added_charge; $current['subscribers'][$subscriberId]['cost'][$type] = $added_charge; } else { $current['cost'][$type] += $added_charge; $subExist = isset($current['subscribers'][$subscriberId]['cost']) && isset($current['subscribers'][$subscriberId]['cost'][$type]); $current['subscribers'][$subscriberId]['cost'][$type] = ($subExist ? $current['subscribers'][$subscriberId]['cost'][$type] : 0) + $added_charge; } $billrun->setRawData($current); // @TODO trigger after update row // the return values will be used for revert return array('newCost' => $current['cost'], 'added' => $added_charge); }
/** * 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); }