<script type="text/javascript">
function createOrderLink(column) {
<?php 
	if ($_GET['order_by'])
		echo 'var order_by =\''.$_GET['order_by'].'\';';
	else
		echo 'var order_by =\'p.name\';';
	if ($_GET['order_type'])
		echo 'var order_type =\''.$_GET['order_type'].'\';';
	else
		echo 'var order_type =\'ASC\';';
		
	//$_GET copy
	$get='';
	echo 'var get=\'\';';
	foreach ($_GET as $k => $v) {
	echo 'alert(\''.$k.'\');';
		if (($k!='order_by') || ($k!='order_type'))
			if (is_array($v)) {
				$tmp = ''; 
				foreach ($v as $k2=>$v2)
					$tmp.='&'.$k.'[]='.$v2;
				echo 'var get=get+\''.$tmp.'\';';
			} else
			echo 'var get=get+\'&'.$k.'='.$v.'\';';
	}

?>
	var link;
	if (column == order_by) {
		if (order_type == 'ASC')
			link='index.php?order_by='+column+'&order_type=DESC'+get;
		if (order_type == 'DESC') {
			link='index.php?order_by='+column+'&order_type=ASC'+get;
			}
	} else
		link='index.php?order_by='+column+'&order_type=ASC'+get;

		//alert(link);
	location.href = link;
}

function showhide(id) {
	var el = document.getElementById(id);
	
	if (el.style.display == "none")
		el.style.display="block";
	else
		el.style.display="none";
}
function setTableSize() {
	var header_height=document.getElementById('header').offsetHeight;
	var left_width=document.getElementById('left').offsetWidth;
	var screen_height = screen.height;
	var screen_width = screen.width;
	var new_height = (screen_height-header_height-300)+'px';
	document.getElementById('myListView').style.height=new_height;
	var new_width = (screen_width-left_width-50)+'px';
	document.getElementById('tableDiv').style.width=new_width;
	
	//td
	var i=1;
	for (i=1; i<=11; i++) {
		var title ='title'+i;
		var data ='data'+i;
		document.getElementById(title).style.width=document.getElementById(data).offsetWidth
		//alert(document.getElementById(title).offsetWidth+'  -  '+document.getElementById(data).offsetWidth);
		}
}
</script>
<table cellspacing="0" cellpadding="0" border="0"><tr><td width="19"><img src="themes/Sugar/images/EcmProducts.gif" style="margin-top: 3px; margin-right: 3px;" alt="EcmProducts" width="16" border="0" height="16"></td>
<td width="402"><h2>Purchase Orders Report</h2></td></tr></table><br />
<div id="search_box">
<ul class="tablist">
<li>
	<a class="current" href="#">Basic Search</a>
