<?php require "_header.php";
error_reporting(1); ?>
<div id="main-report" align="center">
	<center>
		<h4>Monthly Tarij 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-monthly-tarij.php';
		</script>
		<?php
	} elseif ($_SESSION['cCompany_id'] != "" && isset($_SESSION['cCompany_id'])) {
		$condition = "";
		$condition_OpeningCashBank = "";
		$condition_ClosingCashBank = "";

		$header_row_string = "Monthly Tarij Report";

		$financial_start_date = "";

		if ($profitstartdate != "") {
			$startdate = DateConversion($profitstartdate, "Y-m-d", "d/m/Y");
			$year = substr($startdate, 0, 4);
			$month = substr($startdate, 5, 2);
			if ($month >= 1 && $month <= 3) {
				$year -= 1;
			}
			$financial_start_date = $year . "-04-01";
			$condition .= " AND vm.voucher_date >= '$startdate' ";
		}

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

		$order_field = "";
		$order_type = "";
		$tarij = array();

		$objVoucherdate = new VoucherMasterMgmt();

		$creditTotal = 0;
		$debitTotal = 0;
		$i = 0;
		$j = 0;
		$count = 1;

		$Qry_CASHBANK = "SELECT am.account_id,account_name
		FROM 
			ams_accountgroup ag,ams_accountmaster am,ams_accountmasterdetail ad
		WHERE 
			ad.company_id = '" . $_SESSION['cCompany_id'] . "' AND
			ag.company_id = '" . $_SESSION['cCompany_id'] . "' AND
			am.company_id = '" . $_SESSION['cCompany_id'] . "' AND
			ad.year_id = '" . $_SESSION['cYear'] . "' AND
			am.account_id = ad.account_id AND
			(ag.account_groupname = 'Cash-In-Hand' OR ag.account_groupname = 'Bank A/c') AND
			ag.account_groupid = am.account_groupid Order By account_groupname DESC ";

		$Qry_OTHER = "SELECT am.account_id,account_name
		FROM 
			ams_accountgroup ag,ams_accountmaster am,ams_accountmasterdetail ad
		WHERE 
			ad.company_id = '" . $_SESSION['cCompany_id'] . "' AND
			ag.company_id = '" . $_SESSION['cCompany_id'] . "' AND
			am.company_id = '" . $_SESSION['cCompany_id'] . "' AND
			ad.year_id = '" . $_SESSION['cYear'] . "' AND
			am.account_id = ad.account_id AND
			(ag.account_groupname != 'Cash-In-Hand' AND ag.account_groupname != 'Bank A/c') AND
			ag.account_groupid = am.account_groupid Order By account_groupname DESC ";

		//START opening balance
		$result_CASHBANK = dbQuery($Qry_CASHBANK);
		if (dbRows($result_CASHBANK) > 0) {
			while ($row_CASHBANK = dbArray($result_CASHBANK)) {
				$result = $objVoucherdate->getNewAccountOpeningBalance($financial_start_date, $startdate, $row_CASHBANK['account_id']);
				if (dbRows($result) > 0) {
					while ($row = dbArray($result)) {
						$final = 0;
						$total = $row['TOTAL'];
						$op_balance = $row['account_opbalance'];

						if ($row['account_crdr'] == "C") {
							$final = $op_balance + $total;
						} else if ($row['account_crdr'] == "D") {
							$final = $op_balance - $total;
							$final = -$final;
						}

						if ($final != "" && $final != "0") {
							if ($final > 0) {
								$tarij[$i]["creditaccount_name"] = "<b>" . $row_CASHBANK['account_name'] . "</b>";
								$tarij[$i]["credittotal_amount"] = $final;
								$creditTotal += $tarij[$i]["credittotal_amount"];
								$i++;
							} else {
								$final = -$final;
								$tarij[$j]["debitaccount_name"] = "<b>" . $row_CASHBANK['account_name'] . "</b>";
								$tarij[$j]["debittotal_amount"] = $final;
								$debitTotal += $tarij[$j]["debittotal_amount"];
								$j++;
							}
						}
					}
				}
			}
		}
		// END opening balance
	
		// START voucher data
		$result_OTHER = dbQuery($Qry_OTHER);
		if (dbRows($result_OTHER) > 0) {
			while ($row_OTHER = dbArray($result_OTHER)) {
				$result = $objVoucherdate->getNewAccountClosingBalance($startdate, $enddate, $row_OTHER['account_id']);
				if (dbRows($result) > 0) {
					while ($row = dbArray($result)) {
						if ($row['voucher_creditamount'] != "" && $row['voucher_creditamount'] != "0") {
							$tarij[$i]["creditaccount_name"] = $row_OTHER['account_name'];
							$tarij[$i]["credittotal_amount"] = $row['voucher_creditamount'];
							$creditTotal += $tarij[$i]["credittotal_amount"];
							$i++;
						}
						if ($row['voucher_debitamount'] != "" && $row['voucher_debitamount'] != "0") {
							$tarij[$j]["debitaccount_name"] = $row_OTHER['account_name'];
							$tarij[$j]["debittotal_amount"] = $row['voucher_debitamount'];
							$debitTotal += $tarij[$j]["debittotal_amount"];
							$j++;
						}
					}
				}
			}
		}
		// END voucher data
	
		// START closing balance
		$result_CASHBANK = dbQuery($Qry_CASHBANK);
		if (dbRows($result_CASHBANK) > 0) {
			while ($row_CASHBANK = dbArray($result_CASHBANK)) {
				$result = $objVoucherdate->getNewAccountClosingBalance($financial_start_date, $enddate, $row_CASHBANK['account_id']);
				if (dbRows($result) > 0) {
					while ($row = dbArray($result)) {
						$final = 0;
						$total = $row['TOTAL'];
						$op_balance = $row['account_opbalance'];

						if ($row['account_crdr'] == "C") {
							$final = $op_balance + $total;
						} else if ($row['account_crdr'] == "D") {
							$final = $op_balance - $total;
							$final = -$final;
						}

						if ($final != "" && $final != "0") {
							if ($final > 0) {
								$tarij[$i]["creditaccount_name"] = "<b>" . $row_CASHBANK['account_name'] . "</b>";
								$tarij[$i]["credittotal_amount"] = $final;
								$creditTotal += $tarij[$i]["credittotal_amount"];
								$i++;
							} else {
								$final = -$final;
								$tarij[$j]["debitaccount_name"] = "<b>" . $row_CASHBANK['account_name'] . "</b>";
								$tarij[$j]["debittotal_amount"] = $final;
								$debitTotal += $tarij[$j]["debittotal_amount"];
								$j++;
							}
						}
					}
				}
			}
		}
		// END closing balance
	
		?>
		<table width="70%" class="report" cellpadding="0" cellspacing="0">
			<tr>
				<th style="text-align:center;" width="40%"><b>Credit</b></th>
				<th align="right" width="10%"><b>Amount</b></th>
				<th style="text-align:center;" width="40%"><b>Debit</b></th>
				<th align="right" width="10%"><b>Amount</b></th>
			</tr>
			<div style="width:70%;height:30px;">
				<div style="width:50%;float:right;text-align:right;">
					Date: <?php print $_REQUEST['profitstartdate']; ?> to <?php print $_REQUEST['profitenddate']; ?>
				</div>
			</div>
			<?php
			$row_num = count($tarij);
			if ($row_num > 0) {
				$j = 0;
				while ($j < $row_num) {
					?>
					<tr>
						<td style="border-bottom:0 none; border-top:0 none; border-right:0 none;">
							<?php print $tarij[$j]["creditaccount_name"]; ?>
						</td>
						<td style="text-align:right; border-bottom:0 none; border-top:0 none; border-left:0 none">
							<?php
							if ($tarij[$j]["credittotal_amount"] != "") {
								print number_format($tarij[$j]["credittotal_amount"], 2);
							}
							?>
							&nbsp;
						</td>
						<td style="border-bottom:0 none; border-top:0 none; border-right:0 none;">
							<?php print $tarij[$j]["debitaccount_name"]; ?>
						</td>
						<td style="text-align:right; border-bottom:0 none; border-top:0 none; border-left:0 none">
							<?php
							if ($tarij[$j]["debittotal_amount"] != "") {
								print number_format($tarij[$j]["debittotal_amount"], 2);
							}
							?>
							&nbsp;
						</td>
					</tr>
					<?php

					$j++;
				}
			}
			if ($creditTotal > $debitTotal) {
				?>
				<tr>
					<td style="text-align:center;">&nbsp;</td>
					<td style="text-align:right">&nbsp;</td>
					<td style="text-align:center;"="center"><b>CASH</b></td>
					<td style="text-align:right;"><b><?php echo number_format($creditTotal - $debitTotal, 2); ?></b>&nbsp;</td>
				</tr>
				<tr>
					<td style="text-align:center;"><b> Total Credit</b></td>
					<td style="text-align:right"><b><?php echo number_format($creditTotal, 2); ?></b>&nbsp;</td>
					<td style="text-align:center;"="center"><b>Total Debit</b></td>
					<td style="text-align:right;"><b><?php echo number_format($creditTotal, 2); ?></b>&nbsp;</td>
				</tr>
				<?php
			} else {
				?>
				<tr>
					<td style="text-align:center;"="center"><b>CASH</b></td>
					<td style="text-align:right;"><b><?php echo number_format($debitTotal - $creditTotal, 2); ?></b>&nbsp;</td>
					<td style="text-align:center;">&nbsp;</td>
					<td style="text-align:right">&nbsp;</td>
				</tr>
				<tr>
					<td style="text-align:center;"><b> Total Credit</b></td>
					<td style="text-align:right"><b><?php echo number_format($debitTotal, 2); ?></b>&nbsp;</td>
					<td style="text-align:center;"="center"><b>Total Debit</b></td>
					<td style="text-align:right;"><b><?php echo number_format($debitTotal, 2); ?></b>&nbsp;</td>
				</tr>
				<?php
			}
			?>
		</table>
		<?php

		$HtmlCode = ob_get_contents();
		ob_end_flush();
		$HtmlUrl = "employee-service-certificate-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 "<center><b>Session Expired</center></b>";
	}
	?>
</div>
<?php require "_footer.php"; ?>