	<?php
	
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  
  	include("configi.php");


  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conn, $theValue) : mysqli_escape_string($conn, $theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

	 include 'configi.php';	

	$phx_result = "";
	$new_line_amount = 0;

	
	$client_id 				=  $_REQUEST['client_id'];
	$cart_function			=  $_REQUEST['cart_function'];
	$cart_uid				=  $_REQUEST['cart_uid'];
	$newqty					=  $_REQUEST['newqty'];



	$new_qty_supp			= 0;
	$new_qty_bo				= 0;
	$new_qty_bo_pay			= 0;
	
	$result = '';
	
	$cart_id	= $_REQUEST['cart_id'];
	
	$new_qty = 0;
	
	 include 'configi.php';

	$phxcrmuserid = $_COOKIE['phxcrmuserid'];
	$phxclientid = $_COOKIE['phxcrmclient_id'];

	//check stock mgmt level
	
	
	$queryphxu="SELECT * from phx_userid where idphx_userid = " . $phxclientid;
//	$result = $result . $queryclient;
	$resultphxu=mysqli_query($conn, $queryphxu);

	while($rowphxu=mysqli_fetch_array($resultphxu)) { 

		$comp_stock_type	 				= $rowphxu['comp_stock_type'];

	
	}
	
//	echo "comp_stock_type".$comp_stock_type;
	
//echo $queryphxu;

//	$queryclient="SELECT * from ".$_COOKIE['phxcrmclient_id']."_staff where uid_staff = " . $phxcrmuserid;
//	$result = $result . $queryclient;
//	$resultclient=mysqli_query($conn, $queryclient);

//	while($rowclient=mysqli_fetch_array($resultclient)) { 

	//	$cart_id			 				= $rowclient['staff_sales_id'];

	
//	}
	
	
	
	
	if ($cart_function == "delallitems"){



		$querydelall="delete from ".$_COOKIE['phxcrmclient_id']."_stl where st_id = " . $cart_id;
	
		//	echo $queryinsert;
		$resultdelall=mysqli_query($conn, $querydelall);

		
		$phx_result = "";
	

	}else if ($cart_function == "updtitle"){	
			$new_value					=  $_REQUEST['new_value'];

		$field_value1	=	GetSQLValueString($new_value, "text");
		$querydelall="update ".$_COOKIE['phxcrmclient_id']."_stl set stl_title = ".$field_value1." where id_stl = " . $cart_uid;
	
		//	echo $queryinsert;
		$resultdelall=mysqli_query($conn, $querydelall);

		
		$phx_result = "";
		$outputarray = array('phx_result' => $phx_result);

//		echo $outputarray;
		

		$output2 = json_encode($outputarray);
		
		echo $output2;
		
		exit;
		
		
	
	}else if ($cart_function == "delitem"){



		$querydelitem="delete from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;
	
		//	echo $queryinsert;
		$resultdelitem=mysqli_query($conn, $querydelitem);
		
		$new_line_amount			 				= 0;
		$new_item_amount			 				= 0;

		
		$phx_result = "";		
	
	}else if ($cart_function == "addqty"){
		
		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty = qty + 1 where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
			
			$querystlq="SELECT qty, prodserv_uid from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

			$resultstlq=mysqli_query($conn, $querystlq);

			while($rowstlq=mysqli_fetch_array($resultstlq)) { 

				$qtyu			 				= $rowstlq['qty'];
				$prodserv_uidu			 		= $rowstlq['prodserv_uid'];
				
	
			}
			
			$new_qty = $qtyu;
			
			//get latest qty_on_hand
			
			$queryprods="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_prod_search where sel_uid = ".$prodserv_uidu;
				//	echo $queryprod;
			$resultprods=mysqli_query($conn, $queryprods);

			while($rowprods=mysqli_fetch_array($resultprods)) { 

				$qty_on_handu	 				= $rowprods['qty_on_hand'];
		
			}
			
			$qty_suppu = 0;
			$qty_bou = 0;
			
			//work out qty_supp and qty_bo and update
					
			if ($qty_on_handu >= $qtyu){

				$qty_suppu = $qtyu;		
				$qty_bou = 0;
				
			}else if ($qty_on_handu <=0){

			$qty_suppu = 0;		
			$qty_bou = $qtyu;
			
			}else {

				$qty_suppu = $qty_on_handu;
				$qty_bou = $qtyu-$qty_on_handu;
			
			}
			
			$qty_bo_payu = $qty_bou;

			$queryupdatesuppu="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_supp = ".$qty_suppu.", qty_bo=".$qty_bou.", qty_bo_pay=".$qty_bou." where id_stl = " . $cart_uid;
	
			//	echo $queryupdatelinetotal;
			$resultupdatesuppu=mysqli_query($conn, $queryupdatesuppu);
			
			
		
		
		$qty_topay = $qty_suppu + $qty_bo_payu;


		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bou;

		$phx_result = "";

	
	}else if ($cart_function == "removedelivery"){

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_st set postage = 0 where st_id = " . $cart_id;
	
		//	echo $queryupdateqty;
		$resultupdateqty=mysqli_query($conn, $queryupdateqty);

		
		$phx_result = "";
		
		
			}else if ($cart_function == "newqtysupp"){

		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_supp = ".$newqty." where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
				$qty_suppu	 					= 0;
				$qty_bou	 					= 0;
				$qty_bo_payu	 				= 0;

			//get latest qty_on_hand
			
			$querystl="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = ".$cart_uid;
				//	echo $queryprod;
			$resultstl=mysqli_query($conn, $querystl);

			while($rowstl=mysqli_fetch_array($resultstl)) { 

				$qty_suppu	 					= $rowstl['qty_supp'];
				$qty_bou	 					= $rowstl['qty_bo'];
				$qty_bo_payu	 				= $rowstl['qty_bo_pay'];
		
			}
				
		$qty_topay = $qty_suppu + $qty_bo_payu;


		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
				
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bou;

		$phx_result = "";

	

			
	}else if ($cart_function == "addqtysupp"){

		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_supp = qty_supp + 1 where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
				$qty_suppu	 					= 0;
				$qty_bou	 					= 0;
				$qty_bo_payu	 				= 0;

			//get latest qty_on_hand
			
			$querystl="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = ".$cart_uid;
				//	echo $queryprod;
			$resultstl=mysqli_query($conn, $querystl);

			while($rowstl=mysqli_fetch_array($resultstl)) { 

				$qty_suppu	 					= $rowstl['qty_supp'];
				$qty_bou	 					= $rowstl['qty_bo'];
				$qty_bo_payu	 				= $rowstl['qty_bo_pay'];
		
			}
				
		$qty_topay = $qty_suppu + $qty_bo_payu;


		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
				
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bou;

		$phx_result = "";

	


	
	}else if ($cart_function == "subqtysupp"){

		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_supp = qty_supp - 1 where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
				$qty_suppu	 					= 0;
				$qty_bou	 					= 0;
				$qty_bo_payu	 				= 0;
								

			//get latest qty_on_hand
			
			$querystl="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = ".$cart_uid;
				//	echo $queryprod;
			$resultstl=mysqli_query($conn, $querystl);

			while($rowstl=mysqli_fetch_array($resultstl)) { 
			
				$qty_suppu	 					= $rowstl['qty_supp'];
				$qty_bou	 					= $rowstl['qty_bo'];
				$qty_bo_payu	 				= $rowstl['qty_bo_pay'];
		
			}
				
		$qty_topay = $qty_suppu + $qty_bo_payu;
		
		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
				
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bou;

		$phx_result = "";

	

	
	}else if ($cart_function == "addqtybo"){

	//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_bo = qty_bo + 1, qty_bo_pay = qty_bo_pay + 1 where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
				$qty_suppu	 					= 0;
				$qty_bou	 					= 0;
				$qty_bo_payu	 				= 0;
								

			//get latest qty_on_hand
			
			$querystl="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = ".$cart_uid;
				//	echo $queryprod;
			$resultstl=mysqli_query($conn, $querystl);

			while($rowstl=mysqli_fetch_array($resultstl)) { 
			
				$qty_suppu	 					= $rowstl['qty_supp'];
				$qty_bou	 					= $rowstl['qty_bo'];
				$qty_bo_payu	 				= $rowstl['qty_bo_pay'];
		
			}
				
		$qty_topay = $qty_suppu + $qty_bo_payu;
		
		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
				
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bo_payu;

		$phx_result = "";


	
	}else if ($cart_function == "newqtybopay"){

		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_bo_pay = ".$newqty." where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
				$qty_suppu	 					= 0;
				$qty_bou	 					= 0;
				$qty_bo_payu	 				= 0;
								

			//get latest qty_on_hand
			
			$querystl="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = ".$cart_uid;
				//	echo $queryprod;
			$resultstl=mysqli_query($conn, $querystl);

			while($rowstl=mysqli_fetch_array($resultstl)) { 
			
				$qty_suppu	 					= $rowstl['qty_supp'];
				$qty_bou	 					= $rowstl['qty_bo'];
				$qty_bo_payu	 				= $rowstl['qty_bo_pay'];
		
			}
				
		$qty_topay = $qty_suppu + $qty_bo_payu;
		
		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
				
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bo_payu;

		$phx_result = "";

		

	}else if ($cart_function == "addqtybopay"){

		//add 1 to qty and commit

		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_bo_pay = qty_bo_pay + 1 where id_stl = " . $cart_uid;


		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
				$qty_suppu	 					= 0;
				$qty_bou	 					= 0;
				$qty_bo_payu	 				= 0;
								

			//get latest qty_on_hand
			
			$querystl="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = ".$cart_uid;
				//	echo $queryprod;
			$resultstl=mysqli_query($conn, $querystl);

			while($rowstl=mysqli_fetch_array($resultstl)) { 
			
				$qty_suppu	 					= $rowstl['qty_supp'];
				$qty_bou	 					= $rowstl['qty_bo'];
				$qty_bo_payu	 				= $rowstl['qty_bo_pay'];
		
			}
				
		$qty_topay = $qty_suppu + $qty_bo_payu;
		
		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
				
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bo_payu;

		$phx_result = "";

		

	}else if ($cart_function == "subqtybopay"){

		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_bo_pay = qty_bo_pay - 1 where id_stl = " . $cart_uid;


		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
				$qty_suppu	 					= 0;
				$qty_bou	 					= 0;
				$qty_bo_payu	 				= 0;
								

			//get latest qty_on_hand
			
			$querystl="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = ".$cart_uid;
				//	echo $queryprod;
			$resultstl=mysqli_query($conn, $querystl);

			while($rowstl=mysqli_fetch_array($resultstl)) { 
			
				$qty_suppu	 					= $rowstl['qty_supp'];
				$qty_bou	 					= $rowstl['qty_bo'];
				$qty_bo_payu	 				= $rowstl['qty_bo_pay'];
		
			}
				
		$qty_topay = $qty_suppu + $qty_bo_payu;
		
		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
				
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bo_payu;

		$phx_result = "";


		
	}else if ($cart_function == "subqtybo"){

		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_bo = qty_bo - 1, qty_bo_pay = qty_bo_pay - 1 where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
				$qty_suppu	 					= 0;
				$qty_bou	 					= 0;
				$qty_bo_payu	 				= 0;
								

			//get latest qty_on_hand
			
			$querystl="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = ".$cart_uid;
				//	echo $queryprod;
			$resultstl=mysqli_query($conn, $querystl);

			while($rowstl=mysqli_fetch_array($resultstl)) { 
			
				$qty_suppu	 					= $rowstl['qty_supp'];
				$qty_bou	 					= $rowstl['qty_bo'];
				$qty_bo_payu	 				= $rowstl['qty_bo_pay'];
		
			}
				
		$qty_topay = $qty_suppu + $qty_bo_payu;
		
		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
				
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bo_payu;

		$phx_result = "";

	
	
	}else if ($cart_function == "subqty"){

		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty = qty - 1 where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
			
			$qtyu = 0;
			
			$querystlq="SELECT qty, prodserv_uid from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

			$resultstlq=mysqli_query($conn, $querystlq);

			while($rowstlq=mysqli_fetch_array($resultstlq)) { 

				$qtyu			 				= $rowstlq['qty'];
				$prodserv_uidu			 		= $rowstlq['prodserv_uid'];
				
	
			}
			
			$new_qty = $qtyu;
			
			//get latest qty_on_hand
			
			$queryprods="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_prod_search where sel_uid = ".$prodserv_uidu;
				//	echo $queryprod;
			$resultprods=mysqli_query($conn, $queryprods);

			while($rowprods=mysqli_fetch_array($resultprods)) { 

				$qty_on_handu	 				= $rowprods['qty_on_hand'];
		
			}
			
			$qty_suppu = 0;
			$qty_bou = 0;
			
			//work out qty_supp and qty_bo and update
					
			if ($qty_on_handu >= $qtyu){

				$qty_suppu = $qtyu;		
				$qty_bou = 0;
							
			}else if ($qty_on_handu <=0){

			$qty_suppu = 0;		
			$qty_bou = $qtyu;
			
			}else {

				$qty_suppu = $qty_on_handu;
				$qty_bou = $qtyu-$qty_on_handu;
			
			}
			
			$qty_bo_payu = $qty_bou;

			$queryupdatesuppu="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_supp = ".$qty_suppu.", qty_bo=".$qty_bou.", qty_bo_pay=".$qty_bou." where id_stl = " . $cart_uid;
	
			//	echo $queryupdatelinetotal;
			$resultupdatesuppu=mysqli_query($conn, $queryupdatesuppu);
			
			
		
		
		$qty_topay = $qty_suppu + $qty_bo_payu;


		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bou;

		$phx_result = "";

	
		
	}else if ($cart_function == "newdisc"){

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set line_disc = ".$newqty." where id_stl = " . $cart_uid;
	
		//	echo $queryupdateqty;
		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if ($newqty == 0){

			$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = qty * item_amount where id_stl = " . $cart_uid;
	
			//	echo $queryupdatelinetotal;
			$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);

			
		}else{
			

			$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = (item_amount*qty)-(item_amount /100*line_disc)*qty  where id_stl = " . $cart_uid;
	
			//	echo $queryupdatelinetotal;
			$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
			
		}

		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

		$new_line_amount			 				= $rowttl['line_amount'];
		$new_item_amount			 				= $rowttl['item_amount'];
	
		}
		
		$phx_result = "";
		
	}else if ($cart_function == "newqty"){
		
				//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty = ".$newqty." where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
			
			$querystlq="SELECT qty, prodserv_uid from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

			$resultstlq=mysqli_query($conn, $querystlq);

			while($rowstlq=mysqli_fetch_array($resultstlq)) { 

				$qtyu			 				= $rowstlq['qty'];
				$prodserv_uidu			 		= $rowstlq['prodserv_uid'];
				
	
			}
			
			$new_qty = $qtyu;
			

			
			//get latest qty_on_hand
			
			$queryprods="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_prod_search where sel_uid = ".$prodserv_uidu;
				//	echo $queryprod;
			$resultprods=mysqli_query($conn, $queryprods);

			while($rowprods=mysqli_fetch_array($resultprods)) { 

				$qty_on_handu	 				= $rowprods['qty_on_hand'];
		
			}
			
			$qty_suppu = 0;
			$qty_bou = 0;
			
			//work out qty_supp and qty_bo and update
			
			$qty_on_handu = 0;
					
			if ($qty_on_handu >= $qtyu){

				$qty_suppu = $qtyu;		
				$qty_bou = 0;
			
			}else {

				$qty_suppu = $qty_on_handu;
				$qty_bou = $qtyu-$qty_on_handu;
			
			}
			
			$qty_bo_payu = $qty_bou;

			$queryupdatesuppu="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_supp = ".$qty_suppu.", qty_bo=".$qty_bou.", qty_bo_pay=".$qty_bou." where id_stl = " . $cart_uid;
	
			//	echo $queryupdatelinetotal;
			$resultupdatesuppu=mysqli_query($conn, $queryupdatesuppu);
			
			
		
		
		$qty_topay = $qty_suppu + $qty_bo_payu;


		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bou;

		$phx_result = "";

	
		

		
		
	}else if ($cart_function == "newitemvalue"){

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set item_amount = ".$newqty." where id_stl = " . $cart_uid;
	
		//	echo $queryupdateqty;
		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		//go get discount and quantity
		
				
		$querydq="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultdq=mysqli_query($conn, $querydq);

		while($rowdq=mysqli_fetch_array($resultdq)) { 

			$get_qty			 				= $rowdq['qty'];
			$get_line_disc			 			= $rowdq['line_disc'];
	
		}


		if ($get_qty == 0){

			$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = qty * item_amount where id_stl = " . $cart_uid;
	
			//	echo $queryupdatelinetotal;
			$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);

			
		}else{
			

			$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = (item_amount*qty)-(item_amount /100*line_disc)*qty  where id_stl = " . $cart_uid;
	
			//	echo $queryupdatelinetotal;
			$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
			
		}


		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
		$phx_result = "";
		
	}else if ($cart_function == "newqtybo"){
		
		//add 1 to qty and commit

		$queryupdateqty="update ".$_COOKIE['phxcrmclient_id']."_stl set qty_bo = ".$newqty.", qty_bo_pay = ".$newqty." where id_stl = " . $cart_uid;

		$resultupdateqty=mysqli_query($conn, $queryupdateqty);
		
		if (!mysqli_commit($conn)) {
			echo "Commit transaction failed";
			exit();
		}

		//advanced stock mgmt

			//get new qty
				$qty_suppu	 					= 0;
				$qty_bou	 					= 0;
				$qty_bo_payu	 				= 0;
								

			//get latest qty_on_hand
			
			$querystl="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = ".$cart_uid;
				//	echo $queryprod;
			$resultstl=mysqli_query($conn, $querystl);

			while($rowstl=mysqli_fetch_array($resultstl)) { 
			
				$qty_suppu	 					= $rowstl['qty_supp'];
				$qty_bou	 					= $rowstl['qty_bo'];
				$qty_bo_payu	 				= $rowstl['qty_bo_pay'];
		
			}
				
		$qty_topay = $qty_suppu + $qty_bo_payu;
		
		$queryupdatelinetotal="update ".$_COOKIE['phxcrmclient_id']."_stl set line_amount = ".$qty_topay." * item_amount where id_stl = " . $cart_uid;
	
		//	echo $queryupdatelinetotal;
		$resultupdatelinetotal=mysqli_query($conn, $queryupdatelinetotal);
		
		
		// get latest total line and item_amount
		
		$queryttl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where id_stl = " . $cart_uid;

		$resultttl=mysqli_query($conn, $queryttl);

		while($rowttl=mysqli_fetch_array($resultttl)) { 

			$new_line_amount			 				= $rowttl['line_amount'];
			$new_item_amount			 				= $rowttl['item_amount'];
		}
		
				
		$new_qty_supp			= $qty_suppu;
		$new_qty_bo				= $qty_bou;
		$new_qty_bo_pay			= $qty_bo_payu;

		$phx_result = "";


	}
	
		
		
		//recalc totals for order
		
		$postage = 0;
		
	//get postage for cart
	
		$querypostage="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_st where st_id = ".$cart_id;
	//	echo $querypostage;
		$resultpostage=mysqli_query($conn, $querypostage);

		while($rowpostage=mysqli_fetch_array($resultpostage)) { 

			$postage		 		=	$rowpostage['postage'];

				
		}

	//get cart line totals
	
	//get postage for cart
	
		$querypostage="SELECT * FROM ".$_COOKIE['phxcrmclient_id']."_st where st_id = ".$cart_id;
	//	echo $querypostage;
		$resultpostage=mysqli_query($conn, $querypostage);

		while($rowpostage=mysqli_fetch_array($resultpostage)) { 

			$postage		 		=	$rowpostage['postage'];
				
		}

	//get cart line totals
	
	$total_sale = 0;
	$total_xtax = 0;
	$total_tax = 0;
	

	//calculate and udate totals for order
	
		//	$st_id			= $rowbr['st_id'];


		$querystl="SELECT * from ".$_COOKIE['phxcrmclient_id']."_stl where inv_id = ".$cart_id;
		
