<?php 

session_name('CAKEPHP'); session_start();

$PageSecurity = 2;
if (empty($_GET['identifier'])) {
			 $identifier=date('U');
		} else {
			$identifier=$_GET['identifier'];
		}
$title = _('Material Receipt Note');

$url_cancel= "index.php?route=Inventory/transation/material_receipt_note_index_direct".$rurl;
$url_add= "index.php?route=Inventory/transation/create_direct_grn".$rurl;
$url_GRN_direct= "index.php?route=Inventory/transation/create_direct_grn".$rurl;
$url_edit= "index.php?route=Inventory/transation/material_receipt_note_edit_direct".$rurl;
$url_view= "index.php?route=Inventory/transation/material_receipt_note_edit_direct".$rurl;

?>
<script src="js/jsapi.js" type="text/javascript"></script>
<script type="text/javascript">
google.load("jquery", "1"); 
</script>

<style>
#slidingDiv {
    display: none;
	height:auto;
	padding:0px;
}
</style>	

<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>Goods Receipt Note</h1>	
<p align="left">
<?php if($_SESSION['frmAdd']=='1'){?>
		<a href="<?php echo $url_add;?>"><input type="button" class="search" value="Add Direct GRN" /></a>
		&nbsp;
		<!--<a href="<?php echo $url_GRN_direct;?>"><input type="button" class="search" value="Add Direct GRN" /></a>-->
<?php }?>

<a href="#" onClick="showSlidingDiv(); return false;"><input class="search" type="button" value="Search" /> </a>
</p>

