<?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';	
$cur_date=date('d/m/Y');
?>
<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("vendor").innerHTML=data[2];
	  }
  }
  xmlhttp.open("GET","get_sitenamedata2_datalist.php?site="+str,true);
	xmlhttp.send();

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

	
}
</script>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Purchase Order Register</h1>
<form method="post" action="">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
          <tr class="row1">
            <td width="13%" align="right">Branch</td>
            <td width="20%"><select name="site" id="site" onChange="showsitename160(this.value)">
                <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 width="13%" align="right">PO Type</td>
		  	<td width="20%"><select name="purch_ord_type" id="purch_ord_type" onChange="showhideorddive(this.value);"  >
				<option value="">All</option>
				<option value="IO" <?php echo $_REQUEST['purch_ord_type']=='IO'?'selected':''?>>Import Order</option>
				<option value="DO" <?php echo $_REQUEST['purch_ord_type']=='DO'?'selected':''?>>Domestic Order</option>
			</select></td>
			<td width="13%" align="right">Vendor Name</td>
			<td><!--<select name="vendor" id="vendor"> -->
	  			<!--<option value="">All</option> -->
  			  <input type="text" id="vendor" name="vendor" list="vendor1" placeholder="Vendor Name" value ="<?php echo $_REQUEST['vendor']; ?>" autocomplete="off">
	  			<datalist id="vendor1">
			 	 <?php 
			  	$vendor_sql=DB_query("Select id,vendor_code,vendor_name from suppliers order by vendor_name",$db);
			 	 while($vendor_row=DB_fetch_array($vendor_sql)) 
			  	{
	  				  echo "<option  data-xyz='".$vendor_row['id']."' value='".$vendor_row['vendor_name']."'></option>";
	   } ?>
	   </datalist>
	    <input type="hidden" name="bbb" id="bbb" value="<?php echo $_REQUEST['bbb'];?>">
        <!-- </select> -->   
			</td>
		</tr>
	    <tr class="row1">
		  	<td align="right">PO No.</td>
			<td><input name="purchase_ordNo" type="text" value="<?php echo $_REQUEST['purchase_ordNo'];?>" /></td>
            <td align="right">From Date</td>
            <td><input type="text" name="from_date" class="datepicker" id="fromdate" value="<?php if(isset($_REQUEST['from_date'])) { echo $_REQUEST['from_date']; } else { echo ConvertSQLDate($_SESSION['financialYrStart']); } ?>" onchange="javascript:checkdate(this.value)" /></td>
            <td align="right">To Date</td>
            <td><input type="text" name="to_date" class="datepicker" id="todate" value="<?php if(isset($_REQUEST['to_date'])) { echo $_REQUEST['to_date'];} else { echo $cur_date; } ?>"  onchange="javascript:checkdate(this.value)">
            	<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" id='from'>
				<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" id='to'>
				<input type="hidden" id="cur_date" value="<?php echo $cur_date;?>">
            </td>
		</tr>
		<tr class="row1">
            <td 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 
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='".$_REQUEST['site']."'";
		if($_REQUEST['purch_ord_type']!='')
		$filter[]="purchorders.purchase_ord_Type='".$_REQUEST['purch_ord_type']."'";
		if($_REQUEST['vendor']!='')
		$filter[]="purchorders.vendorId='".$_REQUEST['bbb']."'";

	    if($_REQUEST['purchase_ordNo']!='')
		$filter[]="purchorders.purchase_ordNo like '%".$_REQUEST['purchase_ordNo']."%'";
					  
		if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
		$filter[]="purchorders.po_date between '".$From."' and '".$To."'";		
		
	    $sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
		$sitename1=$s->getField('suppliers','id',$_REQUEST['bbb'],'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.id AS MainId,purchorders.*, purchorderdetails.* from purchorders INNER JOIN purchorderdetails on(purchorders.id = purchorderdetails.parantId) where $filter1 group by purchorders.id";
	         // echo $sql;
	}
	 else 
	 {	 
	  if($_SESSION['AssignedSites']!='0')
	   {   
	  	   $sql="SELECT purchorders.id AS MainId,purchorders.*, purchorderdetails.* from purchorders INNER JOIN purchorderdetails on(purchorders.id = purchorderdetails.parantId) where sitecode in (".$_SESSION['AssignedSites'].") group by purchorders.id";
	   }
	  else
	   { 
	  	   $sql="SELECT purchorders.id AS MainId,purchorders.*, purchorderdetails.* from purchorders INNER JOIN purchorderdetails on(purchorders.id = purchorderdetails.parantId) group by purchorders.id";
	   } 
	 }
	$_SESSION['POorderRegPrnt']=$sql; 
}

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

