<?php session_start();

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;
}
}

$gettotals	=	"0";

//echo "dave22";

	include("configi.php");

//	echo "job fields" . stripslashes($_POST['jobfields']);
	
	// Test if our data came through
if (isset($_POST["jobfields"])) {

//echo "data came through";
    // Decode our JSON into PHP objects we can use

//    $jobfields = json_decode($_POST['jobfields']);
	   $jobfields = json_decode(stripslashes($_POST['jobfields']));
	   
//	   echo "jobfields is" . json_decode(stripslashes($_POST['jobfields']));
	   
	   
    // Access our object's data and array values.
    //echo "sales_id is: " . $jobfields->sales_id . "<br>";
   // echo "field_name is: " . $jobfields->field_name . "<br>";
   // echo "field_type is: " . $jobfields->field_type . "<br>";
   // echo "field_name1_db is: " . $jobfields->field_name1_db . "<br>";
   // echo "field_name2_db is: " . $jobfields->field_name2_db . "<br>";
   // echo "field_value1 is: " . $jobfields->field_value1 . "<br>";
   // echo "field_value2 is: " . $jobfields->field_value2 . "<br>";
		
}else{
	echo "data did not come through";
}


//echo $jobfields;

$outlet_id = 1;

$uid_client					=	$jobfields->uid_client;
$uid_r						=	$jobfields->uid_r;
$current_function			=	$jobfields->current_function;

$result_message 			= 	"";


if ($current_function == "delcustrecurr"){

		$gettotals	=	"1";	
		
		$new_line_amount = 0;
		$new_item_amount = 0;
		
	//	echo $field_value1;

		$querydelete = "delete from ".$_COOKIE['phxcrmclient_id']."_client_recurr where uid_r = " . $uid_r;
	
	//	echo $querydelete;

		$result=mysqli_query($conn, $querydelete);

		if($result){ 
			$result_message = ""; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error();
		}
		
//		echo $result_message;
	//	
		//exit;
		
}else if ($current_function == "addcustrecurr"){
	
	//get dept name

$queryac2ddc="SELECT * FROM " . $_COOKIE['phxcrmclient_id'] . "_code_lookup WHERE code_pk = " . $cat_id;
//echo $queryac2;
$resultac2ddc=mysqli_query($conn, $queryac2ddc);

 while($rowac2ddc=mysqli_fetch_array($resultac2ddc)) { 
 
 
	$cat_name			 = $rowac2ddc['code_value']; 
	
	$cat_name = GetSQLValueString($cat_name, "text");
	
 }



			


	//	echo $field_value1;

		$queryinsert = "insert into ".$_COOKIE['phxcrmclient_id']."_client_category (cc_uid_client, cc_category) values(".$uid_client.",".$cat_name.")";
	
	//	echo $queryinsert;

		$result=mysqli_query($conn, $queryinsert);

		if($result){ 
			$result_message = ""; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error();
		}
		
//		echo $result_message;
	//	
		//exit;
	

}


//update category totals


	

		
	if ($result_message == ""){
	
		$result_message = "successful";
		
	}
	
	

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

//		echo $outputarray;
		

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

?>