Emergency button for missing persons
This commit is contained in:
@@ -228,11 +228,12 @@ public function updateContract(ClientCase $clientCase, string $uuid, UpdateContr
|
||||
public function debugContractAccounts(ClientCase $clientCase, string $uuid, Request $request)
|
||||
{
|
||||
abort_unless(config('app.debug'), 404);
|
||||
$contract = $clientCase->contracts()->where('uuid', $uuid)->firstOrFail(['id','uuid','reference']);
|
||||
$contract = $clientCase->contracts()->where('uuid', $uuid)->firstOrFail(['id', 'uuid', 'reference']);
|
||||
$accounts = \DB::table('accounts')
|
||||
->where('contract_id', $contract->id)
|
||||
->orderBy('id')
|
||||
->get(['id','contract_id','initial_amount','balance_amount','type_id','created_at','updated_at']);
|
||||
->get(['id', 'contract_id', 'initial_amount', 'balance_amount', 'type_id', 'created_at', 'updated_at']);
|
||||
|
||||
return response()->json([
|
||||
'contract' => $contract,
|
||||
'accounts' => $accounts,
|
||||
@@ -1108,7 +1109,7 @@ public function show(ClientCase $clientCase)
|
||||
logger()->info('Show contracts balances', [
|
||||
'case_id' => $case->id,
|
||||
'contract_count' => $contracts->count(),
|
||||
'contracts' => $contracts->map(fn($c) => [
|
||||
'contracts' => $contracts->map(fn ($c) => [
|
||||
'id' => $c->id,
|
||||
'uuid' => $c->uuid,
|
||||
'reference' => $c->reference,
|
||||
@@ -1330,8 +1331,6 @@ public function archiveContract(ClientCase $clientCase, string $uuid, Request $r
|
||||
$hasReactivateRule = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
$executor = app(\App\Services\Archiving\ArchiveExecutor::class);
|
||||
$context = [
|
||||
'contract_id' => $contract->id,
|
||||
@@ -1475,4 +1474,70 @@ public function archiveContract(ClientCase $clientCase, string $uuid, Request $r
|
||||
|
||||
return back()->with('success', $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emergency: recreate a missing / soft-deleted person for a client case and re-link related data.
|
||||
*/
|
||||
public function emergencyCreatePerson(ClientCase $clientCase, Request $request)
|
||||
{
|
||||
$oldPersonId = $clientCase->person_id;
|
||||
/** @var \App\Models\Person\Person|null $existing */
|
||||
$existing = \App\Models\Person\Person::withTrashed()->find($oldPersonId);
|
||||
if ($existing && ! $existing->trashed()) {
|
||||
return back()->with('flash', [
|
||||
'type' => 'info',
|
||||
'message' => 'Person already exists – emergency creation not needed.',
|
||||
]);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'full_name' => ['nullable', 'string', 'max:255'],
|
||||
'first_name' => ['nullable', 'string', 'max:255'],
|
||||
'last_name' => ['nullable', 'string', 'max:255'],
|
||||
'tax_number' => ['nullable', 'string', 'max:99'],
|
||||
'social_security_number' => ['nullable', 'string', 'max:99'],
|
||||
'description' => ['nullable', 'string', 'max:500'],
|
||||
]);
|
||||
|
||||
$fullName = $data['full_name'] ?? trim(($data['first_name'] ?? '').' '.($data['last_name'] ?? ''));
|
||||
if ($fullName === '') {
|
||||
$fullName = 'Unknown Person';
|
||||
}
|
||||
|
||||
$newPerson = null;
|
||||
|
||||
\DB::transaction(function () use ($oldPersonId, $clientCase, $fullName, $data, &$newPerson) {
|
||||
$newPerson = \App\Models\Person\Person::create([
|
||||
'nu' => null,
|
||||
'first_name' => $data['first_name'] ?? null,
|
||||
'last_name' => $data['last_name'] ?? null,
|
||||
'full_name' => $fullName,
|
||||
'gender' => null,
|
||||
'birthday' => null,
|
||||
'tax_number' => $data['tax_number'] ?? null,
|
||||
'social_security_number' => $data['social_security_number'] ?? null,
|
||||
'description' => $data['description'] ?? 'Emergency recreated person (case)',
|
||||
'group_id' => 2,
|
||||
'type_id' => 1,
|
||||
]);
|
||||
|
||||
// Re-point related data referencing old person
|
||||
$tables = [
|
||||
'emails', 'person_phones', 'person_addresses', 'bank_accounts',
|
||||
];
|
||||
foreach ($tables as $table) {
|
||||
\DB::table($table)->where('person_id', $oldPersonId)->update(['person_id' => $newPerson->id]);
|
||||
}
|
||||
|
||||
// Update the client case
|
||||
$clientCase->person_id = $newPerson->id;
|
||||
$clientCase->save();
|
||||
});
|
||||
|
||||
return back()->with('flash', [
|
||||
'type' => 'success',
|
||||
'message' => 'New person created and case re-linked.',
|
||||
'person_uuid' => $newPerson?->uuid,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,4 +158,75 @@ public function update(Client $client, Request $request)
|
||||
|
||||
return to_route('client.show', $client);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emergency endpoint: if the linked person record is missing (hard deleted) or soft deleted,
|
||||
* create a new minimal Person and re-point all related child records (emails, phones, addresses, bank accounts,
|
||||
* client cases) from the old person_id to the new one, then update the client itself.
|
||||
*/
|
||||
public function emergencyCreatePerson(Client $client, Request $request)
|
||||
{
|
||||
$oldPersonId = $client->person_id;
|
||||
|
||||
// If person exists and is not trashed, abort – nothing to do
|
||||
/** @var \App\Models\Person\Person|null $existing */
|
||||
$existing = \App\Models\Person\Person::withTrashed()->find($oldPersonId);
|
||||
if ($existing && ! $existing->trashed()) {
|
||||
return redirect()->back()->with('flash', [
|
||||
'type' => 'info',
|
||||
'message' => 'Person already exists – emergency creation not needed.',
|
||||
]);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'full_name' => ['nullable', 'string', 'max:255'],
|
||||
'first_name' => ['nullable', 'string', 'max:255'],
|
||||
'last_name' => ['nullable', 'string', 'max:255'],
|
||||
'tax_number' => ['nullable', 'string', 'max:99'],
|
||||
'social_security_number' => ['nullable', 'string', 'max:99'],
|
||||
'description' => ['nullable', 'string', 'max:500'],
|
||||
]);
|
||||
|
||||
// Provide sensible fallbacks.
|
||||
$fullName = $data['full_name'] ?? trim(($data['first_name'] ?? '').' '.($data['last_name'] ?? ''));
|
||||
if ($fullName === '') {
|
||||
$fullName = 'Unknown Person';
|
||||
}
|
||||
|
||||
$newPerson = null;
|
||||
|
||||
\DB::transaction(function () use ($oldPersonId, $client, $fullName, $data, &$newPerson) {
|
||||
$newPerson = \App\Models\Person\Person::create([
|
||||
'nu' => null, // boot event will generate
|
||||
'first_name' => $data['first_name'] ?? null,
|
||||
'last_name' => $data['last_name'] ?? null,
|
||||
'full_name' => $fullName,
|
||||
'gender' => null,
|
||||
'birthday' => null,
|
||||
'tax_number' => $data['tax_number'] ?? null,
|
||||
'social_security_number' => $data['social_security_number'] ?? null,
|
||||
'description' => $data['description'] ?? 'Emergency recreated person',
|
||||
'group_id' => 1,
|
||||
'type_id' => 2,
|
||||
]);
|
||||
|
||||
// Re-point related records referencing the old (missing) person id
|
||||
$tables = [
|
||||
'emails', 'person_phones', 'person_addresses', 'bank_accounts', 'client_cases',
|
||||
];
|
||||
foreach ($tables as $table) {
|
||||
\DB::table($table)->where('person_id', $oldPersonId)->update(['person_id' => $newPerson->id]);
|
||||
}
|
||||
|
||||
// Finally update the client itself (only this one; avoid touching other potential clients)
|
||||
$client->person_id = $newPerson->id;
|
||||
$client->save();
|
||||
});
|
||||
|
||||
return redirect()->back()->with('flash', [
|
||||
'type' => 'success',
|
||||
'message' => 'New person created and related records re-linked.',
|
||||
'person_uuid' => $newPerson?->uuid,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user