From 0d0798482142e6db40b0262925450e5485d1ac51 Mon Sep 17 00:00:00 2001 From: Praesidiarius Date: Thu, 25 Apr 2024 17:59:42 +0200 Subject: [PATCH] enable item document generator (#3) --- src/Controller/DocumentController.php | 5 ++++ src/Service/Document/DocumentGenerator.php | 31 ++++++++++++++++++++-- translations/messages.de.yaml | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Controller/DocumentController.php b/src/Controller/DocumentController.php index 590e4c4..343bc91 100644 --- a/src/Controller/DocumentController.php +++ b/src/Controller/DocumentController.php @@ -190,6 +190,11 @@ public function generate( $document, $generateDocumentRequest->getEntityId(), ), + 'item' => $this->documentGenerator->generateItemDocument( + $template, + $document, + $generateDocumentRequest->getEntityId(), + ) }; $document->setFileName($fileName); diff --git a/src/Service/Document/DocumentGenerator.php b/src/Service/Document/DocumentGenerator.php index f616a19..d54741e 100644 --- a/src/Service/Document/DocumentGenerator.php +++ b/src/Service/Document/DocumentGenerator.php @@ -8,6 +8,7 @@ use App\Repository\ContactAddressRepository; use App\Repository\ContactRepository; use App\Repository\DynamicFormFieldRepository; +use App\Repository\ItemRepository; use App\Repository\JobRepository; use App\Service\Contact\ContactHistoryWriter; use PhpOffice\PhpWord\Element\TextRun; @@ -21,6 +22,7 @@ public function __construct( private readonly ContactRepository $contactRepository, private readonly ContactAddressRepository $addressRepository, private readonly JobRepository $jobRepository, + private readonly ItemRepository $itemRepository, private readonly Security $security, private readonly TranslatorInterface$translator, private readonly DynamicFormFieldRepository $formFieldRepository, @@ -101,8 +103,7 @@ public function generateJobDocument( DocumentTemplate $template, Document $document, int $jobId, - ): string - { + ): string { $job = $this->jobRepository->findById($jobId); $fileName = $template->getName() . '-' . $job->getId() . '-' . $document->getId() . '.docx'; @@ -241,6 +242,31 @@ public function generateJobDocument( return $fileName; } + public function generateItemDocument( + DocumentTemplate $template, + Document $document, + int $itemId, + ): string { + $item = $this->itemRepository->findById($itemId); + + $fileName = $template->getName() . '-' . $item->getId() . '-' . $document->getId() . '.docx'; + $templateProcessor = new TemplateProcessor($this->documentBaseDir + . '/templates/' . $template->getId() . '.docx' + ); + + // item specific placeholders + $this->replaceDynamicPlaceHolders('item', $templateProcessor, $item); + + // general placeholders + $templateProcessor->setValue('date', date('d.m.Y', time())); + + $templateProcessor->saveAs($this->documentBaseDir + . '/' . $template->getType()->getIdentifier() . '/' . $fileName + ); + + return $fileName; + } + public function getDocumentAsBase64Download( Document $document ) : string { @@ -264,6 +290,7 @@ private function replaceDynamicPlaceHolders(string $formKey, TemplateProcessor & } $fieldValue = match ($field->getFieldType()) { 'date' => date('d.m.Y', strtotime($data->getTextField($field->getFieldKey()))), + 'select' => $this->translator->trans($data->getSelectField($field->getFieldKey())['name']), default => $data->getTextField($field->getFieldKey()) }; $templateProcessor->setValue(substr($formKey,0,1). '_' . $field->getFieldKey(), $fieldValue); diff --git a/translations/messages.de.yaml b/translations/messages.de.yaml index 47b378f..fbf656f 100644 --- a/translations/messages.de.yaml +++ b/translations/messages.de.yaml @@ -40,6 +40,7 @@ document: type: contact: Kundendokumente job: Auftragsdokumente + item: Artikeldokumente form: section: basic: Vorlage