<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
$PageName = 'Add Items';
include('includes/DefinePOClass.php');
include('includes/session.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');
include('includes/PaginationClass.php');
//include('includes/header.inc');
include('includes/function_lib.php');
$table='opening_stockitemwise';
/*$start=0;
$limit=10;
*/
?>
<link rel="stylesheet" href="css/pagnation.css">

<link rel="stylesheet" href="css/jquery.ui.all.css">
 <script src="js/jquery-1.5.1.js"></script>
<script src="ui/jquery.ui.core.js"></script>
<script src="ui/jquery.ui.widget.js"></script>
<script src="ui/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="js/dynamicRow.js"></script>
<script src="js/tax_calculation.js"></script>
<script src="js/add_mrn.js"></script>

<script>
 $(function() {
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
changeYear: true,
yearRange: '-80:+20'});
});
 function popup(url,width,height) 
{
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=yes';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'windowname5', params);
 if (window.focus) {newwin.focus()}
 return false;
 }
 
 

 
 
 </script>
<?php
if($_REQUEST['Save'])
{
	$data['site']=$_REQUEST['site'];
	$data['store']=$_REQUEST['store'];
	
for($i=0;$i<$_REQUEST['row_count'];$i++)
{

if($_REQUEST['row_id'][$i]!='')
{
$SQL_item=DB_fetch_array(DB_query("SELECT * from item_master where id='".$_REQUEST['row_id'][$i]."'",$db ));
//$data['site']=$_REQUEST['site'];
$data['reveive_date']=FormatDateForSQL($_REQUEST['reveive_date'][$i]);
$data['CompanyId']=$_SESSION['ACGCompanyId'];
//$data['store']=$_REQUEST['store'];
//$data['mstid']=$_REQUEST['itemcode'];
$data['itemcode']=$_REQUEST['row_id'][$i];
$data['itemdesc']=$SQL_item['item_name'];
$data['stock']=$_REQUEST['stock'][$i];
$data['rate']=$_REQUEST['rate'][$i];
$data['amount']=$_REQUEST['stock'][$i]*$_REQUEST['rate'][$i];
$data['user_type']=$_REQUEST['type'];


 	$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);
	
		
	unset($_REQUEST['site']);

}
	
	} 
?>
<script type="text/javascript">
	
opener.window.location="index.php?route=DealerDistrbutions/Inventory/transation/Opening_Stock_Item_Wise_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?> ";self.close();


</script>	
 
<?php }
?>
<script> 

function getdata1(str)
	{		
	 
   if (str=="")
  {
  document.getElementById("hiddiv").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("hiddiv").innerHTML=xmlhttp.responseText;
    }
  }
  //alert("mrn_item_list.php.php?str1="+str);
  xmlhttp.open("GET","mrn_item_list.php?str1="+str,true);
xmlhttp.send();
	}
function ExitWindow()
{
opener.window.location="index.php?route=Inventory/transation/material_receipt_note_add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&type=Direct";self.close();
}
</script>
<script language="javascript" >
function calAmt(i)
{
   var a=  document.getElementById('stock_'+i).value;
	var   b=  document.getElementById('rate_'+i).value;
    document.getElementById('amount_'+i).value=a*b;
   
}


function check_selectedItem()
{


var countItem=document.getElementById("row_count").value;
//alert(countItem);
var countflag=0;
//alert(countflag);



for ( var i=0; i<countItem;i++)
{

if(document.getElementById("row_id_"+i).checked==true)
{
var countflag=1;
bresk;
}

}
if(countflag==0)
{
alert("Must Select Item");
return false;
}
else
{
return true;
}

 }
function autoSearch(str)
{
	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("ItemListDiv").innerHTML=xmlhttp.responseText;
		
    }
  }
  xmlhttp.open("GET","mrn_item_list_ajax.php?part_name="+str,true);
xmlhttp.send();
}

</script>

