<commit_before><?php

class nrtrdePlugin extends Billrun_Plugin_BillrunPluginFraud {

	/**
	 * plugin name
	 *
	 * @var string
	 */
	protected $name = 'nrtrde';

	const time_format = 'YmdHis';

	public function beforeFTPReceive($ftp) {
		return true;
	}

	/**
	 * method to extend the behaviour after file received with FTP receiver
	 * the method will extract the NRTRDE zip file to the same location
	 * 
	 * @param type $local_path
	 * @param Zend_Ftp_File $file ftp object
	 * @param Billrun_Receiver_Ftp $ftp ftp receiver
	 */
	public function afterFTPFileReceived($local_path, $file, $ftp) {
		//Create filter object
//		$filter = new Zend_Filter_Decompress(
//			array(
//				'adapter' => 'Zend_Filter_Compress_Zip', //Or 'Tar', or 'Gz'
//				'options' => array(
//					'target' => dirname($local_path),
//				)
//			));
//
//		$filter->filter($local_path);
//		
		return true;
	}

	protected function decompress($local_path) {
		//Create filter object
		$filter = new Zend_Filter_Decompress(
			array(
			'adapter' => 'Zend_Filter_Compress_Zip', //Or 'Zend_Filter_Compress_Tar', or 'Zend_Filter_Compress_Gz'
			'options' => array(
				'target' => dirname($local_path),
			)
		));

		$filter->filter($local_path);

		return true;
	}

	public function afterFTPReceived($ftp, $files) {
		return true;
	}

	/**
	 *  method to extend and add data to the log of the receiver
	 * 
	 * @param array $log_data the data to log
	 * @param Billrun_Receiver $receiver Billrun receiver object
	 * 
	 * @return true success
	 */
	public function beforeLogReceiveFile($log_data, $receiver) {
//		$log_data['source'] = 'nrtrde';
//		
//		return TRUE;
	}

	public function afterDataParsing(&$row, $parser) {
		if ($parser->getType() == 'nrtrde') {
			// make the duration rounded by minute
			if (isset($row['callEventDuration'])) {
				$callEventDuration = $row['callEventDuration'];
				$row['callEventDurationRound'] = ceil($callEventDuration / 60) * 60;
			}

			// add record opening time UTC aligned
			if (isset($row['callEventStartTimeStamp'])) {
				$row['unified_record_time'] = new MongoDate(Billrun_Util::dateTimeConvertShortToIso($row['callEventStartTimeStamp'], $row['utcTimeOffset']));
			}
		}
	}

	/**
	 * move zip files to backup path after the processing was done
	 * @param Billrun_Processor $processor the proce
	 * @param string $file_path the path of the current processing file.
	 */
	public function afterProcessorBackup($processor, &$file_path) {
		if ($processor->getType() != $this->getName()) {
			return;
		}
		$path = Billrun_Factory::config()->getConfigValue($this->getName() . '.processor.zip_move_path', false, 'string');
		if (!$path)
			return;

		if ($processor->retrievedHostname) {
			$path = $path . DIRECTORY_SEPARATOR . $processor->retrievedHostname;
		}

		$path .= DIRECTORY_SEPARATOR . date("Ym");

		if (!file_exists($path)) {
			Billrun_Factory::log()->log("Creating Directory : $path", Zend_Log::DEBUG);
			mkdir($path, 0777, true);
		}

		$srcPath = $file_path . ".zip";
		if (file_exists($srcPath)) {
			Billrun_Factory::log()->log("Saving zip file to : $path", Zend_Log::DEBUG);
			if (!rename($srcPath, $path . DIRECTORY_SEPARATOR . basename($srcPath))) {
				Billrun_Factory::log()->log(" Failed when trying to save file : " . basename($srcPath) . " to third party path : $path", Zend_Log::ERR);
			}
		}
	}

