<?php
/*** COPYRIGHT NOTICE *********************************************************
 *
 * Copyright 2009-2017 ProjeQtOr - Pascal BERNARD - support@projeqtor.org
 * Contributors : -
 * 
 * This file is part of ProjeQtOr.
 * 
 * ProjeQtOr is free software: you can redistribute it and/or modify it under 
 * the terms of the GNU Affero General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * ProjeQtOr is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for 
 * more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with
 * ProjeQtOr. If not, see <http://www.gnu.org/licenses/>.
 *
 * You can get complete code of ProjeQtOr, other resource, help and information
 * about contributors at http://www.projeqtor.org 
 *     
 *** DO NOT REMOVE THIS NOTICE ************************************************/

include_once '../tool/projeqtor.php';
include_once '../tool/formatter.php';
//echo 'versionReport.php';

$user=getSessionUser();
$headerParameters="";
include "header.php";

$lstVersion=SqlList::getList('ProductVersion',null,null,true);
asort($lstVersion);

$version=array();
foreach($lstVersion as $id=>$name) {
  $code=$name;
  $code=str_replace('ProjeQtOr','',$name);
  if (strlen($code)==4) $code=$code.'.0';
  $lastPoint=strrpos($code,'.');
  $ext=($lastPoint)?substr($code,$lastPoint+1):'';
  if ($ext and intval($ext)==0) continue;
  if (trim($code)=='Version not planned') continue;
  if ($lastPoint and strlen($ext)<4) $code=substr($code,0,$lastPoint);
  if (! isset($version[$code])) $version[$code]=array('id'=>$id,'count'=>0,'work'=>0,'list'=>array());
  $version[$code]['list'][$id]=$id;
}
ksort($version,SORT_NATURAL);
if (checkNoData($lstVersion)) if (!empty($cronnedScript)) goto end; else exit;
  
$w=new Work();
$t=new Ticket();
foreach ($version as $code=>$vers) {
  $critVers="idTargetProductVersion in ".transformListIntoInClause($version[$code]['list']);
  $crit="refType='Ticket' and refId in (select id from ticket where $critVers)";
  $sum=$w->sumSqlElementsFromCriteria('work', null,$crit);
  $cpt=$t->countSqlElementsFromCriteria(null,$critVers);
  $version[$code]['work']+=$sum;
  $version[$code]['count']+=$cpt;
  $crit="refType='Activity' and refId in (select id from ticket where $critVers)";
}

// title
echo '<table width="500px" align="center">';
echo '<tr>';
echo ' <td class="reportTableHeader">' . i18n('Version') . '</td>';
echo ' <td class="reportTableHeader">' .i18n('colWork'). '</td>';
echo ' <td class="reportTableHeader">' .i18n('count'). '</td>';
echo '</tr>';

$sumTot=0;
$cptTot=0;
foreach ($version as $code=>$vers) {
  echo '<tr>';
  echo ' <td class="reportTable" style="width:200px;" >' . $code . '</td>';
  echo ' <td class="reportTable" style="width:100px;" >' . workFormatter($vers['work']). '</td>';
  echo ' <td class="reportTable" style="width:100px;" >' . $vers['count']. '</td>';
  $sumTot+=$vers['work'];
  $cptTot+=$vers['count'];
  // count
//   $arrLines=array('count','estimated','real','left');
//   foreach ($arrLines as $val) {
//   	$sum=0;
//   	if ($val!='count') {echo '<tr>';}
//     echo '<td class="reportTableLineHeader" style="width:10%;" >' . i18n($val) . '</td>';
//     foreach ($arrayVers as $idType=>$arrayType) {
//   	  echo '<td class="reportTableData" style="width:' . $medWidth . '%;">' . $arrayType[$val] . '</td>';
//   	  $sum+=$arrayType[$val];
//     }
//     echo '<td class="reportTableData" style="width:' . $medWidth . '%;">' . $sum . '</td></tr>';
//  }  
}  
echo '<tr>';
echo ' <td class="reportTableLineHeader" style="width:200px;" >' . i18n('sum') . '</td>';
echo ' <td class="reportTableLineHeader" style="width:100px;" >' . workFormatter($sumTot). '</td>';
echo ' <td class="reportTableLineHeader" style="width:100px;" >' . $vers[$cptTot]. '</td>';
echo '</table>';
echo '<br/>';

end:
