<?php
include_once '../tool/projeqtor.php';
include_once('../tool/formatter.php');

$user=getSessionUser();
// ---------------------------
// Filters
// ---------------------------
$globalCrit="";
$headerParameters="";
$startDate=(RequestHandler::isCodeSet('startDate'))?RequestHandler::getValue('startDate'):false;
$endDate=(RequestHandler::isCodeSet('endDate'))?RequestHandler::getValue('endDate'):false;
$idOrganization=(RequestHandler::isCodeSet('idOrganization'))?trim(RequestHandler::getValue('idOrganization')):false;
$idTeam=(RequestHandler::isCodeSet('idTeam'))?trim(RequestHandler::getValue('idTeam')):false;



if ($startDate) {
  $week=weekFormat($startDate);
  $startWeek=substr($week,-2);
  $yearStart=substr($week,0,4);
  $firstDay = date('Y-m-d', firstDayofWeek($startWeek,$yearStart));
  $whereStartDate=" and (endDate is null or endDate>='$startDate')";
  $headerParameters.= i18n("colStartDate") . ' : ' . htmlFormatDate($firstDay) . '<br/>';
}
if ($endDate) {
  $week=weekFormat($endDate);
  $endWeek=substr($week,-2);
  $yearEnd=substr($week,0,4);
  $lastDay = lastDayofWeek($endWeek,$yearEnd);
  $whereEndDate=" and (startDate is null or startDate<='$endDate')";
  $headerParameters.= i18n("colEndDate") . ' : ' . htmlFormatDate($lastDay) . '<br/>';
}
$whereOrga="";
if ($idOrganization and $idOrganization!='') {
  $whereOrga=" and idOrganization= $idOrganization";
  $headerParameters.= i18n("colIdOrganization") . ' : ' . htmlEncode(SqlList::getNameFromId('Organization',$idOrganization)) . '<br/>';
}
$whereTeam="";
if ($idTeam and $idTeam!='') {
  $whereTeam=" and idTeam= $idTeam";
  $headerParameters.= i18n("colIdTeam") . ' : ' . htmlEncode(SqlList::getNameFromId('Team',$idTeam)) . '<br/>';
}

include "header.php";

if(!$startDate or !$endDate){
  $msg="";
  $msg.=(!$startDate)?i18n('selectStartDate'):'';
  $msg.=(!$endDate)?((!$startDate)?'<br/>':'').i18n('selectEndDate'):'';

  echo '<table style="width:95%;" align="center" '.excelName().'>';
  echo '<tr>';
  echo '<td colspan="5" class="reportTableData"  '.excelFormatCell('header').'><div style="background: #FFDDDD;font-size:150%;color:#808080;text-align:center;padding:20px">' . $msg . '</div></td>';
  echo '</tr>';
  echo '</table>';
  exit;
}else if($endDate < $startDate){
  echo '<table style="width:95%;" align="center" '.excelName().'>';
  echo '<tr>';
  echo '<td colspan="5" class="reportTableData"  '.excelFormatCell('header').'><div style="background: #FFDDDD;font-size:150%;color:#808080;text-align:center;padding:20px">' . i18n('EndDateMustBeSuperiorOrEqualToStartDate') . '</div></td>';
  echo '</tr>';
  echo '</table>';
  exit;
}

$r=new Resource(); 
$w=new Work();
$wTable=$w->getDatabaseTableName();
$wResourceColName=$w->getDatabaseColumnName('idResource');
$wWorkDateColName=$w->getDatabaseColumnName('workDate');
$rTable=$r->getDatabaseTableName();
$rCap=$r->getDatabaseColumnName('');
$where="rattachementDosi=1";
if($whereOrga!='')$where.=$whereOrga;
if($whereTeam!='')$where.=$whereTeam;
if($startDate)$where.=$whereStartDate;
if($endDate)$where.=$whereEndDate;
$where.=" and idle=0"; // To activate or not to always hide resources "idle"
$lstRes=$r->getSqlElementsFromCriteria(null,false,$where,'fullName asc, idOrganization');

$query="select $wResourceColName, work, $wWorkDateColName from $wTable where $wWorkDateColName>='$firstDay' and $wWorkDateColName<='$lastDay' and idResource in (select r.id from $rTable as r where $where) order by idResource asc ";
$result=Sql::query($query);
$tabVal=array();
$lastRes="";
while ($line=Sql::fetchLine($result)){
   $workDate=$line[$wWorkDateColName];
   $idRes=$line[$wResourceColName];
   if($lastRes!=$idRes){
     $lastRes=$idRes;
   }
  $work=$line['work'];
  if(isset($tabVal[$lastRes][$workDate])){
    $tabVal[$lastRes][$workDate]+=$work;
  }else{
    $tabVal[$lastRes][$workDate]=$work;
  }
}

