Created
January 4, 2017 16:35
-
-
Save 4lb0/4282ab3fc8d26a38e8e614569fce68fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
setlocale(LC_ALL, 'es_AR.utf8'); | |
function emailToName($email) | |
{ | |
// Write emails here! | |
$emails = [ | |
'Jhon Doe' => '[email protected]', | |
'Jane Doe' => '[email protected]', | |
]; | |
if (!isset($emails[$email])) { | |
throw new Exception("Email $email no tiene nombre asociado"); | |
} | |
return $emails[$email]; | |
} | |
function stdinToCsv() | |
{ | |
$entries = array(); | |
$stdin = fopen('php://stdin', 'r'); | |
while (false !== ($row = fgetcsv($stdin, 1024))) { | |
if (!isset($cols)) { | |
$cols = $row; | |
} else { | |
$entry = array(); | |
foreach ($cols as $i => $col) { | |
$col = str_replace(' ', '_', strtolower($col)); | |
$entry[$col] = $row[$i]; | |
} | |
$entries[] = (object) $entry; | |
} | |
} | |
return $entries; | |
} | |
function dateToWeek($date) | |
{ | |
$date = new DateTime($date); | |
$week = new DateTime(); | |
$week->setISODate($date->format('Y'), $date->format('W')); | |
$from = $week->format('m') == $date->format('m') ? (int) $week->format('d') : 1; | |
$week->modify('+4 days'); | |
$to = $week->format('m') == $date->format('m') ? | |
(int) $week->format('d'): | |
(int) $date->format('t'); | |
return "Del $from al $to"; | |
} | |
function timeToInt($time) | |
{ | |
list($hours, $minutes) = explode(':', $time); | |
return round($hours + $minutes / 60, 1); | |
} | |
function group(array $entries) | |
{ | |
$grouped = array(); | |
foreach ($entries as $entry) { | |
$email = $entry->email; | |
if (!isset($grouped[$email])) { | |
$grouped[$email] = []; | |
} | |
$project = $entry->project; | |
if (!isset($grouped[$email][$project])) { | |
$grouped[$email][$project] = []; | |
} | |
$grouped[$email][$project][] = (object) array( | |
'week' => dateToWeek($entry->start_date), | |
'description' => $entry->description, | |
'hours' => timeToInt($entry->duration), | |
); | |
} | |
return $grouped; | |
} | |
function totalHours(array $entries) | |
{ | |
$totalHours = 0; | |
foreach ($entries as $entry) { | |
$totalHours += $entry->hours; | |
} | |
return $totalHours; | |
} | |
function getMonthYear(array $entries) | |
{ | |
$middleEntry = $entries[ceil(count($entries) / 2)]; | |
$date = new DateTime($middleEntry->start_date); | |
return ucfirst(strftime('%B de %Y', $date->getTimestamp())); | |
} | |
$entries = stdinToCsv(); | |
$grouped = group($entries); | |
$monthYear = getMonthYear($entries); | |
$byProject = []; | |
foreach ($grouped as $email => $projects) { | |
$total = 0; | |
foreach ($projects as $project => $entries) { | |
if (!isset($byProject[$project])) { | |
$byProject[$project] = ['total' => 0]; | |
} | |
$horas = totalHours($entries); | |
$byProject[$project][$email] = $horas; | |
$byProject[$project]['total'] += $horas; | |
} | |
} | |
print_r($byProject); | |
foreach ($grouped as $email => $projects) { | |
$name = emailToName($email); | |
$title = "Reporte de Horas de $name - $monthYear"; | |
ob_start(); | |
?> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<title><?php echo $title ?></title> | |
</head> | |
<body> | |
<h1><?php echo $title ?></h1> | |
<style>td, th{ width: 100px } .desc{ width: 500px }</style> | |
<?php foreach ($projects as $project => $entries): ?> | |
<p><strong><?php echo $project ?></strong></p> | |
<p>Total: <?php echo totalHours($entries) ?></p> | |
<table> | |
<tr> | |
<th>Semana</th> | |
<th class="desc">Descripcion</th> | |
<th>Horas</th> | |
</tr> | |
<?php foreach ($entries as $entry): ?> | |
<tr> | |
<td><?php echo $entry->week ?></td> | |
<td class="desc"><?php echo $entry->description ?></td> | |
<td><?php echo $entry->hours ?></td> | |
</tr> | |
<?php endforeach ?> | |
</table> | |
<?php endforeach; ?> | |
</body> | |
</html> | |
<?php | |
$html = ob_get_clean(); | |
$file = strtolower(str_replace(' ', '_', $name)) . '.html'; | |
file_put_contents($file, $html); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment