-
-
Save 4lb0/4ed2f2d66837d818607cb94d87c9afbe to your computer and use it in GitHub Desktop.
Toggl script
This file contains 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' => 'jhon.doe@example.com', | |
'Jane Doe' => 'jane.doe@example.com', | |
]; | |
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