<?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['reveive_date']=='')
	{
	   $msgErr[]='Please Select Reveiving Date';
	   $status=false;
	}
	if($_REQUEST['itemcode']=='')
	{
	   $msgErr[]='Please Select Item name';
	   $status=false;
	}
	 
	 if($_REQUEST['stock']=='')
	{
	   $msgErr[]='Please Enter Stoct 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'];
	$reveive_date=$RData['reveive_date'];
	$store=$RData['store'];
	$itemcode=$RData['mstid'];
	$itemdesc=$RData['itemdesc'];
	$stock=$RData['stock'];
	$rate=$RData['rate'];
	$amount=$RData['amount'];
	 $size=$RData['size'];
}
 

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['mstid']=$_REQUEST['itemcode'];
	$data['itemcode']=$SQL_item['StockID'];
	$data['itemdesc']=$SQL_item['item_name'];
	$data['stock']=$_REQUEST['stock'];
	$data['rate']=$_REQUEST['rate'];
	$data['amount']=$_REQUEST['stock']*$_REQUEST['rate'];
	$data['size']=$_REQUEST['size'];
	
  	$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*********************************************** 
if($_REQUEST['Save'] &&  $status && $action=='')
{
$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['mstid']=$_REQUEST['itemcode'];
$data['itemcode']=$SQL_item['StockID'];
$data['itemdesc']=$SQL_item['item_name'];
$data['stock']=$_REQUEST['stock'];
$data['rate']=$_REQUEST['rate'];
$data['amount']=$_REQUEST['stock']*$_REQUEST['rate'];
$data['size']=$_REQUEST['size'];


 	$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'] && $stock_row['site']==$_REQUEST['site'] && $stock_row['store']==$_REQUEST['store'])
	{
		$condition1="itemcode='".$_REQUEST['itemcode']."' && site='".$_REQUEST['sitecode']."' && store='".$_REQUEST['store']."'"; 
		$current_bal=$stock_row['stockbalance']+ $_REQUEST['stock'];
		//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['stock'];
	
	$balance_arr['created_by']=$_SESSION['UserID'];  
	//echo "<pre>";
	//	print_r($balance_arr);
	//	exit;
	$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=Inventory/transation/Opening_Stock_Item_Wise_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']);
}
?>

<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>
<script type="text/javascript">
function showSlidingDiv(){
$("#slidingDiv").animate({"height": "toggle"}, { duration: 700 });
}
</script>
<form action="" method="post"><fieldset>
<legend align="center"><h1>Opening Stock Item Wise</h1></legend>

<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border-collapse:collapse;">
 
  <?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  
 	$SQL_client=DB_query("SELECT loccode,prefix,locationname from locations Where companyid='".$_SESSION['ACGCompanyId']."'",$db);
	?>
   <select  name="site" id="site" style="width: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>
    <th>Store</th>
    <td>
	<div id='hid-Godown-div'><?php

	     $sql_store ="select id, StoreCode, StoreName from storetype  where Store_Site='".$site."'";
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select name="store"  style="width:100%;">
        <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['StoreCode'];?></option>
        <?php 
		}
		?>
      </select></div></td>
  </tr>
  	<tr>
   		 <th>Received Date </th>
    	<td><input name="reveive_date" type="text" class="datepicker" id="reveive_date"     value="<?php  echo ConvertSQLDate($reveive_date)?>"  style="width:100%;" /></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:100%;" 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:100%"type="text" name="stock"  id="stock" value="<?php echo $stock ;?>"/>
	  <th>Rate
   		<td></th>	  
	      <input style="width:100%;"type="text" name="rate"  id="rate" value="<?php echo $rate ;?>"/>	  </tr>
	  <tr>
	  		 <th></td>
  		       Amount
	  		 <td><input style="width:100%;"type="text" name="amount" id="amount" value="<?php echo $amount ;?>" onfocus="calAmt();"/></td>
			 <th>Size</th>
			 <?php 
			 
			 
			  $size_sql=DB_query("Select * from size_master",$db);
			 
			 ?>
			 <td><select style="width:100%;" name="size" id="size" />
			<option value="0">--select size--</option>
			<?php 
			while($size_row=DB_fetch_array($size_sql)){?>
			<option value="<?php echo  $size_row['id'] ?>" <?php echo( $size_row['id']==$size?'selected':'')?>><?php echo $size_row['name']?></option>
			<?php }?>
			 </select></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>
