<?php

    include'connection.php';
    include'../Retailers/functions.php';
    include'time.php';
    
    $quTest="INSERT INTO Test (Date,Time,Testdata) VALUES ('$date','$time','Test Cron Job Everyday')";
    mysqli_query($con,$quTest);
    
    exit();
    $quAdminBal="SELECT * FROM admin WHERE Userid='1111'";
    $reAdminBal=mysqli_query($con,$quAdminBal);
    $dataAdminBal=mysqli_fetch_array($reAdminBal);
    $adminBal=$dataAdminBal['Balance'];
    
        $queryMd="SELECT * FROM masterdis";
	    $resultMd=mysqli_query($con,$queryMd);
	    $rowMd=mysqli_num_rows($resultMd);
	    $totalMdBal=0;
	    while($mdData=mysqli_fetch_array($resultMd))
	    {
		    $MdBal=$mdData['Balance'];
	        $totalMdBal=$totalMdBal+$MdBal;
	    }
	    
	    $querydis="SELECT * FROM distributer";
	    $resultdis=mysqli_query($con,$querydis);
	    $rowdis=mysqli_num_rows($resultdis);
	    $DiswalletBal=0;
	    while($disData=mysqli_fetch_array($resultdis))
	    {
	        $totalDisBal=$disData['Balance'];
		    $DiswalletBal=$DiswalletBal+$totalDisBal;
	    }
	    
	    $query1="SELECT * FROM retailer";
	    $result1=mysqli_query($con,$query1);
	    $row1=mysqli_num_rows($result1);
	    $RetWalletBal=0;
	    while($row11=mysqli_fetch_array($result1))
	    {
		    $retalrBal=$row11['Balance'];
	        $RetWalletBal=$RetWalletBal+$retalrBal;
	    }
	    
	    $query4="SELECT * FROM commwallet WHERE Active='0'";
	    $result4=mysqli_query($con,$query4);
	    $Commission=0;
	    while($row4=mysqli_fetch_array($result4))
	    {
		    $data4=$row4['Balance'];
	        $Commission=$Commission+$data4;
	    }
	    
	    $totalUserWalletBal=$totalMdBal+$DiswalletBal+$RetWalletBal+$Commission;
        
	    
	    $InsertQu="INSERT INTO Diff (Date,Time,AdminBal,MdBal,DisBal,RetBal,CommBal,TotUsrBal) 
	    VALUES ('$date','$time','$adminBal','$totalMdBal','$DiswalletBal','$RetWalletBal','$Commission','$totalUserWalletBal')";
	    mysqli_query($con,$InsertQu);
	
	    exit();
	    
	    
	    
	    
        
        
	    $TotalAirtelBal=getLB('Airtel');
        $ActualAirtelBal=$TotalAirtelBal*100/103;
    
	    $TotalIdeaBal=getLB('Idea'); 
        $ActualIdeaBal=$TotalIdeaBal*100/104;
        
        $TotalVodaBal=getLB('Vodafone');
        $ActualVodaBal=$TotalVodaBal*100/104;
        
        $TotalJioBal=getLB('Reliance Jio');
        $ActualJioBal=$TotalJioBal*100/105;
    
	    
	    $update3="UPDATE Diff SET AirtelBal='$ActualAirtelBal', IdeaBal='$ActualIdeaBal',VodaBal='$ActualVodaBal', JioBal='$ActualJioBal' WHERE Id='$mysqli_Insertd_Id'";
	    mysqli_query($con,$update3);
	    
	    
	    
	    $totalWalletBal=$AdminBal+$totalMdBal+$DiswalletBal+$RetWalletBal+$Commission;
	    
    
        $quMyApiTata=getLB('Tata');
        $ActualTataBal=$TotalTataBal*100/103;
    
    
    
    
        // Ozzy API Bal //
        $OzzyBal = getOzzyBal();
    
        // DIZI API Bal //
        $DiziBal = getDiziBal();
    
    
    
        $TotalApiBal=$ActualJioBal+$ActualVodaBal+$ActualAirtelBal+$OzzyBal+$DiziBal+$ActualIdeaBal+$ActualTataBal;
	    $Managebal=$totalWalletBal-$TotalApiBal;
	    $BenefitBal=2000000-$Managebal;
	    $RestBal=$Managebal-2000000;
	
	
	    $AllBankBal=0;
        $quBank="SELECT * FROM bank";
	    $reBank=mysqli_query($con,$quBank);
	    while($dataBank=mysqli_fetch_array($reBank))
	    {
	        $TotalBankBal=0;
		    $AllBank=$dataBank['Bankname'];
		    $BankId=$dataBank['Id'];
		            
		    $quGetBankBal="select * from Bank_Account_Txn where Bank_Name='$AllBank'";
		    $reGetBankBal=mysqli_query($con,$quGetBankBal);
		    while($dataGetBankBal=mysqli_fetch_array($reGetBankBal))
		    {
		        $Txn=$dataGetBankBal['Txn'];
		        $TotalBankBal=$TotalBankBal+$Txn;
		    }
		    $AllBankBal=$AllBankBal+$TotalBankBal;
	    }
            
        $TotalBizBal=$TotalApiBal+$RestBal+$AllBankBal;
        $BizDiff=$TotalBizBal-$totalUserWalletBal;
    
    
	    $update4="UPDATE Diff SET DiziApi='$DiziBal',OzzyApi='$OzzyBal',TotApiBal='$TotalApiBal',Diff='$BenefitBal',BizDiff='$BizDiff' WHERE Id='$mysqli_Insertd_Id'";
	    mysqli_query($con,$update4);
	    
	    
	}
	
	$quTest1="INSERT INTO Test (Date,Time,Testdata) VALUES ('$date','$time','Test Cron Job Everyday-1')";
    mysqli_query($con,$quTest1);
    
	
	
	
/*	
	echo "<br><p>Admin Bal</p>".$AdminBal."<br>";
	echo "<br><p>MD Bal</p>".$totalMdBal."<br>";
	echo "<br><p>Dis Bal</p>".$DiswalletBal."<br>";
	echo "<br><p>Ret Bal</p>".$RetWalletBal."<br>";
	echo "<br><p>Comm.</p>".$Commission."<br>";
	echo "<br><p>Diff.</p>".$BenefitBal."<br>";
	echo "<br><p>Biz Diff.</p>".floor($BizDiff)."<br>";
	echo "<br><p>Total Api Bal.</p>".$TotalApiBal."<br>";
	echo "<br><p>Total Biz Bal.</p>".$TotalBizBal."<br>";
	echo "<br><p>Total User Bal.</p>".$totalUserWalletBal."<br>";
	echo "<br><p>Total Bank Bal.</p>".$AllBankBal."<br>";
*/	
	
	
	
	
	
	
	
	
    
?>