<?php require "_header.php"; ?>
<div id="main-report" align="center">
	<center>
		<h4>Customer Due Payment Report </h4>
	</center>
	<?php
	$customer_idhedden = Escape($_REQUEST["customer_idhedden"]);
	$hiddendist_code = Escape($_REQUEST["hiddendist_code"]);
	$startdate = Escape($_REQUEST["startdate"]);
	$enddate = Escape($_REQUEST["enddate"]);

	if ($hiddendist_code == "") {
		?>
		<script language="javascript" type="text/javascript">
			location.href = 'report-customer-duepayment.php';
		</script>
		<?php
	} else if ($_SESSION['cCompany_id'] != "" && isset($_SESSION['cCompany_id'])) {
		$header_row_string = "Customer Due Payment Report ";

		$total = 0;

		ob_start();
		$where = "";
		if ($hiddendist_code != "" && $hiddendist_code != "All") {
			$where = " and lc.district_id='" . $hiddendist_code . "'";
		}

		$invoicid = "SELECT 	lci.customerinvoice_no ,lc.customer_cpname1 , lc.customer_address,(select user_name from lms_usermaster where lms_usermaster.user_id = lci.createdby)as UserName,lci.customerinvoice_date,lci.status,lc.customer_cpmobile1,lc.customer_phone1,lc.customer_phone2,lc.customer_orgname,lci.tamount,lci.dueamount from lms_customerinvoice lci,lms_customer lc,lms_distmaster dm where lci.company_id='" . $_SESSION['cCompany_id'] . "' and lci.year_id='" . $_SESSION['cYear'] . "' and lci.customer_id=lc.customer_id and dm.dist_id=lc.district_id and lci.status='DuePayment'";
		$invoicid = $invoicid . $where;

		if (isset($customer_idhedden) && $customer_idhedden != '') {
			$invoicid .= " AND lci.customer_id = " . $customer_idhedden;
		}


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

		if ($enddate != "To") {
			$enddate = DateConversion($enddate, "Y-m-d", "d/m/Y");
			$invoicid .= " AND lci.customerinvoice_date <= '$enddate' ";
		}


		$invoicid = $invoicid . " order by lci.customerinvoice_no"; // lci.company_id='".$_SESSION['cCompany_id']."' and
		//echo $invoicid;
		$finalinvoiceid = dbQuery($invoicid);
		if (dbRows($finalinvoiceid) > 0) { ?>
				<table width="100%" class="report">
					<tr style="background-color:#CCC">
						<th width="6%" style="text-align:center">Invoice No.</th>
						<th width="7%" style="text-align:center">Date</th>
						<th width="20%" style="text-align:center">Customer Name</th>
						<th width="25%" style="text-align:center">Address</th>
						<th width="8%" style="text-align:center">User Name</th>
						<th width="8%" style="text-align:center">Contact Person</th>
						<th width="7%" style="text-align:center">ContactNo</th>
						<th width="8%" style="text-align:center">Total Amount</th>
						<th width="10%" style="text-align:center">Due Amount</th>

					</tr>
				<?php
				while ($row1 = dbArray($finalinvoiceid)) {
					?>

						<tr>


							<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["customer_address"]; ?></td>
							<td style="text-align:center"><?php echo $row1["UserName"]; ?></td>
							<th style="text-align:center"> <?php echo $row1["customer_cpname1"]; ?> </td>
							<td style="text-align:center"><?php
							if ($row1['customer_cpmobile1'] != "")
								echo $row1["customer_cpmobile1"];
							else if ($row1['customer_phone1'] != "") {
								echo $row1["customer_phone1"];
							} else if ($row1['customer_phone2'] != "") {
								echo $row1["customer_phone2"];
							} else
								echo '-';
							?></td>
							<td style="text-align:center"><?php echo $row1["tamount"]; ?></td>
							<td style="text-align:center; color:#F00; font-weight:600"><?php echo $row1["dueamount"]; ?></td>

						</tr>

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

				?>
					<tr>
						<th colspan="8" style="text-align:right; font-weight:bold; font-size:14px">Total Due Amount&nbsp;</th>
						<td style="text-align:center; font-weight:bold; font-size:14px; color:#F00;"><?php echo $total; ?></td>
					</tr>

				</table>
				<?php

				$HtmlCode = ob_get_contents();
				ob_end_flush();

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