<?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=LOG/masters/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);		
	$site  =$RData['site'];
	$reveive_date=$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']=$_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=LOG/masters/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']=$_REQUEST['itemcode'];
$data['itemdesc']=$SQL_item['item_name'];
$data['stock']=$_REQUEST['stock'];
$data['rate']=$_REQUEST['rate'];
$data['amount']=$_REQUEST['stock']*$_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'] && $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());
	}
	}
	
	unset($_REQUEST['site']);
	
	
 //$s->pageLocation("index.php?route=Inventory/transation/Opening_Stock_Item_Wise_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']);
}

if(isset($_REQUEST['item_popup']))
{
?>	
<script language="javascript" type="text/javascript">

	<?php
if($_REQUEST['action']=='Edit'){?>
	 	window.open("issu_searchpopup.php?action=Edit<?php echo $rurl;?>&id=<?php echo $_REQUEST['id']?>","profiles_popup_window","height=425,width=600,toolbar=no,menubar=no,dependent=yes,resizable=yes");
<?php }else{?>
	
		 window.open("issu_searchpopup.php?1=1<?php echo $rurl;?>&site=<?php echo $_SESSION['site']?>&store=<?php echo $_SESSION['godwan'];?>","profiles_popup_window","height=425,width=600,toolbar=no,menubar=no,dependent=yes,resizable=yes");
		 <?php } ?>

</script>
<?php }
?>

<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>
<link rel="stylesheet" href="css/new_mxerp.css">
<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="">---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='".$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 ;?>"/>
	  <th>Rate
   		<td></th>	  
	      <input style="width:200px;"type="text" name="rate"  id="rate" value="<?php echo $rate ;?>"/>	  </tr>
	  <tr>
	  		 <th></td>
  		       Amount
	  		 <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>
<?php if($_SESSION['frmAdd']=='1'){?>
<p align="left"><a href="#"  onclick="open_items();"><input type="button" class="search" value="Add Stock" /></a>  </p>
<?php }?>

</fieldset>

</form>
<div id="container">
<script type="text/javascript">
function showSlidingDiv(){
$("#slidingDiv").animate({"height": "toggle"}, { duration: 700 });
}
</script>
<div class="add_new"><a href="#" onClick="showSlidingDiv(); return false;"><input type="button" class="search" value="SEARCH" /></a></div>
<div id="slidingDiv" class="centre">
<form action="" method="post">	
<table width="50%" border="0" cellspacing="1" cellpadding="2" align="center">
  <tr><th><strong>Item Name</strong></th>
    <td colspan="3"><select  name="item_name"  style="width:300px;" >
    <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['id']; ?>"><?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"  size="12" /></td>
	<th><strong>To Date</strong></th>
	<td><input type="text" name="todate1" class="datepicker"  size="12"  /></td>
	</tr>
  </table>
  
  <p align="center"><input name="Search" type="submit" value="Search" class="button1" /></p>
  </form>
	</div>
	<br>
 <table width="100%" border="1" cellpadding="3" style="border:1px solid #000000">
  <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>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!='')
		{
		   if($_SESSION['AssignedBranch']!='')
	       {	
		   $GetRecord ="Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."' and site in ('".$_SESSION['AssignedBranch']."') and $filter2 order by id desc";	
		   }
		   else
		   {
		    $GetRecord ="Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."' and $filter2 order by id desc";	
		  
		   }
		
		}
		else {
		if($_SESSION['AssignedBranch']!='0')
	       {
		 $GetRecord ="Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."' and site in ('".$_SESSION['AssignedBranch']."') order by id desc";	
			}
			else
			{
			 $GetRecord ="Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."' order by id desc";	
		
			}
		}
	}	
	
	else {		
		if($_SESSION['AssignedBranch']!='0')
	       {
     $GetRecord = "Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."' and  site in ('".$_SESSION['AssignedBranch']."') order by id desc";
	 }
	 else
	 {
	  $GetRecord = "Select * from $table where CompanyId='". $_SESSION['ACGCompanyId']."' order by id desc";
	 }
	}
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "<td colspan='9'>--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'";
	  
	  $check=DB_query("select mst_itemid from addmrnqty where mst_itemid='".$RowData['itemcode']."' ",$db);
	  $addmrn_check_row=DB_num_rows($check);
	
	  $checkissue=DB_query("select mst_itemid from issueqty where mst_itemid='".$RowData['itemcode']."' ",$db);
	  $addissue_check_row=DB_num_rows($checkissue);
	  $checkreceive=DB_query("select itemid from material_return_details where itemid='".$RowData['itemcode']."' ",$db);
	  $addreceive_check_row=DB_num_rows($checkreceive);
	  $checksitetosite=DB_query("select itemid from transfer_issue_details where itemid='".$RowData['itemcode']."' ",$db);
	  $addsitetosite_check_row=DB_num_rows($checksitetosite);
	  $sitetositereceive=DB_query("select itemid from transfer_receipt_details where itemid='".$RowData['itemcode']."' ",$db);
	  $addsitetositereceive_check_row=DB_num_rows($sitetositereceive);
	  $inc=DB_query("select itemid from inv_material_return_details where itemid='".$RowData['itemcode']."'",$db);
      $inc_check=DB_num_rows($inc);
	
	 
	 if($addmrn_check_row<=0 && $addissue_check<=0 && $addreceive_check_row<=0 &&  $addsitetosite_check_row<=0 
	    && $addsitetositereceive_check_row<=0 && $inc_check<=0 && $recjection_check<=0)
		{
		$status="";
		}
		else
		{
		$status="No Permission";
		}
	  
	  
	  
	  
   ?>
<tr bgcolor="#FFFFFF" onmouseover="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onmouseout="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
  
   <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['itemcode']."'",'item_name');	 ?>	  </td>
    <td><?php echo $RowData['stock']; ?>	  </td>
	<td><?php echo $RowData['rate']; ?>	  </td>
	<td><?php echo $RowData['amount']; ?>	  </td>
    
   <td><div align="center">
   <?php if($_SESSION['frmEdit']=='1'){?>
   <?php if($status=='') { ?> 
   <a href="index.php?route=LOG/masters/Opening_Stock_Item_Wiseedit_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 $status; }?> 
   <?php }else{echo "No Permission";} ?>
   
   </div></td>
	<td><div align="center">
	<?php if($_SESSION['frmDelete']=='1'){?>
	
	<?php if($status=='') { ?> 
	<a href="index.php?route=LOG/masters/Opening_Stock_Item_Wiseedit_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 $status; }?> 
	 <?php }else{echo "No Permission";} ?>
	 
	</div></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;	
}	
	
function open_items()
{
//alert(document.getElementById('site').value);	
if(document.getElementById('site').value!='' )
{   var site1=document.getElementById("site").value;
//alert(site1);
	var store=document.getElementById("store").value;
	//alert(store);
	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> 
 
  