<?php 
	if (!$_GET['year']) $_GET['year']=date('Y');
?>
<style type="text/css">
.MYoddListRowS1 {
	font-size: 12px;
	padding-left: 5px;
	padding-right: 12px;
	padding-bottom: 5px;
	padding-top: 3px;
	vertical-align: middle;
}

.MYlistViewThS1 {
	font-size: 11px;
	font-weight: bold;
	text-align: left;
	padding-left: 5px;
	padding-right: 11px;
	padding-bottom: 5px;
	padding-top: 3px;
	border-left: none;
	border-right: none;
	border-top: 1px solid #cccccc;
	border-bottom: 1px solid #cccccc;
	background: #ebebed;
}
</style>
<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\';';

if (!$_GET['year']) $_GET['year'] = date('%Y');
	
	// $_GET copy
$get = '';
echo 'var get=\'\';';
foreach ( $_GET as $k => $v ) {
	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;
	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 header_width=document.getElementById('header').offsetWidth;
	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);
	document.getElementById('tableDiv').style.width=new_width+'px';
    document.getElementById('tableDiv').style.width=new_width+'px';
}
function clearSearch() {
	link='index.php?module=EcmPurchaseOrders&action=reportPurchaseOrders';
	location.href = link;
}
</script>
<?php global $mod_strings;?>
<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><?php echo "Innowacyjny raport sprzedaży" ?></h2></td>
	</tr>