echo '<table style="width:95%;" align="center" '.excelName().'>';
echo '<tr>';
echo '<td style="width:30%" class="reportTableHeader"  '.excelFormatCell('header').'>' . i18n('Resource') . '</td>';
echo '<td style="width:30%" class="reportTableHeader"  '.excelFormatCell('header').'>' . i18n('Organization') . '</td>';
echo '<td style="width:30%" class="reportTableHeader"  '.excelFormatCell('header').'>' . i18n('Team') . '</td>';
echo '<td style="width:20%" class="reportTableHeader"  '.excelFormatCell('header',10).'>' . i18n('week') . '</td>';
echo '<td style="width:20%" class="reportTableHeader"  '.excelFormatCell('header',12).'>' . ucfirst(i18n('entrie')) .(($outMode=='excel')?'(%)':'') .'</td>';
echo '</tr>';


$color='white';
$display=false;
$formatEndDate=weekFormat($endDate);
if(!empty($lstRes)){
  foreach ($lstRes as $res ){
    $idCalendar=$res->idCalendarDefinition;
    $date="";
    $year=$yearStart;
    $week=numericFormatter($startWeek);
    
    
    while($date!=$formatEndDate){
      if($yearEnd!=$yearStart and $week>52){
    	   $week=1;
           $year++;
      }
      $date=$year.'-'.(($week<10)?'0'.$week:$week);
      $dateFilter=$year.(($week<10)?'0'.$week:$week);
      $expected = 0;
      $real=0;
      $yearEnd=date('Y', strtotime($endDate));
      $first = date('Y-m-d', firstDayofWeek($week,$year));
      $last = lastDayofWeek($week,$year);
      $weekDayArray =array();
      $cp=0;
      while ($first<=$last){
        if(isOpenDay($first, $idCalendar)){
          $cp++;
          $expected += round($res->getCapacityPeriod($first),2);
        }
        if(isset($tabVal[$res->id][$first]))$real+=$tabVal[$res->id][$first];
        $first=addDaysToDate($first, 1);
      }
      if($expected!=0){
        $display=(!$display)?true:$display;
        $attempt=($real==0)?0:round($real/$expected,2)*100;
        if($attempt<100){
          $background=($color!='white')?'background-color:#ADCAFE;':'background-color:#E8EAF0;';
          $color=($color=='white')?'grey':'white';
          echo '<tr>';
          echo '<td style="width:30%;'.$background.'" class="reportTableData"  '.excelFormatCell('data',30,null,null,null,'left').'>' .(($outMode=='excel')?$res->name: htmlEncode($res->name) ). '</td>';
          echo '<td style="width:30%;'.$background.'" class="reportTableData"  '.excelFormatCell('data',30,null,null,null,'left').'>' . (($outMode=='excel')?SqlList::getNameFromId('Organization', $res->idOrganization):htmlEncode(SqlList::getNameFromId('Organization', $res->idOrganization))) . '</td>';
          echo '<td style="width:30%;'.$background.'" class="reportTableData"  '.excelFormatCell('data',30,null,null,null,'left').'>' . (($outMode=='excel')?SqlList::getNameFromId('Team', $res->idTeam):htmlEncode(SqlList::getNameFromId('Team', $res->idTeam))) . '</td>';
          echo '<td style="width:20%;'.$background.'" class="reportTableData"  '.excelFormatCell('data',30,null,null,null,'center').'>' .(($outMode=='excel')?$dateFilter: htmlFormatDate($dateFilter)). '</td>';
          echo '<td style="width:20%;'.$background.'" class="reportTableData"  '.excelFormatCell('data',30).'>' . (($outMode=='excel')?$attempt:$attempt.'&nbsp;%').' </td>';
          echo '</tr>';
        }
      }
      $week++;
    }
  }
  
  if(!$display){
    echo '<tr>';
    echo '<td colspan="5" class="reportTableData"  '.excelFormatCell('header').'><div style="background: #FFDDDD;font-size:150%;color:#808080;text-align:center;padding:20px">' . i18n('noDataToDisplay') . '</div></td>';
    echo '</tr>';
  }
}else{
  if($outMode!='excel'){
    echo '<tr>';
    echo '<td colspan="5" class="reportTableData"  '.excelFormatCell('header').'><div style="background: #FFDDDD;font-size:150%;color:#808080;text-align:center;padding:20px">' . i18n('noDataToDisplay') . '</div></td>';
    echo '</tr>';
  }

}
echo "</table>";

end:
