<?php
require_once('config.php');
require_once('send_notification.php');
require_once('f_insert_notification_log.php');
require_once('find_online_tablet.php');
require_once('f_logging.php');

$error_message;
$valid = true;
$insert_ntf1 = false;
$insert_ntf2 = false;
$insert_ntf3 = false;
$insert_ntf4 = false;
$wko_status;
/* 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);

//echo $data;	
$site_cd = $data['site_cd'];
$sync_step = $data['sync_step'];
$sync_time = $data['sync_time'];
$sync_status = $data['sync_status'];
$sync_url = $data['sync_url'];
$LOGINID = $data['LOGINID'];
$dvc_id = $data['dvc_id'];

if($sync_status =="online"){
	
}else{
	f_logging($json,"update_workorder.php",$sync_url,$LOGINID,$sync_step,$sync_time,$site_cd);
}

$EmpID = $data['EmpID'];
$EmpName = $data['EmpName'];

$wko_mst_originator = $data['wko_mst_originator'];
$wko_mst_phone = $data['wko_mst_phone'];
 
$ast_mst_asset_no = $data['ast_mst_asset_no'];
$ast_mst_asset_shortdesc = $data['ast_mst_asset_shortdesc'];
$ast_mst_asset_group = $data['ast_mst_asset_group'];
$mst_war_work_area = $data['mst_war_work_area'];
$ast_mst_asset_locn = $data['ast_mst_asset_locn'];
$ast_mst_asset_lvl = $data['ast_mst_asset_lvl'];
$wko_mst_chg_costcenter = $data['wko_mst_chg_costcenter'];

$wko_mst_wo_no = $data['wko_mst_wo_no'];
$wko_mst_orig_priority = $data['wko_mst_orig_priority'];
$wko_mst_plan_priority = $data['wko_mst_plan_priority'];
$wko_mst_org_date = $data['wko_mst_org_date'];
$wko_mst_due_date = $data['wko_mst_due_date'];
$wko_det_work_type = $data['wko_det_work_type'];
//$wko_mst_flt_code = $data['wko_mst_flt_code'];
$wko_mst_descs = $data['wko_mst_descs'];
$wko_mst_status = $data['wko_mst_status'];
$wko_det_assign_to =$data['wko_det_assign_to'];
$wko_det_laccount = $data['wko_det_laccount'];
$wko_det_caccount = $data['wko_det_caccount'];
$wko_det_maccount = $data['wko_det_maccount'];

$ast_mst_asset_code = $data['ast_mst_asset_code'];
$per_assignto = $data['per_assignto'];
$assign_remake = $data['assign_remake'];
$ROW_ID = $data['RowID'];

$wko_mst_project_id = $data['wko_mst_project_id'];
$wko_det_numeric1 = $data['wko_det_numeric1'];
$wko_det_numeric2 = $data['wko_det_numeric2'];
$wko_det_numeric3 = $data['wko_det_numeric3'];
			
$sql_wko_mst_status = "select wko_mst_status ,sts_typ_typ_cd  
						from wko_mst (NOLOCK) ,wrk_sts (NOLOCK) , sts_typ (NOLOCK)  
						where wko_mst.site_cd = ? 
						and wko_mst.site_cd = wrk_sts.site_cd
						and wko_mst.wko_mst_status = wrk_sts.wrk_sts_status
						and wko_mst.site_cd = sts_typ.site_cd
						and wrk_sts.wrk_sts_typ_cd = sts_typ.sts_typ_typ_cd
						and sts_typ.sts_typ_cat_cd = 'WORK'
						and 	wko_mst_wo_no = ?";						
//echo $sql_wko_mst_status;	
$params_wko_mst_status = array($site_cd,$wko_mst_wo_no);

$stmt_wko_mst_status = sqlsrv_query( $conn, $sql_wko_mst_status,$params_wko_mst_status, array("Scrollable"=>'keyset'));	

$row = sqlsrv_has_rows( $stmt_wko_mst_status );
	
if( !$stmt_wko_mst_status ) {
	$error_message = "Error Select table (wko_mst)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}

if ($row == true) { 
	do {
		 while ($row = sqlsrv_fetch_array($stmt_wko_mst_status, SQLSRV_FETCH_ASSOC)) {
		 
		 $wko_status = $row['wko_mst_status'];
		 $sts_typ_typ_cd = $row['sts_typ_typ_cd'];
		// echo  $sts_typ_typ_cd; 
		
	   }
	} while ( sqlsrv_next_result($stmt_wko_mst_status));
	sqlsrv_free_stmt( $stmt_wko_mst_status);
	
}else{
	
	$error_message = "No record found sql_wko_mst_status";
	returnError($error_message);
}


$sql_sys_ver = "select sys_ver_mobile_ver,sys_ver_mobile_api_ver from sys_ver (NOLOCK)";	
$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)) {
	 
	 $sys_ver_mobile_ver = $row['sys_ver_mobile_ver'];
	 $sys_ver_mobile_api_ver = $row['sys_ver_mobile_api_ver'];
	 	 
   }
} while ( sqlsrv_next_result($stmt_sys_ver));
sqlsrv_free_stmt( $stmt_sys_ver);


if($sts_typ_typ_cd == 'FORCE-CLOSE' || $sts_typ_typ_cd == 'CLOSE' || $sts_typ_typ_cd == 'COMPLETE'){
	
	
	$sql_insert_syn_mst = "	INSERT syn_mst 
								(	site_cd ,			syn_mst_module,		syn_mst_doc_no,		syn_mst_action, 
									syn_mst_status,		syn_mst_desc,		syn_mst_dvc_id ,	syn_mst_json,
									syn_mst_mobile_ver,	syn_mst_api_ver,	audit_user,			audit_date,		mbl_audit_user,		mbl_audit_date ) 
						VALUES 
								(	?,					?,					?,					?,
									?,					?,					?,					?,				
									?,					?,					?,					GetDate(),		?,					GetDate())";

	$params_insert_syn_mst = array($site_cd,			"Work Order",		$wko_mst_wo_no,		"Upload",
									"No Action",		"No action for this Work Order No : ".$wko_mst_wo_no.", Mobile Status : ".$wko_mst_status.", Server Status : ".$wko_status."",$dvc_id,	$json,
									$sys_ver_mobile_ver,$sys_ver_mobile_api_ver,			$LOGINID,							$LOGINID);
		$stmt_insert_syn_mst = sqlsrv_query( $conn, $sql_insert_syn_mst,$params_insert_syn_mst);	
		if( !$stmt_insert_syn_mst ) {
		$error_message = "Error (INSERT Table syn_mst)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
			
		}
		sqlsrv_free_stmt( $stmt_insert_syn_mst);	
		sqlsrv_commit( $conn );
		
		returnNoaction($wko_mst_wo_no, $sts_typ_typ_cd );	
	
}else {

 if($wko_mst_status != $wko_status ){
	
	
			$sql_wko_sts = "UPDATE	wko_sts WITH (UPDLOCK)
							SET 	wko_sts_end_date = GetDate(),
									wko_sts_duration = 0,
									audit_user = ?,
									audit_date = getdate(),
									mbl_audit_user = ?,
									mbl_audit_date = getdate()
							WHERE	wko_sts_end_date IS NULL
							AND		site_cd = ?
							AND		wko_sts_wo_no = ?
							AND		wko_sts_status = ?
							AND		mst_RowID = ?";
				
			
			$params_wko_sts = array($LOGINID,$LOGINID,$site_cd,$wko_mst_wo_no,$wko_status,$ROW_ID);
			$stmt_wko_sts = sqlsrv_query( $conn, $sql_wko_sts,$params_wko_sts);	
				
		if( !$stmt_wko_sts ) {
			$error_message = "Error update table (wko_sts)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}sqlsrv_free_stmt( $stmt_wko_sts);
	
	
		$sql_insert_wko_sts = "	INSERT wko_sts 
								(	site_cd ,					mst_RowID ,					wko_sts_wo_no ,					wko_sts_status , 
									wko_sts_originator ,		wko_sts_start_date ,		wko_sts_end_date ,				wko_sts_duration , 
									audit_user ,				audit_date,					mbl_audit_user,					mbl_audit_date ) 
						VALUES 
								(	?,							?,							?,								?,
									?,							GetDate(),					NULL,							NULL,
									?,							GetDate(),					?,								GetDate())";

		$params_insert_wko_sts = array($site_cd,$ROW_ID,$wko_mst_wo_no,$wko_mst_status,$wko_mst_originator,$LOGINID,$LOGINID);
		$stmt_insert_wko_sts = sqlsrv_query( $conn, $sql_insert_wko_sts,$params_insert_wko_sts);	
		if( !$stmt_insert_wko_sts ) {
		$error_message = "Error (INSERT Table wko_sts)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
			
		}
		sqlsrv_free_stmt( $stmt_insert_wko_sts);

}

$sq_wko_mst = "UPDATE  	wko_mst  WITH (UPDLOCK) SET							
						wko_mst_originator=?,			
						wko_mst_phone=?,				
						wko_mst_asset_level=?,			
						wko_mst_assetno=?,				
									
						wko_mst_status=?,
						wko_mst_descs=?,	
						wko_mst_org_date=?,
						wko_mst_due_date=?,			
						wko_mst_chg_costcenter=?,			
						wko_mst_work_area=?,				
						wko_mst_asset_location=?,		
						wko_mst_asset_group_code=?,	
						wko_mst_orig_priority=?,			
						wko_mst_plan_priority=?,					
						wko_mst_ast_cod=?,	
						wko_mst_project_id =?,						
						audit_user=?,						
						audit_date=GETDATE(),
						mbl_audit_user=?,						
						mbl_audit_date=GETDATE()						
						where 	site_cd = ?
						AND		wko_mst_wo_no = ?	
						AND		RowID = ?";								
								
//echo $sql;
$params_wko_mst = array($wko_mst_originator,$wko_mst_phone,$ast_mst_asset_lvl,$ast_mst_asset_no,$wko_mst_status,$wko_mst_descs,$wko_mst_org_date,$wko_mst_due_date,$wko_mst_chg_costcenter,$mst_war_work_area,$ast_mst_asset_locn,$ast_mst_asset_group,$wko_mst_orig_priority,$wko_mst_plan_priority,$ast_mst_asset_code,$wko_mst_project_id,$LOGINID,$LOGINID,$site_cd,$wko_mst_wo_no,$ROW_ID);	

$stmt_wko_mst = sqlsrv_query( $conn, $sq_wko_mst,$params_wko_mst);

if( !$stmt_wko_mst ) {
	$error_message = "Error insert table (UPDATE Table wko_mst)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}	
sqlsrv_free_stmt( $stmt_wko_mst);	


if($sync_status =="online"){
	
	$sql_wko_det = "UPDATE  wko_det WITH (UPDLOCK)  SET
						wko_det_work_type=?,
						wko_det_laccount=?,			
						wko_det_caccount=?,				
						wko_det_maccount=?,
						wko_det_assign_to=?,
						wko_det_numeric1=?,		
						wko_det_numeric2=?,		
						wko_det_numeric3=?,								
						mbl_audit_user=?,						
						mbl_audit_date=GETDATE()						
						where 	site_cd = ?		
						AND		mst_RowID = ?";								
								
	$params_wko_det = array($wko_det_work_type,$wko_det_laccount,$wko_det_caccount,$wko_det_maccount,$wko_det_assign_to,$wko_det_numeric1,$wko_det_numeric2,$wko_det_numeric3,$LOGINID,$site_cd,$ROW_ID);	
	$stmt_wko_det = sqlsrv_query( $conn, $sql_wko_det,$params_wko_det);
					

	if( !$stmt_wko_det ) {
		$error_message = "Error insert table (UPDATE Table wko_det)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt_wko_det);
	
}else{
	
	
	$sql_wko_det = "UPDATE  wko_det WITH (UPDLOCK) SET
							wko_det_varchar9=?,
							wko_det_varchar10=?,			
							wko_det_datetime5=?,
							mbl_audit_user=?,						
							mbl_audit_date=GETDATE()							
							where 	site_cd = ?		
							AND		mst_RowID = ?";								
									
	$params_wko_det = array($data['wko_det_varchar9'],$data['wko_det_varchar10'],$data['wko_det_datetime5'],$LOGINID,$site_cd,$ROW_ID);	
	$stmt_wko_det = sqlsrv_query( $conn, $sql_wko_det,$params_wko_det);
					

	if( !$stmt_wko_det ) {
		$error_message = "Error insert table (UPDATE Table wko_det)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt_wko_det);
	
}





//AssignTo Value From Value A(WIthdraw) to Value B(Assign)
 if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and !empty($wko_det_assign_to)){	

	// Insert notification_log for Old Value Withdrawal
	// $title = "Work Order Withdrawal";
	// $body = "The work order ".$wko_mst_wo_no." been assigned to another employee.";
	// $module = "Work Order";
	// $message_withdraw ="Asset No:".$ast_mst_asset_no." 
	// \r\nAsset Desc:".$ast_mst_asset_shortdesc."
	// \r\nWork Area:".$ast_mst_asset_shortdesc."
	// \r\nLocation:".$ast_mst_asset_locn."";
	
	// $insert_ntf1 = insert_notification_log($site_cd, $EmpID, $wko_det_assign_to, $wko_mst_wo_no, $title, $body, $message_withdraw, $module, $dvc_id, $LOGINID);
	
	// foreach($insert_ntf1 as $x => $val) {
			
		// $row = $x = $val["row"];
		// $insert_ntf = $x = $val["insert_ntf"];
		
		//echo '<pre>'; print_r($row); echo '</pre>';
		//echo '<pre>'; print_r($insert_ntf); echo '</pre>';
		
	// }
	
	
	// Get New Value Assignto Online Token
	//$tkn_token_1 = get_online($per_assignto, $site_cd);
	// echo '<pre>'; print_r($tkn_token_1); echo '</pre>';
	
	// Notify token user
	// if (sizeof($tkn_token_1) > 0){		
		// foreach($tkn_token_1 as $x => $val) {			
			// $tokenlist = $x = $val["token"];
			// $RowID = $x = $val["RowID"];
			// $tkn_site = $x = $val["tkn_site"];
			
			//echo '<pre>'; print_r($tkn_site); echo '</pre>';
			//echo '<pre>'; print_r($RowID); echo '</pre>';			
			
			//push_notification_android( $tokenlist,'The work order '.$wko_mst_wo_no.' been assigned to another employee',"Work Order Withdrawal (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
		// }		
		
	// }	
	
	// Insert notification_log for New Value Assign
	// $title = "Work Order Assigned";
	// $body = "The work order ".$wko_mst_wo_no." been assigned to you.";
	// $module = "Work Order";
	// $message_assign ="Asset No:".$ast_mst_asset_no." 
	// \r\nAsset Desc:".$ast_mst_asset_shortdesc."
	// \r\nWork Area:".$ast_mst_asset_shortdesc."
	// \r\nLocation:".$ast_mst_asset_locn."";
	
	$sql_wko_ls7_max = "select max_level = coalesce( MAX (wko_ls7_level ),0) + 1 from  wko_ls7 where site_cd =? and mst_RowID = ?";
				
			
	$params_wko_ls7_max = array($site_cd,$ROW_ID);
	$stmt_wko_ls7_max = sqlsrv_query( $conn, $sql_wko_ls7_max,$params_wko_ls7_max);	
				
	if( !$stmt_wko_ls7_max ) {
		$error_message = "Error select table (wko_ls7)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}
	do {
		
		while ($row = sqlsrv_fetch_array($stmt_wko_ls7_max, SQLSRV_FETCH_ASSOC)) {		
		
		$max_level = $row['max_level'];		
		
		}
		
	} while ( sqlsrv_next_result($stmt_wko_ls7_max) );
	sqlsrv_free_stmt( $stmt_wko_ls7_max);

	
	$sql_wko_ls7 = "UPDATE	wko_ls7 WITH (UPDLOCK)
					SET 	wko_ls7_date3 = GetDate(),
							mbl_audit_user=?,						
							mbl_audit_date=GETDATE()	
					WHERE	wko_ls7.site_cd = ?
					AND		wko_ls7.mst_rowid = ?
					AND		wko_ls7.wko_ls7_emp_id = ? 
					AND		wko_ls7.wko_ls7_date3 IS NULL ";
				
			
			$params_wko_ls7 = array($LOGINID,$site_cd,$ROW_ID,$per_assignto);
			$stmt_wko_ls7 = sqlsrv_query( $conn, $sql_wko_ls7,$params_wko_ls7);	
				
		if( !$stmt_wko_ls7 ) {
			$error_message = "Error update table (wko_ls7)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}sqlsrv_free_stmt( $stmt_wko_ls7);
	
	
	
	$sql_insert_wko_ls7 = "	INSERT wko_ls7 
								(	site_cd ,					mst_rowid ,					wko_ls7_level ,					wko_ls7_emp_id , 
									wko_ls7_due_days ,			wko_ls7_wo_org_date ,		wko_ls7_due_date ,				wko_ls7_total_days , 
									audit_user ,				audit_date,					column1,						mbl_audit_user,			mbl_audit_date ) 
						VALUES 
								(	?,							?,							?,								?,
									?,							?,							?,								'1',
									?,							GetDate(),					?,								?,						GetDate())";

		$params_insert_wko_ls7 = array($site_cd,$ROW_ID,$max_level,$wko_det_assign_to,'1',$wko_mst_org_date,$wko_mst_due_date,$LOGINID,$assign_remake,$LOGINID);
		$stmt_insert_wko_ls7 = sqlsrv_query( $conn, $sql_insert_wko_ls7,$params_insert_wko_ls7);	
		if( !$stmt_insert_wko_ls7 ) {
		$error_message = "Error (INSERT Table wko_ls7)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
			
		}
		sqlsrv_free_stmt( $stmt_insert_wko_ls7);
	
	// $insert_ntf2 = insert_notification_log($site_cd, $EmpID, $wko_det_assign_to, $wko_mst_wo_no, $title, $body, $message_assign, $module, $dvc_id, $LOGINID);
	
	// foreach($insert_ntf2 as $x => $val) {
			
			// $row = $x = $val["row"];
			// $insert_ntf = $x = $val["insert_ntf"];
			
			//echo '<pre>'; print_r($row); echo '</pre>';
			//echo '<pre>'; print_r($insert_ntf); echo '</pre>';		
		
	// }
	
	
	//Get New Value Assignto Online Token
	//$tkn_token_2 = get_online($wko_det_assign_to, $site_cd);
	
	//echo '<pre>'; print_r($tkn_token_2); echo '</pre>';
	
	//Notify token user
	// if (sizeof($tkn_token_2) > 0){		
		
		
		// foreach($tkn_token_2 as $x => $val) {			
			// $tokenlist = $x = $val["token"];
			// $RowID = $x = $val["RowID"];
			// $tkn_site = $x = $val["tkn_site"];
			
			//echo '<pre>'; print_r($tkn_site); echo '</pre>';
			//echo '<pre>'; print_r($RowID); echo '</pre>';			
			
			//push_notification_android( $tokenlist,'The work order '.$wko_mst_wo_no.' been assigned to you',"Work Order Assigned (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
			
		
		// }		
		
	// }	
	
	
		
}

//AssignTo Value From Value A to NULL
 if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and empty($wko_det_assign_to)){
	 
	//Insert notification_log for Old Value Withdrawal
	// $title = "Work Order Withdrawal";
	// $body = "The work order ".$wko_mst_wo_no." been withdraw.";
	// $module = "Work Order";
	// $message_withdraw ="Asset No:".$ast_mst_asset_no." 
	// \r\nAsset Desc:".$ast_mst_asset_shortdesc."
	// \r\nWork Area:".$ast_mst_asset_shortdesc."
	// \r\nLocation:".$ast_mst_asset_locn."";
	
	$sql_wko_ls7 = "UPDATE	wko_ls7 WITH (UPDLOCK)
					SET 	wko_ls7_date3 = GetDate(),
							mbl_audit_user=?,						
							mbl_audit_date=GETDATE(),	
					column1 = 
					CASE WHEN column1 IS NULL THEN '".$assign_remake."'
					ELSE column1 + ' | ' + '".$assign_remake."'
					END
					WHERE		wko_ls7.site_cd = ?
					AND			wko_ls7.mst_rowid = ?
					AND			wko_ls7.wko_ls7_emp_id = ? 
					AND			wko_ls7.wko_ls7_date3 IS NULL ";
				
			
			$params_wko_ls7 = array($LOGINID,$site_cd,$ROW_ID,$per_assignto);
			$stmt_wko_ls7 = sqlsrv_query( $conn, $sql_wko_ls7,$params_wko_ls7);	
				
		if( !$stmt_wko_ls7 ) {
			$error_message = "Error update table (wko_ls7)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}sqlsrv_free_stmt( $stmt_wko_ls7);
		
	
	//$insert_ntf3 = insert_notification_log($site_cd, $EmpID, $wko_det_assign_to, $wko_mst_wo_no, $title, $body, $message_withdraw, $module, $dvc_id, $LOGINID);
	
	// foreach($insert_ntf3 as $x => $val) {
			
		// $row = $x = $val["row"];
		// $insert_ntf = $x = $val["insert_ntf"];
		
		//echo '<pre>'; print_r($row); echo '</pre>';
		//echo '<pre>'; print_r($insert_ntf); echo '</pre>';		
	// }
	
	
	//Get New Value Assignto Online Token
	//$tkn_token_3 = get_online($per_assignto, $site_cd);
	
	//echo '<pre>'; print_r($tkn_token_2); echo '</pre>';
	
	//Notify token user
	// if (sizeof($tkn_token_3) > 0){		
		
		
		// foreach($tkn_token_3 as $x => $val) {			
			// $tokenlist = $x = $val["token"];
			// $RowID = $x = $val["RowID"];
			// $tkn_site = $x = $val["tkn_site"];
			
			//echo '<pre>'; print_r($tkn_site); echo '</pre>';
			//echo '<pre>'; print_r($RowID); echo '</pre>';			
			
			//push_notification_android( $tokenlist,'The work order '.$wko_mst_wo_no.' been withdraw',"Work Order Withdrawal  (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
			
		
		// }		
		
	// }	
	
	
}


//AssignTo Value From Value Null to A
if(!empty($wko_det_assign_to) and empty($per_assignto)){

	
	// $title = "Work Order Assigned";
	// $body = "The work order ".$wko_mst_wo_no." been assigned to you.";
	// $module = "Work Order";
	// $message_assign ="Asset No:".$ast_mst_asset_no."
	// \r\nAsset Desc:".$ast_mst_asset_shortdesc."
	// \r\nWork Area:".$ast_mst_asset_shortdesc."
	// \r\nLocation:".$ast_mst_asset_locn."";
	
	
	
	$sql_wko_ls7_max = "select max_level = coalesce( MAX (wko_ls7_level ),0) + 1 from  wko_ls7 where site_cd =? and mst_RowID = ?";
				
			
	$params_wko_ls7_max = array($site_cd,$ROW_ID);
	$stmt_wko_ls7_max = sqlsrv_query( $conn, $sql_wko_ls7_max,$params_wko_ls7_max);	
		
	if( !$stmt_wko_ls7_max ) {
		$error_message = "Error select table (wko_ls7)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}
	do {
		
		while ($row = sqlsrv_fetch_array($stmt_wko_ls7_max, SQLSRV_FETCH_ASSOC)) {		
		
		$max_level = $row['max_level'];		
		
		}
		
	} while ( sqlsrv_next_result($stmt_wko_ls7_max) );
	
	//sqlsrv_free_stmt( $stmt_wko_ls7_max);
	
	
	$sql_insert_wko_ls7 = "	INSERT wko_ls7 
								(	site_cd ,					mst_rowid ,					wko_ls7_level ,					wko_ls7_emp_id , 
									wko_ls7_due_days ,			wko_ls7_wo_org_date ,		wko_ls7_due_date ,				wko_ls7_total_days , 
									audit_user ,				audit_date,					column1,						mbl_audit_user,			mbl_audit_date ) 
						VALUES 
								(	?,							?,							?,								?,
									?,							?,							?,								'1',
									?,							GetDate(),					?,								 ?,							GetDate())";

		$params_insert_wko_ls7 = array($site_cd,$ROW_ID,$max_level,$wko_det_assign_to,'1',$wko_mst_org_date,$wko_mst_due_date,$LOGINID,$assign_remake,$LOGINID);
		$stmt_insert_wko_ls7 = sqlsrv_query( $conn, $sql_insert_wko_ls7,$params_insert_wko_ls7);	
		if( !$stmt_insert_wko_ls7 ) {
		$error_message = "Error (INSERT Table wko_ls7)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
			
		}
		sqlsrv_free_stmt( $stmt_insert_wko_ls7);
	
	//$insert_ntf4 = insert_notification_log($site_cd, $EmpID, $wko_det_assign_to, $wko_mst_wo_no, $title, $body, $message_assign, $module, $dvc_id, $LOGINID);
	
	// foreach($insert_ntf4 as $x => $val) {
			
		// $row = $x = $val["row"];
		// $insert_ntf = $x = $val["insert_ntf"];
		
		//echo '<pre>'; print_r($row); echo '</pre>';
		//echo '<pre>'; print_r($insert_ntf); echo '</pre>';		
			
	// }
	
	
	//Get New Value Assignto Online Token
	//$tkn_token_4 = get_online($wko_det_assign_to, $site_cd);
	
	//echo '<pre>'; print_r($tkn_token_2); echo '</pre>';
	
	//Notify token user
	// if (sizeof($tkn_token_4) > 0){		
		
		
		// foreach($tkn_token_4 as $x => $val) {			
			// $tokenlist = $x = $val["token"];
			// $RowID = $x = $val["RowID"];
			// $tkn_site = $x = $val["tkn_site"];
			
			// echo '<pre>'; print_r($tkn_site); echo '</pre>';
			// echo '<pre>'; print_r($RowID); echo '</pre>';			
			
			// push_notification_android( $tokenlist,'The work order '.$wko_mst_wo_no.' been assigned to you',"Work Order Assigned (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
			
		
		// }		
		
	// }	
}	


if($stmt_wko_mst_status){ 
	
	 
	 if($wko_mst_status != $wko_status){
		 
		 if( $stmt_wko_sts && $stmt_insert_wko_sts && $stmt_wko_mst && $stmt_wko_det  ) {			 
			
			
			 //AssignTo Value From Value A(WIthdraw) to Value B(Assign)
			if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and !empty($wko_det_assign_to)){
				// if($insert_ntf1 && $insert_ntf2){
					
					// sqlsrv_commit( $conn );
					// returnData($ROW_ID,$wko_mst_wo_no);
				// }else{
					
					// sqlsrv_rollback( $conn );
					// $error_message = "Transaction rolled back.<br />";
					// returnError($error_message); 
				// }
				
				sqlsrv_commit( $conn );
				returnData($ROW_ID,$wko_mst_wo_no);
				
			}
			//AssignTo Value From Value A to NULL
			else if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and empty($wko_det_assign_to)){
				// if($insert_ntf3){
					// sqlsrv_commit( $conn );
					// returnData($ROW_ID,$wko_mst_wo_no);
				// }else{
					// sqlsrv_rollback( $conn );
					// $error_message = "Transaction rolled back.<br />";
					// returnError($error_message); 
				// }
				
				sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no);
			}
			//AssignTo Value From Value Null to A
			else if(!empty($wko_det_assign_to) and empty($per_assignto)){
				// if($insert_ntf4){
					// sqlsrv_commit( $conn );
					// returnData($ROW_ID,$wko_mst_wo_no); 
				// }else{
					// sqlsrv_rollback( $conn );
					// $error_message = "Transaction rolled back.<br />";
					// returnError($error_message); 
				// }
				
				sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no); 
			}
			else{
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no); 
			}
			 
		 }else{
			 
			 //1
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		 }
		 
	 }else{
		 
		 
		if( $stmt_wko_mst && $stmt_wko_det  ) {
			
			 //AssignTo Value From Value A(WIthdraw) to Value B(Assign)
			if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and !empty($wko_det_assign_to)){
				// if($insert_ntf1 && $insert_ntf2){
					
					// sqlsrv_commit( $conn );
					// returnData($ROW_ID,$wko_mst_wo_no);
				// }else{
					
					// sqlsrv_rollback( $conn );
					// $error_message = "Transaction rolled back.<br />";
					// returnError($error_message); 
				// }
				
				sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no);
				
			}
			//AssignTo Value From Value A to NULL
			else if($per_assignto != $wko_det_assign_to and !empty($per_assignto) and empty($wko_det_assign_to)){
				// if($insert_ntf3){
					// sqlsrv_commit( $conn );
					// returnData($ROW_ID,$wko_mst_wo_no);
				// }else{
					// sqlsrv_rollback( $conn );
					// $error_message = "Transaction rolled back.<br />";
					// returnError($error_message); 
				// }
				
				sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no);
			}
			//AssignTo Value From Value Null to A
			else if(!empty($wko_det_assign_to) and empty($per_assignto)){
				// if($insert_ntf4){
					// sqlsrv_commit( $conn );
					// returnData($ROW_ID,$wko_mst_wo_no); 
				// }else{
					// sqlsrv_rollback( $conn );
					// $error_message = "Transaction rolled back.<br />";
					// returnError($error_message); 
				// }
				
				sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no); 
			}
			else{
					sqlsrv_commit( $conn );
					returnData($ROW_ID,$wko_mst_wo_no); 
			}
			 
		 }else{
			 
			 //2
			sqlsrv_rollback( $conn );
			$error_message = "Transaction rolled back.<br />";
			returnError($error_message); 
		 } 
	 }
	 
	 
 }else{
	 
	 //3
	sqlsrv_rollback( $conn );
	$error_message = "Transaction rolled back.<br />";
	returnError($error_message);
 }
 

}
 

function returnNoaction($wko_mst_wo_no,$sts_typ_typ_cd){
	$returnData = array(
	'status' => 'SUCCESS',	
	'message' => 'No action for this Work Order No:'.$wko_mst_wo_no.', Status : '.$sts_typ_typ_cd.'');
	echo json_encode($returnData);
	exit();
}


 
function returnData($ROW_ID,$wko_mst_wo_no){
	$returnData = array(
	'status' => 'SUCCESS',
	'ROW_ID'=>	$ROW_ID,
	'WorkOrderno' => $wko_mst_wo_no,
	'message' => 'Work Order No:' . $wko_mst_wo_no . ' update successfully');
	echo json_encode($returnData);
	 
	//echo '1';
}

function returnError($error_message){	

	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message);	
	echo json_encode($returnData);
	exit();
}


 
?>