<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selid")
    $tr_id = $paramValue;
  else if ($paramName == "scopeid")
    $pw_id = $paramValue;
}

$dal = new DAL();

$sql = "SELECT PWP.id, " .
       "CASE WHEN " .
          "(LENGTH(PE.surname) < 10 && LENGTH(PE.firstname) < 10) " . 
            "THEN CONCAT(PE.surname,', ',PE.firstname) " . 
            "ELSE CONCAT(PE.surname,', ',SUBSTRING(PE.firstname,1,1)) " . 
       "END AS text, " .
       "PT.travelrole_id AS role_id, PT.id as pt_id " .
       "FROM projectworkdaypersonnel PWP, projectteamrole PTR, " .
       "personnel  PE, travel TR, personneltravel PT  " .
       "WHERE PWP.projectworkday_id = $pw_id AND TR.id = $tr_id " .
       "AND PTR.projectworkdaypersonnel_id = PWP.id AND PTR.teamrole_id <> 7 " .
       "AND PWP.personnel_id = PE.id AND PT.projectworkdaypersonnel_id = PWP.id " .
       "AND PT.travel_id = TR.id AND TR.destination = 'to' " . 
       "GROUP BY PWP.id ORDER BY surname ";
$result = $dal->query($sql);

echo json_encode($result);

?>

