<?php
require_once('config.php');
//require_once('f_update_budget.php');

$error_message;
$valid = true;
$message='';


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}


$json = file_get_contents('php://input');

// Converts it into a PHP object
$data = json_decode($json, true);


$dvc_usr_dvc_id = $data['dvc_usr_dvc_id'];
$dvc_usr_brand = $data['dvc_usr_brand'];
$dvc_usr_model = $data['dvc_usr_model'];
$dvc_usr_app_ver = $data['dvc_usr_app_ver'];
$dvc_usr_app_gen_time = $data['dvc_usr_app_gen_time'];




$sql_exits_dvc_user = "Select dvc_exits = count(*) from dvc_usr where dvc_usr_dvc_id ='".$dvc_usr_dvc_id."'"  ;
		
	$stmt_exits_dvc_user = sqlsrv_query( $conn, $sql_exits_dvc_user);	
			
	if( !$stmt_exits_dvc_user ) {
		$error_message = "Error Select table (dvc_user)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}
	do {
		 while ($row = sqlsrv_fetch_array($stmt_exits_dvc_user, SQLSRV_FETCH_ASSOC)) {	
		 
			$dvc_exits= $row['dvc_exits'];		
		
		 }
	}while ( sqlsrv_next_result($stmt_exits_dvc_user) );
	
	
	if($dvc_exits == '1'){
		
			
		$sql_det_dvc_user = "delete from  dvc_usr where dvc_usr_dvc_id= '".$dvc_usr_dvc_id."'"  ;
			
		$stmt_det_dvc_user = sqlsrv_query( $conn, $sql_det_dvc_user);	
				
		if( !$stmt_det_dvc_user ) {
			$error_message = "Error Select table (det_dvc_user)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		 while ( sqlsrv_next_result($stmt_det_dvc_user));
		 
		 
		 
		
		
		$sql_sys_ver = "Select mobile_license = sys_ver_mobile_license from sys_ver"  ;
		
		$stmt_sys_ver = sqlsrv_query( $conn, $sql_sys_ver);	
				
		if( !$stmt_sys_ver ) {
			$error_message = "Error Select table (sys_ver)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_sys_ver, SQLSRV_FETCH_ASSOC)) {	
			 
				$mobile_license= $row['mobile_license'];		
			
			 }
		} while ( sqlsrv_next_result($stmt_sys_ver) );





		$sql_dvc_user = "Select total_dvc = count(*) from dvc_usr"  ;
				
			$stmt_dvc_user = sqlsrv_query( $conn, $sql_dvc_user);	
				
		if( !$stmt_dvc_user ) {
			$error_message = "Error Select table (dvc_user)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_dvc_user, SQLSRV_FETCH_ASSOC)) {	
			 
				$total_dvc= $row['total_dvc'];		
			
			 }
		}while ( sqlsrv_next_result($stmt_dvc_user) );
		
		
		if($total_dvc >= $mobile_license){
			
			$message = "License exceed, please contact system administrator.";
			
			sqlsrv_commit( $conn );
			sqlsrv_close( $conn);
			returnError($message);
			
			
		}else{
			
			
			$sql_insert_dvc_usr = "	Insert into dvc_usr (	dvc_usr_dvc_id,			dvc_usr_brand,		dvc_usr_model,		dvc_usr_app_ver,		dvc_usr_app_gen_time,			dvc_usr_val_time,
															dvc_usr_varchar1,		dvc_usr_varchar2,	dvc_usr_varchar3,	dvc_usr_varchar4,		dvc_usr_varchar5,				dvc_usr_numeric1,
															dvc_usr_numeric2,		dvc_usr_numeric3,	dvc_usr_numeric4,	dvc_usr_numeric5,		dvc_usr_datetime1,				dvc_usr_datetime2,
															dvc_usr_datetime3,		dvc_usr_datetime4,	dvc_usr_datetime5,	audit_user,				audit_date,						column1,
															column2,				column3,			column4,			column5)
									VALUES(					?,						?,					?,					?,						?,								Getdate(),
															NULL,					NULL,				NULL,				NULL,					NULL,							NULL,
															NULL,					NULL,				NULL,				NULL,					NULL,							NULL,
															NULL,					NULL,				NULL,				NULL,					Getdate(),						NULL,
															NULL,					NULL,				NULL,				NULL)";

				$params_insert_dvc_usr = array($dvc_usr_dvc_id,$dvc_usr_brand,$dvc_usr_model,$dvc_usr_app_ver,$dvc_usr_app_gen_time);

				$stmt_insert_dvc_usr = sqlsrv_query( $conn, $sql_insert_dvc_usr,$params_insert_dvc_usr);	

				if( !$stmt_insert_dvc_usr ) {
				$error_message = "Error (INSERT Table dvc_usr)";
				returnError($error_message);
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));	

				}
				
				sqlsrv_commit( $conn );
				sqlsrv_close( $conn);	
				$message = "insert successfully";
				returnData($message);
				
			
		}		
		
		
		
	}else {
		$sql_sys_ver = "Select mobile_license = sys_ver_mobile_license from sys_ver"  ;
		
		$stmt_sys_ver = sqlsrv_query( $conn, $sql_sys_ver);	
				
		if( !$stmt_sys_ver ) {
			$error_message = "Error Select table (sys_ver)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_sys_ver, SQLSRV_FETCH_ASSOC)) {	
			 
				$mobile_license= $row['mobile_license'];		
			
			 }
		} while ( sqlsrv_next_result($stmt_sys_ver) );





		$sql_dvc_user = "Select total_dvc = count(*) from dvc_usr"  ;
				
			$stmt_dvc_user = sqlsrv_query( $conn, $sql_dvc_user);	
				
		if( !$stmt_dvc_user ) {
			$error_message = "Error Select table (dvc_user)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_dvc_user, SQLSRV_FETCH_ASSOC)) {	
			 
				$total_dvc= $row['total_dvc'];		
			
			 }
		}while ( sqlsrv_next_result($stmt_dvc_user) );
		
		
		if($total_dvc >= $mobile_license){
			
			$message = "License exceed, please contact system administrator.";
			
			sqlsrv_commit( $conn );
			sqlsrv_close( $conn);
			returnError($message);
			
			
		}else{
			
			
			$sql_insert_dvc_usr = "	Insert into dvc_usr (	dvc_usr_dvc_id,			dvc_usr_brand,		dvc_usr_model,		dvc_usr_app_ver,		dvc_usr_app_gen_time,			dvc_usr_val_time,
																dvc_usr_varchar1,		dvc_usr_varchar2,	dvc_usr_varchar3,	dvc_usr_varchar4,		dvc_usr_varchar5,				dvc_usr_numeric1,
																dvc_usr_numeric2,		dvc_usr_numeric3,	dvc_usr_numeric4,	dvc_usr_numeric5,		dvc_usr_datetime1,				dvc_usr_datetime2,
																dvc_usr_datetime3,		dvc_usr_datetime4,	dvc_usr_datetime5,	audit_user,				audit_date,						column1,
																column2,				column3,			column4,			column5)
										VALUES(					?,						?,					?,					?,						?,								Getdate(),
																NULL,					NULL,				NULL,				NULL,					NULL,							NULL,
																NULL,					NULL,				NULL,				NULL,					NULL,							NULL,
																NULL,					NULL,				NULL,				NULL,					Getdate(),						NULL,
																NULL,					NULL,				NULL,				NULL)";

				$params_insert_dvc_usr = array($dvc_usr_dvc_id,$dvc_usr_brand,$dvc_usr_model,$dvc_usr_app_ver,$dvc_usr_app_gen_time);

				$stmt_insert_dvc_usr = sqlsrv_query( $conn, $sql_insert_dvc_usr,$params_insert_dvc_usr);	

				if( !$stmt_insert_dvc_usr ) {
				$error_message = "Error (INSERT Table dvc_usr)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));	

				}
				
				sqlsrv_commit( $conn );
				sqlsrv_close( $conn);	
				$message = "insert successfully";
				returnData($message);
				
			
		}		
	}

function returnData($message){
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => $message);
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'Alert',
	'message' => $error_message);	
	
	echo json_encode($returnData);		
	exit();
}
 
?>