Location: PHPKode > projects > OutBoard - In/Out Office Board > outboard-2.2.5/lib/OutboardTimeclock.php
<?php

// OutboardTimeclock.php
//
// Calculates and displays timeclock information
//
// 2005-02-17	richardf - Introduced in, and updated for, OutBoard 2.0
// 2001-03-16	Richard F. Feuerriegel	(hide@address.com)
//

require_once("lib/OutboardConfig.php");

Class OutboardTimeclock extends OutboardConfig {

var $forPDF = null;  // Boolean true if the output is for PDF.
var $log = null;     // The log data array generated by $ob->getLogDataArray
var $userid = null;  // The userid of the person for this calculation
var $start = null;   // The start date for the calculation
var $end = null;     // The end date for the calculation
var $totalHoursWorked = null; 
var $details = null; // The HTML output of the timeclock details;
var $summary = null; // The HTML output of the timeclock summary;

Function OutboardTimeclock($log,$userid,$start,$end) {
  $this->OutboardConfig();
  $this->log = $log;
  $this->userid = $userid;
  $this->start = $start;
  $this->end = $end;
}

Function setPDF($boolean) {
  $this->forPDF = $boolean;
}

Function getTotalHoursWorked() {
  return $this->totalHoursWorked;
}

Function getDetails() {
  return $this->details;
}

Function getSummary() {
  return $this->summary;
}


Function isIn($value) {
  if ($value == $this->getConfig('in')) { 
    return true; 
  } else {
    return false;
  }
}


Function dateToName($datetime) {
  if ($this->isIn($datetime)) { 
    return "IN";
  } elseif ($datetime == $this->getConfig('out')) {
    return "OUT";
  } else {
    return $datetime;
  }
}


Function calculate() {

  $DEBUG = false;  // Set to 'true' to turn on debugging

  $rv = "";

  // 2004-04-05	richardf - added for Jonathan
  if ($this->start == "first") { $start = "0000-00-00"; }
  if ($this->end == "last") { $end = "2030-01-01"; }

  $work_count = 0;
  $time_first_in = "";

  //----------------------------------------------
  // Go through all the log data for this person
  //
  $total_time = 0;
  $work_time = null;
  $work_date = null;
  $work_start = null;
  $work_end = null;

  if (count($this->log) > 0) {
    foreach($this->log as $row) {
      $changetime = $row['changetime'];
      $timeinseconds = $row['timeinseconds'];
      $day = $row['day'];
      $changedate = $row['changedate'];
      $back = $row['back'];
      if ($this->isIn($back)) {                 // Is the dot IN?
        if ($time_first_in == "") {            // If it has not been IN yet:
	  if ($DEBUG) { echo "[in]"; }
          $time_first_in = $changetime;
          $time_in_seconds = $timeinseconds;
          $day_first_in = $day;
          $date_first_in = $changedate;
	} else {                              // We have seen an IN already.
          if ($day != $day_first_in) {   // If not same day, Invalidate the time.
	    if ($DEBUG) { echo "[in p.i]"; }
	    $work_date[$work_count] = $date_first_in;
	    $work_start[$work_count] = $time_first_in;
	    $work_end[$work_count] = "?";
	    $work_time[$work_count] = "0";
	    $work_count++;
            $time_first_in = $changetime;
            $time_in_seconds = $timeinseconds;
            $day_first_in = $day;
            $date_first_in = $changedate;
	  }
	}
      } else {                                // This is not an IN time.
        if ($time_first_in != "") {           // If the dot was IN already
	  if ($day == $day_first_in) {   // and this is the same day:
            $time_diff_seconds       = $timeinseconds - $time_in_seconds; // current - past
	    $work_date[$work_count]  = $changedate;
	    $work_start[$work_count] = $time_first_in;
	    $work_end[$work_count]   = $changetime;
	    $work_time[$work_count]  = sprintf("%4.2f",($time_diff_seconds / 60) / 60);
	    $total_time += $work_time[$work_count];
	    $work_count++;
 	  } else {
	    $work_date[$work_count] = $date_first_in;
	    $work_start[$work_count] = $time_first_in;
	    $work_end[$work_count] = "?";
	    $work_time[$work_count] = "0";
	    $work_count++;
	  }
          $time_first_in = "";                // Empty the temp variables
	  $day_first_in = "";                 // to start a new timing block.
          $date_first_in = "";
	  if ($DEBUG) { echo "[out]"; }
	}
      }
    }

    if ($this->isIn($back)) {   // Catch a last record that is IN
       $work_date[$work_count] = $date_first_in;
       $work_start[$work_count] = $time_first_in;
       $work_end[$work_count] = "?";
       $work_time[$work_count] = "0";
    }

  } // 2004-03-29 - richardf
    // moved the table outside of the while loop so that it shows up even
    // for 0 hours.

    if ($this->forPDF) {
      $border = "1";
    } else {
      $border = "0";
    }

    //----------------------------------------------
    // Generate the table of time worked (summary)
    //
       if (! $this->forPDF) {
	 $rv .= "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD CLASS=tabline>\n";
       }
       $rv .= "<TABLE BORDER=$border CELLPADDING=3 CELLSPACING=1>\n";
       $rv .= "<tr><th colspan=4>Timeclock Summary:</th></tr>\n";
       $rv .= "<tr>";
       $rv .= "<th>Date</th>";
       $rv .= "<th>In</th>";
       $rv .= "<th>Out</th>";
       $rv .= "<th>Hours</th>";
       $rv .= "</tr>\n";
       for($i=0;$i<count($work_time);$i++) {
	 $rv .= "<tr>";
	 $rv .= "<td class=tabdatafront align=center>" .$work_date[$i] ."</td>";
	 $rv .= "<td class=tabdatafront align=center>" .$work_start[$i] ."</td>";
	 $rv .= "<td align=center class=tabdatafront>" .$work_end[$i] ."</td>";
	 $rv .= "<td align=right class=tabdatafront>" .$work_time[$i] ."</td>";
	 $rv .= "</tr>\n";
       }
       $rv .= "<tr>";
       $rv .= "<td align=right class=tabdatafront colspan=3><b>TOTAL:</b></td>";
       $rv .= "<td align=right class=tabdatafront><b>" . sprintf("%4.2f",$total_time) ."</b></td>";
       $rv .= "</tr>\n";
       $rv .= "</TABLE>\n";
       if (! $this->forPDF) { $rv .= "</TD></TR></TABLE>\n"; }


  $this->summary = $rv;

  $rv = ""; 

  $this->totalHoursWorked = $total_time;

  //-----------------------------------------------
  // If we want details, show them
  //
  if (count($this->log) > 0) {
    if (! $this->forPDF) {
      $class = "class=tabdatafront";
    } else {
      $class = "";
    }
    reset($this->log);
    if (! $this->forPDF) {
      $rv .= "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD CLASS=tabline>\n";
    } else {
      // $rv .= "<!-- NEED 6in -->\n";  // This is for VERTICAL spacing
    }
    $rv .= "<TABLE BORDER=$border CELLPADDING=3 CELLSPACING=1>\n";
    if (! $this->forPDF) {
      $rv .= "<tr><th colspan=4>Outboard Details:</th></tr>\n";
    } else {
      $rv .= "<tr><th colspan=4>Outboard Details for ".$this->userid."</th></tr>\n";
    }
    $rv .= "<tr>";
    $rv .= "<th>Date</th>";
    $rv .= "<th>Dot</th>";
    $rv .= "<th>Change</th>";
    $rv .= "<th>Remarks</th>";
    $rv .= "</tr>\n";

    $rowcount = 0;
    $previous_remarks = "";
    foreach($this->log as $row) {
      $rowcount++;
      if ($this->forPDF and ($rowcount > 20) and 0) {
        $rv .= "</TABLE><p>\n";
        $rv .= "<!--NewPage-->\n";
        $rv .= "<HR class=PAGE-BREAK>\n";
        $rv .= "Details continued...<p>\n";
        $rv .= "<TABLE BORDER=$border CELLPADDING=3 CELLSPACING=1>\n";
        $rv .= "<tr><th colspan=4>Outboard Details for ".$this->userid."</th></tr>\n";
        $rv .= "<tr>";
        $rv .= "<th>Date</th>";
        $rv .= "<th>Dot</th>";
        $rv .= "<th>Change</th>";
        $rv .= "<th>Remarks</th>";
        $rv .= "</tr>\n";
        $rowcount = 0;
      }
      $rv .= "<tr>";
      $rv .= "<td $class align=center>".$row['changedate']."</td>";
      $back = $this->dateToName($row['back']);
      if ($back != "IN" && $back != "OUT") {
        $back = $row['backtime'];
      } 
      $rv .= "<td $class align=center>$back</td>";
      $rv .= "<td $class align=center>".$row['changetime']."</td>";
      if ($row['remarks'] != $previous_remarks) { 
        $previous_remarks = $row['remarks'];
	$row['remarks'] = htmlspecialchars($row['remarks']); 
        $rv .= "<td $class>".$row['remarks']."</td>";
      } else {
        $rv .= "<td $class>&nbsp;</td>";
      }
      $rv .= "</tr>\n";
    }
    $rv .= "</TABLE>\n";
    $rv .= "</TD></TR></TABLE>\n";
  }

  $this->details = $rv;

  if ($this->details != "" and $this->summary != "") {
    return true;
  } else {
    return false;
  }
}

}

?>
Return current item: OutBoard - In/Out Office Board