<?php

$PageSecurity = 2;

include('includes/session.inc');
$title = _('Costed Bill Of Material');
include('includes/header.inc');
$StockID=$_REQUEST['StockID'];
//Get Out if we have no order number to work with
if (isset($StockID) and $StockID!=""){
$result = DB_query("SELECT description, units FROM stockmaster WHERE stockid='" . $StockID  . "'",$db);
	$myrow = DB_fetch_row($result);

	echo "<div class='centre'><br><font size=4><b>" . $myrow[0] . ' : ' . _('per') . ' ' . $myrow[1] . "</b></font></div>";

	  $sql = "SELECT bom.parent,
			bom.component,
			stockmaster.description,
			stockmaster.decimalplaces,
			stockmaster.materialcost+ stockmaster.labourcost+stockmaster.overheadcost as standardcost,
			bom.quantity,
			bom.quantity * (stockmaster.materialcost+ stockmaster.labourcost+ stockmaster.overheadcost) AS componentcost
		FROM bom INNER JOIN stockmaster ON bom.component = stockmaster.stockid
		WHERE bom.parent = '" . $StockID . "'
		AND bom.effectiveafter < Now()
		AND bom.effectiveto > Now()";

	$ErrMsg = _('The bill of material could not be retrieved because');
	$BOMResult = DB_query ($sql,$db,$ErrMsg);

	if (DB_num_rows($BOMResult)==0){
		prnMsg(_('The bill of material for this part is not set up') . ' - ' . _('there are no components defined for it'),'warn');
	} else {

		echo "<table cellpadding=2 BORDER=0>";
		$TableHeader = '<tr>
				<th>' . _('Component') . '</th>
				<th>' . _('Description') . '</th>
				<th>' . _('Quantity') . '</th>
				<th>' . _('Unit Cost') . '</th>
				<th>' . _('Total Cost') . '</th>
				</tr>';

		echo $TableHeader;

		$j = 1;
		$k=0; //row colour counter

		$TotalCost = 0;

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

			if ($k==1){
				echo '<tr class="EvenTableRows">';
				$k=0;
			} else {
				echo '<tr class="OddTableRows">';
				$k++;
			}

			//$ComponentLink = "<a href='$rootpath/SelectProduct.php?" . SID . "&StockID=" . $myrow['component'] . "'>" .  . "</a>";

			/* Component Code  Description                 Quantity            Std Cost*                Total Cost */
			printf("<td>%s</td>
				<td>%s</td>
				<td class=number>%s</td>
				<td class=number>%.2f</td>
				<td class=number>%.2f</td>
				
				</tr>",
				$myrow['component'],
				$myrow['description'],
				number_format($myrow['quantity'],
				$myrow['decimalplaces']),
				$myrow['standardcost'],
				$myrow['componentcost']);
				

			$TotalCost += $myrow['componentcost'];

			$j++;
		}

		echo '<tr>
			<td colspan=4 class=number><b>' . _('Total Cost') . '</b></td>
			<td class=number><b>' . number_format($TotalCost,2) . '</b></td>
		</tr>';

		echo '</table>';
		
		 $sqlla='select * from costsheet where parent_id="'.$StockID.'"';
		$laboResult = DB_query($sqlla,$db,$ErrMsg);
		$myrowlab=DB_fetch_array($laboResult);
		
		
		echo "<table cellpadding=2 BORDER=0>";
		$TableHeader = '<tr>
				<th>' . _('Item Code') . '</th>
				<th>' . _('Labour Cost') . '</th>
				<th>' . _('Overhead Cost') . '</th>
				</tr>';

		echo $TableHeader;


			printf("<td>%s</td>
				
				<td>%.2f</td>
				<td class=number>%.2f</td>
				
				</tr>",
				$myrowlab['parent_id'],
				$myrowlab['labourcost'],
				$myrowlab['overheadcost']);
	
		echo '</table>';
		
		///
	}
} else { //no stock item entered
	prnMsg(_('Enter a stock item code above') . ', ' . _('to view the costed bill of material for'),'info');
}









if (!isset($_POST['StockID']) or $_POST['StockID']=='') {
	echo "<script>defaultControl(document.forms[0].StockCode);</script>";
}

include('includes/footer.inc');
?>
