Skip to content

Commit

Permalink
enable item document generator (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Praesidiarius authored Apr 25, 2024
1 parent d8cc476 commit 0d07984
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Controller/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ public function generate(
$document,
$generateDocumentRequest->getEntityId(),
),
'item' => $this->documentGenerator->generateItemDocument(
$template,
$document,
$generateDocumentRequest->getEntityId(),
)
};

$document->setFileName($fileName);
Expand Down
31 changes: 29 additions & 2 deletions src/Service/Document/DocumentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions translations/messages.de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ document:
type:
contact: Kundendokumente
job: Auftragsdokumente
item: Artikeldokumente
form:
section:
basic: Vorlage
Expand Down

0 comments on commit 0d07984

Please sign in to comment.