<?php
    include'connection.php';
	include'../Retailers/functions.php';
    ob_start();

    //echo "Ajax Hello";
    
    if($_GET['billInputValue'])
    {
        $SellerTxnId=$_GET['SellerTxnId'];
        $billInputValue=$_GET['billInputValue'];
        
        $update="UPDATE SellerTxn SET Bill='$billInputValue' WHERE Id='$SellerTxnId'";
        if(mysqli_query($con,$update))
        {
            echo "UPDATED";
        }
    }
    
    if($_GET['SellerTxnId'])
    {
        $SellerTxnId=$_GET['SellerTxnId'];
        $itcInputValue=$_GET['itcInputValue'];
        $TotalTxn=$_GET['TotalTxn'];
        $RestTxn=$TotalTxn-$itcInputValue;
        if($RestTxn==0)
        {
            $SettleStatus="YES";
        }
        else
        {
            $SettleStatus="NO";
        }
        $update="UPDATE SellerTxn SET ITC=$itcInputValue, RestTxn='$RestTxn', Settle='$SettleStatus' WHERE Id='$SellerTxnId'";
        if(mysqli_query($con,$update))
        {
            echo "UPDATED";
        }
    }
    
    
    
    if($_GET['disId'])
    {   
        $inpid = $_GET['inpid'];
        $disId = $_GET['disId'];
        
        $dataUsr=getUser($disId);
        $usrSttaus=$dataUsr['Status'];
        if($usrSttaus=="ACTIVE")
        {
            $update="UPDATE distributer SET Status='DEACTIVE' WHERE Userid='$disId'";
            if(mysqli_query($con,$update))
            {
                echo "<i class='fa fa-ban' style='color:red;' onclick='unblockUsr(".$inpid.")'></i>";
            }
        }
        else
        {
            $update="UPDATE distributer SET Status='ACTIVE' WHERE Userid='$disId'";
            if(mysqli_query($con,$update))
            {
                echo "<i class='fa fa-check' style='color:lime;' onclick='blockUsr(".$inpid.")'></i>";
            }
        }
    }
    
    if($_GET['txnDeactiveId'])
    {
        $txnDeactiveId=$_GET['txnDeactiveId'];
        $update="UPDATE recharge SET ActiveMinus='1' WHERE Id='$txnDeactiveId'";
        if(mysqli_query($con,$update))
        {
            echo "Deactivate";
        }
    }
    
    if($_GET['contDeacId'])
    {
        $contDeacId=$_GET['contDeacId'];
        $update="UPDATE contactus SET Active='1' WHERE Id='$contDeacId'";
        if(mysqli_query($con,$update))
        {
            echo "Deactivate";
        }
    }
    
    if($_GET['deleteContId'])
    {
        $deleteContId=$_GET['deleteContId'];
        $update="DELETE from contactus WHERE Id='$deleteContId'";
        if(mysqli_query($con,$update))
        {
            echo "DELETED";
        }
    }
    
    if($_GET['txnRefundId'])
    {
        $txnRefundId=$_GET['txnRefundId'];
        $rechId=$txnRefundId;
        
        $query="SELECT * FROM recharge WHERE Id=$txnRefundId";
	    $result=mysqli_query($con,$query);
	    $data=mysqli_fetch_array($result);
	
	    $uid=$data['Userid'];
	    $debitamount=$data['Debitamount'];
	    $ewalletid=$data['Ewalletid'];
	    $oper=$data['Companyname'];
	    $mobile=$data['Mobile'];
	    $amount=$data['Amount'];
	    $Partnerid=$data['Partnerid'];
	    $Status=$data['Status'];
	    
	    if($Status=="FAILED" || $Status=="SUCCESS")
	    {
	        echo "FAILED";
	    }
	    else
	    {
	        if(10==strlen($uid))
    	    {
	            $wallet="MLM_Wallet";
	        }
    	    else
	        {
	            $wallet="wallet";
	        }
	    
	        $query2="SELECT * FROM retailer WHERE Userid='$uid'";
	    	$result2=mysqli_query($con,$query2);
		    $databal=mysqli_fetch_array($result2);
    		$CallBackUrl=$databal['CallBackUrl'];
	    	$ApiActive=$databal['ApiActive'];
	    
	        $UsrOpBal=getBal($uid);
	        $UsrClBal=$UsrOpBal+$debitamount;
	        
	        $APBU="1111";
	        
	        $query3="INSERT INTO $wallet(Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Date,Time,APBU)
    	    values('$uid',$UsrOpBal,'$debitamount','$UsrClBal','Recharge_Refund','ADMIN','$rechId','$date','$time','$APBU')";
	        if(mysqli_query($con,$query3))
	        {   
		        $ewalletid=mysqli_insert_id($con);
		    
	    	    $query12="UPDATE retailer SET Balance='$UsrClBal' WHERE Userid='$uid'";
		        mysqli_query($con,$query12);
		        
		        $msg="Failed !";
		        
		        $query4="UPDATE recharge SET Creditewalletid='$ewalletid', Operatorid='$msg', AdminOpId='F', ReHit='0', Balance='$UsrClBal', Creditamount='$debitamount', Status='FAILED' WHERE Id='$rechId' ";
		        mysqli_query($con, $query4);
		
		        $query5="DELETE FROM commwallet WHERE Rechargeid='$rechId'";
		        mysqli_query($con,$query5);
		    
		        if($ApiActive==1)
	            {
	                $status="FAILED";
	            
            		$url = "$CallBackUrl?partnertid=$Partnerid&status=$status&orderid=$rechId&opid=$msg&errmsg=$msg";
                    $curl = curl_init();
                
                    curl_setopt_array($curl, array(
                    CURLOPT_URL => $url,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_CONNECTTIMEOUT => 60,
                    CURLOPT_TIMEOUT => 60,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_HTTPHEADER => array(
                    "cache-control: no-cache"
                        ),
                    ));

                    $response = curl_exec($curl);
                    $hhtpCode =curl_getinfo($curl, CURLINFO_HTTP_CODE);
                    $err = curl_error($curl);

                    curl_close($curl);
                
                    if ($err)
                    {
                            //$do=mysql_query("INSERT INTO kwk_cbhis (oid,uid,status_code,msg,url)VALUES(paste your values)");
                    }
                    else
                    {
                        //$do=mysql_query("INSERT INTO kwk_cbhis (oid,uid,status_code,msg,url)VALUES(paste your values)");
                    }
                    $insertCBH="INSERT INTO cbhis (oid,uid,status_code,msg,url)VALUES('$rechId','$uid','$hhtpCode','$response','$url')";
                    mysqli_query($con,$insertCBH);
	            }
	            echo "FAILED";
	        }
	
	    }
        
    }
    
    if($_GET['lapuValue'])
    {
        //echo "Ajax Hello";
        
        $inpVal1=$_GET['inpVal1'];
        $inpVal2=$_GET['inpVal2'];
        $lapuValue=$_GET['lapuValue'];
        $sl=0;
        $totalTxnAmt=0;
        $totalRO=0;
        $lapuCheck=substr($lapuValue,0,2);
        if($lapuCheck==68)
        {
           $lapuValue="0".$lapuValue; 
        }
        if($inpVal1<$inpVal2)
        {
            $quReach="SELECT * FROM recharge WHERE LapuNo='$lapuValue' AND Id>='$inpVal1' AND Id<='$inpVal2' AND Status='SUCCESS'";
        }
        else
        {
            $quReach="SELECT * FROM recharge WHERE LapuNo='$lapuValue' AND Id>='$inpVal2' AND Id<='$inpVal1' AND Status='SUCCESS'";
        }
        
        $reReach=mysqli_query($con,$quReach);
        while($dataReach=mysqli_fetch_array($reReach))
        {
            $sl=$sl+1;
            if($sl==1)
            {
                $opBal=$dataReach['ApiBal'];
            }
            else
            {
                $totalTxnAmt=$totalTxnAmt+$dataReach['Amount'];
                $totalRO=$totalRO+$dataReach['R_Offer'];
            }
            $clBal=$dataReach['ApiBal'];
        }
        
        $diff=floor($clBal+$totalTxnAmt-$opBal-$totalRO);
        echo "<table>
                <tr>
                    <td>Op Bal = ".$opBal."</td>
                    <td>Amount = ".$totalTxnAmt."</td>
                    <td>RO = ".$totalRO."</td>
                    <td>Cl.Bal = ".$clBal."</td>
                    <td>Diiff = ".$diff."</td>
                </tr>
            </table>";
    }
    
    
    
    
    
    
    
    
    
    
?>