<p align="center"><strong><?php echo $message; ?></strong> <a href="purchaseorderRegisterPrint.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['modid']?>&vendor=<?php echo $_REQUEST['vendor'];?>" target="_blank" class="search" style="float:right;">Print </a></p>

<div class="overpanel">
<table width="130%" cellpadding="3" cellspacing="0" border="0" class="thborder">
  <tr>
    <th width="3%">S. No.</th>
    <th width="5%">Date</th>
    <th width="8%">PO No.</th>
    <th width="5%">PO Type</th>
   <?php if($_REQUEST['vendor']=='') {?> <th>Vendor Name</th>
   <?php } ?>
    <th width="7%">Quotation No.</th>
    <th width="5%" class="text-right">PO Amt.</th>
	<th width="6%">Delivery Date</th>
    <th width="100">Item Code</th>
    <th width="140">Item Name</th>
    <th width="70">UOM</th>
	<th width="95" class="text-right">PO Qty</th>
    <th width="95" class="text-right">Received Qty.</th>
    <th width="95" class="text-right">Free Qty.</th>
    <th width="95" class="text-right">Short Close Qty.</th>
    <th width="95" class="text-right">Pending Qty.</th>
  </tr>
  <?php 
  $sno=1;
  $result=DB_query($sql,$db);
  while($myrow=DB_fetch_array($result))
  { ?>
  <tr>
    <td style="text-align:center;"><?php echo $sno;?></td>
    <td nowrap="nowrap"><?php echo $myrow['po_date'];?></td>
    <td nowrap="nowrap"><?php echo $myrow['purchase_ordNo']; ?></td>
	<td><?php echo $myrow['purchase_ord_Type'];?></td>
   <?php if($_REQUEST['vendor']=='') {?> <td><?php 
	  $vendorname=$s->getField('suppliers','id',$myrow['vendorId'],'vendor_name');
	  echo $vendorname;?></td><?php } ?>	 
    <td nowrap="nowrap"><?php echo $vernder_quotation=$s->getField('venderquotations','id',$myrow['quotationId'],'quotation_no');?></td>	
	<td align="right"><a href="purchase_print_gst.php?id=<?php echo $myrow['MainId'] ?>" target="_blank"><?php echo $myrow['GrandTotal'];?></a></td>
	<td><?php echo $myrow['delivery_date'];?></td>
	<?php
	?>
	<td colspan="8" style="padding:0px;">
	<div id="amc-tab">
	<?php 
		$query = "select * from purchorderdetails where parantId='".$myrow['MainId']."'";
		$res = DB_query($query,$db) or die(DB_error());
	?>
		<table width="100%" cellpadding="3" cellspacing="0" border="0">
		<?php 
		if(DB_num_rows($res)>0){
		while($result_order = DB_fetch_array($res))
		{   //echo "select * from item_master where id='".$result_order['mst_itemid']."'";
			$itemname1=DB_query("select StockID,item_name,mst_unitid from item_master where id='".$result_order['mst_itemid']."'",$db);
		$itemname2=DB_fetch_array($itemname1);
		?>
			<tr>
				<td width="100"><?php echo $itemname2['StockID'];?></td>
				<td width="140"><?php echo $itemname2['item_name']; ?></td>
				<td width="70"><?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$itemname2['mst_unitid']."'",'unitname'); ?></td>
				<td width="95" style="text-align:right;"><?php echo $result_order['Quantity']; ?></td>
				<td width="95" style="text-align:right;"><?php 
                               //echo  $parentIDD = $s->GetFieldValue('addmrn',"order_no='".$myrow['MainId']."'",'id'); 
				//echo $reciveQty = $s->GetFieldValue('addmrnqty',"issue_id='".$parentIDD."'",'reciveqty');
				$rec=DB_fetch_assoc(DB_query("SELECT SUM(reciveqty) as rec
				FROM addmrn a,addmrnqty b
				WHERE a.id=b.issue_id AND a.order_no='".$myrow['MainId']."' AND b.mst_itemid='".$result_order['mst_itemid']."'",$db));
				echo $reciveQty=$rec['rec'];
				$poQty= $result_order['Quantity'];

				$short_close_qty=$s->GetFieldValue('po_short_close',"po_id='".$parentIDD."' and item_id='".$result_order['mst_itemid']."'",'short_close');
		 ?></td>
				<td width="95" style="text-align:right;"><?php echo number_format((float)($result_order['free_qty']),2,'.',','); ?></td>
				<td width="95" style="text-align:right;"><?php echo number_format((float)($short_close_qty),2,'.',','); ?></td>
				<td width="95" style="text-align:right;"><?php echo number_format((float)($poQty-$reciveQty-$short_close_qty+$result_order['free_qty']),2,'.',','); ?></td>
			</tr>
			<?php } }
			else { ?>
			<tr>
			<td colspan="6">No Record Found</td>
			</tr>
			<?php } ?>
		</table>
		</div>
		</td>
  </tr>
  <?php  $sno++; }
   if(DB_num_rows($result)>0) { ?>
  <?php } else { ?>
  <tr>
   <td colspan="16" align="center"><strong>No Records</strong></td>
  </tr>
  <?php } ?>
</table>
</div>
</div>
</div>
<?php } ?>
<script type="text/javascript">
	function checkdate(date)
{
	var from =document.getElementById('from').value;
	var to =document.getElementById('to').value;
	var cur_date =document.getElementById('cur_date').value;
	
	
		var cdate=date.split("/");
		var sdate=from.split("/");
		var edate=cur_date.split("/");

		cdate[1]=cdate[1]-1;
		sdate[1]=sdate[1]-1;
		edate[1]=edate[1]-1;
			 
		var dateC = new Date(cdate[2], cdate[1], cdate[0]); //Year, Month, Date 
		var dateS = new Date(sdate[2], sdate[1], sdate[0]); //Year, Month, Date
	    var dateE = new Date(edate[2], edate[1], edate[0]); //Year, Month, Date

	    if (dateS > dateE) {
	        alert("Start Date is not greather then Date End.");
	        document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=cur_date;
	        
	    }

	    if (dateC > dateE) {
	        alert("Date Not greather than "+cur_date);
	       document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=cur_date;
	       
	    }

	    if (dateC < dateS) {
	        alert(" Date Not less than "+from);
	        document.getElementById('fromdate').value=from;
	        document.getElementById('todate').value=cur_date;
	       
	    }	
	
	
}



$( document ).ready(function() {

$(function() {
$("#vendor").change(function (){
 var val = $('#vendor').val()
    var xyz = $('#vendor1 option').filter(function() {
        return this.value == val;
    }).data('xyz');
   // alert(xyz);
   // var msg = xyz ? '' + xyz : 'No Match';
$('#bbb').val(xyz);
   // alert(msg)
}); 
})





		//alert();
      // var subjectId = $('#subjects option:selected').attr('data-subjectid');
 

  //  var subjectId = $('#subjects option:selected').attr('data-subjectid');


});


</script>