<div align="center"><a href="#" onClick="showSlidingDiv(); return false;">SEARCH</a></div>
<div id="container">
<div id="slidingDiv" class="centre">
<form action="" method="post">
<table width="100%" border="1" cellspacing="3" style="border-collapse:collapse;">
  <tr><th><strong>Item Name</strong></th>
    <td colspan="3"><select  name="item_name"  style="width:80%;" >
    <option value="0">Select Item</option>
    <?php 
    $itemdata=DB_query("select * from item_master",$db);
	while($item=DB_fetch_array($itemdata))
	{
    ?>
    <option value="<?php echo $item['StockID']; ?>"><?php echo $item['item_name']; ?></option>
    <?php } ?>
    </select>	
    </td>
	 </tr>
	<tr>
	<th><strong>From Date</strong></th>
	<td><input type="text" name="fromdate1" class="datepicker"  style="100%" /></td>
	<th><strong>To Date</strong></th>
	<td><input type="text" name="todate1" class="datepicker"  style="100%" /></td>
	</tr>
  </table>
  <p align="center"><input name="Search" type="submit" value="Search" class="button1" /></p>
  </form>
	</div>
	<br>
	<p align="right"><!--<a href="tcpdf/Pdf_opening_stock_exp.php?itemcode=<?php echo $_REQUEST['item_name']?>&fromdate=<?php echo $_REQUEST['fromdate1']?>&todate=<?php echo $_REQUEST['todate1']?>">PDF</a>-->
	<!--<a href="opening_stock_print.php?ID=<?php echo $_REQUEST['modid']; ?>&item_name=<?php echo $itemcode?>" target="_blank">Print</a>-->
 	&nbsp;&nbsp;<!--<a href="PO_Order_Print.php?qtid=<?php echo $_REQUEST['VQuotesId'];?>">Print</a>-->
	<a href="opening_stock_print.php?itemcode=<?php echo $_REQUEST['item_name']?>&fromdate=<?php echo $_REQUEST['fromdate1']?>&todate=<?php echo $_REQUEST['todate1']?>" target="_blank">Print</a>
 </p>
 <table width="100%" border="1" style="border-collapse:collapse" cellpadding="3">
  <tr>
  <th><strong>Sno.</strong></th>
    <th><strong>Received Date</strong></th> <th><strong>Branch</strong></th>
    <th><strong>Item Name</strong></th>
    <th><strong>Stock Qty</strong></th>
    <th><strong>Rate</strong></th>
    <th><strong>Amount</strong></th>
	<th><strong>Size</strong></th>
	    <th>Edit</th>
	    <th>Delete</th>
  </tr>
  
    <?php 
    if(isset($_POST['Search']))
	{
	$filter=array();
		$filter1=array();
	
		 $fromDate=$_REQUEST['fromdate1'];
		$fromDateBrk=explode("/",$fromDate);
		$fromDate=$fromDateBrk[0];
		$fromMonth=$fromDateBrk[1];
		$fromYear=$fromDateBrk[2];
		$From=$fromYear."-".$fromMonth."-".$fromDate;
		 $toDate=$_REQUEST['todate1'];
		$toDateBrk=explode("/",$toDate);
		$toDate=$toDateBrk[0];
		$toMonth=$toDateBrk[1];
		$toYear=$toDateBrk[2];
		$To=$toYear."-".$toMonth."-".$toDate;
		if($_REQUEST['item_name']!='0')	
		{
		$filter[]="itemcode  ='".$_REQUEST['item_name']."'";
		}
		if($_REQUEST['fromdate1']!='' && $_REQUEST['todate1']!='')
		$filter[]="reveive_date between '".$From."' and '".$To."'";	
		if($filter!='')
		{
		$filter2=implode(" and ",$filter);
		}
		if($filter2!='')
		{
		   $GetRecord ="Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."' and $filter2 order by id desc";	
		}
		else 
		{
		 $GetRecord ="Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."' order by id desc";	
		}
	}	
	else 
	{		
     $GetRecord = "Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."' order by id desc";
	}
	$_SESSION['opening1_stockitemwise1']=$GetRecord;
   $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))
   {
      if($j++%2==0)
	  $bg="style='background-color:#FFFFFF'";
	  else
	  $bg="style='background-color:#CCCCCC'";
	  
	 
	  
	     $Edit=1;
	         $Delete=1;
	  
	  
	  
	  
	  
	  
   ?>

   
<tr>
  
   <td><?php echo $i; ?></td>
    <td><?php echo ConvertSQLDate($RowData['reveive_date']); ?>	  </td>
	<!-- <td><?php //echo $RowData['AccountsType']; ?>	  </td>-->
	 <td><?php echo $RowData['site'] ?>	  </td>
 	 <td><?php echo $s->GetFieldValue('item_master',"id='".$RowData['mstid']."'",'item_name');	 ?>	  </td>
    <td><?php echo $RowData['stock']; ?>	  </td>
	<td><?php echo $RowData['rate']; ?>	  </td>
	<td><?php echo $RowData['amount']; ?>	  </td>
	<?php 
	$size_name= $s->GetFieldValue('size_master',"id='".$RowData['size']."'",'name');
	?>
	<td><?php echo $size_name; ?>	  </td>
    
   <td><?php if($Edit==1) { ?>
   <a href="index.php?route=Inventory/transation/Opening_Stock_Item_Wise_index&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> <?php } else { echo  $Edit; }?> </td>
	<td><?php if($Delete==1) { ?>
	<a href="index.php?route=Inventory/transation/Opening_Stock_Item_Wise_index&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>
	<?php }  else { echo $Delete; } ?></td>
  </tr>
	<?php $i++;}   
   ?> 
 </table>
 
<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;	
}	
	
	
	
</script> 
 <script type="text/javascript">
function showSlidingDiv(){
$("#slidingDiv").animate({"height": "toggle"}, { duration: 700 });
}
</script>
  