<?php

defined('PUBLIC_ACCESS') or die('Restricted Access');
 
require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

class modOfficeTimesheet  { //One variable for each select list.
  private $workday_id;
  private $date;
  private $parcel_name;
  private $project_id;
  private $project_name;
  private $team_role;
  private $start_time;
  private $end_time;
  private $duration;
  private $office_timesheet;   //Variable to assign summary table to?
  private $projectworkdaypersonnel_id;   //Variable to hold db search id.
  private $lo_id;

  private $today;
  private $fraction_list;
  private $minute_list;
  private $start_range;
  private $duration_range;
  private $starttime;
  
  public function __construct($lo_id) {  //Uses hard-coded projectworkdaypersonnel_id for testing.

    if (isset($lo_id))
      $this->lo_id = $lo_id;
    else
      $this->lo_id = 0;


    $this->today          = date("Y-m-d");
    $this->fraction_list  = "00,25,50,75";
    $this->minute_list    = "00,15,30,45";
    $this->start_range    = "00,24";
    $this->duration_range = "00,09";
    $this->starttime      = "08:30";
    $this->endtime        = "16:30";

    $dal = new DAL();

    $sql = "SELECT PTR.id, PR.name AS project, PR.refno AS ref_no, PA.name AS parcel, " .
           "TR.name AS role, PTR.moduration, " .
           "CASE WHEN PTR.billable = 1 THEN 'Yes' ELSE 'No' END AS billable, PTR.note " .
           "FROM workday WO, projectworkday PW, projectworkdaypersonnel PWP, " .
           "parcel PA, project PR, projectteamrole PTR, teamrole TR " . 
           "WHERE WO.date = '$this->today' AND PW.workday_id = WO.id AND PW.login_id = $this->lo_id " .
           "AND PWP.projectworkday_id = PW.id AND PTR.projectworkdaypersonnel_id = PWP.id AND TR.id = PTR.teamrole_id  " .
           "AND PA.id = PW.parcel_id AND PR.id = PA.project_id ORDER BY PTR.start_time DESC ";
    $this->timesheet = $dal->query($sql);
 
    if (null == $this->timesheet) {
      $default = array('1' => array("pw_id"=>"0", "Project"=>"", "RefNo"=>"","Parcel"=>"",
                       "Role"=>"", "Duration"=>"", "Billable"=>"", "Note"=>""));
      $this->timesheet = $this->toObject ($default);
    }

    $hours = date("H");
    $minutes = date("i");
    $m = ((($minutes + 7.5)/15 | 0) * 15) % 60;
    $h = (((($minutes/105) + .5) | 0) + $hours) % 24;
    $now = sprintf("%02d", $h) . ':' . sprintf("%02d", $m);

    $default = array("id"=>"0", "awayfromwork"=>"0", "liveprojectname"=>"", "rowliveprojectname"=>"0",
                     "parcel_id"=>"0", "role_id"=>"0", "rolestarttime"=>$now, "roleendtime"=>"00:00",
                     "breakduration"=>"00.00", "hoursduration"=>"00.00", "billabletime"=>"1", "statusrationale"=>"", "officenote"=>"" );
    $this->form_default = $this->toObject ($default);

    $absent = array("id"=>"0", "liveprojectname"=>"Administrative", "rowliveprojectname"=>"7",
                    "parcel_id"=>"4",  "role_id"=>"7", "rolestarttime"=>$this->starttime, "roleendtime"=>$this->endtime, 
                    "breakduration"=>"00.00", "hoursduration"=>"08.00", "billabletime"=>"0", "statusrationale"=>"", "officenote"=>"" );
    $this->form_absent = $this->toObject ($absent);

    $sql = "SELECT id, name AS descr FROM teamrole"; 
    $this->team_roles = $dal->query($sql);
  }
  
  public function get($prop) {
    return $this->$prop;
  }

  private function toObject($array) {
    $object = new stdClass();
    foreach ($array as $key => $value) {
      if (is_array($value)) {
        $value = $this->toObject($value);
      }
      $object->$key = $value;
    }
    return $object;
  }


}  

?>
