<style type="text/css">
.body_lightbox {
width:100%;
background-color:#e7e7e7;
position:absolute;
left:280px;
top:95px;
padding-left:5px;
padding-right:5px;
border:#666666 solid 3px;
}
</style>
<div class="body_lightbox" id="lightbox" style="overflow:auto; width:600px; height:250px; display:none;"></div>
<?php ob_start();
$PageName = 'Physical Stock Adjustment';
if(isset($_REQUEST['Save']))
{
		 
		 
	 if($_REQUEST['physical_stock']['site']){
	 
		    $year=date('y');
			$year_after=$year+1;
			
			$str=substr($_REQUEST['physical_stock']['site'],0,3);			
			$prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$PageName."'",$db);
			$prefix_row=DB_fetch_array($prefix_sql);
			$prefix=$prefix_row['prefix_code'];
	     	$x=substr($prefix,6,9);
			
			 $sql="Select MAX(requisitionno) as Reqno from materialrequisitions where sitecode='".$_REQUEST['physical_stock']['site']."' order by Reqno desc limit 1";
			$result=DB_query($sql,$db);
			$myrow=DB_fetch_array($result);
			
			if(DB_num_rows($result)>0)
			{
				$Reqno=$myrow['Reqno'];
				 $req_part1=substr($Reqno,-4);
				//echo $req_part1;
				$req_final=$req_part1+1;
				//echo "<br>".$req_final;
			}
			else
			{
				$req_final="1";
			}
			
			 $string=$str."/".$x."/".$year.$year_after."/";
			  $final_string=$string.str_pad($req_final,6,"0",STR_PAD_LEFT);
			  
			 $_REQUEST['physical_stock']['ADJ_voucher_no'] =$final_string;
			  
			  
		 }
		 
		  $_REQUEST['physical_stock']['date1']=FormatDateForSQL($_REQUEST['physical_stock']['date1']);
		
	      extract($_REQUEST['physical_stock']);
//		physical_stock
	if($s->RecordInsert('physical_stock_adjustment',$_REQUEST['physical_stock']))
	{
		$last_id=DB_Last_Insert_Id($db,"physical_stock_adjustment","id");
			// print_r($last_id);
			// print_r($_REQUEST);exit;

			for($i=0;$i<count($_REQUEST['itemcode']);$i++)
			{
				$_REQUEST['physical_stock_id']=$last_id;
				
				
					//echo "Select * from stock_maintenance where itemcode='".$_REQUEST['itemcode'][$i]."'";
				$query_add=DB_query("Select * from stock_maintenance where itemcode='".$_REQUEST['itemcode'][$i]."'",$db);
				$row=DB_fetch_array($query_add);
				
				
				// $item_adj=$row['stockbalance'];
				if($row['itemcode']==$_REQUEST['itemcode'][$i] && $row['site']==$_REQUEST['physical_stock']['site'] && $row['store']==$_REQUEST['physical_stock']['store'])
				{
					if($_REQUEST['adjustment_type'][$i]=='plus')
					{
									
					$total_qty=$_REQUEST['stock'][$i]+$item_adj;
				 $condition1="itemcode='".$_REQUEST['itemcode'][$i]."' && site='".$_REQUEST['physical_stock']['site']."' && store='".$_REQUEST['physical_stock']['store']."'"; 	
				$current_bal=$row['stockbalance']+ $_REQUEST['adjustment_qty'][$i];
					//echo $current_bal; exit;
					//print_r($current_bal); exit;
					$balance_arr['site'] = $_REQUEST['physical_stock']['site'];
					$balance_arr['store'] = $_REQUEST['physical_stock']['store'];  
					$balance_arr['itemcode']= $_REQUEST['itemcode'][$i];
					$balance_arr['adjustment_qty']=$current_bal;
					$balance_arr['created_by']=$_SESSION['UserID'];
					$update1=$s->EditRecord(stock_maintenance, $balance_arr, $condition1);
					//print_r($current_bal); exit;
					$row1=DB_query($update1,$db);					
					}
					else {
					$total_qty=$_REQUEST['stock'][$i]-$item_adj;
					$condition1="itemcode='".$_REQUEST['itemcode'][$i]."' && site='".$_REQUEST['physical_stock']['site']."' && store='".$_REQUEST['physical_stock']['store']."'"; 	
					$current_bal=$row['stockbalance']+ $_REQUEST['adjustment_qty'][$i];
					//echo $current_bal; exit;
					//print_r($current_bal); exit;
					$balance_arr['site'] = $_REQUEST['physical_stock']['site'];
					$balance_arr['store'] = $_REQUEST['physical_stock']['store'];  
					$balance_arr['itemcode']= $_REQUEST['itemcode'][$i];
					$balance_arr['adjustment_qty']=$current_bal;
					$balance_arr['created_by']=$_SESSION['UserID'];
					$update1=$s->EditRecord(stock_maintenance, $balance_arr, $condition1);
					$row1=DB_query($update1,$db);
					}
				}
					
					$insert_details=array('id'=>'',
						'physical_stock_id'=>$last_id,
						'itemcode'=>$_REQUEST['itemcode'][$i],
						'itemname'=>$_REQUEST['itemname'][$i],
						'make'=>$_REQUEST['Make'][$i],
						'grade'=>$_REQUEST['grade'][$i],
						'color'=>$_REQUEST['color'][$i],
						'uom'=>$_REQUEST['uom'][$i],
						'size'=>$_REQUEST['size'][$i],
						'gsm'=>$_REQUEST['gsm'][$i],
						'stock'=>$_REQUEST['stock'][$i],
						'adjustment_qty'=>$_REQUEST['adjustment_qty'][$i],
						'adjustment_type'=>$_REQUEST['adjustment_type'][$i],
						'remarks'=>$_REQUEST['remarks'][$i]);
					if(!empty($insert_details['physical_stock_id']))
					{
						$s->RecordInsert('physical_stock_adj_details',$insert_details);
					}
				
			}
			
		 echo "<script>location.href='index.php?route=Inventory/transation/physical_adjustment_index&modid=".$_REQUEST['modid'].                      "&frmid=".$_REQUEST['frmid']."'</script>";
	}	
}
	
	$sql2="Select auto_number from physical_stock_adjustment order by auto_number desc limit 1";
	$result2=DB_query($sql2,$db); /*where store='".$_REQUEST['store']."' */
	
	if(DB_num_rows($result2)>0)
	{
		$row2=DB_fetch_array($result2);
		$challanDigit=$row2['auto_number'];
		$next_digit=$challanDigit+1;
		$next=str_pad($next_digit,4,"0",STR_PAD_LEFT);
	}
	else
	{
		$next="0001";
	}
	
