<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<?php
$path="../../";
include("../../map.php");
include("../../secure.php");
include("../../scripts.php");
if ($_GET["ne"]=="haber"){
	$nedir=$t22s[0]; $sislem="sayfa";
}else if($_GET["ne"]=="sayfa"){
	$nedir=$t21s[0]; $sislem="sayfa";
}else if($_GET["ne"]=="msayfa"){
	$nedir=$n8s[0]; $sislem="sayfa";
}else if($_GET["ne"]=="kayit"){
	$nedir=$t24s[0]; $sislem="kayitlar";
}else if($_GET["ne"]=="uye" and $_GET["islem"]=="uyeler"){
	$nedir=$t25s[0]; $sislem="uyeler";
}else if($_GET["ne"]=="uye" and $_GET["islem"]=="relations"){
	$nedir=cctranslate("İlişki"); $sislem="relations";
}else if($_GET["ne"]=="form"){
	$nedir=$form[0]; $sislem="form";
}else if($_GET["ne"]=="reklam"){
	$nedir=$reklam[0]; $sislem="reklam";
}else if($_GET["ne"]=="service"){
	$nedir=$t36s[0]; $sislem="service";
}else if($_GET["ne"]=="gorev"){
	$nedir=$tasks[0]; $sislem="gorev";
}else if($_GET["ne"]=="tgorev"){
	$nedir=$tasks[0]; $sislem="tgorev";
}else if($_GET["ne"]=="eticaret"){
	$nedir=""; $sislem="siparisler";
}else if($_GET["islem"]=="sfatura"){
	$nedir="".$sdil[148]." ".$sdil[24].""; $sislem="muhasebe";
}else if($_GET["islem"]=="afatura"){
	$nedir="".$sdil[149]." ".$sdil[24].""; $sislem="muhasebe";
}else if($_GET["islem"]=="ifatura"){
	$nedir="".$sdil[139]." ".$sdil[24].""; $sislem="muhasebe";
}else{
	$nedir=$_GET["ne"];
}
$sqlf = mysqli_query($conn, "select * from t4 where id=".$_GET["id"]." order by id desc"); $rsf=mysqli_fetch_assoc($sqlf);
?>
<style>
<?php if($rsf["t2"]<>"Fatura"){echo ".icol {display:none}";}?>
	legend {
		margin-bottom: 5px;
		border-bottom: 0px solid #e5e5e5;
		font-weight: bold;
		font-size: 14px;
	}
	.sam {
		display:none !important;
	}
	.cke_contents{
		height:165px;
	}
