<?php



include("config.inc.php");

include("Function.inc.php");

session_start();


include("Functions_UserAccess.php");


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");

mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");


$service='delivery';

$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2,ifnull(cashreg_colortheme,'default') as cashreg_colortheme from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}



print <<<HERE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>



<head>
<style type="text/css">
::-webkit-scrollbar {width: 20px; height: 4px; background: #4682B4; }
::-webkit-scrollbar-thumb { background-color: #DCDCDC; -webkit-border-radius: 1ex; }
</style>

HERE;

if($cashreg_colortheme=='red'){
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-red.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-red.css" />';
}	
else if($cashreg_colortheme=='' || $cashreg_colortheme=='default')	{
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-default.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-default.css" />';
}


print <<<HERE

<link rel="stylesheet" href="Style.css" type="text/css" />

<script type="text/javascript">

function closewin(){
self.close();
}

function setsale(vobj,vsaleid,vobjid,intime,outtime){

var vobjectid = 'out'+vobjid;
var vobjectid1 = 'in'+vobjid;


if (vobj.checked) {

if (outtime != '') {
	document.getElementById(vobjectid).value = outtime;
	document.getElementById(vobjectid1).disabled = false;
	}
	
if (intime != '') {
	document.getElementById(vobjectid).disabled = false;
	document.getElementById(vobjectid1).value = intime;
	}
if (intime == '' && outtime == '') {
			document.getElementById(vobjectid).disabled = false;	
			document.getElementById(vobjectid).value = 'Out';	
			document.getElementById(vobjectid1).disabled = true;	
			document.getElementById(vobjectid1).value = 'In';				
		}
}else {
document.getElementById(vobjectid).disabled = true;
document.getElementById(vobjectid1).disabled = true;
document.getElementById(vobjectid).value = 'Out';	
document.getElementById(vobjectid1).value = 'In';
}	
	
if (vobj.checked) {

if (document.getElementById(vobjid).value=='')
	document.getElementById(vobjid).value = '-1';

	document.getElementById(vobjid).value = document.getElementById(vobjid).value + ',' + vsaleid;
	}	else {
	var vval1 = document.getElementById(vobjid).value;
	var vval2 = document.getElementById(vobjid).value;
	var vval3  = ','+vsaleid;
	vval1 = vval2.replace(vval3,'');
	document.getElementById(vobjid).value = vval1;
	}
	if (document.getElementById(vobjid).value=='-1')
		document.getElementById(vobjid).value='';
//alert(vsaleid);
}


function settime(vobj,vdeldriver_id,vtype){

var vsaleid = (document.getElementById(vdeldriver_id).value);


	obj1='out'+vdeldriver_id;
	obj2='in'+vdeldriver_id;
	
	
	
if (window.XMLHttpRequest)

		  {

		  xmlhttp=new XMLHttpRequest();

		  }

		else	  {

		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

		  }

		

		xmlhttp.onreadystatechange=function()

		  {

		  if (xmlhttp.readyState==4 && xmlhttp.status==200){
				
				vobj.value = (xmlhttp.responseText);
				vobj.disabled = true;
				
				if (vtype=='start') {
					document.getElementById(obj1).disabled=true;
					document.getElementById(obj2).disabled=false;
					}
				else if (vtype=='end'){
					document.getElementById(obj1).disabled=true;
					document.getElementById(obj2).disabled=true;
				}
				window.location.reload();
				 }

		  }


	
	xmlhttp.open("GET","Form_DispatchScreen_Action.php?qq="+vsaleid+"&action=settime&deldriver_id="+vdeldriver_id+"&type="+vtype,true);

	xmlhttp.send();

		

}


</script>

<style type="text/css">

img:hover {
    opacity: 0.5;
    filter: alpha(opacity=50);
		cursor: hand; 
		cursor: pointer;
}

</style>
</head>
<body leftmargin="0px" topmargin="0px" marginwidth="0px" marginheight="0px"> 
<form method="GET" name='dispatchscreen' action=''>

HERE;



echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' ><br>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:650px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Dispatch
			</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr><tr height = 400 valign = top><td id='search-content1'></td><td>
	
";



print <<<HERE
<table width='800' border=0>
<tr><td ></td><td ></td></tr>
<tr><td ><div style='overflow-y:scroll;width:840px;height:570px' >
<table width='800' border=0 style='font-size:12px;color:white;font-weight:bold'>
HERE;

			$query="select distinct(clients.ledger_number),clients.ledger_name 
			from retail left join clients on clients.ledger_number = retail.driver_code 
			left join customer on customer.Customer_Code= retail.Customer_Code
			where 1 = 1   	
			and date_format(sale_date,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')
			and retail.Customer_Code <> ''
			and clients.ledger_number > 0
			order by clients.ledger_name asc";
						
	
	//echo $query;
	$j=0;
	$result=mysql_query($query,$connection) or die(mysql_error());
	while($row=mysql_fetch_array($result)){
	
			$query1="select sale_id,Sale_Reference,CashRegister_Name,Sale_Total,retail.currency_code,ifnull(sale_closed,0),date_format(sale_date,'%h-%i-%s') 
			,clients.ledger_name ,Customer_Address 
			,customer_name,customer.area_code,clients.ledger_number
			,date_format(dispatchdriver.dispatch_in,'%h:%i:%s'),date_format(dispatchdriver.dispatch_out,'%h:%i:%s')
			from retail left join clients on clients.ledger_number = retail.driver_code 
			left join customer on customer.customer_code= retail.customer_code
			inner join dispatchdriver on dispatchdriver.deldriver_id= clients.ledger_number 
					and dispatchdriver.retail_id=retail.sale_id
			where 1 = 1   	
			and date_format(sale_date,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')
			and clients.ledger_number='".$row[0]."'
			and retail.customer_code <> ''
			and (
				(dispatchdriver.dispatch_in is null or dispatchdriver.dispatch_in = '')
				or
				(dispatchdriver.dispatch_out is null or dispatchdriver.dispatch_out = '')
				)
			order by clients.ledger_name asc";
			//echo $query1;
			$result1=mysql_query($query1,$connection) or die(mysql_error());
			$num = mysql_num_rows($result1);
	if ($num>0){		
	if ($j>0){
	//echo"<tr><td colpan=11>&nbsp;</td></tr>";
	//echo"<tr><td colpan=11>&nbsp;</td></tr>";
	}
	
	echo"<tr   bgcolor='#D8D8D8' >";
	echo"<td  width='15'><input type='hidden' name='$row[0]' id='$row[0]' value=''>

	<button type='button' id='btnSubmit1' name='btnSubmit1' onclick='showaccount($row[0])' readonly title=''><span><span>".$row[1]."</span></span></button>

	</td>";
	
	echo"<td>$row[2]</td>";
	echo"<td>$row[10]</td>";
	echo"<td>$row[9]</td>";

	
	echo"<td>
	<button type='button' name='out$row[0]'  id='out$row[0]'  onMouseOver=\"style.cursor='pointer'\" disabled onclick=settime(this,$row[0],'start') style='border:0px;width:108px;height:56px;color:black;font-weight:bold;font-size:18px' ><span><span>Out</span></span></button></td>";
	echo"<td><button type='button' name='in$row[0]' id='in$row[0]'  onMouseOver=style.cursor='pointer'  disabled    onclick=settime(this,$row[0],'end') style=' border:0px;width:108px;height:56px;color:black;font-weight:bold;font-size:18px' ><span><span>In</span></span></button></td>";
	echo"<td  align=center><font color=green size=5px><b><u></u></b></font></td>";
	echo"</tR>";
	
	while($row1=mysql_fetch_array($result1)){
	 
	 $ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";		
		
		
			echo"<tr $Color height='30px'>
				<tD  width='40px'align=center></td>
				<tD  width='50px'  style=' border-top: thin solid' align=center>
				<input type='checkbox' name='n$row1[0]' value='1' onclick=setsale(this,$row1[0],$row[0],'$row1[12]','$row1[13]')></td>
				<tD  style='border-top: thin solid'  width='50px'><span class='style5'></td>
				<tD  style=' border-top: thin solid'  width='180px'><span class='style5'>$row1[9]</td>
				<tD  style=' border-top: thin solid'  width='140px'><span class='style5'>$row1[10]</td>
				<tD   style=' border-top: thin solid' width='300px'><span class='style5'>$row1[8]</td>
				<tD   style=' border-top: thin solid; border-top: thin solid' width='300px' align=center><font color=red  size=5px><b>$row1[13]</b></font></td>
								
				</tr>";
			
			$i+=1;
			}

					$j+=1;
	}
	
}
	
	//echo"<tr><td colspan=10 height='20'></td></tr>";
echo"</table><div>";	


if ($deldriver_id!='' and $saleid!='')
	{
	$query="update retailorder set  deldriver_id = '".$deldriver_id."' where sale_id = '".$saleid."'";
	$result=mysql_query($query,$connection) or die(mysql_error());
	$sale_id='';
	$deldriver_id='';
	print <<<HERE
	<script type="text/javascript">
	opener.location.reload();
	self.close();
	</script>
HERE;
	}



print <<<HERE
   </table></td><td id='search-content3' ></td></tr>
  <tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:600px;' valign='top'>
HERE;
	 
		echo"</td>
		<td id='search-bottom3' style='height:34px; width:31px;'>";

 
	
print <<<HERE
&nbsp;</td></tr>
		
   </table>
</form>
</body>
</html>

HERE;

?>