<?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">Purchase Order Register</h2>
<table width="100%" align="center">
          <tr>
            <td>Branch</td>
            <td><select name="site" id="site" onchange="showsitename160(this.value)">
                <option value="">Select Site</option>
                <?php 		
		echo $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>Warehouse</td>
            <td>	
		<?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">
		<option value="000">--Select--</option> 
		<option value="00">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>From</td>
            <td><input type="text" name="from_date" id="from_date" value="<?php echo $_POST['from_date'];?>" class="datepicker"></td>

            <td>To</td>
            <td><input type="text" name="to_date" id="to_date" value="<?php echo $_POST['to_date'];?>" class="datepicker"></td>

	      <td>Vendor</td>
         <td>
  
      <select name="vendor" id="vendor">
	   <option value="">Select</option>
	   <option value="00" <?php echo($_REQUEST['vendor']=='00'?'selected':'')?>>All</option>
	  <?php 
	  $vendor_sql=DB_query("Select id,vendor_code,vendor_name from suppliers",$db);
	  while($vendor_row=DB_fetch_array($vendor_sql)) 
	  {
	  ?>
	  <option value="<?php echo $vendor_row['id'];?>" <?php echo($_REQUEST['vendor']==$vendor_row['id']?'selected':'')?> ><?php echo str_replace(array("M/s ","M/S"),'',$vendor_row['vendor_name']);?></option>
	  <?php } ?>
      </select>    </td>

   <!-- <td>Item</th>
    <td id="itemid">
    <select name="itemCode" id="itemCode">
	<option value="">Select ITEM</option>
    <?php 
	$itemSql=DB_query("Select id,StockID,item_name from item_master",$db);
	while($itemRow=DB_fetch_array($itemSql))
	{?>
    <option value="<?php echo $itemRow['id'];?>"<?php echo($_REQUEST['itemCode']==$itemRow['id']?'selected':'')?>><?php echo $itemRow['StockID']." - ".$itemRow['item_name'];?></option>
	<?php 	
	}
	?>    	
    </select></td>-->

            <td align="center"><p align="center">
                <input type="submit" name="view" value="View" onClick="return validation();">
                <!--<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() { 
		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("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_po.site='".$_REQUEST['site']."'";
		if($_REQUEST['storecode']!='' && $_REQUEST['storecode']!='00')
		$filter[]="log_po.store='".$_REQUEST['storecode']."'";	
		if($_REQUEST['storecode']=='00')
		$filter[]="1=1";
		if($_REQUEST['vendor']!=''&& $_REQUEST['vendor']!='00')
		$filter[]="log_po.vender_id='".$_REQUEST['vendor']."'";	
		if($_REQUEST['vendor']=='00')
		$filter[]="1=1";	
		if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
		$filter[]="log_po.pr_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['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_po.*,log_po.id as main_id, log_po_details.* from log_po INNER JOIN log_po_details on(log_po.id = log_po_details.parent_id) where $filter1 and log_po.CompanyId='".$_SESSION['ACGCompanyId']."' group by log_po.id order by log_po.pr_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 >PO No. </th>
    <th>PO Date </th>
  <th width="10%">Vendor Name</th>
  <th width="10%">Delivery Date</th>
    <th>Contact Person </th>
    <th style="width:100px;">Part Code</th>
    <th style="width:75px;">Part name </th>
    <th  style="width:50px;">UOM </th>
    <th >PO  Qty</th>
	<th>Received Qty</th>
	<th>Pending Qty</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['pr_no'];?></td>
    <td><?php echo ConvertSQLDate($myrow['pr_date']); ?></td>
    <td><?php 
	  $vendorname=$s->getField('suppliers','id',$myrow['vender_id'],'vendor_name'); echo $vendorname;?></td>
	  <td><?php echo ConvertSQLDate($myrow['delivery_date']);?></td>
    <td><?php echo $myrow['contact_person']; ?>&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_po_details where parent_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 width="11%"  class="jjrs_tabledata1" style="width:22px;"><?php  
		echo $details_row['qty']; ?>            </td>
            <td class="jjrs_tabledata1"  style="width:82px;"><span class="jjrs_tabledata1">
              <?php
			   $details_sql1="Select sum(reciveqty) AS rsum from log_addmrnqty where mst_itemid='".$details_row['mst_itemid']."' AND grn_id IN (select id from log_add_mrn where po_order='".$details_row['parent_id']."')";
	$details_res1=DB_query($details_sql1,$db);
	$rr_row=DB_fetch_array($details_res1);
	echo $rr_row['rsum'];
		$pendingQty = $details_row['qty']-$rr_row['rsum']; ?>            </span></td>
            <td  class="jjrs_tabledata1"><?php echo $pendingQty; ?></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>
