<?php require "_header.php";
error_reporting(1); ?>
<div id="main-report" align="center">
	<center>
		<h4>Customer Report For AMC ProductDatewise</h4>
	</center>
	<?php
	$hiddendist_code = Escape($_REQUEST["hiddendist_code"]);
	$bill_startdate = Escape($_REQUEST["bill_startdate"]);
	$bill_enddate = Escape($_REQUEST["bill_enddate"]);

	if ($_SESSION['cCompany_id'] != "" && isset($_SESSION['cCompany_id'])) {
		/*	echo "<pre>";
				  print_r($_REQUEST);
				  exit;*/

		$header_row_string = "Customer AMC Product Report Datewise  ";
		$total = 0;

		ob_start();

		$invoicid = "SELECT 
			lci.customerinvoice_no,
			lci.customerinvoice_date, 
			lci.status,
			lc.customer_id as custid, 
			lc.customer_orgname,
			lp.product_name, 
			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='4' and 
			lci.year_id='5' 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 AND 
			lp.product_type = 'AMC' ";
		if (isset($hiddendist_code) && $hiddendist_code != 'All' && $hiddendist_code != "") {
			$invoicid .= " AND lc.district_id = '" . $hiddendist_code . "'";
		}

		$invoicid .= " Order By lc.customer_id";
		$out_product = dbQuery($invoicid);
		$data = array();

		if (dbRows($out_product) > 0) { ?>
			<table width="100%" class="report">
				<tr style="background-color:#CCC">
					<th width="3%" style="text-align:center">Invoice No.</th>
					<th width="5%" style="text-align:center">Invoice Date</th>
					<th width="5%" style="text-align:center">Expiery 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>


				</tr>
				<?php
				$from = "";
				$to = "";
				while ($result1 = dbArray($out_product)) {
					if ($result1['custid'] != $temp) {
						$temp = $result1['custid'];
						$data[$temp] = $result1;
					} else {
						$data[$temp]['customerinvoice_date'] = $result1['customerinvoice_date'];
						continue;
					}
				}
				foreach ($data as $d) {
					$flage = false;
					$next_date = date('d-m-Y', strtotime(date("Y/m/d", strtotime($d['customerinvoice_date'])) . " +12 month"));
					$from = str_replace("/", "-", $bill_startdate);
					$to = str_replace("/", "-", $bill_enddate);
					if ($from != "From" && $to != "To") {
						$date = strtotime($next_date);
						$frm_date = strtotime($from);
						$to_date = strtotime($to);

						if ($date >= $frm_date && $date <= $to_date) {
							$flage = true;
						} else {
							continue;
						}
					} else {
						$flage = true;
					}
					if ($flage) {
						if ($d["status"] == 'Cancelled') {
							continue;
							/*?>
															<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($d['customerinvoice_date'])); ?></td>
						<?php

						if (date('Y-m-d') >= (date('Y-m-d', strtotime('+11 month', strtotime($d['customerinvoice_date']))))) {
							if (date('Y-m-d') <= (date('Y-m-d', strtotime('+12 month', strtotime($d['customerinvoice_date']))))) { ?>
								<td style="text-align:center;color:#F00; font-weight:600""><?php echo $next_date; ?></td>
		   <?php } else { ?>
					   <td style=" text-align:center"><?php echo $next_date ?></td>
							<?php }
						} else { ?>
							<td style="text-align:center"><?php echo $next_date ?></td>
						<?php } ?>

						<td style="text-align:left"><?php echo $d["customer_orgname"]; ?></td>
						<td style="text-align:left"><?php echo $d["product_name"]; ?></td>
						<td style="text-align:center;"><?php echo $d["status"]; ?></td>
						<td style="text-align:center;"><?php echo $d["user_name"]; ?></td>

						</tr>

						<?php


					}
				}

				?>


			</table>
			<?php

			$HtmlCode = ob_get_contents();
			ob_end_flush();

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