<?php

session_start();

if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");



$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");


//Read From Setup Table							
$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}


$MainCompanyName=explode('_',$HTTP_SESSION_VARS['dbName']);

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " From ".$HTTP_SESSION_VARS['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			  " And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
		  
$resultperiod = mysql_query($queryperiod,$connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while($rows=mysql_fetch_array($resultperiod)){
	extract($rows);
}	

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;

if (empty($ViewLockDaily))
   $ViewLockDaily = 0;  
   
if (empty($LockViewPeriod))
	$LockViewPeriod = 0;	
	
	

$EditMode='';
$DeleteMode='';

$query_login="select ifNull(FI_ShipmentMod,0),ifNull(FI_ShipmentDel,0) from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlogin=mysql_query($query_login,$connection) or die(mysql_error());
$row_login=mysql_fetch_row($resultlogin);

$FI_ShipmentMod=$row_login[0];
$FI_ShipmentDel=$row_login[1];

if($FI_ShipmentMod==0)	
	$EditMode='disabled';

if($FI_ShipmentDel==0)	
	$DeleteMode='disabled';



	
	
	
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search Shipment Refernece</title>
<script type="text/javascript" language="javascript">

function confirm_delete(Code){

	
		if (confirm ( "Are you sure you want to Delete this Record ?") )
		{	
			document.search.mainmode.value='delete';
			document.search.shipment_id.value=Code;
			document.search.action="Form_ShipmentRouting_Actions.php";
			document.search.submit();
		}
}
				
				
function clearform()
{
  document.search.keyword.value='';

  document.search.action='Form_ShipmentRoutingSearch.php';
  document.search.submit();

}


function SetNextPage(startrange,perpage){
	
	document.search.action='Form_ShipmentRoutingSearch.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	document.search.action='Form_ShipmentRoutingSearch.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function set_limit(){

perpage=document.search.perpage.value;
	document.search.action='Form_ShipmentRoutingSearch.php?perpage='+perpage;
	document.search.submit();
}


function ChooseProject(id){
	opener.document.shipment.shipment_id.value=id;
	opener.document.shipment.mainmode.value='editmode';
	opener.document.shipment.action="Form_ShipmentRouting.php";
	opener.document.shipment.submit();
	self.close();
	
}

function PrintShipment (code,ref){

if (confirm ( "Do you want to Print Shipment Reference " + ref+"?"))
	{
	var link = '';
	link = 'Form_shipmentRouting_Preview.php?shipment_id='+code;
	window.open(link);
	}
}

</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; }
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body>
<form name='search' method='post' action=''>
<input type='hidden' name='Proforma_InvoiceNo' value='$Proforma_InvoiceNo'>
<input type='hidden' name='mainmode' value='$mainmode'>
<input type='hidden' name='shipment_id' value='$shipment_id'>

HERE;


echo"<table border='0' >
		 <col width=3%>	
	<col width=3%>
	<col width=3%>
	<col width=15%>
	<col width=15%>
	<col width=15%>
	<col width=10%>
	<col width=25%>

		     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='9'><span class='style16'>List of Shipment Reference</span></td></tr>
	 <tr bgcolor='#DDDDDD' height=30px>
	    <td align='left' colspan='4'><span class='style11'>Search by Code</span></td>
		<td align='left' colspan='3'><span class='style11'><input type='text' name='keyword' id='keyword' value='$keyword' size='15' maxlength='30'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
		    <input type='submit' name='Submit' value='Find' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>
		<td align='left' valign='center' colspan='2'><span class='style25'>Result per page </span><span class='style11'><select name='perpage' id='perpage' value='$perpage' onchange=set_limit();>";
              


        if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';

			
				
        echo'<option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span></td>';	
echo" </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='9'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
	  
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
	<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>	
	<td  height='25' align='center'><span class='style25'>Reference</span></td>
	
		<td  height='25' align='center'><span class='style25'>License</span></td>
		<td  height='25' align='center' ><span class='style25'>Date</span></td>
		<td  height='25' align='center' ><span class='style25'>Total</span></td>
		<td  height='25' align='center' ><span class='style25'>Client</span></td>
		<td  height='25' align='center' ><span class='style25'>SSD</span></td>	
	
	  </tr>";
	  
/// Search Query	
$querysearch="select shipments.shipment_id,Shipment_reference,cltcar_code,shipment_totalamount,ledger_name,Delivery_reference  ,date_format(shipments.shipment_date,'%d-%m-%Y')
			from shipments
			inner join shipmentsdt on shipmentsdt.shipment_id=shipments.shipment_id
			left join clients on clients.ledger_number=shipmentsdt.client_code";

$querysearch=$querysearch." where (Shipment_reference like"."('%".$keyword."%')
							or cltcar_code like"."('%".$keyword."%')
							or shipment_totalamount like"."('%".$keyword."%')
							or ledger_name like"."('%".$keyword."%')
							or Delivery_reference like"."('%".$keyword."%')
							) 
							 and ifnull(shipment_confirmed,0)=0";
							 
							 
$querysearch=$querysearch." And (($rowsperiod = 0 and 1 = 1) Or ($rowsperiod > 0
																	and (($LockViewPeriod = 1 and  Date_Format(shipments.shipment_date,'%Y-%m-%d') >= Date_Format(ifNull('".$ViewPeriodDate."',shipments.shipment_date),'%Y-%m-%d')) Or ($LockViewPeriod = 0 and 1=1 ))   ".
				"                                                   and (($ViewLockDaily = 1 and DateDiff(Date_Format(Now(),'%Y-%m-%d'),Date_Format(shipments.shipment_date,'%Y-%m-%d')) <= $ViewNumberOfDays) Or ($ViewLockDaily = 0 and  1 = 1 ))) ) ";	
$querysearch=$querysearch." group by shipments.shipment_id";	
//echo$querysearch;
$resultsearch=mysql_query($querysearch,$connection);
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;
$resultsearch=mysql_query($querysearch,$connection);		

while($rows=mysql_fetch_array($resultsearch)){
	
	extract($rows);
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
			

	echo'<tr '.$Color.'>';
	
	
	$Deltitle='Delete Record';
	$Edittitle='Edit Record';
	$status='';
	

print<<<HERE
<td align=center width='3%'><img border='0' src='img/mag.jpg' width='12' height='13'  onmouseover=this.style.cursor='pointer' title='Print Preview' onclick=PrintShipment('$rows[0]','$rows[1]');  title='$Prvtitle' $printstatus></td>

<td align=center width='3%'><img border='0' src='img/button_edit.png' width='12' height='13' title='$Edittitle' onmouseover="this.style.cursor='pointer'" onclick="ChooseProject('$rows[0]')" ;  $EditMode></td>
<td align=center width='3%'><img border='0' src='img/c_delete.png' width='9' height='10' title='$Deltitle' onmouseover="this.style.cursor='pointer'" OnClick="confirm_delete('$rows[0]');" $DeleteMode ></td>
<td align='center'><font face='Arial' size='2' color='#186194'><span class="style15">$rows[1]&nbsp;</span></font></td>
<td align='center'><font face='Arial' size='2' color='#186194'><span class='style15'>$rows[2]&nbsp;</span></font></td>
<td align='center'><font face='Arial' size='2' color='#186194'><span class='style15'>$rows[6]&nbsp;</span></font></td>
<td align='right'><font face='Arial' size='2' color='#186194'><span class='style15'>$rows[3]&nbsp;</span></font></td>
<td align='left'><font face='Arial' size='2' color='#186194'><span class='style15'>$rows[4]&nbsp;</span></font></td>
<td align='center'><font face='Arial' size='2' color='#186194'><span class='style15'>$rows[5]&nbsp;</span></font></td>
HERE;

echo"</tr>";


}
							
		
			
		
echo"   <tr><td colspan='9'>&nbsp;</td></tr>
		<tr><td colspan=9><table width='100%' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"<table>";

echo"</td></tr></table>";
	



print <<<HERE
</form>
</body>

HERE;

?>