<?php
$path="../../";
include("../../map.php");
if($_POST["rb"]==""){$rb=0;}else{$rb = $_POST["rb"];};
if($_POST["s4"]==0){$ps4=0;}else{$ps4=$_POST["s4"];}
if($_POST["s5"]==0){$ps5=1;}else{$ps5=$_POST["s5"];}
	$iarr = array();
	foreach ($_POST as $key => $value){
		if(substr($key,0,1)=="i"){
			$iarr = $iarr + array( $key => $value);
		}
	}
	$rjsi = json_encode($iarr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
	$pb2=($_POST["fufiyat"]*((($_POST["s4"]+100)/100))); //kdv dahil fiyat
//--/Ürün Ekle/---------
if($_POST["s3"]==1){ $ps3=1;}else{$ps3=0;}
if($_GET["durum"]=="uekle"){
	$sqlu = mysqli_query($conn, "select * from ".$_SESSION["lang"]." where (t4='".$_POST["t1"]."' or t5='".$_POST["t1"]."') order by id desc"); $rsu=mysqli_fetch_assoc($sqlu);
	$id=$rsu["id"];
	include("../../../deppo/tools/script/prices.php");
	$sqlc = mysqli_query($conn, "select * from t4 where s1=".$_GET["id"]." and s2=".$rsu["id"].""); $rsc=mysqli_fetch_assoc($sqlc);
	$sqlfc = mysqli_query($conn, "select * from t4 where s1=".$_GET["id"].""); $rsfc=mysqli_fetch_assoc($sqlfc);
	if(mysqli_num_rows($sqlu)==0){	#------------------->//Ürün Yok
		$sql  = $conn->query("insert into t4(t1,t2,t6,t10,ne,ts1,s1,s2,s3,s4,s5,s6,pb1,pb2,n1,n17) values ('".$_GET["islem"]."', '".$_POST["t2"]."', '".$_POST["t6"]."', '".$_POST["t10"]."', 'furun', now(), ".$_GET["id"].", '0', ".$ps3.", ".$ps4.", '".$ps5."', '".$rb."', '".sprice($_POST["price"])."', '".sprice($pb2)."', '".addslashes(stripslashes($_POST["t1"]))."', '".addslashes(stripslashes($rjsi))."')");
	}elseif(mysqli_num_rows($sqlc)<>0 and $rb==""){	#----------->//Ürün Faturada var
		if($_GET["islem"]=="afatura" or $_GET["islem"]=="ifatura"){
			$qty = $rsu["s5"]+$ps5;
			$s5=$rsc["s5"]+$ps5;
			$Rs   = mysqli_query($conn, "update ".$_SESSION["lang"]." set s5='".$qty."', s4='".$_POST["s4"]."', pb3='".sprice($_POST["fufiyat"])."' where id=".$rsu["id"]."");
			$Rs = mysqli_query($conn, "update t4 set s5='".$s5."', n17='".addslashes(stripslashes($rjsi))."' where s1=".$_GET["id"]."");
			//echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok2";
		}else{
			$fiyat=$fiyat1;
			if($f7 == 1){
				if($stok >= $ps5){
					$qty = $rsu["s5"]-$ps5;
					$s5=$rsc["s5"]+$ps5;
					$Rs   = mysqli_query($conn, "update ".$_SESSION["lang"]." set s5='".$qty."', s4='".$_POST["s4"]."' where id=".$rsu["id"]."");	
					$Rs = mysqli_query($conn, "update t4 set s5='".$s5."', n17='".addslashes(stripslashes($rjsi))."' where s1=".$_GET["id"]."");
					//echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok2";
				}else{
					echo "Yeterli Stok Yok [2]";
				}
			}else{
				$stok = $rsu["s5"]-$ps5;
				$s5=$rsc["s5"]+$ps5;
				$Rs = mysqli_query($conn, "update t4 set s5='".$s5."', n17='".addslashes(stripslashes($rjsi))."' where s1=".$_GET["id"]."");
				//echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok2";
			}
		} //end if
	}else{ #--------------------------------------------------------->//Ürün Faturada Yok
		if($_GET["islem"]=="afatura" or $_GET["islem"]=="ifatura"){
			$qty = $rsu["s5"]+$ps5;
			if($f7 == 1){
				$Rs   = mysqli_query($conn, "update ".$_SESSION["lang"]." set s5='".$qty."', s4='".$_POST["s4"]."' where id=".$rsu["id"]."");
			}
			$sql  = $conn->query("insert into t4(t1,t2,t6,t10,ne,ts1,s1,s2,s3,s4,s5,s6,pb1,pb2,n17) values ('".$_GET["islem"]."', '".$_POST["t2"]."', '".$_POST["t6"]."', '".$_POST["t10"]."', 'furun', now(), ".$_GET["id"].", ".$rsu["id"].", ".$ps3.", ".$ps4.", '".$ps5."', '".$rb."', '".sprice($_POST["price"])."', '".sprice($pb2)."', '".addslashes(stripslashes($rjsi))."')");	
			//echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok2";
		}else{
			$fiyat=$fiyat1;
			if($f7 == 1){
				if($stok >= $ps5){
					$qty = $rsu["s5"]-$ps5;
					$Rs   = mysqli_query($conn, "update ".$_SESSION["lang"]." set s5='".$qty."', s4='".$_POST["s4"]."' where id=".$rsu["id"]."");
					$sql  = $conn->query("insert into t4(t1,t2,t6,t10,ne,ts1,s1,s2,s3,s4,s5,pb1,pb2,n17) values ('".$_GET["islem"]."', '".$_POST["t2"]."', '".$_POST["t6"]."', '".$_POST["t10"]."', 'furun', now(), ".$_GET["id"].", ".$rsu["id"].", ".$ps3.", ".$ps4.", '".$ps5."', '".sprice($_POST["price"])."', '".sprice($pb2)."', '".addslashes(stripslashes($rjsi))."')");	
					//echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok2";
				}else{
					echo "Yeterli Stok Yok [2]";
				}
			}else{
					$stok = $rsu["s5"]-$ps5;
					$sql  = $conn->query("insert into t4(t1,t2,t6,t10,ne,ts1,s1,s2,s3,s4,s5,pb1,pb2,n17) values ('".$_GET["islem"]."', '".$_POST["t2"]."', '".$_POST["t6"]."', '".$_POST["t10"]."', 'furun', now(), ".$_GET["id"].", ".$rsu["id"].", ".$ps3.", ".$ps4.", '".$ps5."', '".sprice($_POST["price"])."', '".sprice($pb2)."', '".addslashes(stripslashes($rjsi))."')");	
					//echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok2";
			}
		} //end if
	}
}elseif($_GET["durum"]=="uguncelle"){
	$sqlu = mysqli_query($conn, "select * from ".$_SESSION["lang"]." where id=".$_POST["urunid"]." order by id desc"); $rsu=mysqli_fetch_assoc($sqlu);
	$sql = mysqli_query($conn, "select * from t4 where id=".$_GET["uid"]." order by id desc"); $rs=mysqli_fetch_assoc($sql);
	if($_GET["islem"]=="afatura" or $_GET["islem"]=="ifatura"){
//--/Alış Faturası/-----
//--/Varyasyon İşle/-----
		if($_POST["rb"]<>""){
			$rb=$_POST["rb"];
			$sqlb = mysqli_query($conn, "select * from t1 where ne='variation' and id=".$rb." order by id desc"); $rsb=mysqli_fetch_assoc($sqlb);
			if($_POST["fuadet"]=$rs["s5"]){
				$vadet=$rsb["s5"];
			}else{
				if($_POST["fuadet"] < $rs["s5"]){
					$vfadet=$_POST["fuadet"]-$rs["s5"];
					$vadet=$rs["s5"]+$vfadet;
				}elseif ($_POST["fuadet"] > $rs["s5"]){
					$vfadet=$rs["s5"]-$_POST["fuadet"];
					$vadet=$rs["s5"]-$vfadet;
				}
		} //end if
		$Rs = mysqli_query($conn, "update t1 set s5='".$vadet."' where id=".$_POST["rb"]."");
	}else{
		$rb="0";
	}
	//--/Varyasyon İşle/-----
	if ($_POST["fuadet"]==$rs["s5"]){
		$adet=$rs["s5"];
	}else{   
		if($_POST["fuadet"] < $rs["s5"]){
			$fadet=$_POST["fuadet"]-$rs["s5"];
			$adet=$rsu["s5"]+$fadet;
		}elseif($_POST["fuadet"] > $rs["s5"]){
			$fadet=$rs["s5"]-$_POST["fuadet"];
			$adet=$rsu["s5"]-$fadet;
		}
	}
	$Rs = mysqli_query($conn, "update ".$_SESSION["lang"]." set s5='".$adet."', pb3='".sprice($_POST["fufiyat"])."', s4='".$_POST["s4"]."' where id=".$_POST["urunid"]."");
	$Rs = mysqli_query($conn, "update t4 set s5='".$_POST["fuadet"]."', pb1='".sprice($_POST["fufiyat"])."', pb2='".sprice($pb2)."', s3='".$ps3."', s4='".$ps4."', s6='".$rb."', t10='".$_POST["t10"]."', t2='".$_POST["t2"]."', t6='".$_POST["t6"]."', n1='".addslashes(stripslashes($_POST["t1"]))."', n17='".addslashes(stripslashes($rjsi))."' where id=".$_GET["uid"]."");
	//--/Alış Faturası/-----
}elseif($_GET["islem"]=="sfatura"){
	//--/Varyasyon İşle/-----
	if($_POST["rb"]<>""){
		$rb=$_POST["rb"];
		$sqlb = mysqli_query($conn, "select * from t1 where ne='variation' and id=".$rb." order by id desc"); $rsb=mysqli_fetch_assoc($sqlb);
		if($_POST["fuadet"]==$rs["s5"]){
			$vadet=$rsb["s5"];
		}else{
			if($_POST["fuadet"] < $rs["s5"]){
				$vfadet=$_POST["fuadet"]-$rs["s5"];
				$vadet=$rsb["s5"]-$fadet;
			}elseif($_POST["fuadet"] > $rs["s5"]){
				$vfadet=$rs["s5"]-$_POST["fuadet"];
				$vadet=$rsb["s5"]+$vfadet;
				if($vadet < 0){ echo location("?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&uid=".$_GET["uid"]."&id=".$_GET["id"]."&durum=hata2",self,500,1); }
				//--/Stok Kontrol/---
			} //end if
		} //end if
		$Rs = mysqli_query($conn, "update t1 set s5='".$vadet."' where id=".$_POST["rb"]."");
	}else{
		$rb="0";
	} //end if
	//--/Varyasyon İşle/-----
	//--/Satış Faturası/-----
	if($_POST["fuadet"]==$rs["s5"]){
		$adet=$rsu["s5"];
	}else{  
		if($_POST["fuadet"] < $rs["s5"]){
			$fadet=$_POST["fuadet"]-$rs["s5"];
			$adet=$rsu["s5"]-$fadet;
		}elseif($_POST["fuadet"] > $rs["s5"]){
			$fadet=$rs["s5"]-$_POST["fuadet"];
			$adet=$rsu["s5"]+$fadet;
			if($adet < 0){ echo location("?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&uid=".$_GET["uid"]."&id=".$_GET["id"]."&durum=hata2",self,500,1); }
		//--------------/Stok Kontrol/---
		}else{
			$fadet=$_POST["fuadet"];
			$adet=$rsu["s5"]-$fadet;
			if($adet < 0){ echo location("?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&uid=".$_GET["uid"]."&id=".$_GET["id"]."&durum=hata2",self,500,1); }
		} //end if
	} //end if
	$Rs = mysqli_query($conn, "update ".$_SESSION["lang"]." set s5='".$adet."', s4='".$ps4."' where id=".$_POST["urunid"]."");
	$Rs = mysqli_query($conn, "update t4 set t2='".$_POST["t2"]."', s5='".$_POST["fuadet"]."', pb1='".sprice($_POST["fufiyat"])."', pb2='".sprice($pb2)."', s3='".$ps3."', s4='".$ps4."', s6='".$rb."', n1='".addslashes(stripslashes($_POST["t1"]))."', n17='".addslashes(stripslashes($rjsi))."' where id=".$_GET["uid"]."");
//--/Satış Faturası/-----
}
	//echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&uid=".$_GET["uid"]."&durum=ok2";
//--/Ürün Güncelle/-----
//--/Stok İade/---------
}elseif($_GET["durum"]=="usil"){
	$sqlfu = mysqli_query($conn, "select * from t4 where id=".$_GET["uid"]." order by id desc"); $rsfu=mysqli_fetch_assoc($sqlfu);
	$sqlu = mysqli_query($conn, "select * from ".$_SESSION["lang"]." where id=".$_GET["urunid"]." order by id desc"); $rsu=mysqli_fetch_assoc($sqlu); //Ürün Adet
	if($_GET["islem"]=="afatura" or $_GET["islem"]=="ifatura"){
		$istock=$rsu["s5"]-$rsfu["s5"];
		$Rs = mysqli_query($conn, "update ".$_SESSION["lang"]." set s5='".$istock."' where id=".$_GET["urunid"]."");
		//--/Varyasyon iade/----
		if($rsfu["s6"]<>"0"){
			$vstock=$rsb["s5"]-$rsfu["s5"];
			$sqlb = mysqli_query($conn, "select * from t1 where ne='variation' and id=".$rsfu["s6"]." order by id desc"); $rsb=mysqli_fetch_assoc($sqlb);
			$Rs = mysqli_query($conn, "update t1 set s5='".$vstock."' where id=".$rsfu["s6"]."");
		} //end if
		//--/Varyasyon iade/----
	}elseif($_GET["islem"]=="sfatura"){
		//--/Varyasyon iade/----
		if($rsfu["s6"]<>"0"){
			$vstock=$rsb["s5"]+$rsfu["s5"];
			$sqlb = mysqli_query($conn, "select * from t1 where id=".$rsfu["s6"]." order by id desc"); $rsb=mysqli_fetch_assoc($sqlb);
			$Rs = mysqli_query($conn, "update t1 set s5='".$vstock."' where id=".$rsfu["s6"]."");
		} //end if
		//--/Varyasyon iade/----
		//--/Stok İade/---------
		$stock=$rsu["s5"]+$rsfu["s5"];
		$Rs = mysqli_query($conn, "update ".$_SESSION["lang"]." set s5='".$stock."' where id=".$_GET["urunid"]."");
		//--/Stok İade/---------
	} //end if
	$sql = mysqli_query($conn, "delete from t1 where s2=".$_GET["id"]."");
	$sql = mysqli_query($conn, "delete from t4 where id=".$_GET["uid"]."");
	//echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok2";
}
?>