<?php

// =============
// Metrics Class
// =============

class Metrics extends Base {
 
  // ==================
  // Class Constructor.
  // ==================
   
  public function __construct() {
    
    set_error_handler(array($this,"displayError"));
    
    date_default_timezone_set(self::TIMEZONE);
    
  }
  
  // ========================================
  // Return a Daily Work Order Metrics Object
  // ========================================
  
  private function getDailyWorkOrderMetrics() {

    $SQL = "select dayname(workorder_created) as day,
                   count(*)                   as count
              from view_workorders
             where date(workorder_created) >= (date(now()) - interval 6 day)
          group by day
          order by workorder_created desc";
    
    return $this->getTable("dailymetrics", $SQL);
    
  }

  // =====================================
  // Return a Monthly Call Metrics Object.
  // =====================================
  
  private function getMonthlyWorkOrderMetrics() {

    $SQL = "select monthname(workorder_created) as month,
                   count(*)                     as count
              from view_workorders
             where date(workorder_created) >= (date(now()) - interval 6 month)
             group by month
          order by workorder_created desc";
    
    return $this->getTable("monthlymetrics", $SQL);
    
  }

  // =========================================
  // Return a Weekly Work Order Metrics Object
  // =========================================
  
  private function getWeeklyWorkOrderMetrics() {

    $SQL = "select week(workorder_created) as week,
                   count(*)                as count
              from view_workorders
             where date(workorder_created) >= (date(now()) - interval 6 week)
             group by week
          order by workorder_created desc";
    
    return $this->getTable("weeklymetrics", $SQL);
    
  }
  
  // =======================
  // View Work Order Metrics
  // =======================
  
  public function viewWorkOrderMetrics() {

    $dailymetrics   = $this->getDailyWorkOrderMetrics();
    $weeklymetrics  = $this->getWeeklyWorkOrderMetrics();
    $monthlymetrics = $this->getMonthlyWorkorderMetrics();

    $XSLParms["DAILYMETRICS"]   = $dailymetrics->asXML();
    $XSLParms["WEEKLYMETRICS"]  = $weeklymetrics->asXML();
    $XSLParms["MONTHLYMETRICS"] = $monthlymetrics->asXML();

    $content = $this->applyXSL("", $this->getXSL("viewWorkOrderMetrics"), $XSLParms);

    $this->displayContent($content);
    
    exit();
    
  }
  
}

?>
