<?php require "_header.php";
error_reporting(1); ?>
<div id="main-report" align="center">
	<center>
		<h4>Curent Stock Report</h4>
	</center>
	<?php
	if ($_SESSION['cCompany_id'] == "") {
		?>
		<script language="javascript" type="text/javascript">
			location.href = 'report-current-stock.php';
		</script>
		<?php
	} elseif ($_SESSION['cCompany_id'] != "" && isset($_SESSION['cCompany_id'])) {
		$header_row_string = "Curent Stock Report";
		$product_id = Escape($_REQUEST["ID"]);
		if (isset($product_id)) {
			if ($product_id != "") {
				$qry = "SELECT 
					pm.*,
					(
						SELECT SUM(ip.product_qty) 
						FROM lms_invoiceproduct ip 
						WHERE pm.product_id = ip.product_id
					) AS puchase,
					(
						SELECT SUM(cip.product_qty) 
						FROM lms_customerinvoiceproduct cip 
						WHERE pm.product_id = cip.product_id
					) AS sale
			   FROM
					lms_productmaster pm
			  where
					pm.company_id = '" . $_SESSION["cCompany_id"] . "'
					and pm.product_id = '$product_id'";

			} else {
				$qry = "SELECT
						pm.*,
						(
							SELECT SUM(ip.product_qty) 
							FROM lms_invoiceproduct ip 
							WHERE pm.product_id = ip.product_id
						) AS puchase,
						(
							SELECT SUM(cip.product_qty) 
							FROM lms_customerinvoiceproduct cip 
							WHERE pm.product_id = cip.product_id
						) AS sale
				   FROM
						lms_productmaster pm
				  where
						pm.company_id = '" . $_SESSION["cCompany_id"] . "'
						GROUP BY pm.product_id";

			}

		} else {
			$qry = "SELECT
					pm.*,
					(
						SELECT SUM(ip.product_qty) 
						FROM lms_invoiceproduct ip 
						WHERE pm.product_id = ip.product_id
					) AS puchase,
					(
						SELECT SUM(cip.product_qty) 
						FROM lms_customerinvoiceproduct cip 
						WHERE pm.product_id = cip.product_id
					) AS sale
			   FROM
					lms_productmaster pm
			  where
					pm.company_id = '" . $_SESSION["cCompany_id"] . "'
					GROUP BY pm.product_id";

		}


		ob_start();
		$sno = 1;
		$out = dbQuery($qry);
		if (dbRows($out) > 0) { ?>

			</h1>
			<table class="report" id="report-table-lanscap" style="font-size:9px">
				<tr>
					<td align="center" width="3%"><b>Sr No</b></td>
					<td align="center" width="10%"><b>Product Name</b></td>
					<td align="center" width="20%"><b>Description</b></td>
					<td align="center" width="5%"><b>Category</b></td>
					<td align="center" width="5%"><b>Type</b></td>
					<td align="center" width="5%"><b>Mesurement</td>
					<td align="center" width="5%"><b>Reorder</b></td>
					<td align="center" width="7%"><b>Parchase QTY</b></td>
					<td align="center" width="5%"><b>Sale QTY</td>
					<td align="center" width="7%"><b>Available QTY</td>

				</tr>
				<?php
				$date = new Date();
				while ($row = dbArray($out)) {
					?>
					<tr>
						<td align="center" width="3%"><?php print $sno; ?></td>
						<td align="left" width="10%"><?php print $row['product_name']; ?></td>
						<td align="left" width="20%"><?php print $row['product_description']; ?></td>
						<td align="center" width="5%"><?php print $row['product_category']; ?></td>
						<td align="center" width="5%"><?php print $row['product_type']; ?></td>
						<td align="center" width="5%"><?php print $row['product_measurement']; ?></td>
						<td align="center" width="5%"><?php print $row['product_reorder']; ?></td>
						<td align="center" width="7%"><?php print $row['puchase']; ?></td>
						<td align="center" width="5%"><?php print $row['sale']; ?></td>
						<td align="center" width="7%"><?php print $row['puchase'] - $row['sale']; ?></td>



						<?php $sno++;
				} ?>
			</table>
			<?php
			$HtmlCode = ob_get_contents();
			ob_end_flush();

			$HtmlUrl = "report-form-customer-support" . $_SESSION["cUserId"] . ".html";
			$HtmlPath = "report/" . $HtmlUrl;
			createHTMLReportPage($header_row_string, $HtmlUrl, $HtmlPath, $HtmlCode);
			?>
			<br />
			<form name="frm" id="frm" action="" method="post">
				<input type="hidden" name="txtFile" id="txtFile" value="<?php echo $HtmlUrl; ?>" />
				<input type="button" name="btnPdf" id="btnPdf" value="Print" onClick="SetPageAction('PRINT');" />
				<input type="button" name="btnPdf" id="btnPdf" value="Create PDF" onclick="SetPageAction('PDF');" />
				<input type="button" name="btnHtml" id="btnHtml" value="Create HTML" onclick="SetPageAction('HTML');" />
				<input type="button" name="btnWord" id="btnWord" value="Create WORD" onclick="SetPageAction('WORD');" />
				<input type="button" name="btnExcel" id="btnExcel" value="Create EXCEL" onclick="SetPageAction('Excel');" />
			</form>
			<script language="javascript" type="text/javascript">
				function SetPageAction(varAction) {
					if (varAction == "PRINT") {
						document.frm.action = "print/create-print.php";
						document.frm.target = "_blank";
						document.frm.submit();
					}
					if (varAction == "PDF") {
						document.frm.action = "convertpdf/create-pdf.php";
						document.frm.target = "_blank";
						document.frm.submit();
					}
					else if (varAction == "HTML") {
						document.frm.action = "<?php echo $HtmlPath; ?>";
						document.frm.target = "_blank";
						document.frm.submit();
					}
					else if (varAction == "WORD") {
						document.frm.action = "convertword/create-word.php";
						document.frm.target = "_blank";
						document.frm.submit();
					}
					else if (varAction == "Excel") {
						document.frm.action = "convertword/create-excel.php";
						document.frm.target = "_blank";
						document.frm.submit();
					}
				}
			</script>
			<?php
		} else {
			echo "<b>No Data Found</b>";
		}

	} else {
		echo "Session Expired";
	}
	?>