<link rel="stylesheet" href="css/new_mxerp.css">
<?php 
$table='opening_stockitemwise';
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;
if($_REQUEST['Save'])
{
 	if(empty($_REQUEST['site']))
	{
	   $msgErr[]='Please Select Branch Name';
	   $status=false;
	}
	if($_REQUEST['store']==0)
	{
	   $msgErr[]='Please Select Godown Name';
	   $status=false;
	}
	if($_REQUEST['recievedDate']=='')
	{
	   $msgErr[]='Please Select Receiving Date';
	   $status=false;
	}
	if($_REQUEST['itemCode']=='')
	{
	   $msgErr[]='Please Select Item';
	   $status=false;
	}
	 
	 if($_REQUEST['openingQty']=='')
	{
	   $msgErr[]='Please Enter Opening QTY';
	   $status=false;
	}
 } 

//************************Delete*********************************************** 
if($action=='del')
{
	$condition="id='".$_REQUEST['ID']."'";
	$s->DeleteRecord($table,$condition);
	
	
	$s->pageLocation("index.php?route=OpeningStockItemWise&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']);
}
 //************************Edit*********************************************** 

if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
  	$condition="id='".$_REQUEST['ID']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
	$site  =$RData['site'];
	$recievedDate=ConvertSQLDate($RData['recievedDate']);
	$store=$RData['store'];
	$itemCode=$RData['itemCode'];
	$openingQty=$RData['openingQty'];
	$rate=$RData['rate'];
	$amount=$RData['amount'];
}
else
{
	$site=$_REQUEST['site'];
	$recievedDate=date('d/m/Y');
	$store=$_REQUEST['store'];
	$itemCode=$_REQUEST['itemCode'];
	$openingQty=$_REQUEST['openingQty'];
	$rate=$_REQUEST['rate'];
	$amount=$_REQUEST['amount'];
}
 

