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


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

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

<script>

$(document).ready(function () {


//  var table = $('#userDataTable').DataTable();
var table5 =  $('#HistoryDataTableAgent').DataTable( {
					dom: 'Brtip',
					buttons: [
							'copy', 'csv', 'excel', 'pdf', 'print'
					],
					"footerCallback": function ( row, data, start, end, display ) {
            var api = this.api(), data;

            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace('PHP', '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            };

            // Total over all pages
            total = api
                .column( 4 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );

            // Total over this page
            pageTotal = api
                .column( 5, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );

            // Update footer
            $( api.column( 5 ).footer() ).html(
                pageTotal +' PHP'
            );
						$( api.column( 4 ).footer() ).html(
                total +' PHP'
            );
        	}
			} );


			$('#HistoryDataTableAgent thead tr:eq(1) th').each( function () {
			 // if($(this).index() == 0)
				//{



					var title = $('#HistoryDataTableAgent thead tr:eq(1) th').eq( $(this).index() ).text();
					$(this).html( '<input type="text" placeholder="Search '+title+'" />' );

			} );



			table5.columns().every(function (index) {
				$('.dataTable thead tr:eq(1) th:eq(' + index + ') input').on('keyup change', function () {
						//table.column($(this).parent().index() + ':visible')
						alert(this.value);
						table5.column($(this).parent().index() + ':visible')
								.search(this.value)
								.draw();
				});
		});


// 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 HistoryDataTableAgent" id="HistoryDataTableAgent" width="100%" cellspacing="0">
<thead>
	<tr>
  <th>Date</th>
  <th>Name</th>
  <th>Product</th>
  <th>Quantity</th>
  <th>Sales</th>
	<th>Comission</th>
	<th>Payment Status</th>
</tr>
<tr>
	<th>Date</th>
	<th>Name</th>
	<th>Product</th>
	<th>Quantity</th>
	<th>Sales</th>
	<th>Comission</th>
	<th>Payment Status</th>
</tr>
</thead>
<tbody>
<?php

if($historyname!="")
  {
  //echo $historyname;
  $nameID = getRecord('id','test','name ="'.$historyname.'"');
	$getlevelroot = getRecord('commisionsoldby','sales','soldby ="'.$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."'";*/
		if($getlevelroot == "Level 1")
		{
							$strSQL = "select id,
						         productID,
						         amount,
						         quantity,
										 soldby,
										 boughtby,
										 datecreated,
										 commisionsoldby,
										 level1,
										 level2,
										 level3,
										 level4,
										 level5,
										 id,
										 paymentstatus,
										 total_sales
						 from    (select * from sales where status=1
						          order by soldby) products_sorted,
						         (select @pv := '".$nameID."') initialisation
						 where   find_in_set(soldby, @pv)
						 and     length(@pv := concat(@pv, ',', boughtby))";

			/*			 $strSQL = "select @pv:=boughtby as boughtby,id,
										productID,
										amount,
										quantity,
										soldby,
										datecreated,
										commisionsoldby,
										level1,
										level2,
										level3,
										level4,
										level5,
										paymentstatus,
										total_sales from sales
										 join
										 (select @pv:='".$nameID."')tmp
										 where soldby=@pv";
*/

						  $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>";

						$comission = $row['level1'];
						$paymentstatus = $row['paymentstatus'];
						$getpaymentstatus = getRecord('paymentstatus','payments','salesID ='.$row['id']);
						echo "<td>".$comission." PHP</td>";
						echo "<td>".$paymentstatus."</td>";
						echo "</tr>";
					}
			}
			else if($getlevelroot == "Level 2")
			{

					/*			$strSQL = "select @pv:=boughtby as boughtby,id,
	 										productID,
	 										amount,
	 										quantity,
	 										soldby,
	 										datecreated,
	 										commisionsoldby,
	 										level1,
	 										level2,
	 										level3,
	 										level4,
	 										level5,
											paymentstatus,
	 										total_sales from sales
	 										 join
	 										 (select @pv:='".$nameID."')tmp
	 										 where soldby=@pv"; */

											 $strSQL = "select id,
	 									         productID,
	 									         amount,
	 									         quantity,
	 													 soldby,
	 													 boughtby,
	 													 datecreated,
	 													 commisionsoldby,
	 													 level1,
	 													 level2,
	 													 level3,
	 													 level4,
	 													 level5,
	 													 paymentstatus,
	 													 total_sales
	 									 from    (select * from sales where status=1
	 									          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>";

							$comission = $row['level2'];
							$paymentstatus = $row['paymentstatus'];
							echo "<td>".$comission." PHP</td>";
							echo "<td>".$paymentstatus."</td>";
							echo "</tr>";
						}
				}
				else if($getlevelroot == "Level 3")
				{
							/*		$strSQL = "select @pv:=boughtby as boughtby,id,
		 										productID,
		 										amount,
		 										quantity,
		 										soldby,
		 										datecreated,
		 										commisionsoldby,
		 										level1,
		 										level2,
		 										level3,
		 										level4,
		 										level5,
												paymentstatus,
		 										total_sales from sales
		 										 join
		 										 (select @pv:='".$nameID."')tmp
		 										 where soldby=@pv"; */

												 $strSQL = "select id,
			 												productID,
			 												amount,
			 												quantity,
			 												soldby,
			 												boughtby,
			 												datecreated,
			 												commisionsoldby,
			 												level1,
			 												level2,
			 												level3,
			 												level4,
			 												level5,
			 												paymentstatus,
			 												total_sales
			 								from    (select * from sales where status=1
			 												 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>";

								$comission = $row['level3'];
								$paymentstatus = $row['paymentstatus'];
								echo "<td>".$comission." PHP</td>";
								echo "<td>".$paymentstatus."</td>";
								echo "</tr>";
							}
					}
					else if($getlevelroot == "Level 4")
					{
							/*		$strSQL = "select @pv:=boughtby as boughtby,id,
			 										productID,
			 										amount,
			 										quantity,
			 										soldby,
			 										datecreated,
			 										commisionsoldby,
			 										level1,
			 										level2,
			 										level3,
			 										level4,
			 										level5,
													paymentstatus,
			 										total_sales from sales
			 										 join
			 										 (select @pv:='".$nameID."')tmp
			 										 where soldby=@pv";
													*/

													$strSQL = "select id,
																productID,
																amount,
																quantity,
																soldby,
																boughtby,
																datecreated,
																commisionsoldby,
																level1,
																level2,
																level3,
																level4,
																level5,
																paymentstatus,
																total_sales
												from    (select * from sales where status=1
																 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>";

									$comission = $row['level4'];
									$paymentstatus = $row['paymentstatus'];
									echo "<td>".$comission." PHP</td>";
									echo "<td>".$paymentstatus."</td>";
									echo "</tr>";
								}
						}
						else if($getlevelroot == "Level 5")
						{
							/*
											$strSQL = "select @pv:=boughtby as boughtby,id,
				 										productID,
				 										amount,
				 										quantity,
				 										soldby,
				 										datecreated,
				 										commisionsoldby,
				 										level1,
				 										level2,
				 										level3,
				 										level4,
				 										level5,
														paymentstatus,
				 										total_sales from sales
				 										 join
				 										 (select @pv:='".$nameID."')tmp
				 										 where soldby=@pv"; */
														 $strSQL = "select id,
			 														productID,
			 														amount,
			 														quantity,
			 														soldby,
			 														boughtby,
			 														datecreated,
			 														commisionsoldby,
			 														level1,
			 														level2,
			 														level3,
			 														level4,
			 														level5,
			 														paymentstatus,
			 														total_sales
			 										from    (select * from sales where status=1
			 														 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>";

										$comission = $row['level5'];
										$paymentstatus = $row['paymentstatus'];
										echo "<td>".$comission." PHP</td>";
										echo "<td>".$paymentstatus."</td>";
										echo "</tr>";
									}
							}
							else if($getlevelroot == "Level 6")
							{
								/*				$strSQL = "select @pv:=boughtby as boughtby,id,
					 										productID,
					 										amount,
					 										quantity,
					 										soldby,
					 										datecreated,
					 										commisionsoldby,
					 										level1,
					 										level2,
					 										level3,
					 										level4,
					 										level5,
															paymentstatus,
					 										total_sales from sales
					 										 join
					 										 (select @pv:='".$nameID."')tmp
					 										 where soldby=@pv"; */
															 $strSQL = "select id,
			  	 									         productID,
			  	 									         amount,
			  	 									         quantity,
			  	 													 soldby,
			  	 													 boughtby,
			  	 													 datecreated,
			  	 													 commisionsoldby,
			  	 													 level1,
			  	 													 level2,
			  	 													 level3,
			  	 													 level4,
			  	 													 level5,
			  	 													 paymentstatus,
			  	 													 total_sales
			  	 									 from    (select * from sales where status=1
			  	 									          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>";

											$comission = 0;
											$paymentstatus = $row['paymentstatus'];
											echo "<td>".$comission." PHP</td>";
											echo "<td>".$paymentstatus."</td>";
											echo "</tr>";
										}
								}

mysqli_close($con);
}



 ?>

</tbody>
<tfoot>
            <tr>
                <th colspan="4" style="text-align:right">Total:</th>
                <th></th>
								<th></th>
								<th></th>
            </tr>
        </tfoot>
</table>