</style>
<form id="addfis" action="?ne=<?php echo $_GET['ne'];?>&islem=<?php echo $_GET['islem'];?>&id=<?php echo $_GET['id'];?>&durum=updatevoucher" method="post" autocomplete="off">
		<fieldset class="grey-bg col-sm-3 col-xs-3">
			<label>Fiş Tarihi <span style="font-size:8px; color:#ccc;">(t9)</span></label>
			<input type="date" name="t9" value="<?php echo $rsf["t9"]?>" class="form-control">
		</fieldset>
		<fieldset class="grey-bg col-sm-3 col-xs-3">
			<label>Belge No <span style="font-size:8px; color:#ccc;">(t4)</span></label>
			<input type="text" name="t4" value="<?php echo $rsf["t4"]?>" class="form-control">
		</fieldset>
		<fieldset class="grey-bg col-sm-3 col-xs-3">
			<label>Belge Tarihi <span style="font-size:8px; color:#ccc;">(t3)</span></label>
			<input type="date" name="t3" value="<?php echo $rsf["t3"]?>" class="form-control">
		</fieldset>
		<fieldset class="grey-bg col-sm-3 col-xs-3">
			<label>Belge Tipi <span style="font-size:8px; color:#ccc;">(t2)</span></label>
			<input list="lbt" type="text" name="t2" value="<?php echo $rsf["t2"]?>" class="form-control">
			<datalist id="lbt">
				<option value="Fatura">Fatura</option>
			</datalist>
		</fieldset>
		<fieldset class="grey-bg col-sm-12 col-xs-12">
			<label>Açıklama <span style="font-size:8px; color:#ccc;">(n1)</span></label>
			<input type="text" name="n1" id="fis_n1" value="<?php echo $rsf["n1"]?>" class="form-control">
		</fieldset>
		<fieldset class="grey-bg col-sm-12 col-xs-12">
			<label>Fiş İçerik</label>
			<table class="table calc">
				<tr>
					<td scope="col" style="width:14%;" class="">Hesap Kodu</td>
					<td scope="col" style="width:20%;" class="">Hesap Adı</td>
					<td scope="col" style="" class="">Açıklama</td>
					<td scope="col" style="width:10%;" class="icol tac"><?php echo cctranslate("Miktar")?></td>
					<td scope="col" style="width:10%;" class="icol tar"><?php echo cctranslate("B.Fiyat")?></td>
					<td scope="col" style="width:15%;" class=" tar">Borç</td>
					<td scope="col" style="width:15%;" class=" tar">Alacak</td>
				</tr>
					<?php $i = 0; for ($i = 1; $i <= 9; $i++) {?>
					<tr id="row_<?php echo $i?>" data-id="<?php echo $i?>" style="display:none;">
					<input type="hidden" name="ADD[]" value="<?php echo $i?>"/>
						<td scope="col" class="w6">
						<input type="text" class="form-control" name="i1<?php echo $i?>" list="li1<?php echo $i?>" id="i1<?php echo $i?>" placeholder="<?php echo cctranslate("Hesap Kodu")?>">
							<datalist id="li1<?php echo $i?>">
								<?php $sqlt1=mysqli_query($conn, "select * from t1 where s1=".$_SESSION["fID"]." and s2=1 order by n1,s10 asc"); while($rst1=mysqli_fetch_assoc($sqlt1)){?>
									<option <?php if($rst1["n3"]=="Grup" or $rst1["n3"]=="Kebir"){echo "disabled";}?> value="<?php echo $rst1["n1"]?>" data-ad="<?php echo $rst1["n2"]?>" data-tur="<?php echo $rst1["n3"]?>" data-tip="<?php echo $rst1["n4"]?>"><?php echo $rst1["n2"]?> {<?php echo $rst1["n3"]?>}</option>
								<?php }?>
							</datalist>
						</td>
						<td scope="col" class="">
						<input type="text" class="form-control" list="li2<?php echo $i?>" name="i2<?php echo $i?>" id="i2<?php echo $i?>" placeholder="<?php echo cctranslate("Hesap Adı")?>">
							<datalist id="li2<?php echo $i?>">
								<?php $sqlt1=mysqli_query($conn, "select * from t1 where s1=".$_SESSION["fID"]." and s2=1 order by n1,s10 asc"); while($rst1=mysqli_fetch_assoc($sqlt1)){?>
									<option <?php if($rst1["n3"]=="Grup" or $rst1["n3"]=="Kebir"){echo "disabled";}?> value="<?php echo $rst1["n2"]?>" data-kod="<?php echo $rst1["n1"]?>" data-tur="<?php echo $rst1["n3"]?>" data-tip="<?php echo $rst1["n4"]?>"><?php echo $rst1["n2"]?> {<?php echo $rst1["n3"]?>}</option>
								<?php }?>
							</datalist>
						</td>
						<td scope="col" class=""><input type="text" class="form-control" id="i3<?php echo $i?>" name="i3<?php echo $i?>" placeholder="<?php echo cctranslate("Açıklama")?>"></td>
						<td scope="col" class="icol tac" style="display:none;"><input type="number" class="form-control tac" name="i6<?php echo $i?>" placeholder="<?php echo cctranslate("Miktar")?>" value="1"></td>
						<td scope="col" class="icol tar" style="display:none;"><input type="text" class="form-control tar bfiyat" name="i7<?php echo $i?>" placeholder="<?php echo cctranslate("B.Fiyat")?>" value="0"></td>
						<td scope="col" class="tar"><input type="text" class="form-control tar borc" name="i4<?php echo $i?>" id="i4<?php echo $i?>" placeholder="<?php echo cctranslate("Borç Tutar")?>" value="0" onfocus="if(this.value == '0') { this.value = ''} "  onblur="if(this.value == '') { this.value='0'; }"></td>
						<td scope="col" class="tar"><input type="text" class="form-control tar alacak" name="i5<?php echo $i?>" id="i5<?php echo $i?>" placeholder="<?php echo cctranslate("Alacak Tutar")?>" value="0" onfocus="if(this.value == '0') { this.value = ''} "  onblur="if(this.value == '') { this.value='0'; }"></td>
					</tr>
					<script>
						jQuery(function(){
							jQuery('#i1<?php echo $i?>').on('change',function(){
								var val2=$('#i2<?php echo $i?>').val();
								var n1=$('#fis_n1').val();
								var c=0;
								var val=$('#i1<?php echo $i?>').val();
								var ad= $('#li1<?php echo $i?>').find('option[value="'+val+'"]').data('ad');
								<?php $sqlt1=mysqli_query($conn, "select * from t1 where s1=".$_SESSION["fID"]." and s2=1 order by n1,s10 asc"); while($rst1=mysqli_fetch_assoc($sqlt1)){?>
								if(val=="<?php echo $rst1["n1"]?>"){c++;}
								<?php }?>
								if(c!=0){
									jQuery('#i2<?php echo $i?>').val(ad);
									jQuery('#i3<?php echo $i?>').val(n1);
									var tip = $('#li1<?php echo $i?>').find('option[value="'+val+'"]').data('tip');
									//if(tip=="Alacak"){jQuery("#i4<?php echo $i?>").show();jQuery("#i5<?php echo $i?>").hide();}
									//if(tip=="Borç"){jQuery("#i4<?php echo $i?>").hide();jQuery("#i5<?php echo $i?>").show();}
									//vtr('row_<?php echo $i+1?>');
								}
									if(c==0){alert('Hesap Bulunamadı, Yeni Eklenecek !'); jQuery("#row_<?php echo $i?>").css("background","rgb(255 17 0 / 19%)");}else{jQuery("#row_<?php echo $i?>").css("background","transparent");}
							});
							jQuery('#i2<?php echo $i?>').on('change',function(){
								var val1=$('#i1<?php echo $i?>').val();
									var c=0;
									var val=$('#i2<?php echo $i?>').val();
									var kod= $('#li2<?php echo $i?>').find('option[value="'+val+'"]').data('kod');
									<?php $sqlt1=mysqli_query($conn, "select * from t1 where s1=".$_SESSION["fID"]." and s2=1 order by n1,s10 asc"); while($rst1=mysqli_fetch_assoc($sqlt1)){?>
									if(kod=="<?php echo $rst1["n1"]?>"){c++;}
									<?php }?>
								if(c!=0){
									jQuery('#i1<?php echo $i?>').val(kod);
									var tip = $('#li2<?php echo $i?>').find('option[value="'+val+'"]').data('tip');
									//if(tip=="Alacak"){jQuery("#i4<?php echo $i?>").show();jQuery("#i5<?php echo $i?>").hide();}
									//if(tip=="Borç"){jQuery("#i4<?php echo $i?>").hide();jQuery("#i5<?php echo $i?>").show();}
									//vtr('row_<?php echo $i+1?>');
									//if(c==0){alert('Hesap Bulunamadı, Yeni Eklenecek !'); jQuery("#row_<?php echo $i?>").css("background","rgb(255 17 0 / 19%)");}else{jQuery("#row_<?php echo $i?>").css("background","transparent");}
								}
							}); 
						});
					</script>
				<?php }?>
				<?php
				$i=0;$sqlfu = mysqli_query($conn, "select * from t4 where s1=".$_GET["id"]." order by id asc"); while($rsfu=mysqli_fetch_assoc($sqlfu)){$i++;?>
					<tr onmouseover="show('del<?php echo $rsfu["id"]?>');"  onmouseout="hide('del<?php echo $rsfu["id"]?>');">
						<input type="hidden" name="KID[]" value="<?php echo $rsfu["id"]?>"/>
						<td scope="col" class="" style="position:relative;">
							<input type="text" name="i1<?php echo $rsfu["id"]?>" class="form-control" value="<?php echo $rsfu["t2"]?>" placeholder="Hesap Kodu">
							<a onclick="return confirm('Silmek İstediğinize Eminmisiniz?')" href="?durum=delitem&id=<?php echo $_GET["id"]?>&iid=<?php echo $rsfu["id"]?>" style="position: absolute; top: 9px; right: -12px; padding: 5px; display:none;" id="del<?php echo $rsfu["id"]?>" class="v3-btn v3-btn-xs v3-btn-danger fa fa-trash"></a>
						</td>
						<td scope="col" class=""><input type="text" name="i2<?php echo $rsfu["id"]?>" class="form-control" value="<?php echo $rsfu["t3"]?>"></td>
						<td scope="col" class=""><input type="text" name="i3<?php echo $rsfu["id"]?>" class="form-control" value="<?php echo $rsfu["t4"]?>"></td>
						<td scope="col" class="icol tac" style=""><input type="number" class="form-control tac" name="i6<?php echo $rsfu["id"]?>" placeholder="<?php echo cctranslate("Miktar")?>" value="<?php echo $rsfu["t6"]?>"></td>
						<td scope="col" class="icol tar" style=""><input type="text" class="form-control tar bfiyat" name="i7<?php echo $rsfu["id"]?>" placeholder="<?php echo cctranslate("B.Fiyat")?>" value="<?php echo number_format($rsfu["t7"],2, ',', '.')?>"></td>
						<td scope="col" class=" tar"><input type="text"  name="i4<?php echo $rsfu["id"]?>" class="form-control tar borc"  value="<?php echo number_format($rsfu["pb1"],2, '.', '')?>"></td>
						<td scope="col" class=" tar"><input type="text" name="i5<?php echo $rsfu["id"]?>" class="form-control tar alacak"  value="<?php echo number_format($rsfu["pb2"],2, '.', '')?>"></td>
					</tr>
				<?php }?>
				<tr>
					<td scope="col" class="">Toplam</td>
					<td scope="col" class=""></td>
					<td scope="col" class="icol" style=""></td>
					<td scope="col" class="icol" style=""></td>
					<td scope="col" class=""></td>
					<td scope="col" class=" tar"><input type="text" class="form-control tar" id="btoplam" name="btoplam" placeholder="Borç Toplam" value="<?php echo number_format($rsf["pb1"],2, ',', '.')?>" readonly></td>
					<td scope="col" class=" tar"><input type="text" class="form-control tar" id="atoplam" name="atoplam" placeholder="Alacak Toplam" value="<?php echo number_format($rsf["pb2"],2, ',', '.')?>" readonly></td>
				</tr>
			</table>
		</fieldset>
		<div class="col-sm-12 col-xs-12" style="clear:both; width:100%; margin-top:20px;">
			<a onclick="return confirm('Silmek İstediğinize Eminmisiniz?')" href="?ne=<?php echo $_GET['ne'];?>&islem=<?php echo $_GET['islem'];?>&id=<?php echo $_GET['id'];?>&durum=delvoucher" class="btn btn-danger" style="float:right;margin-right:10px;"><?php echo cctranslate("Fişi Sil")?></a>
			<button type="button" onclick="sumcheck();" name="btn" class="btn btn-success" style="float:right;margin-right:10px;"><?php echo cctranslate("Kaydet")?></button>
			<a onclick="toggle('#row_1')" class="btn btn-warning" style="float:right;margin-right:10px;"><?php echo cctranslate("Satır Ekle")?></a>
			<a href="../print/fis/fis.php?id=<?php echo $_GET['id'];?>" class="btn btn-info" style="float:right;margin-right:10px;"><?php echo cctranslate("Yazdır")?></a>
		</div>
		<div style="width: 80%; position: fixed; margin-top: 10%; left: 10%; right: 10%; padding: 20px; background: black; text-align: center; color: white; display:none;" id="hata">
			<h4><?php echo cctranslate("Tutarlar Eşit Değil, Lütfen Kontrol Edin!")?></h4>
			<br>
			<button type="button" id="bduzenle" class="btn btn-warning"><?php echo cctranslate("Kapat")?></button>
		</div>
	<script>
			jQuery(".borc, .alacak, .bfiyat").keyup(function(){
				var str = jQuery(this).val();
				jQuery(this).val(str.replace(",", "."));
			});
			function sumcheck() {
				var atoplam = jQuery('#atoplam').val();
				var btoplam = jQuery('#btoplam').val();
				var atoplam = atoplam.replace(".", "");
				var btoplam = btoplam.replace(".", "");
				if(atoplam != btoplam){
					alert(btoplam);
					jQuery("#hata").show();
				}else{
					document.getElementById('addfis').submit();
				}
			}
			jQuery(function(){
				jQuery("table input").keypress(function (e) {
					if (e.keyCode == 13) {
						var trid_ = $(this).closest('tr').attr('data-id'); // table row ID
						var trid =parseInt(trid_, 10)+1;
						if(jQuery('#i1' + trid_ + '').val()!="" & jQuery('#i2' + trid_ + '').val()!=""){
							jQuery('#row_' + trid + '').show();
							jQuery('#i1' + trid + '').focus();
						}
					}
				});
			});
			jQuery("#bduzenle").click(function() {
			  jQuery("#hata").hide();
			});
			jQuery('.borc').on('keyup',function(){
				var borc = 0;
				$(".borc").each(function(index) {
					var intInputValue = parseFloat(jQuery(this).val());
					if (!isNaN(intInputValue)) {
						borc = borc + intInputValue;
					}
				});
				borc = borc.toFixed(2).replace(".", ",");
				jQuery('#btoplam').attr('value', borc);
			});
			jQuery('.alacak').on('keyup',function(){
				var alacak = 0;
				$(".alacak").each(function(index) {
					var intInputValue = parseFloat(jQuery(this).val());
					if (!isNaN(intInputValue)) {
						alacak = alacak + intInputValue;
					}
				});
				alacak = alacak.toFixed(2).replace(".", ",");
				jQuery('#atoplam').attr('value', alacak);
			});
	</script>
