<?php


include('includes/session.php');
$Title = _('Stock Purchase Details');
/* EMP manual links before header.php */
$ViewTopic= 'Inventory';
$BookMark = 'InventoryMovement';
include('includes/header.php');

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

$StockInfo = '';
if ('' != $StockID) {
	$result = DB_query("SELECT description, units FROM stockmaster WHERE stockid='" . $StockID . "'");
	$myrow = DB_fetch_row($result);

	$StockInfo = '<br /><b>' . $StockID . ' - ' . $myrow['0'] . ' : ' . _('in units of') . ' : ' . $myrow[1] . '</b>';
}

echo '<p class="page_title_text">
		<img src="', $RootPath, '/css/', $_SESSION['Theme'], '/images/inventory.png" title="', _('Inventory'), '" alt="" /> ', $Title,
		$StockInfo,
	'</p>';

echo '<form action="', htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8'), '" method="post">
	<div>
	<input type="hidden" name="FormID" value="', $_SESSION['FormID'], '" />';

if (!isset($_POST['BeforeDate']) OR !Is_date($_POST['BeforeDate'])){
   $_POST['BeforeDate'] = Date($_SESSION['DefaultDateFormat']);
}
if (!isset($_POST['AfterDate']) OR !Is_date($_POST['AfterDate'])){
   $_POST['AfterDate'] = Date($_SESSION['DefaultDateFormat'], Mktime(0,0,0,Date('m')-3,Date('d'),Date('y')));
}

echo '<br />
	<table class="selection">
	<tr>
		<th colspan="12">', _('Stock Code'), ':<input type="text" name="StockID" size="21" value="', $StockID, '" required="required" maxlength="20" />';


echo '<tr>
		<th colspan="12">', _('Show Movements between'), ':
			<input type="text" name="AfterDate" class="date" size="11" required="required" maxlength="10" value="', $_POST['AfterDate'], '" /> ' . _('and') . ':
			<input type="text" name="BeforeDate" class="date" size="11" required="required" maxlength="10" value="', $_POST['BeforeDate'], '" />
			<input type="submit" name="ShowMoves" value="', _('Show Stock Purchase Details'), '" />
		</th>
	</tr>';

$SQLBeforeDate = FormatDateForSQL($_POST['BeforeDate']);
$SQLAfterDate = FormatDateForSQL($_POST['AfterDate']);

$SQL = "SELECT grns.deliverydate, suppliers.suppname, grns.qtyrecd, grns.quantityinv, purchorderdetails.actprice AS invoice_price, purchorderdetails.orderno AS po_no, supptrans.suppreference AS supplier_invoice, supptrans.trandate
		FROM grns
		LEFT JOIN purchorderdetails
		ON grns.podetailitem = purchorderdetails.podetailitem
		LEFT JOIN suppinvstogrn
		ON grns.grnno = suppinvstogrn.grnno 
		LEFT JOIN supptrans
		ON suppinvstogrn.suppinv = supptrans.transno AND TYPE = 20
		LEFT JOIN suppliers
		ON grns.supplierid = suppliers.supplierid
		WHERE  
			grns.deliverydate >= '" . $SQLAfterDate . "'
			AND grns.itemcode = '" . $StockID . "'
			AND grns.deliverydate <= '" . $SQLBeforeDate . "'
		ORDER BY grns.grnno";

$ErrMsg = _('The stock purchase details for the selected criteria could not be retrieved because') . ' - ';
$DbgMsg = _('The SQL that failed was') . ' ';

$MovtsResult = DB_query($SQL, $ErrMsg, $DbgMsg);