if(isset($_REQUEST['action']) && isset($_REQUEST['phy']))
{
	if($_REQUEST['action']=='edit' && !empty($_REQUEST['phy']))
	{
		$recEdit=DB_fetch_array($s->GetRecordById('physical_stock_adjustment','id='.$_REQUEST['phy']));
		extract($recEdit);
		$recEdit2=$s->GetRecordById('physical_stock_adj_details','physical_stock_id='.$_REQUEST['phy']);
	}
}

if(isset($_REQUEST['update']))
{

		print_r($_REQUEST);	 exit;
		extract($_REQUEST['physical_stock']);
		if($s->RecordEdit('physical_stock_adjustment',$_REQUEST['physical_stock'],"id=".$_REQUEST['physical_stock']['id']))
		{
			if($s->DeleteRecord('physical_stock_adj_details',"physical_stock_id=".$_REQUEST['physical_stock']['id']))
			{
			$last_id=$_REQUEST['physical_stock']['id'];
			$_REQUEST['physical_stock_id']=$last_id;
			for($i=0;$i<count($_REQUEST['itemcode']);$i++)
			{
				$_REQUEST['physical_stock_id']=$last_id;
				
				if(!empty($_REQUEST['itemcode'][$i]))
				{
						if($_REQUEST['adjustment_type'][$i]='plus')
						{
							$total_qty=$_REQUEST['stock'][$i]+$_REQUEST['adjustment_qty'][$i];
						}
						else
					    {
							$total_qty=$_REQUEST['stock'][$i]-$_REQUEST['adjustment_qty'][$i];	
						}
					$insert_details=array('id'=>'',
						'physical_stock_id'=>$last_id,
						'itemcode'=>$_REQUEST['itemcode'][$i],
						'itemname'=>$_REQUEST['itemname'][$i],
						'make'=>$_REQUEST['Make'][$i],
						'grade'=>$_REQUEST['grade'][$i],
						'color'=>$_REQUEST['color'][$i],
						'uom'=>$_REQUEST['uom'][$i],
						'size'=>$_REQUEST['size'][$i],
						'gsm'=>$_REQUEST['gsm'][$i],
						'stock'=>$_REQUEST['stock'][$i],
						'adjustment_qty'=>$total_qty,
						'adjustment_type'=>$_REQUEST['adjustment_type'][$i],
						'remarks'=>$_REQUEST['remarks'][$i]);
					if(!empty($insert_details['physical_stock_id']))
					{
						$s->RecordInsert('physical_stock_adj_details',$insert_details);
					}
				}
			}
			}
	
		}	
	
			unset($_REQUEST['physical_stock']);
			$_SESSION['success_msg']="Saved Successfully";
			echo "<script>location.href='$url_cancel'</script>";
			

	
}

