diff --git a/files/custom/Espo/Modules/LegalAssistance/SmartAssistant/Tools/DirectAccessReportGenerator.php b/files/custom/Espo/Modules/LegalAssistance/SmartAssistant/Tools/DirectAccessReportGenerator.php index 6c9a94a..2896f70 100644 --- a/files/custom/Espo/Modules/LegalAssistance/SmartAssistant/Tools/DirectAccessReportGenerator.php +++ b/files/custom/Espo/Modules/LegalAssistance/SmartAssistant/Tools/DirectAccessReportGenerator.php @@ -106,10 +106,10 @@ class DirectAccessReportGenerator ]); $this->entityManager->saveEntity($document); - // Link Document to Case - $this->entityManager->getRDBRepository('Document') - ->getRelation($document, 'cases') - ->relate($case); + // Link Document to Case (via Case side of relationship) + $this->entityManager->getRDBRepository('Case') + ->getRelation($case, 'documents') + ->relate($document); $this->log->info("LegalAssistance: Report generated — Document {$document->getId()} attached to Case {$caseId}"); @@ -230,32 +230,31 @@ class DirectAccessReportGenerator private function resolveTemplatePath(): string { - // Look up template via DocumentTemplate entity $template = $this->entityManager ->getRDBRepository('DocumentTemplate') ->where(['name' => self::TEMPLATE_NAME]) ->findOne(); if ($template) { - $fileId = $template->get('fileId'); - if ($fileId) { - $filePath = 'data/upload/' . $fileId; - if (file_exists($filePath)) { - return $filePath; - } - } - - // Try templatePath field $templatePath = $template->get('templatePath'); - if ($templatePath && file_exists($templatePath)) { - return $templatePath; + if ($templatePath) { + // Try as-is (absolute or relative) + if (file_exists($templatePath)) { + return $templatePath; + } + + // Try under data/document-templates/ + $resolved = 'data/document-templates/' . ltrim($templatePath, '/'); + if (file_exists($resolved)) { + return $resolved; + } } } - // Fallback: look in known locations + // Fallback: known locations $fallbackPaths = [ + 'data/document-templates/direct-access-report.docx', 'custom/Espo/Modules/LegalAssistance/templates/direct-access-report.docx', - __DIR__ . '/../../../../../../templates/direct-access-report.docx', ]; foreach ($fallbackPaths as $path) { @@ -269,6 +268,15 @@ class DirectAccessReportGenerator private function processTemplate(string $inputPath, string $outputPath, array $data): void { + // Ensure PHPWord autoloader is registered + if (!class_exists(TemplateProcessor::class, false)) { + $autoloader = 'vendor/phpoffice/phpword/src/PhpWord/Autoloader.php'; + if (file_exists($autoloader)) { + require_once $autoloader; + \PhpOffice\PhpWord\Autoloader::register(); + } + } + $processor = new TemplateProcessor($inputPath); foreach ($data as $key => $value) {