<?php
include "../db/db.php";
session_start();
$log=$_SESSION['user'];

if (empty($log)) {    
  $redirectUrl = "../index.php";
  echo "<script type=\"text/javascript\">";
  echo "window.location.href = '$redirectUrl'";
  echo "</script>";

}else{

$sqlu="SELECT * FROM user WHERE user='$log' and val='1' and access=1";
$resultu=mysqli_query($conn, $sqlu);
$countu=mysqli_num_rows($resultu);
if($countu==1){



$sqlprr=mysqli_query($conn, "SELECT * FROM p3_q WHERE id_no=0 and id!=1 ORDER BY id ASC");
if(mysqli_num_rows($sqlprr) > 0) {
	echo "<script>alert('Please Generate ID No(s)')</script>";
	$redirectUrl = "../home.php";
	echo "<script type=\"text/javascript\">";
    echo "window.location.href = '$redirectUrl'";
    echo "</script>";

} else {

	$sqlpr = mysqli_fetch_array(mysqli_query($conn, "SELECT process_id FROM invoice_no"));
	$pro_id = $sqlpr['process_id'];
			
	$sqlr="SELECT * FROM p3_q";
	$resultr=mysqli_query($conn, $sqlr);
	
	while ($rowr = mysqli_fetch_array($resultr)){
		$id = $rowr['id'];
		$reg = $rowr['reg_no'];
		$id1 = $rowr['id_no'];
		$uname = $rowr['username'];
		$name = $rowr['name'];
		$block = $rowr['block'];
	
		$x = 9;
		$emp = array();
	
		while($reg-$x) {
			ini_set('max_execution_time', 300); 
			$x++;
			
			if($id == 1) {
				$sql1 = "SELECT id_no FROM p3_q where id_no like '$x%' and id_no NOT IN(SELECT pay_id FROM p3payment WHERE owner_id='$id1')"; 		
			} else {
				$sql1 = "SELECT id_no FROM p3_q where id_no like '$id1$x%' and id_no NOT IN(SELECT pay_id FROM p3payment WHERE owner_id='$id1')";
			}
			$result1 = mysqli_query($conn, $sql1);
			
			$nrow = mysqli_num_rows($result1); 
			if($nrow > 0) {
				$emp[$x] = $nrow;
			}			
		}
	
		$length = count($emp);
		
		while($length > 1) {
			$min = min(array_filter($emp));
			Payment($id1, $min, $id, $p_id, $length, $pro_id, $uname, $conn, $name, $block);
			$length--;	
		}
	}
	
	$pro_new = $pro_id + 1; 
	$sqluppr = mysqli_query($conn, "UPDATE invoice_no SET process_id='$pro_new'");

}


} 
else{
  $redirectUrl = "../index.php";
  echo "<script type=\"text/javascript\">";
  echo "window.location.href = '$redirectUrl'";
  echo "</script>";

}
}

