
<?php
session_start();
require_once __DIR__."/allfunctions.php";

if(empty($_SESSION["MEMBER_ID"]) && empty($_SESSION["TYPE"]) && empty($_SESSION["FIRST_NAME"]))
{
	 $itemarray = array();
	 $memberid = "";
	$type = "";
	$fname = "";
}
else
{
	$memberid = $_SESSION['MEMBER_ID'];
	$type = $_SESSION['TYPE'];
	$fname = $_SESSION['FIRST_NAME'];
}

	$con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
	$echo="";

	$checklistdata = $_GET['checklistdata'];
	$commission = $_GET['commission'];
	$agent = $_GET['agent'];
	$saledate = $_GET['saledate'];
	//echo $historyname;
	//$checklistdata = explode(",",$historyname);
	//echo $checklistdata;
	//echo $commission;
	$array = explode(',', $checklistdata);
	$arraycommision = explode(',', $commission);
	$arrayagent = explode(',', $agent);
	$arraysaledate = explode(',', $saledate);
	$ctr = count($array);
	$dateToday = date('Y-m-d');
	//var_dump($arraycommision);
	for ($c=0;$c<$ctr;$c++)
	{
	//echo $checklistdata[$c];
	//echo $c;
	$now =date("Y-m-d H:i:s");
	$status = "Paid";
	$paymentref= generateCode();
echo $paymentref;

	//$paymentref = NULL;
	$soldby = getRecord('soldby','sales','id ='.$array[$c].'');

	$strSQL = "UPDATE sales SET paymentstatus='Paid',paymentdate='".$dateToday."',approvedpaymentby='".$memberid."' WHERE status=1 and id=".$array[$c];
	//echo $strSQL;
		if(mysqli_query($con, $strSQL) or die("database error: ". mysqli_error($con)))
		{
			$getlevelroot = getRecord('commisionsoldby','sales','soldby ='.$soldby);

			if($getlevelroot == "Level 1")
			{


					$checkif = checkifpaymentexist($array[$c]);
					 $sqlQuery3 = "select  boughtby,
										soldby,
										level1,
										id
						from    (select * from sales where status=1 and paymentstatus='Paid' and datecreated='".$arraysaledate[$c]."'
										 order by soldby, id) products_sorted,
										(select @pv := '".$soldby."') initialisation
						where   find_in_set(soldby, @pv)
						and     length(@pv := concat(@pv, ',', boughtby))";
			    $sqlQuery3 = mysqli_query($con,$sqlQuery3) or die(mysqli_error($con));
			    $numRows = mysqli_num_rows($sqlQuery3);

			    while($rows = mysqli_fetch_array($sqlQuery3,MYSQLI_ASSOC)) {
				      $total =	$rows['level1'];

							if(!$checkif)
							{
								$insert_sql3 = "INSERT INTO payments(id,paymentref,salesID,amountpaid,paymento,paymentstatus,paymentdate,approvedpaymentby,salesdate)
								VALUES(NULL,'".$paymentref."','".$array[$c]."','".$total."', '".$soldby."','".$status."','".$now."','".$memberid."', '".$arraysaledate[$c]."')";
								$rs = mysqli_query($con,$insert_sql3) or die(mysqli_error($con));

								if($rs)
								{

									echo 1;
								}
								else
								{
									echo 0;
								}

							}
							else {
								echo "Exist";
							}
				}

	    }
			elseif($getlevelroot == "Level 2")
			{


					$checkif = checkifpaymentexist($array[$c]);
					 $sqlQuery3 = "select  boughtby,
										soldby,
										level2,
										id
						from    (select * from sales where datecreated='".$arraysaledate[$c]."'
										 order by soldby, id) products_sorted,
										(select @pv := '".$soldby."') initialisation
						where   find_in_set(soldby, @pv)
						and     length(@pv := concat(@pv, ',', boughtby))";
			    $sqlQuery3 = mysqli_query($con,$sqlQuery3) or die(mysqli_error($con));
			    $numRows = mysqli_num_rows($sqlQuery3);

			    while($rows = mysqli_fetch_array($sqlQuery3,MYSQLI_ASSOC)) {
				      $total =	$rows['level2'];
							if(!$checkif)
							{
								$insert_sql3 = "INSERT INTO payments(id,paymentref,salesID,amountpaid,paymento,paymentstatus,paymentdate,approvedpaymentby,salesdate)
								VALUES(NULL,'".$paymentref."','".$array[$c]."','".$total."', '".$soldby."','".$status."','".$now."','".$memberid."', '".$arraysaledate[$c]."')";
								$rs = mysqli_query($con,$insert_sql3) or die(mysqli_error($con));

								if($rs)
								{
									echo 1;
								}
								else
								{
									echo 0;
								}

							}
							else {
								echo "Exist";
							}
				}

	    }
			elseif($getlevelroot == "Level 3")
			{


					$checkif = checkifpaymentexist($array[$c]);
					 $sqlQuery3 = "select  boughtby,
										soldby,
										level3
						from    (select * from sales where datecreated='".$arraysaledate[$c]."'
										 order by soldby, id) products_sorted,
										(select @pv := '".$soldby."') initialisation
						where   find_in_set(soldby, @pv)
						and     length(@pv := concat(@pv, ',', boughtby))";
			    $sqlQuery3 = mysqli_query($con,$sqlQuery3) or die(mysqli_error($con));
			    $numRows = mysqli_num_rows($sqlQuery3);

			    while($rows = mysqli_fetch_array($sqlQuery3,MYSQLI_ASSOC)) {
				      $total =	$rows['level3'];
							if(!$checkif)
							{
								$insert_sql3 = "INSERT INTO payments(id,paymentref,salesID,amountpaid,paymento,paymentstatus,paymentdate,approvedpaymentby,salesdate)
								VALUES(NULL,'".$paymentref."','".$array[$c]."','".$total."', '".$soldby."','".$status."','".$now."','".$memberid."', '".$arraysaledate[$c]."')";
								$rs = mysqli_query($con,$insert_sql3) or die(mysqli_error($con));

								if($rs)
								{
									echo 1;
								}
								else
								{
									echo 0;
								}

							}
							else {
								echo "Exist";
							}
				}

	    }
			elseif($getlevelroot == "Level 4")
			{


					$checkif = checkifpaymentexist($array[$c]);
					 $sqlQuery3 = "select  boughtby,
										soldby,
										level4
						from    (select * from sales where datecreated='".$arraysaledate[$c]."'
										 order by soldby, id) products_sorted,
										(select @pv := '".$soldby."') initialisation
						where   find_in_set(soldby, @pv)
						and     length(@pv := concat(@pv, ',', boughtby))";
			    $sqlQuery3 = mysqli_query($con,$sqlQuery3) or die(mysqli_error($con));
			    $numRows = mysqli_num_rows($sqlQuery3);

			    while($rows = mysqli_fetch_array($sqlQuery3,MYSQLI_ASSOC)) {
				      $total =	$rows['level4'];
							if(!$checkif)
							{
								$insert_sql3 = "INSERT INTO payments(id,paymentref,salesID,amountpaid,paymento,paymentstatus,paymentdate,approvedpaymentby,salesdate)
								VALUES(NULL,'".$paymentref."','".$array[$c]."','".$total."', '".$soldby."','".$status."','".$now."','".$memberid."', '".$arraysaledate[$c]."')";
								$rs = mysqli_query($con,$insert_sql3) or die(mysqli_error($con));

								if($rs)
								{
									echo 1;
								}
								else
								{
									echo 0;
								}

							}
							else {
								echo "Exist";
							}
				}

	    }
			elseif($getlevelroot == "Level 5")
			{


					$checkif = checkifpaymentexist($array[$c]);
					 $sqlQuery3 = "select  boughtby,
										soldby,
										level5
						from    (select * from sales where datecreated='".$arraysaledate[$c]."'
										 order by soldby, id) products_sorted,
										(select @pv := '".$soldby."') initialisation
						where   find_in_set(soldby, @pv)
						and     length(@pv := concat(@pv, ',', boughtby))";
			    $sqlQuery3 = mysqli_query($con,$sqlQuery3) or die(mysqli_error($con));
			    $numRows = mysqli_num_rows($sqlQuery3);

			    while($rows = mysqli_fetch_array($sqlQuery3,MYSQLI_ASSOC)) {
				      $total =	$rows['level5'];
							if(!$checkif)
							{
								$insert_sql3 = "INSERT INTO payments(id,paymentref,salesID,amountpaid,paymento,paymentstatus,paymentdate,approvedpaymentby,salesdate)
								VALUES(NULL,'".$paymentref."','".$array[$c]."','".$total."', '".$soldby."','".$status."','".$now."','".$memberid."', '".$arraysaledate[$c]."')";
								$rs = mysqli_query($con,$insert_sql3) or die(mysqli_error($con));

								if($rs)
								{
									echo 1;
								}
								else
								{
									echo 0;
								}

							}
							else {
								echo "Exist";
							}
				}

	    }




			//mysqli_close($con);

		}
		else
		{
			echo 0;
		}
	}
//	echo $historyname;
?>