</li>		
</ul>
<form action="index.php" method="get">
<input type="hidden" name="module" value="EcmProductReports" />
<input type="hidden" name="action" value="index4" />
<table style="border-top: 0px none; margin-bottom: 4px;" class="tabForm" width="100%" border="0" cellpadding="0" cellspacing="0">
<tbody>
	<tr>
	<td class="dataLabel" width="5%" nowrap="nowrap">
	Product name		</td>
	<td class="dataField" width="18%" nowrap="nowrap">
		<input name="product_name" id="product_name" size="30" maxlength="255" value="<? echo $_GET['product_name'];?>" title="" tabindex="" type="text">		
	</td>
	<td class="dataLabel" width="5%" nowrap="nowrap">
	Product code		</td>
	<td class="dataField" width="18%" nowrap="nowrap">
		<input name="product_code" id="product_code" size="30" maxlength="255" value="<? echo $_GET['product_code'];?>" title="" tabindex="" type="text">		
	</td>
	<td class="dataLabel" width="5%" nowrap="nowrap">
	Order no.		</td>
	<td class="dataField" width="18%" nowrap="nowrap">
		<input name="purchase_order_number" id="purchase_order_number" size="30" maxlength="255" value="<? echo $_GET['purchase_order_number'];?>" title="" tabindex="" type="text">		
	</td>	
	<td class="dataLabel" width="5%" nowrap="nowrap">
	Order status</td>
	<td class="dataField" width="26%" nowrap="nowrap">
		<select name="purchase_order_status[]" multiple="true" size="6" style="width: 150px;"><option value="">select</option>
		<?
		foreach ($GLOBALS['app_list_strings']['ecmpurchaseorders_status_dom'] as $k=>$v) {
			echo '<option value="'.$k.'"'; 
				if (count($_GET['purchase_order_status']) > 0)
				foreach ($_GET['purchase_order_status'] as $status) {
					if($k==$status)echo ' selected';
				}
			echo '>'.$v.'</option>';
		}
		?>
	</td>
	</tr><tr>
	<td class="dataLabel" width="5%" nowrap="nowrap">
		User	</td>
	<td class="dataField" width="18%" nowrap="nowrap">
		<select name="user"><option value="">select</option>
			<?
			$w=mysql_query("select id, first_name, last_name from users where deleted=0 and status='Active' order by last_name asc");
			while($r=mysql_fetch_array($w)){
				echo '<option value="'.$r['id'].'"';
				if($r['id']==$_REQUEST['user'])echo ' selected';
				echo '>'.$r['first_name'].' '.$r['last_name'].'</option>';
			}
			?>
	</td>
	<td class="dataLabel" width="5%" nowrap="nowrap">
		Category		</td>
	<td class="dataField" width="18%" nowrap="nowrap">
		<select name="category"><option value="">select</option>
		<?
		$w=mysql_query("select id,name from ecmproductcategories where deleted='0' order by name asc");
		while($r=mysql_fetch_array($w)){
			echo '<option value="'.$r['id'].'"';
			if($r['id']==$_REQUEST['category'])echo ' selected';
			echo '>'.$r['name'].'</option>';
		}
		?>
	</td>
	<td class="dataLabel" width="5%" nowrap="nowrap">
	Tracking No.		</td>
	<td class="dataField" width="18%" nowrap="nowrap">
		<input name="tracking_no" id="tracking_no" size="30" maxlength="255" value="<? echo $_GET['product_name'];?>" title="" tabindex="" type="text">		
	</td>	
	</tr>
</tbody>
</table>
</div>
<input class="button" name="submit" value="Search" type="submit">
<input class="button" name="hide" value="Hide/Show Search" type="button" onClick="showhide('search_box');">
<?php
//create query
//$wh_products = array();
//$wh_orders = array();
if ($_GET['submit']) {
	if ($_GET['product_name']) $wh_products[] = "p.name LIKE '%".$_GET['product_name']."%'";
	if ($_GET['product_code']) $wh_products[] = "p.code LIKE '%".$_GET['product_code']."%'";
	if ($_GET['purchase_order_number']) $wh_orders[] = "o.name LIKE '%".$_GET['purchase_order_number']."%'";
	if (count($_GET['purchase_order_status']) > 0) {
		$wh_status = array();
		foreach ($_GET['purchase_order_status'] as $status) {
			$wh_status[] = "o.status ='".$status."'";		
		}
		$wh_orders[] = "(".implode(" OR ", $wh_status).")";
	}
	if ($_GET['user']) $wh_orders[] = "o.assigned_user_id = '".$_GET['user']."'";
	if ($_GET['category']) $wh_products[] = "p.product_category_id = '".$_GET['category']."'";
	if ($_GET['tracking_no']) $wh_products[] = "o.tracking_no LIKE '%".$_GET['tracking_no']."%'";
}

	if (($_GET['order_by']) && $_GET['order_type']) 
		$order_by=" ORDER BY ".$_GET['order_by'].' '.$_GET['order_type'];
	else $order_by=" ORDER BY p.name ASC";
		
	$query = "SELECT p.name, p.product_category_name, o.date_entered, o.document_no, o.status, po.product_quantity, o.delivery_date, o.tracking_no, u.first_name, u.last_name, po.ecmpurchaseorder_id, po.ecmproduct_id, p.code, o.parent_id, o.parent_name FROM ecmproducts_ecmpurchaseorders AS po ";
	$query.="INNER JOIN ecmproducts AS p ON p.id=po.ecmproduct_id ";
	if (isset($wh_products)) $query.=" AND ".implode(" AND ", $wh_products);
	$query.="INNER JOIN ecmpurchaseorders AS o ON o.id=po.ecmpurchaseorder_id AND o.deleted='0' ";
	if (isset($wh_orders)) $query.=" AND ".implode(" AND ", $wh_orders);
	$query.="INNER JOIN users As u ON o.assigned_user_id=u.id";
	$query.=$order_by;
	
	$post_copy = array();
	$post_copy 
