<?php session_name('CAKEPHP'); session_start();
error_reporting(FALSE);
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
$title = _('Purchase Order Register');
$PageName ='Purchase Order Register';	
?>
<script language="javascript" type="text/javascript">

function get_itemsubgroup(str)
{
	if(str=="")
  {
  document.getElementById("sub_id").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("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?main="+str,true);
xmlhttp.send();
}
function get_item(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","Get_itemcode.php?sub="+str,true);
xmlhttp.send();
}
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","Get_itemcode.php?partName="+str,true);
xmlhttp.send();
}



function showsitename160(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)
    {
	var data=xmlhttp.responseText.split("__");	
	document.getElementById("storedata7").innerHTML=data[0];
	  }
  }
  xmlhttp.open("GET","get_sitenamedata2.php?site="+str,true);
	xmlhttp.send();

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

	
}
</script>

<form method="post" action="">


  <h2 align="center">GRN Register</h2>
  <table width="100%" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td align="left" valign="top" width="4%"><strong>Branch</strong></td>
      <td align="left" valign="top" width="9%"><select name="site" id="site"  onchange="showsitename160(this.value)" style="width:100%;">
          <option value="">Select Site</option>
          <?php 		
		 $site_sql = "SELECT loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_result = DB_query($site_sql,$db);
		while($site_row = DB_fetch_array($site_result))
		{
			$p='';
			if($site_row['loccode'] == $_POST['site'])	
			$p="selected";
		?>
          <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
          <?php } ?>
      </select>      </td>
      <td width="5%" align="left" valign="top"><strong>Warehouse</strong></td>
      <td align="left" valign="top" width="9%"><?php
		$sql_store = "select id, StoreCode, StoreName from storetype where CompanyId='".$_SESSION['ACGCompanyId']."' order by StoreCode ASC";
		$resultStkLocs = DB_query($sql_store,$db);
		?>
          <select id="storedata7" name="storecode" style="width:100%;">
            <option value="000">--Select--</option>
            <option value="00" <?php if($_REQUEST['storecode']=='00') { echo 'selected'; } else { echo '';} ?>>All</option>
            <?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{
				$p='';
				if($_REQUEST['storecode']==$myrow['id'])
				$p="selected";
		?>
            <option value="<?php echo $myrow['id']; ?>" <?php echo $p; ?>><?php echo $myrow['StoreName']; ?></option>
            <?php }
		?>
      </select></td>
      <td width="9%" align="left" valign="middle"><strong>Purchase Type</strong></td>
      <td align="left" valign="top" width="9%"><select name="ptype" id="ptype"	style="width:100%;" onchange="changetype()">
          <option value="">Select</option>
		  <option value="all" <?php if($_REQUEST['ptype']=='all') { echo 'selected'; } else { echo '';} ?>>All</option>
          <option value="1" <?php if($_REQUEST['ptype']=='1') { echo 'selected'; } else { echo '';} ?>>LOCAL</option>
          <option value="2" <?php if($_REQUEST['ptype']=='2') { echo 'selected'; } else { echo '';} ?>>IMPORT</option>
          <option value="3" <?php if($_REQUEST['ptype']=='3') { echo 'selected'; } else { echo '';} ?>>FOC</option>
      </select>      </td>
      <td width="9%" align="left" valign="middle"><strong>Supplier Name</strong></td>
      <td align="left" valign="top" width="18%"><select name="vendor" id="vendor"  style="width:100%;" onchange="getaddress(this.value)">
          <option value="0" >---Select---</option>
          <option value="000" <?php if($_REQUEST['vendor']=='000') { echo 'selected'; } else { echo '';} ?>>All</option>
          <?php
			$sql="select id, vendor_code, vendor_name from suppliers";
			$result = DB_query($sql, $db);
			while($myrow1 = DB_fetch_array($result))
			{ 
			$q='';
				if($_REQUEST['vendor']==$myrow1['id'])
				$q="selected";
			?>
          <option  value="<?php echo $myrow1['id']; ?>"<?php echo $q;?> ><?php echo  str_replace(array("M/s ","M/S"),'',$myrow1['vendor_name']); ?></option>
          <?php }
			?>
      </select></td>
      <td width="4%" align="left" valign="middle" ><strong>From</strong></td>
      <td align="left" valign="top" width="9%" ><input type="text" name="from_date" id="from_date" value="<?php echo $_POST['from_date'];?>" class="datepicker" style="width:100%;" /></td>
      <td width="3%" align="left" valign="middle" ><strong>To</strong></td>
      <td align="left" valign="top" ><input type="text" name="to_date" id="to_date" value="<?php echo $_POST['to_date'];?>" class="datepicker" style="width:100%;" /></td>
    </tr>
    <tr>
      <td colspan="12" align="left" valign="top">
	  <p align="center">
    <input type="submit" name="view" value="View" onclick="return validation1();" />
    <!--<input type="submit" name="print" value="Print">
    <input type="submit" name="cancel" value="Cancel">-->
  </p>
	  </td>
    </tr>
	 	
  </table>
