<?php


// InventoryQuantities.php - Report of parts with quantity. Sorts by part and shows
// all locations where there are quantities of the part

include('includes/session.php');

if ($_SESSION['StockLocatorIntegrated'] == '0')
{
    $Title = _('Warehouse Stock Integration Not Enabled');
	include('includes/header.php');
	echo '<p class="page_title_text"><img alt="" src="', $RootPath, '/css/', $Theme,
		'/images/error.png" title="', // Icon image.
		$Title, '" /> ', // Icon title.
		$Title, '</p>';// Page title.
	echo '<br />';
	prnMsg(_('The system is not configured for warehouse stock integration. A configuration parameter is required for warehouse stock integration. Please consult your system administrator.'), 'info');
	include('includes/footer.php');
	exit;
}

$Title=_('Stock Placement Reporting');
include('includes/header.php');
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/inventory.png" title="' . _('Inventory') . '" alt="" />' . ' ' . _('Stock Placement Report') . '</p>';
echo '<div class="page_help_text">' . _('Use this report to search or display the stock placement and quantity of Inventory items in different locations.') . '</div><br />';

echo '<br />
	<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">
	<div>
	<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
	<table class="selection">
	<tr>
                <td>' . _('Location').':</td>
                <td><select name="StockLocation">';

    $sql = "SELECT locations.loccode, locationname FROM locations INNER JOIN locationusers ON locationusers.loccode=locations.loccode AND locationusers.userid='" .  $_SESSION['UserID'] . "' AND locationusers.canview=1";
    $resultStkLocs = DB_query($sql);
    while($myrow=DB_fetch_array($resultStkLocs)) {
        if($myrow['loccode']==$_SESSION['UserStockLocation']) {
            echo '<option selected="selected" value="' . $myrow['loccode'] . '">' . $myrow['locationname'] . '</option>';
        } else {
            echo '<option value="' . $myrow['loccode'] . '">' . $myrow['locationname'] . '</option>';
        }
    }
	echo '</select>
			</td>
		</tr>	
		<tr>
			<td>' .  _('Stock Code'). ':</td>
			<td><input type="text" name="StockID" size="21" value="" maxlength="20" /></td>
		</tr>
		<tr>
			<td></td>
			<td>
				<input type="submit" name="Search" value="Search" />
			</td>
		</tr>
	</table>';

if(isset($_POST['Search']))
{
	$sql = "SELECT stockid, loccode, quantity FROM locstock WHERE loccode = '".$_POST['StockLocation']."' AND stockid LIKE '%".$_POST['StockID']."%' AND quantity > 0";
	$result_total = DB_query($sql);
	if(DB_num_rows($result_total) > 0)
	{
		echo '<table class="selection">
			<thead>
				<tr>
					<th class="ascending">' . _('Stock Code') . '</th>
					<th class="ascending">' . _('Rack') . '</th>
					<th class="ascending">' . _('Row') . '</th>
					<th class="ascending">' . _('Bin') . '</th>
					<th class="ascending">' . _('Qty') . '</th>
					<th class="ascending">' . _('Action') . '</th>
				</tr>
			</thead>
			<tbody>';
			
		while($row=DB_fetch_array($result_total))
		{
			$total_quantity_at_location = $row['quantity'];
			$current_stockid = $row['stockid'];

			$sql = "SELECT rack, row, bin, stockid, quantity FROM stock_locators WHERE stockid = '".$row['stockid']."' AND loccode = '".$row['loccode']."' AND active = 1 ORDER BY stockid, rack, row, bin";
			$ErrMsg=_('The stock information cannot be retrieved because');
			$DbgMsg=_('The SQL to get the stock description was');
			$result = DB_query($sql,$ErrMsg,$DbgMsg);
			
			while($myrow = DB_fetch_array($result))
			{
				echo '<tr>
						<td>' . $myrow['stockid'] . '</td>
						<td>' . $myrow['rack'] . '</td>
						<td>' . $myrow['row'] . '</td>
						<td>' . $myrow['bin'] . '</td>
						<td>' . $myrow['quantity'] . '</td>
						<td></td>
					</tr>';

				$total_quantity_at_location -= $myrow['quantity'];
			}

			if($total_quantity_at_location > 0)
			{
				echo '<tr>
						<td>' . $current_stockid . '</td>
						<td> - </td>
						<td> - </td>
						<td> - </td>
						<td>' . $total_quantity_at_location . '</td>
							<td><a target="_blank"	 href="' . $RootPath . '/emp_StockLocatorTransfer.php?StockID='.$current_stockid.'&amp;Location='.$row['loccode'].'">Stock Placement</a></td>
					</tr>';
			}
		}
		echo '</tbody></table>';
	}
	else
	{
		prnMsg(_('No Result(s) found!'),'info');
	}
}

echo '</div>
		</form>';
include('includes/footer.php');
?>