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

class modCrewTimesheet {

  private $pw_id;

  public function __construct($pw_id) {
    if (isset($pw_id)) {
      $this->pw_id = $pw_id;
    }  
    else {
      $this->pw_id = 0; 
	}

    $dal = new DAL();

    $sql = "SELECT note FROM projectworkevent " .
           "WHERE id = $this->pw_id LIMIT 1" ;
    $pw_note = $dal->query($sql)->note;
print_r("note:$pw_note");    
    if ($pw_note != "") {
        
      $sql = "UPDATE projectworkdaypersonnel PWP, projectteamrole PTR " .
             "SET PTR.note='$pw_note' " .
             "WHERE PWP.projectworkevent_id = $pw_id " .
             "AND PTR.projectworkdaypersonnel_id = PWP.id AND PTR.note = '' ";
      $result = $dal->query($sql);
    } 
    
    $sql = "SELECT PE.id, 'project' as role_type, PTR.id AS put_id, " .
           "CONCAT (surname, ', ', firstname, ' ', middlename) as text, SPACE(0) AS '*', " .
           "PR.name AS project_name, PR.refno, TE.name as role, " .
           "(CASE WHEN PTR.moduration > 0 THEN PTR.moduration ELSE PW.duration END) AS duration, PTR.billable, PTR.rationale, PTR.note " .
           "FROM projectworkevent PW, projectworkdaypersonnel PWP, " .
           "personnel PE, projectparcel PP, project PR, projectteamrole PTR, teamrole TE " .
           "WHERE PW.id = $this->pw_id " .
           "AND PP.id = PW.projectparcel_id AND PR.id = PP.project_id " .
           "AND PW.id = PWP.projectworkevent_id AND PWP.id = PTR.projectworkdaypersonnel_id " .
           "AND PTR.teamrole_id = TE.id AND PWP.personnel_id = PE.id "  .
           "UNION ALL " .
           "SELECT PE.id, 'travel' as role_type, PT.id AS put_id, " .
           "CONCAT (surname, ', ', firstname, ' ', middlename) as text, SPACE(0) AS '*', " .
           "PR.name AS project_name, PR.refno, CONCAT(TV.name, ' ', TR.destination), " .
           "(CASE WHEN PT.moduration > 0 THEN PT.moduration ELSE PT.duration END) AS duration, PT.billable, PT.rationale, PT.note " .
           "FROM projectworkevent PW, projectworkdaypersonnel PWP, personnel PE, projectparcel PP, project PR, " .
           "personneltravel PT, travel TR,  travelrole AS TV " .
           "WHERE PW.id = $this->pw_id AND PP.id = PW.projectparcel_id AND PR.id = PP.project_id " .
           "AND PW.id = PWP.projectworkevent_id AND PWP.id = PT.projectworkdaypersonnel_id " .
           "AND PT.travel_id = TR.id AND TV.id = PT.travelrole_id AND PWP.personnel_id = PE.id "  .
           "ORDER BY text ";
    $this->time_sheet = $dal->query($sql);

    $currentId = 0;
    foreach ($this->time_sheet as $line) {
      if ($line->id != $currentId) {
        $line->firstOf = 1;
        $currentId = $line->id;
      }  
      else
        $line->firstOf = 0;
    }     

  
    $total_dur = 0;
    foreach (array_reverse($this->time_sheet) as $line) {
      $total_dur += $line->duration;
      if ($line->firstOf) {
        $line->total_dur = $total_dur;
        $total_dur = 0;
      }
    }
  }

  public function get($prop) {
    return $this->$prop;
  }
  

}

?>