?>
<h2 align="center" style="background-color:#CCC; width:100%; border:solid 2px #FFF;">Physical Stock Adjustment</h2>
<form action="" method="post">
<table width="100%" border="0" cellspacing="1" cellpadding="1" style="background-color:#FFF; border:solid 1px #999">
	<tr align="center"> <td colspan="2">&nbsp;</td>
	<th>Site:</th>
		<td>
		<select name="physical_stock[site]" id="site" style="width:100%;height:20px" onchange="getStore(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></tr>
	<tr><td>&nbsp;</td></tr>
  <tr>
    <th width="15%">ADJ. Voucher No.</th>
    <td width="15%"><label>
    	<input type="hidden" name="physical_stock[auto_number]" id="auto_number" value="<?php echo $next;?>" style="width:100%">
      <input type="text" name="physical_stock[ADJ_voucher_no]" id="ADJ_voucher_no" value="Auto generated after saving" style="width:100%"  readonly="true">
    </label></td>
    <th width="11%">Date:</th>
    <td width="15%"><input type="text" name="physical_stock[date1]" id="date" style="width:100%" class="datepicker">
    	</td>
    <th width="20%">Store:</th>
    <td width="15%"  id="storeDiv" > <label>
      <select name="physical_stock[store]"   style="width:100%">
        <option value="0">Select</option>       
      </select></label></td>
  </tr>
</table>

<p align="left">
<input type="button" onclick="addRow('dataTable')" value="Add More" />&nbsp;
<input type="button" class="crmbutton small delete"  onclick="deleteRow('dataTable')" value="Delete" />
</p>
<div align="center">
<div style="overflow:auto; width:950px; height:150px">
<table width="100%" border="0" cellspacing="1" cellpadding="1" style="background-color:#FFF; border:solid 1px #999">
  <tr>
    <th></th>
<!--    <th>Pick Item</th>-->
    <th>Part Code</th>
    <th>Item Name</th>
    <th>UOM</th>

    <th>Make</th>
    <th>Grade</th>
    <th>Color</th>
 <!--   <th>Size</th>
    <th>GSM</th>-->
    <th>Stock Qty</th>
    <th>Adjustment Qty</th>
    <th>Adjustment Type</th>
    <th>Remarks</th>
  </tr>
 <tbody id="dataTable">
  <tr bgcolor="#FFFFFF">
  <td><input type="checkbox" name="chk" />
	  <input type="hidden" value="" id="user_shipping" name="user_shipping[]" /></td>
<!--  <td><input type="button" name="profile_popup" value="+" id="profile_popup"  onclick="openPopupProfile(this.id)" />      </td>-->
  <td><input type="text" value="" tabindex=""   id="itemcode" name="itemcode[]"size="10"  onkeyup="opendata(this.value,this.id)"  />      </td>
  <td><input type="text" name="itemname[]"  id="item_name" size="25" readonly/></td>
    <td><input type="text" name="uom[]" id="uom" value="" readonly/></td>
  <td> <select name="Make[]" id="make" style="width:100%">
			<option value="0">Select Make</option>
      <?php
			 $querymake=DB_query("select * from product_manufacture where 1=1" ,$db);
			while($que_row=DB_fetch_array($querymake))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($details_row['make']==$que_row['id'] ?'selected':'')?> > <?php echo $que_row['manufacturer'];?></option>
      <?php  }?>
	  </select>
	  </td>
  <td><select name="grade[]" style="width:100%" id="grade">
			<option value="0">Select Grade</option>
		<?php
			 $querygrade=DB_query("select * from product_grade where 1=1" ,$db);
			while($que_row=DB_fetch_array($querygrade))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($details_row['grade']==$que_row['id']?'selected':'')?>><?php echo  $que_row['grade'];?></option>
      <?php  }?>
	  </select>
	  </td>
  <td><select name="color[]" style="width:100%" id="color">
			<option value="0">Select Color</option>
		<?php
			 $querycolor=DB_query("select * from product_color where 1=1" ,$db);
			while($que_row=DB_fetch_array($querycolor))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($details_row['color']==$que_row['id'] ?'selected':'')?>><?php echo  $que_row['color'];?></option>
      <?php  }?></select>
	  </td>
<!--  <td> <select name="size[]" id="size" style="width:100%">
		  	<option value="0">Select Size</option>
		    <?php 
		
			$sizeId=DB_query("Select * from size_master where 1",$db);
			while($sizeRow=DB_fetch_array($sizeId))
			{
		?>
		    <option value="<?php echo $sizeRow['id'];?>"><?php echo $sizeRow['name'];?></option>
		    <?php } ?>
		    </select>
			</td>
  <td><input type="text" name="gsm[]" id="uom" value="" readonly/></td>-->
  <td><input type="text" name="stock[]"  id="Stockbalance"  size="10" readonly/></td>
  <td><input type="text" name="adjustment_qty[]"  id="adjustment_qty" size="10" /></td>
  <td><select name="adjustment_type[]" id="adjustment_type">
  	<option value="0">Select</option>
  	<option value="plus"><strong>Plus</strong></option><option value="minus"><strong>Minus<strong></option></select> </td>
  <td><input type="text" name="remarks[]"  id="remarks" size="20" /></td>
  </tr>
  </tbody>
