<?php require "_header.php";
error_reporting(1); ?>
<div id="main-report" align="center">
	<center>
		<h4>Profit & Loss A/C</h4>
	</center>
	<?php
	if ($_SESSION['cCompany_id'] != "" && isset($_SESSION['cCompany_id'])) {

		$start_date = "";
		$end_date = "";

		$condition = "";
		$profit = 0;
		$loss = 0;
		$financial_start_date = "";
		$profitstartdate = Escape($_REQUEST["profitstartdate"]);
		$profitenddate = Escape($_REQUEST["profitenddate"]);
		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 >= '$financial_start_date' ";
		}

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

		$objVoucherMasterMgmt = new VoucherMasterMgmt();
		$result = $objVoucherMasterMgmt->profit_loss_balance_sheet_adjustment($condition, "Profit & Loss");

		$profitlossArray = array();
		$creditFinal = 0;
		$debitFinal = 0;
		$i = 0;
		$j = 0;


		if (dbRows($result) > 0) {

			while ($row = dbArray($result)) {

				$final = 0;
				$op_balance = $row['account_opbalance'];
				$total = $row['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['account_id'];
						$profitlossArray[$i]["creditaccount_name"] = $row['account_name'];
						$profitlossArray[$i]["creditsubgroup_name"] = $row["subgroup_name"];
						$profitlossArray[$i]["credittotal_amount"] = $final;
						$creditFinal += $profitlossArray[$i]["voucher_creditamount"];
						$i++;
					} else {
						$final = -$final;
						$profitlossArray[$j]["debitaccount_id"] = $row['account_id'];
						$profitlossArray[$j]["debitaccount_name"] = $row['account_name'];
						$profitlossArray[$j]["debitsubgroup_name"] = $row["subgroup_name"];
						$profitlossArray[$j]["debittotal_amount"] = $final;
						$debitFinal += $profitlossArray[$j]["voucher_debitamount"];
						$j++;
					}
				}
			}

			$row_num = count($profitlossArray);
			if ($row_num > 0) {
				$j = 0;
				?>
				<table width="70%" cellpadding="0" cellspacing="0" class="report">
					<tr>
						<th colspan="2" width="50%">CREDIT A/C</th>
						<th colspan="2" width="50%">DEBIT A/C</th>
					</tr>
					<tr>
						<th>A/C Name</th>
						<th>Amount</th>
						<th>A/C Name</th>
						<th>Amount</th>
					</tr>

					<?php
					$group1 = "";
					$group2 = "";
					while ($j < $row_num) {

						if (
							(($profitlossArray[$j]["creditsubgroup_name"] != "") && ($group1 != $profitlossArray[$j]["creditsubgroup_name"])) ||
							(($profitlossArray[$j]["debitsubgroup_name"] != "") && ($group2 != $profitlossArray[$j]["debitsubgroup_name"]))
						) {
							?>
							<tr>
								<td style="border:0 none">
									<b>
										<?php
										if ($group1 != $profitlossArray[$j]["creditsubgroup_name"])
											echo $group1 = $profitlossArray[$j]["creditsubgroup_name"];
										else
											echo "&nbsp;";
										?>
									</b>
								</td>
								<td style="border-left:0 none; border-top:0 none; border-bottom:0 none;">&nbsp;</td>
								<td style="border:0 none">
									<b>
										<?php
										if ($group2 != $profitlossArray[$j]["debitsubgroup_name"])
											echo $group2 = $profitlossArray[$j]["debitsubgroup_name"];
										else
											echo "&nbsp;";
										?>
									</b>
								</td>
								<td style="border-left:0 none; border-top:0 none; border-bottom:0 none;">&nbsp;</td>
							</tr>
						<?php } ?>
						<tr>
							<td style="border:0 none">&nbsp;&nbsp;&nbsp;<?php echo $profitlossArray[$j]["creditaccount_name"]; ?></td>
							<td style="border-left:0 none; border-top:0 none; border-bottom:0 none; text-align:right">
								<?php
								if ($profitlossArray[$j]["credittotal_amount"] != "") {
									echo number_format($profitlossArray[$j]["credittotal_amount"], 2);
								}
								?>
								&nbsp;
							</td>
							<td style="border:0 none">&nbsp;&nbsp;&nbsp;<?php echo $profitlossArray[$j]["debitaccount_name"]; ?></td>
							<td style="border-left:0 none; border-top:0 none; border-bottom:0 none; text-align:right">
								<?php
								if ($profitlossArray[$j]["debittotal_amount"] != "") {
									echo number_format($profitlossArray[$j]["debittotal_amount"], 2);
								}
								?>
								&nbsp;
							</td>
						</tr>
						<?php
						$creditFinal += $profitlossArray[$j]["credittotal_amount"];
						$debitFinal += $profitlossArray[$j]["debittotal_amount"];
						$j++;
					}
					$objAccountMasterMgmt = new AccountMasterMgmt();
					if ($creditFinal < $debitFinal) {
						$loss = $debitFinal - $creditFinal;
						$objAccountMasterMgmt->addedit_profit_loss($loss, 'D');
						?>
						<tr>
							<td style="text-align:right"><b>Loss</b></td>
							<td style="text-align:right"><b><?php print number_format($loss, 2); ?></b>&nbsp;</td>
							<td style="text-align:right">&nbsp;</td>
							<td style="text-align:right">&nbsp;</td>
						</tr>
						<tr>
							<td style="text-align:right"><b>Total Final</b></td>
							<td style="text-align:right"><b><?php print number_format($debitFinal, 2); ?></b>&nbsp;</td>
							<td style="text-align:right"><b>Total Final</b></td>
							<td style="text-align:right"><b><?php print number_format($debitFinal, 2); ?></b>&nbsp;</td>
						</tr>

						<?php
					} else {
						$profit = $creditFinal - $debitFinal;
						$objAccountMasterMgmt->addedit_profit_loss($profit, 'C');
						?>
						<tr>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td style="text-align:right"><b>Profit</b></td>
							<td style="text-align:right"><b><?php print number_format($profit, 2); ?></b>&nbsp;</td>
						</tr>
						<tr>
							<td style="text-align:right"><b>Total Final</b></td>
							<td style="text-align:right"><b><?php print number_format($creditFinal, 2); ?></b>&nbsp;</td>
							<td style="text-align:right"><b>Total Final</b></td>
							<td style="text-align:right"><b><?php print number_format($creditFinal, 2); ?></b>&nbsp;</td>
						</tr>
						<?php
					}
					?>
				</table>
				<?php
			} else {
				print "No data found.";
			}
		} else {
			echo "<center><b>Session Expired</center></b>";
		}
	}
	?>
</div>
<?php require "_footer.php"; ?>