<?php require "_header.php"; ?>
<div id="main-content1" align="center">
	<center>
		<h1>Monthaly Tarij Report</h1>
	</center>
	<?php
	$profitstartdate = Escape($_POST["profitstartdate"]);
	$profitenddate = Escape($_POST["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;

		//START opening balance
		$Qry = "SELECT 
				account_id,account_name 
			FROM 
				ams_accountgroup ag,ams_accountmaster am 
			WHERE 
				ag.company_id = '" . $_SESSION['cCompany_id'] . "' AND
				am.company_id = '" . $_SESSION['cCompany_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 ";

		$result_accountid = dbQuery($Qry);
		if (dbRows($result_accountid) > 0) {
			while ($row = dbArray($result_accountid)) {

				/*$result_openingdata =$objVoucherdate->getNewAccountOpeningBalance($financial_start_date,$startdate,$row['account_id']);
						 if(dbRows($result_openingdata) > 0){
							 while($row_openingdata = dbArray($result_openingdata)){
								 $final = 0;
								 print "<br>".$op_balance = $row_openingdata['account_opbalance'];
								 print " ".$total = $row_openingdata['total'];
							 
								 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){
										 $profitlossArray[$i]["creditaccount_id"] = $row_openingdata['account_id'];
										 $profitlossArray[$i]["creditaccount_name"] = $row_openingdata['account_name'];
										 $profitlossArray[$i]["credittotal_amount"] = $final;
										 $creditFinal += $profitlossArray[$i]["voucher_creditamount"];
										 $i++;
									 } else{
										 $final = -$final;
										 $profitlossArray[$j]["debitaccount_id"] = $row_openingdata['account_id'];
										 $profitlossArray[$j]["debitaccount_name"] = $row_openingdata['account_name'];
										 $profitlossArray[$j]["debittotal_amount"] = $final;
										 $debitFinal += $profitlossArray[$j]["voucher_debitamount"];
										 $j++;
									 }
								 }
							 
							 }
						 }*/
			}
		}
		// END opening balance
	
		// START voucher data
		$result_voucherdata = $objVoucherdate->getNewAccountClosingBalance($startdate, $enddate, "");
		if (dbRows($result_voucherdata) > 0) {
			ob_start();
			while ($row = dbArray($result_voucherdata)) {
				if ($row['voucher_creditamount'] > 0) {
					$tarij[$i]["creditaccount_id"] = $row['account_id'];
					$tarij[$i]["creditaccount_name"] = $row['account_name'];
					$tarij[$i]["voucher_creditamount"] = $row['voucher_creditamount'];
					$creditTotal += $tarij[$i]["voucher_creditamount"];
					$i++;
				}
				if ($row['voucher_debitamount'] > 0) {
					$tarij[$j]["debitaccount_id"] = $row['account_id'];
					$tarij[$j]["debitaccount_name"] = $row['account_name'];
					$tarij[$j]["voucher_debitamount"] = $row['voucher_debitamount'];
					$debitTotal += $tarij[$j]["voucher_debitamount"];
					$j++;
				}
			}
		} else {
			echo "<center><b>No Data Found</b></center>";
		}
		// END voucher data
	
		// START closing balance
		if (dbRows($result_accountid) > 0) {
			while ($row = dbArray($result_accountid)) {
				$result_openingdata = $objVoucherdate->getNewAccountClosingBalance($financial_start_date, $startdate, $row['account_id']);
				if (dbRows($result_openingdata) > 0) {
					while ($row_openingdata = dbArray($result_openingdata)) {
						$final = 0;
						print "<br>" . $op_balance = $row_openingdata['account_opbalance'];
						print " " . $total = $row_openingdata['total'];

						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) {
								$profitlossArray[$i]["creditaccount_id"] = $row_openingdata['account_id'];
								$profitlossArray[$i]["creditaccount_name"] = $row_openingdata['account_name'];
								$profitlossArray[$i]["credittotal_amount"] = $final;
								$creditFinal += $profitlossArray[$i]["voucher_creditamount"];
								$i++;
							} else {
								$final = -$final;
								$profitlossArray[$j]["debitaccount_id"] = $row_openingdata['account_id'];
								$profitlossArray[$j]["debitaccount_name"] = $row_openingdata['account_name'];
								$profitlossArray[$j]["debittotal_amount"] = $final;
								$debitFinal += $profitlossArray[$j]["voucher_debitamount"];
								$j++;
							}
						}

					}
				}
			}
		}
		// END closing balance
	
		?>
		<table width="70%" style="border:none;">
			<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 $_POST['profitstartdate']; ?> to <?php print $_POST['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]["voucher_creditamount"] != "") {
								print number_format($tarij[$j]["voucher_creditamount"], 2);
							}
							?>
						</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]["voucher_debitamount"] != "") {
								print number_format($tarij[$j]["voucher_debitamount"], 2);
							}
							?>
						</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></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></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></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></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></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></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"; ?>