<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 2;

$title = _('SRN Dispatch Receipt Note');

$url_cancel= "index.php?route=LOG/transaction/srndispatch_note_index".$rurl;
$url_add= "index.php?route=LOG/transaction/srndispatch_note_add".$rurl;
$url_edit= "index.php?route=LOG/transaction/srndispatch_note_edit".$rurl;
$url_view= "index.php?route=LOG/transaction/srndispatch_note_view".$rurl;

if($_REQUEST['action']=='Cancel')
{
	$site=$_SESSION['AssignedBranch'];
	$store=$_REQUEST['store'];
		
	$dbl_addmrn_Row= DB_fetch_array(DB_query("delete from log_srndispatch where id='".$_REQUEST['mrnid']."'",$db));
	$Update_Query=DB_query("delete log_srndispatchqty where srn_id='".$_REQUEST['mrnid']."'",$db);
	$Update_Query1 =DB_query("update log_srndispatchqty set Can_status='Y' where srn_id='".$_REQUEST['mrnid']."'",$db);
}

?>
<script src="js/jsapi.js" type="text/javascript">
</script>

<style>
#slidingDiv {
    display: none;
	height:100px;
	background-color: #D9D6DA;
	padding:20px;
}
</style>
<?php
	$type="paging";
	$rowsPerPage = 10;
	$pageNum = 1;
	if(isset($_GET['page']))
	{
		$pageNum = $_GET['page'];
	}
	$offset = ($pageNum - 1) * $rowsPerPage;



echo '<form action="' . $_SERVER['PHP_SELF'] . '?' . 'route=LOG/transaction/goods_receipt_note_index&modid='.$_REQUEST['modid'].'&frmid=' .$_REQUEST['frmid'].'" method=post>';

