[ Index ]

Source Code Reference for V1.00

title

Body

[close]

/modules/reports/reports/ -> allocateduserhours.php (source)

   1  <?php
   2  if (!defined('W2P_BASE_DIR')) {
   3      die('You should not access this file directly.');
   4  }
   5  global $AppUI, $cal_sdf;
   6  $AppUI->loadCalendarJS();
   7  
   8  $coarseness = w2PgetParam($_POST, 'coarseness', 1);
   9  $do_report = w2PgetParam($_POST, 'do_report', 0);
  10  $hideNonWd = w2PgetParam($_POST, 'hideNonWd', 0);
  11  $log_start_date = w2PgetParam($_POST, 'log_start_date', 0);
  12  $log_end_date = w2PgetParam($_POST, 'log_end_date', 0);
  13  $use_assigned_percentage = w2PgetParam($_POST, 'use_assigned_percentage', 0);
  14  $user_id = w2PgetParam($_POST, 'user_id', $AppUI->user_id);
  15  
  16  // create Date objects from the datetime fields

  17  $start_date = intval($log_start_date) ? new CDate($log_start_date) : new CDate(date('Y-m-01'));
  18  $end_date = intval($log_end_date) ? new CDate($log_end_date) : new CDate();
  19  
  20  $end_date->setTime(23, 59, 59);
  21  ?>
  22  
  23  <script language="javascript">
  24  function setDate( frm_name, f_date ) {
  25      fld_date = eval( 'document.' + frm_name + '.' + f_date );
  26      fld_real_date = eval( 'document.' + frm_name + '.' + 'log_' + f_date );
  27      if (fld_date.value.length>0) {
  28        if ((parseDate(fld_date.value))==null) {
  29              alert('The Date/Time you typed does not match your prefered format, please retype.');
  30              fld_real_date.value = '';
  31              fld_date.style.backgroundColor = 'red';
  32          } else {
  33              fld_real_date.value = formatDate(parseDate(fld_date.value), 'yyyyMMdd');
  34              fld_date.value = formatDate(parseDate(fld_date.value), '<?php echo $cal_sdf ?>');
  35              fld_date.style.backgroundColor = '';
  36            }
  37      } else {
  38            fld_real_date.value = '';
  39      }
  40  }
  41  </script>
  42  
  43  <form name="editFrm" action="index.php?m=reports" method="post">
  44  <input type="hidden" name="project_id" value="<?php echo $project_id; ?>" />
  45  <input type="hidden" name="report_category" value="<?php echo $report_category; ?>" />
  46  <input type="hidden" name="report_type" value="<?php echo $report_type; ?>" />
  47  
  48  <?php
  49  if (function_exists('styleRenderBoxTop')) {
  50      echo styleRenderBoxTop();
  51  }
  52  ?>
  53  <table cellspacing="0" cellpadding="4" border="0" width="100%" class="std">
  54  
  55  
  56  <tr>
  57      <td nowrap="nowrap"><?php echo $AppUI->_('For period'); ?>:
  58          <input type="hidden" name="log_start_date" id="log_start_date" value="<?php echo $start_date ? $start_date->format(FMT_TIMESTAMP_DATE) : ''; ?>" />
  59          <input type="text" name="start_date" id="start_date" onchange="setDate('editFrm', 'start_date');" value="<?php echo $start_date ? $start_date->format($df) : ''; ?>" class="text" />
  60          <a href="javascript: void(0);" onclick="return showCalendar('start_date', '<?php echo $df ?>', 'editFrm', null, true)">
  61              <img src="<?php echo w2PfindImage('calendar.gif'); ?>" width="24" height="12" alt="<?php echo $AppUI->_('Calendar'); ?>" border="0" />
  62          </a>
  63      </td>
  64      <td nowrap="nowrap"><?php echo $AppUI->_('to'); ?>
  65          <input type="hidden" name="log_end_date" id="log_end_date" value="<?php echo $end_date ? $end_date->format(FMT_TIMESTAMP_DATE) : ''; ?>" />
  66          <input type="text" name="end_date" id="end_date" onchange="setDate('editFrm', 'end_date');" value="<?php echo $end_date ? $end_date->format($df) : ''; ?>" class="text" />
  67          <a href="javascript: void(0);" onclick="return showCalendar('end_date', '<?php echo $df ?>', 'editFrm', null, true)">
  68              <img src="<?php echo w2PfindImage('calendar.gif'); ?>" width="24" height="12" alt="<?php echo $AppUI->_('Calendar'); ?>" border="0" />
  69          </a>
  70      </td>
  71      <td nowrap='nowrap'>
  72         <input type="radio" name="coarseness" value="1" <?php if ($coarseness == 1)
  73      echo "checked" ?> />
  74         <?php echo $AppUI->_('Days'); ?>
  75         <input type="radio" name="coarseness" value="7" <?php if ($coarseness == 7)
  76      echo "checked" ?> />
  77         <?php echo $AppUI->_('Weeks'); ?>
  78  </td>
  79      <td nowrap='nowrap'>
  80         <?php
  81  echo $AppUI->_('Tasks created by');
  82  echo ' ';
  83  echo getUsersCombo($user_id);
  84  ?>
  85      </td>
  86  </tr>
  87  <tr>
  88      <td nowrap="nowrap">
  89          <input type="checkbox" name="log_all_projects" id="log_all_projects" <?php if ($log_all_projects)
  90      echo 'checked="checked"' ?> />
  91          <label for="log_all_projects"><?php echo $AppUI->_('Log All Projects'); ?></label>
  92      </td>    
  93      <td nowrap="nowrap">
  94         <input type="checkbox" name="use_assigned_percentage" id="use_assigned_percentage" <?php if ($use_assigned_percentage)
  95      echo 'checked="checked"' ?> />
  96         <label for="use_assigned_percentage"><?php echo $AppUI->_('Use assigned percentage'); ?></label>
  97      </td>    
  98      <td nowrap="nowrap">
  99         <input type="checkbox" name="hideNonWd" id="hideNonWd" <?php if ($hideNonWd)
 100      echo 'checked="checked"' ?> />
 101         <label for="hideNonWd"><?php echo $AppUI->_('Hide non-working days'); ?></label>
 102      </td>    
 103      <td align="right" width="50%" nowrap="nowrap">
 104          <input class="button" type="submit" name="do_report" value="<?php echo $AppUI->_('submit'); ?>" />
 105      </td>
 106  </tr>
 107  </table>
 108  </form>
 109  <?php
 110  if ($do_report) {
 111  
 112      if (function_exists('styleRenderBoxBottom')) {
 113          echo styleRenderBoxBottom();
 114      }
 115      echo '<br />';
 116      if (function_exists('styleRenderBoxTop')) {
 117          echo styleRenderBoxTop();
 118      }
 119      echo '<table cellspacing="0" cellpadding="4" border="0" width="100%" class="std">
 120  <tr>
 121      <td>';
 122  
 123      // Let's figure out which users we have

 124      $q = new DBQuery;
 125      $q->addTable('users', 'u');
 126      $q->addQuery('u.user_id, u.user_username, contact_first_name, contact_last_name');
 127      $q->addJoin('contacts', 'c', 'u.user_contact = contact_id', 'inner');
 128      $user_list = $q->loadHashList('user_id');
 129      $q->clear();
 130  
 131      $q = new DBQuery;
 132      $q->addTable('tasks', 't');
 133      $q->addTable('user_tasks', 'ut');
 134      $q->addTable('projects', 'pr');
 135      $q->addQuery('t.*, ut.*, pr.project_name');
 136      $q->addWhere('( task_start_date
 137                 BETWEEN \'' . $start_date->format(FMT_DATETIME_MYSQL) . '\' 
 138                      AND \'' . $end_date->format(FMT_DATETIME_MYSQL) . '\' 
 139                 OR task_end_date    BETWEEN \'' . $start_date->format(FMT_DATETIME_MYSQL) . '\' 
 140                      AND \'' . $end_date->format(FMT_DATETIME_MYSQL) . '\' 
 141             OR ( task_start_date <= \'' . $start_date->format(FMT_DATETIME_MYSQL) . '\'
 142                      AND task_end_date >= \'' . $end_date->format(FMT_DATETIME_MYSQL) . '\') )');
 143      $q->addWhere('task_end_date IS NOT NULL');
 144      $q->addWhere('task_end_date <> \'0000-00-00 00:00:00\'');
 145      $q->addWhere('task_start_date IS NOT NULL');
 146      $q->addWhere('task_start_date <> \'0000-00-00 00:00:00\'');
 147      $q->addWhere('task_dynamic <> 1');
 148      $q->addWhere('task_milestone = 0');
 149      $q->addWhere('task_duration  > 0');
 150      $q->addWhere('t.task_project = pr.project_id');
 151      $q->addWhere('t.task_id = ut.task_id');
 152      $q->addWhere('pr.project_active = 1');
 153      if (($template_status = w2PgetConfig('template_projects_status_id')) != '') {
 154          $q->addWhere('pr.project_status <> ' . (int)$template_status);
 155      }
 156  
 157      if ($user_id) {
 158          $q->addWhere('t.task_owner = ' . (int)$user_id);
 159      }
 160      if ($project_id != 0) {
 161          $q->addWhere('t.task_project = ' . (int)$project_id);
 162      }
 163  
 164      $proj = &new CProject;
 165      $proj->setAllowedSQL($AppUI->user_id, $q, null, 'pr');
 166  
 167      $obj = &new CTask;
 168      $obj->setAllowedSQL($AppUI->user_id, $q);
 169  
 170      $task_list_hash = $q->loadHashList('task_id');
 171  
 172      $q->clear();
 173  
 174      $task_list = array();
 175      $fetched_projects = array();
 176      foreach ($task_list_hash as $task_id => $task_data) {
 177          $task = new CTask();
 178          $task->bind($task_data);
 179          $task_list[] = $task;
 180          $fetched_projects[$task->task_project] = $task_data['project_name'];
 181      }
 182  
 183      $user_usage = array();
 184      $task_dates = array();
 185  
 186      $actual_date = $start_date;
 187      $days_header = ''; // we will save days title here

 188  
 189      $user_tasks_counted_in = array();
 190      $user_names = array();
 191  
 192      if (count($task_list) == 0) {
 193          echo '<p>' . $AppUI->_('No data available') . '</p>';
 194      } else {
 195          foreach ($task_list as $task) {
 196              $task_start_date = new CDate($task->task_start_date);
 197              $task_end_date = new CDate($task->task_end_date);
 198  
 199              $day_difference = $task_end_date->dateDiff($task_start_date);
 200              $actual_date = $task_start_date;
 201  
 202              $users = $task->getAssignedUsers();
 203  
 204              if ($coarseness == 1) {
 205                  userUsageDays();
 206              } elseif ($coarseness == 7) {
 207                  userUsageWeeks();
 208              }
 209  
 210          }
 211  
 212          if ($coarseness == 1) {
 213              showDays();
 214          } elseif ($coarseness == 7) {
 215              showWeeks();
 216          }
 217  ?>
 218              <center><table class="std">
 219              <?php echo $table_header . $table_rows; ?>
 220              </table>
 221              <table width="100%"><tr><td align="center">
 222          <?php
 223  
 224  
 225          echo '<h4>' . $AppUI->_('Total capacity for shown users') . '</h4>';
 226          echo $AppUI->_('Allocated hours') . ': ' . number_format($allocated_hours_sum, 2) . '<br />';
 227          echo $AppUI->_('Total capacity') . ': ' . number_format($total_hours_capacity, 2) . '<br />';
 228          echo $AppUI->_('Percentage used') . ': ' . (($total_hours_capacity > 0) ? number_format($allocated_hours_sum / $total_hours_capacity, 2) * 100 : 0) . '%<br />';
 229  ?>
 230              </td>
 231              <td align="center">
 232          <?php
 233  
 234  
 235          echo '<h4>' . $AppUI->_('Total capacity for all users') . '</h4>';
 236          echo $AppUI->_('Allocated hours') . ': ' . number_format($allocated_hours_sum, 2) . '<br />';
 237          echo $AppUI->_('Total capacity') . ': ' . number_format($total_hours_capacity_all, 2) . '<br />';
 238          echo $AppUI->_('Percentage used') . ': ' . (($total_hours_capacity_all > 0) ? number_format($allocated_hours_sum / $total_hours_capacity_all, 2) * 100 : 0) . '%<br />';
 239      }
 240  ?>
 241         </td></tr>
 242         </table>
 243         </center>
 244  <?php
 245      foreach ($user_tasks_counted_in as $user_id => $project_information) {
 246          echo '<b>' . $user_names[$user_id] . '</b><br /><blockquote>';
 247          echo '<table width="50%" border="1" class="std">';
 248          foreach ($project_information as $project_id => $task_information) {
 249              echo '<tr><th colspan="3"><span style="font-weight:bold; font-size:110%">' . $fetched_projects[$project_id] . '</span></th></tr>';
 250  
 251              $project_total = 0;
 252              foreach ($task_information as $task_id => $hours_assigned) {
 253                  echo '<tr><td>&nbsp;</td><td>' . $task_list_hash[$task_id]['task_name'] . '</td><td style="text-align:right;">' . number_format(round($hours_assigned, 2), 2) . ' hrs</td></tr>';
 254                  $project_total += round($hours_assigned, 2);
 255              }
 256              echo '<tr><td colspan="2" align="right"><b>' . $AppUI->_('Total assigned') . '</b></td><td style="text-align:right;"><b>' . number_format($project_total, 2) . ' hrs</b></td></tr>';
 257  
 258          }
 259          echo '</table></blockquote>';
 260      }
 261      echo '</td>
 262  </tr>
 263  </table>';
 264  }
 265  
 266  function userUsageWeeks() {
 267      global $task_start_date, $task_end_date, $day_difference, $hours_added, $actual_date, $users, $user_data, $user_usage, $use_assigned_percentage, $user_tasks_counted_in, $task, $start_date, $end_date;
 268  
 269      $task_duration_per_week = $task->getTaskDurationPerWeek($use_assigned_percentage);
 270      $ted = new CDate(Date_Calc::endOfWeek($task_end_date->day, $task_end_date->month, $task_end_date->year));
 271      $tsd = new CDate(Date_Calc::beginOfWeek($task_start_date->day, $task_start_date->month, $task_start_date->year));
 272      $ed = new CDate(Date_Calc::endOfWeek($end_date->day, $end_date->month, $end_date->year));
 273      $sd = new CDate(Date_Calc::beginOfWeek($start_date->day, $start_date->month, $start_date->year));
 274  
 275      $week_difference = $end_date->workingDaysInSpan($start_date) / count(explode(',', w2PgetConfig('cal_working_days')));
 276  
 277      $actual_date = $start_date;
 278  
 279      for ($i = 0; $i <= $week_difference; $i++) {
 280          if (!$actual_date->before($tsd) && !$actual_date->after($ted)) {
 281              $awoy = $actual_date->year . Date_Calc::weekOfYear($actual_date->day, $actual_date->month, $actual_date->year);
 282              foreach ($users as $user_id => $user_data) {
 283                  if (!isset($user_usage[$user_id][$awoy])) {
 284                      $user_usage[$user_id][$awoy] = 0;
 285                  }
 286                  $percentage_assigned = $use_assigned_percentage ? ($user_data['perc_assignment'] / 100) : 1;
 287                  $hours_added = $task_duration_per_week * $percentage_assigned;
 288                  $user_usage[$user_id][$awoy] += $hours_added;
 289                  if ($user_usage[$user_id][$awoy] < 0.005) {
 290                      //We want to show at least 0.01 even when the assigned time is very small so we know

 291                      //that at that time the user has a running task

 292                      $user_usage[$user_id][$awoy] += 0.006;
 293                      $hours_added += 0.006;
 294                  }
 295  
 296                  // Let's register the tasks counted in for calculation

 297                  if (!array_key_exists($user_id, $user_tasks_counted_in)) {
 298                      $user_tasks_counted_in[$user_id] = array();
 299                  }
 300  
 301                  if (!array_key_exists($task->task_project, $user_tasks_counted_in[$user_id])) {
 302                      $user_tasks_counted_in[$user_id][$task->task_project] = array();
 303                  }
 304  
 305                  if (!array_key_exists($task->task_id, $user_tasks_counted_in[$user_id][$task->task_project])) {
 306                      $user_tasks_counted_in[$user_id][$task->task_project][$task->task_id] = 0;
 307                  }
 308                  // We add it up

 309                  $user_tasks_counted_in[$user_id][$task->task_project][$task->task_id] += $hours_added;
 310              }
 311          }
 312          $actual_date->addSeconds(168 * 3600); // + one week

 313      }
 314  }
 315  
 316  function showWeeks() {
 317      global $allocated_hours_sum, $end_date, $start_date, $AppUI, $user_list, $user_names, $user_usage, $hideNonWd, $table_header, $table_rows, $df, $working_days_count, $total_hours_capacity, $total_hours_capacity_all;
 318  
 319      $working_days_count = 0;
 320      $allocated_hours_sum = 0;
 321  
 322      $ed = new CDate(Date_Calc::endOfWeek($end_date->day, $end_date->month, $end_date->year));
 323      $sd = new CDate(Date_Calc::beginOfWeek($start_date->day, $start_date->month, $start_date->year));
 324  
 325      $week_difference = ceil($ed->workingDaysInSpan($sd) / count(explode(',', w2PgetConfig('cal_working_days'))));
 326  
 327      $actual_date = $sd;
 328  
 329      $table_header = '<tr><th>' . $AppUI->_('User') . '</th>';
 330      for ($i = 0; $i < $week_difference; $i++) {
 331          //$table_header .= "<th>".Date_Calc::weekOfYear($actual_date->day, $actual_date->month, $actual_date->year)."<br><table><td style='font-weight:normal; font-size:70%'>".$actual_date->format( $df )."</td></table></th>";

 332          $actual_date->addSeconds(168 * 3600); // + one week

 333          $working_days_count = $working_days_count + count(explode(',', w2PgetConfig('cal_working_days')));
 334      }
 335      $table_header .= '<th nowrap="nowrap" colspan="2">' . $AppUI->_('Allocated') . '</th></tr>';
 336  
 337      $table_rows = '';
 338  
 339      foreach ($user_list as $user_id => $user_data) {
 340          $user_names[$user_id] = $user_data['contact_first_name'] . ' ' . $user_data['contact_last_name'];
 341          if (isset($user_usage[$user_id])) {
 342              $table_rows .= '<tr><td nowrap="nowrap">(' . $user_data['user_username'] . ') ' . $user_data['contact_first_name'] . ' ' . $user_data['contact_last_name'] . '</td>';
 343              $actual_date = $sd;
 344              /*

 345              for($i=0; $i<$week_difference; $i++){    

 346              $awoy = $actual_date->year.Date_Calc::weekOfYear($actual_date->day,$actual_date->month,$actual_date->year);

 347  

 348              $table_rows .= "<td align='right'>";

 349              if(isset($user_usage[$user_id][$awoy])){

 350              $hours = number_format($user_usage[$user_id][$awoy],2);

 351              $table_rows .= $hours;

 352              $percentage_used = round(($hours/(w2PgetConfig('daily_working_hours')*count(explode(',',w2PgetConfig('cal_working_days')))) )*100);

 353              $bar_color       = "blue";

 354              if($percentage_used > 100){

 355              $bar_color = "red";

 356              $percentage_used = 100;

 357              }

 358              $table_rows .= "<div style='height:2px;width:$percentage_used%; background-color:$bar_color'>&nbsp;</div>";

 359              } else {

 360              $table_rows .= "&nbsp;";

 361              } 

 362              $table_rows .= "</td>";

 363  

 364              $actual_date->addSeconds(168*3600);    // + one week

 365              }

 366              */
 367              $array_sum =