<script>
function showsitename160(str) 
{
//alert(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)
    {
	document.getElementById("storedata7").innerHTML=xmlhttp.responseText;
	 }
  }
  xmlhttp.open("GET","ajax_get_store_location.php?branch="+str,true);
	xmlhttp.send();

//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);
	
}
</script>

<script>
function validate_search()
{
	var branch = $('#branch').val();
	var storedata7 = $('#storedata7').val();
	if(branch == "")
	{
		alert("Select Branch");
		return false;
	}
	return true;
}
</script>

<?php
include('POModule/Class-ItemMaster.php');

$first_date=date('01/m/Y');
$cur_date=date('d/m/Y');

//For hmis User
$sql_user_store="select hmis_storeid,StoreCode from www_users left join storetype on storetype.id=www_users.hmis_storeid where www_users.id='".$_SESSION['UserID']."'";
$result_user_store=DB_query($sql_user_store,$db);
$rs_user_store=DB_fetch_array($result_user_store);
$main_store=$rs_user_store[0];


$sql_other_store="select store_id,StoreCode from www_users_dtls left join storetype on storetype.id=www_users_dtls.store_id where user_id='".$_SESSION['UserID']."'";
$result_other_store=DB_query($sql_other_store,$db);
while($rs_other_store=DB_fetch_array($result_other_store))
{
  $store[]=$rs_other_store['store_id'];
}
if(!empty($store))
{
  $get_store_id=implode(",",$store);
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Physical Adjustment Register</h1>
<div align="center">
<form action="" method="post">
<table width="100%"  border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
  	<td width="13%" align="right">Branch</td>
  	<td width="20%"><select name="branch" id="branch" onchange="showsitename160(this.value);" >
  			<option value="">Select Branch</option>
  			<?php
			
				$ss1="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 		$site_sql1=DB_query($ss1,$db);
	  		
  			while($siteres=DB_fetch_array($site_sql1))  	
		 	{
		 	?>
		 		<option value="<?php echo $siteres['loccode'];?>" <?php echo($_REQUEST['branch']==$siteres['loccode']?'selected':'')?>>
					<?php echo $siteres['locationname'];?>
				</option>
			
			<?php
		 	}	 
		 	?>
  		</select></td>
	<td width="13%" align="right">From Date</td>
    <td width="20%"><input name="fromdate"  type="text" id="fromdate" class="datepicker" value="<?php if($_REQUEST['fromdate']!='') { echo $_REQUEST['fromdate']; } 
		   else { echo $first_date; }  ?>"  onchange="javascript:checkdate(this.value)" readonly="true" ></td>
	<td width="13%" align="right">To Date</td>
    <td><input name="todate"  class="datepicker"type="text" id="todate" value="<?php if($_REQUEST['todate']!='') { echo $_REQUEST['todate']; } else { echo $cur_date; } ?>"  onchange="javascript:checkdate(this.value)" class="datepicker" readonly="true" >
		<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">Store Location</td>
    <td ><select name="ItemList" id="storedata7" >
     <?php 
        if($_SESSION['UserType']!='SuperAdmin')
        {
          if($get_store_id!='')
          {
               $query = "select * from storetype where  id in(".$main_store.",".$get_store_id.") order by StoreCode ASC";
          }
          elseif ($main_store!='')
         {
  	          $query = "select * from storetype where id in(".$main_store.") order by StoreCode ASC";
         } 
       }  
         else
         {
         	$query = "select * from storetype order by StoreCode ASC";
         }
  ?>
  		<option value="">select Location</option>
			 <?php 
			 	$res = DB_query($query,$db) or die(DB_error());
				while($result = DB_fetch_array($res))
				{
				$p='';
				   //echo $result['id']."##".$_REQUEST['ItemList'];

				/* if($result['id'] == $_REQUEST['ItemList'])
				{
					$p="selected";
				} */
			?>
			<option value="<?php echo $result['id'];?>" <?php if($_REQUEST['ItemList']==$result['id']) { ?> selected="selected" <?php } ?> ><?php echo $result['StoreName'];?></option>
			<?php
			}
			?>
        </select></td>
  	 <td align="right">PSA No.</td>
    <td><input name="ADJ_voucher_no" type="text" value="<?php  echo $_REQUEST['ADJ_voucher_no'];?>" ></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" style="cursor:pointer;" onclick="return validate_search();"></td>
   </tr>
</table>
</form>
<?php 
 if($_REQUEST['search']){
$filter=array();

		$fromDate=$_REQUEST['fromdate'];
		$fromDateBrk=explode("/",$fromDate);
		$fromDate=$fromDateBrk[0];
		$fromMonth=$fromDateBrk[1];
		$fromYear=$fromDateBrk[2];
		$From=$fromYear."-".$fromMonth."-".$fromDate;
		
		$toDate=$_REQUEST['todate'];
		$toDateBrk=explode("/",$toDate);
		$toDate=$toDateBrk[0];
		$toMonth=$toDateBrk[1];
		$toYear=$toDateBrk[2];
		$To=$toYear."-".$toMonth."-".$toDate;	
		//echo $_REQUEST['branch'];
		//echo $_REQUEST['ItemList'];
		//echo $_REQUEST['fromdate'];
		//echo $_REQUEST['todate'];
		
		if($_REQUEST['branch']!='')
		$filter[]="physical_stock.site='".$_REQUEST['branch']."'";
		
		if($_REQUEST['ItemList']!='')
		$filter[]="physical_stock.store='".$_REQUEST['ItemList']."'";

		if($_REQUEST['ADJ_voucher_no']!='')
		$filter[]="physical_stock.ADJ_voucher_no like '%".$_REQUEST['ADJ_voucher_no']."%'";
			
		if($_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
		$filter[]="physical_stock.date1 between '".$From."' and '".$To."'";
	
	    $sitename=$s->getField('locations','loccode',$_REQUEST['branch'],'locationname');
		$from=$_REQUEST['fromdate'];
		$to=$_REQUEST['todate'];
		$storeNames=$s->getField('storetype','id',$_REQUEST['ItemList'],'StoreName');
		
		if($_REQUEST['ptype']=="1") { $purchType = 'Local'; } elseif($_REQUEST['ptype']=="2") { $purchType = 'Import'; } elseif($_REQUEST['ptype']=="3") { $purchType = 'FOC'; }
		
		if($_REQUEST['branch']!='' && $_REQUEST['vendor']!='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='' && $_REQUEST['storecode']!='')
	   {
	$message="Showing Records of Branch ".$sitename."  , vendor '".$sitename1."', Warehouse '".$storeNames."' From  ".$From." to ".$To."";
	}
	else if($_REQUEST['branch']!='' && $_REQUEST['vendor']=='' && $_REQUEST['fromdate']=='' && $_REQUEST['todate']=='' && $_REQUEST['storecode']=='')
	{
	$message="Showing Records of Branch ".$sitename." ";
	
	}
	else if($_REQUEST['branch']=='' && $_REQUEST['vendor']=='' && $_REQUEST['storecode']=='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	{
	$message="Showing Records   From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['branch']==''  && $_REQUEST['storecode']=='' && $_REQUEST['vendor']!='' && $_REQUEST['fromdate']=='' && $_REQUEST['todate']=='')
	{
	$message="Showing Records  of vendor   ".$sitename1." ";
	}
	else if($_REQUEST['branch']!='' && $_REQUEST['vendor']!='' && $_REQUEST['storecode']!='' && $_REQUEST['fromdate']=='' && $_REQUEST['todate']=='')
	{
	$message="Showing Records  of Branch ".$sitename.", Warehouse '".$storeNames."' and vendor '".$sitename1."'";
	
	}
	else if($_REQUEST['branch']=='' && $_REQUEST['vendor']!='' && $_REQUEST['storecode']!='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	{
	$message="Showing Records  of  vendor '".$sitename1."' From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['branch']=='' && $_REQUEST['vendor']!='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='' && $_REQUEST['storecode']!='')
	{
	$message="Showing Records  of vendor '".$sitename1."', Warehouse '".$storeNames."' From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['branch']!='' && $_REQUEST['vendor']=='' && $_REQUEST['storecode']=='' && $_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
	{
	$message="Showing Records  of  Branch ".$sitename."  From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['branch']!='' && $_REQUEST['vendor']=='' && $_REQUEST['storecode']==''  && $_REQUEST['fromdate']=='' && $_REQUEST['todate']=='')
	{
	$message="Showing Records  of   Branch ".$sitename."";
	}
	
	//$_SESSION['mess']=$message;
		
		if($filter != "")
		{
		 $filter1 = implode(" and ",$filter);
		}
		if($filter1!='')
		{	
			$sql = "select * from physical_stock where $filter1 and physical_stock.CompanyId='".$_SESSION['ACGCompanyId']."' group by physical_stock.id";
			
	 	} 
	 }
	
	$_SESSION['mess2']=$message;
	$_SESSION['period']="From"."&nbsp".$_REQUEST['fromdate']."&nbsp;"."to"."&nbsp".$_REQUEST['todate'];
      
       $branch=$s->getField('locations','loccode',$_REQUEST['branch'],'locationname');
       $_SESSION['branch']="Branch:".$branch;
?>
<?php if($_REQUEST['search']!=''){?>

<p align="center"><strong><?php echo $message; ?></strong> <a target="_blank" href="stockadjustment_print.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=Report&id=<?php echo $myrow['id']; ?>" class="search" style="float:right;">Print</a>
<!--<a href="tcpdf/Pdf_POReport.php?loccode=<?php echo $_REQUEST['site'];?>&itemname=<?php echo $_REQUEST['ItemList'];?>&todate=<?php echo $_REQUEST['todate'];?>&fromdate=<?php echo $_REQUEST['fromdate'];?>">PDF</a>
<a href="Export_POReport.php?loccode=<?php echo $_REQUEST['site'];?>&itemname=<?php echo $_REQUEST['ItemList'];?>&todate=<?php echo $_REQUEST['todate'];?>&fromdate=<?php echo $_REQUEST['fromdate'];?>">Export</a>--></p>

<h3>Product Details</h3> 
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
  <tr>
     <th width="3%">S. No.</th>  
	 <th width="11%">PSA No.</th>
     <th width="6%">Adjst. Date</th>     
	 <th width="110">Part Code</th>
     <th width="280">Part Name</th>
     <th width="80">Batch No.</th>
     <!--<th width="100">Size</th>
     <th width="80">Grade</th>
	 <th width="40">UOM</th>-->
	 <th style="text-align:center;">Adjst. Qty.</th> 
     <th width="70">Adjst. Type</th>
	 <th width="140">Remark</th>	
	 <th width="10%">Created By</th>  
  </tr>
 
   <?php

	 $i=1;
	  $_SESSION['phy_sql_query']=$sql;
	 $res = DB_query($sql,$db);
	if(DB_num_rows($res)>0)
	{
  	 while($result = DB_fetch_array($res))
	 {
	  // echo '<pre>';print_r($result);
	 ?>
   <tr>
<td><?php echo $i++;?></td>
<td><?php  echo $result['ADJ_voucher_no'];?></td>
<td><?php  echo $result['date1']; ?></td>
<td colspan="6" style="padding:0px;" valign="top">
<div id="amc-tab">
	<table width="100%" border="0" cellpadding="3" cellspacing="0">
<?php   
		$item_query = "select * from physical_stock_details where physical_stock_id='".$result['id']."'";
		$item_res = DB_query($item_query,$db) or die(DB_error());
		while($item_result = DB_fetch_array($item_res))
		{
			$item_query_1 = "select * from item_master where id='".$item_result['mstitemid']."'";
			$item_res_1 = DB_query($item_query_1,$db) or die(DB_error());
			while($item_result_1 = DB_fetch_array($item_res_1))
			{
	?>
	<tr>
		<td align="left" valign="top" width="110"><?php echo $item_result_1['StockID'];?></td>
		<td align="left" valign="top" width="280"><?php  echo $item_result_1['item_name']; ?></td> 
		<td align="left" valign="top" width="80"><?php  echo $item_result['batch_no']; ?></td>
		<!--<td align="left" valign="top" width="100"><?php  //echo $s->getField('size_master','id',$item_result['size_id'],'size');   ?></td> 
		<td align="left" valign="top" width="80"><?php //echo $s->getField('product_grade','id',$item_result['grade_id'],'grade');  ?></td> 
		<?php 
			//$query2 = "select * from unitsofmeasure where unitid='".$item_result_1['mst_unitid']."'";
			//$res2 = DB_query($query2,$db) or die(DB_error());
			//$result2 = DB_fetch_array($res2);
		?>
		<td align="left" valign="top" width="40"><?php  echo $result2['unitname'];?> </td>-->
		<td align="left" valign="top" style="text-align:right" width="80"><?php  echo number_format($item_result['adjustment_qty'],2); ?></td>
		<td width="50" valign="top"><?php echo $s->getField('unitsofmeasure','unitid',$item_result['uom'],'unitname');   ?></td>
		<td valign="top" style="text-align:right" width="80"><?php  echo number_format($item_result['stock_qty2'],2); ?></td>
		<td valign="top"><?php echo $s->getField('unitsofmeasure','unitid',$item_result['uom1'],'unitname');   ?></td>	
		<td align="left" valign="top" width="70"><?php  echo $item_result['adjustment_type']; ?> </td> 
		<td width="140" align="left" valign="top"><?php   echo $item_result['remarks']; ?></td> 
	</tr> 
	<?php } }  
         
	?>
</table>
</div>
</td> 
<td><?php echo $s->getField('www_users','id',$result['createdBy'],'realname'); ?></td>
  </tr>    
  <?php } } else {

        	?>
         <tr>
         	 <td colspan="9">There are no records found</td>
         </tr>
        	<?php
        } 
   
  ?>
  
    <?php if($numrow1>0) { ?>
   <?php if(!empty($a)) { ?>
   

      <?php } } else { ?>  
      <?php } ?>   
</table>
 <? } ?>

</div>
</div>

 <script type="text/javascript">
	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>
