<?php
// Database connection
$servername = "localhost";
$username = "sikkimsi_bluewin";
$password = "sikkimsi_bluewin";
$dbname = "sikkimsi_bluewin";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Commission values from database
$sql = "SELECT level1, level2, level3, level4, level5, level6 FROM web_commission LIMIT 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $level1commission = $row['level1'];
    $level2commission = $row['level2'];
    $level3commission = $row['level3'];
    $level4commission = $row['level4'];
    $level5commission = $row['level5'];
    $level6commission = $row['level6'];
} else {
    die("No commission data found.");
}


	$level1 = (floatval($totalamount) * floatval($level1commission) / 100);
    $level2 = (floatval($totalamount) * floatval($level2commission) / 100);
    $level3 = (floatval($totalamount) * floatval($level3commission) / 100);
    $level4 = (floatval($totalamount) * floatval($level4commission) / 100);
    $level5 = (floatval($totalamount) * floatval($level5commission) / 100);
    $level6 = (floatval($totalamount) * floatval($level6commission) / 100);
	
	$codesquery = "SELECT code, code1, code2, code3, code4, code5 FROM shonu_subjects WHERE id = '".$byabaharkarta."'";//Add ID
	$codesresult = $conn->query($codesquery);
	$codesarr = mysqli_fetch_array($codesresult);
	$code = $codesarr['code'];
	$code1 = $codesarr['code1'];
	$code2 = $codesarr['code2'];
	$code3 = $codesarr['code3'];
	$code4 = $codesarr['code4'];
	$code5 = $codesarr['code5'];

    function add_to_be($userid, $amount, $level = 1) {
        global $conn;  
        $query = "INSERT INTO `to_be_add_comission` (user_id, type, amount, level, status) 
                  VALUES ('$userid', 'betcommission', '$amount', '$level', 1)";  
    
        if ($conn->query($query)) {
            // echo "Commission for user {$userid} has been added successfully.";
        } else {
            // echo "Error adding commission: " . $conn->error;
        }
    }

	
	if($code != null){
		$codeqr = "SELECT id FROM shonu_subjects WHERE owncode = '".$code."'";
		$coders = $conn->query($codeqr);
		$codear = mysqli_fetch_array($coders);
		if(isset($codear['id'])){
			$codeid = $codear['id'];		
			$balquery = "SELECT motta
			  FROM shonu_kaichila
			  WHERE balakedara = '".$codeid."'";
			$balresult = $conn->query($balquery);
			$balarr = mysqli_fetch_array($balresult);
			$motta = round($balarr['motta'], 2);
			$nuamotta = round(($motta + $level1), 2);
			$tathya = mysqli_query($conn,"INSERT INTO `vyavahara` (`balakedara`,`ketebida`,`prakara`,`purba`,`bartaman`,`ayoga`,`koduvavanu`,`tiarikala`) VALUES ('".$codeid."','".$totalamount."','LVLCOMM1','".$motta."','".$nuamotta."','".$level1."','".$byabaharkarta."','".$shnunc."')");
			
			add_to_be($codeid, $level1, 1);
			
		 	// $nabikarana = "UPDATE shonu_kaichila set motta = '$nuamotta' where balakedara='$codeid'";
		 	// $conn->query($nabikarana);
		}
	}
	
	if($code1 != null){
		$codeqr = "SELECT id FROM shonu_subjects WHERE owncode = '".$code1."'";
		$coders = $conn->query($codeqr);
		$codear = mysqli_fetch_array($coders);
		if(isset($codear['id'])){
			$codeid = $codear['id'];
			$balquery = "SELECT motta
			  FROM shonu_kaichila
			  WHERE balakedara = '".$codeid."'";
			$balresult = $conn->query($balquery);
			$balarr = mysqli_fetch_array($balresult);
			$motta = round($balarr['motta'], 2);
			$nuamotta = round(($motta + $level2), 2);
			$tathya = mysqli_query($conn,"INSERT INTO `vyavahara` (`balakedara`,`ketebida`,`prakara`,`purba`,`bartaman`,`ayoga`,`koduvavanu`,`tiarikala`) VALUES ('".$codeid."','".$totalamount."','LVLCOMM2','".$motta."','".$nuamotta."','".$level2."','".$byabaharkarta."','".$shnunc."')");
			
			add_to_be($codeid, $level2, 2);
		
		    $nabikarana = "UPDATE shonu_kaichila set motta = '$nuamotta' where balakedara='$codeid'";
		    //  	$conn->query($nabikarana);
		}		
	}
	
	if($code2 != null){
		$codeqr = "SELECT id FROM shonu_subjects WHERE owncode = '".$code2."'";
		$coders = $conn->query($codeqr);
		$codear = mysqli_fetch_array($coders);
		if(isset($codear['id'])){
			$codeid = $codear['id'];
			$balquery = "SELECT motta
			  FROM shonu_kaichila
			  WHERE balakedara = '".$codeid."'";
			$balresult = $conn->query($balquery);
			$balarr = mysqli_fetch_array($balresult);
			$motta = round($balarr['motta'], 2);
			$nuamotta = round(($motta + $level3), 2);
			$tathya = mysqli_query($conn,"INSERT INTO `vyavahara` (`balakedara`,`ketebida`,`prakara`,`purba`,`bartaman`,`ayoga`,`koduvavanu`,`tiarikala`) VALUES ('".$codeid."','".$totalamount."','LVLCOMM3','".$motta."','".$nuamotta."','".$level3."','".$byabaharkarta."','".$shnunc."')");
			
			add_to_be($codeid, $level3, 3);
			$nabikarana = "UPDATE shonu_kaichila set motta = '$nuamotta' where balakedara='$codeid'";
	    	// $conn->query($nabikarana);
		}		
	}
	
	if($code3 != null){
		$codeqr = "SELECT id FROM shonu_subjects WHERE owncode = '".$code3."'";
		$coders = $conn->query($codeqr);
		$codear = mysqli_fetch_array($coders);
		if(isset($codear['id'])){
			$codeid = $codear['id'];
			$balquery = "SELECT motta
			  FROM shonu_kaichila
			  WHERE balakedara = '".$codeid."'";
			$balresult = $conn->query($balquery);
			$balarr = mysqli_fetch_array($balresult);
			$motta = round($balarr['motta'], 2);
			$nuamotta = round(($motta + $level4), 2);
			$tathya = mysqli_query($conn,"INSERT INTO `vyavahara` (`balakedara`,`ketebida`,`prakara`,`purba`,`bartaman`,`ayoga`,`koduvavanu`,`tiarikala`) VALUES ('".$codeid."','".$totalamount."','LVLCOMM4','".$motta."','".$nuamotta."','".$level4."','".$byabaharkarta."','".$shnunc."')");
			
			add_to_be($codeid, $level4, 4);
		 	$nabikarana = "UPDATE shonu_kaichila set motta = '$nuamotta' where balakedara='$codeid'";
		 	// $conn->query($nabikarana);
		}		
	}
	
	if($code4 != null){
		$codeqr = "SELECT id FROM shonu_subjects WHERE owncode = '".$code4."'";
		$coders = $conn->query($codeqr);
		$codear = mysqli_fetch_array($coders);
		if(isset($codear['id'])){
			$codeid = $codear['id'];
			$balquery = "SELECT motta
			  FROM shonu_kaichila
			  WHERE balakedara = '".$codeid."'";
			$balresult = $conn->query($balquery);
			$balarr = mysqli_fetch_array($balresult);
			$motta = round($balarr['motta'], 2);
			$nuamotta = round(($motta + $level5), 2);
			$tathya = mysqli_query($conn,"INSERT INTO `vyavahara` (`balakedara`,`ketebida`,`prakara`,`purba`,`bartaman`,`ayoga`,`koduvavanu`,`tiarikala`) VALUES ('".$codeid."','".$totalamount."','LVLCOMM5','".$motta."','".$nuamotta."','".$level5."','".$byabaharkarta."','".$shnunc."')");
			
			add_to_be($codeid, $level5, 5);
			$nabikarana = "UPDATE shonu_kaichila set motta = '$nuamotta' where balakedara='$codeid'";
			// $conn->query($nabikarana);
		}		
	}
	
	if($code5 != null){
		$codeqr = "SELECT id FROM shonu_subjects WHERE owncode = '".$code5."'";
		$coders = $conn->query($codeqr);
		$codear = mysqli_fetch_array($coders);
		if(isset($codear['id'])){
			$codeid = $codear['id'];
			$balquery = "SELECT motta
			  FROM shonu_kaichila
			  WHERE balakedara = '".$codeid."'";
			$balresult = $conn->query($balquery);
			$balarr = mysqli_fetch_array($balresult);
			$motta = round($balarr['motta'], 2);
			$nuamotta = round(($motta + $level6), 2);
			$tathya = mysqli_query($conn,"INSERT INTO `vyavahara` (`balakedara`,`ketebida`,`prakara`,`purba`,`bartaman`,`ayoga`,`koduvavanu`,`tiarikala`) VALUES ('".$codeid."','".$totalamount."','LVLCOMM6','".$motta."','".$nuamotta."','".$level6."','".$byabaharkarta."','".$shnunc."')");
			
			add_to_be($codeid, $level6, 6);
			
			$nabikarana = "UPDATE shonu_kaichila set motta = '$nuamotta' where balakedara='$codeid'";
			// $conn->query($nabikarana);
		}		
	}
?>