<?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 "text22":
      $theValue = ($theValue != "") ? $theValue : "NULL";
      break; 
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : 0;
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

//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']));
	   
    // 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";
}

//$c_favcmts 				=  GetSQLValueString($_POST['c_favcmts'], "text");
//$c_popup 				=  GetSQLValueString($_POST['c_popup'], "text");
	
//echo $jobfields;

$outlet_id = 1;

$uid_client		=	$jobfields->uid_client;
$field_name		=	$jobfields->field_name;
$field_type		=	$jobfields->field_type;
$field_name1_db	=	$jobfields->field_name1_db;
$field_name2_db	=	$jobfields->field_name2_db;	
$field_value1	=	$jobfields->field_value1;
$field_value2	=	$jobfields->field_value2;
$old_status		=	$jobfields->old_status;
$outlet_id		=	$jobfields->outlet_id;
$uid_r			=	$jobfields->uid_r;

//$phxcrmclient_id	=	'94';

//echo "field name is " . $field_name;
//$field_name1_db = 'c_acctaddr2';
//echo "dave".$field_type."client".$uid_client;
//$uid_client = 100;


if ($field_type == "inputcheck"){
			
		$field_value1	=	GetSQLValueString($field_value1, "text");

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client set ".$field_name1_db."=".$field_value1." where uid_client = ".$uid_client;
	
	//	echo $queryupdate;

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

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$return_array = array('result_message' =>$result_message);

		$return_json  = json_encode($return_array);

		echo $return_json;

		
		exit;

}else if ($field_name == "r_unit"){
	
	// echo "r_unit";
			
		$field_value1	=	GetSQLValueString($field_value1, "double");
		
		
		//get current qty
		
		$r_qty = 1;
			
		$queryaqty="SELECT * FROM " . $_COOKIE['phxcrmclient_id'] . "_client_recurr WHERE uid_r = " . $uid_r;

		//echo $queryac2r;
		$resultaqty=mysqli_query($conn, $queryaqty);

		while($rowaqty=mysqli_fetch_array($resultaqty)) { 
 
			$r_qty				 = $rowaqty['r_qty']; 
	
		}
		
		
		$r_line_amt = $field_value1 * $r_qty;
	
		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client_recurr set r_line_amt = ".$r_line_amt.",".$field_name1_db."=".$field_value1." where uid_r = ".$uid_r;

	//	echo $queryupdate;
		
		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$r_line_amt = number_format($r_line_amt, 2, '.', ',');
		$r_unit = number_format($field_value1, 2, '.', ',');
		
		$return_array = array('result_message' =>$result_message,'r_line_amt' => $r_line_amt,'r_qty' => $r_qty,'r_unit' => $r_unit);

		$return_json  = json_encode($return_array);

		echo $return_json;

		
		exit;



}else if ($field_name == "r_qty"){
	
//	echo "r_qty";
			
		$field_value1	=	GetSQLValueString($field_value1, "double");
		
		
		//get current qty
		
		$r_unit = 0;
			
		$queryaqty="SELECT * FROM " . $_COOKIE['phxcrmclient_id'] . "_client_recurr WHERE uid_r = " . $uid_r;

		//echo $queryac2r;
		$resultaqty=mysqli_query($conn, $queryaqty);

		while($rowaqty=mysqli_fetch_array($resultaqty)) { 
 
			$r_unit				 = $rowaqty['r_unit']; 
	
		}
		
		
		$r_line_amt = $r_unit * $field_value1;
	
		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client_recurr set r_line_amt = ".$r_line_amt.",".$field_name1_db."=".$field_value1." where uid_r = ".$uid_r;

	//	echo $queryupdate;
		
		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$r_line_amt = number_format($r_line_amt, 2, '.', ',');
		$r_qty = $field_value1;
		
		$return_array = array('result_message' =>$result_message,'r_line_amt' => $r_line_amt,'r_qty' => $r_qty);

		$return_json  = json_encode($return_array);

		echo $return_json;
		
		exit;
}else if ($field_type == "inputdouble"){
			
		$field_value1	=	GetSQLValueString($field_value1, "double");

	//	echo $field_value1;
	
	$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client_recurr set ".$field_name1_db."=".$field_value1." where uid_r = ".$uid_r;
	
	//	echo $queryupdate;

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

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$return_array = array('result_message' =>$result_message);

		$return_json  = json_encode($return_array);

		echo $return_json;
		
		exit;
		
		
}else if ($field_type == "input"){
			
		$field_value1	=	GetSQLValueString($field_value1, "text");

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_client_recurr set ".$field_name1_db."=".$field_value1." where uid_r = ".$uid_r;
	
	//	echo $queryupdate;

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

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		$return_array = array('result_message' =>$result_message);

		$return_json  = json_encode($return_array);

		echo $return_json;
		
		exit;
		

		}
		


