<?php
    include_once dirname(__FILE__) . '/../global_after_session.php';
    include_once dirname(__FILE__) . '/../database.php';
    include_once dirname(__FILE__) . '/../global_functions.php';
    
    include_once dirname(__FILE__) . '/../presentation.php';
    include_once dirname(__FILE__) . '/../note_presentation.php';
    include_once dirname(__FILE__) . '/../blank_presentation.php';
    include_once dirname(__FILE__) . '/../toolLists_presentation.php';
    include_once dirname(__FILE__) . '/../setup_presentation.php';
    include_once dirname(__FILE__) . '/../task_presentation.php';

    // attempt to copy all tasks inside of or this particular task to the task requested
    function copyMachiningTasksToTask($db, $userRow, $parentTaskNumber, $copyTaskNumber, &$fromWhatCompany, &$fromWhatType, &$informationalMessage) {
        try {
            // Determine if we have a parent task number to work on
            if ($parentTaskNumber==null || !is_numeric($parentTaskNumber) || $parentTaskNumber<1){
                // Return an error string to be reported to the user
                return 'ABORT COPY (import_machining_tasks)::The parent task number (' . $parentTaskNumber . ')to copy tasks into is not valid.  Try a different parent TID. Aborting...';
            }
            
            // Determine if we have a parent task number to work on
            if ($copyTaskNumber==null || !is_numeric($copyTaskNumber) || $copyTaskNumber<1){
                // Return an error string to be reported to the user
                return 'ABORT COPY (import_machining_tasks)::The TID (' . $copyTaskNumber . ')to copy tasks from is not valid.  Try a different TID. Aborting...';
            }
            
            // Create the query to get the parent we are copying things into
            $sql = 'SELECT *, c.CompanyName ' . 
                   'FROM tasks t ' . 
                   'LEFT JOIN companies c ON t.CompanyID = c.CompanyID ' .
                   'WHERE t.TaskID=' . $parentTaskNumber . ';';

            // Attempt to query the parent task
            $rowParentTask = $db->queryDB($sql, $userRow, "import_machining_tasks Form occurance 1");

            // Attempt to get the task row
            $assocRowParentTask = $rowParentTask->fetch_assoc();
            
            // Create message info
            $duplicateToolsMessage="";
            $feedbackMessage="";
            $failedToSendMessage="";
            $duplicateToolsMessageHTML="";
            $feedbackMessageHTML="";
                    
            // Determine if we got a parent task
            if ($assocRowParentTask==NULL){
                // Return an error string to be reported to the user
                return 'ABORT COPY (import_machining_tasks)::The parent task number "' . $parentTaskNumber . '" is not valid.  Try a different parent TID. Aborting...';
            }
            
            // Create the query to get the parent we are copying things into
            $sql = 'SELECT * ' . 
                   'FROM tasks t ' . 
                   'WHERE t.TaskID=' . $copyTaskNumber . ';';

            // Attempt to query the task to copy
            $rowCopyTask = $db->queryDB($sql, $userRow, "import_machining_tasks Form occurance 2");

            // Attempt to get the task row
            $assocRowCopyTask = $rowCopyTask->fetch_assoc();
            
            // Determine if we got a task to copy
            if ($assocRowCopyTask==NULL){
                // Return an error string to be reported to the user
                return 'ABORT COPY (import_machining_tasks)::The task to copy number "' . $copyTaskNumber . '" is not valid.  Try a different TID to copy. Aborting...';
            }
            
            // Get the TMS home one time
            $tmsHome = getTMSHome();

            // Initial checks are good.  Start this by initializing variables
            $newTaskID=-1;
            $fromWhatCompany=$assocRowParentTask["CompanyID"];
            $fromWhatType=$assocRowParentTask["TypeID"];

            // Set the message and subject to send to nothing for now
            $messageToSend='';
            $subject='';
            
            // Now determine if we are copying a task with children or a lone sole
            if ($assocRowCopyTask["HasChildren"]==0){
                // Initialize the has feedback flag
                $hasFeedback=0;

                // Reset the has duplicates string
                $duplicateToolsString="";

                // We are good to just copy this task directly
                $success=copyTask($db, $userRow, $duplicateToolsString, $copyTaskNumber, $parentTaskNumber, $newTaskID, $hasFeedback);
                // Determine if we had success
                if (!$success){
                    // Return an error string to be reported to the user
                    return 'ABORT COPY (import_machining_tasks)::There was an error copying this task that has NO children (TID:' . $copyTaskNumber . '). Aborting...';
                }
                else {
                    // Determine if there are duplicate tools in any setup
                    if ($duplicateToolsString!==""){
                        // Add this information to the informational message
                        $duplicateToolsMessage = $duplicateToolsString;
                        
                        // Add message to e-mail about duplicate tools
                        $duplicateToolsMessageHTML= '<font color="red"><b>[Copied From <a href="https://www.eestms.com/' . 
                                                    $tmsHome . '/updateTask.php?updateTaskID=' . $copyTaskNumber . 
                                                    '">TID ' . $copyTaskNumber . '</a> Has Duplicate Tools]</b></font>';
                    }
                    
                    // Determine if the task has feedback
                    if ($hasFeedback==1){
                        // Create an informational message for the user at the end
                        $feedbackMessage = "The copied Task ID " . $copyTaskNumber . " has feedback notes. Please ensure these are reviewed before issuing out the task to machining.";
                            
                        // Add a message to the e-mail about this one
                        $feedbackMessageHTML= '<font color="red"><b>[Copied From <a href="https://www.eestms.com/' . 
                                              $tmsHome . '/updateTask.php?updateTaskID=' . $copyTaskNumber . 
                                              '">TID ' . $copyTaskNumber . '</a> Has Feedback]</b></font>';
                    }
                    
                    // Create the subject
                    $subject = htmlspecialchars_decode ("EES Subscription Message: Child Task Copied To " . 
                                                        $assocRowParentTask["Title"] . " for company '" . 
                                                        $assocRowParentTask["CompanyName"] . "'.");
                    
                    // Add this to the message to send
                    $messageToSend= 'There has been a child machining task added to TID ' . 
                                    '<a href="https://www.eestms.com/' . $tmsHome . 
                                    '/updateTask.php?updateTaskID=' . $parentTaskNumber . 
                                    '">  ' . $parentTaskNumber . '</a>. <br>' . 
                                    'Information about the addition is as follows:' .
                                    '<br><br>COMPANY: "' . $assocRowParentTask["CompanyName"] . 
                                    '"<br>Parent Task Title: "' . $assocRowParentTask["Title"] . 
                                    '"<br>Parent TID#: ' . $parentTaskNumber .
                                    '<a href="https://www.eestms.com/' . $tmsHome . 
                                    '/updateTask.php?updateTaskID=' . $parentTaskNumber . 
                                    '&parentTaskId=0">  (Click To View)</a>. <br>' . 
                                    '<br>New Child Task Title: "' . $assocRowCopyTask["Title"] . 
                                    '"<br>New Child TID#: ' . $newTaskID .
                                    '<a href="https://www.eestms.com/' . $tmsHome . 
                                    '/updateTask.php?updateTaskID=' . $newTaskID . 
                                    '&parentTaskId=' . $parentTaskNumber . '">  (Click To View)</a> ' . $feedbackMessageHTML . ' *** ' . $duplicateToolsMessageHTML . '<br>' .
                                    '<br><br>If you need help with your user name or password, you can ' .
                                    'reply to this e-mail or call us at (720)552-7997.<br><br>' .
                                    'Regards, <br><br>Evolve Engineering and Software Support Team<br>' .
                                    '(720)552-7997';
                }
            }
            else{
                // Now create a query to get all of this parent's children.
                $sql = 'SELECT TaskID, Title ' . 
                       'FROM tasks t ' . 
                       'WHERE t.ParentID=' . $copyTaskNumber . ' ' .
                       'ORDER BY SubPriority ASC;';

                // Run the query
                $allchildren = $db->queryDB($sql, $userRow, "import_machining_tasks Form occurance 3");

                // Determine if there are any rows before attempting to loop through them
                if ($allchildren == null){
                    // Create an error string to be reported to the user
                    return 'ABORT COPY (import_machining_tasks)::No children were queried for TID ' . $copyTaskNumber . ' even though it is identified as a task with children. Aborting...';
                }

                // Loop through the returned results to copy them all
                while ($child=mysqli_fetch_array($allchildren)){
                    // Reset the new TaskID value
                    $newTaskID=-1;
                    
                    // Reset the has feedback flag
                    $hasFeedback=0;
                    
                    // Reset the has duplicates string
                    $duplicateToolsString="";

                    // We are good to just copy this task directly
                    $success=copyTask($db, $userRow, $duplicateToolsString, $child["TaskID"], $parentTaskNumber, $newTaskID, $hasFeedback);

                    // Determine if we had success
                    if (!$success){
                        // Return an error string to be reported to the user
                        return 'ABORT COPY (import_machining_tasks)::There was an error copying this child task (TID:' . $child["TaskID"] . '). Aborting...';
                    }
                    else {
                        // Determine if there are duplicate tools in any setup
                        if ($duplicateToolsString!==""){
                            // Add this information to the informational message
                            $duplicateToolsMessage .= $duplicateToolsString;

                            // Add message to e-mail about duplicate tools
                            $duplicateToolsMessageHTML= '<font color="red"><b>[Copied From <a href="https://www.eestms.com/' . 
                                                        $tmsHome . '/updateTask.php?updateTaskID=' . $child["TaskID"] . '&parentTaskId=' . $copyTaskNumber .
                                                        '">TID ' . $child["TaskID"] . '</a> Has Duplicate Tools]</b></font>';
                        }

                        // Determine if the task has feedback
                        if ($hasFeedback==1){
                            // Add this ID as one that has had feedback 
                            $feedbackMessage .= $newTaskID . ', ';
                            
                            // Add a message to the e-mail about this one
                            $feedbackMessageHTML= '<font color="red"><b>[Copied From <a href="https://www.eestms.com/' . 
                                                  $tmsHome . '/updateTask.php?updateTaskID=' . $child["TaskID"] . '&parentTaskId=' . $copyTaskNumber .
                                                  '">TID ' . $child["TaskID"] . '</a> Has Feedback]</b></font>';
                        }

                        // Add this to the message to send
                        $messageToSend= $messageToSend . '<br>   ++  <b>TID#</b>:' . $newTaskID . 
                                        '    <b>Title</b>:' .$child["Title"] . '<a href="https://www.eestms.com/' . 
                                        $tmsHome . '/updateTask.php?updateTaskID=' . $newTaskID . '&parentTaskId=' . $parentTaskNumber . 
                                        '">  (Click To View)</a>' . $feedbackMessageHTML . ' *** ' . $duplicateToolsMessageHTML;
                    }
                }
                
                // Determine if the task has feedback
                if ($feedbackMessage!=""){
                    // Remove the last character
                    $feedbackMessage=substr($feedbackMessage, 0, -2);

                    // Create an informational message for the user at the end
                    $feedbackMessage = "The copied Task ID(s) (" . $feedbackMessage . ") has/have feedback notes. Please ensure these are reviewed before issuing out the task to machining.";
                }
                
                // Determine if we have a message to send to people who are interested
                if ($messageToSend!=''){
                    // Create the subject
                    $subject = htmlspecialchars_decode ("EES Subscription Message: Child(ren) Task(s) Copied To " . 
                                                        $assocRowParentTask["Title"] . " for company '" . 
                                                        $assocRowParentTask["CompanyName"] . "'.");

                    // Add this to the message to send
                    $messageToSend= 'Child(ren) machining task(s) have been added to ' . 
                                    'TID <a href="https://www.eestms.com/' . $tmsHome . 
                                    '/updateTask.php?updateTaskID=' . $parentTaskNumber . 
                                    '&parentTaskId=0">  ' . $parentTaskNumber . '</a>.<br>' . 
                                    'Information about the addition is as follows:' .
                                    '<br><br><b>COMPANY</b>: "' . $assocRowParentTask["CompanyName"] . 
                                    '"<br><b>Parent Task Title</b>: "' . $assocRowParentTask["Title"] . 
                                    '"<br><b>Parent TID#</b>: ' . $parentTaskNumber .
                                    '<a href="https://www.eestms.com/' . $tmsHome . 
                                    '/updateTask.php?updateTaskID=' . $parentTaskNumber . 
                                    '&parentTaskId=0">  (Click To View)</a>.<br><br>' .
                                    'Child(ren) Task(s) added:' .
                                    $messageToSend .
                                    '<br><br>If you need help with your user name or password, you can ' .
                                    'reply to this e-mail or call us at (720)552-7997.<br><br>' .
                                    'Regards, <br><br>Evolve Engineering and Software Support Team<br>' .
                                    '(720)552-7997';

                }
            }
            
            // Determine if we have a message to send to people who are interested
            if ($messageToSend!=''){
                // Let's create and get the users to inform.
                $sql='SELECT u.* 
                      FROM users u
                      WHERE u.UserID<>' . $userRow["UserID"] . '
                      AND u.UserTemplateOnly=0 
                      AND u.AccountStatus="Active"
                      AND u.SilenceAllTaskNotifications<>1 
                      AND ((u.CompanyID = ' . $assocRowParentTask["CompanyID"] . ' AND u.ReceiveAddTaskUpdate=1) 
                           OR (u.CompanyID = 1 AND u.SUEmployee=1 AND u.ReceiveAddTaskUpdate=1) 
                           OR (u.UserID = ' . $assocRowParentTask["UserID"] . '));';

                // Make the query
                $usersToNotify = $db->queryDB($sql, $userRow, "import_machining_tasks Form occurance 4");

                // setup a users not sent to notification
                $usersNotSentTo='';
                
                // Loop through the returned results
                while ($userToNotify=mysqli_fetch_array($usersToNotify)){
                    // First determine if they care about any changes
                    if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $userToNotify["EMail"])){
                        // Make the headers for this e-mail
                        $headers = "From: EES Mail Admin"." <admin@eestms.com>\r\n"; 
                        $headers .= "MIME-Version: 1.0\r\n";
                        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

                        try {
                            // Attempt to mail it
                            mail($userToNotify["EMail"],$subject,$userToNotify["FirstName"] . ' ' . $userToNotify["LastName"] . 
                                                                 ',<br><br>' . $messageToSend,$headers);
                        } 
                        catch (Exception $e) {
                            $usersNotSentTo .= $userToNotify["EMail"] . ', '; 
                        }
                    }
                }
                
                // Determine if there were some failed sending
                if ($usersNotSentTo!=''){
                    // Lop off the last 2 characters
                    $usersNotSentTo=substr($usersNotSentTo, 0, -2);
                    
                    // Set the return message as failed sending of e-mails
                    $failedToSendMessage="The notification was not sent to some users with regards to children added to parent task (" . $usersNotSentTo . ").";
                }
            }
            
            // Determine if the task has duplicate tools
            if ($duplicateToolsMessage!=""){
                // Create an informational message for duplicate tools
                $informationalMessage .= "There are duplicate tools in G-Code of some setups copied. Information on those setups are as follows:\n" . $duplicateToolsMessage . "\n\n";
            }

            // Determine if the task has feedback
            if ($feedbackMessage!=""){
                // Create an informational message that there is feedback
                $informationalMessage .= $feedbackMessage . "\n\n";
            }

            // Determine if sending the message failed for some users
            if ($failedToSendMessage!=""){
                // Create an informational message for the user at the end
                $informationalMessage .= $failedToSendMessage . "\n\n";
            }
        } 
        catch (Exception $exc) {
            // Create the error string to return back to the caller
            return 'ABORT COPY (import_machining_tasks)::There was an uncaught error while importing tasks.  The exception is: ' . $exc->getMessage() . '. Aborting...';
        }
        
        // If we got here then assume success
        return "";
    }
    
    function checkAndChangeParentStatus($db, $userRow, $parentTaskNumber, &$informationalMessage) {
        try {
            // Determine if we have a parent task number to work on
            if ($parentTaskNumber==null || !is_numeric($parentTaskNumber) || $parentTaskNumber<1){
                // Return an error string to be reported to the user
                return 'ABORT COPY (import_machining_tasks)::The parent task number (' . $parentTaskNumber . ')to copy tasks into is not valid.  Try a different parent TID. Aborting...';
            }
            
            // Create the query to get the parent we are copying things into
            $sql = 'SELECT *, s.ProcessOrder, s.Name AS StatusName, c.CompanyName, u.CompanyID AS AssignedToCompanyID, ' .
                   '(SELECT StatusID FROM status WHERE Name="In Review") as StatusIDOfChild, ' .
                   '(SELECT ProcessOrder FROM status WHERE Name="In Review") as ProcessOrderOfChild ' .
                   'FROM tasks t ' .
                   'LEFT JOIN status s ON t.StatusID = s.StatusID ' .
                   'LEFT JOIN companies c ON t.CompanyID = c.CompanyID ' .
                   'LEFT JOIN users u ON t.UserID = u.UserID ' .
                   'WHERE t.TaskID=' . $parentTaskNumber . ';';
            
            // Attempt to query the parent task
            $rowParentTask = $db->queryDB($sql, $userRow, "import_machining_tasks Form occurance 5");

            // Attempt to get the task row
            $assocRowParentTask = $rowParentTask->fetch_assoc();
            
            // Determine if we got a parent task
            if ($assocRowParentTask==NULL){
                // Return an error string to be reported to the user
                return 'ABORT COPY (import_machining_tasks)::The parent task number "' . $parentTaskNumber . '" is not valid.  Try a different parent TID. Aborting...';
            }
            
            // Determine if the process order of the children are before the process order of the parent
            if ($assocRowParentTask["ProcessOrderOfChild"]<$assocRowParentTask["ProcessOrder"]){
                // We will need to change the status of the parent. Creat the query.
                $sql='UPDATE tasks SET StatusID=' . $assocRowParentTask["StatusIDOfChild"] . ' ' .
                     'WHERE TaskID=' . $parentTaskNumber . ';';
                
                // Attempt to run the query and evaluate the response
                if (!$db->queryDB($sql, $userRow, "import_machining_tasks Form occurance 6")){
                    // Return an error string to be reported to the user
                    return 'ABORT COPY (import_machining_tasks)::Unable to update the status of parent (TID' . $parentTaskNumber . ') while copying children. Aborting...';
                }
                else {
                    // Get the TMS home one time
                    $tmsHome = getTMSHome();

                    // Let's create and get the users to inform.
                    $sql='SELECT u.*, usl.NotificationCategory 
                          FROM users u
                          LEFT JOIN (SELECT NotificationCategory, UserID FROM usertostatuslinker 
                                     WHERE StatusID=' . $assocRowParentTask["StatusIDOfChild"] . 
                                    ') usl ON u.UserID = usl.UserID
                          WHERE u.UserID<>' . $userRow["UserID"] . '
                          AND u.UserTemplateOnly=0 
                          AND u.AccountStatus="Active"
                          AND u.SilenceAllTaskNotifications<>1
                          AND ((u.CompanyID = ' . $assocRowParentTask["CompanyID"] . ') 
                               OR (u.CompanyID = 1 AND u.SUEmployee=1) 
                               OR (u.UserID = ' . $assocRowParentTask["UserID"] . '));';

                    // Make the query
                    $usersToNotify = $db->queryDB($sql, $userRow, "import_machining_tasks Form occurance 4");

                    // setup a users not sent to notification
                    $usersNotSentTo='';
                    // Loop through the returned results
                    while ($userToNotify=mysqli_fetch_array($usersToNotify)){
                        // Reset the message to send
                        $messageToSend="";
                        
                        // First determine if they care about any changes
                        if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $userToNotify["EMail"])){
                            // Determine if they care about status change or in packaging notifications. 
                            //  Also if they are in the SU company and task is being 
                            //  assigned to them.  The last is forced.)
                            if (($userToNotify["NotificationCategory"]=="PARENT") || 
                                ($userToNotify["NotificationCategory"]=="ALL") || 
                                ($assocRowParentTask["AssignedToCompanyID"]==1 &&
                                 $userToNotify["UserID"]==$assocRowParentTask["UserID"])){
                                // Create the subject
                                $subject = htmlspecialchars_decode ("EES Subscription Message: Parent Task (" . $assocRowParentTask["Title"] . 
                                                                    ") Status Change Via Child Copy for company '" . 
                                                                    $assocRowParentTask["CompanyName"] . "'.");

                                // Add this to the message to send
                                $messageToSend= $userToNotify["FirstName"] . ' ' . $userToNotify["LastName"] . ',<br><br>' .
                                                'Child(ren) machining task(s) have been added to ' . 
                                                'Parent TID <a href="https://www.eestms.com/' . $tmsHome . 
                                                '/updateTask.php?updateTaskID=' . $parentTaskNumber . 
                                                '&parentTaskId=0">  ' . $parentTaskNumber . '</a> ' . 
                                                'changing it\'s status to \'In Review\' from ' . 
                                                $assocRowParentTask["StatusName"] . '.<br>' . 
                                                'Information about the addition is as follows:' .
                                                '<br><br><b>COMPANY</b>: "' . $assocRowParentTask["CompanyName"] . 
                                                '"<br><b>Parent Task Title</b>: "' . $assocRowParentTask["Title"] . 
                                                '"<br><b>Parent TID#</b>: ' . $parentTaskNumber .
                                                '<a href="https://www.eestms.com/' . $tmsHome . 
                                                '/updateTask.php?updateTaskID=' . $parentTaskNumber . 
                                                '&parentTaskId=0">  (Click To View)</a>.<br><br>' .
                                                'If you need help with your user name or password, you can ' .
                                                'reply to this e-mail or call us at (720)552-7997.<br><br>' .
                                                'Regards, <br><br>Evolve Engineering and Software Support Team<br>' .
                                                '(720)552-7997';

                                // Make the headers for this e-mail
                                $headers = "From: EES Mail Admin"." <admin@eestms.com>\r\n"; 
                                $headers .= "MIME-Version: 1.0\r\n";
                                $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

                                try {
                                    // Attempt to mail it
                                    mail($userToNotify["EMail"],$subject,$messageToSend,$headers);
                                } 
                                catch (Exception $e) {
                                    $usersNotSentTo .= $userToNotify["EMail"] . ', '; 
                                }
                            }
                        }
                    }
                    
                    // Determine if there were some failed sending
                    if ($usersNotSentTo!=''){
                        // Lop off the last 2 characters
                        $usersNotSentTo=substr($usersNotSentTo, 0, -2);

                        // Set the return message as failed sending of e-mails
                        $informationalMessage .= "The notification was not sent to some users with regards to parent status changing (" . $usersNotSentTo . ").\n\n";
                    }
                }
                
                // Create the error string to return back to the caller
                return 'FULL REFRESH (surface_clean_task)::The parent status has been changed as a result of adding the child(ren) task(s).  Full refresh required.';
            }
        } 
        catch (Exception $exc) {
            // Create the error string to return back to the caller
            return 'ABORT COPY (import_machining_tasks)::There was an uncaught error while changing parent status after copy.  The exception is: ' . $exc->getMessage() . '. Aborting...';
        }
    }
    
    $db = new Database();
    $db->setupTmsDB();
    
    // Set defaults for the passed in variables
    $sessionID=-1;

    // check the passed in session ID
    if (isset($_GET['sessionID']) ){
        // get the passed in session ID
        $sessionID = $_GET['sessionID'];
    }

    // Attempt to log in with this user
    $currentUser = $db->getLoggedInSessionUser($sessionID);
    if (mysqli_num_rows($currentUser)!=1){
        echo 'ABORT COPY (import_machining_tasks)::Your session has expired.  ' . 
             'Refresh the page before copying tasks into this task.';
    }
    else {
        // Set defaults for the passed in variables
        $parentTaskNumber=-1;
        $copyTaskNumber=-1;
        $fromWhatCompany=-1;
        $fromWhatType="";
        $informationalMessage="";
        
        // check the passed in parent task ID
        if (isset($_GET['parentTaskNumber']) ){
            // get the passed in parent task ID
            $parentTaskNumber = $_GET['parentTaskNumber'];
        }

        // check the passed in task ID to copy
        if (isset($_GET['copyTaskNumber']) ){
            // get the passed in task ID to copy
            $copyTaskNumber = $_GET['copyTaskNumber'];
        }
        
        // Attempt to log in with this user
        $userRow = $currentUser->fetch_assoc();

        // Attempt to copy all of the machining tasks to the parent task passed in.
        $returnString=copyMachiningTasksToTask($db, $userRow, $parentTaskNumber, $copyTaskNumber, $fromWhatCompany, $fromWhatType, $informationalMessage);

        
        // Determine if we are gtg onto the next phase
        if (trim($returnString)=='') {
            // Modify the parent if necessary
            $returnString = checkAndChangeParentStatus($db, $userRow, $parentTaskNumber, $informationalMessage);
        }
        
        // Determine if we are gtg onto the next phase
        if (trim($returnString)=='') {
            // Now we can get the child tasks to send back
            $returnString = retrieveChildTasksAsList($db, $userRow, $parentTaskNumber, true, $fromWhatCompany, $fromWhatType);
        }
        
        // Determine if we there were no errors, but informational messages
        if (trim($informationalMessage)!="") {
            // Set the return string as the informational message
            $returnString = $returnString . '%%INFORMATIONAL MESSAGE (import_machining_tasks)::' . $informationalMessage;
        }

        // return the result
        echo $returnString;
    }
?>