	/**
	 * method to unzip the processing file of NRTRDE (received as zip archive)
	 * 
	 * @param string $file_path the path of the file
	 * @param Billrun_Processor $processor instance of the processor who dispatch this event
	 * 
	 * @return boolean
	 */
	public function processorBeforeFileLoad(&$file_path, $processor) {
		if ($processor instanceof Billrun_Processor_Nrtrde && file_exists($file_path)) {
			$this->decompress($file_path);
			$file_path = str_replace('.zip', '', $file_path);

			return true;
		}
		return false;
	}

	public function beforeDataParsing($line, $processor) {
		
	}

	protected function get_last_charge_time($return_timestamp = false) {
		$dayofmonth = Billrun_Factory::config()->getConfigValue('billrun.charging_day', 25, 'int');
		$format = "Ym" . $dayofmonth . "000000";
		if (date("d") >= $dayofmonth) {
			$time = date($format);
		} else {
			$time = date($format, strtotime('-1 month'));
		}
		if ($return_timestamp) {
			return strtotime($time);
		}
		return $time;
	}

	/**
	 * method to collect data which need to be handle by event
	 */
	public function handlerCollect() {

		$lines = Billrun_Factory::db()->linesCollection();
		$charge_time = $this->get_last_charge_time();

		// TODO: take it to config ? how to handle variables ?
		$base_match = array(
			'$match' => array(
				'source' => 'nrtrde',
			)
		);
		$where = array(
			'$match' => array(
				'record_type' => 'MOC',
				'connectedNumber' => array('$regex' => '^972'),
				'unified_record_time' => array('$gte' => $charge_time),
				'event_stamp' => array('$exists' => false),
				'deposit_stamp' => array('$exists' => false),
				'callEventDurationRound' => array('$gt' => 0), // not sms
			),<commit_msg>fix issue with the new unified_record_time<commit_after><?php

class nrtrdePlugin extends Billrun_Plugin_BillrunPluginFraud {

	/**
	 * plugin name
	 *
	 * @var string
	 */
	protected $name = 'nrtrde';

	const time_format = 'YmdHis';

	public function beforeFTPReceive($ftp) {
		return true;
	}

	/**
	 * method to extend the behaviour after file received with FTP receiver
	 * the method will extract the NRTRDE zip file to the same location
	 * 
	 * @param type $local_path
	 * @param Zend_Ftp_File $file ftp object
	 * @param Billrun_Receiver_Ftp $ftp ftp receiver
	 */
	public function afterFTPFileReceived($local_path, $file, $ftp) {
		//Create filter object
//		$filter = new Zend_Filter_Decompress(
//			array(
//				'adapter' => 'Zend_Filter_Compress_Zip', //Or 'Tar', or 'Gz'
//				'options' => array(
//					'target' => dirname($local_path),
//				)
//			));
//
//		$filter->filter($local_path);
//		
		return true;
	}

	protected function decompress($local_path) {
		//Create filter object
		$filter = new Zend_Filter_Decompress(
			array(
			'adapter' => 'Zend_Filter_Compress_Zip', //Or 'Zend_Filter_Compress_Tar', or 'Zend_Filter_Compress_Gz'
			'options' => array(
				'target' => dirname($local_path),
			)
		));

		$filter->filter($local_path);

		return true;
	}

	public function afterFTPReceived($ftp, $files) {
		return true;
	}

	/**
	 *  method to extend and add data to the log of the receiver
	 * 
	 * @param array $log_data the data to log
	 * @param Billrun_Receiver $receiver Billrun receiver object
	 * 
	 * @return true success
	 */
	public function beforeLogReceiveFile($log_data, $receiver) {
//		$log_data['source'] = 'nrtrde';
//		
//		return TRUE;
	}

	public function afterDataParsing(&$row, $parser) {
		if ($parser->getType() == 'nrtrde') {
			// make the duration rounded by minute
			if (isset($row['callEventDuration'])) {
				$callEventDuration = $row['callEventDuration'];
				$row['callEventDurationRound'] = ceil($callEventDuration / 60) * 60;
			}

			// add record opening time UTC aligned
			if (isset($row['callEventStartTimeStamp'])) {
				$row['unified_record_time'] = new MongoDate(Billrun_Util::dateTimeConvertShortToIso($row['callEventStartTimeStamp'], $row['utcTimeOffset']));
			}
		}
	}

	/**
	 * move zip files to backup path after the processing was done
	 * @param Billrun_Processor $processor the proce
	 * @param string $file_path the path of the current processing file.
	 */
	public function afterProcessorBackup($processor, &$file_path) {
		if ($processor->getType() != $this->getName()) {
			return;
		}
		$path = Billrun_Factory::config()->getConfigValue($this->getName() . '.processor.zip_move_path', false, 'string');
		if (!$path)
			return;

		if ($processor->retrievedHostname) {
			$path = $path . DIRECTORY_SEPARATOR . $processor->retrievedHostname;
		}

		$path .= DIRECTORY_SEPARATOR . date("Ym");

		if (!file_exists($path)) {
			Billrun_Factory::log()->log("Creating Directory : $path", Zend_Log::DEBUG);
			mkdir($path, 0777, true);
		}

		$srcPath = $file_path . ".zip";
		if (file_exists($srcPath)) {
			Billrun_Factory::log()->log("Saving zip file to : $path", Zend_Log::DEBUG);
			if (!rename($srcPath, $path . DIRECTORY_SEPARATOR . basename($srcPath))) {
				Billrun_Factory::log()->log(" Failed when trying to save file : " . basename($srcPath) . " to third party path : $path", Zend_Log::ERR);
			}
		}
	}

	/**
	 * method to unzip the processing file of NRTRDE (received as zip archive)
	 * 
	 * @param string $file_path the path of the file
	 * @param Billrun_Processor $processor instance of the processor who dispatch this event
	 * 
	 * @return boolean
	 */
	public function processorBeforeFileLoad(&$file_path, $processor) {
		if ($processor instanceof Billrun_Processor_Nrtrde && file_exists($file_path)) {
			$this->decompress($file_path);
			$file_path = str_replace('.zip', '', $file_path);

			return true;
		}
		return false;
	}

	public function beforeDataParsing($line, $processor) {
		
	}

	/**
	 * method to receive the last charge datetime
	 * 
	 * @param boolean $return_timestamp define what type of return value
	 * 
	 * @return mixed timestamp or string in base date format
	 * 
	 * @todo refactoring to external util cause we probably need it in more places
	 */
	protected function get_last_charge_time($return_timestamp = false) {
		$dayofmonth = Billrun_Factory::config()->getConfigValue('billrun.charging_day', 25, 'int');
		$format = "Ym" . $dayofmonth . "000000";
		if (date("d") >= $dayofmonth) {
			$time = date($format);
		} else {
			$time = date($format, strtotime('-1 month'));
		}
		if ($return_timestamp) {
			return strtotime($time);
		}
		return $time;
	}

	/**
	 * method to collect data which need to be handle by event
	 */
	public function handlerCollect() {

		$lines = Billrun_Factory::db()->linesCollection();
		$charge_time = $this->get_last_charge_time(true); // true means return timestamp

		// TODO: take it to config ? how to handle variables ?
		$base_match = array(
			'$match' => array(
				'source' => 'nrtrde',
			)
		);
		$where = array(
			'$match' => array(
				'record_type' => 'MOC',
				'connectedNumber' => array('$regex' => '^972'),
				'unified_record_time' => array('$gte' => new MongoDate($charge_time)),
				'event_stamp' => array('$exists' => false),
				'deposit_stamp' => array('$exists' => false),
				'callEventDurationRound' => array('$gt' => 0), // not sms
			),
{'fraction_non_alphanumeric': 0.15047201296843712, 'fraction_numerical': 0.004004958520072471, 'mean_word_length': 3.8849557522123894, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2737118', 'n_tokens_mistral': 3907, 'n_tokens_neox': 3397, 'n_words': 862}