<?php
/**
* @package Reports
*/
class Support_Ticket_Totals extends Report
{
    var $m_hasgraph = false;
    var $friendlyName = 'Support Ticket Totals';

    function process()
    {

        /////////////////////////////////////////////////////////////////
        
        // support_ticket_totals-support.php
        
        //
        
        // This page generates a report showing:
        
        // Displays Support Stats for Each Level of Staff and
        
        // calculates how much pay based on per/ticket cost
        
        // Originally written by: Daniel Jones
        
        // Company: Nation Voice Commuincations LLC (NationVoice.com)
        
        // Modified by Keith with assistance from Mike & Alejandro 07/2007
        
        // Modified by Juan David 07/2007
        
        //
        
        //
        
        //---------------------------------------------------------------



        $VERSION = urlencode(NE_Registry::getAppVersion());
        
        echo "<script type=\"text/javascript\" src=\"newedge/javascript/jscalendar/NE_calendarConsolidated.php?version=$VERSION\"></script>";
        echo "<script type=\"text/javascript\" src=\"newedge/javascript/jscalendar/lang/calendar-en.js?version=$VERSION\"></script>";
        
        if(isset($_GET['startdate'])){
            $startDateArray = split('/', $_GET['startdate']);
            $tempStartDate = date("Y-m-d", mktime(0, 0, 0, $startDateArray[0], $startDateArray[1], $startDateArray[2]));
            $temp2StartDate = mktime(0, 0, 0, $startDateArray[0], $startDateArray[1], $startDateArray[2]);
        }else{
            $tempStartDate = date("Y-m-d", mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
            $temp2StartDate = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
        }
        
        if(isset($_GET['enddate'])){
            $endDateArray = split('/', $_GET['enddate']);
            $tempEndDate = date("Y-m-d", mktime(0, 0, 0, $endDateArray[0], $endDateArray[1], $endDateArray[2]));
            $temp2EndDate = mktime(0, 0, 0, $endDateArray[0], $endDateArray[1]+1, $endDateArray[2]);
        }else{
            $tempEndDate = date("Y-m-d");
            $temp2EndDate = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
        }
        $amountOfDays = ((($temp2EndDate-$temp2StartDate)/60)/60)/24;
       
        if ($this->settings->get('Date Format') == 'm/d/Y') {
            $dateFormat = '%m/%d/%Y';
        } else {
            $dateFormat = '%d/%m/%Y';
        }

        echo "<br>";
        echo "<form id='reportdropdown' method='GET'>";
        echo "<table border=0 cellpadding=2 cellspacing=0>";
        echo "<tr><td align=center>";
        echo $this->user->lang('Start Date').":&nbsp;&nbsp;";

        echo '<input class=body type=text size=12 MAXLENGTH=10 name="startdate" id="startdate" value=\''.NE_Lib::db_to_form($tempStartDate, $this->settings->get('Date Format'), "/").'\'>'
            .'&nbsp;&nbsp;<img id="calButton" src="images/dynCalendar.gif" style="cursor:pointer"/>'
            .'<link rel="stylesheet" type="text/css" href="newedge/javascript/jscalendar/calendar-win2k-cold-1.css" />'
            .'<script type="text/javascript">'
            .'Calendar.setup({'
            ."inputField      : 'startdate',"
            ."ifFormat        : '$dateFormat',"
            ."showsTime       : false,"
            ."button          : 'calButton',"
            ."weekNumbers     : false,"
            ."step            : 1"
            .'});'
            .'</script>';

        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$this->user->lang('End Date').":&nbsp;&nbsp;";

        echo '<input class=body type=text size=12 MAXLENGTH=10 name="enddate" id="enddate" value=\''.NE_Lib::db_to_form($tempEndDate, $this->settings->get('Date Format'), "/").'\'>'
            .'&nbsp;&nbsp;<img id="calButton" src="images/dynCalendar.gif" style="cursor:pointer"/>'
            .'<link rel="stylesheet" type="text/css" href="newedge/javascript/jscalendar/calendar-win2k-cold-1.css" />'
            .'<script type="text/javascript">'
            .'Calendar.setup({'
            ."inputField      : 'enddate',"
            ."ifFormat        : '$dateFormat',"
            ."showsTime       : false,"
            ."button          : 'calButton',"
            ."weekNumbers     : false,"
            ."step            : 1"
            .'});'
            .'</script>';

        echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type=button name=search class="button_medium" value=\''.$this->user->lang('search').'\' onclick="ChangeTable($(\'startdate\').value, $(\'enddate\').value);">
              &nbsp;&nbsp;';
        echo "</td></tr></table>";
        echo "</form>";
        echo "<br>";
        echo "<br>";
        echo "<br>";


        echo "\n\n<script type='text/javascript'>

                function ChangeTable(strStartDate, strEndDate){
                    location.href='index.php?fuse=reports&report=Support_Ticket_Totals&type=Support&view=ViewReport&startdate='+strStartDate+'&enddate='+strEndDate;
                }
            </script>";
        echo "\n\n";
        
        
        
        $queryTypes = "SELECT name FROM troubleticket_type ORDER BY myorder";
        $resultTypes = $this->db->query($queryTypes);
        $types = array();
        $tags = array($this->user->lang('Name'));
        while(list($type) = $resultTypes->fetch()){
            $types[] = $type;
            $tags[] = $this->user->varLang($type);
        }
        $tags[] = "<font color=green>".$this->user->lang('Total Tickets Closed')."</font>";
        $tags[] = "<font color=green>".$this->user->lang('Tickets/Day')."</font>";
        
        $totalTotal = array();
        foreach($types AS $tType){
            $totalTotal[$tType] = 0;
            $totalTotal[$tType.'Answers'] = 0;
        }
        
        $groupids = array (2, 3, 4, 5);
        foreach($groupids AS $tGroupID){
            switch($tGroupID){
                case 2:
                    $level = "Super Admin";
                    break;
                case 3:
                    $level = "Support Staff";
                    break;
                case 4:
                    $level = "Billing Manager";
                    break;
                case 5:
                    $level = "Support Manager";
                    break;
            }
            $query = "SELECT id, firstname, lastname FROM users WHERE groupid=? ORDER BY firstname";
            $result = $this->db->query($query, $tGroupID);
            $aGroup = array();
            
            $totalLevel = array();
            foreach($types AS $tType){
                $totalLevel[$tType] = 0;
                $totalLevel[$tType.'Answers'] = 0;
            }
            
            while(list($id, $firstname, $lastname) = $result->fetch()){
                $values = array($firstname.' '.$lastname);
                $total = 0;
                $totalAnswers = 0;
                foreach($types AS $tType){
                    $amount = 0;
                    $arrayTicketIDs = array();
                    $query2 = "SELECT id FROM troubleticket WHERE assignedtoid=? AND status=-1 AND lastlog_datetime >= '$tempStartDate' AND lastlog_datetime <= '$tempEndDate 23:59:59' AND messagetype = ?";
                    $result2 = $this->db->query($query2, $id, $tType);
                    while(list($ticketID) = $result2->fetch()){
                        $arrayTicketIDs[] = $ticketID;
                        $amount++;
                    }
                    
                    $amountAnswers = 0;
                    if($amount > 0){
                        $ticketIDs = implode(', ', $arrayTicketIDs);
                        $query3 = "SELECT COUNT(userid) FROM troubleticket_log WHERE troubleticketid IN (".$ticketIDs.") AND userid = ? AND mydatetime >= '$tempStartDate' AND mydatetime <= '$tempEndDate 23:59:59'";
                        $result3 = $this->db->query($query3, $id);
                        list($answers) = $result3->fetch();
                        $amountAnswers = $answers;
                    }

                    $totalLevel[$tType] += $amount;
                    $totalTotal[$tType] += $amount;
                    $totalLevel[$tType.'Answers'] += $amountAnswers;
                    $totalTotal[$tType.'Answers'] += $amountAnswers;
                    $values[] = "$amount ($amountAnswers)";
                    $total += $amount;
                    $totalAnswers += $amountAnswers;
                }
                $values[] = "<b>$total ($totalAnswers)</b>";
                $values[] = '<b>'.round($total/$amountOfDays, 2).' ('.round($totalAnswers/$amountOfDays, 2).')</b>';
                
                $aGroup[] = $values;
            }
            if(count($aGroup) > 0){
                $totalLevelValues = array('<font color=green><b>'.$this->user->lang('TOTAL').'</b></font>');
                $totalTotalLevel = 0;
                $totalTotalLevelAnswers = 0;
                foreach($types AS $tType){
                    $totalLevelValues[] = '<font color=green><b>'.$totalLevel[$tType].' ('.$totalLevel[$tType.'Answers'].')</b></font>';
                    $totalTotalLevel += $totalLevel[$tType];
                    $totalTotalLevelAnswers += $totalLevel[$tType.'Answers'];
                }
                
                $totalLevelValues[] = "<font color=green><b>$totalTotalLevel ($totalTotalLevelAnswers)</b></font>";
                $totalLevelValues[] = '<font color=green><b>'.round($totalTotalLevel/$amountOfDays, 2).' ('.round($totalTotalLevelAnswers/$amountOfDays, 2).')</b></font>';
                $aGroup[] = $totalLevelValues;
                $this->Add($aGroup,$this->user->varLang($level),$tags);
            }else{
                $this->Add(array(array('<font color=red>'.$this->user->lang('no values').'</font>')) ,$this->user->varLang($level),array(''));
            }
        }
        
        $totalValues = array('<font color=green><b>'.$this->user->lang('TOTAL').'</b></font>');
        $totalTotalValues = 0;
        $totalTotalValuesAnswers = 0;
        foreach($types AS $tType){
            $totalValues[] = '<font color=green><b>'.$totalTotal[$tType].' ('.$totalTotal[$tType.'Answers'].')</b></font>';
            $totalTotalValues += $totalTotal[$tType];
            $totalTotalValuesAnswers += $totalTotal[$tType.'Answers'];
        }
        
        $totalValues[] = "<font color=green><b>$totalTotalValues ($totalTotalValuesAnswers)</b></font>";
        $totalValues[] = '<font color=green><b>'.round($totalTotalValues/$amountOfDays, 2).' ('.round($totalTotalValuesAnswers/$amountOfDays, 2).')</b></font>';
        $aGroup2 = array();
        $aGroup2[] = $totalValues;
        $this->Add($aGroup2,$this->user->varLang('<font color=green>'.$this->user->lang('TOTAL').'</font>'),$tags);
                
        //Class Initialize
        $this->SetDescription($this->user->lang('Displays how many tickets each staff member has closed (and how many times has replied to them) based on a date range.'));

        //Display Report
        $this->Show();
    }
}
?>