<?php

	require_once('../../libs/Ultraviolet.php');

	require_once('../../libs/Database.php');

	require_once('../../libs/AdminFunctions.php');
	
	require_once('../../libs/SimpleImage.php');
	
	require_once('../../libs/FileUpload.php');

	

	$database =& DatabaseConnect::getInstance();

	$functions =& AdminFunctions::getInstance();

	$database->query("SET CHARACTER SET utf8");
	
	$database->setBareQuery("SELECT user_name, user_email, user_fname, user_lname FROM admin_users");
	$usern = $database->loadResultArray(0);
	$email = $database->loadResultArray(1);
	$fname = $database->loadResultArray(2);
	$lname = $database->loadResultArray(3);
	
	for($j = 0; $j < count($usern); $j++):
	
	$user = $usern[$j]; $fullname = $fname[$j].' '.$lname[$j]; 
	
	$d2 = date("m",strtotime("-1 month"));
	
	$d1 = date("Y",strtotime("-1 month"));
	
	switch($d2){
		
		case('12'):$mesec = 'Decembar ';break;
		case('1'):$mesec = 'Januar ';break;
		case('2'):$mesec = 'Februar ';break;
		case('3'):$mesec = 'Mart ';break;
		case('4'):$mesec = 'April ';break;
		case('5'):$mesec = 'Maj ';break;
		case('6'):$mesec = 'Jun ';break;
		case('7'):$mesec = 'Jul ';break;
		case('8'):$mesec = 'Avgust ';break;
		case('9'):$mesec = 'Septembar ';break;
		case('10'):$mesec = 'Oktobar ';break;
		case('11'):$mesec = 'Novembar ';break;
		
		}
	
	$fd =  $d1.'-'.$d2;
	
	$daynum = cal_days_in_month(CAL_GREGORIAN, $d2, $d1);
	
	$fullnamecol = $daynum + 1;
	
	
// multiple recipients
$to  =  $fname[$j].' '.$lname[$j].' <'.$email[$j].'>'; // note the comma


// subject
$subject = 'Znamo šta ste radili prošlog meseca';

// message
$message = '
<title>Znamo šta ste radili prošlog meseca</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">

A:LINK      { 
	text-decoration : none; 
	font-weight: normal;
	color: #2b4770;
	font-family:Arial, Helvetica, sans-serif;
	font-size:10px}

A:VISITED {
	text-decoration: none; 
	font-weight: normal; 
	color: #2b4770;
	font-family:Arial, Helvetica, sans-serif;
	font-size:10px}

A:HOVER  { 

	text-decoration : none; 
	font-weight: normal; 
	color: #2b4770;
	font-family:Arial, Helvetica, sans-serif;
	font-size:10px }

A:ACTIVE  {
	text-decoration: none; 
	font-weight: normal; 
	color: #2b4770;
	font-family:Arial, Helvetica, sans-serif;
	font-size:10px }
	
th {
	background:#333333;
	color:#fff;
	padding:5px;font-family:Arial, Helvetica, sans-serif;}
	
td {padding:5px;font-family:Arial, Helvetica, sans-serif; }

.title { background:##333333; color:#fff;}	

.even { background:#cacaca; padding:5px;font-family:Arial, Helvetica, sans-serif; }

.odd { background:#ededed; padding:5px;font-family:Arial, Helvetica, sans-serif; }

.clients { background:#333333; color:#fff; padding:5px;font-family:Arial, Helvetica, sans-serif;}

.clientssum { background:#333333; color:#fff; padding:5px;font-family:Arial, Helvetica, sans-serif; text-align:center}


.weekend { background:#518d37}

</style>
</head>
<body style=" font-family:Arial, Helvetica, sans-serif">

<table style="font-family:Arial, Helvetica, sans-serif" cellspacing="1" cellpadding="1">
<tr><td colspan="'.$fullnamecol.'">'.$fullname.'</td><td>Captain`s Log</td></tr>
<tr>

<th style="background:#333333;color:#fff;padding:5px;font-family:Arial, Helvetica, sans-serif;">'.$mesec.$d1.'</th>';
for($i = 0; $i < $daynum; $i++): $currday = $i + 1;
$message.= '<th';
if(date("D", strtotime($fd.'-'.$currday)) == 'Sat' || date("D", strtotime($fd.'-'.$currday)) == 'Sun'){$message.=' class="weekend" style="background:#518d37; color:#fff; padding:5px;font-family:Arial, Helvetica, sans-serif;"';}else{$message.=' style="background:#333333; color:#fff; padding:5px;font-family:Arial, Helvetica, sans-serif;"';}
$message.='>'.$currday.'</th>';
endfor; $colspan = $i + 1;
$message.='<th style="background:#333333;color:#fff;padding:5px;font-family:Arial, Helvetica, sans-serif;">Suma</th></tr>';


$database->setBareQuery("SELECT DISTINCT(t1.name), t1.id FROM client t1, task t2, project t3 WHERE SUBSTRING_INDEX(t2.date, '-', 2) = '".$fd."' AND t2.user = '".$user."' AND t3.parent = t1.id AND t3.id = t2.project ORDER BY t1.name");
$clients = $database->loadResultArray(0);

$cid = $database->loadResultArray(1);
for($i = 0; $i < count($clients); $i++):
$message.='<tr>
<td colspan="'.$colspan.'" class="clients" style="color:#fff; background:#333333; padding:5px;font-family:Arial, Helvetica, sans-serif;">'.$clients[$i].'</td><td class="clientssum" style="color:#fff; background:#333333; text-align:center; background:#333333; padding:5px;font-family:Arial, Helvetica, sans-serif;">';

$database->setBareQuery("SELECT SUM(duration) FROM task WHERE user = '".$user."' AND project IN (SELECT id FROM project WHERE parent = '".$cid[$i]."') AND SUBSTRING_INDEX(date, '-', 2) = '".$fd."'");
$clientssum = $database->loadResultArray(0);
$message.=$clientssum[0].'</td></tr>';

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

$message.='<tr><td  class="'.$rowcol.'">'.$projects[$z].'</td>';

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

$message.=$dur[0].'</td>';
endfor;
$database->setBareQuery("SELECT SUM(duration) FROM task WHERE user = '".$user."' AND project = '".$pid[$z]."' AND SUBSTRING_INDEX(date, '-', 2) = '".$fd."'");
$projectsum = $database->loadResultArray(0);

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

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

$message.=$totalsum[0].'</th></tr></table></body></html> ';

echo '<p>'.$message.'</p>';

endfor;
?>