171 lines
5.3 KiB
PHP
171 lines
5.3 KiB
PHP
<?php
|
|
// Zugriff nur, wenn angemeldet
|
|
if (empty($_SESSION['user_logged_in'])) {
|
|
echo "<p>Zugriff verweigert.</p>";
|
|
exit;
|
|
}
|
|
|
|
$conn = db_connect();
|
|
|
|
$conn->query("SET @row := 0");
|
|
|
|
$wochenQuery = "
|
|
SELECT MIN(DATUM) AS start_date,
|
|
MAX(DATUM) AS end_date
|
|
FROM (
|
|
SELECT DATUM,
|
|
DATE_SUB(DATUM, INTERVAL (@row := @row + 1) DAY) AS grp
|
|
FROM Firmenberichte
|
|
ORDER BY DATUM
|
|
) AS t
|
|
GROUP BY grp
|
|
ORDER BY start_date;
|
|
";
|
|
|
|
$resultWochen = $conn->query($wochenQuery);
|
|
|
|
$wochenArray = [];
|
|
while($row = $resultWochen->fetch_assoc()){
|
|
$wochenArray[] = $row;
|
|
}
|
|
|
|
// Standard-Datenbereich: letzte 5 Tage
|
|
$startDate = $_POST['start_date'] ?? date('Y-m-d', strtotime('-5 days'));
|
|
$endDate = $_POST['end_date'] ?? date('Y-m-d');
|
|
|
|
$berichteQuery = "SELECT `DATUM`, `PROZESS_1`, `PROZESS_2`, `PROZESS_3`, `PROZESS_4`
|
|
FROM Firmenberichte
|
|
WHERE `DATUM` BETWEEN ? AND ?";
|
|
|
|
$stmt = $conn->prepare($berichteQuery);
|
|
$stmt->bind_param("ss", $startDate, $endDate);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
$berichteArray = [];
|
|
while($row = $result->fetch_assoc()){
|
|
$berichteArray[] = $row;
|
|
}
|
|
|
|
$stmt->close();
|
|
$conn->close();
|
|
|
|
// PDF-Generierung
|
|
function generatePDF($berichteArray) {
|
|
$Einzelstunden = "2";
|
|
$Gesamtstunden = "8";
|
|
$data = [
|
|
'Text1' => '', // Nr
|
|
'Text2' => $berichteArray[0]['DATUM'] ?? '',
|
|
'Text3' => end($berichteArray)['DATUM'] ?? '',
|
|
'Text4' => 'Eckhardt',
|
|
'Text5' => 'Bruno Renato',
|
|
'Text6' => '1',
|
|
];
|
|
|
|
$fieldIndex = 7;
|
|
foreach ($berichteArray as $bericht) {
|
|
for ($i = 1; $i <= 4; $i++) {
|
|
$data["Text$fieldIndex"] = $bericht["PROZESS_$i"] ?? '';
|
|
$fieldIndex++;
|
|
$data["Text$fieldIndex"] = $Einzelstunden;
|
|
$fieldIndex++;
|
|
}
|
|
$data["Text$fieldIndex"] = $Gesamtstunden;
|
|
$fieldIndex++;
|
|
}
|
|
|
|
$fdfFields = array_map(function($field, $value){
|
|
$value = str_replace(['(', ')'], ['\(', '\)'], $value);
|
|
return "<< /T ($field) /V ($value) >>";
|
|
}, array_keys($data), $data);
|
|
|
|
$fdf = "%FDF-1.2\n1 0 obj\n<<\n/FDF << /Fields [\n" .
|
|
implode("\n", $fdfFields) .
|
|
"\n] >> >>\nendobj\ntrailer\n<< /Root 1 0 R >>\n%%EOF";
|
|
|
|
$fdfFile = tempnam(sys_get_temp_dir(), 'fdf_') . '.fdf';
|
|
file_put_contents($fdfFile, $fdf);
|
|
|
|
$inputPdf = (new getSpecificData())->getDocumentRoot() . '/templates/ausbildungsnachweis.pdf';
|
|
ConsoleLog((new getSpecificData())->getDocumentRoot() . '/templates/ausbildungsnachweis.pdf');
|
|
$outputPdf = (new getSpecificData())->getDocumentRoot() . '/static/ausbildungsnachweis_' . $berichteArray[0]['DATUM'] . '-' . end($berichteArray)['DATUM'] . '.pdf';
|
|
|
|
exec("pdftk $inputPdf fill_form $fdfFile output $outputPdf flatten");
|
|
|
|
unlink($fdfFile);
|
|
|
|
return $outputPdf;
|
|
}
|
|
|
|
// Handle PDF generation
|
|
$pdfFile = null;
|
|
if (isset($_POST['generate_pdf']) && count($berichteArray) > 0) {
|
|
$pdfFile = generatePDF($berichteArray);
|
|
}
|
|
?>
|
|
|
|
<div class="content-box">
|
|
<h2>Ausbildungsberichte PDF erstellen</h2>
|
|
|
|
<form method="post" class="overtime-container">
|
|
<label for="start_date">Von:</label>
|
|
<input type="date" name="start_date" id="start_date" value="<?= htmlspecialchars($startDate) ?>">
|
|
|
|
<label for="end_date">Bis:</label>
|
|
<input type="date" name="end_date" id="end_date" value="<?= htmlspecialchars($endDate) ?>">
|
|
|
|
<button type="submit" name="generate_pdf">PDF erstellen</button>
|
|
</form>
|
|
|
|
<?php if ($pdfFile): ?>
|
|
<div class="overtime-result">
|
|
<?= htmlspecialchars(basename($pdfFile)) ?> erfolgreich erstellt!
|
|
</div>
|
|
<?php elseif(isset($_POST['generate_pdf'])): ?>
|
|
<div class="overtime-result negative">
|
|
Keine Berichte im gewählten Zeitraum gefunden.
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if(!empty($wochenArray)): ?>
|
|
<div class="wochen-liste">
|
|
<h3>Verfügbare Wochenabschnitte:</h3>
|
|
<ul>
|
|
<?php
|
|
$outputDir = (new getSpecificData())->getDocumentRoot() . '/static/';
|
|
foreach($wochenArray as $woche):
|
|
$pdfName = 'ausbildungsnachweis_' . $woche['start_date'] . '-' . $woche['end_date'] . '.pdf';
|
|
$pdfPath = $outputDir . $pdfName;
|
|
$exists = file_exists($pdfPath);
|
|
?>
|
|
<li>
|
|
<code><?= htmlspecialchars($woche['start_date']) ?></code> bis
|
|
<code><?= htmlspecialchars($woche['end_date']) ?></code>
|
|
<?php if($exists): ?>
|
|
<span style="color:green;">(PDF vorhanden)</span>
|
|
<a href="<?= htmlspecialchars('/static/' . $pdfName) ?>" download class="btn btn-success">
|
|
Download
|
|
</a>
|
|
|
|
|
|
<?php else: ?>
|
|
<span style="color:red;">(noch keine PDF)</span>
|
|
<form method="post" style="display:inline;">
|
|
<input type="hidden" name="start_date" value="<?= htmlspecialchars($woche['start_date']) ?>">
|
|
<input type="hidden" name="end_date" value="<?= htmlspecialchars($woche['end_date']) ?>">
|
|
<button type="submit" name="generate_pdf" class="btn btn-primary">
|
|
PDF erzeugen
|
|
</button>
|
|
|
|
</form>
|
|
<?php endif; ?>
|
|
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
|
|
|
|
</div>
|