?>
<div id="tableDiv" style="overflow-x: scroll; width: 100px">
<table width="100%"><tr><td>
<div style="overflow-y: scroll;">
<table width="3000" class="h3Row" border="0" cellpadding="0" cellspacing="0">
<tr>
	<td id="title1" class="listViewThS1" width="8%"><a class="listViewThLinkS1" href="javascript:createOrderLink('p.code')">Code<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title2" class="listViewThS1" width="15%"><a class="listViewThLinkS1" href="javascript:createOrderLink('p.name')">Name<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title3" class="listViewThS1" width="15%"><a class="listViewThLinkS1" href="javascript:createOrderLink('p.product_category_name')">Category<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title4" class="listViewThS1" width="15%"><a class="listViewThLinkS1" href="javascript:createOrderLink('o.parent_name')">Account name<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title5" class="listViewThS1" width="8%"><a class="listViewThLinkS1" href="javascript:createOrderLink('o.date_entered')">Order date<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title6" class="listViewThS1" width="8%"><a class="listViewThLinkS1" href="javascript:createOrderLink('o.document_no')">Order No.<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title7" class="listViewThS1" width="8%"><a class="listViewThLinkS1" href="javascript:createOrderLink('o.status')">Order status<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title8" class="listViewThS1" width="10%"><a class="listViewThLinkS1" href="javascript:createOrderLink('p.user')">User<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title9" class="listViewThS1" width="8%"><a class="listViewThLinkS1" href="javascript:createOrderLink('po.product_quantity')">Quantity<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title10" class="listViewThS1" width="8%"><a class="listViewThLinkS1" href="javascript:createOrderLink('o.delivery_date')">Delivery date<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
	<td id="title11" class="listViewThS1" width="10%"><a class="listViewThLinkS1" href="javascript:createOrderLink('o.tracking_no')">Tracking No<img src="themes/Sugar/images/arrow.gif" border="0"/></td>
</tr>
</table>
</div>
<div id="myListView" style="overflow-y: scroll;">
<table width="3000" border="0" cellpadding="0" cellspacing="0">
<tr>
<?php

	$result = $GLOBALS['db']->query($query);
	while ($r=$GLOBALS['db']->fetchByAssoc($result)) {
	//	foreach ($r as $k => $v) echo $k.' - ';
		echo "<td id=\"data1\" class=\"oddListRowS1\" width=\"8%\"><a href=\"index.php?module=EcmProducts&action=DetailView&record=".$r['ecmproduct_id']."\">".$r['code']."</a></td>";
		echo "<td id=\"data2\" class=\"oddListRowS1\" width=\"15%\"><a href=\"index.php?module=EcmProducts&action=DetailView&record=".$r['ecmproduct_id']."\">".$r['name']."</a></td>";
		echo "<td id=\"data3\" class=\"oddListRowS1\" width=\"15%\">".$r['product_category_name']."</td>";
		echo "<td id=\"data4\" class=\"oddListRowS1\" width=\"8%\"><a href=\"index.php?module=Accounts&action=DetailView&record=".$r['parent_id']."\">".$r['parent_name']."</a></td>";
		echo "<td id=\"data5\" class=\"oddListRowS1\" width=\"8%\">".substr($r['date_entered'],0,10)."</td>";
		echo "<td id=\"data6\" class=\"oddListRowS1\" width=\"8%\"><a href=\"index.php?module=EcmPurchaseOrders&action=DetailView&record=".$r['ecmpurchaseorder_id']."\">".$r['document_no']."</a></td>";
		echo "<td id=\"data7\" class=\"oddListRowS1\" width=\"8%\">".$GLOBALS['app_list_strings']['ecmpurchaseorders_status_dom'][$r['status']]."</td>";
		echo "<td id=\"data8\" class=\"oddListRowS1\" width=\"10%\">".$r['first_name'].' '.$r['last_name']."</td>";
		echo "<td id=\"data9\" class=\"oddListRowS1\" width=\"8%\">".$r['product_quantity']."</td>";
		echo "<td id=\"data10\" class=\"oddListRowS1\" width=\"8%\">".$r['delivery_date']."</td>";
		echo "<td id=\"data11\" class=\"oddListRowS1\" width=\"10%\">".$r['tracking_no']."</td></tr>";
	}
?>
</tr>
</div>
</td></tr></table>
</div>
<script type="text/javascript">setTableSize();</script>
