<?php require "_header.php"; ?>
<div id="main-report" align="center">
	<center>
		<h4>Customer Invoice Report Datewise<br />From :
			<?php echo DateConversion($_REQUEST['bill_startdate'], "d-m-Y", "d/m/Y"); ?> TO :
			<?php echo DateConversion($_REQUEST['bill_enddate'], "d-m-Y", "d/m/Y"); ?></h4>
	</center>
	<?php


	$bill_startdate = Escape($_REQUEST["bill_startdate"]);
	$bill_enddate = Escape($_REQUEST["bill_enddate"]);
	$hiddendist_code = Escape($_REQUEST["hiddendist_code"]);

	if ($bill_startdate == "") {
		?>
		<script language="javascript" type="text/javascript">
			location.href = 'report-customerinvoicesummary.php';
		</script>
		<?php
	} elseif ($_SESSION['cCompany_id'] != "" && isset($_SESSION['cCompany_id'])) {
		$header_row_string = "Customer Invoice Report Datewise  ";
		$total = 0;
		ob_start();

		$invoicid = "SELECT 	lci.customerinvoice_no,lci.customerinvoice_date,lci.status,lc.customer_orgname,group_concat(lp.product_name) as products,lci.tamount,lci.dueamount,lum.user_name from lms_customerinvoice lci,lms_customerinvoiceproduct lcp,lms_customer lc,lms_productmaster lp,lms_usermaster lum where lci.company_id='" . $_SESSION['cCompany_id'] . "' and lci.year_id='" . $_SESSION['cYear'] . "' and lci.customer_id=lc.customer_id and lci.customerinvoice_id=lcp.customerinvoice_id and lcp.product_id=lp.product_id and lci.createdby=lum.user_id";

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

		if ($bill_enddate != "To") {
			$bill_enddate = DateConversion($bill_enddate, "Y-m-d", "d/m/Y");
			$invoicid .= " AND lci.customerinvoice_date <= '$bill_enddate' ";
		}
		if ($hiddendist_code != "" && $hiddendist_code != "All") {
			$invoicid .= " AND lc.district_id = '" . $hiddendist_code . "' ";
		}

		$invoicid .= " group by lcp.customerinvoice_id ";
		/*echo $invoicid;
		   exit;*/
		$finalinvoiceid = dbQuery($invoicid);
		if (dbRows($finalinvoiceid) > 0) { ?>
			<table width="100%" class="report">
				<tr style="background-color:#CCC">
					<th width="3%" style="text-align:center">Invoice No.</th>
					<th width="10%" style="text-align:center">Date</th>
					<th width="28%" style="text-align:center">Customer Name</th>
					<th width="28%" style="text-align:center">Products</th>
					<th width="8%" style="text-align:center">Status</th>
					<th width="7%" style="text-align:center">Generated By</th>
					<th width="8%" style="text-align:center">Total Amount</th>
					<th width="8%" style="text-align:center">Due Amount</th>

				</tr>
				<?php
				while ($row1 = dbArray($finalinvoiceid)) {
					?>
					<?php if ($row1["status"] == 'Cancelled') { ?>
						<tr style=" color:#C00; font-weight:600">
						<?php } else { ?>
						<tr style="color:#000">
						<?php } ?>

						<td style="text-align:center"> <?php echo $row1["customerinvoice_no"]; ?> </td>
						<td style="text-align:center"><?php echo date("d-m-Y", strtotime($row1['customerinvoice_date'])); ?></td>
						<td style="text-align:left"><?php echo $row1["customer_orgname"]; ?></td>
						<td style="text-align:left"><?php echo $row1["products"]; ?></td>
						<td style="text-align:center;"><?php echo $row1["status"]; ?></td>
						<td style="text-align:center;"><?php echo $row1["user_name"]; ?></td>
						<td style="text-align:right; margin-right:10px"><?php echo $row1["tamount"]; ?></td>
						<td style="text-align:right; margin-right:10px;color:#F00; font-weight:600"><?php echo $row1["dueamount"]; ?>
						</td>
					</tr>

					<?php
					if ($row1["status"] != 'Cancelled') {
						$total += $row1["tamount"];
						$Duetotal += $row1["dueamount"];
					}
				}

				?>
				<tr>
					<th colspan="6" 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>
					<td style="text-align:right; font-weight:bold; margin-right:10px; font-size:14px;color:#F00; font-weight:600""><?php echo $Duetotal; ?></td>
	  </tr>
	   
	 </table>
	 <?php

		   $HtmlCode = ob_get_contents();
		   ob_end_flush();

		   $HtmlUrl = "report-form-customer-invoice-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"; ?>