<?php

session_start();

    require_once('../libs/Ultraviolet.php');
	
	require_once('../libs/Database.php');
	
	require_once('../libs/AdminFunctions.php');
		
	$database =& DatabaseConnect::getInstance();
	
	$database->query("SET CHARACTER SET utf8");
	
	$functions =& AdminFunctions::getInstance();


for($i = 0; $i < count($_POST); $i++):

		${key($_POST)} = $_POST[key($_POST)];

		next($_POST);	

	endfor;
	
	$database->setQuery("SELECT name FROM client WHERE id = '$client'");
	
	$clientname = $database->loadResult();
	
	$convertm = explode('-', $month);
	
	$monthheader = $convertm[1].'.'.$convertm[0];
	

	
	
	
	$fd =  $convertm[0].'-'.$convertm[1];
	
	$daynum = cal_days_in_month(CAL_GREGORIAN, $convertm[1], $convertm[0]);
	

	
	

	
?>

<table style="font-family:Arial, Helvetica, sans-serif" cellspacing="1" cellpadding="1">


<th style="background:#333333;color:#fff;padding:5px;font-family:Arial, Helvetica, sans-serif; font-weight:normal" width="250"><?=$clientname.' - '.$monthheader?></th>
<?php
for($i = 0; $i < $daynum; $i++): $currday = $i + 1;
?><th<?php
if(date("D", strtotime($fd.'-'.$currday)) == 'Sat' || date("D", strtotime($fd.'-'.$currday)) == 'Sun'){?> class="weekend" style="background:#518d37; color:#fff; padding:5px;font-family:Arial, Helvetica, sans-serif;"<?php }else{ ?> style="background:#333333; color:#fff; padding:5px;font-family:Arial, Helvetica, sans-serif;"<?php } ?>
><?=$currday?></th><?php 
endfor; $colspan = $i + 1;
?><th>Suma</th></tr>
<?php $database->setBareQuery("SELECT DISTINCT(t1.name), t1.id, t1.project_num, t1.project_year FROM project t1, task t2 WHERE SUBSTRING_INDEX(t2.date, '-', 2) = '".$fd."' AND t1.parent = '".$client."' AND t1.id = t2.project ORDER BY t1.project_year, t1.project_num"); 
$projects = $database->loadResultArray(0);
$pid = $database->loadResultArray(1);
$projectnum = $database->loadResultArray(2);
$pryear = $database->loadResultArray(3);


for($i = 0; $i < count($projects); $i++):

if($projectnum[$i] < 100){
			
			if($projectnum[$i] < 10){ $visiblenum = '00'.$projectnum[$i];}
			
			else { $visiblenum = '0'.$projectnum[$i];}
			
			}
			
			else { $visiblenum = $projectnum[$i];}


?>

<tr>
<td colspan="<?=$colspan?>'" class="clients" style="text-align:left; color:#fff; background:#333333; padding:5px;font-family:Arial, Helvetica, sans-serif;"><?=$pryear[$i].$visiblenum.' '.$projects[$i]?></td><td class="clientssum" style="color:#fff; background:#333333; text-align:center; background:#333333; padding:5px;font-family:Arial, Helvetica, sans-serif;">

<?php

$database->setQuery("SELECT SUM(duration) FROM task WHERE  project = '$pid[$i]' AND SUBSTRING_INDEX(date, '-', 2) = '".$fd."'");
$clientssum = $database->loadResult();?>

<?=$clientssum?></td></tr>

<?php 

$database->setBareQuery("SELECT DISTINCT(t1.user) FROM task t1, project t2 WHERE SUBSTRING_INDEX(t1.date, '-', 2) = '".$fd."' AND t1.project = '".$pid[$i]."' ORDER BY t1.user");
$users = $database->loadResultArray(0);
for($z = 0; $z < count($users); $z++):
if($z%2 == 0){ $rowcol = 'even';}else{ $rowcol = 'odd';} ?>

<tr><td  class="<?=$rowcol?>" style="text-align:left"><?=$users[$z]?></td>

<?php 
for($w = 0; $w < $daynum; $w++): 
 $currday = $w + 1; if(strlen($currday) == 1){ $currday = '0'.$currday;};

?>

<td<?php if(date("D", strtotime($fd.'-'.$currday)) == 'Sat' || date("D", strtotime($fd.'-'.$currday)) == 'Sun'){if($rowcol == 'even'){?> class="weekendday" style="background:#ced9bb; color:#000; padding:5px;font-family:Arial, Helvetica, sans-serif;"<?php }else{?> class="weekendday" style="background:#eff3e5; color:#000; padding:5px;font-family:Arial, Helvetica, sans-serif;"<?php }}else{
if($rowcol == 'even'){?> class="even" style="background:#cacaca; color:#000; padding:5px;font-family:Arial, Helvetica, sans-serif;"<?php }else{?> class="odd" style="background:#ededed; color:#000; padding:5px;font-family:Arial, Helvetica, sans-serif;"<?php }}
?>><?php 
$database->setBareQuery("SELECT duration FROM task WHERE date = '".$fd."-".$currday."' AND user = '".$users[$z]."' AND project = '".$pid[$i]."'");
$dur = $database->loadResultArray(0);?>
<?=$dur[0]?></td>

<?php

endfor;

$database->setBareQuery("SELECT SUM(duration) FROM task WHERE user = '".$users[$z]."' AND project = '".$pid[$i]."' AND SUBSTRING_INDEX(date, '-', 2) = '".$fd."'");
$usersum = $database->loadResultArray(0);

?><td style="text-align:center;background:#cacaca; color:#000; padding:5px;font-family:Arial, Helvetica, sans-serif;" class="even"><?=$usersum[0]?></td>

<?php 

endfor; ?>

</tr>

<?php

endfor;

?>

<tr><td class="clientssum" style="color:#fff; background:#333333; text-align:center; background:#333333; padding:5px;font-family:Arial, Helvetica, sans-serif;">Sum</td><?php 
 for($w = 0; $w < $daynum; $w++): $currday = $w + 1; if(strlen($currday) == 1){ $currday = '0'.$currday;};
?><th<?php 
if(date("D", strtotime($fd.'-'.$currday)) == 'Sat' || date("D", strtotime($fd.'-'.$currday)) == 'Sun'){?> class="weekend" style="background:#518d37; color:#fff; padding:5px;font-family:Arial, Helvetica, sans-serif;"<?php }else{$message.=' style="background:#333333; color:#fff; padding:5px;font-family:Arial, Helvetica, sans-serif;"';}
?>><?php 
$database->setBareQuery("SELECT SUM(duration) FROM task WHERE date = '".$fd."-".$currday."' AND project IN (SELECT id FROM project WHERE parent = '".$client."')");
$daysum = $database->loadResultArray(0);

?><?=$daysum[0]?></th><?php 
endfor;
?><th class="clientssum" style="color:#fff; background:#333333; text-align:center; background:#333333; padding:5px;font-family:Arial, Helvetica, sans-serif;"><?php 
$database->setBareQuery("SELECT SUM(duration) FROM task WHERE SUBSTRING_INDEX(date, '-', 2) = '".$fd."' AND project IN (SELECT id FROM project WHERE parent = '".$client."')");
$totalsum = $database->loadResultArray(0);

?><?=$totalsum[0]?></th></tr></table>

