diff --git a/httpdocs/src/Controller/UserController.php b/httpdocs/src/Controller/UserController.php index f584d18..adea851 100644 --- a/httpdocs/src/Controller/UserController.php +++ b/httpdocs/src/Controller/UserController.php @@ -16,10 +16,17 @@ use App\Entity\UserWorksheetTask; use App\Repository\UserRepository; use App\Service\CalculatorService; use App\Utils\Utils; +use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Style\Alignment; +use PhpOffice\PhpSpreadsheet\Style\Fill; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; +use PhpOffice\PhpSpreadsheet\Writer\IWriter; +use PhpOffice\PhpSpreadsheet\Writer\Xls; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\Routing\Annotation\Route; use Twig\Environment; @@ -233,7 +240,7 @@ class UserController extends AbstractController /** * @Route("/download/{uuid}", name="download") */ - public function downloadLastCataloguePdf(Environment $twig, $uuid) + public function downloadPdf(Environment $twig, $uuid) { ini_set('max_execution_time', '60'); @@ -259,6 +266,129 @@ class UserController extends AbstractController exit(0); } + /** + * @Route("/excel/{uuid}", name="excel") + */ + public function exportExcel($uuid) + { + /** @var User $user */ + $user = $this->getUser(); + $em = $this->getDoctrine()->getManager(); + + /** @var UserWorksheet $worksheet */ + $worksheet = $em->getRepository(UserWorksheet::class)->findOneBy(['id' => $uuid]); + + if (null === $worksheet || $user->getId() !== $worksheet->getUser()->getId()) { + throw new \Exception('not allowed or not exists'); + } + + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + + $sheet->getStyle('A1:I200')->applyFromArray( + [ + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_LEFT, + 'wrapText' => true + ], + 'font' => [ + 'size' => 12, + 'name' => 'Arial' + ], + ] + ); + $sheet->getColumnDimension('A')->setWidth(80); + $sheet->getColumnDimension('B')->setWidth(50); + $sheet->getColumnDimension('C')->setWidth(20); + $sheet->getColumnDimension('D')->setWidth(20); + $sheet->getColumnDimension('E')->setWidth(20); + $sheet->getColumnDimension('F')->setWidth(20); + $sheet->getColumnDimension('G')->setWidth(20); + $sheet->getColumnDimension('H')->setWidth(20); + $sheet->getColumnDimension('I')->setWidth(50); + + $sheet->getStyle('A1')->getFont()->setSize(18); + $sheet->setCellValue('A1', 'Arbeitsblatt'); + $sheet->setCellValue('B1', 'erstellt am: ' . $worksheet->getCreationDate()->format('d.m.Y H:i')); + $sheet->getStyle('A3')->getFont()->setSize(16); + $sheet->setCellValue('A3', $worksheet->getDinNumber(). " ".$worksheet->getDinAssetName()); + + $sheet->getStyle('A5')->getFont()->setSize(16); + $sheet->setCellValue('A5', 'Risikoanalyse'); + $sheet->setCellValue('A6', 'Wie hoch ist die Ausfallwahrscheinlichkeit?'); + $sheet->setCellValue('B6', $worksheet->getFailureProbability()); + $sheet->setCellValue('A7', 'Wie hoch ist der Schaden bei Ausfall?'); + $sheet->setCellValue('B7', $worksheet->getFailureDamage()); + + $sheet->getStyle('A9')->getFont()->setSize(16); + $sheet->setCellValue('A9', 'Optimierungscheck'); + $sheet->setCellValue('A10', 'Wird durch die Wartung der Abnutzungsvorrat positiv beeinflusst?'); + $sheet->setCellValue('B10', $worksheet->getMaintenanceAdvantage() ? 'Ja' : 'Nein'); + $sheet->setCellValue('A11', 'Kann durch eine Inspektion der Zustand der Anlage erkannt werden?'); + $sheet->setCellValue('B11', $worksheet->getInspectionAdvantage() ? 'Ja' : 'Nein'); + + $sheet->getStyle('A13')->getFont()->setSize(16); + $sheet->setCellValue('A13', 'Zyklus und Größencluster'); + $sheet->setCellValue('A14', 'Aktueller Zyklus (in Monaten)'); + $sheet->setCellValue('B14', $worksheet->getUserCycleMonths()); + $sheet->setCellValue('A15', 'Größencluster'); + $sheet->setCellValue('B15', $worksheet->getBandwidth()); + + $sheet->getStyle('A17')->getFont()->setSize(16); + $sheet->setCellValue('A17', 'Ergebnis'); + $sheet->setCellValue('A18', 'Empfehlung Wartung (in Monaten)'); + $sheet->setCellValue('B18', $worksheet->getRecMaintenanceCycleMonths()); + $sheet->setCellValue('A19', 'Empfehlung Inspektion (in Monaten)'); + $sheet->setCellValue('B19', $worksheet->getRecInspectionCycleMonths()); + $sheet->setCellValue('A20', 'Einsparpotential p.a. (%)'); + $sheet->setCellValue('B20', $worksheet->getDeviationPercentage() * 100); + $sheet->setCellValue('A21', 'Einsparpotential p.a. (€)'); + $sheet->setCellValue('B21', $worksheet->getDeviationCosts()); + + $sheet->getStyle('A23')->getFont()->setSize(16); + $sheet->setCellValue('A23', 'Tätigkeiten'); + $sheet->getStyle('A23:I24')->getFont()->setSize(14); + $sheet->getStyle('A24:I24')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('ffa4ffa4'); + $sheet->setCellValue('A24', 'Aufgabe'); + $sheet->setCellValue('B24', 'Baugruppen'); + $sheet->setCellValue('C24', 'Inspektion'); + $sheet->setCellValue('D24', 'Wartung'); + $sheet->setCellValue('E24', 'Instandsetzung'); + $sheet->setCellValue('F24', 'Sachverständiger'); + $sheet->setCellValue('G24', 'Sachkundige'); + $sheet->setCellValue('H24', 'Fachkraft'); + $sheet->setCellValue('I24', 'Normative Grundlage'); + + $row = 25; + $worksheetTasks = UserRepository::getSortedWorksheetTasks($worksheet); + /** @var UserWorksheetTask $task */ + foreach ($worksheetTasks as $task) { + $sheet->setCellValue('A'.$row, (($row-24) .". ").$task->getTask()); + $sheet->setCellValue('B'.$row, $task->getDelimitation()); + $sheet->setCellValue('C'.$row, $task->getInspection() ? 'Ja' : 'Nein'); + $sheet->setCellValue('D'.$row, $task->getMaintenance() ? 'Ja' : 'Nein'); + $sheet->setCellValue('E'.$row, $task->getService() ? 'Ja' : 'Nein'); + $sheet->setCellValue('F'.$row, $task->getExpert() ? 'Ja' : 'Nein'); + $sheet->setCellValue('G'.$row, $task->getSpecialist() ? 'Ja' : 'Nein'); + $sheet->setCellValue('H'.$row, $task->getSkilledPersonnel() ? 'Ja' : 'Nein'); + $sheet->setCellValue('I'.$row, $task->getNorm()); + $row++; + } + + $writer = new Xls($spreadsheet); + + $response = new StreamedResponse( + function () use ($writer) { + $writer->save('php://output'); + } + ); + $filename = "Arbeitskarte_".$worksheet->getCreationDate()->format('Y-m-d_H-i'); + $response->headers->set('Content-Type', 'application/vnd.ms-excel'); + $response->headers->set('Content-Disposition', 'attachment;filename="'.$filename.'.xls"'); + $response->headers->set('Cache-Control','max-age=0'); + return $response; + } + /** * @Route("/legal-page", name="legal_page", methods={"GET"}) */ diff --git a/httpdocs/templates/pages/worksheet.html.twig b/httpdocs/templates/pages/worksheet.html.twig index 0992fde..23adaa1 100644 --- a/httpdocs/templates/pages/worksheet.html.twig +++ b/httpdocs/templates/pages/worksheet.html.twig @@ -16,5 +16,6 @@ } %} Arbeitskarte als PDF herunterladen + Arbeitskarte als Excel herunterladen {% endblock %}