<?php require "_header.php";
error_reporting(1); ?>
<div id="main-report" align="center">
	<center>
		<h4>Day Book Report</h4>
	</center>

	<?php
	$profitstartdate = Escape($_REQUEST["profitstartdate"]);
	$profitenddate = Escape($_REQUEST["profitenddate"]);

	if ($profitstartdate == "" && $profitenddate == "") {
		?>
		<script language="javascript" type="text/javascript">
			location.href = 'report-form-day-book-result.php';
		</script>
		<?php
	} elseif ($_SESSION['cCompany_id'] != "" && isset($_SESSION['cCompany_id'])) {
		$condition = "";

		if ($profitstartdate != "") {
			$startdate = DateConversion($profitstartdate, "Y-m-d", "d/m/Y");
			$condition .= " AND voucher_date >= '$startdate' ";
		}

		if ($profitenddate != "") {
			$enddate = DateConversion($profitenddate, "Y-m-d", "d/m/Y");
			$condition .= " AND voucher_date <= '$enddate' ";
		}

		$order_field = "";
		$order_type = "";
		$header_row_string = "Day Book Report";

		$objVoucherMasterMgmt = new VoucherMasterMgmt();
		$result = $objVoucherMasterMgmt->getVoucherDistinctDates("", "", $condition, $order_field, $order_type);
		$date_num = dbRows($result);

		$i = 1;

		$table = array();
		ob_start(); //starting fors
		if (dbRows($result) > 0) {
			while ($row = dbArray($result)) {
				$voucher_date = FormatDate($row["voucher_date"], "d/m/Y");
				$voucher_ids = $row["voucher_ids"];
				?>
				<div style="width:70%;height:30px;">
					<div style="width:50%;float:left;text-align:left;">Date:<?php print $voucher_date; ?></div>
					<div style="width:50%;float:right;text-align:right;">Date: <?php print $_POST['profitstartdate']; ?> To :
						<?php print $_POST['profitenddate']; ?></div>
				</div>
				<table width="70%" class="report" cellpadding="0" cellspacing="0">
					<thead>
						<tr>
							<th align="center" width="40%" style="text-align:center"><b>Credit</b></th>
							<th align="right" width="10%" style="text-align:center"><b>Amount</b></th>
							<th align="center" width="40%" style="text-align:center"><b>Debit</b></th>
							<th align="right" width="10%" style="text-align:center"><b>Amount</b></th>
						</tr>
					</thead>
					<tr>
						<td><b>Op.Balance</b></td>
						<td style="text-align:right">
							<?php echo $objVoucherMasterMgmt->getOpeningBalance($row["voucher_date"], $startdate); ?>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
					<?php
					$order_field = "";
					$order_type = "";
					$condition = " AND find_in_set(voucher_id,'" . $row["voucher_ids"] . "') ";
					$limit = 0;
					$start = 0;

					$arrayDayBookEntry = $objVoucherMasterMgmt->getDayBookEntry($limit, $start, $condition, $order_field, $order_type);

					$row_num = count($arrayDayBookEntry);


					if ($row_num > 0) {
						$j = 0;
						$creditTotal = 0;
						$debitTotal = 0;
						while ($j < $row_num) {

							?>
							<tr>
								<td width="40%" style="text-align:left"><?php echo $arrayDayBookEntry[$j]["CreditAccount"]; ?>&nbsp;</td>
								<td width="10%" style="text-align:right"><?php echo $arrayDayBookEntry[$j]["CreditAmount"]; ?>&nbsp;</td>
								<td width="40%" style="text-align:left"><?php echo $arrayDayBookEntry[$j]["DebitAccount"]; ?>&nbsp;</td>
								<td width="10%" style="text-align:right"><?php echo $arrayDayBookEntry[$j]["DebitAmount"]; ?>&nbsp;</td>
							</tr>
							<?php
							$creditTotal += $arrayDayBookEntry[$j]["CreditAmount"];
							$debitTotal += $arrayDayBookEntry[$j]["DebitAmount"];
							$j++;
						}
					}
					?>
					<tr>
						<td align="center" style="text-align:right"><b> Total Credit</b></td>
						<td style="text-align:right"><b><?php echo $creditTotal; ?></b>&nbsp;</td>
						<td align="center" style="text-align:right"><b>Total Debit</b></td>
						<td style="text-align:right"><b><?php echo $debitTotal; ?></b>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp; </td>
						<td>&nbsp;</td>
						<td><b>Closing Balance</b></td>
						<td style="text-align:right">
							<?php echo $objVoucherMasterMgmt->getClosingBalance($row["voucher_date"], $startdate); ?>&nbsp;</td>
					</tr>
				</table>
				<br />
				<?php $i++;
			}

			$HtmlCode = ob_get_contents();
			ob_end_flush();
			$HtmlUrl = "account-day-book-report-" . $_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 "No Data Found";
		}
	} else {
		echo "Session Expired";
	}
	?>
</div>
<?php require "_footer.php"; ?>