<?php
if($_GET["durum"]=="updatevoucher"){
	$btoplam=$_POST["btoplam"];
	$atoplam=$_POST["atoplam"];
	$Rs = mysqli_query($conn, "update t4 set t2='".$_POST["t2"]."', t3='".$_POST["t3"]."', t4='".$_POST["t4"]."', t9='".$_POST["t9"]."', n1='".$_POST["n1"]."', pb2='".para($atoplam)."', pb1='".para($btoplam)."' Where id=".$_GET["id"]."");
	$KIDS = implode(',',$_POST["KID"]);
		$fisler = explode(',',$KIDS); foreach($fisler as $fisID){
			if($_POST["i1".$fisID.""]<>""){
				$Rs = mysqli_query($conn, "update t4 set t2='".$_POST["i1".$fisID.""]."', t3='".$_POST["i2".$fisID.""]."', t4='".$_POST["i3".$fisID.""]."', t6='".$_POST["i6".$fisID.""]."', t7='".para($_POST["i7".$fisID.""])."', t9='".$_POST["t9"]."', pb1='".para($_POST["i4".$fisID.""])."', pb2='".para($_POST["i5".$fisID.""])."' Where id=".$fisID."");
			}
		}
	$ADDS = implode(',',$_POST["ADD"]);
		$items = explode(',',$ADDS); foreach($items as $itemID){
			if($_POST["i1".$itemID.""]<>""){
				$Rs	= $conn->query("insert into t4(ne,s1,s2,t1,t2,t3,t4,t6,t7,t9,t10,t11,pb1,pb2,ts1) values ('furun', '".$rsf["id"]."', '".$_SESSION["fID"]."', 'fis', '".$_POST["i1".$itemID.""]."', '".$_POST["i2".$itemID.""]."', '".$_POST["i3".$itemID.""]."', '".$_POST["i6".$itemID.""]."', '".$_POST["i7".$itemID.""]."', '".$_POST["t9"]."', '".substr($_POST["i1".$itemID.""],0,3)."', '".substr($_POST["i1".$itemID.""],0,6)."', '".$_POST["i4".$itemID.""]."', '".$_POST["i5".$itemID.""]."', now())");
			}
		}
	echo location("?ne=".$_GET["ne"]."&id=".$_GET["id"]."&islem=fis&durum=ok",self,0,1);
}elseif($_GET["durum"]=="delvoucher"){
	$sql = mysqli_query($conn, "delete from t4 where id=".$_GET["id"]);
	$sql = mysqli_query($conn, "delete from t4 where s1=".$_GET["id"]);
	echo location("".$wurl."/".$crmpath."?ne=".$_GET["ne"]."&islem=fis&durum=ok",top,0,1);
}elseif($_GET["durum"]=="delitem"){
	$sql = mysqli_query($conn, "delete from t4 where id=".$_GET["iid"]);
	$i=0;$sqlfu = mysqli_query($conn, "select * from t4 where s1=".$_GET["id"]." order by id asc"); while($rsfu=mysqli_fetch_assoc($sqlfu)){$i++;
		$borc = $borc + $rsfu["pb1"];
		$alacak = $alacak + $rsfu["pb2"];
	}
	$Rs = mysqli_query($conn, "update t4 set pb2='".para($alacak)."', pb1='".para($borc)."' Where id=".$_GET["id"]."");
	echo location("?id=".$_GET["id"]."&durum=ok",self,0,1);
}
include("../public/log.php");
?>