</form>
<script type="text/javascript">
function validation() { //alert(document.getElementById("site").value);
		if(document.getElementById("site").value=='')
		{
			alert("Please Select Branch First");
			(document.getElementById("site")).focus();
			return false;
		}
		else if(document.getElementById("storecode").value=='000')
		{
			alert("Please Select Warehouse");
			(document.getElementById("storecode")).focus();
			return false;
		}
		else if(document.getElementById("ptype").value=='')
		{
			alert("Please Select Purchase Type");
			(document.getElementById("ptype")).focus();
			return false;
		}
		else if(document.getElementById("from_date").value=='')
		{
			alert("Please Select From Date");
			(document.getElementById("from_date")).focus();
			return false;
		}
		else if(document.getElementById("to_date").value=='')
		{
			alert("Please Select To Date");
			document.getElementById("to_date").focus();
			return false;
		}
	}
	</script>
<?php 
if($_REQUEST['view']!='')
{
 
 $filter=array();
		
		$fromDate=$_REQUEST['from_date'];
		$fromDateBrk=explode("/",$fromDate);
		$fromDate=$fromDateBrk[0];
		$fromMonth=$fromDateBrk[1];
		$fromYear=$fromDateBrk[2];
		$From=$fromYear."-".$fromMonth."-".$fromDate;
		
		$toDate=$_REQUEST['to_date'];
		$toDateBrk=explode("/",$toDate);
		$toDate=$toDateBrk[0];
		$toMonth=$toDateBrk[1];
		$toYear=$toDateBrk[2];
		$To=$toYear."-".$toMonth."-".$toDate;	
		 
		if($_REQUEST['site']!='')
		$filter[]="log_add_mrn.site='".$_REQUEST['site']."'";
		if($_REQUEST['storecode']!='' && $_REQUEST['storecode']!='00')
		$filter[]="log_add_mrn.store='".$_REQUEST['storecode']."'";	
		if($_REQUEST['storecode']=='00')
		$filter[]="1=1";
		if($_REQUEST['ptype']!='' && $_REQUEST['ptype']!='all')
		$filter[]="log_add_mrn.ptype='".$_REQUEST['ptype']."'";
		if($_REQUEST['ptype']=='all')
		$filter[]="1=1";
		if($_REQUEST['vendor']!='' && $_REQUEST['vendor']!='000')
		$filter[]="log_add_mrn.vendor='".$_REQUEST['vendor']."'";
		if($_REQUEST['vendor']=='000')
		$filter[]="1=1";
		if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
		$filter[]="log_add_mrn.grn_date between '".$From."' and '".$To."'";		
		//print_r($filter);exit();
	    $sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
		
		$sitename1=$s->getField('suppliers','id',$_REQUEST['vendor'],'vendor_name');
		$storeNames=$s->getField('storetype','id',$_REQUEST['storecode'],'StoreName');
		if($_REQUEST['ptype']=="1") { $purchType = 'Local'; } elseif($_REQUEST['ptype']=="2") { $purchType = 'Import'; } elseif($_REQUEST['ptype']=="3") { $purchType = 'FOC'; }
		
		if($_REQUEST['site']!='' && $_REQUEST['vendor']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['storecode']!='')
	{
	$message="Showing Records of Branch ".$sitename."  , vendor '".$sitename1."', Warehouse '".$storeNames."' From  ".$From." to ".$To."";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='' && $_REQUEST['storecode']=='')
	{
	$message="Showing Records of Branch ".$sitename." ";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']=='' && $_REQUEST['storecode']=='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records   From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']==''  && $_REQUEST['storecode']=='' && $_REQUEST['vendor']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of vendor   ".$sitename1." ";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']!='' && $_REQUEST['storecode']!='' && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of Branch ".$sitename.", Warehouse '".$storeNames."' and vendor '".$sitename1."'";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']!='' && $_REQUEST['storecode']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records  of  vendor '".$sitename1."' From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']!='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='' && $_REQUEST['storecode']!='')
	{
	$message="Showing Records  of vendor '".$sitename1."', Warehouse '".$storeNames."' From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['storecode']=='' && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records  of  Branch ".$sitename."  From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['storecode']==''  && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of   Branch ".$sitename."";
	}
	
	//$_SESSION['mess']=$message;
		
		if($filter != '')
		{
		$filter1 = implode(" and ",$filter);
		}
		
		if($filter1!='')
	{	
	     	
			 $sql="SELECT log_add_mrn.*,log_add_mrn.id as main_id, log_add_mrn_details.* from log_add_mrn INNER JOIN log_add_mrn_details on(log_add_mrn.id = log_add_mrn_details.grn_id) where $filter1 and log_add_mrn.CompanyId='".$_SESSION['ACGCompanyId']."' group by log_add_mrn.id order by log_add_mrn.grn_date ";
	 }
	
 //echo $sql;
				
	 //$_SESSION['data1']=$sql;
}

