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

function wo_required($site_cd, $wko_mst_status_after, $ROW_ID,$wko_mst_type){
	global $conn;
	
	
	
	$now = date("Ymdhisa");
	$file_name = __DIR__ . "\mobile_log\_update_action_workorder.log";
	

	$sql_wrk_sts = "Select 	wrk_sts_tim_crd,wrk_sts_cm_ack_req,wrk_sts_pm_ack_req 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 === false)
	{
		if (($errors = sqlsrv_errors()) != null)
		{
			foreach ($errors as $error)
			{
				file_put_contents($file_name, $now . "\r\n" . $sql_wrk_sts . "\r\n" . "SQLSTATE: " . $error['SQLSTATE'] . "\r\n" . "code: " . $error['code'] . "\r\n" . "message: " . $error['message'] . "\r\n\r\n", FILE_APPEND);
			}
		}

		sqlsrv_rollback($conn);
		sqlsrv_close($conn);

		$error_message = "Error selecting table (wrk_sts) - ".$now;
		returnError($error_message);
	}
	do
	{
		while ($row = sqlsrv_fetch_array($stmt_wrk_sts, SQLSRV_FETCH_ASSOC))
		{

			$wrk_sts_tim_crd = $row['wrk_sts_tim_crd'];
			$wrk_sts_cm_ack_req = $row['wrk_sts_cm_ack_req'];
			$wrk_sts_pm_ack_req = $row['wrk_sts_pm_ack_req'];

		}
	}
	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 === false)
		{
			if (($errors = sqlsrv_errors()) != null)
			{
				foreach ($errors as $error)
				{
					file_put_contents($file_name, $now . "\r\n" . $sql_wrk_sts . "\r\n" . "SQLSTATE: " . $error['SQLSTATE'] . "\r\n" . "code: " . $error['code'] . "\r\n" . "message: " . $error['message'] . "\r\n\r\n", FILE_APPEND);
				}
			}

			sqlsrv_rollback($conn);
			sqlsrv_close($conn);

			$error_message = "Error selecting table (wko_ls8) - ".$now;
			returnError($error_message);
		}

		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")
		{
						
			return 'Time card is required before completion.';
		}
		else
		{	
			if($wko_mst_type == 'G' || $wko_mst_type == 'P'){
				
				if ($wrk_sts_pm_ack_req == "1")
				{
					$sql_wrk_sts = "Select wko_det_ack_name = CASE WHEN wko_det_ack_name IS NOT NULL Then '1' Else '0' End from wko_det (NOLOCK) Where site_cd = '".$site_cd ."' And mst_RowID = '".$ROW_ID ."'";
					$stmt_wrk_sts = sqlsrv_query($conn, $sql_wrk_sts);

					if ($stmt_wrk_sts === false)
					{
						if (($errors = sqlsrv_errors()) != null)
						{
							foreach ($errors as $error)
							{
								file_put_contents($file_name, $now . "\r\n" . $sql_wrk_sts . "\r\n" . "SQLSTATE: " . $error['SQLSTATE'] . "\r\n" . "code: " . $error['code'] . "\r\n" . "message: " . $error['message'] . "\r\n\r\n", FILE_APPEND);
							}
						}

						sqlsrv_rollback($conn);
						sqlsrv_close($conn);

						$error_message = "Error selecting table (wko_ls8) - ".$now;
						returnError($error_message);
					}

					do
					{
						while ($row = sqlsrv_fetch_array($stmt_wrk_sts, SQLSRV_FETCH_ASSOC))
						{

							$wko_det_ack_name = $row['wko_det_ack_name'];

						}
					}
					while (sqlsrv_next_result($stmt_wrk_sts));
					
					if ($wko_det_ack_name == "0")
					{

						return 'Acknowledgement is required before completion.';

					}
					else
					{
						return 'Ok';
					}
					
				}else{
					
					return 'Ok';
				}
				
				
			}
			else
			{
				
				if ($wrk_sts_cm_ack_req == "1")
				{
					$sql_wrk_sts = "Select wko_det_ack_name = CASE WHEN wko_det_ack_name IS NOT NULL Then '1' Else '0' End from wko_det (NOLOCK) Where site_cd = '".$site_cd ."' And mst_RowID = '".$ROW_ID ."'";
					$stmt_wrk_sts = sqlsrv_query($conn, $sql_wrk_sts);

					if ($stmt_wrk_sts === false)
					{
						if (($errors = sqlsrv_errors()) != null)
						{
							foreach ($errors as $error)
							{
								file_put_contents($file_name, $now . "\r\n" . $sql_wrk_sts . "\r\n" . "SQLSTATE: " . $error['SQLSTATE'] . "\r\n" . "code: " . $error['code'] . "\r\n" . "message: " . $error['message'] . "\r\n\r\n", FILE_APPEND);
							}
						}

						sqlsrv_rollback($conn);
						sqlsrv_close($conn);

						$error_message = "Error selecting table (wko_ls8) - ".$now;
						returnError($error_message);
					}

					do
					{
						while ($row = sqlsrv_fetch_array($stmt_wrk_sts, SQLSRV_FETCH_ASSOC))
						{

							$wko_det_ack_name = $row['wko_det_ack_name'];

						}
					}
					while (sqlsrv_next_result($stmt_wrk_sts));
					
					if ($wko_det_ack_name == "0")
					{

						return 'Acknowledgement is required before completion.';

					}
					else
					{
						return 'Ok';
					}
				
				}
				else
				{
				
					return 'Ok';
				}
				
			}
			
			
		}
		
	}
	else
	{
		if($wko_mst_type =='G' || $wko_mst_type =='P'){
				
			if ($wrk_sts_pm_ack_req == "1")
			{
				$sql_wrk_sts = "Select wko_det_ack_name = CASE WHEN wko_det_ack_name IS NOT NULL Then '1' Else '0' End from wko_det (NOLOCK) Where site_cd = '".$site_cd ."' And mst_RowID = '".$ROW_ID ."'";
				$stmt_wrk_sts = sqlsrv_query($conn, $sql_wrk_sts);

				if ($stmt_wrk_sts === false)
				{
					if (($errors = sqlsrv_errors()) != null)
					{
						foreach ($errors as $error)
						{
							file_put_contents($file_name, $now . "\r\n" . $sql_wrk_sts . "\r\n" . "SQLSTATE: " . $error['SQLSTATE'] . "\r\n" . "code: " . $error['code'] . "\r\n" . "message: " . $error['message'] . "\r\n\r\n", FILE_APPEND);
						}
					}

					sqlsrv_rollback($conn);
					sqlsrv_close($conn);

					$error_message = "Error selecting table (wko_ls8) - ".$now;
					returnError($error_message);
				}

				do
				{
					while ($row = sqlsrv_fetch_array($stmt_wrk_sts, SQLSRV_FETCH_ASSOC))
					{

						$wko_det_ack_name = $row['wko_det_ack_name'];

					}
				}
				while (sqlsrv_next_result($stmt_wrk_sts));
				
				if ($wko_det_ack_name == "0")
				{

					return 'Acknowledgement is required before completion.';

				}
				else
				{
					return 'Ok';
				}
				
			}else{
				
				return 'Ok';
			}
			
			
		}
		else
		{
			
			if ($wrk_sts_cm_ack_req == "1")
			{
				$sql_wrk_sts = "Select wko_det_ack_name = CASE WHEN wko_det_ack_name IS NOT NULL Then '1' Else '0' End from wko_det (NOLOCK) Where site_cd = '".$site_cd ."' And mst_RowID = '".$ROW_ID ."'";
				$stmt_wrk_sts = sqlsrv_query($conn, $sql_wrk_sts);

				if ($stmt_wrk_sts === false)
				{
					if (($errors = sqlsrv_errors()) != null)
					{
						foreach ($errors as $error)
						{
							file_put_contents($file_name, $now . "\r\n" . $sql_wrk_sts . "\r\n" . "SQLSTATE: " . $error['SQLSTATE'] . "\r\n" . "code: " . $error['code'] . "\r\n" . "message: " . $error['message'] . "\r\n\r\n", FILE_APPEND);
						}
					}

					sqlsrv_rollback($conn);
					sqlsrv_close($conn);

					$error_message = "Error selecting table (wko_ls8) - ".$now;
					returnError($error_message);
				}

				do
				{
					while ($row = sqlsrv_fetch_array($stmt_wrk_sts, SQLSRV_FETCH_ASSOC))
					{

						$wko_det_ack_name = $row['wko_det_ack_name'];

					}
				}
				while (sqlsrv_next_result($stmt_wrk_sts));
				
				if ($wko_det_ack_name == "0")
				{

					return 'Acknowledgement is required before completion.';

				}
				else
				{
					return 'Ok';
				}
			
			}
			else
			{
			
				return 'Ok';
			}
			
		}
		
	}
	
}
?>