<?php
session_start();
include '../../db_connect.php';

function date_formatc($temp_date)
{
list($year, $month, $day) = explode('-', $temp_date);
return "$day/$month/$year";
}

$today_date=date("d/m/Y");
$from_date = $today_date;
$to_date = $today_date;

$cmp_key=$_POST['cmp_key'];


if(trim($_POST['from_date']))
$from_date =$_POST['from_date'];

if(trim($_POST['to_date']))
$to_date =$_POST['to_date'];

$sql="";
if($_POST['sortby']=='NAME'){
	
	$qrypart="and client like '". $_POST['searchtype']."%'";
	}
else if($_POST['sortby']=='JOBID'){
	$qrypart="and jobid like '". $_POST['searchtype']."%'";
	}
	else
	{
	$qrypart="where  issdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y')";
	//$qrypart="";	
		}
	$datefill="where  issdate between STR_TO_DATE('$from_date','%d/%m/%Y') and STR_TO_DATE('$to_date','%d/%m/%Y')";	
	//$sql = "select * from  inventoryreg where  $qrypart and $datefill subdesc = 'stockin' order by jobid desc";

$sql = "select * from  inventoryreg $datefill and subdesc = 'stockin' order by jobid desc";

if(isset($_POST['next_rec']))
$sql=$sql." limit $_POST[next_rec],200";

if(isset($_POST['next_rec']))
$sql=$sql." limit $_POST[next_rec],200";

	if($result = mysqli_query($con,$sql))
	{
	$no_of_rec=mysqli_num_rows($result);
	if($no_of_rec==0)
	echo "";
	else{
	$show_rec=$no_of_rec;
	if($no_of_rec>200)
	{
	$result = mysqli_query($con,$sql." limit 0,200");
	$show_rec=$_POST[next_rec]+mysqli_num_rows($result);
	}
	echo "<table id='demotable' border='1' bordercolor='#053400'  width='100%' cellspacing='0' ><thead><tr>
	<th ><textarea name='jobid[]'>JOB ID</textarea></th>
	<th ><textarea name='batchno[]'>BATCH NO.</textarea></th>
	<th ><textarea name='refno[]'>REF NO.</textarea></th>
	<th ><textarea name='date[]'>JOB DATE</textarea></th>
	<th ><textarea name='client[]'>COMP. DATE</textarea></th>
	<th><textarea name='calltype[]'>JOBBER</textarea></th>
	<th ><textarea name='transport[]'>TRANSPORT</textarea></th>
	<th ><textarea name='attendby[]'>ATTEND BY</textarea></th>
	<th><textarea name='attendmob[]'>ATTEND MOB</textarea></th>
	<th><textarea name='status[]'>STATUS</textarea></th>
	</tr></thead><tbody>";
	while($row = mysqli_fetch_array($result))
	{
	$highlight='';
	if(strtotime($row['rdate'])>strtotime(date("Y-m-d"))&&substr($row['rdate'],0,3)=='OUT')
	{
	$highlight="Cont. ".date_formatc($row['rdate']);
	$style="style='color:#CC3520;'";
	}
	$statusArray = $_SESSION[SESS_STATUSARR];
  	echo "<tr><td  ><a href='stockentry.php?jobid=$row[jobid]' onClick='positionedPopup(this.href,1200,650);return false'><textarea name ='jobid[]' >$row[jobid]</textarea></a></td>
	<td><textarea name ='batchno[]'>$row[batchno]</textarea></td>
	<td><textarea name ='refno[]'>$row[refno]</textarea></td>
<td><textarea name ='date[]'>".date_formatc($row[issdate])."</textarea></td>
<td><textarea name ='client[]' class ='callby'>".date_formatc($row[divdate])."</textarea></td>
<td><textarea name ='calltype[]' class ='calltype'  >$row[jobcode]</textarea></td>
<td><textarea name ='transport[]' class ='transport' >$row[transport]</textarea></td>
<td><textarea name ='attendby[]' class ='attentedby'>$row[Supervisor]</textarea></td>
<td><textarea name ='attendmob[]'>$row[scontact]</textarea></td>
<td><textarea name ='status[]' class='status' >".$statusArray[$row[status]]."</textarea></td>
</tr>";
}
$temp_var=0;
if(isset($_POST['next_rec']))
$temp_var=$_POST['next_rec'];

if(isset($_POST[tot_rec]))
$no_of_rec=$_POST[tot_rec];

echo "</tbody></table>###<div style='float:left;font-weight: bold;color: #000;width:30%;padding:5px'>Showing records ".($_POST[next_rec]+1)."-".($show_rec+$temp_var)." of  <span id='tot_rec'>".($no_of_rec)."</span></div>";

if($no_of_rec>200||isset($_POST['next_rec']))
{
echo "<div style='text-align: right;font-weight: bold;color: #000;padding:5px;width:30%;float: left;'>";
for($i=0;($no_of_rec/200)>0;$i++,$no_of_rec-=200)
echo "<span class='links'><a style='color:#000' class='linkspages' href='#' id=".($i*200).">".($i+1)."</a></span>";
echo "</div>";
}
	}
	}
	else 
	echo mysqli_error($con);


mysqli_close($con);
?>