//		echo $querystl;
		
			$found = "no";
		
	//	echo $querystl;
		$resultstl=mysqli_query($conn, $querystl);
	
		$ttl_job = 0;
	

		while($querystl=mysqli_fetch_array($resultstl)) { 
		

			
			$line_amount = $querystl['line_amount'];

		//	echo "line amount".$line_amount;
		
			$ttl_job = $ttl_job + $line_amount;
			
			
			
	 
		}
		
	//	echo "ttl job is".$ttl_job;
	
	if ($ttl_job == 0){
	
		$job_ttl_quote_xgst	 = 0;
		$job_ttl_quote_gst	 = 0;
		$job_ttl_quote	 = 0;


		
	}else{

		$job_ttl_quote_xgst	 = $ttl_job;
		$job_ttl_quote_gst	 = $ttl_job/100*15;
		$job_ttl_quote	 = $job_ttl_quote_xgst + $job_ttl_quote_gst;
		
		
	}
		
	$updst = "update ".$_COOKIE['phxcrmclient_id']."_st set job_ttl_quote_xgst = ".$job_ttl_quote_xgst.",job_ttl_quote_gst = ".$job_ttl_quote_gst.",job_ttl_quote = ".$job_ttl_quote. " where st_id = ".$cart_id;
	
	
//	echo "<br>";
	//echo $updst;
	
	$resultst=mysqli_query($conn, $updst);
	
	

		
	$outputarray = array('phx_result' => $phx_result, 'new_line_amount' => $new_line_amount, 'new_item_amount' => $new_item_amount, 'sales_id' => $cart_id, 'new_qty_supp' => $new_qty_supp, 'new_qty_bo' => $new_qty_bo, 'new_qty_bo_pay' => $new_qty_bo_pay, 'new_qty' => $new_qty,'comp_stock_type' => $comp_stock_type);

//		echo $outputarray;
		

	$output2 = json_encode($outputarray);
	
	echo $output2;

	exit;
	
			
			?>