<?php
	header('Content-Type: text/html; charset=utf-8');
	header("Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, max-age=0, post-check=0");

#	require_once("config/connect.php");
#	require_once("../global_functions.php");
	

	if(isset($_POST['functionKey']) && $_POST['functionKey'] != '') $functionKey=$_POST['functionKey'];

	switch ($functionKey){
		case "addToSplittbon": addToSplittbon(); break;
		case "operateRegisterFunction": operateRegisterFunction(); break;
		case "addToSplittbonArray": addToSplittbonArray(); break;
		case "clearSplitBon": clearSplitBon(); break;
		case "splittBonBill": splittBonBill(); break;

		default: echo "Error !!! Falscher Aufruf von ajax_funktionen, Funktion nicht bekannt";
	}
	
	
	
	function splittBonBill(){
		if(!isset($_SESSION)){session_start();}
		$currentCart=$_SESSION['aktuellerWarenkorb'];
		
		if(isset($_SESSION['splitBon'.$currentCart])){
			$cc=0;
			$xOp="";
			$respMsg="";
			for($i=0; $i < sizeof($_SESSION['splitBon'.$currentCart]); $i++){
				$lineId=$_SESSION['splitBon'.$currentCart][$i][0];
				$openBonId = preg_replace('/\D/', '', $lineId);
				$qtyToBill=$_SESSION['splitBon'.$currentCart][$i][1];
				$rQl=mysql_fetch_assoc(mysql_query("SELECT * FROM offener_bon WHERE ID=$openBonId"));
				$openSplitBonId=900+$currentCart;
				$wQl=mysql_query("INSERT INTO offener_bon (bon_id, produkt_id, REFERENCE, CODE, NAME, PRICEBUY, PRICESELL, CATEGORY, TAXCAT, IMAGE, DISPLAY, VARPRICE, 
											PRINTNAME, NEGATIVEPRICE, PRODUKTORDER, SICHTBAR, stueck, tax_rate, kunden_id)
										SELECT $openSplitBonId, produkt_id, REFERENCE, CODE, NAME, PRICEBUY, PRICESELL, CATEGORY, TAXCAT, IMAGE, DISPLAY, VARPRICE, 
												PRINTNAME, NEGATIVEPRICE, PRODUKTORDER, SICHTBAR, $qtyToBill, tax_rate, kunden_id 
									FROM offener_bon
									WHERE ID=$openBonId");
				if(!$wQl)$respMsg.="Datenbank Copy/Insert Error: ".mysql_error();
				
				$udQl=mysql_query("UPDATE offener_bon SET stueck = (stueck - $qtyToBill) WHERE ID=$openBonId");
				if(!$udQl)$respMsg.="Datenbank Update Error: ".mysql_error();

			}
			
			unset($_SESSION['splitBon'.$currentCart]);
			if($respMsg=='')$respMsg="Success-|-".$openSplitBonId;
		}
		else $respMsg.="Kein Splitbon vorhanden.";
		
		echo $respMsg;
	
	}
	
	// Wenn beim splitten der down oder up button gedrückt wird - 
	function addToSplittbonArray(){		
		if(isset($_POST['originBonLineId']) && $_POST['originBonLineId'] != '') $originBonLineId=$_POST['originBonLineId'];
		if(isset($_POST['splitQty']) && $_POST['splitQty'] != '') $splitQty=$_POST['splitQty'];		
		if(isset($_POST['functionDirection']) && $_POST['functionDirection'] != '') $functionDirection=$_POST['functionDirection'];
		$kpMsg="Qty aus post: ".$splitQty." ";
		if(!isset($_SESSION)){session_start();}
		$currentCart=$_SESSION['aktuellerWarenkorb'];
			$cOp="";
		if(isset($_SESSION['splitBon'.$currentCart])== false){
			$_SESSION['splitBon'.$currentCart]=array();
			$splitLine=array($originBonLineId, $splitQty, '');
			array_push($_SESSION['splitBon'.$currentCart], $splitLine);
		}
		else{	
			$checkIndex=-99;
			for($j=0; $j < sizeof($_SESSION['splitBon'.$currentCart]); $j++){
				$kpMsg.=$_SESSION['splitBon'.$currentCart][$j][0]. " / ".$originBonLineId." ---- ";
				if($originBonLineId==$_SESSION['splitBon'.$currentCart][$j][0])$checkIndex=$j;
			}
			if($checkIndex >=0){
				if($functionDirection=="down")$_SESSION['splitBon'.$currentCart][$checkIndex][1]+=$splitQty;
				if($functionDirection=="up"){
					if(($_SESSION['splitBon'.$currentCart][$checkIndex][1]-$splitQty)>0 )$_SESSION['splitBon'.$currentCart][$checkIndex][1]-=$splitQty;
					else if(($_SESSION['splitBon'.$currentCart][$checkIndex][1]-$splitQty)<=0 ) unset ($_SESSION['splitBon'.$currentCart][$checkIndex]);
					else $_SESSION['splitBon'.$currentCart][$checkIndex][1]-=$splitQty;
				}
			
			}
			else {
				$kpMsg.="array length: ".count($_SESSION['splitBon'.$currentCart]). " / ".$originBonLineId." ---- ";
				$splitLine=array($originBonLineId, $splitQty, '');
				array_push($_SESSION['splitBon'.$currentCart], $splitLine);
			}
		}
		var_dump($_SESSION['splitBon'.$currentCart]);
		#	$checkNo=sizeof($_SESSION['splitBon'.$currentCart]);
		
	}
	
	
	
	function operateRegisterFunction(){
		if(isset($_POST['selectedLineId']) && $_POST['selectedLineId'] != '') $selectedLineId=$_POST['selectedLineId'];
		if(isset($_POST['operateFunction']) && $_POST['operateFunction'] != '') $operateFunction=$_POST['operateFunction'];
		
		if(!isset($_SESSION)){session_start();}
			$currentCart=$_SESSION['aktuellerWarenkorb'];
		if($selectedLineId==true && $operateFunction==true){
				if($operateFunction=='DeleteSplitBonLine'){

					$xop=" DeleteSplitBonLine: ";
					for($i=0; $i < sizeof($_SESSION['splitBon'.$currentCart]); $i++){
						$selectedLineId=str_replace("splittedLine", "split_line", $selectedLineId);
						if($_SESSION['splitBon'.$currentCart][$i][0] == $selectedLineId){								
							$delIndex=$i;	
							$xop.="x-".$_SESSION['splitBon'.$currentCart][$i][0]." -- ";							
						}
						
					
					}
					$lengBefor=sizeof($_SESSION['splitBon'.$currentCart]);
					unset($_SESSION['splitBon'.$currentCart][$delIndex]);
					$_SESSION['splitBon'.$currentCart]=array_values($_SESSION['splitBon'.$currentCart]);
					$lengAfter=sizeof($_SESSION['splitBon'.$currentCart]);
					if($lengAfter==0)unset($_SESSION['splitBon'.$currentCart]);

					$respMsg="".$selectedLineId." / ".$operateFunction." ::: ".$xop." -> ".$lengBefor."/".$lengAfter;

				}
			
				else if($operateFunction=='plus'){

					for($i=0; $i < sizeof($_SESSION['splitBon'.$currentCart]); $i++){
						$selectedLineId=str_replace("splittedLine", "split_line", $selectedLineId);
						if($_SESSION['splitBon'.$currentCart][$i][0] == $selectedLineId){															
							$_SESSION['splitBon'.$currentCart][$i][1]+=1;
						}					
					}
					$respMsg="Anzahl im Splitbon erhöht.";					
				}
				
				else if($operateFunction=='minus'){
					for($i=0; $i < sizeof($_SESSION['splitBon'.$currentCart]); $i++){
						$selectedLineId=str_replace("splittedLine", "split_line", $selectedLineId);
						if($_SESSION['splitBon'.$currentCart][$i][0] == $selectedLineId){															
							$_SESSION['splitBon'.$currentCart][$i][1]-=1;
						}					
					}
					$respMsg="Anzahl im Splitbon reduziert.";	
				}

				else $respMsg="Error -#-Fehler Operation nicht ausgeführt";
				echo $respMsg;
		
		}
		
		
		else echo "Error-#-";
	
	}
	
	function clearSplitBon(){
		if(!isset($_SESSION)){session_start();}
		$currentCart=$_SESSION['aktuellerWarenkorb'];
		if(isset($_SESSION['splitBon'.$currentCart])){
			unset($_SESSION['splitBon'.$currentCart]);
			
			echo "Split Bon zu Bon Id ".$currentCart." gelöscht";
		
		}
		else echo "Split Bon geschlossen";
	
	
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	