
<?php
require_once __DIR__."/allfunctions.php";


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

	$historyname = $_GET['historyname'];
?>

<script>

$(document).ready(function () {


//  var table = $('#userDataTable').DataTable();
var table2 =  $('#HistoryDataTable').DataTable( {
					dom: 'Bfrtip',
					buttons: [
							'copy', 'csv', 'excel', 'pdf', 'print'
					]
			} );


// Event listener to the two range filtering inputs to redraw on input
var mincheck = $('.min').val();
var maxcheck = $('.max').val();
	$('.min, .max').change(function () {
		table2.draw();
			});

});



	$.table2.dataTable.ext.search.push(
			function (settings, data, dataIndex) {
					var min = $('.min').val();
					var max = $('.max').val();
					//alert(min);
					//alert(max);
					//var startDate = new Date(data[0]);
					var startDate = data[0];
					//alert(startDate);

					if ( ( isNaN( min ) && isNaN( max ) ) || ( isNaN( min ) && startDate <= max ) || ( min <= startDate   && isNaN( max ) ) || ( min <= startDate   && startDate <= max ) )
					{
							return true;
					}
					return false;

			}
	);


</script>
<table class="table table-bordered HistoryDataTable" id="HistoryDataTable" width="100%" cellspacing="0">
<thead>
  <th>Date</th>
  <th>Name</th>
  <th>Product</th>
  <th>Quantity</th>
  <th>Sales</th>
	<th>Comission</th>
</thead>
<tbody>
<?php

if($historyname!="")
  {
  //echo $historyname;
  $nameID = getRecord('id','test','name ="'.$historyname.'"');
	$getlevelroot = getRecord('commisionsoldby','sales','id ="'.$nameID.'"');
	echo $getlevelroot;
  //echo $nameID;
/*  $strSQL = "SELECT t1.productID as productID,t1.amount as amount,t1.soldby as soldby,t1.boughtby as boughtby,t1.datecreated as datecreated,t1.quantity as quantity,t1.commisionsoldby as commisionsoldby,t1.status as status,t1.total_sales as total_sales  FROM sales AS t1
	LEFT JOIN sales AS t2 ON t2.soldby = t1.boughtby
	LEFT JOIN sales AS t3 ON t3.soldby = t2.boughtby
	LEFT JOIN sales AS t4 ON t4.soldby = t3.boughtby
	LEFT JOIN sales AS t5 ON t5.soldby = t4.boughtby
	LEFT JOIN sales AS t6 ON t6.soldby = t5.boughtby WHERE t1.status='1'  AND t1.soldby='".$nameID."'";*/

	$strSQL = "select id,
         productID,
         amount,
         quantity,
				 soldby,
				 boughtby,
				 datecreated,
				 commisionsoldby,
				 level1,
				 level2,
				 level3,
				 level4,
				 level5
				 total_sales
 from    (select * from sales
          order by soldby) products_sorted,
         (select @pv := '".$nameID."') initialisation
 where   find_in_set(soldby, @pv)
 and     length(@pv := concat(@pv, ',', boughtby))";
  $query = mysqli_query($con,$strSQL) or die (mysqli_error($con));

while($row = mysqli_fetch_array($query ,MYSQLI_ASSOC)) {


echo "<tr>";
echo "<td>".$row['datecreated']."</td>";

$boughtbyname = getRecord('name','test','id ='.$row['boughtby']);
echo "<td>".$boughtbyname."</td>";

$productname = getRecord('name','products','id ='.$row['productID']);
echo "<td>".$productname."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['total_sales']." PHP</td>";

$getlevel = $row['commisionsoldby'];
if($getlevel == "Level 1")
{
	$comission = GetDataUsingSQL("select
				 level1 as lev1,

				 total_sales
 from    (select * from sales
          order by soldby) products_sorted,
         (select @pv := '".$nameID."') initialisation
 where   find_in_set(soldby, @pv)
 and     length(@pv := concat(@pv, ',', boughtby))","lev1");
	//$comission = $row['level1'];
}
elseif($getlevel == "Level 2")
{
	$comission = GetDataUsingSQL("SELECT * FROM (SELECT sum(t1.level2) AS lev2,t1.soldby as soldby
												FROM sales AS t1
												LEFT JOIN sales AS t2 ON t2.soldby = t1.boughtby
												LEFT JOIN sales AS t3 ON t3.soldby = t2.boughtby
												LEFT JOIN sales AS t4 ON t4.soldby = t3.boughtby
												LEFT JOIN sales AS t5 ON t5.soldby = t4.boughtby
												LEFT JOIN sales AS t6 ON t6.soldby = t5.boughtby
											) as lvlTable","lev2");
	//$comission = 20;
	$comission = $row['level2'];
}
elseif($getlevel == "Level 3")
{
	$comission = GetDataUsingSQL("SELECT * FROM (sum(t1.level3) as lev3,t1.soldby as soldby
												FROM sales AS t1
												LEFT JOIN sales AS t2 ON t2.soldby = t1.boughtby
												LEFT JOIN sales AS t3 ON t3.soldby = t2.boughtby
												LEFT JOIN sales AS t4 ON t4.soldby = t3.boughtby
												LEFT JOIN sales AS t5 ON t5.soldby = t4.boughtby
												LEFT JOIN sales AS t6 ON t6.soldby = t5.boughtby
											) as lvlTable","lev3");
	//$comission = 15;
	$comission = $row['level3'];
}
elseif($getlevel == "Level 4")
{
	$comission = GetDataUsingSQL("SELECT * FROM (SELECT sum(t1.level4) AS lev4,t1.soldby as soldby
												FROM sales AS t1
												LEFT JOIN sales AS t2 ON t2.soldby = t1.boughtby
												LEFT JOIN sales AS t3 ON t3.soldby = t2.boughtby
												LEFT JOIN sales AS t4 ON t4.soldby = t3.boughtby
												LEFT JOIN sales AS t5 ON t5.soldby = t4.boughtby
												LEFT JOIN sales AS t6 ON t6.soldby = t5.boughtby
											) as lvlTable","lev4");
	//$comission = 10;
	$comission = $row['level4'];
}
elseif($getlevel == "Level 5")
{
	$comission = GetDataUsingSQL("SELECT * FROM (SELECT sum(t1.level5) as lev5,t1.soldby as soldby
												FROM sales AS t1
												LEFT JOIN sales AS t2 ON t2.soldby = t1.boughtby
												LEFT JOIN sales AS t3 ON t3.soldby = t2.boughtby
												LEFT JOIN sales AS t4 ON t4.soldby = t3.boughtby
												LEFT JOIN sales AS t5 ON t5.soldby = t4.boughtby
												LEFT JOIN sales AS t6 ON t6.soldby = t5.boughtby
											) as lvlTable","lev5");
	//$comission = 5;
	$comission = $row['level4'];
}



echo "<td>".$comission." PHP</td>";
echo "</tr>";

}
mysqli_close($con);
}



 ?>

</tbody>
</table>
