<?php 
$table='opening_stockitemwise';
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;

//************************Delete*********************************************** 
if($action=='del')
{
	$condition="id='".$_REQUEST['ID']."'";
	$s->DeleteRecord($table,$condition);
	
	
	$s->pageLocation("index.php?route=Inventory/transation/Opening_Stock_Item_Wise_index&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);		
	$_REQUEST['site']=$RData['site'];
	$reveive_date=ConvertSQLDate($RData['reveive_date']);
    $store=$RData['store'];
	$itemcode=$RData['itemcode'];
	$itemdesc=$RData['itemdesc'];
	$stock=$RData['stock'];
	$rate=$RData['rate'];
	$amount=$RData['amount'];
} 

if($_REQUEST['Save'] &&  $status && $action=='Edit')
{		
	$SQL_item=DB_fetch_array(DB_query("SELECT * from item_master where id='".$_REQUEST['itemcode']."' ",$db ));
	$data['site']=$_REQUEST['site'];
	$data['reveive_date']=FormatDateForSQL($_REQUEST['reveive_date']);
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
	$data['store']=$_REQUEST['store'];
	//$data['itemcode']=$_REQUEST['itemcode'];
	$data['itemcode']=$_REQUEST['itemcode'];
	$data['itemdesc']=$SQL_item['item_name'];
	$data['stock']=$_REQUEST['stock'];
	$data['rate']=$_REQUEST['rate'];
	$data['amount']=$_REQUEST['stock']*$_REQUEST['rate'];
  	$cond="id='".$_REQUEST['ID']."'";
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	die('Error'.DB_error());
	}
	//echo"Select stockbalance from stock_maintenance where itemcode='".$_REQUEST['itemcode']."'";
	
			$stock=DB_query("Select stockbalance from stock_maintenance 
							where itemcode='".$_REQUEST['itemcode']."' 
							and store='".$_REQUEST['store']."'",$db);
					$stock_row=DB_fetch_array($stock);
					$condition1="itemcode='".$_REQUEST['itemcode']."' 
					&& site='".$_REQUEST['site']."' && store='".$_REQUEST['store']."' "; 
					
					$current_bal=$_REQUEST['stock'];
					
					$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());
					}

	
  $s->pageLocation("index.php?route=Inventory/transation/Opening_Stock_Item_Wise_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']);
}
 
//************************INSERT*********************************************** 

?>

<script src="js/function-ajax.js"></script>
<script language="javascript" >
function calAmt()
{
   var a=  document.getElementById('stock').value;
	var   b=  document.getElementById('rate').value;
    document.getElementById('amount').value=a*b;
   
}
</script>
<style>
#slidingDiv {
    display: none;
	height:100px;
	background-color: #D9D6DA;
	padding:20px;
}
</style>
<form action="" method="post"><fieldset>
<legend align="center"><h1>Opening Stock Item Wise</h1></legend>




<table width="100%" border="0" cellspacing="3" cellpadding="3">
 
  <?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 }?>  
   
 	 <tr>
   	 <th>Branch</th>
     <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:200px" onchange="ShowDiv(this.value,'hid-Godown-div','GoDownByBranch.php')">
      <option value="000">---Select---</option>
      <?php
	while($site_row=DB_fetch_array($site_sql))
	{
	?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo ($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
      <?php } ?>
    </select></td>
    <th>Store</th>
    <td>
	<div id='hid-Godown-div'><?php

	     $sql_store ="select id, StoreCode, StoreName from storetype  where Store_Site='".$_REQUEST['site']."'";
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select name="store" style="width:200px" id="store">
        <option value="0">Select</option>
        <?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{
		$p='';
	 			 if($myrow['id']== $store)
	  			$p="selected";
		?>
        <option value="<?php echo $myrow['id'];?>" <?php echo $p;?>><?php echo $myrow['StoreName'];?></option>
        <?php 
		}
		?>
      </select></div></td>
  </tr>
  	<tr>
   		 <th>Received Date </th>
    	<td><input name="reveive_date" type="text" class="datepicker" id="reveive_date  " style="width:200px"     value="<?php  echo $reveive_date?>" size="15" /></td>
    
  	    <th>Item Code/Name</th> <td>
			 
		<?php  
		
		$SQL_item=db_query("SELECT * from item_master",$db );
		   $n=db_num_rows($SQL_item);
			?> 
		<select style="width:200px;"name="itemcode" onchange="Get_itemname(this.value)" id="itemcode">
			<option value="">--select itemcode--</option>
		<?php while($row_item=db_fetch_array($SQL_item))
			{
			
				$p='';
	 			 if($row_item['id']==$itemcode)
	  			$p="selected";
		
			 ?>
		 	<option value="<?php echo $row_item['id']?>"  <?php echo $p; ?>><?php echo $row_item['item_name'].' ('.$row_item['StockID'].')' ?>			</option>
		     <?php }?>
		</select>
		</td>
	</tr><tr>
    		<th>Opening Qty
	  <td><input style="width:200px;"type="text" name="stock"  id="stock" value="<?php echo $stock ;?>"/>
	  <td><!--Rate--></td>	  
	      <td><!--<input style="width:200px;"type="text" name="rate"  id="rate" value="<?php echo $rate ;?>"/>--></td>	  </tr>
	  <tr>
	  		 <td><!--Amount--></td>
	  		 <td><!--<input style="width:200px;"type="text" name="amount" id="amount" value="<?php echo $amount ;?>" onfocus="calAmt();"/>--></td>
 	 </tr>
 
   <tr>
    <td>&nbsp;</td>
    <td> 
	
      <div align="right">
        <input type="submit" name="Save" value="Save" onclick="return validate()" />
      </div></td>
	  <td colspan="2"><input type="reset" name="Reset" value="Reset">&nbsp;&nbsp;</td>
  </tr>
</table>

</fieldset>

</form>

	<br>
 
 
<script>
	
function validate()
{
if(document.getElementById("site").value=='0')
{
alert("Must Select Branch Name");
document.getElementById("site").focus();
return false;	
}
if(document.getElementById("itemcode").value=='')
{
alert("Must Select Item Name");
document.getElementById("itemcode").focus();
return false;	
}	
if(document.getElementById("stock").value=='')
{
alert("Must Enter Opening Qty");
document.getElementById("stock").focus();
return false;	
}	
if(document.getElementById("rate").value=='')
{
document.getElementById("rate").focus();	
alert("Must Enter Item Rate");
return false;	
}	
return true;	
}	
	
function open_items()
{
//alert(document.getElementById('site').value);	
if(document.getElementById('site').value!='000')
{   var site1=document.getElementById("site").value;
	var store=document.getElementById("store").value;
	window.open("opening_items.php?action=add&site="+site1+"&store="+store+"&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=700,width=1000,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
else
{
alert("Must Select Branch");	
}
	
	
}

	
	
</script> 
 
  