Added the support for generating docs from template doc
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\StoreDocumentTemplateRequest;
|
||||
use App\Http\Requests\UpdateDocumentTemplateRequest;
|
||||
use App\Models\Action;
|
||||
use App\Models\DocumentTemplate;
|
||||
use App\Services\Documents\TokenScanner;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -19,9 +20,16 @@ public function index()
|
||||
{
|
||||
$this->ensurePermission();
|
||||
$templates = DocumentTemplate::query()->orderByDesc('updated_at')->get();
|
||||
$actions = Action::with(['decisions:id,name'])->orderBy('name')->get(['id', 'name']);
|
||||
$actionsMapped = $actions->map(fn ($a) => [
|
||||
'id' => $a->id,
|
||||
'name' => $a->name,
|
||||
'decisions' => $a->decisions->map(fn ($d) => ['id' => $d->id, 'name' => $d->name])->values(),
|
||||
]);
|
||||
|
||||
return Inertia::render('Admin/DocumentTemplates/Index', [
|
||||
'templates' => $templates,
|
||||
'actions' => $actionsMapped,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -35,10 +43,51 @@ public function toggleActive(DocumentTemplate $template)
|
||||
return redirect()->back()->with('success', 'Status predloge posodobljen.');
|
||||
}
|
||||
|
||||
public function show(DocumentTemplate $template)
|
||||
{
|
||||
$this->ensurePermission();
|
||||
return Inertia::render('Admin/DocumentTemplates/Show', [
|
||||
'template' => $template,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(DocumentTemplate $template)
|
||||
{
|
||||
$this->ensurePermission();
|
||||
$actions = Action::with(['decisions:id,name'])->orderBy('name')->get(['id', 'name']);
|
||||
$actionsMapped = $actions->map(fn ($a) => [
|
||||
'id' => $a->id,
|
||||
'name' => $a->name,
|
||||
'decisions' => $a->decisions->map(fn ($d) => ['id' => $d->id, 'name' => $d->name])->values(),
|
||||
]);
|
||||
|
||||
return Inertia::render('Admin/DocumentTemplates/Edit', [
|
||||
'template' => $template,
|
||||
'actions' => $actionsMapped,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateSettings(UpdateDocumentTemplateRequest $request, DocumentTemplate $template)
|
||||
{
|
||||
$this->ensurePermission();
|
||||
$template->fill($request->only(['output_filename_pattern', 'date_format']));
|
||||
$template->fill($request->only([
|
||||
'output_filename_pattern', 'date_format', 'action_id', 'decision_id', 'activity_note_template',
|
||||
]));
|
||||
// If both action & decision provided, ensure decision belongs to action (parity with import templates)
|
||||
if ($request->filled('action_id') && $request->filled('decision_id')) {
|
||||
$belongs = \DB::table('action_decision')
|
||||
->where('action_id', $request->integer('action_id'))
|
||||
->where('decision_id', $request->integer('decision_id'))
|
||||
->exists();
|
||||
if (! $belongs) {
|
||||
return redirect()->back()->withErrors(['decision_id' => 'Izbrana odločitev ne pripada izbrani akciji.']);
|
||||
}
|
||||
} elseif ($request->filled('action_id') && ! $request->filled('decision_id')) {
|
||||
// Allow clearing decision when action changes
|
||||
if ($template->isDirty('action_id')) {
|
||||
$template->decision_id = null;
|
||||
}
|
||||
}
|
||||
if ($request->has('fail_on_unresolved')) {
|
||||
$template->fail_on_unresolved = (bool) $request->boolean('fail_on_unresolved');
|
||||
}
|
||||
@@ -153,6 +202,19 @@ public function store(StoreDocumentTemplateRequest $request)
|
||||
'currency_space' => true,
|
||||
],
|
||||
];
|
||||
// Optional meta + activity linkage fields (parity with import templates style)
|
||||
if ($request->filled('meta') && is_array($request->input('meta'))) {
|
||||
$payload['meta'] = array_filter($request->input('meta'), fn ($v) => $v !== null && $v !== '');
|
||||
}
|
||||
if ($request->filled('action_id')) {
|
||||
$payload['action_id'] = $request->integer('action_id');
|
||||
}
|
||||
if ($request->filled('decision_id')) {
|
||||
$payload['decision_id'] = $request->integer('decision_id');
|
||||
}
|
||||
if ($request->filled('activity_note_template')) {
|
||||
$payload['activity_note_template'] = $request->input('activity_note_template');
|
||||
}
|
||||
if (Schema::hasColumn('document_templates', 'tokens')) {
|
||||
$payload['tokens'] = $tokens;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user