<?php
require_once('config.php');
$error_message;
$valid = true;

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


foreach ($result->Header as $Header){
	
 $site_cd = $Header->site_cd;
 $ast_dwntime_asset_no = $Header->ast_dwntime_asset_no;
 $ast_dwntime_downtime = $Header->ast_dwntime_downtime;
 $ast_dwntime_down_wo = $Header->ast_dwntime_down_wo;
 $ast_dwntime_out_date = $Header->ast_dwntime_out_date;
 $ast_dwntime_remark = $Header->ast_dwntime_remark;
 $ast_dwntime_repairtime = $Header->ast_dwntime_repairtime;
 $ast_dwntime_repair_from = $Header->ast_dwntime_repair_from;
 $ast_dwntime_repair_to = $Header->ast_dwntime_repair_to;
 $ast_dwntime_rts_date = $Header->ast_dwntime_rts_date;
 $ast_dwntime_sched_flag = $Header->ast_dwntime_sched_flag;
 $ast_dwntime_status_down = $Header->ast_dwntime_status_down;
 $ast_dwntime_status_original = $Header->ast_dwntime_status_original;
 $ast_dwntime_status_up = $Header->ast_dwntime_status_up;
 $ast_dwntime_up_wo = $Header->ast_dwntime_up_wo;
 $EmpID = $Header->EmpID;
 $LogingID = $Header->LOGINID;
 $RowID = $Header->RowID;
 
	if(!empty($RowID)){
		 
		 //echo('NOT EMPTY');
		 
		 //Update
		If (!empty($ast_dwntime_out_date) && !empty($ast_dwntime_rts_date) ){
	
	
			//update ast_dwntime
			$current_datetime = date("Y-m-d h:i:s");
	
			$sql_update_ast_dwntime = "Update ast_dwntime 
			
									Set 
										ast_dwntime_up_wo=?,
										ast_dwntime_out_date=?,
										ast_dwntime_rts_date = ?,
										ast_dwntime_downtime = ?,		
										ast_dwntime_repair_from = ?,
										ast_dwntime_repair_to = ?,
										ast_dwntime_repairtime = ?,
										ast_dwntime_remark=?,
										ast_dwntime_sched_flag=?,
										column1=NULL,
										audit_user = ?,
										audit_date = Getdate(),
										mbl_audit_user = ?, 
										mbl_audit_date = GetDate ()
								Where	rowid = ?";

			$params_update_ast_dwntime = array($ast_dwntime_up_wo,	$ast_dwntime_out_date,	$ast_dwntime_rts_date,	$ast_dwntime_downtime,	
			$ast_dwntime_repair_from, 	$ast_dwntime_repair_to, 	$ast_dwntime_repairtime, $ast_dwntime_remark,$ast_dwntime_sched_flag,
				$LogingID,$LogingID, $RowID);								
					
			$stmt_update_ast_dwntime = sqlsrv_query( $conn, $sql_update_ast_dwntime, $params_update_ast_dwntime);			
	
			if( !$stmt_update_ast_dwntime ) {
				$error_message = "Error update table (ast_dwntime)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}
			sqlsrv_free_stmt( $stmt_update_ast_dwntime);
	
		}else{
			if (!empty($ast_dwntime_out_date) && empty($ast_dwntime_rts_date)){
	
		
		
				//get ast_mst_rowid
				$sql_get_ast_mst_rowid = "Select 	ast_mst.rowid from		ast_mst (NOLOCK) Where	ast_mst.ast_mst_asset_no = ? And		ast_mst.site_cd = ?";	
					
				$params_get_ast_mst_rowid = array($ast_dwntime_asset_no,$site_cd);
						
				$stmt_get_ast_mst_rowid = sqlsrv_query( $conn, $sql_get_ast_mst_rowid, $params_get_ast_mst_rowid);	
				if( !$stmt_get_ast_mst_rowid ) {
					$error_message = "Error select table rowid (ast_mst)";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}
				
				do {
					while ($row = sqlsrv_fetch_array($stmt_get_ast_mst_rowid, SQLSRV_FETCH_ASSOC)) {	 
					$ast_mst_rowid = $row['rowid'];		 
					}
				} while ( sqlsrv_next_result($stmt_get_ast_mst_rowid));
				
				sqlsrv_free_stmt( $stmt_get_ast_mst_rowid);
		
	
		
		
	
				$sql_update_ast_dwntime = "Update ast_dwntime 
									Set ast_dwntime_out_date =?,
										ast_dwntime_status_up =NULL,
										ast_dwntime_rts_date = NULL,
										ast_dwntime_downtime = ?,	
										ast_dwntime_up_wo=?,								
										ast_dwntime_repair_from = ?,
										ast_dwntime_repair_to = NULL,
										ast_dwntime_repairtime = ?,
										ast_dwntime_remark=?,
										ast_dwntime_sched_flag=?,
										column1=NULL,
										audit_user = ?,
										audit_date = Getdate(),
										mbl_audit_user = ?, 
										mbl_audit_date = GetDate ()
								Where	rowid = ?";

				$params_update_ast_dwntime = array(	$ast_dwntime_out_date,$ast_dwntime_downtime, $ast_dwntime_up_wo,	$ast_dwntime_repair_from,  	$ast_dwntime_repairtime, 	$ast_dwntime_remark,$ast_dwntime_sched_flag,$LogingID,$LogingID, $RowID);									
						
				$stmt_update_ast_dwntime = sqlsrv_query( $conn, $sql_update_ast_dwntime, $params_update_ast_dwntime);			
				
				if( !$stmt_update_ast_dwntime ) {
					$error_message = "Error update table (ast_dwntime)";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}
				sqlsrv_free_stmt( $stmt_update_ast_dwntime);
			}
		}

	}else{
		  //echo(' EMPTY');
		  
		If (!empty($ast_dwntime_out_date) && empty($ast_dwntime_rts_date)){

	
			//get ast_mst_rowid
			$sql_get_ast_mst_rowid = "Select 	ast_mst.rowid from		ast_mst (NOLOCK) Where	ast_mst.ast_mst_asset_no = ? And		ast_mst.site_cd = ?";	
				
			$params_get_ast_mst_rowid = array($ast_dwntime_asset_no,$site_cd);
					
			$stmt_get_ast_mst_rowid = sqlsrv_query( $conn, $sql_get_ast_mst_rowid, $params_get_ast_mst_rowid);	
			if( !$stmt_get_ast_mst_rowid ) {
				$error_message = "Error select table rowid (ast_mst)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}
			
			do {
				while ($row = sqlsrv_fetch_array($stmt_get_ast_mst_rowid, SQLSRV_FETCH_ASSOC)) {	 
				$ast_mst_rowid = $row['rowid'];		 
				}
			} while ( sqlsrv_next_result($stmt_get_ast_mst_rowid));
			
			sqlsrv_free_stmt( $stmt_get_ast_mst_rowid);
	
		
			//Insert ast_dwntime
			$sql_insert_ast_dwntime = "Insert into ast_dwntime		
												(	site_cd,					mst_rowid,				ast_dwntime_out_date,			ast_dwntime_rts_date,			ast_dwntime_downtime,	
													ast_dwntime_repair_from,	ast_dwntime_repair_to,	ast_dwntime_repairtime,			ast_dwntime_down_wo,			ast_dwntime_up_wo,
													ast_dwntime_status_original,ast_dwntime_status_down,ast_dwntime_status_up,			ast_dwntime_asset_no,			ast_dwntime_remark,			
													ast_dwntime_sched_flag,		audit_user,				audit_date,						mbl_audit_user,						mbl_audit_date)
										Values	(	?,							?,						?,								NULL,							NULL,						
													?,							NULL,					NULL,							?,								NULL,				
													NULL,						NULL,					NULL,							?,								?,
													?,							?,						getdate(),						?,								getdate())";
														
										
			$params_insert_ast_dwntime = array(		$site_cd,					$ast_mst_rowid,			$ast_dwntime_out_date,			
													$ast_dwntime_repair_from ,															$ast_dwntime_down_wo,			 
																																		$ast_dwntime_asset_no,			$ast_dwntime_remark,
													$ast_dwntime_sched_flag,	$LogingID,$LogingID);						
											
			$stmt_insert_ast_dwntime = sqlsrv_query( $conn, 	$sql_insert_ast_dwntime,	$params_insert_ast_dwntime);	
			
			if( !$stmt_insert_ast_dwntime ) {
				$error_message = "Error insert table (ast_dwntime)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}	
			sqlsrv_free_stmt($stmt_insert_ast_dwntime);	
	
		}else{	
		
			if (!empty($ast_dwntime_out_date) && !empty($ast_dwntime_rts_date)){
		
		
		
				//get ast_mst_rowid
				$sql_get_ast_mst_rowid = "Select 	ast_mst.rowid from		ast_mst (NOLOCK) Where	ast_mst.ast_mst_asset_no = ? And		ast_mst.site_cd = ?";	
					
				$params_get_ast_mst_rowid = array($ast_dwntime_asset_no,$site_cd);
						
				$stmt_get_ast_mst_rowid = sqlsrv_query( $conn, $sql_get_ast_mst_rowid, $params_get_ast_mst_rowid);	
				if( !$stmt_get_ast_mst_rowid ) {
					$error_message = "Error select table rowid (ast_mst)";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}
				
				do {
					while ($row = sqlsrv_fetch_array($stmt_get_ast_mst_rowid, SQLSRV_FETCH_ASSOC)) {	 
					$ast_mst_rowid = $row['rowid'];		 
					}
				} while ( sqlsrv_next_result($stmt_get_ast_mst_rowid));
				
				sqlsrv_free_stmt( $stmt_get_ast_mst_rowid);
		
	
		
				//Insert ast_dwntime
				$sql_insert_ast_dwntime = "Insert into ast_dwntime		
							(			site_cd,					mst_rowid,				ast_dwntime_out_date,			ast_dwntime_rts_date,			ast_dwntime_downtime,	
										ast_dwntime_repair_from,	ast_dwntime_repair_to,	ast_dwntime_repairtime,			ast_dwntime_down_wo,			ast_dwntime_up_wo,
										ast_dwntime_status_original,ast_dwntime_status_down,ast_dwntime_status_up,			ast_dwntime_asset_no,			ast_dwntime_remark,			
										ast_dwntime_sched_flag,		audit_user,					audit_date,					mbl_audit_user,						mbl_audit_date)
							Values	(	?,							?,						?,								?,								?,						
										?,							?,						?,								?,								?,				
										NULL,						NULL,					NULL,							?,								?,
										?,							?,						getdate(),						?,								getdate())";
											
											
				$params_insert_ast_dwntime = array(	
										$site_cd,					$ast_mst_rowid,			$ast_dwntime_out_date,			$ast_dwntime_rts_date,			$ast_dwntime_downtime,
										$ast_dwntime_repair_from,	$ast_dwntime_repair_to,	$ast_dwntime_repairtime,		$ast_dwntime_down_wo,			$ast_dwntime_up_wo,
																															$ast_dwntime_asset_no,			$ast_dwntime_remark,
										$ast_dwntime_sched_flag,	$LogingID,$LogingID);						
												
				$stmt_insert_ast_dwntime = sqlsrv_query( $conn, 	$sql_insert_ast_dwntime,	$params_insert_ast_dwntime);	
				
				if( !$stmt_insert_ast_dwntime ) {
					$error_message = "Error insert table (ast_dwntime)";
					returnError($error_message);
					die( print_r( sqlsrv_errors(), true));
				}	
				sqlsrv_free_stmt($stmt_insert_ast_dwntime);	
			}
		}
	}


}
 
sqlsrv_commit( $conn );
sqlsrv_close( $conn);	
returnData();
 
function returnData(){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Asset downtime insert successfully');
	echo json_encode($returnData);
}

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



 
 
?>