<?php
    include'connection.php';
		
	date_default_timezone_set("asia/kolkata");
    $date=date("Y-m-d");
    $time=date("h:i:s A");
    
	$dateCronJob=date_create($date);
	date_sub($dateCronJob,date_interval_create_from_date_string("1 days"));
    $resultdateCB=date_format($dateCronJob,"Y-m-d");
	
    $quGetCronJob="select * from Cron_Job where Title='Audit'";
    $reGetCronJob=mysqli_query($con,$quGetCronJob);
    $dataGetCronJob=mysqli_fetch_array($reGetCronJob);
    $CronJobDate=$dataGetCronJob['Date'];
    $CronJobTime=$dataGetCronJob['Time'];
    
    if($CronJobDate<$date)
    {
        $quGetUsers="select * from retailer where ApiActive='1'";
        $reGetUsers=mysqli_query($con,$quGetUsers);
        while($dataGetUsers=mysqli_fetch_array($reGetUsers))
        {   
		    $TotalCredit=0;
		    $TotalDebit=0;
            $RetalerId=$dataGetUsers['Userid'];
			
		    $quOpBal="select * from wallet where Userid='$RetalerId' and Date='$CronJobDate' order by Id asc";
			$reOpBal=mysqli_query($con,$quOpBal);
			$rowOpBal=mysqli_fetch_array($reOpBal);
			$OpeningBal=$rowOpBal['Openingbalance'];
			
			$quClBal="select * from wallet where Userid='$RetalerId' and Date='$resultdateCB' order by Id desc";
			$reClBal=mysqli_query($con,$quClBal);
			$rowClBal=mysqli_fetch_array($reClBal);
			$ClosingBal=$rowClBal['Balance'];
			
			$getrechhistq="select * from wallet where Userid='$RetalerId' and Date>='$CronJobDate' and Date<='$resultdateCB' order by Id desc";
			$resultgetrechhid=mysqli_query($con,$getrechhistq);
			while($row=mysqli_fetch_array($resultgetrechhid))
			{   
				$TotalCredit=$TotalCredit+$row['Credit'];
				$TotalDebit=$TotalDebit+$row['Debit'];
			}
			
		    $TCOld=$TotalCredit+$OpeningBal;
		    $TDOld=$TotalDebit+$ClosingBal;
		    $TD = number_format($TDOld, 2, '.', '');
		    $TC = number_format($TCOld, 2, '.', '');
		    $Diff = $TD-$TC;
		    $query1="select * from wallet where Userid='$RetalerId' order by Id desc";
        	$result1=mysqli_query($con,$query1);
        	$data1=mysqli_fetch_array($result1);
	        $oldbal=$data1['Balance'];
	        
	        $newbal=$oldbal-$Diff;
	        if($CronJobDate<$resultdate)
	        {   
	            $msg="Reverse_Audit_Differances_for_".$CronJobDate."_To_".$resultdateCB;
	        }
	        else
	        {   
	            $msg="Reverse_Audit_Differances_for_".$resultdateCB;
	        }
	        
		    if($Diff==0)
		    {
		        
		    }
		    else
		    {
	            $DiffNew = number_format($Diff, 2, '.', '');
            	$query2="insert into wallet(Userid,Openingbalance,Debit,Balance,Date,Time,Transactiontype)values('$RetalerId','$oldbal','$DiffNew','$newbal','$date','$time','$msg')";
	
	            if(mysqli_query($con,$query2))
	            {
	               $query9="update retailer set Balance='$newbal' where Userid='$RetalerId'";
	               mysqli_query($con,$query9);
	            }
		    }
        }
	    $updateCronJobTime="update Cron_Job set Date='$date', Time='$time' where Title='Audit'";
	    mysqli_query($con,$updateCronJobTime);
    }
?>