<link href="css/professional/default.css" rel="stylesheet" type="text/css" />
 <h2 align="center">Search ITEMS</h2>
<form action="" method="post" >
<table align="center" border="0">
  <tr><td><strong>Search By Category</strong></td>
  <td><select name="category" id="category" onchange="getdata1(this.value)" >
    <option value="">Select Group</option>
    <?php 
		 $store_sql ="SELECT * FROM stockmaincategory  where CompanyId='".$_SESSION['ACGCompanyId']."' ";
		$store_result = DB_query($store_sql,$db);
		while($store_row = DB_fetch_array($store_result))
		{
			$p='';
			if($store_row['id'] == $_REQUEST['category'])	
			$p="selected";
		
		?>
    <option value="<?php echo $store_row['id']; ?>" <?php echo $p;?>><?php echo $store_row['stockmaincategoryname']; ?></option>
    <?php } ?>
  </select></td>
  </tr>
   <tr><td><strong>Search By Sub Category Category</strong></td>
     <td>
	 <div  id="hiddiv">
	 <select name="subcategory"  >
  	 <option value="">Select Sub Group</option>
  	 <?php $store_sql = "SELECT * FROM stockcategory  where stockmaincategory='".$_REQUEST['category']."'";
		$store_result = DB_query($store_sql,$db);
		while($store_row = DB_fetch_array($store_result))
		{
			$p1='';
			if($store_row['id'] == $_REQUEST['subcategory'])
			{	
			$p1="selected";		
		}
			?>
            <option value="<?php echo $store_row['id'] ?>"<?php echo $p1 ?>><?php echo $store_row['categorydescription']; ?></option>
          <?php  } ?>
  	 
           
</select> </div> </td>
  </tr>
  <tr>
  <td><strong>Enter Part Name</strong></td>
  <td><input type="text" name="part_name" value="<?php  echo $_REQUEST['part_name'];?>" />
<?php /*   onkeyup="autoSearch(this.value)"  */?>
  <input name="Search"  value="Search" type="submit" />
  </td>
  </tr>
</table>
</form>
  

<?php 
	$targetpage = "opening_items.php?action=add&site=".$_REQUEST['site']."&store=".$_REQUEST['store']."&modid=6&frmid=170&"; 	//your file name  (the name of this file)
	$limit = 20; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;	
		
if($_REQUEST['Search'] != '')
{
//	  echo "<pre>";
	//  print_r($_REQUEST);
	$filter=array();
	$category=$_REQUEST['category'];
 	$subcategory=$_REQUEST['subcategory'];
 	$partname=$_REQUEST['part_name'];
	 
 	if($partname!='')
 	 $filter[]="item_name like '%".$_REQUEST['part_name']."%'";
 		if($category!='')
 		 $filter[]="CategoryID like '%".$_REQUEST['category']."%'";
 	 if($subcategory!='')
 	 $filter[]="SubCategoryID like '%".$_REQUEST['subcategory']."%'";
	 
 //print_r($filter);
		if(count($filter)>0 ) 	
		{
				$f=implode(" and " ,$filter);
				$cond=$f." order by item_name  asc ";
			       	$sql="Select * from item_master where $filter"; 						

		}
		else{
		
		 	   	$sql="Select * from item_master where  CompanyId='".$_SESSION['ACGCompanyId']."' order by item_name asc  "; 	

		}
	 

	
}else{
 //print_r($filter);
$cond="1=1";
 	  	$sql="Select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."' order by item_name asc  LIMIT $start, $limit"; 	
	 
}

 	$result = DB_query($sql,$db);
echo  Pages('item_master',10,$targetpage,$cond,'id');

?>
<form name="form1" action="" method="post">	
<input type="hidden" name="site" value="<?php echo $_REQUEST['site']; ?>" />
<input type="hidden" name="store" value="<?php echo $_REQUEST['store']; ?>" />	
 <input type="hidden" name="category" value="<?php echo $_REQUEST['category']; ?>" />

