<link rel="stylesheet" href="css/new_mxerp.css">

<?php
$PageSecurity = 2;
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
///get form permissions///
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'], $_SESSION['ACGRoleId']);
$_SESSION['frmEdit']=$frmpermission[1];
$_SESSION['frmDelete']=$frmpermission[2];
$_SESSION['frmView']=$frmpermission[3];

$title = _('Search Outstanding Purchase Orders');
if (isset($_GET['SelectedStockItem'])){
	$SelectedStockItem=trim($_GET['SelectedStockItem']);
} elseif (isset($_POST['SelectedStockItem'])){
	$SelectedStockItem=trim($_POST['SelectedStockItem']);
}

if (isset($_GET['OrderNumber'])){
	$OrderNumber=trim($_GET['OrderNumber']);
} elseif (isset($_POST['OrderNumber'])){
	$OrderNumber=trim($_POST['OrderNumber']);
}

if (isset($_GET['SelectedSupplier'])){
	$SelectedSupplier=trim($_GET['SelectedSupplier']);
} elseif (isset($_POST['SelectedSupplier'])){
	$SelectedSupplier=trim($_POST['SelectedSupplier']);
}

echo '<form action="" method=post>';

if($_REQUEST['action'] == 'delete' && $_REQUEST['OrderNumber'] != '' && $_REQUEST['reqno']!='' && $_REQUEST['quotno']!='')
{
	$delete_sql1 = "delete from purchorderdetails where orderno='".$_REQUEST['OrderNumber']."' ";
	$delete_result1 = DB_query($delete_sql1,$db);
	$delete_sql = "delete from purchorders where orderno='".$_REQUEST['OrderNumber']."' ";
	$delete_result = DB_query($delete_sql,$db);
	$sql3 = "update comprative_detail set status_active='0' where Vendor_Quot_No='".$_REQUEST['quotno']."' and RequisitionNo='".             $_REQUEST['reqno']."' and status_active='1'";
	$result3=DB_query($sql3,$db);
	$sql4 = "update venderquotations set status_lock='0' where id='".$_REQUEST['quotno']."'";
	$result4=DB_query($sql4,$db);
	$delete_charge="Delete from po_charge where Orderno = '".$_REQUEST['OrderNumber']."'";
	$charge_res=DB_query($delete_charge,$db);
	echo "<script>location.href='index.php?route=PO_SelectOSPurchOrder&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

if (isset($_POST['ResetPart'])){
     unset($SelectedStockItem);
}
      $type="paging";
		$rowsPerPage = 10;
		$pageNum = 1;
		if(isset($_GET['page']))
		{
			$pageNum = $_GET['page'];
		}
		$offset = ($pageNum - 1) * $rowsPerPage;

/* Not appropriate really to restrict search by date since user may miss older ouststanding orders
	$OrdersAfterDate = Date("d/m/Y",Mktime(0,0,0,Date("m")-2,Date("d"),Date("Y")));
*/
?>
<div class="body_contain">
<div class="tab_div">


<?php

echo '<h1>' . ' ' . $title.'</h1>';
?>

   	<table width="100%" border="0" cellspacing="2" cellpadding="2" align="center">
	<tr class="row1">
	<td class="tabledata1" style="250px;">Order Number</td>
	<td><input type="text" style="width:150px;" name="OrderNumber"  value="<?php echo $_POST['OrderNumber']; ?>"></td>
	<tr class="row1">
	<td class="tabledata1"><strong>Site</strong></td>
	<td><select name="StockLocation">
	<option value="000">---Select Site---</option>
   <?php
   	$sql = "SELECT loccode, locationname FROM locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$_SESSION['AssignedBranch']."'";
	$resultStkLocs = DB_query($sql,$db);
	while ($myrow=DB_fetch_array($resultStkLocs))
	{
		$p='';
		if($myrow['loccode']==$_POST['StockLocation'])
		$p="selected";
	?>
		<option value="<?php echo $myrow['loccode'];?>"<?php echo $p;?>><?php echo $myrow['locationname'];?></option>
	<?php } ?>
 	</select></td></tr>
	<tr class="row1">
	<td class="tabledata1">Vendor</td>
	<td><select name="vendor">
	<option value="000">---Select Vendor---</option>
	<?php 
	$vendor_sql = "Select vendor_code, vendor_name from suppliers";
	$vendor_res = DB_query($vendor_sql,$db);
	while($vendor = DB_fetch_array($vendor_res))
	{
		$p='';
		if($vendor['vendor_code'] == $_POST['vendor'])
		$p="selected";
	?>
	<option value="<?php echo $vendor['vendor_code']; ?>"<?php echo $p;?>><?php echo $vendor['vendor_name']; ?></option>
	<?php } ?>
	</select>
	</td>
	</tr>
  <tr class="row1">
    <td class="tabledata1">Date</td>
    <td><input type="text" name="fromdate" id="datepicker"  size="10" value="<?php echo $_POST['fromdate'];?>"/>
    To 
    <input type="text" name="todate" id="datepicker1" size="10" value="<?php echo $_POST['todate'];?>"/>
    </td>
  </tr>
</table>
	<p align="left" style="padding-left:10px;"><input type="submit" name="SearchOrders" VALUE="Search"></p>
<?php
if (isset($StockItemsResult)) 
{
	echo '<table cellpadding=2 colspan=7 BORDER=2>';
	$TableHeader = 	'<tr><th>' . _('Code') . '</th>
			<th>' . _('Description') . '</th>
			<th>' . _('On Hand') . '</th>
			<th>' . _('Orders') . '<br>' . _('Outstanding') . '</th>
			<th>' . _('Units') . '</th>
			</tr>';
	echo $TableHeader;
	$j = 1;
	$k=0; //row colour counter

	while ($myrow=DB_fetch_array($StockItemsResult)) {

		if ($k==1){
			echo '<tr class="EvenTableRows">';
			$k=0;
		} else {
			echo '<tr class="OddTableRows">';
			$k=1;
		}

		printf("<td><input type=submit name='SelectedStockItem' VALUE='%s'</td>
		        <td>%s</td>
			<td align=right>%s</td>
			<td align=right>%s</td>
			<td>%s</td></tr>",
			$myrow['stockid'],
			$myrow['description'],
			$myrow['qoh'],
			$myrow['qord'],
			$myrow['units']);

		$j++;
		If ($j == 12){
			$j=1;
			echo $TableHeader;
		}
//end of page full new headings if
	}
//end of while loop

	echo '</table>';
}
//end if stock search results to show
else
{
	//figure out the SQL queries required from the inputs available
$completed = " AND purchorderdetails.completed=0";
	      /* $DateAfterCriteria = FormatDateforSQL($OrdersAfterDate); */
			if(isset($_POST['SearchOrders'])) 
				{
					$fromdate = $_POST['fromdate'];
					$todate = $_POST['todate'];
					
					$fromdate_break = explode("/", $fromdate);
					$from_day = $fromdate_break['0'];
					$from_Month = $fromdate_break['1'];
					$from_year = $fromdate_break['2'];
					$fromdate1 = $from_year."-".$from_day."-".$from_Month;
					
					$todate_break   = explode("/", $todate);
					$to_day     = $todate_break['0'];
					$to_Month   = $todate_break['1'];
					$to_year    = $todate_break['2'];
					$todate1  = $to_year."-".$to_day."-".$to_Month;
					
					$filter = array();
			        $sql = "select * from purchorders ";
					$resultpaging_SQL="select * from purchorders ";
					
					if(isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != '')
					$filter[] = "PO_Orderno LIKE '%".$_POST['OrderNumber']."'";
					if(isset($_POST['StockLocation']) && $_POST['StockLocation'] != '000')
					$filter[] = "sitecode = '".$_POST['StockLocation']."'";
					if(isset($_POST['vendor']) && $_POST['vendor'] != '000')
					$filter[] = "supplierno = '".$_POST['vendor']."'";
					if(isset($_POST['fromdate']) && $_POST['fromdate'] != '' && isset($_POST['todate']) && $_POST['todate'] != '')
			$filter[] = "orddate BETWEEN '".$fromdate1."' and '".$todate1."' ";
				
				if($filter != '')
				{
					$sql=$sql." where    CompanyId='".$_SESSION['ACGCompanyId']."' and ".implode(" and ",$filter);
				}
				
				}
			else
			{
				$sql = "select * from purchorders  where  CompanyId='".$_SESSION['ACGCompanyId']."' order by orddate DESC";
				$resultpaging_SQL="select * from purchorders  where  CompanyId='".$_SESSION['ACGCompanyId']."' ";
			}
			
				
	$ErrMsg = _('No orders were returned by the SQL because');
	$PurchOrdersResult = DB_query($sql,$db,$ErrMsg);
	//$myrow = DB_fetch_array($PurchOrdersResult);
	//$resultpaging = DB_query($sql ,$db,$ErrMsg);
	$resultpaging = DB_query($resultpaging_SQL ,$db,$ErrMsg);
	/*show a table of the orders returned by the SQL */
	?>
	<table cellpadding="3" cellspacing="0" border="1" WIDTH="100%" style="border: solid 1px #CCC">
	<tr>
	<td class="tabledata"><strong>Order Number</strong></td>
	<td class="tabledata"><strong>Date</strong></td>
	<td class="tabledata"><strong>Branch</strong></td>
	<td class="tabledata"><strong>Party</strong></td>
	<td class="tabledata"><strong>Status</strong></td>
	<td class="tabledata"><strong>View</strong></td>
	<td class="tabledata"><strong>Edit</strong></td>
	<td class="tabledata"><strong>Delete</strong></td>
	<td class="tabledata"><strong>Print</strong></td>
	</tr>
	<?php
	$j = 1;
	$k=0; //row colour counter
	if(DB_num_rows($PurchOrdersResult)>0)
	{
	while ($myrow=DB_fetch_array($PurchOrdersResult)) 
	{
		if ($k==1){ /*alternate bgcolour of row for highlighting */
			echo '<tr class="EvenTableRows">';
			$k=0;
		} else {
			echo '<tr class="OddTableRows">';
			$k++;
		}
		$REQSQL = "SELECT requisitionno FROM materialrequisitions WHERE Reqno='".$myrow["requisitionno"]."' ";
		$REQRES = DB_query($REQSQL, $db);
		$MYROW = DB_fetch_array($REQRES);
		$requisition = $MYROW['requisitionno'];
		
		$quot_query=DB_query("select id from venderquotations where vernder_quotation ='".$myrow["vendor_quotation_no"]."'",$db);
		$qout_row=DB_fetch_array($quot_query);
		
$delete = "index.php?route=PO_SelectOSPurchOrder&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&OrderNumber=".$myrow["orderno"]."&reqno=".$MYROW["requisitionno"]."&quotno=".$qout_row["id"]."&action=".delete;
		
$ModifyPage = "index.php?route=PO_update&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&OrderNumber=".$myrow["orderno"]."&daction=update";
		
$view = "index.php?route=PO_View&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."&OrderNumber=".$myrow["orderno"]."&daction=view";

		if ($myrow['status']==_('Printed')) {
			$ReceiveOrder = "<a href='".$rootpath . "/GoodsReceived.php?" . SID . "&PONumber=" . $myrow["orderno"]."'>".
				_('Receive').'</a>';
		} else {
			$ReceiveOrder = "Receive";
		}
		if ($myrow["allowprint"]==1){
			$PrintPurchOrder = '<a target="_blank" href="' . $rootpath . '/PO_PDFPurchOrder.php?' . SID . '&OrderNo=' . $myrow['orderno'] . '">' . _('Print Now') . '</a>';
		} else {
// not open yet
//			$PrintPurchOrder = '<font color=GREY>' . _('Printed') . '</font>';

		}
		if ($myrow['status']==_('Authorised')) {
			$PrintPurchOrder = '<a target="_blank" href="' . $rootpath . '/PO_PDFPurchOrder.php?' . SID . '&OrderNo=' . $myrow['orderno'] . '&realorderno=' . $myrow['realorderno'] . '&ViewingOnly=2">' . _('Print') . '</a>';
		} else {
			$PrintPurchOrder = 'Print';
		}
		$PrintPurchOrder2 = '<a target="_blank" href="' . $rootpath . '/PO_PDFPurchOrder.php?' . SID . '&OrderNo=' . $myrow['orderno'] . '&realorderno=' . $myrow['realorderno'] . '&ViewingOnly=1">' . _('Show') . '</a>';
		$s2 = '<a target="_blank" href="' . $rootpath . '/PO_PDFPurchOrder.php?' . SID . '&OrderNo=' . $myrow['orderno'] . '&realorderno=' . $myrow['realorderno'] . '&ViewingOnly=1">' . $myrow['realorderno']. '</a>';

		$FormatedOrderDate = ConvertSQLDate($myrow['orddate']);
		$FormatedOrderValue = number_format($myrow['ordervalue'],2);
		?>
		<tr bgcolor="#c1c1c1">
			<td><?php echo $myrow['PO_Orderno']; ?></td>
			<td><?php echo ConvertSQLDate($myrow['orddate']); ?></td>
			<td><?php echo $myrow['site']; ?></td>
			
			<td><?php 
			$query = "select vendor_name from suppliers where vendor_code='".$myrow['supplierno']."' ";
			$res = DB_query($query,$db);
			$row = DB_fetch_array($res);
			echo $row['vendor_name']; ?></td>
			
			<td><?php if($myrow['Po_recive_status']=='Receive'){echo "Received";}else{echo $myrow['Po_recive_status']; }?></td>
			<td><?php if($_SESSION['frmView']=='1'){?><a href='<?php echo $view; ?>'>View</a><?php } else {echo "No Permission";}?></td>
			<td><?php if($_SESSION['frmEdit']=='1'){?><a href='<?php echo $ModifyPage; ?>'>Edit</a><?php } else {echo "No Permission";}?></td>
			<td><?php if($_SESSION['frmDelete']=='1'){?><a href='<?php echo $delete;?>' onclick="return confirm('Are You Sure You Want To Delete This Record?')">Delete</a><?php }else {echo "No Permission";}?></td>
			<td><a href="purchase_order_details.php?id=<?php echo $myrow['orderno']?>" target=_blank>Print</a></td>
			</tr>
<?php
		$j++;
		If ($j == 12){
			$j=1;
			 echo $TableHeader;
		}
	//end of page full new headings if
		
	}
	}
	else
	{?>
		<tr>
		<td colspan="11"><font color="#FF0000"><h2 align="center">No Record Found!</h2></font></td>
		</tr>
	<?php }
	?>
					 <tr>
					 <td colspan="13" style="text-align:center;">       
	<?php 
					$rows = DB_num_rows($resultpaging);
					$maxPage = ceil($rows/$rowsPerPage);
					$self1="index.php?route=PO_SelectOSPurchOrder&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."";
					$nav  = '';
				     for($page = 1; $page <= $maxPage; $page++)
				     {
					if ($page == $pageNum)
					{
						$nav .= " $page "; 
					}
					else
					{
					  $nav .= " <a href=\"$self1?page=$page\" class=\"gllink\">$page</a> ";
					} 
				}
				if ($pageNum > 1)
				{
					$page  = $pageNum - 1;
					$self1="index.php?route=PO_SelectOSPurchOrder&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."";
					echo $prev  = " <a href=\"$self1&page=$page\" class=\"gllink\">[Prev]</a> ";
					echo $first = " <a href=\"$self1&page=1\" class=\"gllink\">[First Page]</a> ";
				} 
				else
				{
				  echo $prev  = "&nbsp;"; 
				  echo $first = "&nbsp;";
				}
				if ($pageNum < $maxPage)
				{
					$page = $pageNum + 1;
					$self1="index.php?route=PO_SelectOSPurchOrder&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."";
					echo $next = " <a href=\"$self1&page=$page\" class=\"gllink\">[Next]</a>";
					echo $last = " <a href=\"$self1&page=$maxPage\" class=\"gllink\">[Last Page]</a>";
				} 
				else
				{
				   echo $next = "&nbsp;";
				   echo $last = "&nbsp;"; 
				}
			?>				</td>
				</tr>
</table>
	</table>

</div></div>

<?php }
echo "<script>defaultControl(document.forms[0].StockCode);</script>";
echo '</form>';
//include('includes/footer.inc');
?>
<link rel="stylesheet" href="css/jquery.ui.all.css">
	<script src="js/jquery-1.5.1.js"></script>
	<script src="ui/jquery.ui.core.js"></script>
	<script src="ui/jquery.ui.widget.js"></script>
	<script src="ui/jquery.ui.datepicker.js"></script>

<script>
	$(function() {
		$( "#datepicker" ).datepicker({
			showOtherMonths: true,
			selectOtherMonths: true
		});
	});
	</script>
	
	<script>
	$(function() {
		$( "#datepicker1" ).datepicker({
			showOtherMonths: true,
			selectOtherMonths: true
		});
	});
	</script>