if($_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$data['site']=$_REQUEST['site'];
	$data['recievedDate']=FormatDateForSQL($_REQUEST['recievedDate']);
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
	$data['store']=$_REQUEST['store'];
	$data['itemCode']=$_REQUEST['itemCode'];
	$data['openingQty']=$_REQUEST['openingQty'];
	$data['rate']=$_REQUEST['rate'];
	$data['amount']=$_REQUEST['openingQty']*$_REQUEST['rate'];
  	$cond="id='".$_REQUEST['ID']."'";
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
				$stock=DB_query("Select stockbalance from stock_maintenance where itemcode='".$_REQUEST['itemCode']."'",$db);
					$stock_row=DB_fetch_array($stock);
					$condition1="itemcode='".$_REQUEST['itemCode']."' && site='".$_REQUEST['site']."' && store='".$_REQUEST['store']."'"; 
					
					$current_bal=$stock_row['stockbalance']+ $_REQUEST['openingQty'][$i];
					//echo $current_bal; exit;
					//print_r($current_bal); exit;
					$balance_arr['site'] = $_REQUEST['site'];
					$balance_arr['store'] = $_REQUEST['store'];  
					$balance_arr['itemcode']= $_REQUEST['itemCode'][$i];
					$balance_arr['stockbalance']=$current_bal;
					$balance_arr['created_by']=$_SESSION['UserID'];
					$update1=$s->EditRecord(stock_maintenance, $balance_arr, $condition1);
					$row1=DB_query($update1,$db);
					
					if(!$row1)
					{
	  					 die('Error'.DB_error());
					}

	
  $s->pageLocation("index.php?route=OpeningStockItemWise&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']);
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] &&  $status && $action=='')
{
$data['site']=$_REQUEST['site'];
$data['recievedDate']=FormatDateForSQL($_REQUEST['recievedDate']);
$data['CompanyId']=$_SESSION['ACGCompanyId'];
$data['store']=$_REQUEST['store'];
$data['itemCode']=$_REQUEST['itemCode'];
$data['openingQty']=$_REQUEST['openingQty'];
$data['rate']=$_REQUEST['rate'];
$data['amount']=$_REQUEST['openingQty']*$_REQUEST['rate'];

 	$insert= $s->insertRecord($table, $data);
	$row=DB_query($insert,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
	
	$Lastreq_id=DB_Last_Insert_ID($db,'opening_stockitemwise',$ErrMsg);
	
	if($stock_row['itemcode']=$_REQUEST['itemCode'][$i] && $stock_row['site']=$_REQUEST['site'] && $stock_row['store']=$_REQUEST['store'])
	{
		$condition1="itemcode='".$_REQUEST['itemCode'][$i]."' && site='".$_REQUEST['site'][$i]."' && store='".$_REQUEST['store'][$i]."'"; 
		$current_bal=$stock_row['stockbalance']+$_REQUEST['openingQty'];
		//echo $current_bal; exit;
		//print_r($current_bal); exit;
		$balance_arr['site'] = $_REQUEST['site'];
		$balance_arr['store'] = $_REQUEST['store'];  
		$balance_arr['itemcode']= $_REQUEST['itemCode'];
		$balance_arr['stockbalance']=$current_bal;
		$balance_arr['created_by']=$_SESSION['UserID'];
		$update1=$s->EditRecord(stock_maintenance, $balance_arr, $condition1);
		$row1=DB_query($update1,$db);
					
					if(!$row1)
					{
	  					 die('Error'.DB_error());
					}
		}
	else{
	$balance_arr['site'] = $_REQUEST['site'];
	$balance_arr['store'] = $_REQUEST['store'];  
	$balance_arr['itemcode']= $_REQUEST['itemCode'];
	$balance_arr['stockbalance']= $_REQUEST['openingQty'];
	
	$balance_arr['created_by']=$_SESSION['UserID'];  
	
	$insert_stock_maintenance=$s->insertRecord('stock_maintenance', $balance_arr);
	$row1=DB_query($insert_stock_maintenance,$db);
	if(!$row1)			
	{
	   die('Error'.DB_error());
	}
	}
 $s->pageLocation("index.php?route=OpeningStockItemWise&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']);
}
?>
<script language="JavaScript" type="text/javascript">
	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","GetItems.php?partName="+str,true);
xmlhttp.send();
}
function itemSpecs(str)
{
	if(str=="")
  {
  document.getElementById("openingDiv").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("openingDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","GetItems.php?item="+str,true);
xmlhttp.send();
}
function calculateAmount()
{
	var qty=document.getElementById("openingQty").value;
	var rate=document.getElementById("rate").value;
	var Total=qty*rate;
	document.getElementById("amount").value=Total.toFixed(2);
}
</script>
<script src="js/function-ajax.js"></script>

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

<form action="" method="post">
	<h1>Opening Stock Item Wise</h1>
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <?php if(isset($daction))
  {?>
    <tr>
  
    <td colspan="7" ><div class="success">
	<?php 
	 if($_REQUEST['daction']!=1 && $_REQUEST['daction']!=2)
	 echo "Record ".$_REQUEST['daction']." Successfully";
	 elseif($_REQUEST['daction']==1)
	 	 echo "Can't Delete This Record because AccountName Attatch with LedgerDayBook table ";
	 elseif($_REQUEST['daction']==2)		 
		 echo "Can't Delete This Record. First Delete it's Parent Record   ";
		 ?>
	</div></td>
  </tr>
  <?php }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
    <tr>
  
    <td colspan="7" ><div class="error"><?php 
for($kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>
 </table>
 
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
   
 	 <tr>
   	 <td class="tabledata">Branch</th>
     <td width="20%">
	<?php  
 	$SQL_client=DB_query("SELECT loccode,prefix,locationname from locations Where companyid='".$_SESSION['ACGCompanyId']."'",$db);
	?>
   <select name="site" style="width:100%; height:100%;"  onchange="ShowDiv(this.value,'hid-Godown-div','GoDownByBranch.php')"  >
   <option value="0" >--Branch Name--</option>
   <?php
	while($row_client=DB_fetch_array($SQL_client))
	{
	  $p='';
	  if($row_client['loccode']==$site)
	  $p="selected";
	 ?>
          <option value="<?php  echo $row_client['loccode']?>" <?php  echo $p; ?> >
          <?php  echo $row_client['locationname']?>
          </option>
          <?php }?>
        </select>   
   </td>
    <td class="tabledata">GoDown</th>
    <td width="20%">
	<div id='hid-Godown-div'><?php
	     $sql_store ='select distinct StoreCode, StoreName from storetype';
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select name="store" style="width:100%; height:100%;" >
        <option value="0">Select</option>
        <?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{
			$s='';
			if($store==$myrow['StoreCode'])
			$s="selected";
			?>
        <option value="<?php echo $myrow['StoreCode'];?>" <?php echo $s;?>><?php echo $myrow['StoreName'];?></option>
        <?php 
		}
		?>
      </select></div></td>
	<td class="tabledata">Received Date </th>
    <td><input name="recievedDate" type="text" class="datepicker" id="recievedDate  " style="width:100%;height:20px;"     value="<?php  echo $recievedDate;?>" size="15" /></td>
  </tr>
  	<tr>
	  <td class="tabledata" nowrap="nowrap">Enter Part Name</th>
    	<td><input type="text" name="itemName" value="" onkeyup="showItems(this.value)" style="width:100%;height:20px;"/></td>
    
  	    <td class="tabledata">Item</th> 
  	    <td id="itemid">
  	     <?php  	
  	     $query=DB_query("Select StockID,item_name from item_master",$db);
?>
 <select name="itemCode" id="itemCode" style="width:100%;height:20px;"> <!-- onchange="itemSpecs(this.value)" -->
 	<option value="0">Select Item</option>
 	<?php 
 	while($row=DB_fetch_array($query))
	{
			$s='';
			if($itemCode==$row['StockID'])
			$s="selected";
		?>
		<option value="<?php echo $row['StockID']?>" <?php echo $s;?>><?php echo $row['StockID']." - ".$row['item_name']?></option>
	<?php 	
	}
 	?>
 </select>
  	    </td>
  	    <td class="tabledata">Opening Qty</th>
         <td><input type="text" id="openingQty" name="openingQty" style="width:100%;height:20px;"  value="<?php echo $openingQty ;?>"/></td>
	</tr>
  </table>  
   
    
    
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
    
	  <tr>
	  		 <td class="tabledata">Rate</th> <td><input type="text" id="rate" name="rate" style="width:100%;height:20px;"  value="<?php echo $rate ;?>"/></td>
		     <td class="tabledata">Amount</th> <td><input type="text" id="amount" name="amount" style="width:100%;height:20px;"  value="<?php echo $amount ;?>" onfocus="calculateAmount()" readonly/></td>
             
      </tr>
 	 
</table>
<p align="center">
<input type="submit" name="Save" value="Save"><input type="reset" name="Reset" value="Reset"></p>
</form>
 


<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
  <td class="tabledata">Sno.</td>
    <td class="tabledata">Received Date</td>
    <td class="tabledata">Branch</td>
    <td class="tabledata">Item Name</td>
    <td class="tabledata">Stock Qty</td>
    <td class="tabledata">Rate</td>
    <td class="tabledata">Amount</td>
	<td class="tabledata">Edit</td>
	<!-- <td>Delete</td> -->
  </tr>
    <?php 
   $GetRecord = "Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."'";
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "<td colspan='5'>--No Record--</td>";
   $j=0;
   while($RowData = DB_fetch_array($ResultData))
   {
   	$item_name = $fnLib->getField('item_master','StockID',$RowData['itemCode'],'item_name')
   ?>
<tr bgcolor="#c1c1c1">
     <td ><div align="center"><?php echo $i; ?></div></td>
    <td><?php echo ConvertSQLDate($RowData['recievedDate']); ?>	  </td>
	<!-- <td><?php //echo $RowData['AccountsType']; ?>	  </td>-->
	 <td><?php echo $RowData['site'] ?>	  </td>
 	 <td><?php echo $RowData['itemCode']." - ".$item_name ?>	  </td>
    <td><?php echo $RowData['openingQty']; ?>	  </td>
	<td><?php echo $RowData['rate']; ?>	  </td>
	<td><?php echo $RowData['amount']; ?>	  </td>
    
   <td><div align="center"><a href="index.php?route=OpeningStockItemWise&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=Edit&ID=<?php echo $RowData['id']; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<!-- <td><div align="center"><a href="index.php?route=OpeningStockItemWise&modid=<?php //echo $_REQUEST['modid'];?>&frmid=<?php //echo $_REQUEST['frmid'];?>&action=del&ID=<?php //echo $RowData['id']; ?>" onclick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete.png" width="10" height="10" /></a></div></td> -->	
  </tr>
	<?php $i++;}   
   ?> 
 </table> 

</div></div>

 <script language="javascript" type="text/javascript">
function Get_itemname(str)
{

if (str=="")
  {
  document.getElementById("itemdesc").innerHTML="";
  return;
  }
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("itemdesc").value=xmlhttp.responseText;
	
    }
  }
 // alert(str);
xmlhttp.open("GET","get_itemname.php?name="+str,true);
xmlhttp.send();

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

}
</script>