<?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;
}

if($_GET['MoveType'] != "" && $_GET['MoveNo'] != '1')
{
    $_POST['MoveType'] = $_GET['MoveType'];
    $_POST['MoveNo'] = $_GET['MoveNo'];
    
    unset($_GET['MoveType']);
    unset($_GET['MoveNo']);
}


if(isset($_POST['MoveType']) && isset($_POST['MoveNo']))
{
	$sql = "SELECT transno, stockid, loccode, rack, row, bin, trandate, userid, qty, typename
			FROM stock_locator_moves
			LEFT JOIN systypes
			ON stock_locator_moves.type = systypes.typeid
			WHERE type = '".$_POST['MoveType']."' AND transno = '".$_POST['MoveNo']."'";
	$html_result = "";
	$html_result_header = "";
	$result = DB_query($sql);
	if(DB_num_rows($result) > 0)
	{
		$html_result .= '<table class="selection">
							<tr>
								<th width="165">' . _('Stock Code') . '</th>
								<th>' . _('Location') . '</th>
								<th width="50">' . _('Rack') . '</th>
								<th width="50">' . _('Row') . '</th>
								<th width="50">' . _('Bin') . '</th>
								<th>' . _('Quantity') . '</th>
							</tr>';
							
		$pdf_result .= '<table border="1" cellpadding="2">
							<tr>
								<th width="165">' . _('Stock Code') . '</th>
								<th>' . _('Location') . '</th>
								<th width="50">' . _('Rack') . '</th>
								<th width="50">' . _('Row') . '</th>
								<th width="50">' . _('Bin') . '</th>
								<th>' . _('Quantity') . '</th>
							</tr>';

		while($myrow=DB_fetch_array($result))
		{
			if($html_result_header == "")
			{
				$html_result_header .= '<h2>' . _('Warehouse Stock Movement Report') . '</h2>
										<table class="selection">
											<tr>
												<th>Movement Type : </th>
												<td>'.$myrow['typename'].'</td>
											</tr>
											<tr>
												<th>Transaction Number : </th>
												<td>'.$myrow['transno'].'</td>
											</tr>
											<tr>
												<th>Transaction Date : </th>
												<td>'.$myrow['trandate'].'</td>
											</tr>
										</table>';
			}

			if($pdf_result_header == "")
			{
				$pdf_result_header .= '<h2>' . _('Warehouse Stock Movement Report') . '</h2><table>
											<tr>
												<th>Movement Type: </th><td>'.$myrow['typename'].'</td>
											</tr>
											<tr>
												<th>Transaction Number: </th><td>'.$myrow['transno'].'</td>
											</tr>
											<tr>
												<th>Transaction Date: </th><td>'.$myrow['trandate'].'</td>
											</tr>
										</table>';
			}

			$html_result .= '<tr class="striped_row">
								<td>' . $myrow['stockid'] . '</td>
								<td>' . $myrow['loccode'] . '</td>
								<td>' . $myrow['rack'] . '</td>
								<td>' . $myrow['row'] . '</td>
								<td>' . $myrow['bin'] . '</td>
								<td>' . $myrow['qty'] . '</td>
							</tr>';

			$pdf_result .= '<tr>
							<td>' . $myrow['stockid'] . '</td>
							<td>' . $myrow['loccode'] . '</td>
							<td>' . $myrow['rack'] . '</td>
							<td>' . $myrow['row'] . '</td>
							<td>' . $myrow['bin'] . '</td>
							<td>' . $myrow['qty'] . '</td>
						</tr>';
		}

		$html_result .= '</table>';
		$html_result = $html_result_header . '<br>' . $html_result;

		if(isset($_POST['PrintPDF']))
		{
			$pdf_result .= '</table>';
			$pdf_result = $pdf_result_header . '<br><br>' . $pdf_result;
		
			require_once('includes/tcpdf/config/tcpdf_config.php');
			require_once('includes/tcpdf/tcpdf.php');
			
			// create new PDF document
			$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
			
			// set margins
			$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
			$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
			$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
			
			// set auto page breaks
			$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
			
			// set some language-dependent strings (optional)
			if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
				require_once(dirname(__FILE__).'/lang/eng.php');
				$pdf->setLanguageArray($l);
			}

			// set font
			// $pdf->SetFont('dejavusans', '', 10);
			
			// add a page
			$pdf->AddPage();

			$pdf->writeHTML($pdf_result, true, false, true, false, '');
			// output the HTML content
			$pdf->Output($_SESSION['DatabaseName'] . '_StockLocatorMovement_' . $_POST['MoveType'] . '_' . $_POST['MoveNo'] . '.pdf', 'D');
			exit;
		}
	}
}

$Title=_('Warehouse Stock Movement Report');
include('includes/header.php');
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/inventory.png" title="' . _('Inventory') . '" alt="" />' . ' ' . _('Warehouse Stock Movement Report') . '</p>';

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>' . _('Select Movement Type').':</td>
				<td><select name="MoveType">';

	/*
	10 = Sales Invoice
	16 = Location Transfer
	25 = Purchase Order Delivery (Reverse GRN)
	28 = Work Order Issue
	98 = Work Order Reverse
	99 = Stock Placement
	*/
	$sql = "SELECT typeid, typename, typeno FROM systypes WHERE typeid = '10' OR typeid = '16'  OR typeid = '25' OR typeid = '28' OR typeid = '99' OR typeid = '98'";
	$result = DB_query($sql);
	while($myrow=DB_fetch_array($result)) {
		if($myrow['typeid']==$_POST['MoveType']) {
			echo '<option selected="selected" value="' . $myrow['typeid'] . '">' . $myrow['typename'] . '</option>';
		} else {
			echo '<option value="' . $myrow['typeid'] . '">' . $myrow['typename'] . '</option>';
		}
		
		$LastTypeNo[$myrow['typename']] = $myrow['typeno'];
	}
	echo '</select>
			</td>
		</tr>	
		<tr>
			<td>' .  _('Transaction movement number to print'). ':</td>
			<td><input type="text" name="MoveNo" size="21" maxlength="20" value="'.$_POST['MoveNo'].'" /></td>
		</tr>
		<tr>
			<td></td>
			<td>
				<input type="submit" name="Search" value="Search" />
				<input type="submit" name="PrintPDF" value="Print PDF" />
			</td>
		</tr>
	</table>';

	
	echo '<div class="page_help_text">';
	foreach ($LastTypeNo as $typename => $typeno)
	{
		echo _('The last '. $typename .' created was number') . ' <b>' . $typeno . '</b><br>';
	}
	echo '</div></br>';

	if((isset($_POST['Search']) || isset($_POST['PrintPDF'])) && $html_result == "")
	{
		prnMsg(_('The stock movement record was not found!'),'warn');
	}
	else if(isset($_POST['Search']) && $html_result != "")
	{
		echo $html_result;
	}

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

?>