<link rel="stylesheet" href="css/new_mxerp.css">
<?php 
if($_REQUEST['item_popup']!='')
{
	$_SESSION['site']=$_REQUEST['site'];
	$_SESSION['transferNo']=$_REQUEST['transferNo'];
	$_SESSION['date']=$_REQUEST['date'];
	$_SESSION['voucherType']=$_REQUEST['voucherType'];
	$_SESSION['formNo']=$_REQUEST['formNo'];
	$_SESSION['receivedBy']=$_REQUEST['receivedBy'];
	$_SESSION['transportType']=$_REQUEST['transportType'];
	$_SESSION['vehicleNo']=$_REQUEST['vehicleNo'];
	$_SESSION['fromSite']=$_REQUEST['fromSite'];
	$_SESSION['fromStore']=$_REQUEST['fromStore'];
	$_SESSION['toSite']=$_REQUEST['toSite'];
	$_SESSION['toStore']=$_REQUEST['toStore'];
}
if($_REQUEST['Cancel']!='')
{
	unset($_SESSION['site']);
	unset($_SESSION['transferNo']);
	unset($_SESSION['date']);
	unset($_SESSION['voucherType']);
	unset($_SESSION['formNo']);
	unset($_SESSION['receivedBy']);
	unset($_SESSION['transportType']);
	unset($_SESSION['vehicleNo']);
	unset($_SESSION['fromSite']);
	unset($_SESSION['fromStore']);
	unset($_SESSION['toSite']);
	unset($_SESSION['toStore']);
	unset($_SESSION['TransferArray']);
	
}
if($_REQUEST['Save']!='')
{
	 $insert1="Insert into stock_transfer set
					site='".$_REQUEST['site']."',   
					transferNo='".$_REQUEST['transferNo']."',  
					date='".FormatDateForSQL($_REQUEST['date'])."',    
					voucherType='".$_REQUEST['voucherType']."',  
					formNo='".$_REQUEST['formNo']."',  
					receivedBy='".$_REQUEST['receivedBy']."',  
					transportType='".$_REQUEST['transportType']."',  
					vehicleNo='".$_REQUEST['vehicleNo']."',  
					fromSite='".$_REQUEST['fromSite']."',  
					fromStore='".$_REQUEST['fromStore']."',  
					toSite='".$_REQUEST['toSite']."',  
					toStore='".$_REQUEST['toStore']."',  
					remarks='".$_REQUEST['remarks']."',  
					createdBy='".$_SESSION['UserID']."',  
					createdDate='".date('Y-m-d')."',
					CompanyId='".$_SESSION['ACGCompanyId']."'";
	$result1=DB_query($insert1,$db);
	$lastId=DB_Last_Insert_Id($db,"stock_transfer","id");
	if($lastId!='')
	{
		for($j=0;$j<$_REQUEST['count'];$j++)
		{
			if($_REQUEST['make'][$j]==''){$_REQUEST['make'][$j]='0';}
			if($_REQUEST['grade'][$j]==''){$_REQUEST['grade'][$j]='0';}
			if($_REQUEST['color'][$j]==''){$_REQUEST['color'][$j]='0';}
			if($_REQUEST['size'][$j]==''){$_REQUEST['size'][$j]='0';}
			if($_REQUEST['gsm'][$j]==''){$_REQUEST['gsm'][$j]='0';}	
			$insert2="Insert into stock_transfer_details set
							parentId='".$lastId."',  
							itemCode='".$_REQUEST['itemCode'][$j]."',  
							make='".$_REQUEST['make'][$j]."',    
							grade='".$_REQUEST['grade'][$j]."',   
							color='".$_REQUEST['color'][$j]."',   
							size='".$_REQUEST['size'][$j]."',    
							gsm='".$_REQUEST['gsm'][$j]."',     
							quantity='".$_REQUEST['quantity'][$j]."',  
							purpose='".$_REQUEST['purpose'][$j]."'  ";
			$result2=DB_query($insert2,$db);
		}		
	}
	unset($_SESSION['site']);
	unset($_SESSION['transferNo']);
	unset($_SESSION['date']);
	unset($_SESSION['voucherType']);
	unset($_SESSION['formNo']);
	unset($_SESSION['receivedBy']);
	unset($_SESSION['transportType']);
	unset($_SESSION['vehicleNo']);
	unset($_SESSION['fromSite']);
	unset($_SESSION['fromStore']);
	unset($_SESSION['toSite']);
	unset($_SESSION['toStore']);
	unset($_SESSION['TransferArray']);
echo "<script>location.href='index.php?route=stock_transfer_list&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>
<script language="JavaScript" type="text/javascript">
	function showFromStore(str)
	{
		if(str.length==0)
  		{
  			document.getElementById("fromDiv").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("fromDiv").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","getStoreType.php?q="+str,true);
		xmlhttp.send();
	}
function showToStore(str)
{
	if(str.length==0)
  		{
  			document.getElementById("toDiv").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("toDiv").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","getStoreType.php?p="+str,true);
		xmlhttp.send();
}
function transferCode(str)
{
	if(str.length==0)
  		{
  			document.getElementById("transferNo").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("transferNo").value=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","getStoreType.php?r="+str,true);
		xmlhttp.send();
}
function item_window()
{
	window.open("SelectTransferItems.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=500,width=900,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
	
}
</script>

<div class="body_contain">
<div class="tab_div">

<h1>Unit Wise Stock Transfer</h1>
<form action="" method="post">
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td class="tabledata1">Branch</td>
	
	
    <td><?php
	if($_SESSION['AssignedBranch']!='')
	  {	  	
  $ss="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }
	?>
	
	
	<select name="site" id="site" style="width:100%;" onchange="transferCode(this.value)">
    <option value="0">Select</option>
     <?php 
					while($Row=DB_fetch_array($site_sql)){?>
             <option value="<?php echo $Row['loccode']?>"<?php echo($_SESSION['Site']==$Row['loccode'] ? 'selected':'');?> ><?php echo $Row['locationname']?></option>
                                <?php  } ?>
    </select></td>
    <td class="tabledata1">ST. No.</td>
    <td><input type="text" value="<?php echo $_SESSION['transferNo'];?>" name="transferNo" id="transferNo" style="width:100%;"></td>
  </tr>
  <tr>
    <td class="tabledata1">Date</td>
    <td><input type="text" name="date" id="date" style="width:100%;" value="<?php echo $_SESSION['date'];?>" class="datepicker"></td>
    <td class="tabledata1">Voucher Type</td>
    <td>
      <select name="voucherType" id="voucherType" style="width:100%;">
      	<option value="Stock Transfer">Stock Transfer</option>
      </select>
    </td>
  </tr>
  <tr>
    <td class="tabledata1">Form No.</td>
    <td><input type="text" name="formNo" id="formNo" value="<?php echo $_SESSION['formNo'];?>" style="width:100%;"></td>
    <td class="tabledata1">Received By</td>
    <td><select name="receivedBy" id="receivedBy" style="width:100%;">
    	<?php 
    	$empSql=DB_query("Select id, emp_name from empmaster",$db);
		while($empRow=DB_fetch_array($empSql))
		{
			$s='';
			if($_SESSION['receivedBy']==$empRow['id'])
			$s="selected";
			?>
			<option value="<?php echo $empRow['id'];?>" <?php echo $s;?>><?php echo $empRow['emp_name'];?></option>
		<?php 	
		}
    	?>
    </select></td>
  </tr>
  <tr>
    <td class="tabledata1">Transport Type</td>
    <td>
    	<select name="transportType" id="transportType" style="width:100%;">
    	<?php 
    	$transSql=DB_query("Select * from transport_master",$db);
		while($transRow=DB_fetch_array($transSql))
		{
			$s='';
			if($_SESSION['transportType']==$transRow['id'])
			$s="selected";
			?>
			<option value="<?php echo $transRow['id'];?>" <?php echo $s;?>><?php echo $transRow['TransportName'];?></option>
		<?php 	
		}
    	?>
    </select>
    	</td>
    <td class="tabledata1">Vehicle No.</td>
    <td><input type="text" name="vehicleNo" id="vehicleNo" value="<?php echo $_SESSION['vehicleNo'];?>" style="width:100%;"></td>
  </tr>
  <tr>
    <td class="tabledata1">From Branch</td>
    <td><select name="fromSite" id="fromSite" style="width:100%;" onchange="showFromStore(this.value)">
    	<option value="0">Select</option>
    	<?php 
	$siteSql=DB_query("Select loccode,locationname from locations",$db);
	while($siteRow=DB_fetch_array($siteSql))
	{
		$s='';
		if($_SESSION['fromSite']==$siteRow['loccode'])
		$s="selected";
		?>
    <option value="<?php echo $siteRow['loccode'];?>" <?php echo $s;?>><?php echo $siteRow['locationname'];?></option>
	<?php 	
	}
	?>
    </select></td>
    <td class="tabledata1">From Store</td>
    <td id="fromDiv"><select name="fromStore" id="fromStore" style="width:100%;">
    	<?php 
    	$storeSql=DB_query("SELECT DISTINCT StoreCode, StoreName FROM storetype",$db);
		while($storeRow=DB_fetch_array($storeSql))
		{
			$s='';
			if($_SESSION['fromStore']==$storeRow['StoreCode'])
			$s="selected";
			?>
			<option value="<?php echo $storeRow['StoreCode'];?>" <?php echo $s;?>><?php echo $storeRow['StoreName'];?></option>
		<?php 	
		}
    	?>
    </select></td>
  </tr>
  <tr>
    <td class="tabledata1">To Branch</td>
    <td><select name="toSite" id="toSite" style="width:100%;" onchange="showToStore(this.value)">
    	<option value="0">Select</option>
    	<?php 
	$siteSql=DB_query("Select loccode,locationname from locations",$db);
	while($siteRow=DB_fetch_array($siteSql))
	{
			$s='';
			if($_SESSION['toSite']==$siteRow['loccode'])
			$s="selected";?>
    <option value="<?php echo $siteRow['loccode'];?>" <?php echo $s;?>><?php echo $siteRow['locationname'];?></option>
	<?php 	
	}
	?>
    </select></td>
    <td class="tabledata1">To Store</td>
    <td id="toDiv"><select name="toStore" id="toStore" style="width:100%;">
    	<?php 
    	$storeSql=DB_query("SELECT DISTINCT StoreCode, StoreName FROM storetype",$db);
		while($storeRow=DB_fetch_array($storeSql))
		{
			$s='';
			if($_SESSION['toStore']==$storeRow['StoreCode'])
			$s="selected";
			?>
			<option value="<?php echo $storeRow['StoreCode'];?>" <?php echo $s;?>><?php echo $storeRow['StoreName'];?></option>
		<?php 	
		}
    	?>
    </select></td>
  </tr>
</table>


<p align="left"><input type="submit" name="item_popup" value="Pick Items" id="item_popup"  onclick="item_window();" /></p>
<div style="overflow:auto; width:810px ; height:auto"  >
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td class="tabledata">Part Code</td>
    <td class="tabledata">Item Name</td>
    <td class="tabledata">UOM</td>
    <td class="tabledata">Make</td>
    <td class="tabledata">Grade</td>
    <td class="tabledata">Color</td>
    <td class="tabledata">Size</td>
    <td class="tabledata">GSM</td>
    <td class="tabledata">Stock Qty</td>
    <td class="tabledata">Transfer Qty</td>
    <td class="tabledata">Purpose</td>
  </tr>
 <?php 
 if($_SESSION['TransferArray']!=''){
 $break=explode(",", $_SESSION['TransferArray']);
	 //print_r($break);
	for($i=0;$i<count($break);$i++)
	{	 
		$itemSql=DB_query("select item_name, unit from item_master where StockID='".$break[$i]."'",$db);
		$itemRow=DB_fetch_array($itemSql);
 ?>
<tr bgcolor="#c1c1c1">
    <td><?php echo $break[$i]?><input type="hidden" name="itemCode[<?php echo $i;?>]" value="<?php echo $break[$i]?>" id="itemCode_<?php echo $i;?>" /></td>
    <td><?php echo $itemRow['item_name']?></td>
    <td><?php
    	$uomSql=DB_query("Select unitname from unitsofmeasure where unitid='".$itemRow['unit']."'",$db);
		$uomRow=DB_fetch_array($uomSql);
		echo $uomRow['unitname'];
    ?></td>
    <td>
    	<select name="make[<?php echo $i;?>]" id="" style="width:100%;">
    		<?php 
    		$makeSql=DB_query("Select manufacture from item_specification where proc_id='".$break[$i]."' and manufacture!='0'",$db);
    		while($makeRow=DB_fetch_array($makeSql))
			{
				$make=DB_query("Select manufacturer from product_manufacture where id='".$makeRow['manufacture']."'",$db);	
				$result=DB_fetch_array($make);
				$makeName=$result['manufacturer'];
				?>
				<option value="<?php echo $makeRow['manufacture'];?>"><?php echo $makeName;?></option>
			<?php 	
			}
    		?>
    	</select>
    </td>
    <td>
    	<select name="grade[<?php echo $i;?>]" id="" style="width:100%;">
    		<?php 
    		$gradeSql=DB_query("Select grade from item_specification where proc_id='".$break[$i]."' and grade!='0'",$db);
    		while($gradeRow=DB_fetch_array($gradeSql))
			{
				$grade=DB_query("Select grade from product_grade where id='".$gradeRow['grade']."'",$db);
				$result=DB_fetch_array($grade);	
				$gradeName=$result['grade'];
				?>
				<option value="<?php echo $gradeRow['grade'];?>"><?php echo $gradeName;?></option>
			<?php 	
			}
    		?>
    	</select>
    </td>
    <td>
    	<select name="color[<?php echo $i;?>]" id="" style="width:100%;">
    		<?php 
    		$colorSql=DB_query("Select color from item_specification where proc_id='".$break[$i]."' and color!='0'",$db);
    		while($colorRow=DB_fetch_array($colorSql))
			{
				$color=DB_query("Select color from product_color where id='".$colorRow['color']."'",$db);
				$result=DB_fetch_array($color);		
				$colorName=$result['color'];
				?>
				<option value="<?php echo $colorRow['color'];?>"><?php echo $colorName;?></option>
			<?php 	
			}
    		?>
    	</select>
    </td>
    <td>
    	<select name="size[<?php echo $i;?>]" id="" style="width:100%;">
    		<?php 
    		$sizeSql=DB_query("Select size from item_specification where proc_id='".$break[$i]."' and size!='0'",$db);
    		while($sizeRow=DB_fetch_array($sizeSql))
			{
				$size=DB_query("Select name from size_master where id='".$sizeRow['size']."'",$db);
				$result=DB_fetch_array($size);			
				$sizeName=$result['name'];
				?>
				<option value="<?php echo $sizeRow['size'];?>"><?php echo $sizeName;?></option>
			<?php 	
			}
    		?>
    	</select>
    </td>
    <td>
    	<select name="gsm[<?php echo $i;?>]" id="" style="width:100%;">
    		<?php 
    		$gsmSql=DB_query("Select gsm from item_specification where proc_id='".$break[$i]."' and gsm!='0'",$db);
    		while($gsmRow=DB_fetch_array($gsmSql))
			{
				$gsm=DB_query("Select gsm from gsm where id='".$gsmRow['gsm']."'",$db);
				$result=DB_fetch_array($gsm);			
				$gsmName=$result['gsm'];
				?>
				<option value="<?php echo $gsmRow['gsm'];?>"><?php echo $gsmName;?></option>
			<?php 	
			}
    		?>
    	</select>
    </td>
    <td></td>
    <td><input type="text" name="quantity[<?php echo $i;?>]" value="" id="quantity_<?php echo $i;?>" style="width:100%;"/></td>
    <td><input type="text" name="purpose[<?php echo $i;?>]" value="" id="purpose_<?php echo $i;?>" style="width:100%;"/></td>
  </tr>
  <?php } } ?><input type="hidden" name="count" value="<?php echo $i;?>" id=""/>
</table></div>

<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
  <td class="tabledata">Remarks</td>
  <td><textarea name="remarks" style="width:100%;"></textarea></td>
  </tr>
</table>
<p align="center"><input type="submit" name="Save" value="Save"><input type="submit" name="Cancel" value="Cancel"></p>
</form>