</table>
<br />
<div id="search_box">
	<ul class="tablist">
		<li><a class="current" href="#"><?php "Wyszukiwanie" ?></a>
		</li>
	</ul>
	<form action="index.php" method="get" name="search_reports">
		<input type="hidden" name="module" value="EcmReports" /> <input
			type="hidden" name="action" value="SalesReportCategories" />
		<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">
	<?php echo "Rok" ?>	</td>
					<td class="dataField" width="18%" nowrap="nowrap">
					<select
						name="year" id="year">
			<?
			$ey = ( int ) date ( "Y" );
			$sy = 2000;
			for($i = $sy; $i <= $ey; $i ++) {
				echo '<option value="' . $i . '"';
				if ($i == $_GET ['year'])
					echo ' selected';
				echo '>' . $i . '</option>';
			}
			?>
		    </select>
		    </td>
					<td class="dataLabel" width="5%" nowrap="nowrap">
	<?php echo $mod_strings['LBL_REPORT_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">
	<?php echo $mod_strings['LBL_REPORT_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">
	<?php echo $mod_strings['LBL_REPORT_ORDER_STATUS']; ?></td>
					<td class="dataField" width="26%" nowrap="nowrap"><select
						name="purchase_order_status[]" multiple="true" size="6"
						style="width: 150px;">
							<option value=""></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">
	<?php echo $mod_strings['LBL_REPORT_USER']; ?>	</td>
					<td class="dataField" width="18%" nowrap="nowrap"><select
						name="user"><option value=""></option>
			<?
			$w = $GLOBALS ['db']->query ( "select id, first_name, last_name from users where deleted=0 and status='Active' order by last_name asc" );
			while ( $r = $GLOBALS ['db']->fetchByAssoc ( $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">
	<?php echo $mod_strings['LBL_REPORT_PRODUCT_CATEGORY']; ?>		</td>
					<td class="dataField" width="18%" nowrap="nowrap"><select
						name="category"><option value=""></option>
		<?
		$w = $GLOBALS ['db']->query ( "select id,name from ecmproductcategories where deleted='0' order by name asc" );
		while ( $r = $GLOBALS ['db']->fetchByAssoc ( $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">
	<?php echo $mod_strings['LBL_REPORT_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>
					<td class="dataLabel" width="20%" nowrap="nowrap"><?php echo $mod_strings['LBL_REPORT_ACCOUNT_NAME']; ?></td>
					<td class="dataField" width="30%" nowrap="nowrap"><input
						name="account_name" readonly="readonly" tabindex=""
						id="account_name" size=""
						value="<?php echo $_REQUEST['account_name'];?>" title=""
						type="text"> <input name="account_id" id="account_id"
						value="<?php echo $_REQUEST['account_id'];?>" type="hidden"> <input
						name="btn_account_name" tabindex="" title="Select [Alt+T]"
						accesskey="T" class="button" value="Select"
						onclick='open_popup("Accounts", 600, 400, "", true, false, {"call_back_function":"set_return","form_name":"search_reports","field_to_name_array":{"id":"account_id","name":"account_name"}}, "single", true);'
						type="button"></td>
				</tr>
			</tbody>
		</table>

</div>
<input class="button" name="submit"
	value="<?php echo $mod_strings['LBL_REPORT_BTN_SEARCH']; ?>"
	type="submit">
<input class="button" name="hide"
	value="<?php echo $mod_strings['LBL_REPORT_BTN_CLEAR']; ?>"
	type="button" onClick="clearSearch();">
<input class="button" name="hide"
	value="<?php echo $mod_strings['LBL_REPORT_BTN_HIDESHOW']; ?>"
	type="button" onClick="showhide('search_box');">
<?php
// create GET for XLS
$get = '';
foreach ( $_GET as $k => $v ) {
	if (($k != 'module') && ($k != 'action') && ($k != 'purchase_order_status') && ($k != 'submit') && ($k != 'usertheme'))
		$get .= '&' . $k . '=' . $v;
}
if (count ( $_GET ['purchase_order_status'] ) > 0) {
	$wh_status = array ();
	foreach ( $_GET ['purchase_order_status'] as $status ) {
		$get .= '&purchase_order_status[]=' . $status;
	}
}
?>

<input class="button" name="hide"
	value="<?php echo $mod_strings['LBL_CREATE_XLS']; ?>" type="button"
	onClick="window.location='index.php?module=EcmPurchaseOrders&action=createReportXLS<? echo $get; ?>'">
<?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.document_no 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_orders [] = "o.tracking_no LIKE '%" . $_GET ['tracking_no'] . "%'";
	if ($_GET ['account_id'])
		$wh_orders [] = "o.parent_id = '" . $_GET ['account_id'] . "'";
}

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";

?>
<div id="tableDiv" style="overflow-x: scroll; width: 100px">
	<table width="100%">
		<tr>
			<td>
				<div style="overflow-y: scroll;">
					<table width="1170" id="myListViewTableHead" class="h3Row"
						border="0" cellpadding="0" cellspacing="0">
						<tr>
							<td style="width: 80px; min-width: 80px; max-width: 80px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('p.code')"><?php echo $mod_strings['LBL_REPORT_LIST_CODE']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 210px; min-width: 210px; max-width: 210px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('p.name')"><?php echo $mod_strings['LBL_REPORT_LIST_NAME']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 180px; min-width: 180px; max-width: 180px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('p.product_category_name')"><?php echo $mod_strings['LBL_REPORT_LIST_CATEGORY']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 130px; min-width: 130px; max-width: 130px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('o.parent_name')"><?php echo $mod_strings['LBL_REPORT_ACCOUNT_NAME']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 80px; min-width: 80px; max-width: 80px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('o.date_entered')"><?php echo $mod_strings['LBL_REPORT_LIST_ORDER_DATE']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 80px; min-width: 80px; max-width: 80px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('o.document_no')"><?php echo $mod_strings['LBL_REPORT_ORDER_NO']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 80px; min-width: 80px; max-width: 80px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('o.status')"><?php echo $mod_strings['LBL_REPORT_ORDER_STATUS']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 130px; min-width: 130px; max-width: 130px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('p.user')"><?php echo $mod_strings['LBL_REPORT_USER']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 60px; min-width: 60px; max-width: 60px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('po.product_quantity')"><?php echo $mod_strings['LBL_REPORT_LIST_QUANTITY']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 90px; min-width: 90px; max-width: 90px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('o.delivery_date')"><?php echo $mod_strings['LBL_REPORT_LIST_DELIVERY_DATE']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
							<td style="width: 130px; min-width: 130px; max-width: 130px;"
								class="MYlistViewThS1"><a class="listViewThLinkS1"
								href="javascript:createOrderLink('o.tracking_no')"><?php echo $mod_strings['LBL_REPORT_TRACKING_NO']; ?><img
									src="themes/Sugar/images/arrow.gif" border="0" /></td>
						</tr>
					</table>
				</div>
				<div id="myListView" style="overflow-y: scroll;">
					<table id="myListViewTableData" border="0" cellpadding="0"
						cellspacing="0">
<?php
// $max_width = "style=\"max-width: 200px;\"";
$result = $GLOBALS ['db']->query ( $query );
while ( $r = $GLOBALS ['db']->fetchByAssoc ( $result ) ) {
	foreach ( $r as $k => $v )
		if (! $v)
			$v = "&nbsp";
		// style=\"width: 100px; min-width: 100px; max-width:100px; \"
	echo "<tr><td style=\"width: 80px; min-width: 80px; max-width:80px; \" class=\"MYoddListRowS1\"><a href=\"index.php?module=EcmProducts&action=DetailView&record=" . $r ['ecmproduct_id'] . "\">" . $r ['code'] . "</a></td>";
	echo "<td style=\"width: 210px; min-width: 210px; max-width:210px; \" class=\"MYoddListRowS1\"><a href=\"index.php?module=EcmProducts&action=DetailView&record=" . $r ['ecmproduct_id'] . "\">" . $r ['name'] . "</a></td>";
	echo "<td style=\"width: 180px; min-width: 180px; max-width:180px; \" class=\"MYoddListRowS1\">" . $r ['product_category_name'] . "</td>";
	echo "<td style=\"width: 130px; min-width: 130px; max-width:130px; \"class=\"MYoddListRowS1\"><a href=\"index.php?module=Accounts&action=DetailView&record=" . $r ['parent_id'] . "\">" . $r ['parent_name'] . "</a></td>";
	echo "<td style=\"width: 80px; min-width: 80px; max-width: 80px; \" class=\"MYoddListRowS1\">" . substr ( $r ['date_entered'], 0, 10 ) . "</td>";
	echo "<td style=\"width: 80px; min-width: 80px; max-width:80px; \" class=\"MYoddListRowS1\"><a href=\"index.php?module=EcmPurchaseOrders&action=DetailView&record=" . $r ['ecmpurchaseorder_id'] . "\">" . $r ['document_no'] . "</a></td>";
	echo "<td style=\"width: 80px; min-width: 80px; max-width:80px; \" class=\"MYoddListRowS1\">" . $GLOBALS ['app_list_strings'] ['ecmpurchaseorders_status_dom'][$r['status']]."</td>";
		echo "<td style=\"width: 130px; min-width: 130px; max-width:130px; \" class=\"MYoddListRowS1\">".$r['first_name'].' '.$r['last_name']."</td>";
		echo "<td style=\"width: 60px; min-width: 60px; max-width:60px; \" class=\"MYoddListRowS1\">".$r['product_quantity']."</td>";
		echo "<td style=\"width: 90px; min-width: 90px; max-width:90px; \" class=\"MYoddListRowS1\">".$r['delivery_date']."</td>";
		echo "<td style=\"width: 130px; min-width: 130px; max-width:130px; \" class=\"MYoddListRowS1\">".$r['tracking_no']."</td></tr>";
	}
?>
</table>
				</div>
			</td>
		</tr>
	</table>
</div>
<script type="text/javascript">setTableSize();</script>
