You are here

function support_pm_plan_overview_weekly in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support_pm/support_pm.module \support_pm_plan_overview_weekly()
1 string reference to 'support_pm_plan_overview_weekly'
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.module, line 437
Support Project Management. @author Jeremy Andrews <jeremy@tag1consulting.com> @package Support

Code

function support_pm_plan_overview_weekly($account) {
  $output = NULL;
  $week = isset($_GET['week']) ? _support_pm_first_day((int) $_GET['week']) : _support_pm_first_day(time());

  // Set the page title (keep it consisten with the editing page)
  drupal_set_title(t('@start - @end', array(
    '@start' => format_date($week, 'medium'),
    '@end' => format_date($week + 86400 * 6, 'medium'),
  )));
  $header = array(
    '',
  );
  $row = array(
    '<strong>' . t('Plan') . '</strong>',
  );
  $row2 = array(
    '<strong>' . t('Actual') . '</strong>',
  );
  $totals = array(
    'plan' => array(),
  );
  $hours = array();

  // TODO: Allow for 5 day weeks, etc
  for ($i = 0; $i < 7; $i++) {
    $date = $week + 86400 * $i;
    $header[] = t('!day<br />!date', array(
      '!day' => format_date($date, 'custom', 'l'),
      '!date' => format_date($date, 'custom', 'M d'),
    ));
    $day = support_pm_day_load($date, $account);
    if (isset($day[$account->uid]) && is_array($day[$account->uid])) {
      $row[] = theme('support_pm_user_client_hours_details', array(
        'day' => $day[$account->uid],
      ));
    }
    else {
      $row[] = '';
    }

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

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

      // 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 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 AND n.uid = :uid', array(
        ':date' => $convert,
        ':uid' => $account->uid,
      ));
      foreach ($result as $timer) {
        if (!isset($hour[$timer->client])) {
          $hour[$timer->client] = new stdClass();
        }
        if (!isset($hours[$timer->client])) {
          $hours[$timer->client] = new stdClass();
        }
        $hour[$timer->client]->hours += support_pm_timer_to_hours($timer->time);
        $hours[$timer->client]->hours += support_pm_timer_to_hours($timer->time);
      }
      $result = db_query('SELECT tt.time, t.client 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 AND c.uid = :uid', array(
        ':date' => $convert,
        ':uid' => $account->uid,
      ));
      foreach ($result as $timer) {
        if (!isset($hour[$timer->client])) {
          $hour[$timer->client] = new stdClass();
        }
        if (!isset($hours[$timer->client])) {
          $hours[$timer->client] = new stdClass();
        }
        $hour[$timer->client]->hours += support_pm_timer_to_hours($timer->time);
        $hours[$timer->client]->hours += support_pm_timer_to_hours($timer->time);
      }
      $row2[] = theme('support_pm_user_client_hours_details', array(
        'day' => $hour,
      ));
    }
  }
  $rows = array(
    $row,
  );

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

      // Add up totals
      $totals['actual'][$clid] = $data->hours;
    }
  }
  $output = theme('support_pm_pager', array(
    'text' => t('‹ previous'),
    'op' => '<',
  ));
  $header2 = array(
    t('Plan'),
  );
  $plan_sum = is_array($totals['plan']) ? array_sum($totals['plan']) : 0;
  $actual_sum = isset($totals['actual']) && is_array($totals['actual']) ? array_sum($totals['actual']) : 0;
  $max = $plan_sum > $actual_sum ? $plan_sum : $actual_sum;
  $row = array(
    theme('support_pm_user_hours_summary', array(
      'totals' => $totals['plan'],
      'load_callback' => 'support_client_load',
      'max' => $max,
      'message' => t('Not scheduled'),
    )),
  );
  if (count($row2) > 1) {
    $header2[] = t('Actual');
    $row[] = theme('support_pm_user_hours_summary', array(
      'totals' => isset($totals['actual']) ? $totals['actual'] : NULL,
      'load_callback' => 'support_client_load',
      'max' => $max,
      'message' => t('Not worked'),
    ));
  }
  $rows2 = array(
    $row,
  );
  $output .= theme('table', array(
    'header' => $header2,
    'rows' => $rows2,
    'attributes' => array(
      'id' => 'support_pm_summary',
    ),
  ));
  $output .= theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'id' => 'support_pm_week',
    ),
  ));
  $header = array(
    t('Client'),
  );
  if (function_exists('imagecreatetruecolor')) {
    $header[] = t('Color');
  }
  $header[] = t('Planned');
  $header[] = t('Worked');
  $header[] = t('Difference');
  if (isset($totals['plan']) || isset($totals['actual'])) {
    if (isset($totals['plan']) && isset($totals['actual'])) {
      $all_clients = $totals['plan'] + $totals['actual'];
    }
    else {
      $all_clients = isset($totals['plan']) ? $totals['plan'] : $totals['actual'];
    }
  }
  else {
    $all_clients = array();
  }
  $rows = $clients = array();
  foreach ($all_clients as $clid => $data) {
    $client = support_client_load($clid);
    $clients[$clid] = $client->name;
  }
  asort($clients);
  $rows = array();
  foreach ($clients as $clid => $name) {
    $row = array();
    $client = support_client_load($clid);
    $row[] = l($name, "support/{$client->path}");
    $comments = db_query('SELECT comment FROM {support_plan} WHERE clid = :client AND uid = :uid AND day = :day', array(
      ':client' => $clid,
      ':uid' => $account->uid,
      ':day' => $week,
    ))
      ->fetchField();
    if (!empty($comments)) {
      $row[0] .= '<br />' . check_plain($comments);
    }
    if (function_exists('imagecreatetruecolor')) {
      $row[] = "<img src='" . url('support_pm/image/') . support_pm_chartapi_color($clid) . "' alt='swatch' height='15' width='15' />";
    }
    $row[] = isset($totals['plan'][$clid]) ? number_format($totals['plan'][$clid], 2) : "0.00";
    $row[] = isset($totals['actual'][$clid]) ? number_format($totals['actual'][$clid], 2) : "0.00";
    if (isset($totals['plan'][$clid])) {
      if (isset($totals['actual'][$clid])) {
        $diff = $totals['actual'][$clid] - $totals['plan'][$clid];
      }
      else {
        $diff = -$totals['plan'][$clid];
      }
    }
    else {
      if (isset($totals['actual'][$clid])) {
        $diff = $totals['actual'][$clid];
      }
      else {
        $diff = 0.0;
      }
    }
    $plan = isset($totals['plan'][$clid]) ? $totals['plan'][$clid] : 0;
    $row[] = theme('support_pm_plan_diff', array(
      'diff' => $diff,
      'plan' => $plan,
    ));
    $rows[] = $row;
  }
  $row = array(
    '<strong>' . t('Total') . '</strong>',
  );
  if (function_exists('imagecreatetruecolor')) {
    $row[] = "<img src='" . url('support_pm/image/DDDDDD') . "' alt='swatch' height='15' width='15' />";
  }
  $plan = isset($totals['plan']) ? array_sum($totals['plan']) : 0;
  $actual = isset($totals['actual']) ? array_sum($totals['actual']) : 0;
  $row[] = '<strong>' . number_format($plan, 2) . '</strong>';
  $row[] = '<strong>' . number_format($actual, 2) . '</strong>';
  $diff = $actual - $plan;
  $row[] = '<strong>' . theme('support_pm_plan_diff', array(
    'diff' => $diff,
    'plan' => $plan,
  ));
  $rows[] = $row;
  $output .= theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'id' => 'support_pm_clients',
    ),
  ));
  $output .= theme('support_pm_pager', array(
    'text' => t('next ›'),
    'op' => '>',
  ));
  return $output;
}