<?php
    include dirname(__FILE__) . '/../php_only_scripts/global_after_session.php';
    include dirname(__FILE__) . '/../php_only_scripts/database.php';
    include dirname(__FILE__) . '/../php_only_scripts/global_functions.php';
    
    // Function to reset people as though they are NOT sick
    function changeSickPeople($db, $userRow){
        // Define a query to change everyone who is not clocked in and not meant to be kept in the category as 'clocked out' instead
        $sql = 'UPDATE laborclock ' .
               'SET CurrentLaborCategoryID=' .
               '         (SELECT LaborCategoryID FROM laborcategories WHERE Name="Clocked Out"), ' .
               '    CurrentLaborCategoryStartTime=CURRENT_TIMESTAMP ' .
               'WHERE CurrentLaborCategoryID IN ' .
               '         (SELECT LaborCategoryID FROM laborcategories WHERE IsDailyReset=1) ' .
               'AND EndTime IS NOT NULL;';

        // Attempt to run the query
        if (!$db->queryDB($sql, $userRow, "resetsickpeople Form occurance 1")){
            // Log the error to the error file
            error_log("Unable to reset the sick people to better " . 
                      "Query Ran: " . $sql, 0);                

            // Send the error to the main SU of this database
            error_log("Unable to reset the sick people to better " . 
                      "Query Ran: " . $sql, 1, "sdunn@evolveeng.com");    
        }
    }
    
    // Create and Setup the database
    $db = new Database();
    $db->setupTmsDB();

    // Attempt to get the username and password
    $userName = 'cuser';
    $password = '1Crono2User3!';

    // Attempt to log in with this user
    $currentUser = $db->loginUser($userName, $password, 7777777);

    // If there is only one result for a username/password combo, then they are good to go.
    if ($currentUser != null and mysqli_num_rows($currentUser)==1){
        // Attempt to get the user row
        $userRow = $currentUser->fetch_assoc();

        // Determine if they are the SU company
        if ($userRow["CompanyID"]==1){
            // Check for old attachments and delete thm
            changeSickPeople ($db, $userRow);        
        }
    }

    // Exit this code
    exit;
?>       

