updated search and fix error in template where it removed client from template when saving in edit

This commit is contained in:
Simon Pocrnjič
2025-10-05 20:42:51 +02:00
parent bab9d6561f
commit 020c8ce61b
4 changed files with 336 additions and 88 deletions
@@ -294,6 +294,12 @@ public function update(Request $request, ImportTemplate $template)
if (! empty($raw['client_uuid'] ?? null)) {
$raw['client_id'] = Client::where('uuid', $raw['client_uuid'])->value('id');
}
// If template already has mappings, lock client assignment on backend as well
// to prevent accidental clearing when client_uuid/client_id not sent.
$hasMappings = $template->mappings()->exists();
if ($hasMappings) {
unset($raw['client_id'], $raw['client_uuid']);
}
$data = validator($raw, [
'name' => 'required|string|max:100',
'description' => 'nullable|string|max:255',
@@ -338,26 +344,28 @@ public function update(Request $request, ImportTemplate $template)
}
}
$template->update([
// Finalize meta (ensure payments entities forced if enabled)
$finalMeta = $newMeta;
if (! empty($finalMeta['payments_import'])) {
$finalMeta['entities'] = ['contracts', 'accounts', 'payments'];
}
$update = [
'name' => $data['name'],
'description' => $data['description'] ?? null,
'source_type' => $data['source_type'],
'default_record_type' => $data['default_record_type'] ?? null,
'client_id' => $data['client_id'] ?? null,
// Only set client_id if explicitly present and not locked, otherwise keep existing
'is_active' => $data['is_active'] ?? $template->is_active,
'reactivate' => $data['reactivate'] ?? $template->reactivate,
'sample_headers' => $data['sample_headers'] ?? $template->sample_headers,
'meta' => (function () use ($newMeta) {
// If payments import mode is enabled, force entities sequence in meta
$meta = $newMeta;
$payments = (bool) ($meta['payments_import'] ?? false);
if ($payments) {
$meta['entities'] = ['contracts', 'accounts', 'payments'];
}
return $meta;
})(),
]);
'meta' => $finalMeta,
];
if (! $hasMappings && array_key_exists('client_id', $data)) {
$update['client_id'] = $data['client_id'];
}
// When locked, do not touch client_id (prevents clearing to null)
$template->update($update);
return redirect()->route('importTemplates.edit', ['template' => $template->uuid])
->with('success', 'Template updated');