<input type="hidden" name="subcategory" value="<?php echo $_REQUEST['subcategory']; ?>" />

<input type="hidden" name="partname" value="<?php echo $_REQUEST['part_name']; ?>" />
<table width="100%" border="0">
	<tr><td>
<input type="submit" name="Save" id="Select" value="Save" onclick="return check_selectedItem()"></td>
		<td>
		</td>
	</tr></table>
  
<?php
$i=0;
  	if($i%2==0)
	{$bg="#CCCCCC";}
	else
	$bg="#C8BECB";

//echo "Select * from item_master where CategoryID!='FG'";
	?>
	<div style="overflow: scroll; height:1000px;" id="ItemListDiv">
		<table width="100%" border="0" cellspacing="3" cellpadding="2" align="center">
        <tr>
          <th>ITEM CODE</th>
          <th>ITEM NAME</th>
          <th>UNIT</th>
          <th>Stock Qty</th>
          <th>Rate</th>
          <th>Amount</th>
          <th>Received Date </th>
        </tr>
	<?php 
	if(DB_num_rows($result)>0)
	{
		while($myrow=DB_fetch_array($result))
	  	{ 	
	  		$parameter=$myrow['id'];
			$stock=$s->GetFieldValue('opening_stockitemwise',"itemcode='".$myrow['id']."' and site='".$_REQUEST['site']."'
			and store='".$_REQUEST['store']."' " ,'stock');
			$rate=$s->GetFieldValue('opening_stockitemwise',"itemcode='".$myrow['id']."'  and site='".$_REQUEST['site']."'
			and store='".$_REQUEST['store']."'" ,'rate');
			$amount=round($s->GetFieldValue('opening_stockitemwise',"itemcode='".$myrow['id']."'  and site='".$_REQUEST['site']."'
			and store='".$_REQUEST['store']."'" ,'amount'),2);
			$reveive_date=$s->GetFieldValue('opening_stockitemwise',"itemcode='".$myrow['id']."'  and site='".$_REQUEST['site']."'
			and store='".$_REQUEST['store']."'" ,'reveive_date');
  ?>
 <tr bgcolor="<?php echo $bg;?>">
      <td><input type="checkbox" name="row_id[<?php echo $i;?>]" id="row_id_<?php echo $i;?>" value="<?php echo $parameter;?>"  />
      
	  <?php echo $myrow['StockID'];?></td>
      <td><?php echo $myrow['item_name'];?></td>
      <td><?php $unit=$s->getField('unitsofmeasure','unitid',$myrow['mst_unitid'],'unitname'); echo $unit;?></td>
     <td><input type="text" name="stock[<?php echo $i;?>]" id="stock_<?php echo $i;?>" <?php echo ($stock!=''?"readonly":''); ?> value="<?php echo $stock;?>"/></td>
      <td><input type="text" name="rate[<?php echo $i;?>]" id="rate_<?php echo $i;?>" value="<?php echo $rate?>"	 <?php echo ($rate!=''?"readonly":''); ?>  /></td>
      <td><input type="text" name="amount[<?php echo $i;?>]" id="amount_<?php echo $i;?>" value="<?php echo ($amount!=0?$amount:'');?>"	<?php echo ($amount!=''?"readonly":''); ?> 
	  onfocus="calAmt(<?php echo $i; ?>);" /></td>
      <td><input type="text" name="reveive_date[<?php echo $i;?>]" id="reveive_date_<?php echo $i;?>" value="<?php echo $reveive_date;?>" class="datepicker"
	  <?php echo ($reveive_date!=''?"readonly":''); ?> 	 /></td>
     
    </tr>
<?php $i++;
		} ?>
		<input type="hidden" name="row_count" id="row_count" value="<?php echo $i;?>">
<?php 
}

 
?>
</table></div>
  </form>