<div id="slidingDiv">
<form method="post" >
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
	<td width="13%" align="right">Branch</td>
    <td width="20%"><?php $ss="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql=DB_query($ss,$db);?>
	 <select name="site" onchange="showmrn(this.value);"> 
    	<option  value="000">Select Site</option>
		<?php while ($myrow=DB_fetch_array($site_sql))
		{
        ?>
 		<option Value="<?php echo $myrow['loccode']; ?>"><?php echo  $myrow['locationname']; ?></option>
 		<?php }	?> </select></td>
	<td width="13%" align="right">GRN Number</td>
    <td width="20%"><select name="MRNnumber" id='mrnnumber'>
	 <option value="000">Select GRN</option>
	<?php 
		$sql = "SELECT  MRNNo FROM addmrn where site in('".$_SESSION['AssignedBranch']."') and Grn_Type='Direct'";
		$resultStkLocs = DB_query($sql,$db);
		while ($myrow=DB_fetch_array($resultStkLocs)){
 				echo '<option Value="'.$myrow['MRNNo'].'">' . $myrow['MRNNo'].'</option>'; 		
       } ?></select></td>
    <!-- <td>Vendor</strong></td> -->
    <!-- <td><div id="vender_codedata">
		<select name="vendor" id="vendor" onchange="showvendor(this.value)">
		<option value="000">---Select---</option>
		<?php 
		$vendor_sql=DB_query("Select id,vendor_code,vendor_name from suppliers where site in('".$_SESSION['AssignedBranch']."')",$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> -->
	<td width="13%" align="right">Vendor Name</td>
	<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 where site in('".$_SESSION['AssignedBranch']."')",$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 class="row1">
	<td align="right">From Date</td>
	<td><input type="text" name="fromdate" id="fromdate" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" onchange="javascript:checkdate(this.value)"  class="datepicker" /></td>	
	<td align="right">To Date</td>
	<td><input type="text" name="todate" id="todate" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>"  onchange="javascript:checkdate(this.value)"  class="datepicker"   /><input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" id='from'><input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" id='to'></td>
	<td colspan="2">&nbsp;</td>
</tr>
<tr class="row1">
	<td colspan="6" style="text-align:center;"><input name="Search" type="submit" value="Search" class="search" /></td>
</tr>
</table>
<br>
</form>
</div>
	<?php
	 
	   if(isset($_POST['Search']))
	   {
	  	
			$SQL="select * from addmrn";
			$MRnumber=$_REQUEST['MRNnumber'];
			$site=$_REQUEST['site'];
			//$vendor=$_REQUEST['vendor'];
			$vendor_name=$_REQUEST['vendor_name'];
			$fromdate=ConvertSQLDate($_POST['fromdate']);
	    	$todate=ConvertSQLDate($_POST['todate']); 
	    	$filter=array();
	    	if($site!='000')
			{
				$filter[]=" site='".$site."'";
			}
			if($MRnumber!='000')
			{
				$filter[]="id ='".$MRnumber."'";
			}
			if($fromdate!='//' ||$todate!='//')
			{
				 $filter[]="MRNDate BETWEEN '".$fromdate."' AND '".$todate."'";
			}
			/*if($vendor!='000')
			{	
				$filter[]="ReceivedFrom='".$vendor."'";
			} */
			if($vendor_name!='000')
			{
				$filter[]="ReceivedFrom='".$vendor_name."'";
			}
			//$filter[]="MRNDate between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."'";
			
		 	if(count($filter)>0)
		 	{
		 		$filter1=implode(" and ",$filter);			   
		 	}
			 if($_SESSION['UserType']=='Dealer')
   			{
   				$SQL="SELECT * FROM addmrn where  companyID='".$_SESSION['ACGCompanyId']."' and $filter1 and Grn_Type='Direct' and createdby='".$_SESSION['UserID']."' and Cancel_Status='N' or Cancel_Status IS NULL  order by id DESC"   ;
   			}
   			else
   			{
   				 if($filter1!='')
   				 {
   				    $SQL="SELECT * FROM addmrn where  companyID='".$_SESSION['ACGCompanyId']."' and $filter1 and Grn_Type='Direct' and Cancel_Status='N' or Cancel_Status IS NULL  order by id DESC" ;
   				  }
   				  else
   				  {
   				  	 $SQL="SELECT * FROM addmrn where  companyID='".$_SESSION['ACGCompanyId']."' and Grn_Type='Direct' and Cancel_Status='N' or Cancel_Status IS NULL  order by id DESC" ;
   				  }  
   			}
			
		}
		else 
		{    if($_SESSION['UserType']=='dealer')
   			{
   				$SQL = "SELECT * FROM addmrn where  companyID='".$_SESSION['ACGCompanyId']."' and site in('".$_SESSION['AssignedBranch']."') and Grn_Type='Direct' and MRNDate between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."' and createdby='".$_SESSION['UserID']."'  and Cancel_Status='N' or Cancel_Status IS NULL  order by id DESC";
   			}
   			else
   			{
			 $SQL = "SELECT * FROM addmrn where  companyID='".$_SESSION['ACGCompanyId']."' and site in('".$_SESSION['AssignedBranch']."') and Grn_Type='Direct' and MRNDate between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."'  and Cancel_Status='N' or Cancel_Status IS NULL  order by id DESC";
   			}
		}?>
<table border="0" cellpadding="3" cellspacing="0" width="100%" class="thborder">

<?php 
	$purchordersResult = DB_query($SQL, $db, $ErrMsg);
	if(DB_num_rows($purchordersResult)>0)
	{?>
		<tr>
			<th width="15%">Branch</th>
			<th width="10%">GRN No.</th>
			<th width="7%">GRN Date</th>
			<th width="10%">Vendor Code</th>
			<th>Vendor Name</th>
			<th width="14%">Created by</th>
			<th width="5%" style="text-align:center;">Edit</th>
			<th width="5%" style="text-align:center;">Print</th>
			<!--//<td><strong>Export</strong></td>-->			
		</tr>
		<?php 
		while ($siterow=DB_fetch_array($purchordersResult)) 
		{?>
			<tr>
				<td><?php echo $s->getField('locations','loccode',$siterow['site'],'locationname'); ?></td>
				<td><?php echo $siterow['MRNNo'];?></td>
				<td><?php echo ConvertSQLDate($siterow['MRNDate']);?></td>
				<td><?php echo $s->getField('suppliers','id',$siterow['ReceivedFrom'],'vendor_code');?></td>
       			<td><?php echo $s->getField('suppliers','id',$siterow['ReceivedFrom'],'vendor_name');?></td>
       		    <td><?php echo $username=$s->getField('www_users','id',$siterow['createdby'],'realname'); ?></td>
       		    <td style="text-align:center;">
        			<?php 
        			$check=checkEdit($siterow['id']);
        			if($check!=0)
        			{
        				if($_SESSION['frmEdit']=='1'  && $cancel_status!='Cancelled')  
        				{ 

							echo $modification = '<a href="index.php?route=Inventory/transation/material_receipt_note_edit_direct&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'&ModfingOnly=1&mrnid='.$siterow['id'].'&page='.$_REQUEST['page'].'">' . "<img src='images/icon-edit.png' height='15' width='15' title='Edit' />". '</a>';
						}
						else
						{
						?><img src='images/icon-edit.png' height='15' width='15' title='No Permission' style="opacity:0.5;" /><?php
						}
					}
					else
					{
						echo "MRN Used";
					}
					?>
				</td>
				 <td style="text-align:center;">
				<?php if($_SESSION['frmView']=='1'){?>
					<a href="material_receipt_print.php?mrnid=<?php echo $siterow['id'];?>" target="_blank"><img src="images/icon_print.jpg" height="20" width="20" title="Print" /></a>
				<?php }else
				{?>
					<img src="images/icon_print.jpg" height="20" width="20" title="No Permission" style="opacity:0.5;" />
		  <?php }?>
				</td>             
			</tr>	
		<?php 
		}
	}
	else
	{?>
		<tr colspan="8" align="left"><td><strong>No Record</strong></td></tr>
	<?php
	}
?>
</table>
</div>
</div>

<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>


<?php 

  function checkEdit($id)
  {
    global $db;
    $query="SELECT id FROM stock_valuation_tbl WHERE against_tra_id in( SELECT id FROM stock_valuation_tbl WHERE TYPE='MRN' AND against_voucher_main_id='".$id."' ) ";
    $result=Db_query($query,$db);
   
    if(DB_num_rows($result)>0)
      return 0;
    else
      return 1;
  }



?>
<script>
function checkdate(date)
{
	var from =document.getElementById('from').value;
	var to =document.getElementById('to').value;
	
		var cdate=date.split("/");
		var sdate=from.split("/");
		var edate=to.split("/");

		cdate[1]=cdate[1]-1;
		sdate[1]=sdate[1]-1;
		edate[1]=edate[1]-1;
			 
		var dateC = new Date(cdate[2], cdate[1], cdate[0]); //Year, Month, Date 
		var dateS = new Date(sdate[2], sdate[1], sdate[0]); //Year, Month, Date
	    var dateE = new Date(edate[2], edate[1], edate[0]); //Year, Month, Date

	    if (dateS > dateE) {
	        alert("Start Date is not greather then Date End.");
	        document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	        
	    }

	    if (dateC > dateE) {
	        alert("Compare Date Not greather than "+to);
	       document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	       
	    }

	    if (dateC < dateS) {
	        alert("Compare Date Not less than "+from);
	        document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	       
	    }	
	
	
}

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


<script>

function showmrn(str)
{
	$.post('showmrn.php',{'site':str},function(data){
		$('#mrnnumber').html(data);
	});
		


}


function showvendor(vendorcode)
{

	$.post('showmrn.php',{'vendorcode':vendorcode,'vendornameshow':'yes'},function(data){
		$('#vendor_name').html(data);
	});
		






}



function showvendorcode(str)
{
	$.post('showmrn.php',{'vendorname':str,'vendorcodeshow':'yes'},function(data){
		$('#vendor').html(data);
	});

		






}


</script>






<?php




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