<?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 validate_branch()
{
	$branch = $('#site').val();
	if($branch == "")
	{
		alert("Select Branch");
		return false;
	}
	return true;
}
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();s

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

	
}
function getvendor(loccode){
	if(loccode!=''){
		var xmlhttp;
		if (window.XMLHttpRequest){ 
		  xmlhttp=new XMLHttpRequest();
		} else {
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function(){
		 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
				var data=xmlhttp.responseText;
				if(data!=''){
					document.getElementById("client").innerHTML=data;
				}				
			}
		}
		xmlhttp.open("GET","Get_vendor.php?vendorName="+loccode,true);
		xmlhttp.send();
	}
}
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<form method="post" action="">
<h1>Vendor Purchase Order Register</h1>
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="3">
    <tr class="row1">
        <td width="13%" align="right">PO Number</td>
        <td width="20%"><input type="text" name="ponum" id="ponum" value="<?php  echo $_POST['ponum']; ?>" ></td>			
        <td width="13%" align="right">From Date</td>
        <td width="20%"><input type="text" name="from_date" value="<?php echo $_POST['from_date'];?>" class="datepicker"></td>
        <td width="13%" align="right">To Date</td>
        <td><input type="text" name="to_date" value="<?php echo $_POST['to_date'];?>" class="datepicker"></td>
	</tr>
    <tr class="row1">
        <td align="right">Site</td>
        <td><select onchange="getvendor(this.value)" name="site" id="site1"> 
                <option value="">Select</option>
                    <?php  $ss1="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')";
                           $site_sql=DB_query($ss1,$db);
                           while($site_row=DB_fetch_array($site_sql))
                           {
                              $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 align="right">Vendor</td>
        <td><select name="client" id="client">
                <option value="">All</option>
                  <?php $SQLEmp=DB_query("SELECT * FROM  suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' order by vendor_name",$db);

                   while($EmpId=DB_fetch_array($SQLEmp))
                  { ?>
                    <option value="<?php echo $EmpId['id']; ?>"<?php if($EmpId['id'] == $_REQUEST['client']) { echo 'selected="selected"'; } ?>><?php echo $EmpId['vendor_name']; ?></option>
            <?php } ?>  
            </select></td>
        <td align="right"></td>
        <td></td>
	</tr>
	<tr class="row1">
        <td align="center" style="text-align:center;" colspan="6"><input type="submit" name="view" value="Search" class="search" onclick="return validate_branch();">
                <!--<input type="submit" name="print" value="Print">
    <input type="submit" name="cancel" value="Cancel">-->
			</td>
          </tr>
  </table>
</form>
<?php 
// print_r($_SESSION);
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[]="purchorders.site like '%".$_REQUEST['site']."%'";
		}
		if($_REQUEST['client']!=''){
			$filter[]="purchorders.vendorId='".$_REQUEST['client']."'";
		}
		if($_REQUEST['ponum']!=''){
			$filter[]="purchorders.purchase_ordNo like '%".$_REQUEST['ponum']."%'";
		}					  
		if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!=''){
			$filter[]="purchorders.po_date between '".$From."' and '".$To."'";
		} else {
			$filter[]="purchorders.po_date between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."'";
		}           
		
	   // $sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
		//$sitename1=$s->getField('suppliers','id',$_REQUEST['vendor'],'vendor_name');
		
		
		/*if($_REQUEST['site']!='' && $_REQUEST['vendor']!='' && $_REQUEST['purch_ord_type']!='' 
	 && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records of Branch ".$sitename."  , vender '".$sitename1."'
	  and  Purchase Order '".$_REQUEST['purch_ord_type']."' From  ".$From." to ".$To."";
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['purch_ord_type']!=''
	 && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records of Branch ".$sitename." ";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']=='' && $_REQUEST['purch_ord_type']!=''
	 && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records   From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']!='' && $_REQUEST['purch_ord_type']!='' 
	   && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of vender   ".$sitename1." ";
	
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']!='' && $_REQUEST['purch_ord_type']!=''
	   && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of Branch ".$sitename."  and vender '".$sitename1."'";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']!='' && $_REQUEST['purch_ord_type']!='' 
	  && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records  of  vender '".$sitename1."' From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['purch_ord_type']=='' 
	  && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of  Branch ".$sitename."";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']=='' && $_REQUEST['purch_ord_type']!=''
	   && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records  of  Purchase Order '".$_REQUEST['purch_ord_type']."' From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']!='' && $_REQUEST['purch_ord_type']!='' 
	  && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records  of Purchase Order '".$_REQUEST['purch_ord_type']."' and vender '".$sitename1."' ";
	
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['purch_ord_type']!='' 
	  && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records  of  Branch ".$sitename."  and Purchase Order '".$_REQUEST['purch_ord_type']."'  From  ".$From." to ".$To."";
	
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['purch_ord_type']!=''
	   && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of   Branch ".$sitename."  and Purchase Order '".$_REQUEST['purch_ord_type']."'";
	
	}
	else if($_REQUEST['site']=='' && $_REQUEST['vendor']=='' && $_REQUEST['purch_ord_type']!=''
	   && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of   item '".$itemname."' ";
	
	}
	else if($_REQUEST['site']!=='' && $_REQUEST['vendor']=='' && $_REQUEST['purch_ord_type']=''
	   && $_REQUEST['from_date']=='' && $_REQUEST['to_date']=='')
	{
	$message="Showing Records  of   Branch '".$sitename."' ";
	
	}
	else if($_REQUEST['site']!='' && $_REQUEST['vendor']=='' && $_REQUEST['purch_ord_type']!='' 
	   && $_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
	{
	$message="Showing Records  of   Branch ".$sitename."  From  ".$From." to ".$To."";
	
	}*/
	//$_SESSION['mess']=$message;
		
		if($filter != '')
		{
		$filter1 = implode(" and ",$filter);
		}
		
		if($filter1!='')
	{	
	      //$sql="SELECT purchorders.*, purchorderdetails.* from purchorders INNER JOIN purchorderdetails on(purchorders.id = purchorderdetails.parantId) where $filter1 and purchorders.site='".$_SESSION['AssignedBranch']."' and purchorders.vendorId='".$_SESSION['vendorid']."' group by purchorders.id";
	      $sql="SELECT purchorders.*, purchorders.id as purchId, purchorderdetails.* from purchorders INNER JOIN purchorderdetails on(purchorders.id = purchorderdetails.parantId) where $filter1 and purchorders.site in('".$_SESSION['AssignedBranch']."')  group by purchorders.id";
	 }
	 else 
	 {
	 
	  if($_SESSION['AssignedSites']!='0')
	  { 
	  
	  	   $sql="SELECT purchorders.*, purchorders.id as purchId, purchorderdetails.* from purchorders INNER JOIN purchorderdetails on(purchorders.id = purchorderdetails.parantId) where sitecode in (".$_SESSION['AssignedSites'].") group by purchorders.id";
	   
	  
	 }
	 else
	 {
	 
	  	   $sql="SELECT purchorders.*, purchorders.id as purchId, purchorderdetails.* from purchorders INNER JOIN purchorderdetails on(purchorders.id = purchorderdetails.parantId) group by purchorders.id";
	   
	  } 
	 // $sql ="Select * from inv_material_return  group by inv_material_return.id ";
	 }

				
	 $_SESSION['vendor_po']=$sql;
}