if (DB_num_rows($MovtsResult) > 0) {
	$myrow = DB_fetch_array($MovtsResult);

	echo '<tr>
		<th>' . _('Receive Date') . '</th>
		<th>' . _('Purchase Order') . '</th>
		<th>' . _('Invoice Date') . '</th>
		<th>' . _('Supplier') . '</th>
		<th>' . _('Supplier Reference') . '</th>
		<th>' . _('Price') . '</th>
		<th>' . _('Quantity') . '</th>
		<th>' . _('Total') . '</th>
	</tr>';

	DB_data_seek($MovtsResult, 0);

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

		// $DisplayTranDate = ConvertSQLDate($myrow['trandate']);

		// $SerialSQL = "SELECT serialno, moveqty FROM stockserialmoves WHERE stockmoveno='" . $myrow['stkmoveno'] . "'";
		// $SerialResult = DB_query($SerialSQL);

		// $SerialText = '';
		// while ($SerialRow = DB_fetch_array($SerialResult)) {
		// 	if ($myrow['serialised'] == 1) {
		// 		$SerialText .= $SerialRow['serialno'] . '<br />';
		// 	} else {
		// 		$SerialText .= $SerialRow['serialno'] . ' Qty- ' . $SerialRow['moveqty'] . '<br />';
		// 	}
		// }

		// if ($myrow['type']==10){
		// 	/*its a sales invoice allow link to show invoice it was sold on*/

		// 	echo '<tr class="striped_row">
		// 			<td><a target="_blank" href="', $RootPath, '/PrintCustTrans.php?FromTransNo=', urlencode($myrow['transno']), '&amp;InvOrCredit=Invoice">', $myrow['typename'], '</a></td>
		// 			<td>', $myrow['transno'], '</td>
		// 			<td>', $DisplayTranDate, '</td>
		// 			<td>', $myrow['userid'], '</td>
		// 			<td>', $myrow['debtorno'], '</td>
		// 			<td>', $myrow['branchcode'], '</td>
		// 			<td class="number">', locale_number_format($myrow['qty'], $myrow['decimalplaces']), '</td>
		// 			<td>', $myrow['reference'], '</td>
		// 			<td class="number">', locale_number_format($myrow['price'], $_SESSION['CompanyRecord']['decimalplaces']), '</td>
		// 			<td class="number">', locale_number_format($myrow['discountpercent'] * 100, 2), '%%</td>
		// 			<td class="number">', locale_number_format($myrow['newqoh'], $myrow['decimalplaces']), '</td>';
		// 	if ($myrow['controlled'] == 1) {
		// 		echo '<td>', $SerialText, '</td>';
		// 	}
		// 	echo '</tr>';

		// } elseif ($myrow['type']==11){

		// 	echo '<tr class="striped_row">
		// 			<td><a target="_blank" href="', $RootPath, '/PrintCustTrans.php?FromTransNo=', urlencode($myrow['transno']), '&amp;InvOrCredit=Credit">', $myrow['typename'], '</a></td>
		// 			<td>', $myrow['transno'], '</td>
		// 			<td>', $DisplayTranDate, '</td>
		// 			<td>', $myrow['userid'], '</td>
		// 			<td>', $myrow['debtorno'], '</td>
		// 			<td>', $myrow['branchcode'], '</td>
		// 			<td class="number">', locale_number_format($myrow['qty'], $myrow['decimalplaces']), '</td>
		// 			<td>', $myrow['reference'], '</td>
		// 			<td class="number">', locale_number_format($myrow['price'], $_SESSION['CompanyRecord']['decimalplaces']), '</td>
		// 			<td class="number">', locale_number_format($myrow['discountpercent'] * 100, 2), '%%</td>
		// 			<td class="number">', locale_number_format($myrow['newqoh'], $myrow['decimalplaces']), '</td>';
		// 	if ($myrow['controlled'] == 1) {
		// 		echo '<td>', $SerialText, '</td>';
		// 	}
		// 	echo '</tr>';

		// } else {

			echo '<tr class="striped_row">
					<td>', $myrow['deliverydate'], '</td>
					<td>', $myrow['po_no'], '</td>
					<td>', $myrow['trandate'], '</td>
					<td>', $myrow['suppname'], '</td>
					<td>', $myrow['supplier_invoice'], '</td>
					<td class="number">', locale_number_format($myrow['invoice_price'], $_SESSION['CompanyRecord']['decimalplaces']), '</td>
					<td>', $myrow['quantityinv'], '</td>
					<td class="number">', locale_number_format($myrow['invoice_price'] * $myrow['quantityinv'], $_SESSION['CompanyRecord']['decimalplaces']), '</td>
			</tr>';

		// }
	//end of page full new headings if
	}
	//end of while loop
}

echo '</table>
		<div class="centre">
			<br /><a href="', $RootPath, '/StockStatus.php?StockID=', urlencode($StockID), '">', _('Show Stock Status'), '</a>
			<br /><a href="', $RootPath, '/StockUsage.php?StockID=', urlencode($StockID), '&amp;StockLocation=', urlencode($_POST['StockLocation']), '">', _('Show Stock Usage'), '</a>
			<br /><a href="', $RootPath, '/SelectSalesOrder.php?SelectedStockItem=', urlencode($StockID), '&amp;StockLocation=', urlencode($_POST['StockLocation']), '">', _('Search Outstanding Sales Orders'), '</a>
			<br /><a href="', $RootPath, '/SelectCompletedOrder.php?SelectedStockItem=', urlencode($StockID), '">', _('Search Completed Sales Orders'), '</a>
		</div>
	</div>
	</form>';

include('includes/footer.php');

?>
