|
|
|
@@ -20,7 +20,8 @@ class ClientCaseContoller extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
protected ReferenceDataCache $referenceCache,
|
|
|
|
|
protected DocumentStreamService $documentStream
|
|
|
|
|
protected DocumentStreamService $documentStream,
|
|
|
|
|
protected \App\Services\ClientCaseDataService $caseDataService
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -29,7 +30,7 @@ public function __construct(
|
|
|
|
|
public function index(ClientCase $clientCase, Request $request)
|
|
|
|
|
{
|
|
|
|
|
$search = $request->input('search');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$query = $clientCase::query()
|
|
|
|
|
->select('client_cases.*')
|
|
|
|
|
->when($search, function ($que) use ($search) {
|
|
|
|
@@ -60,7 +61,7 @@ public function index(ClientCase $clientCase, Request $request)
|
|
|
|
|
|
|
|
|
|
return Inertia::render('Cases/Index', [
|
|
|
|
|
'client_cases' => $query
|
|
|
|
|
->paginate($request->integer('perPage', 15), ['*'], 'client-cases-page')
|
|
|
|
|
->paginate($request->integer('perPage', 15), ['*'], 'clientCasesPage')
|
|
|
|
|
->withQueryString(),
|
|
|
|
|
'filters' => $request->only(['search']),
|
|
|
|
|
]);
|
|
|
|
@@ -348,7 +349,7 @@ public function deleteActivity(ClientCase $clientCase, \App\Models\Activity $act
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return back()->with('success', 'Activity deleted.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function deleteContract(ClientCase $clientCase, string $uuid, Request $request)
|
|
|
|
|
{
|
|
|
|
@@ -680,137 +681,36 @@ public function show(ClientCase $clientCase)
|
|
|
|
|
'phone_types' => $this->referenceCache->getPhoneTypes(),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// $active = false;
|
|
|
|
|
|
|
|
|
|
// Optional segment filter from query string
|
|
|
|
|
$segmentId = request()->integer('segment');
|
|
|
|
|
|
|
|
|
|
// Determine latest archive (non-reactivate) setting for this context to infer archive segment and related tables
|
|
|
|
|
$latestArchiveSetting = \App\Models\ArchiveSetting::query()
|
|
|
|
|
->where('enabled', true)
|
|
|
|
|
->where(function ($q) {
|
|
|
|
|
$q->whereNull('reactivate')->orWhere('reactivate', false);
|
|
|
|
|
})
|
|
|
|
|
->orderByDesc('id')
|
|
|
|
|
->first();
|
|
|
|
|
$archiveSegmentId = optional($latestArchiveSetting)->segment_id; // may be null
|
|
|
|
|
$relatedArchiveTables = [];
|
|
|
|
|
if ($latestArchiveSetting) {
|
|
|
|
|
$entities = (array) $latestArchiveSetting->entities;
|
|
|
|
|
foreach ($entities as $edef) {
|
|
|
|
|
if (isset($edef['related']) && is_array($edef['related'])) {
|
|
|
|
|
foreach ($edef['related'] as $rel) {
|
|
|
|
|
$relatedArchiveTables[] = $rel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$relatedArchiveTables = array_values(array_unique($relatedArchiveTables));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare contracts and a reference map.
|
|
|
|
|
// Only apply active/inactive filtering IF a segment filter is provided.
|
|
|
|
|
$contractsQuery = $case->contracts()
|
|
|
|
|
// Only select lean columns to avoid oversize JSON / headers (include description for UI display)
|
|
|
|
|
->select(['id', 'uuid', 'reference', 'start_date', 'end_date', 'description', 'meta', 'active', 'type_id', 'client_case_id', 'created_at'])
|
|
|
|
|
->with([
|
|
|
|
|
'type:id,name',
|
|
|
|
|
// Use closure for account to avoid ambiguous column names with latestOfMany join
|
|
|
|
|
'account' => function ($q) {
|
|
|
|
|
$q->select([
|
|
|
|
|
'accounts.id',
|
|
|
|
|
'accounts.contract_id',
|
|
|
|
|
'accounts.type_id',
|
|
|
|
|
'accounts.initial_amount',
|
|
|
|
|
'accounts.balance_amount',
|
|
|
|
|
'accounts.promise_date',
|
|
|
|
|
'accounts.created_at',
|
|
|
|
|
'accounts.updated_at', // include updated_at so FE can detect changes & for debugging
|
|
|
|
|
])->orderByDesc('accounts.id');
|
|
|
|
|
},
|
|
|
|
|
'segments:id,name',
|
|
|
|
|
// Eager load objects so newly created objects appear without full reload logic issues
|
|
|
|
|
'objects:id,contract_id,reference,name,description,type,created_at',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$contractsQuery->orderByDesc('created_at');
|
|
|
|
|
|
|
|
|
|
if (! empty($segmentId)) {
|
|
|
|
|
// Filter to contracts that are in the provided segment and active on pivot
|
|
|
|
|
$contractsQuery->whereExists(function ($q) use ($segmentId) {
|
|
|
|
|
$q->from('contract_segment')
|
|
|
|
|
->whereColumn('contract_segment.contract_id', 'contracts.id')
|
|
|
|
|
->where('contract_segment.segment_id', $segmentId)
|
|
|
|
|
->where('contract_segment.active', true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use pagination for contracts to avoid loading too many at once
|
|
|
|
|
// Default to 50 per page, but allow frontend to request more
|
|
|
|
|
$perPage = request()->integer('contracts_per_page', 50);
|
|
|
|
|
$contracts = $contractsQuery->paginate($perPage, ['*'], 'contracts_page')->withQueryString();
|
|
|
|
|
|
|
|
|
|
// Prepare contract reference map from paginated contracts
|
|
|
|
|
$contractItems = $contracts instanceof \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
|
|
|
|
? $contracts->items()
|
|
|
|
|
: $contracts->all();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$contractRefMap = [];
|
|
|
|
|
$contractUuidMap = [];
|
|
|
|
|
foreach ($contractItems as $c) {
|
|
|
|
|
$contractRefMap[$c->id] = $c->reference;
|
|
|
|
|
$contractUuidMap[$c->id] = $c->uuid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$contractIds = collect($contractItems)->pluck('id');
|
|
|
|
|
|
|
|
|
|
// Resolve current segment for display when filtered
|
|
|
|
|
$currentSegment = null;
|
|
|
|
|
if (! empty($segmentId)) {
|
|
|
|
|
$currentSegment = \App\Models\Segment::query()->select('id', 'name')->find($segmentId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load initial batch of documents (limit to reduce payload size)
|
|
|
|
|
$contractDocs = collect();
|
|
|
|
|
if ($contractIds->isNotEmpty()) {
|
|
|
|
|
$contractDocs = Document::query()
|
|
|
|
|
->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at', 'is_public'])
|
|
|
|
|
->where('documentable_type', Contract::class)
|
|
|
|
|
->whereIn('documentable_id', $contractIds->all())
|
|
|
|
|
->orderByDesc('created_at')
|
|
|
|
|
->limit(50) // Initial batch - frontend can request more via separate endpoint if needed
|
|
|
|
|
->get()
|
|
|
|
|
->map(function ($d) use ($contractRefMap, $contractUuidMap) {
|
|
|
|
|
$arr = $d->toArray();
|
|
|
|
|
$arr['contract_reference'] = $contractRefMap[$d->documentable_id] ?? null;
|
|
|
|
|
$arr['contract_uuid'] = $contractUuidMap[$d->documentable_id] ?? null;
|
|
|
|
|
return $arr;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// Get contracts using service
|
|
|
|
|
$contractsPerPage = request()->integer('contracts_per_page', 10);
|
|
|
|
|
$contracts = $this->caseDataService->getContracts($case, $segmentId, $contractsPerPage);
|
|
|
|
|
$contractIds = collect($contracts->items())->pluck('id')->all();
|
|
|
|
|
|
|
|
|
|
$caseDocs = $case->documents()
|
|
|
|
|
->select(['id', 'uuid', 'documentable_id', 'documentable_type', 'name', 'file_name', 'original_name', 'extension', 'mime_type', 'size', 'created_at', 'is_public'])
|
|
|
|
|
->orderByDesc('created_at')
|
|
|
|
|
->limit(50) // Initial batch
|
|
|
|
|
->get()
|
|
|
|
|
->map(function ($d) use ($case) {
|
|
|
|
|
$arr = $d->toArray();
|
|
|
|
|
$arr['client_case_uuid'] = $case->uuid;
|
|
|
|
|
return $arr;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$mergedDocs = $caseDocs
|
|
|
|
|
->concat($contractDocs)
|
|
|
|
|
->sortByDesc('created_at')
|
|
|
|
|
->values();
|
|
|
|
|
// Get activities using service
|
|
|
|
|
$activitiesPerPage = request()->integer('activities_per_page', 15);
|
|
|
|
|
$encodedFilters = request()->input('filter_activities');
|
|
|
|
|
$activities = $this->caseDataService->getActivities($case, $segmentId, $encodedFilters, $contractIds, $activitiesPerPage);
|
|
|
|
|
|
|
|
|
|
// Get documents using service
|
|
|
|
|
$contractsPerPage = request()->integer('documentsPerPage', 15);
|
|
|
|
|
$documents = $this->caseDataService->getDocuments($case, $contractIds, $contractsPerPage);
|
|
|
|
|
|
|
|
|
|
// Get archive metadata using service
|
|
|
|
|
|
|
|
|
|
$archiveMeta = $this->caseDataService->getArchiveMeta();
|
|
|
|
|
|
|
|
|
|
return Inertia::render('Cases/Show', [
|
|
|
|
|
'client' => $case->client()->with('person', fn ($q) => $q->with(['addresses', 'phones', 'emails', 'bankAccounts', 'client']))->firstOrFail(),
|
|
|
|
|
'client_case' => $case,
|
|
|
|
|
'contracts' => $contracts, // Now paginated
|
|
|
|
|
'documents' => $mergedDocs,
|
|
|
|
|
'contracts' => $contracts,
|
|
|
|
|
'documents' => $documents,
|
|
|
|
|
])->with([
|
|
|
|
|
// Active document templates for contracts (latest version per slug)
|
|
|
|
|
'contract_doc_templates' => \App\Models\DocumentTemplate::query()
|
|
|
|
|
->where('active', true)
|
|
|
|
|
->where('core_entity', 'contract')
|
|
|
|
@@ -819,38 +719,10 @@ public function show(ClientCase $clientCase)
|
|
|
|
|
->groupBy('slug')
|
|
|
|
|
->map(fn ($g) => $g->sortByDesc('version')->first())
|
|
|
|
|
->values(),
|
|
|
|
|
'archive_meta' => [
|
|
|
|
|
'archive_segment_id' => $archiveSegmentId,
|
|
|
|
|
'related_tables' => $relatedArchiveTables,
|
|
|
|
|
],
|
|
|
|
|
'activities' => tap(
|
|
|
|
|
(function () use ($case, $segmentId, $contractIds) {
|
|
|
|
|
$q = $case->activities()
|
|
|
|
|
->with(['action', 'decision', 'contract:id,uuid,reference', 'user:id,name'])
|
|
|
|
|
->orderByDesc('created_at');
|
|
|
|
|
if (! empty($segmentId)) {
|
|
|
|
|
// Only activities for filtered contracts or unlinked (contract_id null)
|
|
|
|
|
$q->where(function ($qq) use ($contractIds) {
|
|
|
|
|
$qq->whereNull('contract_id');
|
|
|
|
|
if ($contractIds->isNotEmpty()) {
|
|
|
|
|
$qq->orWhereIn('contract_id', $contractIds);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $q->paginate(20, ['*'], 'activities')->withQueryString();
|
|
|
|
|
})(),
|
|
|
|
|
function ($p) {
|
|
|
|
|
$p->getCollection()->transform(function ($a) {
|
|
|
|
|
$a->setAttribute('user_name', optional($a->user)->name);
|
|
|
|
|
|
|
|
|
|
return $a;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
'archive_meta' => $archiveMeta,
|
|
|
|
|
'activities' => $activities,
|
|
|
|
|
'contract_types' => $this->referenceCache->getContractTypes(),
|
|
|
|
|
'account_types' => $this->referenceCache->getAccountTypes(),
|
|
|
|
|
// Include decisions with auto-mail metadata and the linked email template entity_types for UI logic
|
|
|
|
|
'actions' => \App\Models\Action::query()
|
|
|
|
|
->with([
|
|
|
|
|
'decisions' => function ($q) {
|
|
|
|
@@ -865,7 +737,6 @@ function ($p) {
|
|
|
|
|
'segments' => $case->segments()->wherePivot('active', true)->get(['segments.id', 'segments.name']),
|
|
|
|
|
'all_segments' => \App\Models\Segment::query()->where('active', true)->get(['id', 'name']),
|
|
|
|
|
'current_segment' => $currentSegment,
|
|
|
|
|
// SMS helpers for per-case sending UI
|
|
|
|
|
'sms_profiles' => \App\Models\SmsProfile::query()
|
|
|
|
|
->select(['id', 'name', 'default_sender_id'])
|
|
|
|
|
->where('active', true)
|
|
|
|
@@ -950,7 +821,7 @@ public function deleteContractDocument(Contract $contract, Document $document, R
|
|
|
|
|
|
|
|
|
|
$document->delete();
|
|
|
|
|
|
|
|
|
|
return back()->with('success', 'Document deleted.')->with('flash_method', 'DELETE');
|
|
|
|
|
return back()->with('success', 'Document deleted.')->with('flash_method', 'DELETE');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|