<script>
	function frmvalidate() 
{ 
  if(document.getElementById("Site").value==0) 
  { 
      alert("Select Site"); 
      document.getElementById("Site").focus(); 
      return false; 
  } 
}
</script>   
<script type="text/javascript">
	function checkdate(date)
{
	var from =document.getElementById('from').value;
	var to =document.getElementById('to').value;
	
		var cdate=date.split("/");
		var sdate=from.split("/");
		var edate=to.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=to;
	        
	    }

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

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

<?php 
if($_REQUEST['action']=='delete')
{

	$condition="parantId='".$_REQUEST['id']."'";
	$s->DeleteRecord('purchorderdetails',  $condition);
	$condition="id='".$_REQUEST['id']."'";
	$s->DeleteRecord('purchorders',  $condition);
	$update=DB_query("update comprative set status_active=0 , po_del_status=1 where id='".$_REQUEST['comid']."'",$db);
}

$array=array('IO'=>'Import Order','DO'=>'Domestic Order','SO'=>'Service Order','AO'=>'Asset Order');
//echo "SELECT * FROM purchorders where ordertype!='D' and po_date = '".date('y-m-d')."' ";

if(isset($_POST['Go'])) 
{ 
	$fromdate=$_REQUEST['fdate']; 
	$todate=$_REQUEST['todate']; 
	$fromdate_brk=explode('/',$fromdate); 
	$from=$fromdate_brk[2]."-".$fromdate_brk[1]."-".$fromdate_brk[0]; 
	$todate_brk=explode('/',$todate); 
	$to=$todate_brk[2]."-".$todate_brk[1]."-".$todate_brk[0]; 
	$sql1="SELECT * FROM purchorders  where ordertype!='DO'"; 
	if($_REQUEST['Site']!="") 
	{ 
		$sql1 .=" AND site='".$_REQUEST['Site']."'";
	} 
	if($_REQUEST['fdate']!="" && $_REQUEST['todate']!="") 
	{ 
			$sql1 .=" AND DATE(created_date) BETWEEN '".$from."' and '".$to."'";	
	} 
	
    $SQLPurch=DB_query($sql1,$db); 
} 
else
{
	$query= "SELECT * FROM purchorders where ordertype!='D' and  po_date between '".$_SESSION['financialYrStart']."' and '".$_SESSION['financialYrEnd']."' and  site in('".$_SESSION['AssignedBranch']."') and include_gst='1' ";
	$SQLPurch=DB_query($query,$db);    
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>View Purchase Orders</h1>
<div class="centre" id="slidingDiv">
<form method="post" action="">	
<table width="100%" cellpadding="3" cellspacing="0" border="0">
	<tr class="row1"> 
		<td width="13%" align="right">Branch:</td> 
		<td width="20%"><select name="Site" id="Site"> 
				<option value="0">--Select--</option> 
				<?php 
				$SiteName=DB_query("Select * from locations where mst_companyid='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db); 
				while($SiteRow=DB_fetch_array($SiteName)) 
				{ ?> 
				<option value="<?php echo $SiteRow['loccode']?>"<?php echo($_REQUEST['Site']==$SiteRow['loccode']?'selected':'');?>><?php echo $SiteRow['locationname']?></option>	 
					 
			<?php } ?> 
			</select> 
		</td> 	 
		<td width="13%" align="right">From Date:</td> 
		<td width="20%"><input type="text" name="fdate" class="datepicker" id="fromdate" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" onchange="javascript:checkdate(this.value)"/></td> 
		<td width="13%" align="right">To Date:</td> 
		<td><input type="text" name="todate" class="datepicker" id="todate" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" 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'>
		</td> 
	</tr> 
	<tr class="row1"> 
	  <td colspan="6" style="text-align:center;"><input name="Go" type="submit" value="Search" onclick="return frmvalidate()" class="search" /></td> 
	</tr> 
</table>
</form>
</div>

<p align="left">
		<a href="index.php?route=POModule/process_purchase_order_gst<?php echo $rurl; ?>"><input type="button" value="Process Purchase Order" class="search"></a>
		<!--<a href="index.php?route=POModule/create_direct_purch_order<?php echo $rurl; ?>"><input type="button" value="Direct Purchase Order" class="search"></a>-->
		<!-- <a href="index.php?route=POModule/create_indent_order<?php //echo $rurl; ?>"><input type="button" value="Indent Purchase Order" class="search"></a> -->
	</p>

<table border="0" cellpadding="3" cellspacing="0" width="100%" class="thborder">
	<tr>
		<th width="10%" rowspan="2">Created</th>
		<th width="10%" rowspan="2">Branch</th>
		<th width="9%" rowspan="2">PO No.</th>
		<th width="7%" rowspan="2">Date</th>
		<th rowspan="2">Vendor Name</th>
		<th width="10%" rowspan="2">Comparative No.</th>
		<!--<th>Indent/PR No.</th>-->
		<th width="7%" rowspan="2">MRN Status</th>
		<th width="7%" rowspan="2">PO Status</th>
		<th width="7%" rowspan="2">Valid Till</th>
		<th width="9%" rowspan="2">PO Type</th>
		<th style="text-align:center;" colspan="4">Action</th>
	</tr>
	<tr>		  		
		<th width="4%" style="text-align:center;">Edit</th>
		<th width="4%" style="text-align:center;">Cancel PO</th>
		<th width="4%" style="text-align:center;">Delete</th>
		<th width="4%" style="text-align:center;">Print</th>
	</tr>
<?php $indentArr=array();

if(db_num_rows($SQLPurch)==0)
{?>
<tr><td colspan="13" align="left">No Record found<td></tr>
<?php exit;}
while($PurchDetails=DB_fetch_array($SQLPurch)){ 
$ComparativeNo=$s->GetFieldValue('comprative',"id='".$PurchDetails['comperativeId']."'",'comparative_no');
$indentNo=$s->GetFieldValue('materialrequisitions',"requisitionno='".$PurchDetails['indentPRno']."'",'Reqno');
$indentArr=explode(',',$PurchDetails['indentPRno']);

$PO_status=$PurchDetails['final_app_status'];
if($PO_status=='1'){
    $status="Approved";
}else if($PO_status=='0'){
     $status="Pending";
}
//$Purchase_Approval_status=$PurchDetails['Purchase_Approval_status'];
//$Approval_status=$PurchDetails['Approval_status'];
//if($Purchase_Approval_status==1 && $Approval_status==0)
//{
//$status="Management Approval Pending";
//}
//else if($Purchase_Approval_status==1 && $Approval_status==1)
//{
//$status="Approve";
//}
//else  if($Purchase_Approval_status==0 && $Approval_status==0)
//{
//$status="Pending";
//}
  $cancelpo=$PurchDetails['cancel_status'];

 
?>
<tr>
	<td><?php echo $PurchDetails['created_date']; ?></td>
	<td><?php echo $s->GetFieldValue('locations',"loccode='".$PurchDetails['site']."'",'locationname'); ?></td>
	<td><?php echo $PurchDetails['purchase_ordNo']; ?></td>
	<td><?php echo ConvertSQLDate($PurchDetails['po_date']); ?></td>
	<td><?php echo $s->GetFieldValue('suppliers',"id='".$PurchDetails['vendorId']."'",'vendor_name'); ?></td>
	<td>
		<?php 
		$vendortype=$s->GetFieldValue('suppliers',"id='".$PurchDetails['vendorId']."'",'vendor_type');
		if($indentNo=='')
		{
			$linkedit="index.php?route=POModule/view_purchase_order_edit_process_gst&".$rurl."&id=".$PurchDetails['id']."&action=edit";
		}
		else
		{
			$linkedit="#";
		}
		//echo $PurchDetails['comperativeId'];
		//echo $s->GetFieldValue('comprative',"id='".$PurchDetails['comperativeId']."'",'comparative_no');
		if($PurchDetails['ordertype']=='P'){echo $ComparativeNo;}
	     else if( $PurchDetails['ordertype']=='I') { echo "Indent Order"; }  ?></td>
	<!--<td>
		<?php /*if($indentNo=='')
			{
				echo 'Direct Order';
			}
			else
			{
            	for($a=0;$a<count($indentArr);$a++)
            	{ 
            		if(count($indentArr)>1)
                		echo $s->GetFieldValue('materialrequisitions',"requisitionno='".$indentArr[$a]."'",'Reqno').'</br>';
              		else
                		echo $s->GetFieldValue('materialrequisitions',"requisitionno='".$indentArr[$a]."'",'Reqno');
            	}
           } 
        */?></td>-->


         <td><?php  echo $PurchDetails['Po_recive_status'];?>    </td>
	<td><?php  echo $status;  ?>    </td>
	<td><?php echo ConvertSQLDate($PurchDetails['valid_till']); ?></td>
	<td><?php echo $array[$PurchDetails['purchase_ord_Type']]; ?></td>  
	<td style="text-align:center;"><?php if($status=='Pending' && $indentNo=='' && $cancelpo!=1 && $_SESSION['frmEdit']==1) { ?>	
	<a href="<?php echo $linkedit; ?>"><img src="images/icon-edit.png" height="15" width="15" title="Edit" /></a><?php }
	else if($status=='Pending' && $indentNo!='' && $cancelpo!=1 && $_SESSION['frmEdit']==1) { ?>
	<a href="index.php?route=POModule/view_purchase_order_edit1_gst&<?php echo $rurl; ?>&id=<?php echo $PurchDetails['id'] ?>&action=edit">
	<img src="images/icon-edit.png" height="15" width="15" title="Edit" /></a>
	 <?php } else {?>
	<img src="images/icon-edit.png" height="15" width="15" title="No Permission" style="opacity:0.5;" /><?php } ?></td>			
	<td style="text-align:center;"><?php if($cancelpo==1) { echo "Cancelled"; ?>
	<input type="button" name="reason" value="Reason" class="search" onclick="javascript:popup('poreason.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $PurchDetails['id'];?>',200,220)">
	<? } else if($PO_status!='1'){?>
	<a onclick="popup('pocancel_gst.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $PurchDetails['id'];?>',600,200)" href="#" style="text-decoration:none;" ><img src="images/icon_cancel.png" height="15" width="15" title="Cancel PO" /><!--<input type="button" name="Cancel" value="Cancel PO" class="search" />--></a><?php } else {?> <img src="images/icon_cancel.png" height="15" style="opacity:0.4" width="15" title="Cancel PO" /> <?php } ?></td>
	<td style="text-align:center;"><?php if($cancelpo!=1 && $status=='Pending' && $_SESSION['frmDelete']=='1' ) {  ?>
	<a href="index.php?route=POModule/view_purchase_order_gst&<?php echo $rurl; ?>&id=<?php echo $PurchDetails['id'] ?>&comid=<?php echo $PurchDetails['comperativeId']; ?>&action=delete" onclick="return confirm('Are you sure you want to delete')">
	<img src="images/Icon_Delete.png" height="15" width="15" title="Delete" /></a>
	<? } else if($cancelpo==1) { echo "Cancelled"; } else { ?><img src="images/Icon_Delete.png" height="15" width="15" title="No Permission" style="opacity:0.5;" /><?php }?>
	</td>	
<td style="text-align:center;"><a target="_blank" href="purchase_print_gst.php?id=<?php echo $PurchDetails['id'] ?>"><img src="images/icon_print.jpg" height="20" width="20" title="Print" /></a></td>
</tr>
<?php } ?>
</table>
</div>
</div>
