<?php
error_reporting(0);
if($fid==""){ $fid=$_GET["id"]; }
if($_GET["show_total"]<>""){
	$path="../../";
	include("../../map.php");
	$sqlf=mysqli_query($conn, "select * from t4 where (id=".$fid." or t1='".$fid."')"); $rsf=mysqli_fetch_assoc($sqlf);
	$jsi = json_decode($rsf["n17"], true);
	$jsm = json_decode($rsf["m1"], true);
	$ftarih="".$rsf["s4"]."-".$rsf["s3"]."-".$rsf["s2"].""; $fno=$rsf["t1"]; $t4=$rsf["t4"];
	$sqlm=mysqli_query($conn, "select * from t5 where id=".$rsf["s1"].""); $rs5=mysqli_fetch_assoc($sqlm);
	$sqlhc=mysqli_query($conn, "select * from t4 where ne='cari' and s5=".$rsf["id"]." order by id desc"); while($hcari=mysqli_fetch_assoc($sqlhc)){
		$tahsilat = $tahsilat + $hcari["pb3"];
	}
	$fcariID=$rsf["s1"]; $fcariName=$rs5["t1"]; $fcariEmail=v3d($rs5["t2"]);
}
	if($rsf["t5"]=="%" or $rsf["t5"]==""){
		if($rsf["pb4"]<>0){$fcisko=$rsf["pb4"]; $fcisk=(100-$fcisko)/100;}else{$fcisko=0; $fcisk=0;}
	//echo "".$fcisko."% İndirim";
	}else{
		if($rsf["pb4"]<>0){$fcisko=$rsf["pb4"]; $fcisk=$rsf["pb4"];}else{$fcisko=0; $fcisk=0;}
	//echo "".$fcisko." ".$kpb." İndirim";
	}
	//$gtotal=0; $stotal=0; $kdv1=0; $kdv8=0; $kdv18=0; $kdv20=0; $kdvdiger=0; $price=0; $price_with_qty=0; $price_with_qty_tax=0;
	$alert=0; $sqlfu = mysqli_query($conn, "SELECT * FROM t4 WHERE LOWER(ne) = 'furun' AND (LOWER(t10) NOT IN ('ay', 'aylık', 'gün', 'günlük', 'yıl', 'yıllık')) AND s1 = " . $rsf["id"]); while ($rsfu = mysqli_fetch_assoc($sqlfu)) {
		//-------------------------------------------------------------------/Kdv Hesaplama/----
		if($s11==1){
			$price=($rsfu["pb1"] / (1+($rsfu["s4"]/100)));
			$price_with_qty=$rsfu["s5"] * ($rsfu["pb1"] / (1+($rsfu["s4"]/100)));
			$price_with_qty_tax=$rsfu["s5"] * $rsfu["pb1"];
		}else{
			$price=$rsfu["pb1"];
			$price_with_qty=$rsfu["s5"] * $rsfu["pb1"];
			$price_with_qty_tax=$rsfu["s5"] * ($rsfu["pb1"] * (1+($rsfu["s4"]/100)));
		}
		if($rsfu["s4"]==1){
			$mkdv1=$mkdv1 + ($price_with_qty_tax - $price_with_qty);
			$old_kdv1=$old_kdv1 + ($price_with_qty_tax - $price_with_qty);
		}elseif($rsfu["s4"]==8){
			$mkdv8=$mkdv8 + ($price_with_qty_tax - $price_with_qty);
			$old_kdv8=$old_kdv8 + ($price_with_qty_tax - $price_with_qty);
		}elseif($rsfu["s4"]==18){
			$mkdv18=$mkdv18 + ($price_with_qty_tax - $price_with_qty);
			$old_kdv18=$old_kdv18 + ($price_with_qty_tax - $price_with_qty);
		}elseif($rsfu["s4"]==20){
			$mkdv20=$mkdv20 + ($price_with_qty_tax - $price_with_qty);
			$old_kdv20=$old_kdv20 + ($price_with_qty_tax - $price_with_qty);
		}else{
			$mkdvdiger=$mkdvdiger + ($price_with_qty_tax - $price_with_qty);
			$old_kdvdiger=$old_kdvdiger + ($price_with_qty_tax - $price_with_qty);
		}
		//-------------------------------------------------------------------/Kdv Hesaplama/----
		//-------------------------------------------------------------------/Toplam/----
			$old_stotal=$old_stotal+$price_with_qty;
			$old_total=$old_stotal;
			$stotal=$stotal+$price_with_qty;
			$mstotal=$mstotal+$price_with_qty;
			if($fcisk<>0 and ($rsf["t5"]=="%" or $rsf["t5"]=="")){
				$total=$stotal*$fcisk;
				$mtotal=$mstotal*$fcisk;
			}elseif($fcisk<>0 and ($rsf["t5"]==$kpb)){
				$total=$stotal-$fcisk;
				$mtotal=$mstotal-$fcisk;
			}else{
				$total=$stotal;
				$mtotal=$mstotal;
			}
			$kdv1=($total*$old_kdv1)/$stotal;
			$mkdv1=($mtotal*$mkdv1)/$mstotal;
			$kdv8=($total*$old_kdv8)/$stotal;
			$kdv18=($total*$old_kdv18)/$stotal;
			$kdv20=($total*$old_kdv20)/$stotal;
			$kdvdiger=($total*$old_kdvdiger)/$stotal;
			$mkdv8=($mtotal*$mkdv8)/$mstotal;
			$mkdv18=($mtotal*$mkdv18)/$mstotal;
			$mkdv20=($mtotal*$mkdv20)/$mstotal;
			$mkdvdiger=($mtotal*$mkdvdiger)/$mstotal;
		//-------------------------------------------------------------------/Toplam/----
	}
	$totals = ['daily' => 0, 'monthly' => 0, 'yearly' => 0];
	$sqlfpu = mysqli_query($conn, "SELECT * FROM t4 WHERE LOWER(ne) = 'furun' AND (LOWER(t10) IN ('ay', 'aylık', 'gün', 'günlük', 'yıl', 'yıllık')) AND s1 = " . $rsf["id"]);  while ($rsfpu = mysqli_fetch_assoc($sqlfpu)) {
		if ($s11 == 1) {
			$pprice = $rsfpu["s5"] * $rsfpu["pb1"];
		} else {
			$pprice = $rsfpu["s5"] * ($rsfpu["pb1"] * (1 + ($rsfpu["s4"] / 100)));
		}
		// Periyodik toplamı ilgili kategoriye ekle
		$key = strtolower($rsfpu["t10"]); 
		if (in_array($key, ['gün', 'günlük'])) {
			$totals['daily'] += $pprice;
		} elseif (in_array($key, ['ay', 'aylık'])) {
			$totals['monthly'] += $pprice;
		} elseif (in_array($key, ['yıl', 'yıllık'])) {
			$totals['yearly'] += $pprice;
		}
	}
	json_encode(['periyodik' => $totals], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
	$old_gtotal=$old_total+$old_kdv1+$old_kdv8+$old_kdv18+$old_kdv20+$old_kdvdiger;
	$gtotal=$total+$kdv1+$kdv8+$kdv18+$kdv20+$kdvdiger;
	$mgtotal=$mtotal+$mkdv1+$mkdv8+$mkdv18+$mkdv20+$mkdvdiger;
	if($rsfu["s5"]-$rsfu["s5"]>0 and $f7==1){$alert=1; $nostock="<i class=\"fa fa-exclamation-triangle text-danger\" aria-hidden=\"true\"></i> ";};
?>
<?php if($_GET["show_total"]<>""){?>
<script>
	jQuery(document).keypress(function(e) {
	  if (e.keyCode == 27) {
		jQuery("#myModal").modal('hide');
	  }
	 });
	jQuery(function() {
		jQuery(".mfrm").click(function () {
			jQuery("#myModal").fadeIn();
			var title = jQuery(this).attr("data-title");
			jQuery(".modal-title").html(title);
			var theModal = jQuery(this).data("target"),
			frmSRC = jQuery(this).attr("data-url");
			var refSRC = jQuery(this).attr("data-ref");
			if(refSRC != undefined){
				jQuery(theModal + ' .mcbtn').attr('onclick', 'location.href=\''+refSRC+'\'');
			}
			jQuery(theModal + ' iframe').attr('src', frmSRC);
			jQuery(theModal + ' .close').click(function () {
				jQuery(theModal + ' iframe').attr('src', 'about:blank');
			});
		});
	});
	jQuery(".cmdl").click(function (){
		event.preventDefault();
		jQuery('#myModal').fadeOut('slow');
		jQuery('#modalFull').fadeOut('slow');
		jQuery('#modalSmall').fadeOut('slow');
		jQuery('.modal-backdrop').fadeOut('slow');
		loadFunction('modules/clients/ajax_client_current.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&ts1=<?php echo $_GET["ts1"];?>&ts2=<?php echo $_GET["ts2"];?>','#icari', '');
	});
</script>
	<table style="width:100%; margin:0px; padding:0px;margin-top: -34px;font-size:14px;" class="noshadow">
		<tr style="height:32px;">
			<td class="w58 tar">
				<?php echo $sdil[154]; //Ödeme Tipi?>
			</td>
			<td style="padding-left:15px;">
				<select id="ptype" class="form-control tar" style="" name="t2" dir="rtl">
					<option ptype="" value=""><?php echo $sdil[154]; //Ödeme Tipi?></option>
					<option ptype="Nakit" value="Nakit" <?php if($rsf["t2"]=="Nakit"){?>Selected<?php } //end if?>><?php echo $sdil[160]; //Nakit?></option>
					<option ptype="Pos" value="Pos" <?php if($rsf["t2"]=="Pos"){?>Selected<?php } //end if?>><?php echo $sdil[162]; //Pos?></option>
					<option ptype="Banka" value="Banka" <?php if($rsf["t2"]=="Banka"){?>Selected<?php } //end if?>><?php echo $sdil[161]; //Banka?></option>
					<option ptype="Çek" value="Çek" <?php if($rsf["t2"]=="Çek"){?>Selected<?php } //end if?>><?php echo $sdil[163]; //Çek?></option>
					<option ptype="Taksit" value="Taksit" <?php if($rsf["t2"]=="Taksit"){?>Selected<?php } //end if?>><?php echo $sdil[227]; //Taksit?></option>
				</select>
			</td>
		</tr>
		<tr style="height:32px;">
			<td class="tar"><?php echo $sdil[175]; //Ara Toplam?></td>
			<td class="tar" style="padding-left:15px;"><input type="text" id="subtotal" name="isubtotal" class="form-control tar" style="" data-old="<?php echo sprice($old_stotal)?>" value="<?php echo wprice($stotal)?>" size="20"></td>
		</tr>
		<tr style="height:32px;">
			<td class="tar">
				<?php echo $sdil[102]; //discount?>
				<?php echo $isk?>
			</td>
			<td class="tar" style="padding-left:15px;">
				<select name="t5" id="discount_type" style="float:left;">
					<option value="<?php echo $kpb?>" <?php if($rsf["t5"]==$kpb){echo "selected";}?>><?php echo $kpb?></option>
					<option value="%" <?php if($rsf["t5"]=="%"){echo "selected";}?>>Oran</option>
				</select>
				<input name="discount" id="discount" type="text" class="form-control tar" style="float:right; width:60%" data-old="<?php echo sprice($old_stotal)?>" value="<?php echo ($fcisko != "") ? $fcisko : "0"; ?>" size="20"  onfocus="if(this.value == '0') { this.value = ''} "  onblur="if(this.value == '') { this.value='0'; }">
			</td>
		</tr>
		<tr style="height:32px;" class="bg-warning">
			<td class="tar"><?php echo $sdil[159]; //Balance?> (<?php echo $ikpb?>)</td>
			<td class="tar" style="padding-left:15px;"><input id="total" type="text" name="itoplam" class="form-control tar" data-old="<?php echo sprice($old_total);?>" style="" value="<?php echo wprice($total);?>" size="20"></td>
		</tr>
			<tr style="height:32px; <?php echo ($old_kdv1 == "") ? "display:none;" : ""; ?>">
				<td class="tar"><?php echo $sdil[86]; //Tax?> 1%</td>
				<td class="tar" style="padding-left:15px;"><input type="text" id="kdv1" name="ikdv1" class="form-control tar" style="" data-old="<?php echo sprice($old_kdv1)?>" value="<?php echo wprice($kdv1)?>" size="20"></td>
			</tr>
			<tr style="height:32px; <?php echo ($old_kdv8 == "") ? "display:none;" : ""; ?>">
				<td class="tar"><?php echo $sdil[86]; //Tax?> 8%</td>
				<td class="tar" style="padding-left:15px;"><input type="text" id="kdv8" name="ikdv8" class="form-control tar" style="" data-old="<?php echo sprice($old_kdv8)?>" value="<?php echo wprice($kdv8)?>" size="20"></td>
			</tr>
			<tr style="height:32px; <?php echo ($old_kdv18 == "") ? "display:none;" : ""; ?>">
				<td class="tar"><?php echo $sdil[86]; //Tax?> 18%</td>
				<td class="tar" style="padding-left:15px;"><input type="text" id="kdv18" name="ikdv18" class="form-control tar" style="" data-old="<?php echo sprice($old_kdv18)?>" value="<?php echo wprice($kdv18)?>" size="20"></td>
			</tr>
			<tr style="height:32px; <?php echo ($old_kdv20 == "") ? "display:none;" : ""; ?>">
				<td class="tar"><?php echo $sdil[86]; //Tax?> 20%</td>
				<td class="tar" style="padding-left:15px;"><input type="text" id="kdv20" name="ikdv20" class="form-control tar" style="" data-old="<?php echo sprice($old_kdv20)?>" value="<?php echo wprice($kdv20)?>" size="20"></td>
			</tr>
			<tr style="height:32px; <?php echo ($old_kdvdiger == "") ? "display:none;" : ""; ?>">
				<td class="tar"><?php echo $sdil[86]; //Tax?></td>
				<td class="tar" style="padding-left:15px;"><input type="text" id="kdvdiger" name="ikdvdiger" class="form-control tar" style="" data-old="<?php echo sprice($old_kdvdiger)?>" value="<?php echo wprice($kdvdiger)?>" size="20"></td>
			</tr>
		<tr style="height:32px;" class="bg-danger">
			<td class="tar"><?php echo $sdil[176]; //Toplam?></td>
			<td class="tar" style="padding-left:15px;"><input name="total" id="gtotal" type="text" class="form-control tar" style="" data-old="<?php echo sprice($old_gtotal);?>" value="<?php echo wprice($gtotal);?>" size="20"></td>
		</tr>
		<tr style="height:32px;" class="bg-info" style="<?= (array_sum($totals) > 0) ? '' : 'display:none;' ?>">
			<td class="tar"><?php echo ctranslate("Periyodik"); //Periyodik?></td>
			<td class="tar" style="padding-left:15px;"><input type="text" class="form-control tar" style="" value="<?php echo wprice($totals["daily"]+$totals["monthly"]+$totals["yearly"]);?>" size="20"></td>
			<input name="iperiodic_daily" type="hidden" value="<?php echo $totals["daily"]?>">
			<input name="iperiodic_monthly" type="hidden" value="<?php echo $totals["monthly"]?>">
			<input name="iperiodic_yearly" type="hidden" value="<?php echo $totals["yearly"]?>">
		</tr>
		<tr style="height:32px; display:none;">
			<td class="tar"><?php echo ctranslate("Maliyet"); //Maliyet?></td>
			<td class="tar" style="padding-left:15px;"><input name="tmaliyet" id="tmaliyet" type="text" class="form-control tar" style="" onclick="'" value="<?php echo wprice($mgtotal);?>" size="20"></td>
		</tr>
		<?php if($gtotal<>0){?>
			<?php if($rsf["pb2"]<>$tahsilat){?>
					<tr class="v3-bg-warning" style="height:32px;">
						<td colspan="2" style="text-align:center;"><a class="cp mfrm v3-btn-xs fa fa-plus" style="font-size: 11px; padding: 5px" data-toggle="modal" data-target="#myModal" data-url="../../fonksiyon.php?islem=cdetail&id=<?php echo $fcariID?>&fid=<?php echo $_GET["id"]?>" data-title="<?php echo ctranslate("Hareket Ekle")?>"> <?php echo ctranslate("Hesap Hareketi Ekle")?></a></td>
					</tr>
			<?php }else{?>
					<tr class="v3-bg-success" style="height:32px;">
						<td colspan="2" style="text-align:center;"><i class="far fa-check-square"></i> <?php echo ctranslate("Tahsil Edildi")?></td>
					</tr>
			<?php }?>
		<?php }?>
	</table>
	<script type='text/javascript'>
	jQuery(function () {
		jQuery("#discount").keyup(function () {
			// Eski değerleri al
			const old_subtotal = parseFloat(jQuery("#subtotal").data("old")) || 0;
			const old_total = parseFloat(jQuery("#total").data("old")) || 0;
			const old_kdv1 = parseFloat(jQuery("#kdv1").data("old")) || 0;
			const old_kdv8 = parseFloat(jQuery("#kdv8").data("old")) || 0;
			const old_kdv18 = parseFloat(jQuery("#kdv18").data("old")) || 0;
			const old_kdv20 = parseFloat(jQuery("#kdv20").data("old")) || 0;
			const old_kdvdiger = parseFloat(jQuery("#old_kdvdiger").data("old")) || 0;
			// İndirim değerini al ve kontrol et
			const discount = parseFloat(jQuery(this).val()) || 0;
			// İndirim türüne göre alt toplamı hesapla
			let stotal = 0;
			if (jQuery("#discount_type").val() !== "%") {
				stotal = old_subtotal - discount;
			} else {
				stotal = old_subtotal * ((100 - discount) / 100);
			}
			// Toplam değerler
			let total = stotal;
			let gtotal = total;
			// KDV alanlarını döngüyle hesapla
			const kdvFields = [
				{ id: "#kdv1", oldValue: old_kdv1 },
				{ id: "#kdv8", oldValue: old_kdv8 },
				{ id: "#kdv18", oldValue: old_kdv18 },
				{ id: "#kdv20", oldValue: old_kdv20 },
				{ id: "#kdvdiger", oldValue: old_kdvdiger },
			];
			kdvFields.forEach((field) => {
				if (jQuery(field.id).val() != "0.00") {
					const kdv = (total * field.oldValue) / old_subtotal;
					jQuery(field.id).val(kdv.toFixed(2));
					gtotal += kdv;
				}
			});
			// Son toplamı güncelle
			jQuery("#total").val(stotal.toFixed(2));
			jQuery("#gtotal").val(gtotal.toFixed(2));
		});
	});
	</script>
<?php }?>