?>

<?php if($_REQUEST['view']!=''){?>

<p align="center"><strong><?php echo $message; ?></strong> <a href="purchaseorderRegisterPrint1.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" class="search" style="float:right;">Print</a></p>

<div class="overpanel">
<table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
  <tr>
    <th width="4%">S.No.</th>
	<th width="8%">PO No.</th>
    <th width="6%">Date</th>
    <th width="6%">PO Type</th>   
    <th width="10%">Vendor Name</th>   
    <th width="8%">Quotation No.</th>
	<th width="6%">Delivery Date</th>
    <th width="100">Part Code</th>
    <th>Part Name</th>
    <th width="60">UOM</th>
	<th width="80" class="text-right">PO Qty.</th>
    <th width="80" class="text-right">Received Qty.</th>
    <th width="80" class="text-right">Pending Qty.</th>
  </tr>
  <?php 
 // $sql = "Select * from addissue";
  //zecho $sql;
  $sno=1;
 // echo $sql;
  $result=DB_query($sql,$db);
  while($myrow=DB_fetch_array($result))
  {
  	//echo $myrow['id'];
  ?>
  <tr>
    <td><?php echo $sno;?></td>
    <td><?php echo $myrow['purchase_ordNo']; ?></td>
	<td><?php echo $myrow['po_date'];?></td>
	<td><?php echo $myrow['purchase_ord_Type'];?></td> 
	<td><?php echo $s->GetFieldValue('suppliers',"id='".$myrow['vendorId']."'",'vendor_name'); ?></td> 
    <td><?php echo $vernder_quotation=$s->getField('venderquotations','id',$myrow['quotationId'],'quotation_no');?></td>	
	<td><?php echo $myrow['delivery_date'];?></td>
	<?php	?>
	<td colspan="6" style="padding:0;" valign="top">
	<div id="amc-tab">
	<?php 
		$query = "select * from purchorderdetails where parantId='".$myrow['purchId']."'";
		$res = DB_query($query,$db) or die(DB_error());
	?>
		<table width="100%" border="0" cellpadding="3" cellspacing="0">
		<?php 
		if(DB_num_rows($res)>0){
		while($result_order = DB_fetch_array($res))
		{

			$itemname1=DB_query("select * from item_master where id='".$result_order['mst_itemid']."'",$db);
		$itemname2=DB_fetch_array($itemname1);
		?>
			<tr>
				<td width="100"><?php echo $s->getField('item_master','id',$itemname2['id'],'StockID'); ?></td>
				<td><?php echo $itemname2['item_name']; ?></td>
				<td width="60"><?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$itemname2['mst_unitid']."'",'unitname'); ?></td>
				<td width="80" style="text-align:right;"><?php echo $result_order['Quantity']; ?></td>
				<td width="80" style="text-align:right;">
				<?php 
				/*echo $str="SELECT SUM(reciveqty) as rec
        FROM addmrn a,addmrnqty b
        WHERE a.id=b.issue_id AND a.order_no='".$myrow['purchId']."' AND b.mst_itemid='".$result_order['mst_itemid']."'"; */

				$rec=DB_fetch_assoc(DB_query($str="SELECT SUM(reciveqty) as rec
        FROM addmrn a,addmrnqty b
        WHERE a.id=b.issue_id AND a.order_no='".$myrow['purchId']."' AND b.mst_itemid='".$result_order['mst_itemid']."'",$db));
        //echo $str;
				echo $reciveQty=$rec['rec'];
				$poQty= $result_order['Quantity']; ?></td>
				<td width="80" style="text-align:right;"><?php echo ($poQty-$reciveQty); ?></td>
			</tr>
			<?php } }
			else { ?>
			<tr>
			<td>No Record Found</td>
			</tr>
			<?php } ?>
		</table>
	</td>
  </tr>
  <?php  $sno++; }
   if(DB_num_rows($result)>0) { ?>
  <?php } else { ?>
  <tr>
    <td colspan="13" align="center"><strong>No Records</strong></td>
  </tr>
  <?php } ?>
</table>
</div>
<?php } ?>

</div>
</div>