if ($_SESSION['UserStockLocation'] == $_SESSION['UserStockLocation'])
  {
	if (!isset($RequisitionNumber) or $RequisitionNumber==''){
	
		
?>		

<div id="container">
<script type="text/javascript">
function showSlidingDiv(){
$("#slidingDiv").animate({"height": "toggle"}, { duration: 700 });
}
</script>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div"> 
 <h1>SRN Dispatch Note</h1>	
 

<div align="left">
		<a href="<?php echo $url_add;?>"><input type="button" class="search" value="Add New" /></a>
<!--<a href="#" onClick="showSlidingDiv(); return false;"><input type="button" class="search" value="SEARCH" /></a>-->
<div id="slidingDiv" class="centre">

<table width="50%" border="0" cellspacing="1" cellpadding="2" align="center">
  <tr>
	<th width="40%"><strong>Warehouse</strong></th>
    <td width="10%">
	<?php
	if($_SESSION['AssignedBranch']!='')
	  {	  	
  $ss="Select * from storetype where CompanyId='".$_SESSION['ACGCompanyId']."' and Store_Site in ('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select * from storetype where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }?>
	 <select name="store" onchange="showmrn(this.value);"> 
    <option  value="0">Select Warehouse</option>
	<?php 
	
		while ($myrow=DB_fetch_array($site_sql)){
 				echo '<option Value="' . $myrow['id'] . '">' . $myrow['StoreName'].'</option>';
 		}
       }

	?> </select></td>
	<th width="40%"><strong>GRN Number</strong></th>
    <td width="10%">
	<div id="mrnno1">
	<select  name="grn_no">
	<option  value="000">Select GRN Number</option>
	<?php 
		$sql = "SELECT dis_no FROM log_srndispatch";
		$resultStkLocs = DB_query($sql,$db);
		while ($myrow=DB_fetch_array($resultStkLocs)){
 				echo '<option Value="'.$myrow['dis_no'].'">' . $myrow['dis_no'].'</option>';
 		
       }

	?> </select>
	</div>
	
	</td></tr>
    <tr>
    <th scope="col"><strong>Vendor</strong></th>	
    <td>
	   <div id="vender_codedata">
		<select name="vendor" id="vendor" onchange="showvendor(this.value)">
		<option value="0">---Select---</option>-->
		<?php 
		$vendor_sql=DB_query("Select id,vendor_code,vendor_name from suppliers",$db);
		while($vendor_res=DB_fetch_array($vendor_sql))
		{?>
		<option value="<?php echo $vendor_res['id'];?>"><?php echo $vendor_res['vendor_code'];?></option>
		<?php } ?>
		</select>
		</div>
	</td>
	<th scope="col"><strong>Vendor Name</strong></th>
	<td>
	<div id="venderdata">
	<select name="vendor_name" id="vendor_name" onchange="showvendorcode(this.value)">
		<option value="000">---Select---</option>-->
		<?php 
	$vendor_sql=DB_query("Select id,vendor_code,vendor_name from suppliers",$db);
		while($vendor_res=DB_fetch_array($vendor_sql))
		{?>
		
		<option value="<?php echo $vendor_res['id'];?>"><?php echo  str_replace(array("M/s ","M/S"),'',$vendor_res['vendor_name']); ?></option>
		<?php } ?>
		</select>
		</div>
		</td>
  
    </tr>	
	<tr>
	<th><strong>From Date</strong></th>
	<td><input type="text" name="fromdate" class="datepicker"  size="12" /></td>
	<th><strong>To Date</strong></th>
	<td><input type="text" name="todate" class="datepicker"  size="12"  /></td>
	</tr>
  </table>
  <br />
  <table width="30%" border="0" cellspacing="1" cellpadding="2" align="center">
</table>
  <p align="center"><input name="Search" type="submit" value="Search" /></p>
</div>
	<br /><br />
	<br />
	<?php
	
	if($store=='0' || $_REQUEST['dis_no']=='0')
	{
	 	$SQL="select * from log_srndispatch";
	}
	   if(isset($_POST['Search']))
	   {
	   unset($_SESSION['filter1']);	  
		 $SQL="select * from log_srndispatch";
		
		//echo $_REQUEST['MRNnumber'];
		
		
        $MRnumber=$_REQUEST['dis_no'];
		$store=$_REQUEST['store'];
		$vendor=$_REQUEST['vendor'];
		$vendor_name=$_REQUEST['vendor_name'];
		$fromdate=ConvertSQLDate($_POST['fromdate']);
	    $todate=ConvertSQLDate($_POST['todate']); 
	    $filter=array();
		if($fromdate!='//' ||$todate!='//')
		{
			 $filter[]="dis_date BETWEEN '".$fromdate."' AND '".$todate."'";
		}
		if($store!='0')
		{
			$filter[]="store='".$store."'";
		}
		if($MRnumber!='0')
		{
			$filter[]="dis_no ='".$MRnumber."'";
		}
		if($vendor!='0')
		{
			$filter[]="vendor='".$vendor."'";
		}
		
		
		if(isset($filter))
		{
		 if(count($filter)>0)
		
			{
			$SQL=$SQL." where ".implode(" and ",$filter);
		    $_SESSION['filter1']="where "  . implode(" and ",$filter);
			}
		else
			{
				 $SQL = "SELECT * FROM log_srndispatch where  companyID='".$_SESSION['ACGCompanyId']."' and Cancel_Status='N'
				  order by id DESC";
	        }
			
		}
		
	}
else 
{   
if($_SESSION['AssignedBranch']!='')
{
$SQL = "SELECT * FROM log_srndispatch where companyID='".$_SESSION['ACGCompanyId']."' and Cancel_Status='N' and site in ('".$_SESSION['AssignedBranch']."') order by id DESC";
}
else
{
 $SQL = "SELECT * FROM log_srndispatch where companyID='".$_SESSION['ACGCompanyId']."' and Cancel_Status='N' and site in ('".$_SESSION['AssignedBranch']."')  order by id DESC";
}   
		   }
		  if(isset($_SESSION['filter1']))
		  {
		  $SQL="select * from log_srndispatch".$_SESSION['filter1'];
		  } 
     
	// echo $SQL;

        $ErrMsg = _('No orders were returned by the SQL because');
		$purchordersResult = DB_query($SQL,$db,$ErrMsg);
		$resultpaging = DB_query($SQL, $db, $ErrMsg);
		/*show a table of the orders returned by the SQL */
	?>
	<table border="1" cellpadding="3" cellspacing="0" width="100%">
      <tr>
        <th><strong>SRN DIS No</strong></th>
        <th><strong>SRN DIS Date</strong></th>
		<th><strong>Type</strong></th>
		<th><strong>Site </strong></th>
        <th><strong>Warehouse </strong></th>
        <th><strong>Created by</strong></th>
        <th><strong>View</strong></th>
        <th><strong>Edit</strong></th>
        <th><strong>Delete</strong></th>
      </tr>
      <?php 
	  
	    //echo $SQL;
		$j = 1;$i=1;
		$k=0;
		if(DB_num_rows($purchordersResult)>0){
		
			$rpp = 10; // results per page
			$adjacents = 5;
			
			$page = intval($_GET["page"]);
			if($page<=0) $page = 1;
			
			$reload ="index.php?route=LOG/transaction/srndispatch_note_index".$rurl;
			
			
			// select appropriate results from DB:
			
			
			//$sql = "SELECT * FROM sub_preparation";
			$result =$purchordersResult;
			
			// count total number of appropriate listings:
			$tcount = DB_num_rows($result);
			
			// count number of pages:
			$tpages = ($tcount) ? ceil($tcount/$rpp) : 1; // total pages, last page number
			
			$count = 0;
			$i = ($page-1)*$rpp;
			
			while(($count<$rpp) && ($i<$tcount)) {
				DB_data_seek($result,$i);
				$query = DB_fetch_array($result);
					@extract($query);
				   	
				  
				
				
				
		//if($Cancel_Status!='Y')
		//{
		
		if($Cancel_Status=='Y')
		{
		$cancel_status="Cancelled";
		}
		else
		{
		$cancel_status="";
		}
		
		
		
		
			if ($k==1){/*alternate bgcolour of row for highlighting */ ?>
      <tr class="EvenTableRows">
        <?php	$k=0;
			} else {?>
      </tr>
      <tr class="OddTableRows">
        <?php 		$k++;
			}
			
				$modification = '<a href="'.$url_edit.'&ModfingOnly=1&mrnid='.$id.'&page='.$_REQUEST['page'].'">' . "<img src='images/icon-edit.png' height='15' width='15'>". '</a>';
				$view = '<a href="'.$url_view.'&ViewingOnly=1&mrnid='.$id.'">' . "<img src='images/search.gif' height='15' width='15'>". '</a>';
				$delete = '<a href="' . $rootpath . 'LOG/transaction/goods_receipt_note_index.php?' . SID . '&orderno=' . $order_no.'&DeleteOnly=1&mrnid='.$id.'" onclick="return confirm("Are You Sure You Want To Delete This Record?")">' . "<img src='images/icon-edit.png' height='15' width='15'>". '</a>';
			
			
			$site_query=DB_query("Select locationname from locations where loccode='".$site."'",$db);
			$site_row=DB_fetch_array($site_query);
			
			
			
			
			/********************************** Check Status of items **************************************/
			
			       
				   
				    
                   $flag=0;
                
				 //echo "select * from dbl_addmrnqty where issue_id='".$id."'";
		          $itemdata=DB_query("select * from log_addmrnqty where grn_id='".$id."' and reciveqty!='0.00'",$db);
				  while($itemid1=DB_fetch_array($itemdata))
				  { 
				   
				 		
				  
					    $receiveqty=$itemid1['reciveqty'];
					
					
					if($privious_open<$receiveqty)
					{
					$flag=1;
					}
					
				  }
			
			
		     
			  if($cancel_status=='' && $flag==1)
			  {
			  $cancel_status="Can Not Cancel";
			  }
			
			
			
			$site=$site_row['locationname'];
			
			
			/************************************************************************************************/
					
			
		?>
      </tr>
      <tr bgcolor="#FFFFFF">
        <td><?php echo $dis_no;?></td>
        <td><?php echo ConvertSQLDate($dis_date);?></td>
		<td><?php echo "SRN";?></td>
		<td><?php echo $site;?></td>
        <td><?php echo $s->getField('storetype','id',$store,'StoreName');?></td>
        <td><?php echo $username=$s->getField('www_users','id',$created_by,'realname'); ?></td>
        <td width="4%">
		<?php if($_SESSION['frmView']=='1'){echo $view;}else{echo "No Permission";}?></td>
        <td width="4%"><?php if($_SESSION['frmEdit']=='1'  && $cancel_status!='Cancelled')  { 
			echo $modification;}else{echo "No Permission";}?>
			
			</td>
        
        <td>
		<?php if($_SESSION['frmDelete']=='1'){?>
            <a href="index.php?route=LOG/transaction/srndispatch_note_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&mrnid=<?php echo $id;?>&action=Cancel&site=<?php echo $site; ?>&store=<?php echo $store ; ?>" onclick="return confirm('Are You Sure You Want To Cancel?')"><img src="images/Icon_Delete.png" height="15" width="15"></a>
			<?php }else{?>
		<?php echo 'No Permission'; ?>
		<?php }?>
			
			
			</td>
      </tr>
      <?php 
			$j++;
			If ($j == 12){
				$j=1;
				 echo $TableHeader;
			}
		//end of page full new headings if
		//}
		$i++;  $count++;			}
		echo paginate_two($reload, $page, $tpages, $adjacents);
		}
		else
		{?>
      <tr>
        <td colspan="10"><h3 align="center"><strong><font color="#FF0000">No Record Found!</font></strong></h3></td>
      </tr>
      <?php }
		//end of while loop ?>
    </table>
	<input type="hidden" name="ReqNo" value="<?php echo $_POST['ShowRequisition'];?>">
	<?php 
	echo "<script>defaultControl(document.forms[0].StockCode);</script>"; ?>
	</form>
	</div>
	</div>
	</div>
<?php 
}

	
	
	
	echo '<input type="hidden" name="ReqNo" value="'.$_POST['ShowRequisition'].'">';
	echo "<script>defaultControl(document.forms[0].StockCode);</script>";
	echo '</table>';
	echo '</form>';
  
include('includes/footer.inc');
?>
<link rel="stylesheet" href="css/jquery.ui.all.css" />
<script src="js/jquery-1.5.1.js"></script>
<script src="ui/jquery.ui.core.js"></script>
<script src="ui/jquery.ui.widget.js"></script>



<script>
$(function() {
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
		changeYear: true,
		yearRange: '-80:+20'});
});
</script>


<script>

function showmrn(str)
{

if (str.length==0)
  		{
  			document.getElementById("mrnno1").innerHTML="";
  			return;
  		}
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("mrnno1").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","mrnnoshow.php?site="+str,true);
		xmlhttp.send();


}


function showvendor(str)
{

if (str.length==0)
  		{
  			document.getElementById("venderdata").innerHTML="";
  			return;
  		}
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("venderdata").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","mrnvendorshow.php?site="+str,true);
		xmlhttp.send();






}



function showvendorcode(str)
{

if (str.length==0)
  		{
  			document.getElementById("vender_codedata").innerHTML="";
  			return;
  		}
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("vender_codedata").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","mrnvendorcodeshow.php?site="+str,true);
		xmlhttp.send();






}

function open_box(str)
{
window.open("mrn_boxes_for_logistic1.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&grnid="+str,"item_popup_window","height=500,width=800,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
</script>


<?php




if(isset($_SESSION['success_msg']))
{ ?>
	<script> alert("<?php echo ($_SESSION['success_msg']);?>");</script>
<?php 
}
unset($_SESSION['success_msg']);
?>