<?php

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

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

$dal = new DAL();

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

echo json_encode($result);

?>

