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

    //echo "Ajax Hello";
    
    if($_GET['AjaxName']=="Ajax1")
    {   
        $setApiId     = $_GET['setApiId'];
        $myOpCode     = $_GET['myOpCode'];
        $apiName      = $_GET['apiName'];
        $Roffer       = $_GET['Roffer'];
        $stv          = $_GET['stv'];
        $circle       = $_GET['circle'];
        $AlwAmt       = $_GET['AlwAmt'];
        $BlkAmt       = $_GET['BlkAmt'];
        $inpGroup     = $_GET['inpGroup'];
        $inpReqRoffer = $_GET['inpReqRoffer'];
        
        $quApiOpCode="SELECT $apiName FROM operator WHERE Id='$myOpCode'";
        $reApiOpCode=mysqli_query($con,$quApiOpCode);
        $dataApiOpCode=mysqli_fetch_array($reApiOpCode);
        $ApiOpCode=$dataApiOpCode[$apiName];
        
        
        if(empty($AlwAmt))
        {
            $AlwAmt=0;
        }
        if(empty($BlkAmt))
        {
            $BlkAmt=0;
        }
        if(empty($inpGroup))
        {
            $inpGroup=0;
        }
        if(empty($inpReqRoffer))
        {
            $inpReqRoffer=0;
        }
        if(empty($ApiOpCode))
        {
            echo $message="Api Operator Is Empty !";
        }
        else
        {
            $update="UPDATE set_api SET UseApi='$apiName', ApiOpCode='$ApiOpCode', R_offer='$Roffer', STV='$stv', Circle='$circle', AlwAmt='$AlwAmt', BlkAmt='$BlkAmt', LapuGroup='$inpGroup', ReqRoffer='$inpReqRoffer'  WHERE Id='$setApiId'";
            if(mysqli_query($con,$update))
            {
                echo $message="Api Updated Successfully !";
            }
        }
    }
    
    if($_GET['AjaxName']=="Ajax2")
    {   
        $apiUsrId     = $_GET['apiUsrId'];
        $myOpCode     = $_GET['myOpCode'];
        $apiName      = $_GET['apiName'];
        $Roffer       = $_GET['Roffer'];
        $stv          = $_GET['stv'];
        $circle       = $_GET['circle'];
        $AlwAmt       = $_GET['AlwAmt'];
        $BlkAmt       = $_GET['BlkAmt'];
        $inpGroup     = $_GET['inpGroup'];
        $inpReqRoffer = $_GET['inpReqRoffer'];
        
        $quApiOpCode="SELECT $apiName FROM operator WHERE Id='$myOpCode'";
        $reApiOpCode=mysqli_query($con,$quApiOpCode);
        $dataApiOpCode=mysqli_fetch_array($reApiOpCode);
        $ApiOpCode=$dataApiOpCode[$apiName];
        
        if(empty($AlwAmt))
        {
            $AlwAmt=0;
        }
        if(empty($BlkAmt))
        {
            $BlkAmt=0;
        }
        if(empty($inpGroup))
        {
            $inpGroup=0;
        }
        if(empty($inpReqRoffer))
        {
            $inpReqRoffer=0;
        }
        
        if(empty($ApiOpCode))
        {
            echo $message="Api Operator Is Empty !";
        }
        else
        {
            $quSetApi    = "SELECT Id FROM set_api WHERE OpCode='$myOpCode' AND Userid='$apiUsrId'";
            $reSetApi    = mysqli_query($con,$quSetApi);
            $countSetApi = mysqli_num_rows($reSetApi);
            if($countSetApi<3)
            {
                $update="INSERT INTO set_api (Userid, OpCode, UseApi, ApiOpCode, R_offer, STV, Circle, AlwAmt, BlkAmt,  LapuGroup, ReqRoffer) 
                VALUES ('$apiUsrId', '$myOpCode', '$apiName', '$ApiOpCode', '$Roffer', '$stv', '$circle', '$AlwAmt', '$BlkAmt', '$inpGroup', '$inpReqRoffer')";
                if(mysqli_query($con,$update))
                {
                    echo $message="Api Added Successfully !";
                }
            }
            else
            {
                echo $message="Maximum Api Add 3 Only !";
            }
        }
    }
    
    if($_GET['AjaxName']=="Ajax3")
    {
        $myOpCode   =$_GET['myOpCode'];
        $apiOpCode  =$_GET['apiOpCode'];
        $apiColName =$_GET['apiColName'];
        
        $update="UPDATE operator SET $apiColName='$apiOpCode' WHERE Id='$myOpCode'";
        if(mysqli_query($con,$update))
        {
            echo "Api Operator Update Successfully !";
        }
    }
    
    if($_GET['AjaxName']=="Ajax4")
    {   
        $setApiId = $_GET['setApiId'];
        
        $delete="DELETE FROM set_api WHERE Id='$setApiId'";
        if(mysqli_query($con,$delete))
        {
            echo "Api Deleted Successfully !";
        }
        else
        {
            echo "Something Error !";
        }
    }
    
    if($_GET['AjaxName']=="Ajax5") //NEW
    {
        $opComm  = $_GET['opComm'];
        $opusr   = $_GET['opusr'];
        $opcode  = $_GET['opcode'];
        $dataUsr    = getUser($opusr);
        $usrType   = $dataUsr['Type'];
        $ParentId   = $dataUsr['Parent'];
        $G_ParentId = $dataUsr['G_Parent'];
        $ParentComm = getComm($ParentId,$opcode);
        $AdminComm  = getComm('AD1111',$opcode);
        $pass=0;
        if($usrType=="ADMIN")
        {   
            $quOpDeactive="UPDATE comm_slab SET Status='DEACTIVE' WHERE Comm>$opComm AND OpCode='$opcode' AND Userid!='$opusr'";
            mysqli_query($con,$quOpDeactive);
            $pass=1;
        }
        elseif($usrType=="API" || $usrType=="MASTER")
        {   
            if($opComm<=$AdminComm)
            {   
                if($usrType=="MASTER")
                {
                    $quGenology="SELECT Userid FROM user WHERE Parent='$opusr' OR G_Parent='$opusr'";
                    $reGenology=mysqli_query($con,$quGenology);
                    while($dataGenology=mysqli_fetch_array($reGenology))
                    {
                        $OpDecUsrId=$dataGenology['Userid'];
                        $quOpDeactive="UPDATE comm_slab SET Status='DEACTIVE' WHERE Comm>$opComm AND OpCode='$opcode' AND Userid='$OpDecUsrId'";
                        mysqli_query($con,$quOpDeactive);
                    }
                }
                $pass=1;
            }
        }
        elseif($usrType=="DISTRIBUTER")
        {   
            if($opComm<=$ParentComm && $ParentComm<=$AdminComm)
            {   
                $quGenology="SELECT Userid FROM user WHERE Parent='$opusr'";
                $reGenology=mysqli_query($con,$quGenology);
                while($dataGenology=mysqli_fetch_array($reGenology))
                {
                    $OpDecUsrId=$dataGenology['Userid'];
                    $quOpDeactive="UPDATE comm_slab SET Status='DEACTIVE' WHERE Comm>$opComm AND OpCode='$opcode' AND Userid='$OpDecUsrId'";
                    mysqli_query($con,$quOpDeactive);
                }
                $pass=1;
            }
        }
        elseif($usrType=="RETAILER")
        {
            $G_ParentComm=getComm($G_ParentId,$opcode);
            if($opComm<=$ParentComm && $ParentComm<=$G_ParentComm && $G_ParentComm<=$AdminComm)
            {   
                $pass=1;
            }
        }
        
        if($pass==1)
        {
            $update="UPDATE comm_slab SET Comm='$opComm' WHERE OpCode='$opcode' AND Userid='$opusr'";
            if(mysqli_query($con,$update))
            {
                $result= "Commission Updated Successfully !";
            }
            else
            {
                $result= "Something Error !";
            }
        }
        else
        {
           $result= "Commission Invalid !"; 
        }
        echo $result;
    }
    
    
    if($_GET['AjaxName']=="Ajax6")
    {   
        $inpid = $_GET['inpid'];
        $usrId = $_GET['opusr'];
        $opcode = $_GET['opcode'];
        
        $dataUsr=getUser($usrId);
        $usrType=$dataUsr['Type'];
        $Parent=$dataUsr['Parent'];
        $G_Parent=$dataUsr['G_Parent'];
        $ParentComm=getComm($Parent,$opcode);
        $G_ParentComm=getComm($G_Parent,$opcode);
        $adminComm=getComm('AD1111',$opcode);
        
        $quOp="SELECT Status FROM comm_slab WHERE Userid='$usrId' AND OpCode='$opcode'";
        $reOp=mysqli_query($con,$quOp);
        $dataOp=mysqli_fetch_array($reOp);
        $opStatus=$dataOp['Status'];
        $opComm=$dataOp['Comm'];
        
        $pass=0;
        if($usrType=="RETAILER")
        {
            if($opComm<=$ParentComm && $ParentComm<=$G_ParentComm && $G_ParentComm<=$adminComm)
            {
                $pass=1;
            }
            else
            {
                $errorMsg="Commsission Invalid !";
            }
        }
        elseif($usrType=="DISTRIBUTER")
        {
            if($opComm<=$ParentComm && $ParentComm<=$G_ParentComm)
            {
                $pass=1;
            }
            else
            {
                $errorMsg="Commsission Invalid !";
            }
        }
        elseif($usrType=="MASTER" || $usrType=="API")
        {
            if($opComm<=$adminComm)
            {
                $pass=1;
            }
            else
            {
                $errorMsg="Commsission Invalid !";
            }
        }
        
        if($pass==1)
        {
            if($opStatus=="ACTIVE")
            {
                $update="UPDATE comm_slab SET Status='DEACTIVE' WHERE Userid='$usrId' AND OpCode='$opcode'";
                if(mysqli_query($con,$update))
                {
                    echo "<i class='fa fa-ban' style='color:red;' onclick='updateOpStatus(".$inpid.")'></i>";
                }
            }
            else
            {
                $update="UPDATE comm_slab SET Status='ACTIVE' WHERE Userid='$usrId' AND OpCode='$opcode'";
                if(mysqli_query($con,$update))
                {
                    echo "<i class='fa fa-check' style='color:lime;' onclick='updateOpStatus(".$inpid.")'></i>";
                }
            }
        }
        else
        {
            echo $errorMsg;
        }
    }
    
    if($_GET['AjaxName']=="Ajax7")
    {   
        $inpid = $_GET['inpid'];
        $usrId = $_GET['usrId'];
        
        $dataUsr=getUser($usrId);
        $usrSttaus=$dataUsr['Status'];
        if($usrSttaus=="ACTIVE")
        {
            $update="UPDATE user SET Status='DEACTIVE' WHERE Userid='$usrId'";
            if(mysqli_query($con,$update))
            {
                echo "<i class='fa fa-ban' style='color:red;' onclick='updateUsrStatus(".$inpid.")'></i>";
            }
        }
        else
        {
            $update="UPDATE user SET Status='ACTIVE' WHERE Userid='$usrId'";
            if(mysqli_query($con,$update))
            {
                echo "<i class='fa fa-check' style='color:lime;' onclick='updateUsrStatus(".$inpid.")'></i>";
            }
        }
    }
    
    if($_GET['AjaxName']=="Ajax8")
    {   
        $sellerid = $_GET['sellerid'];
        $Progtype = $_GET['Progtype'];
        $quFirm   = "SELECT * FROM seller_firm WHERE SellerId='$sellerid'";
	    $reFirm   = mysqli_query($con,$quFirm);
	    if($Progtype=="GenAmt" || $Progtype=="RechReport")
	    {
	        echo "<option>ALL</option>";
	    }
	    else
	    {
	        echo "<option>--Select Seller Firm--</option>";
	    }
	
	    while($dataFirm=mysqli_fetch_array($reFirm))
	    {	
	        echo "<option value='".$dataFirm['Id']."'>".$dataFirm['FirmName']." - ".$dataFirm['GstNo']."</option>";
	    }
    }
    
    if($_GET['AjaxName']=="Ajax9")
    {   
        $sellerid   = $_GET['sellerId'];
        $quSeller   = "SELECT * FROM Seller WHERE Id='$sellerid'";
	    $reSeller   = mysqli_query($con,$quSeller);
	    $dataSeller = mysqli_fetch_array($reSeller);
	    if($dataSeller['Status']=="ACTIVE")
	    {
	        $update = "UPDATE Seller Set Status='DEACTIVE' WHERE Id='$sellerid'";
	        if(mysqli_query($con,$update))
	        {
	            echo "<p style='color:red;' ondblclick='updateSeller($sellerId)'>".$dataSeller['Name']."</p>";
	        }
	    }
	    else
	    {
	        $update = "UPDATE Seller Set Status='ACTIVE' WHERE Id='$sellerid'";
	        if(mysqli_query($con,$update))
	        {
	            echo "<p style='color:green;' ondblclick='updateSeller($sellerId)'>".$dataSeller['Name']."</p>";
	        }
	    }
    }
    
    if($_GET['AjaxName']=="Ajax10")
    {   
        $firmId     = $_GET['firmId'];
        $quSeller   = "SELECT * FROM seller_firm WHERE Id='$firmId'";
	    $reSeller   = mysqli_query($con,$quSeller);
	    $dataSeller = mysqli_fetch_array($reSeller);
	    if($dataSeller['Status']=="ACTIVE")
	    {
	        $update = "UPDATE seller_firm Set Status='DEACTIVE' WHERE Id='$firmId'";
	        if(mysqli_query($con,$update))
	        {
	            echo "<p style='color:red;' ondblclick='updateFirm($firmId)'>".$dataSeller['FirmName']."</p>";
	        }
	    }
	    else
	    {
	        $update = "UPDATE seller_firm Set Status='ACTIVE' WHERE Id='$firmId'";
	        if(mysqli_query($con,$update))
	        {
	            echo "<p style='color:green;' ondblclick='updateFirm($firmId)'>".$dataSeller['FirmName']."</p>";
	        }
	    }
    }
    
    if($_GET['AjaxName']=="Ajax11")
    {   
        $SellerId     = $_GET['SellerId'];
        $FirstOption  = $_GET['FirstOption'];
        $quFirm       = "SELECT * FROM seller_firm WHERE SellerId='$SellerId' AND Status='ACTIVE'";
	    $reFirm       = mysqli_query($con,$quFirm);
	    $countFirm    = mysqli_num_rows($reFirm);
	    if($countFirm==1)
	    {
	        $dataFirm=mysqli_fetch_array($reFirm);
	        echo "<option value='".$dataFirm['Id']."'>".$dataFirm['FirmName']." - ".$dataFirm['GstNo']."</option>";
	    }
	    elseif($countFirm>1)
	    {
	        echo "<option>".$FirstOption."</option>";
	
	        while($dataFirm=mysqli_fetch_array($reFirm))
	        {	
	            echo "<option value='".$dataFirm['Id']."'>".$dataFirm['FirmName']." - ".$dataFirm['GstNo']."</option>";
	        }
	    }
    }
    
    if($_GET['AjaxName']=="Ajax12")
    {
        $rechId = $_GET['orderId'];
        
        $query="SELECT recharge.Userid, recharge.OpId, recharge.Partnerid, recharge.Status, wallet.Txn FROM recharge INNER JOIN wallet ON wallet.TxnId = recharge.Id WHERE recharge.Id=$rechId AND wallet.TxnType='RECHARGE'";
        $result=mysqli_query($con,$query);
	    $data=mysqli_fetch_array($result);
	
	    $uid       = $data[0];
	    $OpId      = $data[1];
	    $Partnerid = $data[2];
	    $Status    = $data[3];
	    $DebitAmt  = abs($data[4]);
	    
	    if($Status=="FAILED" || $Status=="SUCCESS")
	    {
	        echo "FAILED";
	    }
	    else
	    {
	        
	        $UsrOpBal = getBal($uid);
	        $dataUser = getUser($uid);
	        $usrType  = $dataUser['Type'];
	        
	        $UsrClBal=$UsrOpBal+$DebitAmt;
	        
	        $query3 = "INSERT INTO wallet(Date,Time,Userid,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith)
    	    VALUES ('$date','$time','$uid','$UsrOpBal','$DebitAmt','$UsrClBal','$rechId','Recharge_Refund','ADMIN')";
	        if(mysqli_query($con,$query3))
	        {   
		        $ewalletid=mysqli_insert_id($con);
		    
	    	    $msg="Failed !";
		        
		        $query4="UPDATE recharge SET OpId='$msg', UsrBal='$UsrClBal', Status='FAILED' WHERE Id='$rechId' ";
		        mysqli_query($con, $query4);
		
		        $query5="DELETE FROM comm_wallet WHERE TxnId='$rechId'";
		        mysqli_query($con,$query5);
		    
		        if($usrType=="API")
	            {
	                $query2      = "SELECT * FROM api_user WHERE Userid='$uid'";
	    	        $result2     = mysqli_query($con,$query2);
		            $databal     = mysqli_fetch_array($result2);
    		        $CallBackUrl = $databal['CallBack'];
	    	        //$ApiActive   = $databal['ApiActive'];
	    
	                $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['AjaxName']=="Ajax13")
    {   
        $usrType = $_GET['usrType'];
        
        if($usrType=="API" || $usrType=="MASTER")
        {
            $quParent = "SELECT * FROM user WHERE Type='ADMIN'";
        }
        elseif($usrType=="DISTRIBUTER")
        {
            $quParent = "SELECT * FROM user WHERE Type='MASTER'";
        }
        elseif($usrType=="RETAILER")
        {
            $quParent = "SELECT * FROM user WHERE Type='DISTRIBUTER'";
        }
        
	    $reParent = mysqli_query($con,$quParent);
	    echo "<option>--Select Parent--</option>";
	
	    while($dataParent=mysqli_fetch_array($reParent))
	    {
	        
	        echo "<option value='".$dataParent['Userid']."'>".$dataParent['Name']." - ".$dataParent['Userid']."</option>";
	    }
    }
    
    if($_GET['AjaxName']=="Ajax14")
    {   
        $inpVal1   = $_GET['inpVal1'];
        $inpVal2   = $_GET['inpVal2'];
        
        $quLapu   = "SELECT Lapu FROM recharge WHERE Id='$inpVal1'";
        $reLapu   = mysqli_query($con,$quLapu);
        $dataLapu = mysqli_fetch_array($reLapu); 
        $lapu     = $dataLapu['Lapu'];
        
        $sl=0;
        $totalTxnAmt=0;
        $totalRO=0;
        
        if($inpVal1<$inpVal2)
        {
            $bigTxnId   = $inpVal2;
            $smallTxnId = $inpVal1;
        }
        else
        {
            $bigTxnId   = $inpVal1;
            $smallTxnId = $inpVal2;
        }
        $quReach="SELECT * FROM recharge WHERE Lapu='$lapu' AND Id>='$smallTxnId' AND Id<='$bigTxnId' AND Status='SUCCESS'";
        $reReach=mysqli_query($con,$quReach);
        while($dataReach=mysqli_fetch_array($reReach))
        {
            $sl=$sl+1;
            if($sl==1)
            {
                $LapuOpBal   = $dataReach['ApiBal'];
            }
            else
            {
                $totalTxnAmt = $totalTxnAmt+$dataReach['Amount'];
                $totalDebit  = $totalDebit+$dataReach['Debit'];
                $totalRO     = $totalRO+$dataReach['R_offer'];
                $LapuClBal   = $dataReach['ApiBal'];
            }
            if($sl==2)
            {
                $startTxnId = $dataReach['Id'];
            }
        }
        $opBal = $clBalRow1+$firstDebit;
        $diff=floor($LapuClBal+$totalTxnAmt-$LapuOpBal-$totalRO);
        echo "<table>
                <tr>
                    <td>".$startTxnId." to ".$bigTxnId."</td>
                    <td>Op Bal =  ".$LapuOpBal."</td>
                    <td>Amount =  ".$totalTxnAmt."</td>
                    <td>RO     =  ".$totalRO."</td>
                    <td>Cl.Bal =  ".$LapuClBal."</td>
                    <td>Api-Lapu Diff = ".$diff."</td>
                </tr>
            </table>";
    }
    
    if($_GET['AjaxName']=="Ajax15")
    {   
        $SellerId  = $_GET['sellerid'];
        $quFirm    = "SELECT Comm FROM Seller WHERE Id='$SellerId'";
	    $reFirm    = mysqli_query($con,$quFirm);
	    $countFirm = mysqli_num_rows($reFirm);
	    if($countFirm==1)
	    {
	        $dataFirm=mysqli_fetch_array($reFirm);
	        $SellerComm = $dataFirm[0];
	    }
	    else
	    {
	        $SellerComm="Null";
	    }
	    echo $SellerComm;
    }
    
    if($_GET['AjaxName']=="Ajax16")
    {   
        $fdate       = $_GET['fdate'];
        $ldate       = $_GET['ldate'];
        $SellerId    = $_GET['SellerId'];
        $FirmId      = $_GET['FirmId'];
        $GroupBy     = $_GET['GroupBy'];
        $sl          = 0;
        $newDate     = date_create($fdate); 
        $TimePeriod  = date_format($newDate,"F Y");
        $where2 = "";
        if($SellerId!="ALL")
        {
            $where2 = "AND s.Id=$SellerId";
            if($FirmId!="ALL")
            {
                $where2 = "AND s.Id=$SellerId AND f.Id=$FirmId";
            }
        }
        $group1 = "";
        $data   = "t.Id, t.Date, t.Time, s.Name, f.FirmName, t.Amount, t.TxnType, b.BankName, b.AccName, w.BankRefNo";
        $heading = "<tr>
	       	    	    <th>P.Id</th>
	       		        <th>Date</th>
	       		        <th>Time</th>
				        <th>Seller</th>
				        <th>Firm</th>
				        <th>Amount</th>
				        <th>TxnType</th>
				        <th>BankName</th>
				        <th>Acc Name</th>
				        <th>Ref No.</th>
	                </tr>";
        if($GroupBy=="FIRM")
        {
            $group1 = "GROUP BY t.FirmId";
            $data   = "s.Name, f.FirmName, f.Id, SUM(t.Amount)";
            $heading = "<tr>
			    <th>Seller</th>
				<th>Firm</th>
				<th>Old Bal</th>
				<th>Amount</th>
		        <th>Msg</th>
	        </tr>";
        }
        if($GroupBy=="SELLER")
        {
            $group1 = "GROUP BY t.SellerId";
            $data   = "s.Name, s.Id, SUM(t.Amount)";
            $heading = "<tr>
		        <th>Seller</th>
		        <th>Old Bal</th>
		        <th>Amount</th>
		        <th>Msg</th>
	        </tr>";
        }
        
        $IJstNs  = "INNER JOIN Seller AS s ON s.Id=t.SellerId";
        $IJstNf  = "INNER JOIN seller_firm AS f ON f.Id=t.FirmId";
        $IJstNb  = "INNER JOIN bank AS b ON b.Id=t.TxnWith";
        $IJstNbw = "INNER JOIN bank_wallet AS w ON w.Id=t.BankWalletId";
        
        $where1 = "t.Date>='$fdate' AND t.Date<='$ldate'";
        
        $orderBy1 = "ORDER BY s.Name";
        
        $qu    = "SELECT $data FROM seller_txn AS t $IJstNs $IJstNf $IJstNb $IJstNbw WHERE $where1 $where2 $group1 $orderBy1";
	    $re    = mysqli_query($con,$qu);
	    $count = mysqli_num_rows($re);
	    if($count>0)
	    {
	        while($data=mysqli_fetch_array($re))
	        {
	            $sl=$sl+1;
	            if($sl==1)
	            {
	                echo $heading;
	            }
	            if($GroupBy=="ALL")
	            {
    	            echo "<tr>
	                    <td>".$data[0]."</td>
	                    <td>".$data[1]."</td>
	                    <td>".$data[2]."</td>
    	                <td>".$data[3]."</td>
	                    <td>".$data[4]."</td>
    	                <td>".$data[5]."</td>
	                    <td>".$data[6]."</td>
	                    <td>".$data[7]."</td>
	                    <td>".$data[8]."</td>
	                    <td>".$data[9]."</td>
	                </tr>";
	                $totAmt = $totAmt+$data[5];
	            }
	            
	            if($GroupBy=="SELLER")
	            {
	                $seller_id = $data[1];
	                $quClBal   = "SELECT SUM(ClBal) FROM seller_itc WHERE SellerId='$seller_id'";
	                $reClBal   = mysqli_query($con,$quClBal);
	                $dataClBal = mysqli_fetch_array($reClBal);
	                $oldClBal  = $dataClBal[0];
	                
	                $Amt    = $data[2];
	                $TotAmt = $Amt+$oldClBal;
	                $msg    = "Please Send GST Invoice
Total Amount  = rs. ".$TotAmt.",
Time Period   = ".$TimePeriod.",
Our GST No.   = 09IATPM5538N1ZI,
Our Firm Name = MANSOORI TRADERS.";
    	            echo "<tr>
	                    <td>".$data[0]."</td>
	                    <td>".$oldClBal."</td>
	                    <td>".$Amt."</td>
	                    <td><textarea>".$msg."</textarea></td>
	                </tr>";
	                $totAmt = $totAmt+$data[1];
	            }
	            if($GroupBy=="FIRM")
	            {
	                $firm_id   = $data[2];
	                $quClBal   = "SELECT SUM(ClBal) FROM seller_itc WHERE FirmId='$firm_id'";
	                $reClBal   = mysqli_query($con,$quClBal);
	                $dataClBal = mysqli_fetch_array($reClBal);
	                $oldClBal  = $dataClBal[0];
	                
	                $Firm   = $data[1];
	                $Amt    = $data[3];
	                $TotAmt = $Amt+$oldClBal;
	                $msg    = "Please Send GST Invoice.
From Your Firm = $Firm,
Total Amount   = rs. $TotAmt,
Time Period    = $TimePeriod,
Our GST No.    = 09IATPM5538N1ZI,
Our Firm Name  = MANSOORI TRADERS.";
    	            echo "<tr>
	                    <td>".$data[0]."</td>
	                    <td>".$data[1]."</td>
	                    <td>".$oldClBal."</td>
	                    <td>".$Amt."</td>
	                    <td><textarea>".$msg."</textarea></td>
	                </tr>";
	                $totAmt = $totAmt+$data[2];
	            }
	            
	        }
	        echo "<tr>
	                <th colspan='10'><center>Total Amount - ".$totAmt."</center></th>
	            <tr>";
	    }
	    else
	    {
	        echo "<tr>
	       	    	    <th>P.Id</th>
	       		        <th>Date</th>
	       		        <th>Time</th>
				        <th>Amount</th>
				        <th>TxnType</th>
				        <th>Seller</th>
				        <th>Firm</th>
				        <th>BankName</th>
				        <th>Acc Name</th>
				        <th>Ref No.</th>
	        </tr>
	       
	        <tr>
    	        <td colspan='12'><h3 style='color:red'>Data Not Found</h3></td>
	        </tr>";
        }
    }
    
    
    if($_GET['AjaxName']=="Ajax17")
    {
        $txnDeactiveId=$_GET['txnDeactiveId'];
        $update="UPDATE recharge SET Minus='1' WHERE Id='$txnDeactiveId'";
        if(mysqli_query($con,$update))
        {
            echo "Deactivate";
        }
    }
    
    
    if($_GET['AjaxName']=="Ajax18")
    {   
        $fdate       = $_GET['fdate'];
        $ldate       = $_GET['ldate'];
        $userType    = $_GET['userType'];
        $GroupBy     = $_GET['GroupBy'];
        $sl          = 0;
        $newDate     = date_create($fdate); 
        $TimePeriod  = date_format($newDate,"F Y");
        $where2 = "";
        $group1 = "";
        $data1  = "p.Id, p.Date, p.Time, p.Userid, p.ReqType, p.Amount, p.S_Date, p.S_time, p.S_Type, p.TxnNo, p.Mode,";
        $heading = "<tr>
	       	    	    <th>P.Id</th>
	       		        <th>Date<br>Time</th>
	       	    	    <th>ReqType</th>
				        <th>User</th>
				        <th>City</th>
				        <th>Amount</th>
	       		        <th>S_Date<br>S_Time</th>
				        <th>GST NO.<br>Firm Name</th>
				        <th>Txn Disc.</th>
				        <th>Our Bank</th>
	                </tr>";
	    
        
        if($userType!="ALL")
        {
            $where2 = "AND u.Type='$userType'";
        }
        if($GroupBy!="ALL")
        {
            $group1 = "GROUP BY p.Userid";
            $data1  = "p.Userid, SUM(p.Amount),";
            $heading = "<tr>
				        <th>Time Period</th>
				        <th>User</th>
				        <th>City</th>
				        <th>Amount</th>
				        <th>Add Amt.</th>
				        <th>Clinte Firm</th>
				        <th>Our Firm</th>
				        <th>Invoice Value</th>
				        <th>Action</th>
	                </tr>";
	    
        }
        $data2 = "u.Name, u.City,";
        $data3 = "a.Firm, a.Gst,";
        $data4 = "b.BankName, b.AccName, b.firm_id";
        
                    
        $IJorNu   = "LEFT JOIN user AS u ON p.Userid=u.Userid";
        $IJorNa   = "LEFT JOIN api_user AS a ON p.Userid=a.Userid";
        $IJorNb   = "LEFT JOIN bank AS b ON p.BankId=b.Id";
        
        $where1   = "p.S_Date>='$fdate' AND p.S_Date<='$ldate' AND p.Status='APPROVED'";
        $orderBy1 = "ORDER BY u.Name";
        
        $qu = "SELECT $data1 $data2 $data3 $data4 FROM order_req AS p $IJorNu $IJorNa $IJorNb WHERE $where1 $where2 $group1 $orderBy1 ";
	    $re    = mysqli_query($con,$qu);
	    $count = mysqli_num_rows($re);
	    if($count>0)
	    {
	        while($data=mysqli_fetch_array($re))
	        {
	            $sl=$sl+1;
	            if($sl==1)
	            {
	                echo $heading;
	            }
	            if($GroupBy=="ALL")
	            {
	                $ReqId   = $data[0];
	                $Date    = $data[1];
	                $Time    = $data[2];
	                $Uid     = $data[3];
	                $ReqType = $data[4];
	                $Amount  = $data[5];
	                $S_Date  = $data[6];
	                $S_Time  = $data[7];
	                $S_Type  = $data[8];
	                $TxnNo   = $data[9];
	                $Mode    = $data[10];
	                $U_Name  = $data[11];
	                $City    = $data[12];
	                $Firm    = $data[13];
	                $GST     = $data[14];
	                $Bankname= $data[15];
	                $Accname = $data[16];
	                $firm_id = $data[17];
    	            echo "<tr>
	                    <td>".$ReqId."</td>
	                    <td>".$Date."<br>".$Time."</td>
	                    <td>".$ReqType."</td>
	                    <td>".$U_Name."<br> [ ".$Uid." ]</td>
	                    <td>".$City."</td>
	                    <td>".$Amount."</td>
	                    <td>".$S_Date."<br>".$S_Time."</td>
	                    <td>".$GST."<br>".$Firm."</td>
	                    <td>".$S_Type.", ".$Mode."<br>".$TxnNo."</td>
	                    <td>".$Bankname."<br>".$Accname."</td>
	                </tr>";
	                $totAmt = $totAmt+$Amount;
	            }
	            else
	            {
	                $Uid     = $data[0];
	                $Amount  = $data[1];
	                $U_Name  = $data[2];
	                $City    = $data[3];
	                $Firm    = $data[4];
	                $GST     = $data[5];
	                $Bankname= $data[6];
	                $Accname = $data[7];
	                $firm_id = $data[8];
    	            echo "<tr>
	                    <td>".$TimePeriod."</td>
	                    <td>".$U_Name."<br> [ ".$Uid." ]</td>
	                    <td>".$City."</td>
	                    <td>".$Amount."</td>
	                    <td><input type='number'id='Amt".$sl."' value='".$Amount."' style='display:none;'><input type='number'id='addAmt".$sl."' onfocusout='addExtAmt($sl)' style='padding:5px;'></td>
	                    <td>".$GST."<br>".$Firm."</td>
	                    <td>".$Bankname."<br>".$Accname."</td>
	                    <td id='TotalAmt".$sl."'>".$Amount."</td>
	                    <td><button onclick='GenerateInvoice(".$sl.")'>Generate Invoice</button></td>
	                    <input type='hide' id='inp1".$sl."' value='".$ldate."' style='display:none'>
	                    <input type='hide' id='inp2".$sl."' value='".$Amount."' style='display:none'>
	                    <input type='hide' id='inp3".$sl."' value='".$Uid."' style='display:none'>
	                    <input type='hide' id='inp4".$sl."' value='".$TimePeriod."' style='display:none'>
	                    <input type='hide' id='inp5".$sl."' value='".$firm_id."' style='display:none'>
	                </tr>";
	                $totAmt = $totAmt+$Amount;
	            }
	            
	        }
	        echo "<tr>
	                <th colspan='10'><center>Total Amount - ".$totAmt."</center></th>
	            <tr>";
	    }
	    else
	    {
	        echo "<tr>
	       	    	    <th>P.Id</th>
	       		        <th>Date</th>
	       		        <th>Time</th>
				        <th>Amount</th>
				        <th>TxnType</th>
				        <th>Seller</th>
				        <th>Firm</th>
				        <th>BankName</th>
				        <th>Acc Name</th>
				        <th>Ref No.</th>
	        </tr>
	       
	        <tr>
    	        <td colspan='12'><h3 style='color:red'>Data Not Found</h3></td>
	        </tr>";
        }
    }
    
    
    
    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['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";
        }
    }
    
?>