<?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');
require_once ('f_logging.php');

$error_message;
$valid = true;
$insert_email = false;
$insert_ntf = false;
$now = date("Ymdhisa");
$file_name = __DIR__ . "\mobile_log\_update_action_workorder.log";

/* 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'];
$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_action_workorder.php", $sync_url, $LOGINID, $sync_step, $sync_time, $site_cd);
}

$EmpID = $data['EmpID'];
$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_det_note1 = $data['wko_det_note1'];
$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'];

$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_note1 =?,
				wko_det.audit_user =?,						
				wko_det.audit_date=GETDATE(),
				wko_det.mbl_audit_user =?,						
				wko_det.mbl_audit_date=GETDATE()
		WHERE	wko_det.site_cd =?
		AND		wko_det.mst_RowID =?";

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

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

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

    $error_message = "Error updating table (wko_det)";
    returnError($error_message);
}
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 === 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'];

        }
    }
    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")
        {

            sqlsrv_rollback($conn);
            $error_message = "Please enter time card before 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(),
						mbl_audit_user = ?, 
						mbl_audit_date = GETDATE()
						WHERE 	site_cd = ?
						AND 	wko_mst_wo_no = ?";

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

                if ($stmt_wko_mst === false)
                {
                    if (($errors = sqlsrv_errors()) != null)
                    {
                        foreach ($errors as $error)
                        {
                            file_put_contents($file_name, $now . "\r\n" . $sql . "\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 updating table (wko_mst) - ".$now;
                    returnError($error_message);
                }

                sqlsrv_free_stmt($stmt_wko_mst);

                $sql = "update wko_det 
								SET  
								wko_det_sc_date= ?,
								wko_det_cmpl_date= ?,
								mbl_audit_user = ?, 
								mbl_audit_date = GETDATE()								
								WHERE	wko_det.site_cd =?
								AND		wko_det.mst_RowID = ?";

                $params_update_wko_det = array(
                    $current_date,
                    $current_date,
					$LOGINID,
                    $site_cd,
                    $ROW_ID
                );
                $stmt_wko_det1 = sqlsrv_query($conn, $sql, $params_update_wko_det);

                if ($stmt_wko_det1 === false)
                {
                    if (($errors = sqlsrv_errors()) != null)
                    {
                        foreach ($errors as $error)
                        {
                            file_put_contents($file_name, $now . "\r\n" . $sql . "\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 updating table (wko_det) - ".$now;
                    returnError($error_message);
                }

                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(),
						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 =?";

                $params_update_wko_sts = array(
                    $LOGINID,
                    $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 === false)
                {
                    if (($errors = sqlsrv_errors()) != null)
                    {
                        foreach ($errors as $error)
                        {
                            file_put_contents($file_name, $now . "\r\n" . $sql . "\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 updating table (wko_sts) - ".$now;
                    returnError($error_message);
                }
                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,						mbl_audit_user,			mbl_audit_date ) 
					VALUES	(		? ,							? ,								? ,						?,			
									? ,							getdate(),						NULL ,					NULL ,				 
									? ,							getdate(),						? ,						getdate())";

                $params_update_wko_sts = array(
                    $site_cd,
                    $ROW_ID,
                    $wko_mst_wo_no,
                    $wko_mst_status_after,
                    $LOGINID,
                    $LOGINID,
                    $LOGINID
                );
                $stmt_wko_sts1 = sqlsrv_query($conn, $sql_insert_ast_mst, $params_update_wko_sts);

                if ($stmt_wko_sts1 === false)
                {
                    if (($errors = sqlsrv_errors()) != null)
                    {
                        foreach ($errors as $error)
                        {
                            file_put_contents($file_name, $now . "\r\n" . $sql_insert_ast_mst . "\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 inserting table (wko_sts) - ".$now;
                    returnError($error_message);
                }

                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 === false)
                {
                    if (($errors = sqlsrv_errors()) != null)
                    {
                        foreach ($errors as $error)
                        {
                            file_put_contents($file_name, $now . "\r\n" . $sql_email . "\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 (emp_mst,emp_det) - ".$now;
                    returnError($error_message);
                }

                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\nAsset Desc:" . $Assest_description . "
					\r\nWork Area:" . $Work_Area . "
					\r\nLocation:" . $Assest_location . "
					\r\nLevel:" . $Assest_level . "
					\r\n\r\nWork Order No:" . $wko_mst_wo_no . "
					\r\nWO Date:" . $Originationdate . "
					\r\nWO Due Date:" . $Duedate . "
					\r\nWork Order Description:" . $Work_Description . "
					\r\n\r\nCompleted By:" . $EmpID . "
					\r\nRequested By:" . $Requested_by . "
					\r\nContact 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\nAsset Desc:" . $Assest_description . "
					\r\nWork Area:" . $Work_Area . "
					\r\nLocation:" . $Assest_location . "
					\r\nLevel:" . $Assest_level . "
					\r\n\r\nWork Order No:" . $wko_mst_wo_no . "
					\r\nWO Date:" . $Originationdate . "
					\r\nWO Due Date:" . $Duedate . "
					\r\nWork Order Description:" . $Work_Description . "
					\r\n\r\nCompleted By:" . $EmpID . "
					\r\nRequested By:" . $Requested_by . "
					\r\nContact 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(),
						mbl_audit_user = ?, 
						mbl_audit_date = GETDATE()
						WHERE 	site_cd = ?
						AND 	wko_mst_wo_no = ?";

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

            if ($stmt_wko_mst === false)
            {
                if (($errors = sqlsrv_errors()) != null)
                {
                    foreach ($errors as $error)
                    {
                        file_put_contents($file_name, $now . "\r\n" . $sql . "\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 updating table (wko_mst) - ".$now;
                returnError($error_message);
            }

            sqlsrv_free_stmt($stmt_wko_mst);

            $sql = "update wko_det 
								SET  
								wko_det_sc_date= ?,
								wko_det_cmpl_date= ?,
								mbl_audit_user = ?, 
								mbl_audit_date = GETDATE()								
								WHERE	wko_det.site_cd =?
								AND		wko_det.mst_RowID = ?";

            $params_update_wko_det = array(
                $current_date,
                $current_date,
				$LOGINID,
                $site_cd,
                $ROW_ID
            );
            $stmt_wko_det1 = sqlsrv_query($conn, $sql, $params_update_wko_det);

            if ($stmt_wko_det1 === false)
            {
                if (($errors = sqlsrv_errors()) != null)
                {
                    foreach ($errors as $error)
                    {
                        file_put_contents($file_name, $now . "\r\n" . $sql . "\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 updating table (wko_det) - ".$now;
                returnError($error_message);
            }

            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(),
						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 =?";

            $params_update_wko_sts = array(
                $LOGINID,
                $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 === false)
            {
                if (($errors = sqlsrv_errors()) != null)
                {
                    foreach ($errors as $error)
                    {
                        file_put_contents($file_name, $now . "\r\n" . $sql . "\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 updating table (wko_sts) - ".$now;
                returnError($error_message);
            }
            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,						mbl_audit_user,			mbl_audit_date ) 
					VALUES	(		? ,							? ,								? ,						?,			
									? ,							getdate(),						NULL ,						NULL ,				 
									? ,							getdate(),						? ,							getdate())";

            $params_update_wko_sts = array(
                $site_cd,
                $ROW_ID,
                $wko_mst_wo_no,
                $wko_mst_status_after,
                $LOGINID,
                $LOGINID,
                $LOGINID
            );
            $stmt_wko_sts1 = sqlsrv_query($conn, $sql_insert_ast_mst, $params_update_wko_sts);

            if ($stmt_wko_sts1 === false)
            {
                if (($errors = sqlsrv_errors()) != null)
                {
                    foreach ($errors as $error)
                    {
                        file_put_contents($file_name, $now . "\r\n" . $sql_insert_ast_mst . "\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 inserting table (wko_sts) - ".$now;
                returnError($error_message);
            }

            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 === false)
            {
                if (($errors = sqlsrv_errors()) != null)
                {
                    foreach ($errors as $error)
                    {
                        file_put_contents($file_name, $now . "\r\n" . $sql_email . "\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 (emp_mst,emp_det) - ".$now;
                returnError($error_message);
            }

            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\nAsset Desc:" . $Assest_description . "
				\r\nWork Area:" . $Work_Area . "
				\r\nLocation:" . $Assest_location . "
				\r\nLevel:" . $Assest_level . "
				\r\n\r\nWork Order No:" . $wko_mst_wo_no . "
				\r\nWO Date:" . $Originationdate . "
				\r\nWO Due Date:" . $Duedate . "
				\r\nWork Order Description:" . $Work_Description . "
				\r\n\r\nCompleted By:" . $EmpID . "
				\r\nRequested By:" . $Requested_by . "
				\r\nContact 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\nAsset Desc:" . $Assest_description . "
				\r\nWork Area:" . $Work_Area . "
				\r\nLocation:" . $Assest_location . "
				\r\nLevel:" . $Assest_level . "
				\r\n\r\nWork Order No:" . $wko_mst_wo_no . "
				\r\nWO Date:" . $Originationdate . "
				\r\nWO Due Date:" . $Duedate . "
				\r\nWork Order Description:" . $Work_Description . "
				\r\n\r\nCompleted By:" . $EmpID . "
				\r\nRequested By:" . $Requested_by . "
				\r\nContact 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' => 'Work order is completed successfully'
    );

    echo json_encode($returnData);
}

function returnError($error_message)
{

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

?>
