<?php
$nextTime = time() + 50;
function cronJobReport($timeCron = '') {
	global $nextTime;
	include("config.php");
	
	
	$time = mktime(date("H"),date("i"),date("s"),date("n"),date("j"),date("Y")); 
	if(empty($timeCron)) {
	$querySendReport="SELECT `id` FROM `reports_one_msg` WHERE `case`!='1' AND (ISNULL(`group_time`) || `group_time`='')  ORDER BY `id` ASC LIMIT 0, 50";
	$resultSendReport = mysqli_query($conn, $querySendReport);
	$numSendReport=mysqli_num_rows($resultSendReport);
	if($resultSendReport && $numSendReport)
		while($rowSendReport = mysqli_fetch_assoc($resultSendReport))
			mysqli_query($conn, "UPDATE `reports_one_msg` SET `group_time`='$time' WHERE `id`='$rowSendReport[id]'");
	$timeCron = $time;	
	}
	$querySelectReport = "SELECT * FROM `reports_one_msg` WHERE `case`!='1' AND `group_time`='$timeCron' ORDER BY `id` ASC LIMIT 0, 50";
	$resultSelectReport = mysqli_query($conn, $querySelectReport);
	$numSelectReport = mysqli_num_rows($resultSelectReport);
	$msgIdsArr = array();
	if($resultSelectReport && !empty($numSelectReport))
	while($rowSelectReport = mysqli_fetch_assoc($resultSelectReport)){
		$querySelect = "SELECT * FROM `history` WHERE `msgIdMobily`='".$rowSelectReport['msgIdMobily']."'";
		$resultSelect = mysqli_query($conn, $querySelect);
		$numSelect = mysqli_num_rows($resultSelect);
		$notDeliveredExplode = $deliveredExplode = $acceptedExplode = array();
		if(!empty($numSelect)){
			$rowSelect = mysqli_fetch_assoc($resultSelect);
			$deliveredExplode = explode(',',$rowSelect['delivered']);
			$notDeliveredExplode = explode(',',$rowSelect['not_delivered']);
			$acceptedExplode = explode(',',$rowSelect['accepted']);
		}
		$deliveredExplode = implode(',',$deliveredExplode);
		$notDeliveredExplode = implode(',',$notDeliveredExplode);
		$acceptedExplode = implode(',',$acceptedExplode);
		
		if($rowSelectReport['status']=="D")
			$deliveredExplode = $deliveredExplode.",".$rowSelectReport['number'];
		if($rowSelectReport['status']=="S")
			$acceptedExplode = $acceptedExplode.",".$rowSelectReport['number'];
		if($rowSelectReport['status']=="N")
			$notDeliveredExplode = $notDeliveredExplode.",".$rowSelectReport['number'];
			
		$deliveredExplodeTow = array_filter(explode(',',$deliveredExplode));
		$acceptedExplodeTow = array_filter(explode(',',$acceptedExplode));
		$notDeliveredExplodeTow = array_filter(explode(',',$notDeliveredExplode)); 
		
		$queryUpateHistory = "UPDATE `history` SET `delivered`='".$deliveredExplode."',`not_delivered`='".$notDeliveredExplode."',`accepted`='".$acceptedExplode."',`count_delivered`='".count($deliveredExplodeTow)."',`count_not_delivered`='".count($notDeliveredExplodeTow)."',`count_accepted`='".count($acceptedExplodeTow)."',`send_email`='0',`report_date`='".time()."' WHERE `msgIdMobily`='".$rowSelectReport['msgIdMobily']."'";
		$resultUpateHistory = mysqli_query($conn, $queryUpateHistory);
		if($resultUpateHistory)
			mysqli_query($conn, "UPDATE `reports_one_msg` SET `case`='1',`group_time`='".time()."' WHERE id='$rowSelectReport[id]'");
	}
	mysqli_close($conn);
	if(time()<$nextTime)
	{
		ob_flush();
		sleep(5);
		if(empty($numSelectReport))
			cronJobReport();
		else
			cronJobReport($time);
	}
	return 0;
}
cronJobReport();

?>