<?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 $today;
  private $fraction_list;
  private $minute_list;
  private $start_range;
  private $duration_range;
  private $starttime;
  
  public function __construct($lo_id) {

    $this->lo_id = (isset($lo_id)) ? $lo_id : 0;

    $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";
    $this->today          = date("Y-m-d");
    
    $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);

    $dal = new DAL();

    $sql = "SELECT name FROM login WHERE id = $this->lo_id LIMIT 1 ";
    $this->login_name = $dal->query($sql)->name;

    $sql = "SELECT id FROM workday WHERE date = '$this->today' LIMIT 1 ";
    $wo = $dal->query($sql);
    $this->wo_id = (count($wo) == 0) ? 0 : $wo->id;
    
//print_r ("wo:");
//print_r (count($wo));

    $sql = "SELECT CI.instance AS field, CF.keytype, " .
           "COL.value AS dbcolumn, LAB.value AS label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "CF.mandatory, CF.uniquekey, CF.confirm, CF.commit, CF.action, " .
           "CF.sequent, CF.sequent_frm, CF.sequent_upd, CF.sequent_ent, CF.sequent_chk " .
           "FROM controlinstance CI, controlform CF, " .
           "controlentry COL, controlentry LAB " . 
           "WHERE LAB.controlinstance_id = CI.id " .
           "AND LAB.property IN ('label', 'noLabel') " .
           "AND COL.controlinstance_id = CI.id AND COL.property = 'column' " .
           "AND CF.controlinstance_id = CI.id AND CF.formhandler_id = " .
           "(SELECT FH.id FROM controlinstance FH " .
           "WHERE FH.instance = 'frmOfficeWorkday') ";
    $this->owform_data = $dal->query($sql);    

    $sql = "SELECT CI.instance AS field, CF.keytype, " .
           "COL.value AS dbcolumn, LAB.value AS label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "CF.mandatory, CF.uniquekey, CF.confirm, CF.commit, CF.action, " .
           "CF.sequent, CF.sequent_frm, CF.sequent_upd, CF.sequent_ent, CF.sequent_chk " .
           "FROM controlinstance CI, controlform CF, " .
           "controlentry COL, controlentry LAB " . 
           "WHERE LAB.controlinstance_id = CI.id " .
           "AND LAB.property IN ('label', 'noLabel') " .
           "AND COL.controlinstance_id = CI.id AND COL.property = 'column' " .
           "AND CF.controlinstance_id = CI.id AND CF.formhandler_id = " .
           "(SELECT FH.id FROM controlinstance FH " .
           "WHERE FH.instance = 'frmOfficeProject') ";
    $this->opform_data = $dal->query($sql);    

    $sql = "SELECT CI.instance AS field, CF.keytype, " .
           "COL.value AS dbcolumn, LAB.value AS label, " .
           "0 AS newvalue, 0 AS dbvalue, '' AS newtext, '' AS dbtext, " .
           "CF.mandatory, CF.uniquekey, CF.confirm, CF.commit, CF.action, " .
           "CF.sequent, CF.sequent_frm, CF.sequent_upd, CF.sequent_ent, CF.sequent_chk " .
           "FROM controlinstance CI, controlform CF, " .
           "controlentry COL, controlentry LAB " . 
           "WHERE LAB.controlinstance_id = CI.id " .
           "AND LAB.property IN ('label', 'noLabel') " .
           "AND COL.controlinstance_id = CI.id AND COL.property = 'column' " .
           "AND CF.controlinstance_id = CI.id AND CF.formhandler_id = " .
           "(SELECT FH.id FROM controlinstance FH " .
           "WHERE FH.instance = 'frmOfficeTimesheet') ";
    $this->otform_data = $dal->query($sql);    

    $this->summary_hdr = ["0", "Project", "Ref No", "Parcel",
                          "Role", "Duration", "Billable", "Note"] ;

    $sql = "SELECT " .
           "CONCAT (PE.surname, ', ', PE.firstname, ' ', PE.middlename) as name " .
           "FROM login LO, personnel PE " .
           "WHERE LO.id = $this->lo_id AND PE.id = LO.personnel_id " .
           "LIMIT 1 ";
    $this->time_header = $dal->query($sql)->name;

    $default = array("id"=>0,
           "fkid"=>"$this->today", "fkid_text"=>"$this->today");
    $this->owform_def0 = $this->toObject ($default);

    $default = array("id"=>0,
        "workday_id"=>0,       "workday_id_text"=>"",
        "project_id"=>0,       "project_id_text"=>"", 
        "projectparcel_id"=>0, "projectparcel_id_text"=>"",
        "absent"=>0,           "absent_text"=>"No");
    $this->opform_def0 = $this->toObject ($default);

    $default = array("id"=>0,  "fkid"=>0,
        "teamrole_id"=>0,      "teamrole_id_text"=>"",
        "startime"=>"$now",    "startime_text"=>"00:00", 
        "stoptime"=>"00:00",   "stoptime_text"=>"00:00",
        "breakdur"=>"00.00",   "breakdur_text"=>"00.00", 
        "pausetime"=>"00:00",  "pausetime_text"=>"00:00", 
        "duration"=>"00.00",   "duration_text"=>"00.00", 
        "billable"=>1  ,       "billable_text"=>"Yes", 
        "billrationale"=>"",   "billrationale_text"=>"", 
        "note"=>"",            "note_text"=>"" );
    $this->otform_def0 = $this->toObject ($default);

    $absent  = array("id"=>0,
        "workday_id"=>0,       "workday_id_text"=>"",
        "project_id"=>7,       "project_id_text"=>"9999: Administrative",
        "projectparcel_id"=>0, "projectparcel_id_text"=>"",
        "absent"=>1,           "absent_text"=>"Yes");
    $this->opform_def1 = $this->toObject ($absent);

    $absent  = array("id"=>0,      "fkid"=>0,
        "teamrole_id"=>7,          "teamrole_id_text"=>"Absent",
        "startime"=>"08:30",       "startime_text"=>"08:30", 
        "stoptime"=>"16:30",       "stoptime_text"=>"16:30",
        "breakdur"=>"00.00",       "breakdur_text"=>"00.00", 
        "pausetime"=>"00:00",      "pausetime_text"=>"00:00", 
        "duration"=>"08.00",       "duration_text"=>"08.00", 
        "billable"=>0  ,           "billable_text"=>"No", 
        "billrationale"=>"Absent", "billrationale_text"=>"", 
        "note"=>"",                "note_text"=>"" );
    $this->otform_def1 = $this->toObject ($absent);

    $sql = "SELECT 0 AS id, 'Select' AS text " .
           "UNION ALL " .
           "SELECT id, name AS text " .
           "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;
  }


}  

?>
