<?php
include("../../../secure.php");
$path="../../../";
include("../../../map.php");
if($_GET["exp"]=="xls"){
	header("Content-Type: text/html;charset=utf-8");
	header('Content-Encoding: UTF-8');
	header("Content-Disposition: attachment; filename=mizan-".date("d-m-Y").".xls");
	echo "\xEF\xBB\xBF"; // UTF-8 BOM
}else{
	include("../../../scripts.php");
}
$yfd = new DateTime('first day of January');
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<style type="text/css">
	table {font-family: 'Open Sans', sans-serif;}
	@media print { .noprint { display: none !important; } }
.table>thead>tr>th, .table>tbody>tr>th, .table>tfoot>tr>th, .table>thead>tr>td, .table>tbody>tr>td, .table>tfoot>tr>td {
    padding: 3px;
    line-height: 1.2;
}
</style>
<?php if($_REQUEST["tip"]==""){?>
<form action="" method="post">
	<div class="col-sm-3">
		<select name="tip" class="form-control" >
				<option value="genel">Genel</option>
				<option value="detayli">Detaylı</option>
		</select>
	</div>
	<div class="col-sm-3">
		<input type="date" class="form-control" name="ts1" placeholder="" value="<?php echo $yfd->format('Y-m-d')?>">
	</div>
	<div class="col-sm-3">
		<input type="date" class="form-control" name="ts2" placeholder="" value="<?php echo date('Y-m-d')?>">
	</div>
	<div class="col-sm-3">
		<button type="submit" name="btn" class="btn btn-warning" style="float:right;margin-right:10px;"><?php echo translate("Listele")?></button>
	</div>
</form>
<?php }elseif($_REQUEST["tip"]<>""){?>
			<table class="table">
				<tr>
					<td scope="col" style="width:40%; font-size:11px;" class=""><?php echo $_SESSION["fName"]?></td>
					<td scope="col" style="text-align:center;" class=""><b><?php echo ihb($_REQUEST["tip"])?> Mizan <?php if($_REQUEST["ts1"]<>"" or $_REQUEST["ts2"]<>""){?><br><?php echo dtarih($_REQUEST["ts1"],"gg.aa.yyyy")?>-<?php echo dtarih($_REQUEST["ts2"],"gg.aa.yyyy")?><?php }?></b></td>
					<td scope="col" style="width:30%; font-size:11px; text-align:right" class=""><?php echo dtarih(date("Y-m-d H:i:s"),"gg.aa.yyyy time")?></td>
				</tr>
			</table>
			<table class="table" style="font-size:11px;">
				<tr>
					<th scope="col" style="width:13%;" class="">Hesap Kodu</th>
					<th scope="col" style="" class="">Hesap Adı</th>
					<th scope="col" style="width:10%;text-align:right;" class="">Borç</th>
					<th scope="col" style="width:10%;text-align:right;" class="">Alacak</th>
					<th scope="col" style="width:10%;text-align:right;" class="">Bakiye Borç</th>
					<th scope="col" style="width:10%;text-align:right;" class="">Bakiye Alacak</th>
				</tr>
				<?php
					//if($_REQUEST["tip"]=="genel"){$qsor=" and n3='Kebir'";}
					$i=0;$sqlk = mysqli_query($conn, "select * from t1 where s1='".$_SESSION["fID"]."' ".$qsor." order by n1 asc"); while($kplanlar=mysqli_fetch_assoc($sqlk)){
#=======================>Hesaplama
						$bt=0;$at=0;$atb=0;$btb=0;$gbt=0;$gat=0;$gatb=0;$gbtb=0;
						$sqlm = mysqli_query($conn, "select * from t1 where n3='Muavin' and s1='".$_SESSION["fID"]."' order by id asc"); while($mplanlar=mysqli_fetch_assoc($sqlm)){
							if($kplanlar["n1"]==$mplanlar["n1"]){
								$i=0;$sqlh = mysqli_query($conn, "select * from t4 where t2='".$mplanlar["n1"]."' and s2=".$_SESSION["fID"]." order by id asc"); while($rsfu=mysqli_fetch_assoc($sqlh)){
								$sqlf = mysqli_query($conn, "select * from t4 where s1=".$_SESSION["fID"]." and  id='".$rsfu["s1"]."' order by id desc"); $rsf=mysqli_fetch_assoc($sqlf);
								if($_REQUEST["ts1"]<>"" and ($rsf["t9"]>=$_REQUEST["ts1"] and $rsf["t9"]<=$_REQUEST["ts2"])){
									$bt=$bt+$rsfu["pb1"];
									$at=$at+$rsfu["pb2"];
										$gbt=$gbt+$rsfu["pb1"];
										$gat=$gat+$rsfu["pb2"];
									if($bt<$at){
										$atb=$at-$bt;
										$btb=0;
											$gatb=floatval($gat)-floatval($gbt);
											$gbtb=0;
									}else{
										$btb=$bt-$at;
										$atb=0;
											$gbtb=floatval($gbt)-floatval($gat);
											$gatb=0;
									}
								}
							}
							}else if($kplanlar["n1"]==substr($mplanlar["n1"],0,6)){
								$i=0;$sqlh = mysqli_query($conn, "select * from t4 where t2='".$mplanlar["n1"]."' and s2=".$_SESSION["fID"]." order by id asc"); while($rsfu=mysqli_fetch_assoc($sqlh)){
								$sqlf = mysqli_query($conn, "select * from t4 where s1=".$_SESSION["fID"]." and  id='".$rsfu["s1"]."' order by id desc"); $rsf=mysqli_fetch_assoc($sqlf);
								if($_REQUEST["ts1"]<>"" and ($rsf["t9"]>=$_REQUEST["ts1"] and $rsf["t9"]<=$_REQUEST["ts2"])){
									$bt=$bt+$rsfu["pb1"];
									$at=$at+$rsfu["pb2"];
									if($bt<$at){
										$atb=$at-$bt;
										$btb=0;
									}else{
										$btb=$bt-$at;
										$atb=0;
									}
								}
							}
						}else if($kplanlar["n1"]==substr($mplanlar["n1"],0,3)){
							$i=0;$sqlh = mysqli_query($conn, "select * from t4 where t2='".$mplanlar["n1"]."' and s2=".$_SESSION["fID"]." order by id asc"); while($rsfu=mysqli_fetch_assoc($sqlh)){
							$sqlf = mysqli_query($conn, "select * from t4 where s1=".$_SESSION["fID"]." and id='".$rsfu["s1"]."' order by id desc"); $rsf=mysqli_fetch_assoc($sqlf);
								if($_REQUEST["ts1"]<>"" and ($rsf["t9"]>=$_REQUEST["ts1"] and $rsf["t9"]<=$_REQUEST["ts2"])){
									$bt=$bt+$rsfu["pb1"];
									$at=$at+$rsfu["pb2"];
									if($bt<$at){
										$atb=($at-$bt);
										$btb=0;
									}else{
										$btb=$bt-$at;
										$atb=0;
									}
								}
							}
						}
					}
#=======================>Hesaplama
				?>
				<?php if(($bt<>0 or $at<>0)){?>
				<?php if(($_REQUEST["tip"]=="genel" and $kplanlar["n3"]=="Kebir") or ($_REQUEST["tip"]=="detayli")){?>
				<tr <?php if($kplanlar["n3"]=="Kebir"){?>style="font-weight:bold"<?php }?>>
					<td scope="col" style="" class=""><?php echo $kplanlar["n1"]?></td>
					<td scope="col" style="" class=""><?php echo $kplanlar["n2"]?></td>
					<td scope="col" style="text-align:right;" class=""><?php echo number_format(abs($bt),2, ',', '.')?></td>
					<td scope="col" style="text-align:right;" class=""><?php echo number_format(abs($at),2, ',', '.')?></td>
					<td scope="col" style="text-align:right;" class=""><?php echo number_format(abs($btb),2, ',', '.')?></td>
					<td scope="col" style="text-align:right;" class=""><?php echo number_format(abs($atb),2, ',', '.')?></td>
				</tr>
				<?php }?>
				<?php }?>
<?php
				#=======================>Toplam
				$bgt=$bgt+$gbt;	//Alt Toplam
				$agt=$agt+$gat;	//Alt Toplam
				$bgtb=$bgtb+$gbtb;	//Alt Toplam
				$agtb=$agtb+$gatb;	//Alt Toplam
				#=======================>Toplam
?>
<?php }?>
				<tr>
					<th scope="col" style="" class="">
					</th>
					<th scope="col" style="" class=""></th>
					<th scope="col" style="text-align:right;" class=""><?php echo number_format(abs($bgt),2, ',', '.')?></th>
					<th scope="col" style="text-align:right;" class=""><?php echo number_format(abs($agt),2, ',', '.')?></th>
					<th scope="col" style="text-align:right;" class=""><?php echo number_format(abs($bgtb),2, ',', '.')?></th>
					<th scope="col" style="text-align:right;" class=""><?php echo number_format(abs($agtb),2, ',', '.')?></th>
				</tr>
			</table>
			<a class="btn btn-xs btn-success fa fa-file-excel-o noprint" style="position: absolute; top: 35px; right: 10px;" href="?tip=<?php echo $_REQUEST["tip"]?>&ts1=<?php echo $_REQUEST["ts1"]?>&ts2=<?php echo $_REQUEST["ts2"]?>&exp=xls"></a>
			<i class="btn btn-xs btn-warning fa fa-print noprint" style="position: absolute; top: 35px; right: 40px;" onclick="window.print();"></i>
			<a href="?" class="btn btn-xs btn-info fa fa-arrow-left noprint" style="position: absolute; top: 35px; right: 70px;"></a>
<?php }?>