You are here

function support_pm_admin_reports in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support_pm/support_pm.admin.inc \support_pm_admin_reports()
1 string reference to 'support_pm_admin_reports'
support_pm_menu in support_pm/support_pm.module
Implements hook_menu(). TODO: Include date in 'view' and 'edit' tabs

File

support_pm/support_pm.admin.inc, line 22

Code

function support_pm_admin_reports() {
  $form = array();

  // $period must be an integer
  $period = isset($_GET['rp']) ? (int) $_GET['rp'] : NULL;

  // $type must be an integer
  $type = isset($_GET['rt']) ? (int) $_GET['rt'] : NULL;

  // $roles must be a comma-separated list of integer
  $roles = isset($_GET['rr']) ? explode(',', preg_replace('/[^0-9,]/', '', $_GET['rr'])) : NULL;

  // $clients must be a comma-separated list of integer
  $clients = isset($_GET['rc']) ? explode(',', preg_replace('/[^0-9,]/', '', $_GET['rc'])) : NULL;
  if (!is_null($period) && !is_null($period)) {
    switch ($type) {
      case SUPPORT_PM_AGGREGATE:
      default:
        $report_type = 'aggregate';
        break;
      case SUPPORT_PM_USER:
        $report_type = 'user';
        break;
    }
    switch ($period) {
      case SUPPORT_PM_THISWEEK:
      default:
        $start = _support_pm_first_day(time());
        $end = $start + 86400 * 7;
        $days = 7;
        break;
      case SUPPORT_PM_LASTWEEK:
        $start = _support_pm_first_day(time() - 86400 * 7);
        $end = $start + 86400 * 7;
        $days = 7;
        break;
      case SUPPORT_PM_NEXTWEEK:
        $start = _support_pm_first_day(time() + 86400 * 7);
        $end = $start + 86400 * 7;
        $days = 7;
        break;
      case SUPPORT_PM_THISMONTH:
        $start = _support_pm_first_day_month(time());
        $end = _support_pm_last_day_month(time());
        $days = _support_pm_days_in_month(time());
        break;
      case SUPPORT_PM_LASTMONTH:
        $start = _support_pm_first_day_month(time() - 86400 * 31);
        $end = _support_pm_last_day_month(time() - 86400 * 31);
        $days = _support_pm_days_in_month(time() - 86400 * 31);
        break;
      case SUPPORT_PM_NEXTMONTH:
        $start = _support_pm_first_day_month(time() + 86400 * 31);
        $end = _support_pm_last_day_month(time() + 86400 * 31);
        $days = _support_pm_days_in_month(time() + 86400 * 31);
        break;
      case SUPPORT_PM_THISQUARTER:

        // TODO
        break;
      case SUPPORT_PM_LASTQUARTER:

        // TODO
        break;
      case SUPPORT_PM_NEXTQUARTER:

        // TODO
        break;
      case SUPPORT_PM_THISYEAR:

        // TODO
        break;
      case SUPPORT_PM_LASTYEAR:

        // TODO
        break;
      case SUPPORT_PM_NEXTYEAR:

        // TODO
        break;
    }
    $header_details = array(
      '',
    );
    $row = array(
      '<strong>' . t('Plan') . '</strong>',
    );
    $row2 = array(
      '<strong>' . t('Actual') . '</strong>',
    );
    $totals = array(
      'client_plan' => array(),
      'user_plan' => array(),
    );
    $hours_client = array();
    $hours_user = array();
    for ($i = 0; $i < $days; $i++) {
      $date = $start + 86400 * $i;
      $header_details[] = t('!day<br />!date', array(
        '!day' => format_date($date, 'custom', 'l'),
        '!date' => format_date($date, 'custom', 'M d'),
      ));
      $day = support_pm_day_load($date);
      $client_day = support_pm_aggregate_client($day);
      if (is_array($client_day)) {
        $row[] = theme('support_pm_user_client_hours_details', array(
          'day' => $client_day,
          'scale' => 64,
        ));
      }
      else {
        $row[] = '';
      }

      // Add up totals
      if (is_array($client_day)) {
        foreach ($client_day as $clid => $data) {
          if (!isset($totals['client_plan'][$clid])) {
            $totals['client_plan'][$clid] = 0;
          }
          $totals['client_plan'][$clid] += $data->hours;
        }
      }
      else {
        if (!is_array($totals['client_plan'])) {
          $totals['client_plan'] = array();
        }
      }
      $user_day = support_pm_aggregate_user($day);
      if (is_array($user_day)) {
        $row_user[] = theme('support_pm_user_client_hours_details', array(
          'day' => $user_day,
          'scale' => 64,
        ));
      }
      else {
        $row_user[] = '';
      }

      // Add up totals
      if (is_array($user_day)) {
        foreach ($user_day as $uid => $data) {
          if (!isset($totals['user_plan'][$uid])) {
            $totals['user_plan'][$uid] = 0;
          }
          $totals['user_plan'][$uid] += $data->hours;
        }
      }
      else {
        if (!is_array($totals['user_plan'])) {
          $totals['user_plan'] = array();
        }
      }

      // Integrate with the support_timer module, if enabled
      if (module_exists('support_timer')) {
        $hour_client = array();
        $hour_user = array();

        // The support_timer module uses a slightly different date format
        $convert = strtotime(date('d M Y', $date));
        $result = db_query('SELECT tt.time, t.client, n.uid FROM {support_ticket_timer} tt LEFT JOIN {support_ticket} t ON tt.nid = t.nid LEFT JOIN {node} n ON t.nid = n.nid WHERE tt.date = :date', array(
          ':date' => $convert,
        ));
        foreach ($result as $timer) {
          if (!isset($hour_client[$timer->client])) {
            $hour_client[$timer->client]->hours = 0;
            $hours_client[$timer->client]->hours = 0;
          }
          if (!isset($hour_user[$timer->uid])) {
            $hour_user[$timer->uid]->hours = 0;
            $hours_user[$timer->uid]->hours = 0;
          }
          $hour_client[$timer->client]->hours += support_pm_timer_to_hours($timer->time);
          $hours_client[$timer->client]->hours += support_pm_timer_to_hours($timer->time);
          $hour_user[$timer->uid]->hours += support_pm_timer_to_hours($timer->time);
          $hours_user[$timer->uid]->hours += support_pm_timer_to_hours($timer->time);
        }
        $result = db_query('SELECT tt.time, t.client, c.uid FROM {support_ticket_comment_timer} tt LEFT JOIN {support_ticket_comment} t ON tt.cid = t.cid LEFT JOIN {comment} c ON t.cid = c.cid WHERE tt.date = :date', array(
          ':date' => $convert,
        ));
        foreach ($result as $timer) {
          if (!isset($hour_client[$timer->client])) {
            $hour_client[$timer->client]->hours = 0;
            $hours_client[$timer->client]->hours = 0;
          }
          if (!isset($hour_user[$timer->uid])) {
            $hour_user[$timer->uid]->hours = 0;
            $hours_user[$timer->uid]->hours = 0;
          }
          $hour_client[$timer->client]->hours += support_pm_timer_to_hours($timer->time);
          $hours_client[$timer->client]->hours += support_pm_timer_to_hours($timer->time);
          $hour_user[$timer->uid]->hours += support_pm_timer_to_hours($timer->time);
          $hours_user[$timer->uid]->hours += support_pm_timer_to_hours($timer->time);
        }
        $row2[] = theme('support_pm_user_client_hours_details', array(
          'day' => $hour_client,
          'scale' => 64,
        ));
        $row_user[] = theme('support_pm_user_client_hours_details', array(
          'day' => $hour_user,
          'scale' => 64,
        ));
      }
    }
    $rows_details = array(
      $row,
    );

    // Only display actual data if support_timer is enabled to collect it
    if (count($row2) > 1) {
      $rows_details[] = $row2;
      foreach ($hours_client as $clid => $data) {

        // Add up totals
        $totals['client_actual'][$clid] = $data->hours;
      }
    }
    if (count($row_user) > 1) {
      foreach ($hours_user as $uid => $data) {

        // Add up totals
        $totals['user_actual'][$uid] = $data->hours;
      }
    }
    $header_client = array(
      t('Plan'),
    );
    $client_plan_sum = is_array($totals['client_plan']) ? array_sum($totals['client_plan']) : 0;
    $client_actual_sum = is_array($totals['client_actual']) ? array_sum($totals['client_actual']) : 0;
    $max = $client_plan_sum > $client_actual_sum ? $client_plan_sum : $client_actual_sum;
    $row = array(
      theme('support_pm_user_hours_summary', array(
        'totals' => $totals['client_plan'],
        'load_callback' => 'support_client_load',
        'max' => $max,
        'message' => t('Not scheduled'),
      )),
    );
    if (count($row2) > 1) {
      $header_client[] = t('Actual');
      $row[] = theme('support_pm_user_hours_summary', array(
        'totals' => $totals['client_actual'],
        'load_callback' => 'support_client_load',
        'max' => $max,
        'message' => t('Not worked'),
      ));
    }
    $rows_client = array(
      $row,
    );
    $header_user = array(
      t('Plan'),
    );
    $user_plan_sum = is_array($totals['user_plan']) ? array_sum($totals['user_plan']) : 0;
    $user_actual_sum = is_array($totals['user_actual']) ? array_sum($totals['user_actual']) : 0;
    $max = $user_plan_sum > $user_actual_sum ? $user_plan_sum : $user_actual_sum;
    $row = array(
      theme('support_pm_user_hours_summary', array(
        'totals' => $totals['user_plan'],
        'load_callback' => 'user_load',
        'max' => $max,
        'message' => t('Not scheduled'),
      )),
    );
    if (count($row2) > 1) {
      $header_user[] = t('Actual');
      $row[] = theme('support_pm_user_hours_summary', array(
        'totals' => $totals['user_actual'],
        'load_callback' => 'user_load',
        'max' => $max,
        'message' => t('Not worked'),
      ));
    }
    $rows_user = array(
      $row,
    );
    $form['client'] = array(
      '#type' => 'fieldset',
      '#title' => t('Client'),
    );
    $form['client']['client_data'] = array(
      '#type' => 'markup',
      '#markup' => theme('table', array(
        'header' => $header_client,
        'rows' => $rows_client,
        'attributes' => array(
          'id' => 'support_pm_summary',
        ),
      )),
    );
    $form['user'] = array(
      '#type' => 'fieldset',
      '#title' => t('User'),
    );
    $form['user']['user_data'] = array(
      '#type' => 'markup',
      '#markup' => theme('table', array(
        'header' => $header_user,
        'rows' => $rows_user,
        'attributes' => array(
          'id' => 'support_pm_summary',
        ),
      )),
    );
    $form['detail'] = array(
      '#type' => 'fieldset',
      '#title' => t('Detail'),
    );
    $form['detail']['detail_data'] = array(
      '#type' => 'markup',
      '#markup' => theme('table', array(
        'header' => $header_details,
        'rows' => $rows_details,
        'attributes' => array(
          'id' => 'support_pm_week',
        ),
      )),
    );
  }

  // TODO: Custom periods
  $form['report_period'] = array(
    '#type' => 'select',
    '#title' => t('Period'),
    //'#options' => array(SUPPORT_PM_THISWEEK => t('This week'), SUPPORT_PM_LASTWEEK => t('Last week'), SUPPORT_PM_NEXTWEEK => t('Next week'), DIVIDER1 => '--', SUPPORT_PM_THISMONTH => t('This month'), SUPPORT_PM_LASTMONTH => t('Last month'), SUPPORT_PM_NEXTMONTH => t('Next month'), DIVIDER2 => '--', SUPPORT_PM_THISQUARTER => t('This quarter'), SUPPORT_PM_LASTQUARTER => t('Last quarter'), SUPPORT_PM_NEXTQUARTER => t('Next quarter'), DIVIDER3 => '--', SUPPORT_PM_THISYEAR => t('This year'), SUPPORT_PM_LASTYEAR => t('Last year'), SUPPORT_PM_NEXTYEAR => t('Next year')),
    '#options' => array(
      SUPPORT_PM_THISWEEK => t('This week'),
      SUPPORT_PM_LASTWEEK => t('Last week'),
      SUPPORT_PM_NEXTWEEK => t('Next week'),
      DIVIDER1 => '--',
      SUPPORT_PM_THISMONTH => t('This month'),
      SUPPORT_PM_LASTMONTH => t('Last month'),
      SUPPORT_PM_NEXTMONTH => t('Next month'),
    ),
    '#default_value' => !is_null($period) ? $period : 0,
    '#description' => t('Select the duration of your report.'),
    '#required' => TRUE,
  );
  $form['report_type'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    //'#options' => array(SUPPORT_PM_AGGREGATE => t('Aggregate'), SUPPORT_PM_USER => t('Per-user')),
    '#options' => array(
      SUPPORT_PM_AGGREGATE => t('Aggregate'),
    ),
    '#default_value' => !is_null($type) ? $type : 0,
    '#description' => t("Select the type of report to generate."),
    '#required' => TRUE,
  );
  $form['optional'] = array(
    '#type' => 'fieldset',
    '#title' => t('Optional'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['optional']['report_role'] = array(
    '#type' => 'select',
    '#title' => t('Role(s)'),
    '#multiple' => TRUE,
    '#options' => user_roles(),
    '#description' => t('Optionally limit your report to one or more roles.'),
  );
  $available_clients = _support_available_clients();
  if (empty($available_clients)) {
    drupal_set_message(t('You must !create a client before you can generate reports.', array(
      '!create' => l(t('create and enable'), 'admin/support/clients/add'),
    )), 'error');
  }
  $form['optional']['report_client'] = array(
    '#type' => 'select',
    '#title' => t('Client(s)'),
    '#multiple' => TRUE,
    '#options' => $available_clients,
    '#description' => t('Optionally limit your report to one or more clients.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Generate report'),
  );
  return $form;
}