<?php require "_header.php"; ?>
<div id="main-report" align="center">
	<center>
		<h4>Customer Bill Report Datewise </h4>
	</center>
	<?php

	$bill_startdate = Escape($_REQUEST["bill_startdate"]);
	$bill_enddate = Escape($_REQUEST["bill_enddate"]);
	if ($bill_startdate == "") {
		?>
		<script language="javascript" type="text/javascript">
			location.href = 'report-customerbillsummary.php';
		</script>
		<?php
	} elseif ($_SESSION['cCompany_id'] != "" && isset($_SESSION['cCompany_id'])) {
		$header_row_string = "Customer Bill Report Datewise  ";

		$total = 0;

		ob_start();

		$invoicid = "SELECT invoice_no,billdate,customer_name,group_concat(product_name) as product_name,sum(tamount) as tamount from lms_customerbill where company_id='" . $_SESSION['cCompany_id'] . "' ";

		if ($bill_startdate != "From") {
			$bill_startdate = DateConversion($bill_startdate, "Y-m-d", "d/m/Y");
			$invoicid .= " AND billdate >= '$bill_startdate' ";
		}

		if ($bill_enddate != "To") {
			$bill_enddate = DateConversion($bill_enddate, "Y-m-d", "d/m/Y");
			$invoicid .= " AND billdate <= '$bill_enddate' ";
		}
		$invoicid .= " group by invoice_no ";

		// 	echo $invoicid; exit;
		$finalinvoiceid = dbQuery($invoicid);
		if (dbRows($finalinvoiceid) > 0) { ?>
			<table width="100%" class="report">
				<tr style="background-color:#CCC">
					<th width="10%" style="text-align:center">Bill No.</th>
					<th width="10%" style="text-align:center">Date</th>
					<th width="30%" style="text-align:center">Customer Name</th>
					<th width="35%" style="text-align:center">Products</th>
					<th width="15%" style="text-align:center">Total Amount</th>
				</tr>
				<?php
				while ($row1 = dbArray($finalinvoiceid)) {
					?>

					<tr>
						<td width="10%" style="text-align:center"> <?php echo $row1["invoice_no"]; ?> </td>
						<td width="10%" style="text-align:center"><?php echo date("d-m-Y", strtotime($row1['billdate'])); ?></td>
						<td width="30%" style="text-align:left"><?php echo $row1["customer_name"]; ?></td>
						<td width="35%" style="text-align:left"><?php echo $row1["product_name"]; ?></td>
						<td width="15%" style="text-align:right; margin-right:10px"><?php echo $row1["tamount"]; ?></td>
					</tr>

					<?php
					$total += $row1["tamount"];
				}

				?>
				<tr>
					<th colspan="4" style="text-align:right; font-weight:bold; font-size:14px">Total Amount</th>
					<td style="text-align:right; font-weight:bold; margin-right:10px; font-size:14px"><?php echo $total; ?></td>
				</tr>

			</table>
			<?php

			$HtmlCode = ob_get_contents();
			ob_end_flush();

			$HtmlUrl = "report-form-customer-bill-summary" . $_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>
			<br />
			<br />
			<?php
		} else {
			echo "<b>No Data Found </b>";
		}
	} else {
		echo "Session Expired";
	}
	?>
</div>
<?php
require "_footer.php"; ?>