<?php 
/*** COPYRIGHT NOTICE *********************************************************
 *
 * Copyright 2009-2017 ProjeQtOr - Pascal BERNARD - support@projeqtor.org
 * Contributors : -
 * 
 * Most of properties are extracted from Dojo Framework.
 *
 * 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';

$paramTeam = trim(RequestHandler::getId('idTeam'));
$idOrganization = trim(RequestHandler::getId('idOrganization'));
$idDepartement = trim(RequestHandler::getId('idDepartementPlg'));
$idDirection = trim(RequestHandler::getId('idDirectionPlg'));
$paramYear = trim(RequestHandler::getYear('yearSpinner'));
$paramMonth = 1;

$user=getSessionUser();

$periodType=RequestHandler::getValue('periodType'); // not filtering as data as data is only compared against fixed strings
$periodValue='';
if (array_key_exists('periodValue',$_REQUEST))
{
	$periodValue=$_REQUEST['periodValue'];
	$periodValue=Security::checkValidPeriod($periodValue);
}

// Header
$headerParameters="";
if ($idOrganization!="") {
  $headerParameters.= i18n("colIdOrganization") . ' : ' . htmlEncode(SqlList::getNameFromId('Organization',$idOrganization)) . '<br/>';
}
if ($paramTeam!="") {
  $headerParameters.= i18n("colIdTeam") . ' : ' . htmlEncode(SqlList::getNameFromId('Team', $paramTeam)) . '<br/>';
}
if ($idDepartement!="") {
	$headerParameters.= i18n("colIdDepartementPlg") . ' : ' . htmlEncode(SqlList::getNameFromId('DepartementPlg',$idDepartement)) . '<br/>';
}
if ($idDirection!="") {
	$headerParameters.= i18n("colIdDirectionPlg") . ' : ' . htmlEncode(SqlList::getNameFromId('DirectionPlg', $idDirection)) . '<br/>';
}
if ($periodType=='year') {
  $headerParameters.= i18n("year") . ' : ' . $paramYear . '<br/>';
}
if (isset($outMode) and $outMode=='excel') {
  $headerParameters.=str_replace('- ','<br/>',Work::displayWorkUnit()).'<br/>';
}

include "header.php";
$where="1=1";
$whereForecast="refType = 'Activity'";
if ($periodType=='year') {
  if (!$periodValue ) {
    echo '<div style="background: #FFDDDD;font-size:150%;color:#808080;text-align:center;padding:20px">';
    echo i18n('messageNoData',array(i18n('year'))); // TODO i18n message
    echo '</div>';
    if (!empty($cronnedScript)) goto end; else exit;
  }
  if ($paramMonth<10) $paramMonth='0'.intval($paramMonth);
  $where.=" and ((year='" . $periodValue . "' and month>='" . $periodValue.$paramMonth . "')".
          " or (year='" . ($periodValue + 1) . "' and month<'" . ($periodValue + 1) . $paramMonth . "'))";
  
  $whereForecast .=" and ((year='" . $periodValue . "' and month>='" . $paramMonth . "')".
          " or (year='" . ($periodValue + 1) . "' and month<'" . $paramMonth . "'))";
}
$order="idProject asc";
$work=new Work();
$lstWork=$work->getSqlElementsFromCriteria(null,false, $where, $order);

$realWork = array();
$activity = array();
$date = array();
$listResOrg = array();
$resourceList = array();

if($idOrganization){
  $orga = new Organization($idOrganization);
  $listResOrg=$orga->getResourcesOfAllSubOrganizationsListAsArray();
}

foreach ($lstWork as $wrk){
	$res = new Resource($wrk->idResource, true);
	if($paramTeam and $res->idTeam!=$paramTeam)continue;
	if(property_exists('Resource', 'idDepartementPlg') and $idDepartement and $res->idDepartementPlg!=$idDepartement)continue;
	if(property_exists('Resource', 'idDirectionPlg') and $idDirection and $res->idDirectionPlg!=$idDirection)continue;
	if(count($listResOrg)>0 and !in_array($wrk->idResource, $listResOrg))continue;
	
  	if(isset($realWork[$wrk->month][$wrk->idResource])){
  		$realWork[$wrk->month][$wrk->idResource] += round($wrk->work, 2);
  	}else{
  		$realWork[$wrk->month][$wrk->idResource] = round($wrk->work, 2);
  	}
  	//$resourceList[$wrk->idResource]=$wrk->idResource;
}

$order="idResource asc";
$forecast=new PlgForecast();
$lstForecast = $forecast->getSqlElementsFromCriteria(null,false, $whereForecast, $order);

$plannedWork = array();

foreach ($lstForecast as $wrkF){
	$res = new Resource($wrkF->idResource, true);
	if($paramTeam and $res->idTeam!=$paramTeam)continue;
	if(property_exists('Resource', 'idDepartementPlg') and $idDepartement and $res->idDepartementPlg!=$idDepartement)continue;
	if(property_exists('Resource', 'idDirectionPlg') and $idDirection and $res->idDirectionPlg!=$idDirection)continue;
	if(count($listResOrg)>0 and !in_array($wrkF->idResource, $listResOrg))continue;

	if(isset($plannedWork[$wrkF->year.$wrkF->month][$wrkF->idResource])){
		$plannedWork[$wrkF->year.$wrkF->month][$wrkF->idResource] += round($wrkF->work, 2);
	}else{
		$plannedWork[$wrkF->year.$wrkF->month][$wrkF->idResource] = round($wrkF->work, 2);
	}
	//$resourceList[$wrk->idResource]=$wrk->idResource;
}

// if(count($resourceList)<=0){
  $res = new Resource();
  $whereRes = "idDirectionPlg IS NOT NULL or idDepartementPlg IS NOT NULL";
  $resList = $res->getSqlElementsFromCriteria(null,null,$whereRes);
  foreach ($resList as $resource){
    $resourceList[$resource->id]=$resource->id;
  }
// }

$dirResList = array();
$depResList = array();
foreach ($resourceList as $idResource){
  $res = new Resource($idResource);
  if(isset($res->idDirectionPlg))$dirResList[$res->idDirectionPlg][$idResource] = $idResource;
  if(isset($res->idDepartementPlg))$depResList[$res->idDepartementPlg][$idResource] = $idResource;
}

$dirPlg = new DirectionPlg();
$dirPlgList = $dirPlg->getSqlElementsFromCriteria(null,null, "1=1 and idle = 0");
$depPlg = new DepartementPlg();
$depPlgList = $depPlg->getSqlElementsFromCriteria(null,null, "1=1 and idle = 0");

$dirWorkList = array();
$depWorkList = array();
$dirPlannedList = array();
$depPlannedList = array();

$monthRealWorkSum = array();

//Tableau 1
echo '<table style="width:95%;" align="center" '.excelName().'>';

if(isset($outMode) and $outMode == 'excel'){
  echo '<tr><td></td>';
  for ($i=$paramMonth; $i<= 12; $i++){
  	echo '<td></td>';
  }
  echo '<td></td>';
  
  echo '<td '.excelFormatCell('data',10).'></td>';
  
  echo '<td class="reportTableHeader" style="min-width:100px;" '.excelFormatCell('data',20).'></td>';
  for ($i=$paramMonth; $i<= 12; $i++){
  	$mnth = i18n(date('F', mktime(0,0,0,$i)));
  	echo '<td colspan="2" class="reportTableHeader"  '.excelFormatCell('data',20).'>'.$mnth.'</td>';
  }
  echo '<td colspan="2" class="reportTableHeader"  '.excelFormatCell('data',20).'>' . i18n('sum') . '</td>';
  echo '</tr>';
}

echo '<tr><td class="reportTableHeader" style="min-width:100px;" '.excelFormatCell('data',20).'></td>';
for ($i=$paramMonth; $i<= 12; $i++){
  $mnth = i18n(date('F', mktime(0,0,0,$i)));
  echo '<td class="reportTableHeader"  '.excelFormatCell('data',15,null,null,null,'center').'>'.$mnth.'</td>';
}
echo '<td class="reportTableHeader"  '.excelFormatCell('data',15,null,null,null,'center',null,null,null,null,false).'>' . i18n('sum') . '</td>';

if(isset($outMode) and $outMode == 'excel'){
  echo '<td '.excelFormatCell('data',10).'></td>';
  echo '<td class="reportTableHeader" '.excelFormatCell('data',20).'></td>';
  for ($i=$paramMonth; $i<= 12; $i++){
  	echo '<td class="reportTableHeader"  '.excelFormatCell('data',10).'>'.i18n('colPlanned').'</td>';
  	echo '<td class="reportTableHeader"  '.excelFormatCell('data',10).'>'.i18n('colRealised').'</td>';
  }
  echo '<td class="reportTableHeader"  '.excelFormatCell('data',10).'>'.i18n('colPlanned').'</td>';
  echo '<td class="reportTableHeader"  '.excelFormatCell('data',10).'>'.i18n('colRealised').'</td>';
}
echo '</tr>';

if(isset($outMode) and $outMode == 'html'){
  foreach ($dirPlgList as $direction){
    if(($idDirection and $direction->id != $idDirection) or (isset($dirResList[$direction->id]) and count($dirResList[$direction->id])) == 0)continue;
    echo '<tr><td style="text-align:left;background-color:#909090 !important;font-weight:bold" class="reportTableLineHeader" '.excelFormatCell('header',null,'#000000','#909090',true,'left').'>'.$direction->name.'</td>';
    for ($i=$paramMonth; $i<= 12; $i++){
    	echo '<td style="text-align:right;background-color:#909090 !important;" class="reportTableLineHeader" '.excelFormatCell('header',null,null,null,null,'right').'>-</td>';
    }
    echo '<td style="text-align:right;background-color:#909090 !important;" class="reportTableLineHeader" '.excelFormatCell('header',null,null,null,null,'right').'></td>';
    echo '</tr>';
    foreach ($depPlgList as $department){
      if(($idDepartement and $department->id != $idDepartement) or (isset($depResList[$department->id]) and count($depResList[$department->id])) == 0)continue;
      echo '<tr><td style="text-align:left;background-color:#ffefcc !important;" class="reportTableLineHeader" '.excelFormatCell('rowheader',null,'#000000','#ffefcc',null,'left').'>'.$department->name.'</td>';
      for ($i=$paramMonth; $i<= 12; $i++){
      	echo '<td style="text-align:right;background-color:#ffefcc !important;" class="reportTableLineHeader" '.excelFormatCell('data',null,null,'#ffefcc',null,'right').'>-</td>';
      }
      echo '<td style="text-align:right;background-color:#ffefcc !important;" class="reportTableLineHeader" '.excelFormatCell('data',null,null,'#ffefcc',null,'right').'></td>';
      echo '</tr>';
      foreach ($resourceList as $idResource){
      	$res = new Resource($idResource, true);
      	if($res->idDirectionPlg == $direction->id and $res->idDepartementPlg == $department->id){
    			echo '<tr>';
    			echo '<td style="text-align:left;" class="reportTableData" '.excelFormatCell('data',null,null,null,null,'left',null,null,null).'>'.$res->name.'</td>';
    			for ($i=intval($paramMonth); $i<= 12; $i++){
  		          echo '<td style="text-align:right;" class="reportTableData" '.excelFormatCell('data',null,null,null,null,'right',null,null,null).'></td>';
    			}
    			echo '<td style="text-align:right;" class="reportTableData" '.excelFormatCell('data',null,null,null,null,'right',null,null,null).'></td>';
    			echo '</tr>';
  	    }
      }
    }
  }
  
  echo '<tr><td class="reportTableLineHeader" '.excelFormatCell('rowheader',null,null,null,null,null,'left').'>'. i18n('sum') .'</td>';
  for ($i=intval($paramMonth); $i<= 12; $i++){
    echo '<td style="text-align:right;" class="reportTableLineHeader"  '.excelFormatCell('rowheader',null,null,null,null,'right').'></td>';
  }
  echo '<td style="text-align:right;" class="reportTableLineHeader"  '.excelFormatCell('rowheader',null,null,null,null,'right').'></td>';
  echo '</tr>';
  
  echo '<tr><td '.excelFormatCell('data').'><br/></td>';
  for ($i=$paramMonth; $i<= 12; $i++){
  	echo '<td '.excelFormatCell('data').'><br/></td>';
  	echo '<td '.excelFormatCell('data').'><br/></td>';
  }
  echo '<td '.excelFormatCell('data').'><br/></td>';
  echo '<td '.excelFormatCell('data').'><br/></td>';
  echo '</tr>';
}

$monthPlannedWorkSum = array();

//Tableau 2
if(isset($outMode) and $outMode == 'html'){
  echo '<tr><td '.excelFormatCell('data',20).'></td>';
  for ($i=$paramMonth; $i<= 12; $i++){
    	$mnth = i18n(date('F', mktime(0,0,0,$i)));
  	echo '<td style="width:7%" colspan="2" class="reportTableHeader"  '.excelFormatCell('data',20).'>'.$mnth.'</td>';
  }
  echo '<td style="width:7%" colspan="2" class="reportTableHeader"  '.excelFormatCell('data',20).'>' . i18n('sum') . '</td>';
  echo '</tr>';
  
  echo '<tr><td class="reportTableHeader" style="min-width:100px;" '.excelFormatCell('data',20).'></td>';
  for ($i=$paramMonth; $i<= 12; $i++){
  	echo '<td class="reportTableHeader"  '.excelFormatCell('data',10).'>'.i18n('colPlanned').'</td>';
  	echo '<td class="reportTableHeader"  '.excelFormatCell('data',10).'>'.i18n('colRealised').'</td>';
  }
  echo '<td class="reportTableHeader"  '.excelFormatCell('data',10).'>'.i18n('colPlanned').'</td>';
  echo '<td class="reportTableHeader"  '.excelFormatCell('data',10).'>'.i18n('colRealised').'</td>';
  echo '</tr>';
}

foreach ($dirPlgList as $direction){
  if($idDirection and $direction->id != $idDirection or (isset($dirResList[$direction->id]) and count($dirResList[$direction->id])) == 0)continue;
    echo '<tr>';
    if(isset($outMode) and $outMode == 'excel'){
      echo '<td style="text-align:left;background-color:#909090 !important;font-weight:bold" class="reportTableLineHeader" '.excelFormatCell('header',null,'#000000','#909090',true,'left').'>'.$direction->name.'</td>';
      for ($i=$paramMonth; $i<= 12; $i++){
      	echo '<td style="text-align:right;background-color:#909090 !important;" class="reportTableLineHeader" '.excelFormatCell('header',null,null,null,null,'right').'>-</td>';
      }
      echo '<td style="text-align:right;background-color:#909090 !important;" class="reportTableLineHeader" '.excelFormatCell('header',null,null,null,null,'right').'></td>';
      echo '<td '.excelFormatCell('data',10).'></td>';
    }
    echo '<td style="text-align:left;background-color:#909090 !important;font-weight:bold" class="reportTableLineHeader" '.excelFormatCell('header',null,'#000000',null,true,'left').'>'.$direction->name.'</td>';
	$dirRealWorksum = 0;
	$dirPlannedWorksum = 0;
	for ($i=intval($paramMonth); $i<= 12; $i++){
	  $month = ($i < 10)?'0'.$i:$i;
	  $year = $periodValue;
	  $planned = 0;
	  $real = 0;
	  foreach ($dirResList[$direction->id] as $idResource){
        if(isset($realWork[$year.$month][$idResource])){
            $real += $realWork[$year.$month][$idResource];
        	$dirRealWorksum += $realWork[$year.$month][$idResource];
        }
        if(isset($plannedWork[$year.$i][$idResource])){
        	$planned += $plannedWork[$year.$i][$idResource];
        	$dirPlannedWorksum += $plannedWork[$year.$i][$idResource];
        }
	  }
	  if(isset($outMode) and $outMode == 'html'){
	  	$real = Work::displayWorkWithUnit($real);
	  	$planned = Work::displayWorkWithUnit($planned);
	  }
      echo '<td style="text-align:center;background-color:#909090 !important;" class="reportTableLineHeader"  '.excelFormatCell('header',null,null,null,null,'center').'>'.$planned.'</td>';
      echo '<td style="text-align:center;background-color:#909090 !important;" class="reportTableLineHeader"  '.excelFormatCell('header',null,null,null,null,'center').'>'.$real.'</td>';
	}
	if(isset($outMode) and $outMode == 'html'){
		$dirPlannedWorksum = Work::displayWorkWithUnit($dirPlannedWorksum);
		$dirRealWorksum = Work::displayWorkWithUnit($dirRealWorksum);
	}
	echo '<td style="text-align:center;background-color:#909090 !important;" class="reportTableLineHeader"  '.excelFormatCell('header',null,null,null,null,'center').'>'.$dirPlannedWorksum.'</td>';
	echo '<td style="text-align:center;background-color:#909090 !important;" class="reportTableLineHeader"  '.excelFormatCell('header',null,null,null,null,'center').'>'.$dirRealWorksum.'</td>';
	echo '</tr>';
	foreach ($depPlgList as $department){
	  if($idDepartement and $department->id != $idDepartement or (isset($depResList[$department->id]) and count($depResList[$department->id])) == 0)continue;
        echo '<tr>';
	    if(isset($outMode) and $outMode == 'excel'){
	      echo '<td style="text-align:left;background-color:#ffefcc !important;" class="reportTableLineHeader" '.excelFormatCell('rowheader',null,'#000000','#ffefcc',null,'left').'>'.$department->name.'</td>';
	      for ($i=$paramMonth; $i<= 12; $i++){
	      	echo '<td style="text-align:right;background-color:#ffefcc !important;" class="reportTableLineHeader" '.excelFormatCell('data',null,null,'#ffefcc',null,'right').'>-</td>';
	      }
	      echo '<td style="text-align:right;background-color:#ffefcc !important;" class="reportTableLineHeader" '.excelFormatCell('data',null,null,'#ffefcc',null,'right').'></td>';
	      echo '<td style="width:40px" '.excelFormatCell('data',20).'></td>';
	    }
		echo '<td style="text-align:left;background-color:#ffefcc !important;" class="reportTableLineHeader" '.excelFormatCell('data',null,null,'#ffefcc',null,'left').'>'.$department->name.'</td>';
		$depRealWorksum = 0;
		$depPlannedWorksum = 0;
		for ($i=intval($paramMonth); $i<= 12; $i++){
		  $month = ($i < 10)?'0'.$i:$i;
		  $year = $periodValue;
		  $planned = 0;
		  $real = 0;
		  foreach ($depResList[$department->id] as $idResource){
		    $res = new Resource($idResource, true);
		    if($res->idDirectionPlg == $direction->id and $res->idDepartementPlg == $department->id){
		      if(isset($realWork[$year.$month][$idResource])){
                $real += $realWork[$year.$month][$idResource];
		      	$depRealWorksum += $realWork[$year.$month][$idResource];
		      }
		      if(isset($plannedWork[$year.$i][$idResource])){
                $planned += $plannedWork[$year.$i][$idResource];
		      	$depPlannedWorksum += $plannedWork[$year.$i][$idResource];
		      }
		    }
		  }
		  if(isset($outMode) and $outMode == 'html'){
		  	$real = Work::displayWorkWithUnit($real);
		  	$planned = Work::displayWorkWithUnit($planned);
		  }
			echo '<td style="text-align:center;background-color:#ffefcc !important;" class="reportTableLineHeader"  '.excelFormatCell('data',null,null,'#ffefcc',null,'center').'>'.$planned.'</td>';
			echo '<td style="text-align:center;background-color:#ffefcc !important;" class="reportTableLineHeader"  '.excelFormatCell('data',null,null,'#ffefcc',null,'center').'>'.$real.'</td>';
		}
		if(isset($outMode) and $outMode == 'html'){
			$depPlannedWorksum = Work::displayWorkWithUnit($depPlannedWorksum);
			$depRealWorksum = Work::displayWorkWithUnit($depRealWorksum);
		}
		echo '<td style="text-align:center;background-color:#ffefcc !important;" class="reportTableLineHeader" '.excelFormatCell('data',null,null,'#ffefcc',null,'center').'>'.$depPlannedWorksum.'</td>';
		echo '<td style="text-align:center;background-color:#ffefcc !important;" class="reportTableLineHeader" '.excelFormatCell('data',null,null,'#ffefcc',null,'center').'>'.$depRealWorksum.'</td>';
		echo '</tr>';
		foreach ($resourceList as $idResource){
		  $res = new Resource($idResource, true);
		  if($res->idDirectionPlg == $direction->id and $res->idDepartementPlg == $department->id){
  			echo '<tr>';
  			if(isset($outMode) and $outMode == 'excel'){
  			  echo '<td style="text-align:left;" class="reportTableData" '.excelFormatCell('data',null,null,null,null,'left',null,null,null).'>'.$res->name.'</td>';
  			  for ($i=intval($paramMonth); $i<= 12; $i++){
  			  	echo '<td style="text-align:right;" class="reportTableData" '.excelFormatCell('data',null,null,null,null,'right',null,null,null).'></td>';
  			  }
  			  echo '<td style="text-align:right;" class="reportTableData" '.excelFormatCell('data',null,null,null,null,'right',null,null,null).'></td>';
  			  echo '<td style="width:40px" '.excelFormatCell('data',20).'></td>';
  			}
		    $resRealWorksum = 0;
		    $resPlannedWorksum = 0;
		    echo '<td  style="text-align:left;" class="reportTableData"  '.excelFormatCell('data',null,null,null,null,'left',null,null,null).'>'.$res->name.'</td>';
		    for ($i=intval($paramMonth); $i<= 12; $i++){
		        $pWork = 0;
		        $rWork = 0;
		    	$month = ($i < 10)?'0'.$i:$i;
		    	$year = $periodValue;
		    	if(isset($realWork[$year.$month][$idResource])){
		    		$rWork = $realWork[$year.$month][$idResource];
		    		$resRealWorksum += $rWork;
		    		if(isset($monthRealWorkSum[$year.$month])){
		    			$monthRealWorkSum[$year.$month] += $rWork;
		    		}else{
		    			$monthRealWorkSum[$year.$month] = $rWork;
		    		}
		    	}
		    	if(isset($plannedWork[$year.$i][$idResource])){
		    		$pWork = $plannedWork[$year.$i][$idResource];
		    		$resPlannedWorksum += $pWork;
		    		if(isset($monthPlannedWorkSum[$year.$month])){
		    			$monthPlannedWorkSum[$year.$month] += $pWork;
		    		}else{
		    			$monthPlannedWorkSum[$year.$month] = $pWork;
		    		}
		    	}
		    	if(isset($outMode) and $outMode == 'html'){
		    		$rWork = Work::displayWorkWithUnit($rWork);
		    		$pWork = Work::displayWorkWithUnit($pWork);
		    	}
		    	echo '<td style="text-align:center;" class="reportTableData"  '.excelFormatCell('data',null,null,null,null,'center',null,null,null).'>'.$pWork.'</td>';
		    	echo '<td style="text-align:center;" class="reportTableData"  '.excelFormatCell('data',null,null,null,null,'center',null,null,null).'>'.$rWork.'</td>';
		    }
		    if(isset($outMode) and $outMode == 'html'){
		    	$resPlannedWorksum = Work::displayWorkWithUnit($resPlannedWorksum);
		    	$resRealWorksum = Work::displayWorkWithUnit($resRealWorksum);
		    }
		    echo '<td style="text-align:center;" class="reportTableData"  '.excelFormatCell('data',null,null,null,null,'center',null,null,null).'>'.$resPlannedWorksum.'</td>';
	        echo '<td style="text-align:center;" class="reportTableData"  '.excelFormatCell('data',null,null,null,null,'center',null,null,null).'>'.$resRealWorksum.'</td>';
		    echo '</tr>';
		  }
		}
	}
}
echo '<tr>';
if(isset($outMode) and $outMode == 'excel'){
  echo '<td class="reportTableLineHeader" '.excelFormatCell('rowheader',null,null,null,null,null,'left').'>'. i18n('sum') .'</td>';
  for ($i=intval($paramMonth); $i<= 12; $i++){
  	echo '<td style="text-align:right;" class="reportTableLineHeader"  '.excelFormatCell('rowheader',null,null,null,null,'right').'></td>';
  }
  echo '<td style="text-align:right;" class="reportTableLineHeader"  '.excelFormatCell('rowheader',null,null,null,null,'right').'></td>';
  
  echo '<td style="width:40px" '.excelFormatCell('data',20).'></td>';
}

echo '<td class="reportTableLineHeader" '.excelFormatCell('rowheader',null,null,null,null,'left').'>'. i18n('sum') .'</td>';
$totalRealSum = 0;
$totalPlannedSum = 0;
for ($i=intval($paramMonth); $i<= 12; $i++){
  $month = ($i < 10)?'0'.$i:$i;
  $year = $periodValue;
  $sumRealWork = (isset($monthRealWorkSum[$year.$month]))?$monthRealWorkSum[$year.$month]:0;
  $totalRealSum += $sumRealWork;
  $sumPlannedWork = (isset($monthPlannedWorkSum[$year.$month]))?$monthPlannedWorkSum[$year.$month]:0;
  $totalPlannedSum += $sumPlannedWork;
  if(isset($outMode) and $outMode == 'html'){
  	$sumRealWork = Work::displayWorkWithUnit($sumRealWork);
  	$sumPlannedWork = Work::displayWorkWithUnit($sumPlannedWork);
  }
  echo '<td style="text-align:center;" class="reportTableLineHeader"  '.excelFormatCell('rowheader',null,null,null,null,'center').'>'.$sumPlannedWork.'</td>';
  echo '<td style="text-align:center;" class="reportTableLineHeader"  '.excelFormatCell('rowheader',null,null,null,null,'center').'>'.$sumRealWork.'</td>';
}
if(isset($outMode) and $outMode == 'html'){
	$totalPlannedSum = Work::displayWorkWithUnit($totalPlannedSum);
	$totalRealSum = Work::displayWorkWithUnit($totalRealSum);
}
echo '<td style="text-align:center;" class="reportTableLineHeader"  '.excelFormatCell('rowheader',null,null,null,null,'center').'>'.$totalPlannedSum.'</td>';
echo '<td style="text-align:center;" class="reportTableLineHeader"  '.excelFormatCell('rowheader',null,null,null,null,'center').'>'.$totalRealSum.'</td>';
echo '</tr>';
echo '</table>';
end: