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

$error_message;
$valid = true;
$insert_email = false;
$insert_ntf = false;

/* 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);


$site_cd = $data['site_cd'];
$EmpID = $data['EmpID'];
$LOGINID = $data['LOGINID'];
$ROW_ID = $data['RowID'];

$wko_mst_status = $data['wko_mst_status'];
$wko_mst_status_after = $data['wko_mst_status_after'];

$wko_det_cause_code = $data['wko_det_cause_code'];
$wko_det_act_code = $data['wko_det_act_code'];
$wko_det_work_class = $data['wko_det_work_class'];
$wko_det_work_grp = $data['wko_det_work_grp'];
$wko_det_corr_action = $data['wko_det_corr_action'];
$wko_mst_wo_no = $data['wko_mst_wo_no'];

$Is_checked = $data['Is_checked'];



$Assestno = $data['Assestno'];
$Assest_description = $data['Assest_description'];
$Work_Area = $data['Work_Area'];
$Assest_location = $data['Assest_location'];
$Assest_level = $data['Assest_level'];
$Originationdate = $data['Originationdate'];
$Duedate = $data['Duedate'];
$Work_Description = $data['Work_Description'];
$Requested_by = $data['Requested_by'];
$Contact_no = $data['Contact_no'];
$dvc_id = $data['dvc_id'];
$current_date = $data['current_date'];



		


$sql = "UPDATE	wko_det 
		SET		wko_det_cause_code=?,
				wko_det_act_code=?,		
				wko_det_work_class=?,
				wko_det_work_grp=?,
				wko_det_corr_action =?,
				wko_det.audit_user =?,						
				wko_det.audit_date=GETDATE()
		WHERE	wko_det.site_cd =?
		AND		wko_det.mst_RowID =?";								
								


$params_update_wko_det = array($wko_det_cause_code,$wko_det_act_code,$wko_det_work_class,$wko_det_work_grp,$wko_det_corr_action,$LOGINID,$site_cd,$ROW_ID);
$stmt_wko_det = sqlsrv_query( $conn, $sql,$params_update_wko_det);

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



if($Is_checked == 'true'){
	
	
	
	$sql_wrk_sts = "Select 	wrk_sts_tim_crd
					from 	wrk_sts (NOLOCK)
					Where 	site_cd = '".$site_cd."'
					And 	wrk_sts_status = '".$wko_mst_status_after."'";
					
					
		$stmt_wrk_sts = sqlsrv_query( $conn, $sql_wrk_sts);	
				
		if( !$stmt_wrk_sts ) {
			$error_message = "Error Select table (wrk_sts)";
			returnError($error_message);
			die( print_r( sqlsrv_errors(), true));
		}
		do {
			 while ($row = sqlsrv_fetch_array($stmt_wrk_sts, SQLSRV_FETCH_ASSOC)) {
			 
			 $wrk_sts_tim_crd = $row['wrk_sts_tim_crd'];					
				
		   }
		} while ( sqlsrv_next_result($stmt_wrk_sts));
		
		
		if($wrk_sts_tim_crd == "1"){
			
			
			$sql_wrk_sts = "Select 	counts = count(*) 
							from 	wko_ls8 (NOLOCK)
							Where 	site_cd = '".$site_cd."'
							And 	mst_RowID = '".$ROW_ID."'";
					
					
			$stmt_wrk_sts = sqlsrv_query( $conn, $sql_wrk_sts);	
					
			if( !$stmt_wrk_sts ) {
				$error_message = "Error Select table (wrk_sts)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}
			do {
				 while ($row = sqlsrv_fetch_array($stmt_wrk_sts, SQLSRV_FETCH_ASSOC)) {
				 
				 $counts = $row['counts'];					
				
			   }
			} while ( sqlsrv_next_result($stmt_wrk_sts));
			
			
		if($counts == "0"){
				
				
				sqlsrv_rollback( $conn );
				$error_message = "Please enter time card befoure complete";
				returnError($error_message);
				
				
		}else{
				
			if($wko_mst_status != $wko_mst_status_after){
				
				
				$sql = "UPDATE 	wko_mst SET 
						wko_mst_status = ?, 
						audit_user = ?, 
						audit_date = GETDATE()
						WHERE 	site_cd = ?
						AND 	wko_mst_wo_no = ?";								
											
				

				$params_update_wko_mst = array($wko_mst_status_after,$LOGINID,$site_cd,$wko_mst_wo_no);
				$stmt_wko_mst = sqlsrv_query( $conn, $sql,$params_update_wko_mst);

				if( !$stmt_wko_mst ) {
					$error_message = "Error UPDATE Table wko_mst";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}sqlsrv_free_stmt( $stmt_wko_mst);	
			
			
				
				
				$sql = "update wko_det 
								SET  
								wko_det_sc_date= ?,
								wko_det_cmpl_date= ?					
								WHERE	wko_det.site_cd =?
								AND		wko_det.mst_RowID = ?";									
												
								
				$params_update_wko_det = array($current_date,$current_date,$site_cd,$ROW_ID);
				$stmt_wko_det1 = sqlsrv_query( $conn,$sql,$params_update_wko_det);

				if( !$stmt_wko_det1 ) {
					$error_message = "Error UPDATE Table wko_det";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}sqlsrv_free_stmt( $stmt_wko_det1);		
				
				
				
				
				
				$sql = "update wko_sts 
						SET wko_sts_end_date =GETDATE() , 
						wko_sts_duration =0 , 
						audit_user =?, 						
						audit_date =GETDATE() 
						WHERE 
						wko_sts_end_date IS NULL 
						AND site_cd =? 
						AND wko_sts_wo_no =? 
						AND wko_sts_status =?";								
												
						
				$params_update_wko_sts = array($LOGINID,$site_cd,$wko_mst_wo_no,$wko_mst_status);		
				$stmt_wko_sts = sqlsrv_query( $conn,$sql,$params_update_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_ast_mst = "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 ) 
					VALUES	(		? ,							? ,								? ,						?,			
									? ,							getdate(),						NULL ,						NULL ,				 
									? ,							getdate())";
											
											
					
					$params_update_wko_sts = array($site_cd,$ROW_ID,$wko_mst_wo_no,$wko_mst_status_after,$LOGINID,$LOGINID);				
					$stmt_wko_sts1 = sqlsrv_query( $conn,$sql_insert_ast_mst,$params_update_wko_sts);

					if( !$stmt_wko_sts1 ) {
						$error_message = "Error insert table (wko_sts)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt_wko_sts1);		
				
				
				
				
				
				
				$sql_email = " Select emp_mst_empl_id, emp_det_email_id 
							from emp_mst (NOLOCK), emp_det (NOLOCK)
							Where emp_mst.site_cd = emp_det.site_cd
							And emp_mst.RowID = emp_det.mst_RowID
							And emp_mst.emp_mst_empl_id = '".$EmpID."'";
							
							
				$stmt_email = sqlsrv_query( $conn, $sql_email);	
						
				if( !$stmt_email ) {
					$error_message = "Error Select table (emp_mst,emp_det)";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}
				do {
					 while ($row = sqlsrv_fetch_array($stmt_email, SQLSRV_FETCH_ASSOC)) {
					 
					 $emp_mst_empl_id = $row['emp_mst_empl_id'];
					 $emp_det_email_id = $row['emp_det_email_id'];			
						 
				   }
				} while ( sqlsrv_next_result($stmt_email));
				
				
				if(!Empty($emp_det_email_id)){
					
					//Insert Email 
					$title = "Work Order Completed.";
					$body = "The work order  (".$wko_mst_wo_no.") is Completed.";
					$module = "Work Order";
					$message_completed ="Asset No:".$Assestno." 
					\r\n   Asset Desc:".$Assest_description."
					\r\n   Work Area:".$Work_Area."
					\r\n   Location:".$Assest_location."
					\r\n   Level:".$Assest_level."
					\r\n\r\n   Work Order No:".$wko_mst_wo_no."
					\r\n   WO Date:".$Originationdate."
					\r\n   WO Due Date:".$Duedate."
					\r\n   Work Order Description:".$Work_Description."
					\r\n\r\n   Completed By:".$EmpID."
					\r\n   Requested By:".$Requested_by."
					\r\n   Contact No:".$Contact_no."";
					
					$insert_email = insert_email_log($site_cd, $EmpID, $emp_det_email_id, $title, $body, $message_completed, $LOGINID);
					
					
				}
				
				
				//Insert notification_log for Work Order Completed
				$title = "Work Order Completed";
				$body = "The work order  ('".$wko_mst_wo_no."') is Completed.";
				$module = "Work Order Completed";
				$message_completed ="Asset No:".$Assestno." 
						\r\n   Asset Desc:".$Assest_description."
						\r\n   Work Area:".$Work_Area."
						\r\n   Location:".$Assest_location."
						\r\n   Level:".$Assest_level."
						\r\n\r\n   Work Order No:".$wko_mst_wo_no."
						\r\n   WO Date:".$Originationdate."
						\r\n   WO Due Date:".$Duedate."
						\r\n   Work Order Description:".$Work_Description."
						\r\n\r\n   Completed By:".$EmpID."
						\r\n   Requested By:".$Requested_by."
						\r\n   Contact No:".$Contact_no."";
				
				$insert_ntf1 = insert_notification_log($site_cd, $EmpID, $Requested_by, $wko_mst_wo_no, $title, $body, $message_completed, $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($Requested_by, $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.') is Completed',"Work Order Completed (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
						
					
					}		
					
				}	
			
			
			
			}
				
				
		}
			
			
		}else{
			
			
			if($wko_mst_status != $wko_mst_status_after){
				
				
				$sql = "UPDATE 	wko_mst SET 
						wko_mst_status = ?, 
						audit_user = ?, 
						audit_date = GETDATE()
						WHERE 	site_cd = ?
						AND 	wko_mst_wo_no = ?";								
											
				

				$params_update_wko_mst = array($wko_mst_status_after,$LOGINID,$site_cd,$wko_mst_wo_no);
				$stmt_wko_mst = sqlsrv_query( $conn, $sql,$params_update_wko_mst);

				if( !$stmt_wko_mst ) {
					$error_message = "Error UPDATE Table wko_mst";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}sqlsrv_free_stmt( $stmt_wko_mst);	
			
			
				
				
				$sql = "update wko_det 
								SET  
								wko_det_sc_date= ?,
								wko_det_cmpl_date= ?					
								WHERE	wko_det.site_cd =?
								AND		wko_det.mst_RowID = ?";									
												
								
				$params_update_wko_det = array($current_date,$current_date,$site_cd,$ROW_ID);
				$stmt_wko_det1 = sqlsrv_query( $conn,$sql,$params_update_wko_det);

				if( !$stmt_wko_det1 ) {
					$error_message = "Error UPDATE Table wko_det";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}sqlsrv_free_stmt( $stmt_wko_det1);		
				
				
				
				
				
				$sql = "update wko_sts 
						SET wko_sts_end_date =GETDATE() , 
						wko_sts_duration =0 , 
						audit_user =?, 						
						audit_date =GETDATE() 
						WHERE 
						wko_sts_end_date IS NULL 
						AND site_cd =? 
						AND wko_sts_wo_no =? 
						AND wko_sts_status =?";								
												
						
				$params_update_wko_sts = array($LOGINID,$site_cd,$wko_mst_wo_no,$wko_mst_status);		
				$stmt_wko_sts = sqlsrv_query( $conn,$sql,$params_update_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_ast_mst = "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 ) 
					VALUES	(		? ,							? ,								? ,						?,			
									? ,							getdate(),						NULL ,						NULL ,				 
									? ,							getdate())";
											
											
					
					$params_update_wko_sts = array($site_cd,$ROW_ID,$wko_mst_wo_no,$wko_mst_status_after,$LOGINID,$LOGINID);				
					$stmt_wko_sts1 = sqlsrv_query( $conn,$sql_insert_ast_mst,$params_update_wko_sts);

					if( !$stmt_wko_sts1 ) {
						$error_message = "Error insert table (wko_sts)";
						returnError($error_message);
						die( print_r( sqlsrv_errors(), true));
					}sqlsrv_free_stmt( $stmt_wko_sts1);		
				
				
				
				
				
				
				$sql_email = " Select emp_mst_empl_id, emp_det_email_id 
							from emp_mst (NOLOCK), emp_det (NOLOCK)
							Where emp_mst.site_cd = emp_det.site_cd
							And emp_mst.RowID = emp_det.mst_RowID
							And emp_mst.emp_mst_empl_id = '".$EmpID."'";
							
							
				$stmt_email = sqlsrv_query( $conn, $sql_email);	
						
				if( !$stmt_email ) {
					$error_message = "Error Select table (emp_mst,emp_det)";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}
				do {
					 while ($row = sqlsrv_fetch_array($stmt_email, SQLSRV_FETCH_ASSOC)) {
					 
					 $emp_mst_empl_id = $row['emp_mst_empl_id'];
					 $emp_det_email_id = $row['emp_det_email_id'];			
						 
				   }
				} while ( sqlsrv_next_result($stmt_email));
				
				
				if(!Empty($emp_det_email_id)){
					
					//Insert Email 
					$title = "Work Order Completed.";
					$body = "The work order  (".$wko_mst_wo_no.") is Completed.";
					$module = "Work Order";
					$message_completed ="Asset No:".$Assestno." 
					\r\n   Asset Desc:".$Assest_description."
					\r\n   Work Area:".$Work_Area."
					\r\n   Location:".$Assest_location."
					\r\n   Level:".$Assest_level."
					\r\n\r\n   Work Order No:".$wko_mst_wo_no."
					\r\n   WO Date:".$Originationdate."
					\r\n   WO Due Date:".$Duedate."
					\r\n   Work Order Description:".$Work_Description."
					\r\n\r\n   Completed By:".$EmpID."
					\r\n   Requested By:".$Requested_by."
					\r\n   Contact No:".$Contact_no."";
					
					$insert_email = insert_email_log($site_cd, $EmpID, $emp_det_email_id, $title, $body, $message_completed, $LOGINID);
					
					
				}
				
				
				//Insert notification_log for Work Order Completed
				$title = "Work Order Completed";
				$body = "The work order  ('".$wko_mst_wo_no."') is Completed.";
				$module = "Work Order Completed";
				$message_completed ="Asset No:".$Assestno." 
						\r\n   Asset Desc:".$Assest_description."
						\r\n   Work Area:".$Work_Area."
						\r\n   Location:".$Assest_location."
						\r\n   Level:".$Assest_level."
						\r\n\r\n   Work Order No:".$wko_mst_wo_no."
						\r\n   WO Date:".$Originationdate."
						\r\n   WO Due Date:".$Duedate."
						\r\n   Work Order Description:".$Work_Description."
						\r\n\r\n   Completed By:".$EmpID."
						\r\n   Requested By:".$Requested_by."
						\r\n   Contact No:".$Contact_no."";
				
				$insert_ntf1 = insert_notification_log($site_cd, $EmpID, $Requested_by, $wko_mst_wo_no, $title, $body, $message_completed, $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($Requested_by, $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.') is Completed',"Work Order Completed (".$site_cd.")",$site_cd,$tokenlist,$RowID,$row,$tkn_site,$LOGINID);			
						
					
					}		
					
				}	
			
			
			
			}
			
			
	}
	
}

/* if($insert_email || $insert_ntf || $stmt) {
	 sqlsrv_commit( $conn );
	 sqlsrv_close( $conn);	
	 returnData();	
	} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back.<br />";
	 returnError($error_message);
}  */



if($stmt_wko_det) {
	
	if($Is_checked == 'true'){
		
		
		
		if($wko_mst_status != $wko_mst_status_after){
			
			
			if($stmt_wko_mst && $stmt_wko_det1 && $stmt_wko_sts && $stmt_wko_sts1 ){
				
				sqlsrv_commit( $conn );
				sqlsrv_close( $conn);	
				returnData();
			}else{
				 sqlsrv_rollback( $conn );
				 $error_message = "Transaction rolled back check Box true .<br />";
				 returnError($error_message);
			}
				
		}
		
	}else{
			sqlsrv_commit( $conn );
			sqlsrv_close( $conn);	
			returnData();
	} 
} else {
	 sqlsrv_rollback( $conn );
	 $error_message = "Transaction rolled back check Box false.<br />";
	 returnError($error_message);
} 


 
function returnData(){
	$returnData = array(
	'status' => 'SUCCESS',	
	'message' => 'Update Successfully');

	echo json_encode($returnData);
}

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




?>