/**
* Calculate the engineer working time between two timestamps for a given incident
i.e. ignore times when customer has action
* @author Ivan Lucas
@param int $incidentid - The incident ID to perform a calculation on
@param int $t1 - UNIX Timestamp. Start of range
@param int $t2 - UNIX Timestamp. End of range
@param array $states (optional) Does not count time when the incident is set to
any of the states in this array. (Default is closed, awaiting closure and awaiting customer action)
*/
function calculate_incident_working_time($incidentid, $t1, $t2, $states = array(2, 7, 8))
{
    if ($t1 > $t2) {
        $t3 = $t2;
        $t2 = $t1;
        $t1 = $t3;
    }
    $startofday = mktime(0, 0, 0, date("m", $t1), date("d", $t1), date("Y", $t1));
    $endofday = mktime(23, 59, 59, date("m", $t2), date("d", $t2), date("Y", $t2));
    $publicholidays = get_public_holidays($startofday, $endofday);
    $sql = "SELECT id, currentstatus, timestamp FROM `{$GLOBALS['dbUpdates']}` WHERE incidentid='{$incidentid}' ORDER BY id ASC";
    $result = mysql_query($sql);
    if (mysql_error()) {
        trigger_error(mysql_error(), E_USER_WARNING);
    }
    $time = 0;
    $timeptr = 0;
    $laststatus = 2;
    // closed
    while ($update = mysql_fetch_array($result)) {
        //  if ($t1<=$update['timestamp'])
        if ($t1 <= $update['timestamp']) {
            if ($timeptr == 0) {
                // This is the first update
                // If it's active, set the ptr = t1
                // otherwise set to current timestamp ???
                if (is_active_status($laststatus, $states)) {
                    $timeptr = $t1;
                } else {
                    $timeptr = $update['timestamp'];
                }
            }
            if ($t2 < $update['timestamp']) {
                // If we have reached the very end of the range, increment time to end of range, break
                if (is_active_status($laststatus, $states)) {
                    $time += calculate_working_time($timeptr, $t2, $publicholidays);
                }
                break;
            }
            // if status has changed or this is the first (active update)
            if (is_active_status($laststatus, $states) != is_active_status($update['currentstatus'], $states)) {
                // If it's active and we've not reached the end of the range, increment time
                if (is_active_status($laststatus, $states) && $t2 >= $update['timestamp']) {
                    $time += calculate_working_time($timeptr, $update['timestamp'], $publicholidays);
                } else {
                    $timeptr = $update['timestamp'];
                }
                // if it's not active set the ptr
            }
        }
        $laststatus = $update['currentstatus'];
    }
    mysql_free_result($result);
    // Calculate remainder
    if (is_active_status($laststatus, $states) && $t2 >= $update['timestamp']) {
        $time += calculate_working_time($timeptr, $t2, $publicholidays);
    }
    return $time;
}
Example #2
0
/** Chase / Remind customers
    * @author Paul Heaney
    * @note Moved from htdocs/auto/chase_customer.php by INL for 3.40
*/
function saction_ChaseCustomers()
{
    global $CONFIG, $now, $sit;
    global $dbIncidents, $dbUpdates;
    $success = TRUE;
    /**
     * @author Paul Heaney
     */
    function not_auto_type($type)
    {
        if ($type != 'auto_chase_email' and $type != 'auto_chase_phone' and $type != 'auto_chase_manager') {
            return TRUE;
        }
        return FALSE;
    }
    if ($CONFIG['auto_chase'] == TRUE) {
        // if 'awaiting customer action' for more than $CONFIG['chase_email_minutes'] and NOT in an auto state, send auto email
        //$sql = "SELECT incidents.id, contacts.forenames,contacts.surname,contacts.id AS managerid FROM incidents,contacts WHERE status = ".STATUS_CUSTOMER." AND contacts.notify_contactid = contacts.id";
        $sql = "SELECT * FROM `{$dbIncidents}` AS i WHERE status = " . STATUS_CUSTOMER;
        $result = mysql_query($sql);
        if (mysql_error()) {
            trigger_error("MySQL Query Error " . mysql_error(), E_USER_WARNING);
            $success = FALSE;
        }
        while ($obj = mysql_fetch_object($result)) {
            if (!in_array($obj->maintenanceid, $CONFIG['dont_chase_maintids'])) {
                // only annoy these people
                $sql_update = "SELECT * FROM `{$dbUpdates}` WHERE incidentid = {$obj->id} ORDER BY timestamp DESC LIMIT 1";
                $result_update = mysql_query($sql_update);
                if (mysql_error()) {
                    trigger_error("MySQL Query Error " . mysql_error(), E_USER_WARNING);
                    $success = FALSE;
                }
                $obj_update = mysql_fetch_object($result_update);
                if ($CONFIG['chase_email_minutes'] != 0) {
                    //if (not_auto_type($obj_update->type) AND $obj_update->timestamp <= ($now-$CONFIG['chase_email_minutes']*60))
                    if (not_auto_type($obj_update->type) and ($obj->timeofnextaction == 0 and calculate_working_time($obj_update->timestamp, $now) >= $CONFIG['chase_email_minutes'] or $obj->timeofnextaction != 0 and calculate_working_time($obj->timeofnextupdate, $now) >= $CONFIG['chase_email_minutes'])) {
                        $paramarray = array('incidentid' => $obj->id, 'triggeruserid' => $sit[2]);
                        send_email_template($CONFIG['chase_email_template'], $paramarray);
                        $sql_insert = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, currentowner, currentstatus, bodytext, timestamp, customervisibility) VALUES ('{$obj_update->incidentid}','{$sit['2']}', 'auto_chase_email', '{$obj->owner}', '{$obj->status}', 'Sent auto chase email to customer','{$now}','show')";
                        mysql_query($sql_insert);
                        if (mysql_error()) {
                            trigger_error("MySQL Query Error " . mysql_error(), E_USER_ERROR);
                            $success = FALSE;
                        }
                        $sql_update = "UPDATE `{$dbIncidents}` SET lastupdated = '{$now}', nextactiontime = 0 WHERE id = {$obj->id}";
                        mysql_query($sql_update);
                        if (mysql_error()) {
                            trigger_error("MySQL Query Error " . mysql_error(), E_USER_ERROR);
                            $success = FALSE;
                        }
                    }
                }
                if ($CONFIG['chase_phone_minutes'] != 0) {
                    //if ($obj_update->type == 'auto_chase_email' AND $obj_update->timestamp <= ($now-$CONFIG['chase_phone_minutes']*60))
                    if ($obj_update->type == 'auto_chase_email' and ($obj->timeofnextaction == 0 and calculate_working_time($obj_update->timestamp, $now) >= $CONFIG['chase_phone_minutes'] or $obj->timeofnextaction != 0 and calculate_working_time($obj->timeofnextupdate, $now) >= $CONFIG['chase_phone_minutes'])) {
                        $sql_insert = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, currentowner, currentstatus, bodytext, timestamp, customervisibility) VALUES ('{$obj_update->incidentid}','{$sit['2']}','auto_chase_phone', '{$obj->owner}', '{$obj->status}', 'Status: Awaiting Customer Action -&gt; <b>Active</b><hr>Please phone the customer to get an update on this call as {$CONFIG['chase_phone_minutes']} have passed since the auto chase email was sent. Once you have done this please use the update type \"Chased customer - phone\"','{$now}','hide')";
                        mysql_query($sql_insert);
                        if (mysql_error()) {
                            trigger_error("MySQL Query Error " . mysql_error(), E_USER_ERROR);
                            $success = FALSE;
                        }
                        $sql_update = "UPDATE `{$dbIncidents}` SET lastupdated = '{$now}', ";
                        $sql_update .= "nextactiontime = 0, status = " . STATUS_ACTIVE . " WHERE id = {$obj->id}";
                        mysql_query($sql_update);
                        if (mysql_error()) {
                            trigger_error("MySQL Query Error " . mysql_error(), E_USER_ERROR);
                            $success = FALSE;
                        }
                    }
                }
                if ($CONFIG['chase_manager_minutes'] != 0) {
                    //if ($obj_update->type == 'auto_chased_phone' AND $obj_update->timestamp <= ($now-$CONFIG['chase_manager_minutes']*60))
                    if ($obj_update->type == 'auto_chased_phone' and ($obj->timeofnextaction == 0 and calculate_working_time($obj_update->timestamp, $now) >= $CONFIG['chase_manager_minutes'] or $obj->timeofnextaction != 0 and calculate_working_time($obj->timeofnextupdate, $now) >= $CONFIG['chase_manager_minutes'])) {
                        $update = "Status: Awaiting Customer Action -&gt; <b>Active</b><hr>";
                        $update .= "Please phone the customers MANAGER to get an update on this call as " . $CONFIG['chase_manager_minutes'] . " have passed since the auto chase email was sent.<br />";
                        $update .= "The manager is <a href='contact_details.php?id={$obj->managerid}'>{$obj->forenames} {$obj->surname}</a><br />";
                        $update .= " Once you have done this please email the actions to the customer and select the \"Was this a customer chase?\"'";
                        $sql_insert = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, currentowner, currentstatus, bodytext, timestamp, customervisibility) VALUES ('{$obj_update->incidentid}','{$sit['2']}','auto_chase_manager', '{$obj->owner}', '{$obj->status}', {$update},'{$now}','hide')";
                        mysql_query($sql_insert);
                        if (mysql_error()) {
                            trigger_error("MySQL Query Error " . mysql_error(), E_USER_ERROR);
                            $success = FALSE;
                        }
                        $sql_update = "UPDATE `{$dbIncidents}` SET lastupdated = '{$now}', nextactiontime = 0, status = " . STATUS_ACTIVE . " WHERE id = {$obj->id}";
                        mysql_query($sql_update);
                        if (mysql_error()) {
                            trigger_error("MySQL Query Error " . mysql_error(), E_USER_ERROR);
                            $success = FALSE;
                        }
                    }
                }
                if ($CONFIG['chase_managers_manager_minutes'] != 0) {
                    //if ($obj_update->type == 'auto_chased_manager' AND $obj_update->timestamp <= ($now-$CONFIG['chase_managers_manager_minutes']*60))
                    if ($obj_update->type == 'auto_chased_manager' and ($obj->timeofnextaction == 0 and calculate_working_time($obj_update->timestamp, $now) >= $CONFIG['chase_amanager_manager_minutes'] or $obj->timeofnextaction != 0 and calculate_working_time($obj->timeofnextupdate, $now) >= $CONFIG['chase_amanager_manager_minutes'])) {
                        $sql_insert = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, currentowner, currentstatus, bodytext, timestamp, customervisibility) VALUES ('{$obj_update->incidentid}','{$sit['2']}','auto_chase_managers_manager','{$obj->owner}', '{$obj->status}', 'Status: Awaiting Customer Action -&gt; <b>Active</b><hr>Please phone the customers managers manager to get an update on this call as {$CONFIG['chase_manager_minutes']} have passed since the auto chase email was sent. Once you have done this please email the actions to the customer and manager and select the \"Was this a manager chase?\"','{$now}','hide')";
                        mysql_query($sql_insert);
                        if (mysql_error()) {
                            trigger_error("MySQL Query Error " . mysql_error(), E_USER_ERROR);
                            $success = FALSE;
                        }
                        $sql_update = "UPDATE `{$dbIncidents}` SET lastupdated = '{$now}', nextactiontime = 0, status = " . STATUS_ACTIVE . " WHERE id = {$obj->id}";
                        mysql_query($sql_update);
                        if (mysql_error()) {
                            trigger_error("MySQL Query Error " . mysql_error(), E_USER_ERROR);
                            $success = FALSE;
                        }
                    }
                }
            }
        }
    }
    return $success;
}
     if ($last == -1) {
         $updatearray[$row->currentstatus]['time'] = 0;
     } else {
         $updatearray[$laststatus]['time'] += 60 * calculate_incident_working_time($row->incidentid, $last, $row->timestamp, array(2, 7));
     }
     $laststatus = $row->currentstatus;
     $last = $row->timestamp;
 }
 if ($incident->status == 7 or $incident->status == 2) {
     $end = $incident->closed;
 } else {
     $end = $now;
 }
 $publicholidays = get_public_holidays($incident->opened, $end);
 //calculate the last update
 $updatearray[$laststatus]['time'] += 60 * calculate_working_time($last, time(), $publicholidays);
 echo "<h3>{$strStatusSummary}</h3>";
 if (extension_loaded('gd')) {
     $data = array();
     $legends;
     foreach ($updatearray as $row) {
         array_push($data, $row['time']);
         $legends .= $GLOBALS[$row['name']] . "|";
     }
     $data = implode('|', $data);
     $title = urlencode($strStatusSummary);
     echo "<div style='text-align:center;'>";
     echo "<img src='chart.php?type=pie&data={$data}&legends={$legends}&title={$title}&unit=seconds' />";
     echo "</div>";
 } else {
     echo "<table align='center'>";