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

$error_message;
$valid = true;

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

$time_card_nos ;

foreach($result->Header as $Heade){
	
	$site_cd= $Heade->site_cd;
	$wko_mst_wo_no= $Heade->wko_mst_wo_no;
	$mst_RowID= $Heade->mst_RowID;
	$assetno= $Heade->assetno;	
	$originator= $Heade->originator;
	$craft= $Heade->craft;
	$hourtype= $Heade->hourtype;
	$total_hrs= $Heade->total_hrs;
	$rate= $Heade->rate;
	$multiplier= $Heade->multiplier;	
	$adder= $Heade->adder;
	$act_cost= $Heade->act_cost;
	$chg_costcenter= $Heade->chg_costcenter;
	$chg_account= $Heade->chg_account;
	$time_card_no= $Heade->time_card_no;
	$datetime1= $Heade->datetime1;	
	$audit_user= $Heade->audit_user;
	$audit_date= $Heade->audit_date;
	$datetime2= $Heade->datetime2;
	$RowId= $Heade->RowId;
	
	$time_card_nos = "";
	
	if(empty($datetime2))
	{
	// No time card
	}
	Else
	{
			
	$sql = "	SELECT 	ls_doc_no = cnt_mst_prefix + SUBSTRING(CONVERT(VARCHAR(7), cnt_mst_counter + 1000000), 2, 6)
				FROM 	cnt_mst WITH (UPDLOCK)
				WHERE 	site_cd ='".$site_cd."'
				AND 	cnt_mst_module_cd =  'TM'
				
				UPDATE 	cnt_mst WITH (UPDLOCK)
				SET 	cnt_mst_counter = cnt_mst_counter + 1
				WHERE 	site_cd ='".$site_cd."' 
				AND 	cnt_mst_module_cd = 'TM'";			
			
			
	$stmt = sqlsrv_query( $conn, $sql);			
	if( !$stmt ) 
		{
		$error_message = "Error update table (cnt_mst)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
		}
	do {
		while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) 
		{	
		
		$time_card_nos = $row['ls_doc_no'];				
		//echo $time_card_nos;
				 
		}
		} while ( sqlsrv_next_result($stmt));
		
		
	$sql_insert_tim_crd = "INSERT tim_crd 
	
		(	site_cd ,					tim_crd_card_no ,			tim_crd_date ,			tim_crd_empl_id ,			tim_crd_craft ,
			tim_crd_hours_type , 		tim_crd_hrs ,				tim_crd_rate ,			tim_crd_multiplier ,		tim_crd_adder ,				tim_crd_act_cost ,			tim_crd_wo , 				tim_crd_assetno ,		tim_crd_chg_costcenter ,	tim_crd_chg_account ,	tim_crd_crd_costcenter,		tim_crd_crd_account ,		tim_crd_status , 		tim_crd_create_by ,			tim_crd_create_date ,		audit_user ,				audit_date )
			
	VALUES ('".$site_cd."',				'".$time_card_nos."',		'".$datetime1."',		'".$originator."',			'".$craft."',				
			'".$hourtype."',			'".$total_hrs."',			'".$rate."',			'".$multiplier."',			'".$adder."',				
			'".$act_cost."',			'".$wko_mst_wo_no."',		'".$assetno."',			'".$chg_costcenter."',		'".$chg_account."',		NULL,						NULL,						'P',					'".$audit_user."',			GetDate(),					
			'".$audit_user."',			GetDate())";
								
	$stmt = sqlsrv_query( $conn, $sql_insert_tim_crd);

	if( !$stmt ) {
		$error_message = "Error (INSERT Table tim_crd)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}	
	sqlsrv_free_stmt( $stmt);	
	
	//$charge_valid = f_update_budget($datetime1, $chg_costcenter, $chg_account, "CHARGE", $act_cost, $conn, $audit_user);
	//$credit_valid = f_update_budget($datetime1, $itm_mst_crd_costcenter "CHARGE", $act_cost);
	
	$sql= " SELECT 	rowid 
			FROM 	ast_mst (NOLOCK) 
			WHERE 	site_cd =? 
			AND 	ast_mst_asset_no =? ";	
			
	//echo $sql;
	$params = array($site_cd,$assetno);					
							
	$stmt = sqlsrv_query( $conn, $sql,$params);

	if( !$stmt ) {
		$error_message = "Error SELECT Table (crf_mst)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}


	do {
		 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {	
		 
			$astrowid = $row['rowid'];
			
		 }
	} while ( sqlsrv_next_result($stmt) );


	$sql1 = "	UPDATE 	ast_det SET ast_det_ltdlabcost =ast_det_ltdlabcost + '".$act_cost."'					
				WHERE 	ast_det.site_cd = ? 
				AND 	ast_det.mst_rowid = ? ";

	//echo $sql;
	$params1 = array($site_cd, $astrowid);	
	$stmt1 = sqlsrv_query( $conn, $sql1, $params1);	
				
	if( !$stmt1 ) {
		$error_message = "Error update table (UPDATE ast_det)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}sqlsrv_free_stmt( $stmt1);
		
	
	$sql1 = "	UPDATE 	wko_det 
				SET 	wko_det_lab_cost = wko_det_lab_cost + '".$act_cost."'	
				FROM	wko_mst,
						wko_det
				WHERE 	wko_mst.site_cd = wko_det.site_cd
				AND		wko_mst.RowID = wko_det.mst_RowID
				AND		wko_mst.site_cd = ? 
				AND 	wko_mst.wko_mst_wo_no = ? ";
	
	//echo $sql;
	
	$params1 = array($site_cd, $wko_mst_wo_no);	
	$stmt1 = sqlsrv_query( $conn, $sql1, $params1);	
				
	if( !$stmt1 ) {
		$error_message = "Error update table (UPDATE wko_det)";
		returnError($error_message);
		die( print_r( sqlsrv_errors(), true));
	}sqlsrv_free_stmt( $stmt1);
			
	}

	
	
	if(empty($RowId)){
		
		if(empty($datetime2)){
			
				$datetimetest = 'Null';
		}
		else
		{
				$datetimetest = "'".$datetime2."'";
		}
		
		
			$sql_insert_wko_ls8 = "INSERT wko_ls8 
				
					(	site_cd ,					mst_RowID,					wko_ls8_assetno,				wko_ls8_empl_id,				
						wko_ls8_craft,				wko_ls8_hours_type ,		wko_ls8_hrs ,					wko_ls8_rate ,
						wko_ls8_multiplier ,		wko_ls8_adder ,				wko_ls8_act_cost ,				wko_ls8_chg_costcenter ,
						wko_ls8_chg_account ,		wko_ls8_crd_costcenter ,	wko_ls8_crd_account ,			wko_ls8_time_card_no ,
						wko_ls8_datetime1 ,			audit_user ,				audit_date ,					wko_ls8_datetime2  ) 
						
			VALUES 	(	'".$site_cd."',				'".$mst_RowID."',			'".$assetno."',					'".$originator."',		
						'".$craft."',				'".$hourtype."',			'".$total_hrs."',				'".$rate."',							
						'".$multiplier."',			'".$adder."',				'".$act_cost."',				'".$chg_costcenter."',						
						'".$chg_account."',			Null,						Null,							'".$time_card_nos."',	
						'".$datetime1."',			'".$audit_user."',			'".$audit_date."',				".$datetimetest.")";
						
			$stmt = sqlsrv_query( $conn, $sql_insert_wko_ls8);

			if( !$stmt ) {
				$error_message = "Error (INSERT Table wko_ls8)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}	
			sqlsrv_free_stmt( $stmt);	
			
		}
		
		Else // RowID Not Empty
			
		{
			
			if(empty($datetime2)){
		
			$datetimetest = 'Null';
			}			
			else
			{
			$datetimetest = "'".$datetime2."'";;
			}
	
			$sql1 = "UPDATE wko_ls8 SET
							
							wko_ls8_assetno ='".$assetno."',		
							wko_ls8_empl_id='".$originator."',		
							wko_ls8_craft='".$craft."',			
							wko_ls8_hours_type='".$hourtype."',		
							wko_ls8_hrs='".$total_hrs."',			
							wko_ls8_rate='".$rate."',			
							wko_ls8_multiplier='".$multiplier."',		
							wko_ls8_adder='".$adder."',			
							wko_ls8_act_cost='".$act_cost."',		  
							wko_ls8_chg_costcenter='".$chg_costcenter."', 
							wko_ls8_chg_account='".$chg_account."',	
							wko_ls8_datetime1='".$datetime1."',		
							wko_ls8_datetime2=".$datetimetest.",
							wko_ls8_time_card_no ='".$time_card_nos."'
							WHERE 	site_cd = ? 
							AND 	Rowid = ? ;	";
	
				//echo $sql;
				$params1 = array($site_cd, $RowId);	
				$stmt1 = sqlsrv_query( $conn, $sql1, $params1);	
					
			if( !$stmt1 ) {
				$error_message = "Error -1 update table (UPDATE wko_ls8)";
				returnError($error_message);
				die( print_r( sqlsrv_errors(), true));
			}sqlsrv_free_stmt( $stmt1);
			
			
		}
		
}/* 1st lopp */


if ($valid) {	
	returnData($json);	
}

//sqlsrv_free_stmt( $stmt);
sqlsrv_close($conn);

function returnData($json){
	
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Time Card Saved Successfully');
	
	echo json_encode($returnData);
}

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