function Payment($id1, $min, $id, $p_id, $l, $pro_id, $uname, $conn, $name, $block) {
	$l1= count($GLOBALS['emp']);
	
	if($l1 > 1) {		
		$sql = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM invoice_no"));
		$inv = $sql['invoice_no'];
		
		date_default_timezone_set('Asia/Colombo');
		$date = date("Y-m-d");
		
		foreach ($GLOBALS['emp'] as $key => $value) {
			if($min >= 4) {
				$min = 4;
				$flag = 1;
				$paymnt = 50000;
			
			} else if($min >= 3) {
				$min = 3;
				$flag = 1;
				$paymnt = 30000;
			
			} else if($min >= 2) {
				$min = 2;
				$flag = 1;
				$paymnt = 20000;
			
			}  else if($min >= 1) {
				$min = 1;
				$paymnt = 15000;
			}
			
		    	
			if($id == 1) {
				$sql2 = "SELECT * FROM p3_q where id_no like '$key%' and id_no NOT IN(SELECT pay_id FROM p3payment WHERE owner_id='$id1') LIMIT $min";
			} else {
				$sql2 = "SELECT * FROM p3_q where id_no like '$id1$key%' and id_no NOT IN(SELECT pay_id FROM p3payment WHERE owner_id='$id1') LIMIT $min";
			} 
		  
			$result2 = mysqli_query($conn, $sql2);
			
			while($row2 = mysqli_fetch_array($result2)) { 
				$pay = $row2['id_no'];
				$pid = $row2['id'];
				$pname = $row2['username'];
				$sname = $row2['name'];

				$sql3 = "INSERT INTO p3payment SET owner_id='$id1', o_id='$id', pay_id='$pay', p_id='$pid', invoice_no='$inv', process_id='$pro_id', date='$date', payment='', user='$pname', name='$sname'";	
				
				mysqli_query($conn, $sql3);
			}
			
			
			if($flag == 1) {
				if($id == 1) {
					$sql21 = "SELECT * FROM p3_q where id_no like '$key%' and id_no NOT IN(SELECT pay_id FROM p3payment WHERE owner_id='$id1')";
				} else {
					$sql21 = "SELECT * FROM p3_q where id_no like '$id1$key%' and id_no NOT IN(SELECT pay_id FROM p3payment WHERE owner_id='$id1')";
				} 
			  
				$result21 = mysqli_query($conn, $sql21);
				
				while($row21 = mysqli_fetch_array($result21)) { 
					$pay1 = $row21['id_no'];
					$pid1 = $row21['id'];
					$pname1 = $row21['username'];
					$sname1 = $row21['name'];
					
					$sql31 = "INSERT INTO p3payment SET owner_id='$id1', o_id='$id', pay_id='$pay1', p_id='$pid1', process_id='$pro_id', date='$date', user='$pname1', name='$sname1'";
					mysqli_query($conn, $sql31);
				}
				
			} 
			
			if($flag == 1) {
				$val = 0;
			} 
					
			if($val <= 0) {
				unset($GLOBALS['emp'][$key]);
			} else{		
				$GLOBALS['emp'][$key] = $val; 
			}				
		}

		
		if($block == 1) {
			
			echo "RLN".$id." / User ID: ".$uname." / ".$name." / Payment: ".$paymnt.' (Company Commission)'."<br>";
		
			$sqlr = mysqli_query($conn, "INSERT INTO sales SET item_code='Company Commission', dealer_id='1', invoice_no='$inv', date='$date', debit='$paymnt', process_id='$pro_id', description='Payment Process'");	
			
			$sqlr1 = mysqli_query($conn, "INSERT INTO payment_block SET dealer_id='$id', invoice_no='$inv', date='$date', payment='$paymnt', process_id='$pro_id'");
		
		
		} else {
			echo "RLN".$id." / User ID: ".$uname." / ".$name." / Payment: ".$paymnt."<br>";

			$sqlr = mysqli_query($conn, "INSERT INTO sales SET item_code='Commission', dealer_id='$id', invoice_no='$inv', date='$date', debit='$paymnt', process_id='$pro_id', description='Payment Process'");
		
		}

		$inv_new = $inv + 1; 
		$sqlup = mysqli_query($conn, "UPDATE invoice_no SET invoice_no='$inv_new'");
		
	}
	
}
?>
<br><br>
<a href="../home.php" class="btn btn-primary">FINISH</a>

<style>
.btn-primary {
    background-color: #3c8dbc;
    border-color: #367fa9;
}
.btn {
    border-radius: 3px;
    -webkit-box-shadow: none;
    box-shadow: none;
    border: 1px solid transparent;
}

.btn-primary {
    color: #fff;
    background-color: #337ab7;
    border-color: #2e6da4;
}
.btn {
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid transparent;
    border-radius: 4px;
	font-family: 'Source Sans Pro','Helvetica Neue',Helvetica,Arial,sans-serif;
    font-weight: 400;
}

a{
text-decoration:none;

}
</style>