<link rel="stylesheet" href="css/new_mxerp.css">
<?php
$PageSecurity = 11; 
unset($_SESSION['query']);

$first_date=date('01/m/Y');
$cur_date=date('d/m/Y');
?>
<script language="javascript" type="text/javascript">

function get_itemsubgroup(str)
{
	if(str=="")
  {
  document.getElementById("sub_id").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("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?main="+str,true);
xmlhttp.send();
}
function get_item(str)
{
if(str=="")
  {
  document.getElementById("itemid").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("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?sub="+str,true);
xmlhttp.send();
}
function showItems(str)
{
    if(str=="")
  {
  document.getElementById("itemid").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("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_inventory.php?partName="+str,true);
xmlhttp.send();
}

function Get_oderdata(str)
{
	
	

/*var xmlhttp;
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)
    {
    var data=xmlhttp.responseText.split("__");	
	

	document.getElementById("jobcardno").innerHTML=data[0];
	
	
    }
  }
  
xmlhttp.open("GET","get_orderdata9.php?salid="+str,true);
xmlhttp.send();	*/
	
}

function get_store_by_site()
{
	$.ajax({
						url : "get_mrn_by_site.php",
						type : "POST",
						data : {
							action:'getStore',
							site:$('#site').val()
						},
						success : function(data) {//alert(data);
		                $('#store').html(data);
												 }
					       });
}
</script>
<div class="body_contain">
<div class="tab_div">

<h1>Finished Material Received Register</h1>
<form method="post" action="">

   <table width="100%" border="0" cellspacing="0" cellpadding="3">
      <tr class="row1">
        <td width="13%" align="right"><strong>Branch:</strong></td>
        <td width="20%">
		<?php
	
		 $ss1="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql1=DB_query($ss1,$db);
	  
	?>		
	  <select name="site" onchange="get_store_by_site()" id="site" style="width:100%;">
            <option value="">Select Site</option>
            <?php 
		
		while($site_row = DB_fetch_array($site_sql1))
		{
			$p='';
			if($site_row['loccode'] == $_POST['site'])	
			$p="selected";
		?>
            <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
            <?php } ?>
          </select>        </td>
       <td width="13%" align="right"><strong>From Date:</strong></td>
        <td width="20%"><input type="text" name="from_date" id="fromdate" class="datepicker" value="<?php if(isset($_REQUEST['from_date'])) { echo $_REQUEST['from_date']; } else { echo $first_date; } ?>"  onchange="javascript:checkdate(this.value)"   style="width:100%;"/></td>     
        <td width="13%" align="right"><strong>To Date:</strong></td>
        <td><input type="text" name="to_date" id="todate" value="<?php if(isset($_REQUEST['to_date'])) { echo $_REQUEST['to_date']; } else { echo $cur_date; }?>"  onchange="javascript:checkdate(this.value)" class="datepicker" style="width:100%;"/>
        	<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" id='from'>
		<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" id='to'>
        </td>       
  </tr>
  <tr class="row1">
        <td align="right"><strong>Job Card:</strong></td> 
        <td ><select name="jobcardno" id="jobcardno" style="width:100%;">
          <option value="0">Select Site</option>
          <?php 
		$sql=DB_query("select * from job_card where CompanyId='".$_SESSION['ACGCompanyId']."' and site in('".$_SESSION['AssignedBranch']."')",$db);
		while($site_row2 = DB_fetch_array($sql))
		{
			$p='';
			if($site_row2['id'] == $_POST['jobcardno'])	
			$p="selected";
		?>
          <option value="<?php echo $site_row2['id']; ?>" <?php echo $p;?>><?php echo $site_row2['job_card_no']; ?></option>
          <?php } ?>
                </select></td>
          <td align="right"><strong>Store:</strong></td>
    <td >
	<?php
	     $sql_store ='select distinct StoreCode, StoreName from storetype';
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select name="store" id="store" style="width:100%;">
        <option value="0">Select</option>
        <?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{?>
        <option value="<?php echo $myrow['StoreCode'];?>"<?php echo($_REQUEST['store']==$myrow['StoreCode']?'selected':'')?>><?php echo $myrow['StoreName'];?></option>
        <?php 
		}
		?>
      </select></td> 
      <td align="right"><strong>FPR No.:</strong></td>
        <td><input type="text" name="production_slip_no" value="<?php echo
		 $_POST['production_slip_no'];?>" style="width:100%;"/></td>
           
     </tr>
     <tr class="row1">
     	<td align="right"><strong>W/O:</strong></td>
        <td><select name="workorder" id="workorder" onchange="Get_oderdata(this.value)" style="width:100%;">  
					   <option value="0">-Select-</option>	
					   <?php 
					   $sql=DB_query("select * from prd_wo where  companyid ='".$_SESSION['ACGCompanyId']."' and site in('".$_SESSION['AssignedBranch']."')",$db);
					   while($row1=DB_fetch_array($sql))
					   {					   
					   ?>
					  	 <option value="<?php echo $row1['id']; ?>" <?php if($_POST['workorder']==$row1['id']) { ?> selected="selected" <?php } ?>><?php echo $row1['wo_no']; ?></option>
					   	<?php } ?>			   
					   </select>       </td>
       	<td colspan="4">&nbsp;</td>
     </tr>
     <tr class="row1">
     	<td colspan="6" style="text-align:center;"><input type="submit" name="view" value="Search" class="search"> <input type="submit" name="cancel" value="Cancel" class="search"></td>
     </tr>
    </table>

    
 
</form>
<?php 
	if($_POST['view'])
{   $flag=0; 
	$fordate = $_POST['from_date'];
	$todate = $_POST['to_date'];
	$fromdate = explode("/", $fordate);
	$daydate = $fromdate['0'];
	$Monthdate = $fromdate['1'];
	$yeardate = $fromdate['2'];
	$fdate = $yeardate."-".$Monthdate."-".$daydate;
					   
	$tredate   = explode("/", $todate);
	$dayda     = $tredate['0'];
	$Monthda   = $tredate['1'];
	$yearda    = $tredate['2'];
	$toyrdate  = $yearda."-".$Monthda."-".$dayda;
	$filter = array();
	
	if(isset($_REQUEST['site']) && $_REQUEST['site'] != '')
  	$filter[]="finished_recieved.site='".$_REQUEST['site']."' ";
    if(isset($_REQUEST['store']) && $_REQUEST['store'] != 0)
  	$filter[]="finished_recieved.store='".$_REQUEST['store']."' ";
	if(isset($_REQUEST['workorder']) && $_REQUEST['workorder'] != '0')
	$filter[]="finished_recieved.work_order_no='".$_REQUEST['workorder']."' ";
	if(isset($_REQUEST['jobcardno']) && $_REQUEST['jobcardno'] != '0')
	$filter[]="finished_recieved.job_card_no='".$_REQUEST['jobcardno']."' ";
    if($_REQUEST['production_slip_no']!="")
	$filter[]="finished_recieved.production_slip_no like '%".$_REQUEST['production_slip_no']."%' ";
	
	if(isset($_REQUEST['from_date']) && $_REQUEST['from_date'] != '' && isset($_REQUEST['to_date']) && $_REQUEST['to_date'] != '')
 	$filter[]="finished_recieved.recieved_date between '".$fdate."' and '".$toyrdate."'  ";
	
	if($filter != '')
	{
	 $filter1 = implode(" and ",$filter);//}
	}
	$sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
	$from=$_REQUEST['from_date'];
	$to=$_REQUEST['to_date'];
	if($_REQUEST['site']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records of Branch ".$sitename."   From  ".$from." to ".$to."";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records of Branch ".$sitename." ";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records   From  ".$from." to ".$to."";
	
	}
	$_SESSION['mess']=$message;

	  $_SESSION['period']="From"."&nbsp".$_REQUEST['from_date']."&nbsp;"."to"."&nbsp".$_REQUEST['to_date'];
      
       $branch=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
       $_SESSION['branch']="Branch:".$branch;



	if($filter1!='')
 	$sql1 = "select * from finished_recieved  where $filter1";
	
	$_SESSION["sql_fmr"]=$sql1;
	$result1 = DB_query($sql1,$db);
	$k=1;
?>
<div align="center"><strong><?php echo $message; ?></strong> </div>
<p align="right"><a href="finish_material_received_print.php?site=<?php echo $_REQUEST['site'];?>&store=<?php echo $_REQUEST['store']?>&fromdate=<?php echo $_REQUEST['from_date']?>&todate=<?php echo $_REQUEST['to_date']?>&workorder=<?php echo $_REQUEST['workorder'] ?>&jobcardno=<?php echo $_REQUEST['jobcardno']; ?>" target="_blank" class="search">Print</a></p>
<table width="100%" border="1" cellspacing="0" cellpadding="3" style=" border:#808080 1px solid;">
    <tr>
      <th width="8%">FPR No.</th>
      <th width="7%">Receive Date</th>
	  <th width="7%">W/O No.</th>
      <th width="7%">Job Card No.</th>
	  <th width="70">Item Code</th>
	  <th width="300">Item Name</th>
	  <th width="70">UOM</th>
	  <th width="100">Batch No.</th>
	  <th width="100">Received Qty.</th>
	  <th>Process</th>
  </tr>
<?php 
	if(DB_num_rows($result1)>0){
	while($row1=DB_fetch_array($result1))
	{
	$flag=1;	
	 ?>
<tr bgcolor="#fff">
      <td><?php echo $row1['production_slip_no'];?></td> 
      <td><?php echo  ConvertSQLDate($row1['recieved_date']);?></td> 
	  <td><?php echo  $s->getField('salesorder','id',$row1['work_order_no'],'orderNo') ;?></td>  
  		<td><?php  
          if($row1['job_w_type']=='Direct')
          {
          	echo 'Direct';
          }
          else
          {
          echo  $vendorname=$s->getField('job_card','id',$row1['job_card_no'],'job_card_no');	
          }
                  
       ?></td>	  
	    <td colspan="6" style="padding:0px;" valign="top">
	    <div id="amc-tab">
		    <table cellpadding="3" cellspacing="0" border="0" width="100%">			    
			     <?php
				 $sql1=DB_query("select *  from finished_recieved_details where finished_recieved_id='".$row1['id']."'",$db);
				  $numrow=DB_num_rows($sql1);
				 while($row=DB_fetch_array($sql1))
				 {		 
				 ?>
				  <tr>
			     	<td width="70"><?php echo $s->getField('item_master','id',$row['mst_itemid'],'StockID'); ?></td>
			     	<td width="300"><?php echo $s->getField('item_master','id',$row['mst_itemid'],'item_name'); ?></td>
                  	<td width="70"><?php echo $s->getField('unitsofmeasure','unitid',$row['uom'],'unitname'); ?></td>
                    <td width="100"><?php echo $row['batch_no']; ?></td>
                  	<td width="100" style="text-align:right"><?php echo number_format($row['reciveqty'],2);$tot_recievedqty[]=$row['reciveqty']; ?></td>
                  	<td><?php  echo $s->getField('production_process_master','id',$row['process'],'manuf_process_name'); ?></td>
				  </tr>				  
				 <?php } ?>
		 </table>
		 </div>
	</td>
    </tr>
	<?php $k++;
	} ?>
<?php }
	else
	{?>
	<tr>
	<td colspan="10"><font color="#FF0000"><strong>No Record Found</strong></font></td>
	</tr>
	<?php }

} ?> 
  </table>
  <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("Date Not greather than "+to);
	       document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=to;
	       
	    }

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

  </script>

  </div>
 </div>