Compare commits

..

4 Commits

Author SHA1 Message Date
chaim 99bc442495 fix: handle stdClass legalAnalysis in DirectAccessReportGenerator
When legalAnalysis arrives as a stdClass (from JSON decode), cast it to
array before using array access. Prevents fatal error in PHP 8.x.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 20:59:14 +00:00
chaim 61fe16a0ea fix: enforce legal analysis step and correct recommendation selection
- Prompt: CRITICAL instruction — must complete all 7 steps before generating report
- Step 5 (legal analysis) is mandatory, cannot be skipped even if user asks
- Step 6: explicit instruction to use "המשך ייצוג" when user is filing/continuing
- Prevent AI from choosing "עדיין לא ניתן להחליט" when user has already decided

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 16:49:44 +00:00
chaim 14979dba4b fix: direct access report placeholder mismatches and missing recommendation option
- Fix cLegalAidNumber placeholder key mismatch (template expects cLegalAidNumber, code sent legalAidNumber)
- Add fallback for flat legalAnalysis fields (AI may send them outside the nested object)
- Improve assignedUserName resolution: prefer case assignee, filter out system/API user names
- Add "המשך ייצוג" positive recommendation option to enum, checkboxes, prompt, and DOCX template

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 16:30:19 +00:00
chaim 015586a9df fix: resolve report template from WebDAV when not found locally
DirectAccessReportGenerator now downloads the template from NetworkStorage
(WebDAV) if it's not found in data/document-templates/. This means template
updates via WebDAV are picked up automatically without manual file copy.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 13:41:26 +00:00
5 changed files with 92 additions and 10 deletions
@@ -58,9 +58,28 @@ class DirectAccessReportGenerator
? trim($contact->get('firstName') . ' ' . $contact->get('lastName'))
: 'ללא איש קשר';
// Get assigned user name
$assignedUser = $userId ? $this->entityManager->getEntityById('User', $userId) : null;
$assignedUserName = $assignedUser ? $assignedUser->get('name') : ($this->user->get('name') ?? '');
// Get assigned user name — prefer case's assignedUser, then userId, then current user
$assignedUserName = '';
$caseAssignedUserId = $case->get('assignedUserId');
if ($caseAssignedUserId) {
$caseAssignedUser = $this->entityManager->getEntityById('User', $caseAssignedUserId);
if ($caseAssignedUser) {
$assignedUserName = $caseAssignedUser->get('name') ?? '';
}
}
if (empty($assignedUserName) && $userId) {
$assignedUser = $this->entityManager->getEntityById('User', $userId);
if ($assignedUser) {
$assignedUserName = $assignedUser->get('name') ?? '';
}
}
if (empty($assignedUserName)) {
$assignedUserName = $this->user->get('name') ?? '';
}
// Filter out system/API user names
if (in_array(strtolower($assignedUserName), ['api', 'x-api-key', 'system', 'admin'], true)) {
$assignedUserName = '';
}
// Build placeholder data directly from params
$data = $this->buildPlaceholderData($params, $case, $contactName, $assignedUserName);
@@ -83,6 +102,11 @@ class DirectAccessReportGenerator
$content = file_get_contents($outputPath);
@unlink($outputPath);
// Clean up temp template if downloaded from storage
if (str_starts_with($templatePath, self::TEMP_DIR)) {
@unlink($templatePath);
}
// Create Attachment
$attachment = $this->entityManager->getNewEntity('Attachment');
$attachment->set([
@@ -126,11 +150,22 @@ class DirectAccessReportGenerator
$legalAnalysis = $params['legalAnalysis'] ?? [];
if (is_string($legalAnalysis)) {
$legalAnalysis = json_decode($legalAnalysis, true) ?? [];
} elseif (is_object($legalAnalysis)) {
$legalAnalysis = (array) $legalAnalysis;
}
// Fallback: if AI sent legalAnalysis fields flat (not nested), pick them up
$laFields = ['q1DocsDetail', 'q2ReasonDetail', 'q4PanelComposition', 'q6ComplaintsDetail',
'q8ClinicalGap', 'q9MoharaDetail', 'q10RehabDetail', 'q11ScoreDetail', 'q12UniqueDetail'];
foreach ($laFields as $f) {
if (empty($legalAnalysis[$f]) && !empty($params[$f])) {
$legalAnalysis[$f] = $params[$f];
}
}
return [
// Header
'legalAidNumber' => $params['legalAidNumber'] ?? $case->get('cLegalAidNumber') ?? '',
'cLegalAidNumber' => $params['legalAidNumber'] ?? $case->get('cLegalAidNumber') ?? '',
'contactName' => $contactName,
'assignedUserName' => $assignedUserName,
'reportDate' => $this->formatDate(date('Y-m-d')),
@@ -206,6 +241,7 @@ class DirectAccessReportGenerator
$data['chk_contact_bad'] = ($contactStatus === 'לא תקין') ? $CHK : $UNCHK;
$rec = $params['recommendationType'] ?? '';
$data['chk_rec_proceed'] = ($rec === 'המשך ייצוג') ? $CHK : $UNCHK;
$data['chk_rec_consulting'] = ($rec === 'ייעוץ והדרכה חלף ייצוג') ? $CHK : $UNCHK;
$data['chk_rec_waiver'] = ($rec === 'ויתור הלקוח') ? $CHK : $UNCHK;
$data['chk_rec_replacement'] = ($rec === 'החלפת ייצוג') ? $CHK : $UNCHK;
@@ -248,6 +284,12 @@ class DirectAccessReportGenerator
if (file_exists($resolved)) {
return $resolved;
}
// Download from NetworkStorage (WebDAV)
$downloaded = $this->downloadTemplateFromStorage($templatePath);
if ($downloaded) {
return $downloaded;
}
}
}
@@ -266,6 +308,46 @@ class DirectAccessReportGenerator
throw new Error("Template '" . self::TEMPLATE_NAME . "' not found.");
}
private function downloadTemplateFromStorage(string $templatePath): ?string
{
try {
$nds = $this->injectableFactory->create(
\Espo\Modules\NetworkStorageIntegration\Services\NetworkDocumentService::class
);
$client = $nds->getClient();
// Try Templates/{templatePath} on the storage
$integration = $this->entityManager->getEntityById('Integration', 'NetworkStorage');
$templatesFolderPath = 'Templates';
if ($integration && $integration->get('enabled')) {
$data = (array) ($integration->get('data') ?? []);
$templatesFolderPath = $data['templatesFolderPath'] ?? 'Templates';
}
$storagePath = $templatesFolderPath . '/' . $templatePath;
if (!$client->exists($storagePath)) {
$this->log->debug("LegalAssistance: Template not found on storage: {$storagePath}");
return null;
}
$content = $client->downloadFile($storagePath);
// Save locally for this request
if (!is_dir(self::TEMP_DIR)) {
mkdir(self::TEMP_DIR, 0775, true);
}
$localPath = self::TEMP_DIR . 'template_' . uniqid() . '.docx';
file_put_contents($localPath, $content);
$this->log->debug("LegalAssistance: Template downloaded from storage: {$storagePath}{$localPath}");
return $localPath;
} catch (\Exception $e) {
$this->log->warning("LegalAssistance: Failed to download template from storage: " . $e->getMessage());
return null;
}
}
private function processTemplate(string $inputPath, string $outputPath, array $data): void
{
// Ensure PHPWord autoloader is registered
+2 -2
View File
@@ -1,9 +1,9 @@
{
"name": "LegalAssistance",
"version": "2.0.1",
"version": "2.0.5",
"acceptableVersions": [">=8.0.0"],
"php": [">=8.1"],
"releaseDate": "2026-04-07",
"releaseDate": "2026-04-09",
"author": "Marcus-Law",
"description": "סיוע משפטי — יצירת דוח גישה ישירה (JSON ישירות ל-DOCX), אינטגרציה עם שירה",
"scripts": {
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -109,8 +109,8 @@
},
"recommendationType": {
"type": "string",
"enum": ["ייעוץ והדרכה חלף ייצוג", "ויתור הלקוח", "החלפת ייצוג", "לא נוצר קשר", "לא התקיימה פגישה", "עדיין לא ניתן להחליט", "נדרשים מסמכים נוספים", "המלצה לסירוב"],
"description": "סוג ההמלצה/ההודעה"
"enum": ["המשך ייצוג", "ייעוץ והדרכה חלף ייצוג", "ויתור הלקוח", "החלפת ייצוג", "לא נוצר קשר", "לא התקיימה פגישה", "עדיין לא ניתן להחליט", "נדרשים מסמכים נוספים", "המלצה לסירוב"],
"description": "סוג ההמלצה/ההודעה — בחר ׳המשך ייצוג׳ כשמומלץ להגיש ערעור או להמשיך בייצוג"
},
"recommendationDetail": {
"type": "string",
Binary file not shown.