<?php
namespace Tools\Service;
use DateTime;
use DavaHome\Helper\Utils\StringUtils;
class ZeusService
{
private function parse(string $field, array $lines): float
{
foreach ($lines as $line) {
$identifier = sprintf('Urlaub (%s)', $field);
$i = strpos($line, $identifier);
if ($i !== false) {
return (float)str_replace(',', '.', substr($line, $i + strlen($identifier)));
}
}
return 0.0;
}
private function formatLastUpdate(string $dateString = 'now'): string
{
$dateTime = ($dateString === 'now')
? new DateTime($dateString)
: DateTime::createFromFormat('d.m.y', $dateString);
return $dateTime
->format('D, d.m.Y (\K\W W)');
}
public function readZeusData(string $content): array
{
$zeusData = [];
foreach (explode("\n", $content) as $line) {
$zeusData[] = rtrim($line);
}
$zeus = [
'AnspAkt' => $this->parse('AnspAkt', $zeusData),
'AnspGes' => $this->parse('AnspGes', $zeusData),
'GuthAkt' => $this->parse('GuthAkt', $zeusData),
'GuthRst' => $this->parse('GuthRst', $zeusData),
'VerfAlt' => $this->parse('VerfAlt', $zeusData),
];
// Determine days
$available = max($zeus['AnspAkt'], $zeus['AnspGes']);
$planned = $zeus['GuthAkt'] - $zeus['GuthRst'];
$took = $available - $zeus['GuthAkt'];
$used = $planned + $took;
$remaining = $available - $used;
// Determine last update
$lastUpdate = 'Unknown';
foreach ($zeusData as $zeusLine) {
if (StringUtils::contains($zeusLine, 'Ausgabebereich:', true)) {
$zeusLine = trim($zeusLine);
$i = strpos($zeusLine, '-');
if ($i !== false) {
$lastUpdate = $this->formatLastUpdate(trim(substr($zeusLine, $i + 1, -1)));
}
break;
}
}
return [
'total' => $zeus['AnspAkt'],
'available' => $available,
'took' => $took,
'planned' => $planned,
'used' => $used,
'remaining' => $remaining,
'expireCount' => $zeus['VerfAlt'],
'lastUpdate' => $lastUpdate,
];
}
}