<?php
$path="../../";
include("../../map.php");
//--/Ürün Ekle/---------
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);
	if(mysqli_num_rows($sqlu)==0){
		echo "Ürün Bulunamadı";
	}else{
		$id=$rsu["id"];
		include("../../../deppo/tools/script/prices.php");
		if($_POST["s5"]==0){$ps5=1;}else{$ps5=$_POST["s5"];}
			$fiyat=$fiyat1;
				$stok = $rsu["s5"]-$ps5;
				$sql  = $conn->query("insert into t4(t1,ne,ts1,s1,s2,s4,s5,pb1) values ('offer', 'furun', '".$ctarih."', ".$_GET["id"].", ".$rsu["id"].", ".$rsu["s4"].", '".$ps5."', '".$fiyat."')");	
				//$Rs   = mysqli_query($conn, "update ".$_SESSION["lang"]." set s5='".$stok."' where id=".$rsu["id"]."");
				echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok";
	}
//--/Ürün Ekle/---------
//--/Ürün Güncelle/-----
}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);
	$Rs = mysqli_query($conn, "update t4 set s5='".$_POST["fuadet"]."', pb1='".$_POST["fufiyat"]."', s4='".$_POST["s4"]."', s6='".$rb."' where id=".$_GET["uid"]."");
//--/Alış Faturası/-----
}elseif($_GET["islem"]=="sfatura"){
//--/Varyasyon İşle/-----
	if($_POST["rb"]<>""){
		$rb=$_POST["rb"];
		$sqlb = mysqli_query($conn, "select * from varyasyon where 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 varyasyon 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."' where id=".$_POST["urunid"]."");
	$Rs = mysqli_query($conn, "update t4 set s5='".$_POST["fuadet"]."', pb1='".$_POST["fufiyat"]."', s4='".$_POST["s4"]."', s6='".$rb."' where id=".$_GET["uid"]."");
//--/Satış Faturası/-----
}
echo "url:?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&uid=".$_GET["uid"]."&durum=ok";
//--/Ü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 varyasyon where id=".$rsfu["s6"]." order by id desc"); $rsb=mysqli_fetch_assoc($sqlb);
			$Rs = mysqli_query($conn, "update varyasyon 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 varyasyon where id=".$rsfu["s6"]." order by id desc"); $rsb=mysqli_fetch_assoc($sqlb);
		$Rs = mysqli_query($conn, "update varyasyon 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=ok";
}
?>