</table>
</div>
</div>
<br/>
<table width="100%" border="0" cellspacing="1" cellpadding="1" style="background-color:#FFF; border:solid 1px #999">
  <tr>
  <th>Remarks</th>
  <td><textarea name="physical_stock[remarks]" style="width:100%;" rows="5"></textarea>
  <input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
  <input type="hidden" name="" value="" id="edit_cnt" />
  
  </td>
  </tr>
</table>
<p align="center"><input type="submit" name="<?php if($_REQUEST['action']=='edit') echo 'update'; else echo 'Save'; ?>" value="Save" onclick="return savedata();"><input type="submit" name="Cancel" value="Cancel"></p>
</form>

<script language="javascript" type="text/javascript">
function openPopupProfile(i)
{ 
		window.open("issu_searchpopup.php?rowcount="+i,"profiles_popup_window","height=425,width=640,toolbar=no,menubar=no,dependent=yes,resizable=yes");
}

function savedata()
{
if(document.getElementById("site").value=='0')
{
alert("Must select Site ");
 
return false;	
}
if(document.getElementById("date").value=='')
{
alert("Must Enter Date ");
 
return false;	
}	
if(document.getElementById("store").value=='0')
{
alert("Must Select Store Location");
return false;	
}	

 
var j=document.getElementById("dataTable").rows.length;
 
if(j==1)
{
if(document.getElementById("adjustment_type").value=='0')
{
alert("Must Select Adjustment Type");
return false;		
}	
if(document.getElementById("adjustment_qty").value=='')
{
alert("Must Enter Adjustment Qty");
return false;		
}	
}
else
{
for(var i=1;i<count(j);i++)
{
if(document.getElementById("adjustment_type_"+i).value=='0')
{
alert("Must Select Adjustment Type ");
return false;		
}	
if(document.getElementById("adjustment_qty_"+i).value=='')
{
alert("Must Enter Adjustment Qty");
return false;		
}	
}		
}
 

return true;	
}


function getStore(str){
 
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)
    {
		//alert(xmlhttp.status);
	  //alert(xmlhttp.responseText);
     document.getElementById("storeDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_adj_store.php?site="+str,true);
xmlhttp.send();

}




function opendata(str,id)
{

 
 
  var sitecode=document.getElementById("site").value;
  var date=document.getElementById("date").value;
  
	 
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("lightbox").style.display='block';	
	document.getElementById("lightbox").innerHTML=xmlhttp.responseText;
	
   // document.getElementById("textitem").innerHTML=data[1];
	
    }
  }
  
  
    var sitecode=document.getElementById("site").value;
    var date=document.getElementById("date").value;

    xmlhttp.open("GET","showpopup.php?site="+str+"&data="+id+"&site1="+sitecode+"&date="+date,true);
	xmlhttp.send();


	
	
}

function getItemsList1(id,code,shade,netrate,mrprate,rate9,ctr)
{ 

 
    var ctr1=ctr.split("_");	


    var i=ctr1[1];
	
 
	
	var itemcount=parseInt(document.getElementById('hiditemcount').value)+1;
	var edit_cnt=document.getElementById('edit_cnt').value;
	if(edit_cnt==''){var counter=1;} else if(edit_cnt>itemcount){var counter=edit_cnt;
	var itemcount=parseFloat(counter)+parseFloat(itemcount);
	} else  {var counter=edit_cnt;}
	
	 
	  
	if(ctr1[1]==undefined)
	{
	
	//document.getElementById('mst_itemid').value = id;
	document.getElementById('itemcode').value = code;
	document.getElementById('item_name').value = shade;
	 document.getElementById('uom').value = netrate;
	document.getElementById('Stockbalance').value = mrprate;
	//document.getElementById("Unit1").value=rate9;
	}
	else
	{
		
	//document.getElementById('mst_itemid_'+i).value = id;
	document.getElementById('itemcode_'+i).value = code;
	document.getElementById('item_name_'+i).value = shade;
	document.getElementById('uom_'+i).value = netrate;
	 document.getElementById('Stockbalance_'+i).value = mrprate;
	//document.getElementById("Unit1_"+i).value=rate9;
	
	}	
	document.getElementById("lightbox").style.display='none';
	
	
}

function hidedata()
{
document.getElementById("lightbox").style.display='none';
}

</script>