?>
<p align="right"><a href="../../POModule/reports/purchaseorderRegisterPrint2.php?site=<?php echo $_REQUEST['site'];?>&store_code=<?php echo $_REQUEST['store1']?>&fromdate=<?php echo $_REQUEST['from_date']?>&todate=<?php echo $_REQUEST['to_date']?>&itemId=<?php echo $_REQUEST['itemCode']?>&vendor=<?php echo $_REQUEST['vendor'];?>" target="_blank">Print</a></p>
<?php if($_REQUEST['view']!=''){?>
<div style="width:auto; height:auto; overflow:auto; margin-left:auto; margin-right:auto;">
<div align="center"><strong><?php echo $message; ?></strong> </div>
<br />
<table width="100%" cellpadding="3" cellspacing="0" border="1" style="border:solid 1px #666666;">
  <tr>
    <th >S. No. </th>
    <th >GRN No.</th>
    <th>GRN Date</th>
  <th width="10%">Supplier Name</th>
  <th width="10%">Invoice No</th>
    <th>Invoice Date </th>
	<th>Vendor Invoice Value</th>
    <th style="width:100px;">Part Code</th>
    <th style="width:75px;">Part name </th>
    <th  style="width:50px;">UOM </th>
	<th>Received Qty</th>
	<th>Total Box</th>
  </tr>
  <?php 
 // $sql = "Select * from addissue";
  //zecho $sql;
  $sno=0;
  $result=DB_query($sql,$db);
 	$numrow1=DB_num_rows($result);
	if(DB_num_rows($result)>0){
  while($myrow=DB_fetch_array($result))
  {
  $sno++;	
  ?>
  <tr bgcolor="#FFFFFF">
    <td height="34" ><?php echo $sno;?></td>
    <td><?php echo $myrow['grn_no'];?></td>
    <td><?php echo ConvertSQLDate($myrow['grn_date']); ?></td>
    <td><?php 
	  $vendorname=$s->getField('suppliers','id',$myrow['vendor'],'vendor_name'); echo $vendorname;?></td>
	  <td><?php echo $myrow['document_no'];?>&nbsp;</td>
    <td><?php echo ConvertSQLDate($myrow['vendor_invoice_date']); ?>&nbsp;</td>
    <td><?php echo $myrow['vendor_invoice_val']; ?>&nbsp;</td>
        <td colspan="9" align="left" valign="top"><table width="100%" height="25" border="0" cellpadding="0" cellspacing="0">
          <?php
    
	   $details_sql="Select * from log_addmrnqty where grn_id='".$myrow['main_id']."'";
	//$details_sql="Select * from purchorderdetails where orderno='".$myrow['orderno']."' 
	//and mst_itemid='".$_REQUEST['itemCode']."'";
	$details_res=DB_query($details_sql,$db);
	$issueqty[]='';
	$req_qty[]='';
	$numrow1=DB_num_rows($details_res);
	if(DB_num_rows($details_res)>0){
	while($details_row=DB_fetch_array($details_res)){?>
          <tr bgcolor="99CCCC">
            <td style="width:103px;" class="jjrs_tabledata1" ><?php echo  $s->getField('item_master','id',$details_row['mst_itemid'],'StockID'); ?></td>
            <td class="jjrs_tabledata1" style="width:90px;"><?php echo $s->getField('item_master','id',$details_row['mst_itemid'],'item_name'); ?></td>
            <td class="jjrs_tabledata1" style="width:62px;"><?php  $unut = $s->getField('item_master','id',$details_row['mst_itemid'],'mst_unitid');
		echo $s->getField('unitsofmeasure','unitid',$unut,'unitname');
		 ?>            </td>
            <td class="jjrs_tabledata1" style="width:62px;"><?php  
		echo $details_row['reciveqty']; ?>            </td>
            <td  class="jjrs_tabledata1"><?php
			 $query="SELECT * FROM log_addmrnbox WHERE grn_id='".$myrow['main_id']."' and itemid ='".$details_row['mst_itemid']."' ";
			 $details_res3=DB_query($query,$db);
echo $numrow3=DB_num_rows($details_res3);			//echo count($box);  ?></td>
          </tr>
          <?php } ?>
          <?php } 
	   else { ?>
          <tr bgcolor="#e7e7e7" style="height: 25px">
            <td class="jjrs_tabledata1" colspan="8" align="center"><strong><span class="jjrs_tabledata1" style="width:90px;"><?php echo $s->getField('item_master','id',$details_row['mst_itemid'],'item_name'); ?></span>No Records</strong></td>
          </tr>
          <?php } ?>
        </table></td>
  </tr>
  <?php  } ?>
  <?php  }
  else 
  { ?>
  <tr colspan= "2" bgcolor="#e7e7e7" style="height: 25px;">
    <td class="jjrs_tabledata1" colspan="8" align="center"><strong>No Records Found</strong></td>
   </tr>
 <?php } ?>
</table>
</div>
<?php } ?>

	</html>

