29 Commits

Author SHA1 Message Date
Simon Pocrnjič 229c100cc4 again added fix 2026-01-27 18:10:12 +01:00
Simon Pocrnjič 9a4897bf0c fixed normalizing decimal upsertAccount importer 2026-01-27 18:04:50 +01:00
Simon Pocrnjič b2a9350d0f Fixed import check for existing address 2026-01-21 18:31:54 +01:00
Simon Pocrnjič 27bdb942ab Changed Import processor removed getting existing account by reference and just keep contract_id and active true 2026-01-17 17:33:19 +01:00
Simon Pocrnjič 7eaab16e30 added new permission mass-archive instead if limiting mass archiving to admin users 2026-01-15 21:35:53 +01:00
Simon Pocrnjič 6a2dd860fa Mass archiving added to segment view show 2026-01-15 21:16:26 +01:00
Simon Pocrnjič ca8754cd94 birthday normalise date 2026-01-14 22:09:04 +01:00
Simon Pocrnjič 8fdc0d6359 Changes to address added fulltext (address,post_code,city), added imployer column to person fix / updated PersonInfoGrid vue component 2026-01-14 21:38:34 +01:00
Simon Pocrnjič 7fc4520dbf Added address to client contracts table 2026-01-12 19:57:04 +01:00
Simon Pocrnjič dc41862afc Client contracts view added excel export option 2026-01-10 20:36:32 +01:00
Simon Pocrnjič fb6474ab88 changes to old import check if for account if balance_amount and initial_amount are empty or null by default value is set to 0 2026-01-06 19:39:18 +01:00
Simon Pocrnjič 2ad24216ae Added Client case person address to segment tables and exports 2026-01-05 19:41:39 +01:00
Simon Pocrnjič 8031501d25 Changes to import where on reactivation if start_date not set import mapping it should fill field with current date, same for end_date but it sets it to null 2026-01-02 14:49:05 +01:00
Simon Pocrnjič adc2a64687 Fixed some field job problem where field operator could still see archived contracts 2025-12-21 21:00:49 +01:00
Simon Pocrnjič 11206fb4f7 UTF8 fixed 2025-12-18 20:48:11 +01:00
Simon Pocrnjič 39a597f6eb added #N/A check 2025-12-18 20:22:14 +01:00
Simon Pocrnjič 5d4498ac5a fixed address import 2025-12-18 19:40:27 +01:00
Simon Pocrnjič 622f53e401 removed something 2025-12-18 18:25:15 +01:00
Simon Pocrnjič 96473fd60b dwdwf 2025-12-17 22:21:36 +01:00
Simon Pocrnjič 5ddca35389 test 2025-12-17 22:17:00 +01:00
Simon Pocrnjič 94ad0c0772 test 2025-12-17 22:14:43 +01:00
Simon Pocrnjič 2140181a76 sdwsd 2025-12-17 21:50:48 +01:00
Simon Pocrnjič 06fa443b3e trimming additional spaces example TEST 2 now to TEST 2 2025-12-17 21:22:17 +01:00
Simon Pocrnjič 6c45063e47 fixed naslove 2025-12-17 21:12:53 +01:00
Simon Pocrnjič b8c9b51f29 test 2025-12-17 20:51:31 +01:00
Simon Pocrnjič a4db37adfa Fix error reporting 2025-12-17 20:49:11 +01:00
Simon Pocrnjič 76f76f73b4 error handling importer 2025-12-17 20:45:02 +01:00
Simon Pocrnjič d69f4dd6f6 On template creation for entity activities action and decision or not required anymore 2025-12-16 20:18:23 +01:00
Simon Pocrnjič a596177a68 changes to import added activity entity 2025-12-16 19:35:51 +01:00
32 changed files with 3130 additions and 467 deletions
+160
View File
@@ -0,0 +1,160 @@
<?php
namespace App\Exports;
use App\Models\Contract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class ClientContractsExport extends DefaultValueBinder implements FromQuery, ShouldAutoSize, WithColumnFormatting, WithCustomValueBinder, WithHeadings, WithMapping
{
public const DATE_EXCEL_FORMAT = 'dd"."mm"."yyyy';
public const TEXT_EXCEL_FORMAT = NumberFormat::FORMAT_TEXT;
/**
* @var array<string, string>
*/
private array $columnLetterMap = [];
/**
* @var array<string, array{label: string}>
*/
public const COLUMN_METADATA = [
'reference' => ['label' => 'Referenca'],
'customer' => ['label' => 'Stranka'],
'address' => ['label' => 'Naslov'],
'start' => ['label' => 'Začetek'],
'segment' => ['label' => 'Segment'],
'balance' => ['label' => 'Stanje'],
];
/**
* @param array<int, string> $columns
*/
public function __construct(private Builder $query, private array $columns) {}
/**
* @return array<int, string>
*/
public static function allowedColumns(): array
{
return array_keys(self::COLUMN_METADATA);
}
public static function columnLabel(string $column): string
{
return self::COLUMN_METADATA[$column]['label'] ?? $column;
}
public function query(): Builder
{
return $this->query;
}
/**
* @return array<int, mixed>
*/
public function map($row): array
{
return array_map(fn (string $column) => $this->resolveValue($row, $column), $this->columns);
}
/**
* @return array<int, string>
*/
public function headings(): array
{
return array_map(fn (string $column) => self::columnLabel($column), $this->columns);
}
/**
* @return array<string, string>
*/
public function columnFormats(): array
{
$formats = [];
foreach ($this->getColumnLetterMap() as $letter => $column) {
if ($column === 'reference') {
$formats[$letter] = self::TEXT_EXCEL_FORMAT;
continue;
}
if ($column === 'start') {
$formats[$letter] = self::DATE_EXCEL_FORMAT;
}
}
return $formats;
}
private function resolveValue(Contract $contract, string $column): mixed
{
return match ($column) {
'reference' => $contract->reference,
'customer' => optional($contract->clientCase?->person)->full_name,
'address' => optional($contract->clientCase?->person?->address)->address,
'start' => $this->formatDate($contract->start_date),
'segment' => $contract->segments?->first()?->name,
'balance' => optional($contract->account)->balance_amount,
default => null,
};
}
private function formatDate(?string $date): mixed
{
if (empty($date)) {
return null;
}
try {
$carbon = Carbon::parse($date);
return ExcelDate::dateTimeToExcel($carbon);
} catch (\Exception $e) {
return null;
}
}
/**
* @return array<string, string>
*/
private function getColumnLetterMap(): array
{
if ($this->columnLetterMap !== []) {
return $this->columnLetterMap;
}
$letter = 'A';
foreach ($this->columns as $column) {
$this->columnLetterMap[$letter] = $column;
$letter++;
}
return $this->columnLetterMap;
}
public function bindValue(Cell $cell, $value): bool
{
if (is_numeric($value)) {
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
return true;
}
return parent::bindValue($cell, $value);
}
}
+2
View File
@@ -34,6 +34,7 @@ class SegmentContractsExport extends DefaultValueBinder implements FromQuery, Sh
public const COLUMN_METADATA = [
'reference' => ['label' => 'Pogodba'],
'client_case' => ['label' => 'Primer'],
'address' => ['label' => 'Naslov'],
'client' => ['label' => 'Stranka'],
'type' => ['label' => 'Vrsta'],
'start_date' => ['label' => 'Začetek'],
@@ -107,6 +108,7 @@ private function resolveValue(Contract $contract, string $column): mixed
return match ($column) {
'reference' => $contract->reference,
'client_case' => optional($contract->clientCase?->person)->full_name,
'address' => optional($contract->clientCase?->person?->address)->address,
'client' => optional($contract->clientCase?->client?->person)->full_name,
'type' => optional($contract->type)->name,
'start_date' => $this->formatDate($contract->start_date),
+245 -142
View File
@@ -252,11 +252,14 @@ public function storeActivity(ClientCase $clientCase, Request $request)
'action_id' => 'exists:\App\Models\Action,id',
'decision_id' => 'exists:\App\Models\Decision,id',
'contract_uuid' => 'nullable|uuid',
'phone_view' => 'nullable|boolean',
'send_auto_mail' => 'sometimes|boolean',
'attachment_document_ids' => 'sometimes|array',
'attachment_document_ids.*' => 'integer',
]);
$isPhoneView = $attributes['phone_view'] ?? false;
// Map contract_uuid to contract_id within the same client case, if provided
$contractId = null;
if (! empty($attributes['contract_uuid'])) {
@@ -279,10 +282,23 @@ public function storeActivity(ClientCase $clientCase, Request $request)
'decision_id' => $attributes['decision_id'],
'contract_id' => $contractId,
]);
/*foreach ($activity->decision->events as $e) {
$class = '\\App\\Events\\' . $e->name;
event(new $class($clientCase));
}*/
if ($isPhoneView && $contractId) {
$fieldJob = $contract->fieldJobs()
->whereNull('completed_at')
->whereNull('cancelled_at')
->where('assigned_user_id', \Auth::id())
->orderByDesc('id')
->first();
if ($fieldJob) {
$fieldJob->update([
'added_activity' => true,
'last_activity' => $row->created_at,
]);
}
}
logger()->info('Activity successfully inserted', $attributes);
@@ -297,8 +313,8 @@ public function storeActivity(ClientCase $clientCase, Request $request)
->values();
$validAttachmentIds = collect();
if ($attachmentIds->isNotEmpty() && $contractId) {
$validAttachmentIds = \App\Models\Document::query()
->where('documentable_type', \App\Models\Contract::class)
$validAttachmentIds = Document::query()
->where('documentable_type', Contract::class)
->where('documentable_id', $contractId)
->whereIn('id', $attachmentIds)
->pluck('id');
@@ -1458,178 +1474,265 @@ public function archiveContract(ClientCase $clientCase, string $uuid, Request $r
{
$contract = Contract::query()->where('uuid', $uuid)->firstOrFail();
if ($contract->client_case_id !== $clientCase->id) {
\Log::warning('Contract not found uuid: {uuid}', ['uuid' => $uuid]);
abort(404);
}
$reactivateRequested = (bool) $request->boolean('reactivate');
// Determine applicable settings based on intent (archive vs reactivate)
if ($reactivateRequested) {
$latestReactivate = \App\Models\ArchiveSetting::query()
$attr = $request->validate([
'reactivate' => 'boolean',
]);
$reactivate = $attr['reactivate'] ?? false;
$setting = \App\Models\ArchiveSetting::query()
->where('enabled', true)
->where('reactivate', true)
->whereIn('strategy', ['immediate', 'manual'])
->where('reactivate', $reactivate)
->orderByDesc('id')
->first();
if (! $latestReactivate) {
return back()->with('warning', __('contracts.reactivate_not_allowed'));
}
$settings = collect([$latestReactivate]);
$hasReactivateRule = true;
} else {
$settings = \App\Models\ArchiveSetting::query()
->where('enabled', true)
->whereIn('strategy', ['immediate', 'manual'])
->where(function ($q) { // exclude reactivate-only rules from archive run
$q->whereNull('reactivate')->orWhere('reactivate', false);
})
->get();
if ($settings->isEmpty()) {
return back()->with('warning', __('contracts.no_archive_settings'));
}
$hasReactivateRule = false;
if (! $setting->exists()) {
\Log::warning('No archive settings found!');
return back()->with('warning', 'No settings found');
}
// Service archive executor
$executor = app(\App\Services\Archiving\ArchiveExecutor::class);
$result = null;
$context = [
'contract_id' => $contract->id,
'client_case_id' => $clientCase->id,
'account_id' => $contract->account->id ?? null,
];
if ($contract->account) {
$context['account_id'] = $contract->account->id;
}
$overall = [];
$hadAnyEffect = false;
foreach ($settings as $setting) {
$res = $executor->executeSetting($setting, $context, optional($request->user())->id);
foreach ($res as $table => $count) {
$overall[$table] = ($overall[$table] ?? 0) + $count;
if ($count > 0) {
$hadAnyEffect = true;
}
}
}
if ($reactivateRequested && $hasReactivateRule) {
// Reactivation path: ensure contract becomes active and soft-delete cleared.
if ($contract->active == 0 || $contract->deleted_at) {
$contract->forceFill(['active' => 1, 'deleted_at' => null])->save();
$overall['contracts_reactivated'] = ($overall['contracts_reactivated'] ?? 0) + 1;
$hadAnyEffect = true;
}
} else {
// Ensure the contract itself is archived even if rule conditions would have excluded it
if (! empty($contract->getAttributes()) && $contract->active) {
if (! array_key_exists('contracts', $overall)) {
$contract->update(['active' => 0]);
$overall['contracts'] = ($overall['contracts'] ?? 0) + 1;
} else {
$contract->refresh();
}
$hadAnyEffect = true;
}
}
// Create an Activity record logging this archive if an action or decision is tied to any setting
if ($hadAnyEffect) {
$activitySetting = $settings->first(fn ($s) => ! is_null($s->action_id) || ! is_null($s->decision_id));
if ($activitySetting) {
try {
if ($reactivateRequested) {
$note = 'Ponovna aktivacija pogodba '.$contract->reference;
} else {
$noteKey = 'contracts.archived_activity_note';
$note = __($noteKey, ['reference' => $contract->reference]);
if ($note === $noteKey) {
$note = \Illuminate\Support\Facades\Lang::get($noteKey, ['reference' => $contract->reference], 'sl');
}
$result = $executor->executeSetting($setting, $context, \Auth::id());
} catch (Exception $e) {
\Log::error('There was an error executing ArchiveExecutor::executeSetting {msg}', ['msg' => $e->getMessage()]);
return back()->with('warning', 'Something went wrong!');
}
try {
\DB::transaction(function () use ($contract, $clientCase, $setting, $reactivate) {
// Create an Activity record logging this archive if an action or decision is tied to any setting
if ($setting->action_id && $setting->decision_id) {
$activityData = [
'client_case_id' => $clientCase->id,
'action_id' => $activitySetting->action_id,
'decision_id' => $activitySetting->decision_id,
'note' => $note,
'active' => 1,
'user_id' => optional($request->user())->id,
'action_id' => $setting->action_id,
'decision_id' => $setting->decision_id,
'note' => ($reactivate)
? "Ponovno aktivirana pogodba $contract->reference"
: "Arhivirana pogodba $contract->reference",
];
if ($reactivateRequested) {
// Attach the contract_id when reactivated as per requirement
$activityData['contract_id'] = $contract->id;
}
\App\Models\Activity::create($activityData);
} catch (\Throwable $e) {
logger()->warning('Failed to create archive/reactivate activity', [
'error' => $e->getMessage(),
'contract_id' => $contract->id,
'setting_id' => optional($activitySetting)->id,
'reactivate' => $reactivateRequested,
]);
}
}
}
// If any archive setting specifies a segment_id, move the contract to that segment (archive bucket)
$segmentSetting = $settings->first(fn ($s) => ! is_null($s->segment_id)); // for reactivation this is the single reactivation setting if segment specified
if ($segmentSetting && $segmentSetting->segment_id) {
try {
$segmentId = $segmentSetting->segment_id;
\DB::transaction(function () use ($contract, $segmentId, $clientCase) {
// Ensure the segment is attached to the client case (activate if previously inactive)
$casePivot = \DB::table('client_case_segment')
->where('client_case_id', $clientCase->id)
->where('segment_id', $segmentId)
->first();
if (! $casePivot) {
\DB::table('client_case_segment')->insert([
'client_case_id' => $clientCase->id,
'segment_id' => $segmentId,
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
} elseif (! $casePivot->active) {
\DB::table('client_case_segment')
->where('id', $casePivot->id)
->update(['active' => true, 'updated_at' => now()]);
\App\Models\Activity::create($activityData);
} catch (Exception $e) {
\Log::warning('Activity could not be created!');
}
// Deactivate all current active contract segments
\DB::table('contract_segment')
->where('contract_id', $contract->id)
->where('active', true)
->update(['active' => false, 'updated_at' => now()]);
}
// Attach or activate the archive segment for this contract
$existing = \DB::table('contract_segment')
->where('contract_id', $contract->id)
->where('segment_id', $segmentId)
->first();
if ($existing) {
\DB::table('contract_segment')
->where('id', $existing->id)
->update(['active' => true, 'updated_at' => now()]);
// If any archive setting specifies a segment_id, move the contract to that segment (archive bucket)
if ($setting->segment_id) {
$segmentId = $setting->segment_id;
$contract->segments()
->allRelatedIds()
->map(fn (int $val, int|string $key) => $contract->segments()->updateExistingPivot($val, [
'active' => false,
'updated_at' => now(),
])
);
if ($contract->attachedSegments()->find($segmentId)->pluck('id')->isNotEmpty()) {
$contract->attachedSegments()->updateExistingPivot($segmentId, [
'active' => true,
'updated_at' => now(),
]);
} else {
\DB::table('contract_segment')->insert([
$contract->segments()->attach(
$segmentId,
[
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]
);
}
}
$contract->fieldJobs()
->whereNull('completed_at')
->whereNull('cancelled_at')
->update([
'cancelled_at' => date('Y-m-d'),
'updated_at' => now(),
]);
});
} catch (Exception $e) {
\Log::warning('Something went wrong with inserting / updating archive setting partials!');
return back()->with('warning', 'Something went wrong!');
}
return back()->with('success', $reactivate
? __('contracts.reactivated')
: __('contracts.archived')
);
}
/**
* Archive multiple contracts in a batch operation
*/
public function archiveBatch(Request $request)
{
$validated = $request->validate([
'contracts' => 'required|array',
'contracts.*' => 'required|uuid|exists:contracts,uuid',
'reactivate' => 'boolean',
]);
$reactivate = $validated['reactivate'] ?? false;
// Get archive setting
$setting = \App\Models\ArchiveSetting::query()
->where('enabled', true)
->whereIn('strategy', ['immediate', 'manual'])
->where('reactivate', $reactivate)
->orderByDesc('id')
->first();
if (! $setting) {
\Log::warning('No archive settings found for batch archive');
return back()->with('flash', [
'error' => 'No archive settings found',
]);
}
$executor = app(\App\Services\Archiving\ArchiveExecutor::class);
$successCount = 0;
$skippedCount = 0;
$errors = [];
foreach ($validated['contracts'] as $contractUuid) {
try {
$contract = Contract::where('uuid', $contractUuid)->firstOrFail();
// Skip if contract is already archived (active = 0)
if (!$contract->active) {
$skippedCount++;
continue;
}
$clientCase = $contract->clientCase;
$context = [
'contract_id' => $contract->id,
'segment_id' => $segmentId,
'client_case_id' => $clientCase->id,
'account_id' => $contract->account->id ?? null,
];
// Execute archive setting
$executor->executeSetting($setting, $context, \Auth::id());
// Transaction for segment updates and activity logging
\DB::transaction(function () use ($contract, $clientCase, $setting, $reactivate) {
// Create activity log
if ($setting->action_id && $setting->decision_id) {
$activityData = [
'client_case_id' => $clientCase->id,
'action_id' => $setting->action_id,
'decision_id' => $setting->decision_id,
'note' => ($reactivate)
? "Ponovno aktivirana pogodba $contract->reference"
: "Arhivirana pogodba $contract->reference",
];
try {
\App\Models\Activity::create($activityData);
} catch (Exception $e) {
\Log::warning('Activity could not be created during batch archive');
}
}
// Move to archive segment if specified
if ($setting->segment_id) {
$segmentId = $setting->segment_id;
// Deactivate all current segments
$contract->segments()
->allRelatedIds()
->map(fn (int $val) => $contract->segments()->updateExistingPivot($val, [
'active' => false,
'updated_at' => now(),
]));
// Activate archive segment
if ($contract->attachedSegments()->find($segmentId)->pluck('id')->isNotEmpty()) {
$contract->attachedSegments()->updateExistingPivot($segmentId, [
'active' => true,
'updated_at' => now(),
]);
} else {
$contract->segments()->attach($segmentId, [
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
// Cancel pending field jobs
$contract->fieldJobs()
->whereNull('completed_at')
->whereNull('cancelled_at')
->update([
'cancelled_at' => date('Y-m-d'),
'updated_at' => now(),
]);
});
} catch (\Throwable $e) {
logger()->warning('Failed to move contract to archive segment', [
$successCount++;
} catch (Exception $e) {
\Log::error('Error archiving contract in batch', [
'uuid' => $contractUuid,
'error' => $e->getMessage(),
'contract_id' => $contract->id,
'segment_id' => $segmentSetting->segment_id,
'setting_id' => $segmentSetting->id,
]);
$errors[] = [
'uuid' => $contractUuid,
'error' => $e->getMessage(),
];
}
}
if (count($errors) > 0) {
$message = "Archived $successCount contracts";
if ($skippedCount > 0) {
$message .= ", skipped $skippedCount already archived";
}
$message .= ", " . count($errors) . " failed";
return back()->with('flash', [
'error' => $message,
'details' => $errors,
]);
}
$message = $reactivate
? "Successfully reactivated $successCount contracts"
: "Successfully archived $successCount contracts";
if ($skippedCount > 0) {
$message .= " ($skippedCount already archived)";
}
$message = $reactivateRequested ? __('contracts.reactivated') : __('contracts.archived');
return back()->with('success', $message);
return back()->with('flash', [
'success' => $message,
]);
}
/**
+83
View File
@@ -2,10 +2,14 @@
namespace App\Http\Controllers;
use App\Exports\ClientContractsExport;
use App\Http\Requests\ExportClientContractsRequest;
use App\Models\Client;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Inertia\Inertia;
use Maatwebsite\Excel\Facades\Excel;
class ClientController extends Controller
{
@@ -128,6 +132,7 @@ public function contracts(Client $client, Request $request)
->with([
'clientCase:id,uuid,person_id',
'clientCase.person:id,full_name',
'clientCase.person.address',
'segments' => function ($q) {
$q->wherePivot('active', true)->select('segments.id', 'segments.name');
},
@@ -175,6 +180,84 @@ public function contracts(Client $client, Request $request)
]);
}
public function exportContracts(ExportClientContractsRequest $request, Client $client)
{
$data = $request->validated();
$columns = array_values(array_unique($data['columns']));
$from = $data['from'] ?? null;
$to = $data['to'] ?? null;
$search = $data['search'] ?? null;
$segmentsParam = $data['segments'] ?? null;
$segmentIds = $segmentsParam ? array_filter(explode(',', $segmentsParam)) : [];
$query = \App\Models\Contract::query()
->whereHas('clientCase', function ($q) use ($client) {
$q->where('client_id', $client->id);
})
->with([
'clientCase:id,uuid,person_id',
'clientCase.person:id,full_name',
'clientCase.person.address',
'segments' => function ($q) {
$q->wherePivot('active', true)->select('segments.id', 'segments.name');
},
'account:id,accounts.contract_id,balance_amount',
])
->select(['id', 'uuid', 'reference', 'start_date', 'client_case_id'])
->whereNull('deleted_at')
->when($from || $to, function ($q) use ($from, $to) {
if (! empty($from)) {
$q->whereDate('start_date', '>=', $from);
}
if (! empty($to)) {
$q->whereDate('start_date', '<=', $to);
}
})
->when($search, function ($q) use ($search) {
$q->where(function ($inner) use ($search) {
$inner->where('reference', 'ilike', '%'.$search.'%')
->orWhereHas('clientCase.person', function ($p) use ($search) {
$p->where('full_name', 'ilike', '%'.$search.'%');
});
});
})
->when($segmentIds, function ($q) use ($segmentIds) {
$q->whereHas('segments', function ($s) use ($segmentIds) {
$s->whereIn('segments.id', $segmentIds)
->where('contract_segment.active', true);
});
})
->orderByDesc('start_date');
if (($data['scope'] ?? ExportClientContractsRequest::SCOPE_ALL) === ExportClientContractsRequest::SCOPE_CURRENT) {
$page = max(1, (int) ($data['page'] ?? 1));
$perPage = max(1, min(200, (int) ($data['per_page'] ?? 15)));
$query->forPage($page, $perPage);
}
$filename = $this->buildExportFilename($client);
return Excel::download(new ClientContractsExport($query, $columns), $filename);
}
private function buildExportFilename(Client $client): string
{
$datePrefix = now()->format('dmy');
$clientName = $this->slugify($client->person?->full_name ?? 'stranka');
return sprintf('%s_%s-Pogodbe.xlsx', $datePrefix, $clientName);
}
private function slugify(?string $value): string
{
if (empty($value)) {
return 'data';
}
return Str::slug($value, '-') ?: 'data';
}
public function store(Request $request)
{
@@ -58,6 +58,13 @@ public function index()
'fields' => ['reference', 'balance_amount', 'contract_id', 'contract_reference', 'type_id', 'active', 'description'],
'ui' => ['order' => 6],
],
[
'key' => 'activities',
'canonical_root' => 'activity',
'label' => 'Activities',
'fields' => ['note', 'due_date', 'amount', 'action_id', 'decision_id', 'contract_id', 'client_case_id', 'user_id'],
'ui' => ['order' => 7],
],
]);
} else {
// Ensure fields are arrays for frontend consumption
@@ -111,10 +111,10 @@ public function store(Request $request)
'is_active' => 'boolean',
'reactivate' => 'boolean',
'entities' => 'nullable|array',
'entities.*' => 'string|in:person,person_addresses,person_phones,emails,accounts,contracts,client_cases,case_objects,payments',
'entities.*' => 'string|in:person,person_addresses,person_phones,emails,accounts,contracts,client_cases,case_objects,payments,activities',
'mappings' => 'array',
'mappings.*.source_column' => 'required|string',
'mappings.*.entity' => 'nullable|string|in:person,person_addresses,person_phones,emails,accounts,contracts,client_cases,case_objects,payments',
'mappings.*.entity' => 'nullable|string|in:person,person_addresses,person_phones,emails,accounts,contracts,client_cases,case_objects,payments,activities',
'mappings.*.target_field' => 'nullable|string',
'mappings.*.transform' => 'nullable|string|max:50',
'mappings.*.apply_mode' => 'nullable|string|in:insert,update,both,keyref',
@@ -124,7 +124,11 @@ public function store(Request $request)
'meta.segment_id' => 'nullable|integer|exists:segments,id',
'meta.decision_id' => 'nullable|integer|exists:decisions,id',
'meta.action_id' => 'nullable|integer|exists:actions,id',
'meta.activity_action_id' => 'nullable|integer|exists:actions,id',
'meta.activity_decision_id' => 'nullable|integer|exists:decisions,id',
'meta.activity_created_at' => 'nullable|date',
'meta.payments_import' => 'nullable|boolean',
'meta.history_import' => 'nullable|boolean',
'meta.contract_key_mode' => 'nullable|string|in:reference',
])->validate();
@@ -142,7 +146,28 @@ public function store(Request $request)
$template = null;
DB::transaction(function () use (&$template, $request, $data) {
$paymentsImport = (bool) (data_get($data, 'meta.payments_import') ?? false);
$historyImport = (bool) (data_get($data, 'meta.history_import') ?? false);
$entities = $data['entities'] ?? [];
if ($historyImport) {
$paymentsImport = false; // history import cannot be combined with payments mode
$allowedHistoryEntities = ['person', 'person_addresses', 'person_phones', 'contracts', 'activities', 'client_cases'];
$entities = array_values(array_intersect($entities, $allowedHistoryEntities));
// If contracts are present, ensure accounts are included implicitly for reference consistency
if (in_array('contracts', $entities, true) && ! in_array('accounts', $entities, true)) {
$entities[] = 'accounts';
}
// Reject mappings that target disallowed entities for history import
$disallowedMappings = collect($data['mappings'] ?? [])->filter(function ($m) use ($allowedHistoryEntities) {
if (empty($m['entity'])) {
return false;
}
return ! in_array($m['entity'], $allowedHistoryEntities, true);
});
if ($disallowedMappings->isNotEmpty()) {
abort(422, 'History import only allows entities: person, person_addresses, person_phones, contracts, activities, client_cases. Remove other mapping entities.');
}
}
if ($paymentsImport) {
$entities = ['contracts', 'accounts', 'payments'];
}
@@ -162,7 +187,11 @@ public function store(Request $request)
'segment_id' => data_get($data, 'meta.segment_id'),
'decision_id' => data_get($data, 'meta.decision_id'),
'action_id' => data_get($data, 'meta.action_id'),
'activity_action_id' => data_get($data, 'meta.activity_action_id'),
'activity_decision_id' => data_get($data, 'meta.activity_decision_id'),
'activity_created_at' => data_get($data, 'meta.activity_created_at'),
'payments_import' => $paymentsImport ?: null,
'history_import' => $historyImport ?: null,
'contract_key_mode' => data_get($data, 'meta.contract_key_mode'),
], fn ($v) => ! is_null($v) && $v !== ''),
]);
@@ -244,7 +273,7 @@ public function addMapping(Request $request, ImportTemplate $template)
}
$data = validator($raw, [
'source_column' => 'required|string',
'entity' => 'nullable|string|in:person,person_addresses,person_phones,emails,accounts,contracts,client_cases,case_objects,payments',
'entity' => 'nullable|string|in:person,person_addresses,person_phones,emails,accounts,contracts,client_cases,case_objects,payments,activities',
'target_field' => 'nullable|string',
'transform' => 'nullable|string|in:trim,upper,lower',
'apply_mode' => 'nullable|string|in:insert,update,both,keyref',
@@ -314,7 +343,11 @@ public function update(Request $request, ImportTemplate $template)
'meta.segment_id' => 'nullable|integer|exists:segments,id',
'meta.decision_id' => 'nullable|integer|exists:decisions,id',
'meta.action_id' => 'nullable|integer|exists:actions,id',
'meta.activity_action_id' => 'nullable|integer|exists:actions,id',
'meta.activity_decision_id' => 'nullable|integer|exists:decisions,id',
'meta.activity_created_at' => 'nullable|date',
'meta.payments_import' => 'nullable|boolean',
'meta.history_import' => 'nullable|boolean',
'meta.contract_key_mode' => 'nullable|string|in:reference',
])->validate();
@@ -342,6 +375,11 @@ public function update(Request $request, ImportTemplate $template)
unset($newMeta[$k]);
}
}
foreach (['activity_action_id', 'activity_decision_id', 'activity_created_at'] as $k) {
if (array_key_exists($k, $newMeta) && ($newMeta[$k] === '' || is_null($newMeta[$k]))) {
unset($newMeta[$k]);
}
}
}
// Finalize meta (ensure payments entities forced if enabled)
@@ -349,6 +387,20 @@ public function update(Request $request, ImportTemplate $template)
if (! empty($finalMeta['payments_import'])) {
$finalMeta['entities'] = ['contracts', 'accounts', 'payments'];
}
if (! empty($finalMeta['history_import'])) {
$finalMeta['payments_import'] = false;
$allowedHistoryEntities = ['person', 'person_addresses', 'person_phones', 'contracts', 'activities', 'client_cases'];
$finalMeta['entities'] = array_values(array_intersect($finalMeta['entities'] ?? [], $allowedHistoryEntities));
if (in_array('contracts', $finalMeta['entities'] ?? [], true) && ! in_array('accounts', $finalMeta['entities'] ?? [], true)) {
$finalMeta['entities'][] = 'accounts';
}
}
if (in_array('activities', $finalMeta['entities'] ?? [], true)) {
if (empty($finalMeta['activity_action_id']) || empty($finalMeta['activity_decision_id'])) {
return back()->withErrors(['meta.activity_action_id' => 'Activities import requires selecting both a default action and decision.'])->withInput();
}
}
$update = [
'name' => $data['name'],
@@ -381,7 +433,7 @@ public function bulkAddMappings(Request $request, ImportTemplate $template)
}
$data = validator($raw, [
'sources' => 'required|string', // comma and/or newline separated
'entity' => 'nullable|string|in:person,person_addresses,person_phones,emails,accounts,contracts,client_cases,case_objects,payments',
'entity' => 'nullable|string|in:person,person_addresses,person_phones,emails,accounts,contracts,client_cases,case_objects,payments,activities',
'default_field' => 'nullable|string', // if provided, used as the field name for all entries
'apply_mode' => 'nullable|string|in:insert,update,both,keyref',
'transform' => 'nullable|string|in:trim,upper,lower',
@@ -583,6 +635,9 @@ public function applyToImport(Request $request, ImportTemplate $template, Import
'segment_id' => $tplMeta['segment_id'] ?? null,
'decision_id' => $tplMeta['decision_id'] ?? null,
'action_id' => $tplMeta['action_id'] ?? null,
'activity_action_id' => $tplMeta['activity_action_id'] ?? null,
'activity_decision_id' => $tplMeta['activity_decision_id'] ?? null,
'activity_created_at' => $tplMeta['activity_created_at'] ?? null,
'template_name' => $template->name,
], fn ($v) => ! is_null($v) && $v !== ''));
+52 -140
View File
@@ -76,169 +76,81 @@ public function completedToday(Request $request)
public function showCase(\App\Models\ClientCase $clientCase, Request $request)
{
$userId = $request->user()->id;
$completedMode = (bool) $request->boolean('completed');
$completedMode = $request->boolean('completed');
// Eager load client case with person details
$case = \App\Models\ClientCase::query()
->with(['person' => fn ($q) => $q->with(['addresses', 'phones', 'emails', 'bankAccounts'])])
->findOrFail($clientCase->id);
// Eager load case with person details
$case = $clientCase->load('person.addresses', 'person.phones', 'person.emails', 'person.bankAccounts');
// Determine contracts of this case relevant to the current user
// - Normal mode: contracts assigned to me and still active (not completed/cancelled)
// - Completed mode (?completed=1): contracts where my field job was completed today
if ($completedMode) {
$start = now()->startOfDay();
$end = now()->endOfDay();
$contractIds = FieldJob::query()
// Query contracts based on field jobs
$contractsQuery = FieldJob::query()
->where('assigned_user_id', $userId)
->whereNull('cancelled_at')
->whereBetween('completed_at', [$start, $end])
->whereHas('contract', fn ($q) => $q->where('client_case_id', $case->id))
->pluck('contract_id')
->unique()
->values();
} else {
$contractIds = FieldJob::query()
->where('assigned_user_id', $userId)
->whereNull('completed_at')
->whereNull('cancelled_at')
->whereHas('contract', fn ($q) => $q->where('client_case_id', $case->id))
->pluck('contract_id')
->unique()
->values();
}
->when($completedMode,
fn ($q) => $q->whereNull('cancelled_at')->whereBetween('completed_at', [now()->startOfDay(), now()->endOfDay()]),
fn ($q) => $q->whereNull('completed_at')->whereNull('cancelled_at')
);
// Get contracts with relationships
$contracts = \App\Models\Contract::query()
->where('client_case_id', $case->id)
->whereIn('id', $contractIds)
->with(['type:id,name', 'account'])
->whereIn('id', $contractsQuery->pluck('contract_id')->unique())
->with(['type:id,name', 'account', 'latestObject'])
->orderByDesc('created_at')
->get();
// Attach latest object (if any) to each contract as last_object for display
if ($contracts->isNotEmpty()) {
$byId = $contracts->keyBy('id');
$latestObjects = \App\Models\CaseObject::query()
->whereIn('contract_id', $byId->keys())
->whereNull('deleted_at')
->select('id', 'reference', 'name', 'description', 'type', 'contract_id', 'created_at')
// Build merged documents
$documents = $case->documents()
->orderByDesc('created_at')
->get()
->groupBy('contract_id')
->map(function ($group) {
return $group->first();
});
foreach ($latestObjects as $cid => $obj) {
if (isset($byId[$cid])) {
$byId[$cid]->setAttribute('last_object', $obj);
}
}
}
// Build merged documents: case documents + documents of assigned contracts
$contractRefMap = [];
foreach ($contracts as $c) {
$contractRefMap[$c->id] = $c->reference;
}
$contractDocs = \App\Models\Document::query()
->map(fn ($d) => array_merge($d->toArray(), [
'documentable_type' => \App\Models\ClientCase::class,
'client_case_uuid' => $case->uuid,
]))
->concat(
\App\Models\Document::query()
->where('documentable_type', \App\Models\Contract::class)
->whereIn('documentable_id', $contractIds)
->whereIn('documentable_id', $contracts->pluck('id'))
->with('documentable:id,uuid,reference')
->orderByDesc('created_at')
->get()
->map(function ($d) use ($contractRefMap) {
$arr = $d->toArray();
$arr['contract_reference'] = $contractRefMap[$d->documentable_id] ?? null;
$arr['documentable_type'] = \App\Models\Contract::class;
$arr['contract_uuid'] = optional(\App\Models\Contract::withTrashed()->find($d->documentable_id))->uuid;
->map(fn ($d) => array_merge($d->toArray(), [
'contract_reference' => $d->documentable?->reference,
'contract_uuid' => $d->documentable?->uuid,
]))
)
->sortByDesc('created_at')
->values();
return $arr;
});
// Get segment IDs for filtering actions
$segmentIds = \App\Models\FieldJobSetting::query()
->whereIn('id', $contractsQuery->pluck('field_job_setting_id')->filter()->unique())
->pluck('segment_id')
->filter()
->unique();
$caseDocs = $case->documents()->orderByDesc('created_at')->get()->map(function ($d) use ($case) {
$arr = $d->toArray();
$arr['documentable_type'] = \App\Models\ClientCase::class;
$arr['client_case_uuid'] = $case->uuid;
return $arr;
});
$documents = $caseDocs->concat($contractDocs)->sortByDesc('created_at')->values();
// Provide minimal types for PersonInfoGrid
$types = [
return Inertia::render('Phone/Case/Index', [
'client' => $case->client->load('person.addresses', 'person.phones', 'person.emails', 'person.bankAccounts'),
'client_case' => $case,
'contracts' => $contracts,
'documents' => $documents,
'types' => [
'address_types' => \App\Models\Person\AddressType::all(),
'phone_types' => \App\Models\Person\PhoneType::all(),
];
// Case activities (compact for phone): latest 20 with relations
$activities = $case->activities()
],
'account_types' => \App\Models\AccountType::all(),
'actions' => \App\Models\Action::query()
->when($segmentIds->isNotEmpty(), fn ($q) => $q->whereIn('segment_id', $segmentIds))
->with([
'decisions:id,name,color_tag,auto_mail,email_template_id',
'decisions.emailTemplate:id,name,entity_types,allow_attachments',
])
->get(['id', 'name', 'color_tag', 'segment_id']),
'activities' => $case->activities()
->with(['action', 'decision', 'contract:id,uuid,reference', 'user:id,name'])
->orderByDesc('created_at')
->limit(20)
->get()
->map(function ($a) {
$a->setAttribute('user_name', optional($a->user)->name);
return $a;
});
// Determine segment filters from FieldJobSettings for this case/user context
$settingIds = FieldJob::query()
->where('assigned_user_id', $userId)
->whereHas('contract', fn ($q) => $q->where('client_case_id', $case->id))
->when(
$completedMode,
function ($q) {
$q->whereNull('cancelled_at')
->whereBetween('completed_at', [now()->startOfDay(), now()->endOfDay()]);
},
function ($q) {
$q->whereNull('completed_at')->whereNull('cancelled_at');
}
)
->pluck('field_job_setting_id')
->filter()
->unique()
->values();
$segmentIds = collect();
if ($settingIds->isNotEmpty()) {
$segmentIds = \App\Models\FieldJobSetting::query()
->whereIn('id', $settingIds)
->pluck('segment_id')
->filter()
->unique()
->values();
}
// Filter actions and their decisions by the derived segment ids (decisions.segment_id)
$actions = \App\Models\Action::query()
->when($segmentIds->isNotEmpty(), function ($q) use ($segmentIds) {
// Filter actions by their segment_id matching the FieldJobSetting segment(s)
$q->whereIn('segment_id', $segmentIds);
})
->with([
'decisions' => function ($q) {
$q->select('decisions.id', 'decisions.name', 'decisions.color_tag', 'decisions.auto_mail', 'decisions.email_template_id');
},
'decisions.emailTemplate' => function ($q) {
$q->select('id', 'name', 'entity_types', 'allow_attachments');
},
])
->get(['id', 'name', 'color_tag', 'segment_id']);
return Inertia::render('Phone/Case/Index', [
'client' => $case->client()->with('person', fn ($q) => $q->with(['addresses', 'phones', 'emails', 'bankAccounts']))->firstOrFail(),
'client_case' => $case,
'contracts' => $contracts,
'documents' => $documents,
'types' => $types,
'account_types' => \App\Models\AccountType::all(),
// Provide decisions (filtered by segment) with linked email template metadata (entity_types, allow_attachments)
'actions' => $actions,
'activities' => $activities,
->map(fn ($a) => $a->setAttribute('user_name', $a->user?->name)),
'completed_mode' => $completedMode,
]);
}
+7 -2
View File
@@ -65,6 +65,12 @@ public function show(Segment $segment)
$contracts = $this->hydrateClientShortcut($contracts);
// Hide addresses array since we're using the singular address relationship
$contracts->getCollection()->each(function ($contract) {
$contract->clientCase?->person?->makeHidden('addresses');
$contract->clientCase?->client?->person?->makeHidden('addresses');
});
$clients = Client::query()
->whereHas('clientCases.contracts.segments', function ($q) use ($segment) {
$q->where('segments.id', $segment->id)
@@ -191,8 +197,7 @@ private function buildContractsQuery(Segment $segment, ?string $search, ?string
->where('contract_segment.active', '=', 1);
})
->with([
'clientCase.person',
'clientCase.client.person',
'clientCase.person.address',
'type',
'account',
])
@@ -0,0 +1,43 @@
<?php
namespace App\Http\Requests;
use App\Exports\ClientContractsExport;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class ExportClientContractsRequest extends FormRequest
{
public const SCOPE_CURRENT = 'current';
public const SCOPE_ALL = 'all';
public function authorize(): bool
{
return $this->user() !== null;
}
public function rules(): array
{
$columnRule = Rule::in(ClientContractsExport::allowedColumns());
return [
'scope' => ['required', Rule::in([self::SCOPE_CURRENT, self::SCOPE_ALL])],
'columns' => ['required', 'array', 'min:1'],
'columns.*' => ['string', $columnRule],
'search' => ['nullable', 'string', 'max:255'],
'from' => ['nullable', 'date'],
'to' => ['nullable', 'date'],
'segments' => ['nullable', 'string'],
'page' => ['nullable', 'integer', 'min:1'],
'per_page' => ['nullable', 'integer', 'min:1', 'max:200'],
];
}
protected function prepareForValidation(): void
{
$this->merge([
'per_page' => $this->input('per_page') ?? $this->input('perPage'),
]);
}
}
+2
View File
@@ -6,10 +6,12 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Account extends Model
{
/** @use HasFactory<\Database\Factories\Person/AccountFactory> */
use SoftDeletes;
use HasFactory;
protected $fillable = [
+17
View File
@@ -96,6 +96,11 @@ public function segments(): BelongsToMany
->wherePivot('active', true);
}
public function attachedSegments(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Segment::class);
}
public function account(): HasOne
{
// Use latestOfMany to always surface newest account snapshot if multiple exist.
@@ -114,6 +119,18 @@ public function documents(): MorphMany
return $this->morphMany(\App\Models\Document::class, 'documentable');
}
public function fieldJobs(): HasMany
{
return $this->hasMany(\App\Models\FieldJob::class);
}
public function latestObject(): HasOne
{
return $this->hasOne(\App\Models\CaseObject::class)
->whereNull('deleted_at')
->latest();
}
protected static function booted(): void
{
static::created(function (Contract $contract): void {
+6 -1
View File
@@ -24,6 +24,8 @@ class FieldJob extends Model
'priority',
'notes',
'address_snapshot ',
'last_activity',
'added_activity'
];
protected $casts = [
@@ -31,6 +33,8 @@ class FieldJob extends Model
'completed_at' => 'datetime',
'cancelled_at' => 'datetime',
'priority' => 'boolean',
'last_activity' => 'datetime',
'added_activity' => 'boolean',
'address_snapshot ' => 'array',
];
@@ -90,7 +94,8 @@ public function user(): BelongsTo
public function contract(): BelongsTo
{
return $this->belongsTo(Contract::class, 'contract_id');
return $this->belongsTo(Contract::class, 'contract_id')
->where('active', true);
}
/**
+9
View File
@@ -46,6 +46,7 @@ class Person extends Model
'group_id',
'type_id',
'user_id',
'employer'
];
protected $hidden = [
@@ -112,6 +113,14 @@ public function addresses(): HasMany
->orderBy('id');
}
public function address(): HasOne
{
return $this->hasOne(\App\Models\Person\PersonAddress::class)
->with(['type'])
->where('active', '=', 1)
->oldestOfMany('id');
}
public function emails(): HasMany
{
return $this->hasMany(\App\Models\Email::class, 'person_id')
+3 -1
View File
@@ -69,6 +69,8 @@ public function executeSetting(ArchiveSetting $setting, ?array $context = null,
$entities = $flat;
}
// dd($entities);
foreach ($entities as $entityDef) {
$rawTable = $entityDef['table'] ?? null;
if (! $rawTable) {
@@ -97,7 +99,7 @@ public function executeSetting(ArchiveSetting $setting, ?array $context = null,
// Process in batches to avoid locking large tables
while (true) {
$query = DB::table($table)->whereNull('deleted_at');
if (Schema::hasColumn($table, 'active')) {
if (Schema::hasColumn($table, 'active') && ! $reactivate) {
$query->where('active', 1);
}
// Apply context filters or chain derived filters
+681 -26
View File
@@ -24,12 +24,23 @@
use App\Models\Person\PersonPhone;
use App\Models\Person\PersonType;
use App\Models\Person\PhoneType;
use Exception;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\QueryException;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
class ImportProcessor
{
/**
* Track contracts that already existed and were matched during history imports.
*
* @var array<int,bool>
*/
private array $historyFoundContractIds = [];
/**
* Process an import and apply basic dedup checks.
* Returns summary counts.
@@ -42,6 +53,7 @@ public function process(Import $import, ?Authenticatable $user = null): array
$imported = 0;
$invalid = 0;
$fh = null;
$this->historyFoundContractIds = [];
// Only CSV/TSV supported in this pass
if (! in_array($import->source_type, ['csv', 'txt'])) {
@@ -73,6 +85,7 @@ public function process(Import $import, ?Authenticatable $user = null): array
// Template meta flags
$tplMeta = optional($import->template)->meta ?? [];
$paymentsImport = (bool) ($tplMeta['payments_import'] ?? false);
$historyImport = (bool) ($tplMeta['history_import'] ?? false);
$contractKeyMode = $tplMeta['contract_key_mode'] ?? null;
// Prefer explicitly chosen delimiter, then template meta, else detected
$delimiter = $import->meta['forced_delimiter']
@@ -171,10 +184,7 @@ public function process(Import $import, ?Authenticatable $user = null): array
}
// Preflight: warn if any mapped source columns are not present in the header (exact match)
$headerSet = [];
foreach ($header as $h) {
$headerSet[$h] = true;
}
$missingSources = [];
// Regex validation removed per request; rely on basic length/placeholder checks only
foreach ($mappings as $map) {
$src = (string) ($map->source_column ?? '');
if ($src !== '' && ! array_key_exists($src, $headerSet)) {
@@ -207,7 +217,28 @@ public function process(Import $import, ?Authenticatable $user = null): array
if ($isPg) {
// Establish a savepoint so a failing row does not poison the whole transaction
try {
DB::statement('SAVEPOINT import_row_'.$rowNum);
} catch (\Throwable $se) {
Log::error('Import savepoint_failed', [
'import_id' => $import->id,
'row_number' => $rowNum,
'exception' => $this->exceptionContext($se),
]);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'event' => 'savepoint_failed',
'level' => 'error',
'message' => 'Failed to create savepoint; transaction already aborted.',
'context' => [
'row_number' => $rowNum,
'exception' => $this->exceptionContext($se),
],
]);
throw $se; // abort import so root cause surfaces
}
}
// Scope variables per row so they aren't reused after exception
@@ -299,9 +330,13 @@ public function process(Import $import, ?Authenticatable $user = null): array
$contractResult = null;
}
} else {
$contractResult = $this->upsertContractChain($import, $mapped, $mappings);
$contractResult = $this->upsertContractChain($import, $mapped, $mappings, $historyImport);
// If contract was resolved/updated/inserted and reactivation requested but not needed (already active), we just continue normal flow.
if ($contractResult && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) {
// Do not attempt reactivation on freshly inserted contracts
if (($contractResult['action'] ?? null) === 'inserted') {
// Newly created contracts are already active; skip reactivation path
} else {
$found = $contractResult['contract'];
if ($found->active == 0 || $found->deleted_at) {
$reactivationApplied = $this->attemptContractReactivation($found, $user);
@@ -326,7 +361,20 @@ public function process(Import $import, ?Authenticatable $user = null): array
}
}
}
if ($contractResult['action'] === 'skipped') {
}
if ($contractResult['action'] === 'skipped_history') {
// History import: keep existing contract for downstream relations but do not update or attach segments/actions
$skipped++;
$importRow->update(['status' => 'skipped']);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'row_skipped',
'level' => 'info',
'message' => $contractResult['message'] ?? 'Existing contract reused (history import).',
]);
} elseif ($contractResult['action'] === 'skipped') {
// Even if no contract fields were updated, we may still need to apply template meta
// like attaching a segment or creating an activity. Do that if we have the contract.
if (isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) {
@@ -437,12 +485,18 @@ public function process(Import $import, ?Authenticatable $user = null): array
// Accounts
$accountResult = null;
if ($historyImport && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract && ! isset($mapped['account'])) {
$autoAcc = $this->ensureHistoryAccount($contractResult['contract'], $mapped);
if ($autoAcc) {
$accountResult = ['action' => 'inserted', 'account' => $autoAcc, 'contract' => $contractResult['contract'], 'contract_id' => $contractResult['contract']->id];
}
}
if (isset($mapped['account'])) {
// If a contract was just created or resolved above, pass its id to account mapping for this row
if ($contractResult && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) {
$mapped['account']['contract_id'] = $contractResult['contract']->id;
}
$accountResult = $this->upsertAccount($import, $mapped, $mappings);
$accountResult = $this->upsertAccount($import, $mapped, $mappings, $historyImport);
if ($accountResult['action'] === 'skipped') {
$skipped++;
$importRow->update(['status' => 'skipped']);
@@ -555,6 +609,55 @@ public function process(Import $import, ?Authenticatable $user = null): array
}
}
// Activities: create or update activities linked to contracts/cases
if (isset($mapped['activity'])) {
$activityResult = $this->upsertActivity($import, $mapped, $mappings, $contractResult ?? null, $accountResult ?? null);
if ($activityResult['action'] === 'skipped') {
$skipped++;
$importRow->update(['status' => 'skipped']);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'row_skipped',
'level' => 'info',
'message' => $activityResult['message'] ?? 'Skipped (no changes).',
'context' => $activityResult['context'] ?? null,
]);
} elseif (in_array($activityResult['action'], ['inserted', 'updated'], true)) {
$imported++;
$importRow->update([
'status' => 'imported',
'entity_type' => Activity::class,
'entity_id' => $activityResult['activity']->id,
]);
$activityFieldsStr = '';
if (! empty($activityResult['applied_fields'] ?? [])) {
$activityFieldsStr = $this->formatAppliedFieldMessage('activity', $activityResult['applied_fields']);
}
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'row_imported',
'level' => 'info',
'message' => ucfirst($activityResult['action']).' activity'.($activityFieldsStr ? ' '.$activityFieldsStr : ''),
'context' => ['id' => $activityResult['activity']->id, 'fields' => $activityResult['applied_fields'] ?? []],
]);
} else {
$invalid++;
$importRow->update(['status' => 'invalid', 'errors' => [$activityResult['message'] ?? 'Activity processing failed']]);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'row_invalid',
'level' => 'error',
'message' => $activityResult['message'] ?? 'Activity processing failed',
]);
}
}
// Contacts: resolve person via Contract/Account chain, client_case.client_ref, contacts, or identifiers
$personIdForRow = null;
// Prefer person from contract created/updated above
@@ -986,13 +1089,38 @@ public function process(Import $import, ?Authenticatable $user = null): array
}
}
} catch (\Throwable $e) {
$rollbackFailed = false;
$rollbackError = null;
if ($isPg) {
// Roll back only this row's work
try {
DB::statement('ROLLBACK TO SAVEPOINT import_row_'.$rowNum);
} catch (\Throwable $ignored) { /* noop */
} catch (\Throwable $ignored) {
$rollbackFailed = true;
$rollbackError = $ignored;
}
}
if ($rollbackFailed) {
Log::error('Import row_rollback_failed', [
'import_id' => $import->id,
'row_number' => $rowNum,
'exception' => $this->exceptionContext($rollbackError ?? $e),
]);
// Abort the whole import if we cannot rollback to the row savepoint (transaction is poisoned)
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'event' => 'row_rollback_failed',
'level' => 'error',
'message' => 'Rollback to savepoint failed; aborting import.',
'context' => [
'row_number' => $rowNum,
'exception' => $this->exceptionContext($rollbackError ?? $e),
],
]);
throw $rollbackError ?? $e;
}
// Ensure importRow exists for logging if failure happened before its creation
if (! $importRow) {
try {
@@ -1019,6 +1147,12 @@ public function process(Import $import, ?Authenticatable $user = null): array
}
$failedRows[] = $rowNum;
$invalid++;
Log::error('Import row_exception', [
'import_id' => $import->id,
'row_number' => $rowNum,
'exception' => $this->exceptionContext($e),
'raw_preview' => isset($rawAssoc) ? $this->buildRawDataPreview($rawAssoc) : [],
]);
try {
ImportEvent::create([
'import_id' => $import->id,
@@ -1036,6 +1170,12 @@ public function process(Import $import, ?Authenticatable $user = null): array
],
]);
} catch (\Throwable $evtErr) {
Log::error('Import row_exception_event_failed', [
'import_id' => $import->id,
'row_number' => $rowNum,
'exception' => $this->exceptionContext($evtErr),
'original_exception' => $this->exceptionContext($e),
]);
// Swallow secondary failure to ensure loop continues
}
@@ -1060,6 +1200,29 @@ public function process(Import $import, ?Authenticatable $user = null): array
]);
}
$meta = $import->meta ?? [];
if ($historyImport) {
if (! empty($this->historyFoundContractIds)) {
$found = Contract::query()
->with(['clientCase.person'])
->whereIn('id', array_keys($this->historyFoundContractIds))
->get()
->map(function (Contract $c) {
return [
'contract_uuid' => $c->uuid ?? null,
'reference' => $c->reference,
'case_uuid' => $c->clientCase?->uuid,
'full_name' => $c->clientCase?->person?->full_name,
];
})
->values()
->all();
$meta['history_found_contracts'] = $found;
} else {
$meta['history_found_contracts'] = [];
}
}
$import->update([
'status' => 'completed',
'finished_at' => now(),
@@ -1067,6 +1230,7 @@ public function process(Import $import, ?Authenticatable $user = null): array
'imported_rows' => $imported,
'invalid_rows' => $invalid,
'valid_rows' => $total - $invalid,
'meta' => $meta,
]);
DB::commit();
@@ -1084,12 +1248,17 @@ public function process(Import $import, ?Authenticatable $user = null): array
// Mark failed and log after rollback (so no partial writes persist)
$import->refresh();
$import->update(['status' => 'failed', 'failed_at' => now()]);
Log::error('Import processing_failed', [
'import_id' => $import->id,
'exception' => $this->exceptionContext($e),
]);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'event' => 'processing_failed',
'level' => 'error',
'message' => $e->getMessage(),
'message' => $this->safeErrorMessage($e->getMessage()),
'context' => $this->exceptionContext($e),
]);
return ['ok' => false, 'status' => 'failed', 'error' => $e->getMessage()];
@@ -1119,6 +1288,21 @@ protected function applyMappings(array $raw, $mappings, array $supportsMultiple)
{
$recordType = null;
$mapped = [];
$appendValue = function ($existing, $label, $value) {
// Skip empty new values
if (is_null($value) || (is_string($value) && trim($value) === '')) {
return $existing;
}
$stringVal = is_scalar($value) ? (string) $value : json_encode($value);
$existingStr = is_null($existing) ? '' : (is_scalar($existing) ? (string) $existing : json_encode($existing));
if ($existingStr === '') {
return $label.': '.$stringVal;
}
return $existingStr.', '.$label.': '.$stringVal;
};
$fieldFirstLabel = [];
$rootFirstLabel = [];
foreach ($mappings as $map) {
$src = $map->source_column;
$target = $map->target_field;
@@ -1278,9 +1462,34 @@ protected function applyMappings(array $raw, $mappings, array $supportsMultiple)
if (! isset($mapped[$root]) || ! is_array($mapped[$root])) {
$mapped[$root] = [];
}
$canConcat = $this->fieldAllowsConcatenation($root, $field);
if ($canConcat) {
$key = $root.'.'.$field;
if (! array_key_exists($field, $mapped[$root]) || is_null($mapped[$root][$field]) || $mapped[$root][$field] === '') {
$mapped[$root][$field] = $value;
$fieldFirstLabel[$key] = (string) $src;
} else {
$existing = $mapped[$root][$field];
$firstLabel = $fieldFirstLabel[$key] ?? null;
$existingStr = $firstLabel ? ($firstLabel.': '.(is_scalar($existing) ? (string) $existing : json_encode($existing))) : (is_scalar($existing) ? (string) $existing : json_encode($existing));
$mapped[$root][$field] = $appendValue($existingStr, (string) $src, $value);
}
} else {
// For typed fields (dates/numbers), keep the first non-empty value to avoid coercion errors
if (! array_key_exists($field, $mapped[$root]) || is_null($mapped[$root][$field]) || $mapped[$root][$field] === '') {
$mapped[$root][$field] = $value;
}
}
} else {
if (! array_key_exists($root, $mapped) || is_null($mapped[$root]) || $mapped[$root] === '') {
$mapped[$root] = $value;
$rootFirstLabel[$root] = (string) $src;
} else {
$firstLabel = $rootFirstLabel[$root] ?? null;
$existing = $mapped[$root];
$existingStr = $firstLabel ? ($firstLabel.': '.(is_scalar($existing) ? (string) $existing : json_encode($existing))) : (is_scalar($existing) ? (string) $existing : json_encode($existing));
$mapped[$root] = $appendValue($existingStr, (string) $src, $value);
}
}
}
}
@@ -1288,6 +1497,28 @@ protected function applyMappings(array $raw, $mappings, array $supportsMultiple)
return [$recordType, $mapped];
}
/**
* Decide whether multiple mapped source columns should be concatenated for a given target field.
* For date/time and numeric-like fields we avoid concatenation to prevent invalid type coercion.
*/
private function fieldAllowsConcatenation(?string $root, ?string $field): bool
{
if ($field === null) {
return true;
}
$f = strtolower($field);
// Date / datetime indicators
if ($f === 'birthday' || str_contains($f, 'date') || str_ends_with($f, '_at')) {
return false;
}
// Common numeric fields
if (in_array($f, ['amount', 'amount_cents', 'quantity', 'balance_amount'], true)) {
return false;
}
return true;
}
private function arraySetDot(array &$arr, string $path, $value): void
{
$keys = explode('.', $path);
@@ -1301,7 +1532,7 @@ private function arraySetDot(array &$arr, string $path, $value): void
$ref = $value;
}
private function upsertAccount(Import $import, array $mapped, $mappings): array
private function upsertAccount(Import $import, array $mapped, $mappings, bool $historyImport = false): array
{
$clientId = $import->client_id; // may be null, used for contract lookup/creation
$acc = $mapped['account'] ?? [];
@@ -1402,7 +1633,7 @@ private function upsertAccount(Import $import, array $mapped, $mappings): array
$existing = Account::query()
->where('contract_id', $contractId)
->where('reference', $reference)
//->where('reference', $reference)
->where('active', 1)
->first();
@@ -1425,6 +1656,14 @@ private function upsertAccount(Import $import, array $mapped, $mappings): array
$value = $acc[$field] ?? null;
if (in_array($field, ['balance_amount', 'initial_amount'], true) && is_string($value)) {
$value = $this->normalizeDecimal($value);
// Ensure the normalized value is numeric, otherwise default to 0
if ($value === '' || $value === '-' || ! is_numeric($value)) {
$value = 0;
}
}
// Convert empty string to 0 for amount fields
if (in_array($field, ['balance_amount', 'initial_amount'], true) && ($value === '' || $value === null)) {
$value = 0;
}
$mode = $map->apply_mode ?? 'both';
if ($mode === 'keyref') {
@@ -1447,11 +1686,19 @@ private function upsertAccount(Import $import, array $mapped, $mappings): array
}
}
if ($historyImport && $existing) {
return ['action' => 'skipped', 'account' => $existing, 'contract_id' => $contractId, 'message' => 'History import does not update accounts'];
}
if ($existing) {
// Build non-null changes for account fields
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
// Track balance change
$oldBalance = (float) ($existing->balance_amount ?? 0);
// Track balance change - normalize in case DB has malformed data
$rawBalance = $existing->balance_amount ?? 0;
if (is_string($rawBalance) && $rawBalance !== '') {
$rawBalance = $this->normalizeDecimal($rawBalance);
}
$oldBalance = is_numeric($rawBalance) ? (float) $rawBalance : 0;
// Note: meta merging for contracts is handled in upsertContractChain, not here
if (! empty($changes)) {
$existing->fill($changes);
@@ -1460,7 +1707,11 @@ private function upsertAccount(Import $import, array $mapped, $mappings): array
// If balance_amount changed and this wasn't caused by a payment (we are in account upsert), log an activity with before/after
if (array_key_exists('balance_amount', $changes)) {
$newBalance = (float) ($existing->balance_amount ?? 0);
$rawNewBalance = $existing->balance_amount ?? 0;
if (is_string($rawNewBalance) && $rawNewBalance !== '') {
$rawNewBalance = $this->normalizeDecimal($rawNewBalance);
}
$newBalance = is_numeric($rawNewBalance) ? (float) $rawNewBalance : 0;
if ($newBalance !== $oldBalance) {
try {
$contractId = $existing->contract_id;
@@ -1505,6 +1756,11 @@ private function upsertAccount(Import $import, array $mapped, $mappings): array
&& ($initMode === null || in_array($initMode, ['insert', 'both'], true))) {
$applyInsert['initial_amount'] = $applyInsert['balance_amount'];
}
if ($historyImport) {
// Force zero amounts for history imports regardless of mapped amounts
$applyInsert['balance_amount'] = 0;
$applyInsert['initial_amount'] = 0;
}
if (empty($applyInsert)) {
return ['action' => 'skipped', 'message' => 'No fields marked for insert'];
}
@@ -1516,12 +1772,44 @@ private function upsertAccount(Import $import, array $mapped, $mappings): array
if (! array_key_exists('active', $data)) {
$data['active'] = 1;
}
if ($historyImport) {
$data['balance_amount'] = 0;
$data['initial_amount'] = 0;
}
$created = Account::create($data);
return ['action' => 'inserted', 'account' => $created, 'contract_id' => $contractId, 'applied_fields' => $data];
}
}
private function ensureHistoryAccount(Contract $contract, array $mapped): ?Account
{
$existing = Account::query()
->where('contract_id', $contract->id)
->where('active', 1)
->first();
if ($existing) {
return $existing;
}
$reference = $mapped['account']['reference'] ?? $mapped['contract']['reference'] ?? null;
if (is_string($reference)) {
$reference = preg_replace('/\s+/', '', trim($reference));
}
if (! $reference || $reference === '') {
$reference = 'HIST-'.$contract->id;
}
return Account::create([
'contract_id' => $contract->id,
'reference' => $reference,
'type_id' => $this->getDefaultAccountTypeId(),
'active' => 1,
'balance_amount' => 0,
'initial_amount' => 0,
]);
}
private function upsertCaseObject(Import $import, array $mapped, $mappings, int $contractId): array
{
// Support both 'case_object' and 'case_objects' keys (template may use plural)
@@ -1625,6 +1913,210 @@ private function upsertCaseObject(Import $import, array $mapped, $mappings, int
}
}
private function upsertActivity(Import $import, array $mapped, $mappings, ?array $contractResult, ?array $accountResult): array
{
$activity = $mapped['activity'] ?? [];
// Default contract/client_case from freshly created or updated contract when present
if ($contractResult && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) {
$activity['contract_id'] = $activity['contract_id'] ?? $contractResult['contract']->id;
$activity['client_case_id'] = $activity['client_case_id'] ?? $contractResult['contract']->client_case_id;
}
$contractId = $activity['contract_id'] ?? null;
if (! $contractId && $contractResult && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) {
$contractId = $contractResult['contract']->id;
} elseif (! $contractId && $accountResult && isset($accountResult['contract_id'])) {
$contractId = $accountResult['contract_id'];
} elseif (! $contractId && $accountResult && isset($accountResult['contract']) && $accountResult['contract'] instanceof Contract) {
$contractId = $accountResult['contract']->id;
}
if (! $contractId && $import->client_id && ! empty($mapped['contract']['reference'] ?? null)) {
$ref = preg_replace('/\s+/', '', trim((string) $mapped['contract']['reference']));
if ($ref !== '') {
$contractId = Contract::query()
->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
->where('client_cases.client_id', $import->client_id)
->where('contracts.reference', $ref)
->value('contracts.id');
}
}
$clientCaseId = $activity['client_case_id'] ?? null;
if (! $clientCaseId && $contractId) {
$clientCaseId = Contract::where('id', $contractId)->value('client_case_id');
}
if (! $clientCaseId && $contractResult && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) {
$clientCaseId = $contractResult['contract']->client_case_id;
}
if (! $clientCaseId && $accountResult && isset($accountResult['contract']) && $accountResult['contract'] instanceof Contract) {
$clientCaseId = $accountResult['contract']->client_case_id;
}
if (! $clientCaseId && $accountResult && isset($accountResult['contract_id'])) {
$clientCaseId = Contract::where('id', $accountResult['contract_id'])->value('client_case_id');
}
if (! $clientCaseId && $import->client_id && ! empty($mapped['client_case']['client_ref'] ?? null)) {
$clientCaseId = ClientCase::where('client_id', $import->client_id)
->where('client_ref', $mapped['client_case']['client_ref'])
->value('id');
}
if (! $clientCaseId) {
return [
'action' => 'invalid',
'message' => 'Activity requires a client_case_id or resolvable contract.',
'context' => [
'contract_id' => $contractId,
'contract_reference' => $mapped['contract']['reference'] ?? null,
],
];
}
// Collect apply_mode settings for activity fields
$applyModeByField = [];
foreach ($mappings as $map) {
$target = (string) ($map->target_field ?? '');
if (! str_starts_with($target, 'activity.')) {
continue;
}
$field = substr($target, strlen('activity.'));
$applyModeByField[$field] = (string) ($map->apply_mode ?? 'both');
}
$applyInsert = [];
$applyUpdate = [];
foreach ($activity as $field => $value) {
$applyMode = $applyModeByField[$field] ?? 'both';
$normalized = $value;
if ($field === 'due_date') {
$normalized = is_scalar($value) ? $this->normalizeDate((string) $value) : null;
} elseif ($field === 'created_at') {
$normalized = is_scalar($value) ? $this->normalizeDateTime((string) $value) : null;
} elseif ($field === 'amount') {
if (is_string($value)) {
$normalized = $this->normalizeDecimal($value);
}
$normalized = is_null($normalized) ? null : (float) $normalized;
} elseif (in_array($field, ['action_id', 'decision_id', 'user_id'], true)) {
$normalized = is_null($value) ? null : (int) $value;
} elseif (is_string($normalized)) {
// Clean invalid UTF-8 sequences from string fields
$normalized = mb_convert_encoding($normalized, 'UTF-8', 'UTF-8');
$normalized = trim($normalized);
}
if (in_array($applyMode, ['both', 'insert'], true)) {
$applyInsert[$field] = $normalized;
}
if (in_array($applyMode, ['both', 'update'], true)) {
$applyUpdate[$field] = $normalized;
}
}
$settings = \App\Models\PaymentSetting::query()->first();
$tplMeta = optional($import->template)->meta ?? [];
$defaultActionId = $applyInsert['action_id']
?? $applyUpdate['action_id']
?? ($import->meta['activity_action_id'] ?? null)
?? ($tplMeta['activity_action_id'] ?? null)
?? ($import->meta['action_id'] ?? null)
?? ($tplMeta['action_id'] ?? null)
?? ($settings?->default_action_id ?? null);
$defaultDecisionId = $applyInsert['decision_id']
?? $applyUpdate['decision_id']
?? ($import->meta['activity_decision_id'] ?? null)
?? ($tplMeta['activity_decision_id'] ?? null)
?? ($import->meta['decision_id'] ?? null)
?? ($tplMeta['decision_id'] ?? null);
if (! $defaultActionId) {
return [
'action' => 'invalid',
'message' => 'Activity requires action_id (provide via mapping or import meta).',
];
}
$applyInsert['action_id'] = $applyInsert['action_id'] ?? $defaultActionId;
$applyUpdate['action_id'] = $applyUpdate['action_id'] ?? $defaultActionId;
if ($defaultDecisionId) {
$applyInsert['decision_id'] = $applyInsert['decision_id'] ?? $defaultDecisionId;
$applyUpdate['decision_id'] = $applyUpdate['decision_id'] ?? $defaultDecisionId;
}
// Default created_at for inserted activities (from mapped field or template/import meta)
if (empty($applyInsert['created_at'])) {
$defaultCreated = $this->normalizeDateTime($applyInsert['created_at'] ?? $import->meta['activity_created_at'] ?? $tplMeta['activity_created_at'] ?? null);
if ($defaultCreated) {
$applyInsert['created_at'] = $defaultCreated;
if (empty($applyInsert['updated_at'])) {
$applyInsert['updated_at'] = $defaultCreated;
}
}
}
$applyInsert['client_case_id'] = $clientCaseId;
$applyUpdate['client_case_id'] = $applyUpdate['client_case_id'] ?? $clientCaseId;
if ($contractId) {
$applyInsert['contract_id'] = $contractId;
$applyUpdate['contract_id'] = $applyUpdate['contract_id'] ?? $contractId;
}
// Idempotency: if note + due_date + contract match, treat as existing
$existing = null;
$noteKey = $applyInsert['note'] ?? null;
$dueKey = $applyInsert['due_date'] ?? null;
if ($contractId && $noteKey && $dueKey) {
$existing = Activity::query()
->where('contract_id', $contractId)
->where('note', $noteKey)
->whereDate('due_date', $dueKey)
->first();
} elseif ($clientCaseId && $noteKey && $dueKey) {
$existing = Activity::query()
->where('client_case_id', $clientCaseId)
->where('note', $noteKey)
->whereDate('due_date', $dueKey)
->first();
}
if ($existing) {
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
if (! empty($changes)) {
$existing->fill($changes);
$existing->save();
return ['action' => 'updated', 'activity' => $existing, 'applied_fields' => $changes];
}
return ['action' => 'skipped', 'activity' => $existing, 'message' => 'No changes needed'];
}
$data = array_filter($applyInsert, fn ($v) => ! is_null($v));
$activityModel = new Activity;
$activityModel->forceFill($data);
if (array_key_exists('created_at', $data)) {
// Preserve provided timestamps by disabling automatic timestamps for this save
$activityModel->timestamps = false;
$activityModel->save();
$activityModel->timestamps = true;
} else {
$activityModel->save();
}
return ['action' => 'inserted', 'activity' => $activityModel, 'applied_fields' => $data];
}
private function normalizeDateTime(?string $raw): ?string
{
if ($raw === null || trim($raw) === '') {
return null;
}
try {
return Carbon::parse($raw)->format('Y-m-d H:i:s');
} catch (\Throwable $e) {
return null;
}
}
private function mappingsContainRoot($mappings, string $root): bool
{
foreach ($mappings as $map) {
@@ -1657,7 +2149,7 @@ private function findPersonIdByIdentifiers(array $p): ?int
return null;
}
private function upsertContractChain(Import $import, array $mapped, $mappings): array
private function upsertContractChain(Import $import, array $mapped, $mappings, bool $historyImport = false): array
{
$contractData = $mapped['contract'] ?? [];
$reference = $contractData['reference'] ?? null;
@@ -1821,10 +2313,36 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
}
if ($existing) {
if ($historyImport) {
$this->historyFoundContractIds[$existing->id] = true;
return ['action' => 'skipped_history', 'contract' => $existing, 'message' => 'Existing contract left unchanged (history import)'];
}
// Check if contract is soft-deleted and needs reactivation
$isTrashed = $existing->trashed();
// 1) Prepare contract field changes (non-null)
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
// 2) Prepare meta changes if provided via mapping
// 2) Handle reactivation defaults when contract is soft-deleted
if ($isTrashed || $existing->active == 0) {
// Check if start_date is in the mappings
$hasStartDateMapping = $this->mappingIncludes($mappings, 'contract.start_date');
if (!$hasStartDateMapping) {
// Default to current date when not in mappings
$changes['start_date'] = now()->toDateString();
}
// Check if end_date is in the mappings
$hasEndDateMapping = $this->mappingIncludes($mappings, 'contract.end_date');
if (!$hasEndDateMapping) {
// Default to null when not in mappings
$changes['end_date'] = null;
}
}
// 3) Prepare meta changes if provided via mapping
$metaUpdated = false;
$metaAppliedKeys = [];
if (! empty($contractData['meta'] ?? null) && is_array($contractData['meta'])) {
@@ -1867,7 +2385,7 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
}
}
if (empty($changes) && ! $metaUpdated) {
if (empty($changes) && ! $metaUpdated && ! $isTrashed) {
// Nothing to change
return ['action' => 'skipped', 'message' => 'No contract fields or meta changes', 'contract' => $existing];
}
@@ -1875,6 +2393,12 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
if (! empty($changes)) {
$existing->fill($changes);
}
// Restore soft-deleted contract if it was trashed
if ($isTrashed) {
$existing->restore();
}
$existing->save();
// Build applied fields info, include meta keys if any
@@ -1885,7 +2409,9 @@ private function upsertContractChain(Import $import, array $mapped, $mappings):
}
}
return ['action' => 'updated', 'contract' => $existing, 'applied_fields' => $applied];
$actionType = $isTrashed ? 'reactivated' : 'updated';
return ['action' => $actionType, 'contract' => $existing, 'applied_fields' => $applied];
} else {
if (empty($applyInsert)) {
return ['action' => 'skipped', 'message' => 'No contract fields marked for insert'];
@@ -2061,13 +2587,57 @@ private function safeErrorMessage(string $msg): string
}
// Fallback strip invalid bytes
$msg = @iconv('UTF-8', 'UTF-8//IGNORE', $msg) ?: $msg;
if (strlen($msg) > 500) {
$msg = substr($msg, 0, 497).'...';
// Use mb_strlen and mb_substr for UTF-8 safety
if (mb_strlen($msg) > 500) {
$msg = mb_substr($msg, 0, 497).'...';
}
return $msg;
}
/**
* Extract structured exception details for logging.
*/
private function exceptionContext(\Throwable $e): array
{
$ctx = [
'exception' => get_class($e),
'message' => $this->safeErrorMessage($e->getMessage()),
'code' => $e->getCode(),
'file' => $e->getFile().':'.$e->getLine(),
];
if (method_exists($e, 'getPrevious') && $e->getPrevious()) {
$prev = $e->getPrevious();
$ctx['previous'] = [
'exception' => get_class($prev),
'message' => $this->safeErrorMessage($prev->getMessage()),
'code' => $prev->getCode(),
'file' => $prev->getFile().':'.$prev->getLine(),
];
}
if ($e instanceof QueryException) {
$ctx['sql'] = $e->getSql();
$ctx['bindings'] = $e->getBindings();
$info = $e->errorInfo ?? null;
if (is_array($info)) {
$ctx['sqlstate'] = $info[0] ?? null;
$ctx['driver_error_code'] = $info[1] ?? null;
$ctx['driver_error_message'] = $info[2] ?? null;
}
} elseif (property_exists($e, 'errorInfo')) {
$info = $e->errorInfo;
if (is_array($info)) {
$ctx['sqlstate'] = $info[0] ?? null;
$ctx['driver_error_code'] = $info[1] ?? null;
$ctx['driver_error_message'] = $info[2] ?? null;
}
}
return $ctx;
}
/**
* Build a trimmed raw data preview (first 8 columns, truncated values) for logging.
*/
@@ -2108,9 +2678,9 @@ private function formatAppliedFieldMessage(string $root, array $fields): string
} else {
$disp = method_exists($v, '__toString') ? (string) $v : gettype($v);
}
// Truncate very long values for log safety
if (strlen($disp) > 60) {
$disp = substr($disp, 0, 57).'...';
// Truncate very long values for log safety (use mb_substr for UTF-8 safety)
if (mb_strlen($disp) > 60) {
$disp = mb_substr($disp, 0, 57).'...';
}
$parts[] = $k.'='.$disp;
}
@@ -2417,7 +2987,7 @@ private function findOrCreatePersonId(array $p): ?int
// Create person if any fields present; ensure required foreign keys
if (! empty($p)) {
$data = [];
foreach (['first_name', 'last_name', 'full_name', 'tax_number', 'social_security_number', 'birthday', 'gender', 'description', 'group_id', 'type_id'] as $k) {
foreach (['first_name', 'last_name', 'full_name', 'tax_number', 'social_security_number', 'birthday', 'gender', 'description', 'group_id', 'type_id', 'employer'] as $k) {
if (array_key_exists($k, $p)) {
$data[$k] = $p[$k];
}
@@ -2430,6 +3000,16 @@ private function findOrCreatePersonId(array $p): ?int
$data['full_name'] = trim($fn.' '.$ln);
}
}
// normalise birthday date
if (!empty($data['birthday'])) {
try {
$data['birthday'] = date('Y-m-d', strtotime($data['birthday']));
} catch (Exception $e) {
Log::warning('ImportProcessor::findOrCreatePersonId ' . $e->getMessage());
}
}
// ensure required group/type ids
$data['group_id'] = $data['group_id'] ?? $this->getDefaultPersonGroupId();
$data['type_id'] = $data['type_id'] ?? $this->getDefaultPersonTypeId();
@@ -2529,6 +3109,12 @@ private function upsertEmail(int $personId, array $emailData, $mappings): array
if ($value === '') {
return ['action' => 'skipped', 'message' => 'No email value'];
}
$normalizedEmail = filter_var($value, FILTER_VALIDATE_EMAIL);
if (! $normalizedEmail) {
return ['action' => 'skipped', 'message' => 'Invalid email format'];
}
$value = $normalizedEmail;
$emailData['value'] = $normalizedEmail;
$existing = Email::where('person_id', $personId)->where('value', $value)->first();
$applyInsert = [];
$applyUpdate = [];
@@ -2580,14 +3166,60 @@ private function upsertEmail(int $personId, array $emailData, $mappings): array
private function upsertAddress(int $personId, array $addrData, $mappings): array
{
$addressLine = trim((string) ($addrData['address'] ?? ''));
if ($addressLine === '') {
// Normalize whitespace: collapse multiples and tighten around separators
$addressLine = preg_replace('/\s+/', ' ', $addressLine);
$addressLine = preg_replace('/\s*([,;\/])\s*/', '$1 ', $addressLine);
$addressLine = trim($addressLine);
// Skip common placeholders or missing values
if ($addressLine === '' || $addressLine === '0' || strcasecmp($addressLine, '#N/A') === 0 || preg_match('/^(#?n\/?a|na|null|none)$/i', $addressLine)) {
return ['action' => 'skipped', 'message' => 'No address value'];
}
if (mb_strlen($addressLine) < 3) {
return ['action' => 'skipped', 'message' => 'Invalid address value'];
}
// If identical address already exists anywhere, skip to avoid constraint violation
/*$existingAny = PersonAddress::where('address', $addressLine)->first();
if ($existingAny) {
return ['action' => 'skipped', 'message' => 'Address already exists in database'];
}*/
// Default country SLO if not provided
if (! isset($addrData['country']) || $addrData['country'] === null || $addrData['country'] === '') {
$addrData['country'] = 'SLO';
}
$existing = PersonAddress::where('person_id', $personId)->where('address', $addressLine)->first();
if (!empty($addrData['city']) && empty($addrData['post_code'])) {
if (preg_match('/^\d{3,}\s+/',trim($addrData['city']))) {
$cleanStrCity = str($addrData['city'])->squish()->value();
$splitCity = preg_split('/\s/', $cleanStrCity, 2);
if (count($splitCity) >= 2) {
$addrData['post_code'] = $splitCity[0];
$addrData['city'] = $splitCity[1];
}
}
}
// Compare addresses with all spaces removed to handle whitespace variations
/*$addressLineNoSpaces = preg_replace('/\s+/', '', $addressLine);
$existing = PersonAddress::where('person_id', $personId)
->whereRaw("REPLACE(address, ' ', '') = ?", [$addressLineNoSpaces])
->first();*/
// Build search query combining address, post_code and city
$searchParts = [$addrData['address']];
if (!empty($addrData['post_code'])) {
$searchParts[] = $addrData['post_code'];
}
if (!empty($addrData['city'])) {
$searchParts[] = $addrData['city'];
}
$searchQuery = implode(' ', $searchParts);
// Use fulltext search (GIN index optimized)
$existing = PersonAddress::query()->where('person_id', $personId)
->whereRaw("search_vector @@ plainto_tsquery('simple', ?)", [$searchQuery])
->first();
$applyInsert = [];
$applyUpdate = [];
foreach ($mappings as $map) {
@@ -2630,9 +3262,28 @@ private function upsertAddress(int $personId, array $addrData, $mappings): array
$data['person_id'] = $personId;
$data['country'] = $data['country'] ?? 'SLO';
$data['type_id'] = $data['type_id'] ?? $this->getDefaultAddressTypeId();
if (!empty($addrData['post_code']) && $addrData['post_code'] !== '0' && !isset($applyUpdate['post_code'])) {
$data['post_code'] = $addrData['post_code'];
}
try {
$created = PersonAddress::create($data);
return ['action' => 'inserted', 'address' => $created];
} catch (QueryException $e) {
// If unique constraint violation, skip instead of aborting
Log::warning('Address constraint violation during import', [
'person_id' => $personId,
'address' => $addressLine,
'error' => $e->getMessage(),
]);
if ($e->getCode() === '23505' || str_contains($e->getMessage(), 'unique') || str_contains($e->getMessage(), 'duplicate')) {
return ['action' => 'skipped', 'message' => 'Address already exists (constraint violation)'];
}
throw $e;
}
}
}
@@ -2644,6 +3295,10 @@ private function upsertPhone(int $personId, array $phoneData, $mappings): array
if ($nu === '') {
return ['action' => 'skipped', 'message' => 'No phone value'];
}
if (! preg_match('/^[0-9]{6,20}$/', $nu)) {
return ['action' => 'skipped', 'message' => 'Invalid phone value'];
}
$phoneData['nu'] = $nu;
// Find existing phone by normalized number (strip non-numeric from DB values too)
$existing = PersonPhone::where('person_id', $personId)
+96 -3
View File
@@ -25,6 +25,11 @@ class ImportSimulationService
*/
private ?int $clientId = null;
/**
* History import mode flag (from template meta).
*/
private bool $historyImport = false;
/**
* Public entry: simulate import applying mappings to first $limit rows.
* Keeps existing machine keys for backward compatibility, but adds Slovenian
@@ -79,6 +84,7 @@ public function simulate(Import $import, int $limit = 100, bool $verbose = false
$simRows = [];
// Determine keyref behavior for contract.reference from mappings/template
$tplMeta = optional($import->template)->meta ?? [];
$this->historyImport = (bool) ($tplMeta['history_import'] ?? false);
$contractKeyModeTpl = $tplMeta['contract_key_mode'] ?? null; // e.g. 'reference'
$contractRefMode = $this->mappingModeForImport($import, 'contract.reference'); // e.g. 'keyref'
foreach ($rows as $idx => $rawValues) {
@@ -489,6 +495,38 @@ public function simulate(Import $import, int $limit = 100, bool $verbose = false
}
}
// History import: auto-ensure account placeholder when contract exists but no account mapping
if ($this->historyImport && $existingContract && isset($rowEntities['contract']['id']) && ! isset($rowEntities['account'])) {
if (! isset($summaries['account'])) {
$summaries['account'] = [
'root' => 'account',
'total_rows' => 0,
'create' => 0,
'update' => 0,
'missing_ref' => 0,
'invalid' => 0,
'duplicate' => 0,
'duplicate_db' => 0,
];
}
$summaries['account']['total_rows']++;
$summaries['account']['update']++;
$ref = $rowEntities['contract']['reference'] ?? null;
if ($ref === null || $ref === '') {
$ref = 'HIST-'.$rowEntities['contract']['id'];
}
$rowEntities['account'] = [
'reference' => $ref,
'exists' => true,
'id' => null,
'balance_before' => 0,
'balance_after' => 0,
'action' => 'implicit_history',
'action_label' => $translatedActions['implicit'] ?? 'posredno',
'history_zeroed' => true,
];
}
// Payment (affects account balance; may create implicit account)
if (isset($entityRoots['payment'])) {
// Inject inferred account if none mapped explicitly
@@ -891,7 +929,7 @@ private function simulateContract(callable $val, array $summaries, array $cache,
'client_case_id' => $contract?->client_case_id,
'active' => $contract?->active,
'deleted_at' => $contract?->deleted_at,
'action' => $contract ? 'update' : ($reference ? 'create' : 'skip'),
'action' => $contract ? ($this->historyImport ? 'skipped_history' : 'update') : ($reference ? 'create' : 'skip'),
];
$summaries['contract']['total_rows']++;
if (! $reference) {
@@ -902,6 +940,11 @@ private function simulateContract(callable $val, array $summaries, array $cache,
$summaries['contract']['create']++;
}
if ($this->historyImport && $contract) {
$entity['history_reuse'] = true;
$entity['message'] = 'Existing contract reused (history import)';
}
return [$entity, $summaries, $cache];
}
@@ -931,7 +974,7 @@ private function simulateAccount(callable $val, array $summaries, array $cache,
'exists' => (bool) $account,
'balance_before' => $account?->balance_amount,
'balance_after' => $account?->balance_amount,
'action' => $account ? 'update' : ($reference ? 'create' : 'skip'),
'action' => $account ? ($this->historyImport ? 'skipped_history' : 'update') : ($reference ? 'create' : 'skip'),
];
// Direct balance override support.
@@ -940,7 +983,7 @@ private function simulateAccount(callable $val, array $summaries, array $cache,
$rawIncoming = $val('account.balance_amount')
?? $val('accounts.balance_amount')
?? $val('account.balance');
if ($rawIncoming !== null && $rawIncoming !== '') {
if (! $this->historyImport && $rawIncoming !== null && $rawIncoming !== '') {
$rawStr = (string) $rawIncoming;
// Remove currency symbols and non numeric punctuation except , . -
$clean = preg_replace('/[^0-9,\.\-]+/', '', $rawStr) ?? '';
@@ -974,6 +1017,19 @@ private function simulateAccount(callable $val, array $summaries, array $cache,
$summaries['account']['create']++;
}
if ($this->historyImport) {
// History imports keep balances unchanged and do not update accounts
$entity['balance_after'] = $account?->balance_amount ?? 0;
$entity['balance_before'] = $account?->balance_amount ?? 0;
if ($account) {
$entity['message'] = 'Existing account left unchanged (history import)';
} else {
$entity['balance_after'] = 0;
$entity['balance_before'] = 0;
$entity['history_zeroed'] = true;
}
}
return [$entity, $summaries, $cache];
}
@@ -1210,6 +1266,10 @@ private function simulateGenericRoot(
$reference = $val('phone.nu');
} elseif ($root === 'email') {
$reference = $val('email.value');
} elseif ($root === 'activity') {
$noteRef = $val('activity.note');
$dueRef = $val('activity.due_date');
$reference = $noteRef || $dueRef ? trim((string) ($dueRef ?? '')).($noteRef ? ' | '.$noteRef : '') : null;
}
}
@@ -1253,6 +1313,13 @@ private function simulateGenericRoot(
$entity['description'] = $val('case_object.description') ?? null;
$entity['type'] = $val('case_object.type') ?? null;
break;
case 'activity':
$entity['note'] = $val('activity.note') ?? null;
$entity['due_date'] = $val('activity.due_date') ?? null;
$entity['amount'] = $val('activity.amount') ?? null;
$entity['action_id'] = $val('activity.action_id') ?? null;
$entity['decision_id'] = $val('activity.decision_id') ?? null;
break;
}
if ($verbose) {
@@ -1367,6 +1434,16 @@ private function genericIdentityCandidates(string $root, callable $val): array
$ids[] = 'name:'.mb_strtolower(trim((string) $name));
}
return $ids;
case 'activity':
$note = $val('activity.note');
$due = $val('activity.due_date');
$contractRef = $val('contract.reference');
$ids = [];
if ($note || $due) {
$ids[] = 'activity:'.mb_strtolower(trim((string) ($note ?? ''))).'|'.mb_strtolower(trim((string) ($due ?? ''))).'|'.mb_strtolower(trim((string) ($contractRef ?? '')));
}
return $ids;
default:
return [];
@@ -1426,6 +1503,20 @@ private function loadExistingGenericIdentities(string $root): array
}
}
break;
case 'activity':
foreach (\App\Models\Activity::query()->get(['note', 'due_date', 'contract_id', 'client_case_id']) as $rec) {
$note = mb_strtolower(trim((string) ($rec->note ?? '')));
$due = $rec->due_date ? mb_strtolower(trim((string) $rec->due_date)) : '';
$contractRef = null;
if ($rec->contract_id) {
$contractRef = Contract::where('id', $rec->contract_id)->value('reference');
}
$key = 'activity:'.$note.'|'.$due.'|'.mb_strtolower(trim((string) ($contractRef ?? '')));
if (trim($key, 'activity:|') !== '') {
$set[$key] = true;
}
}
break;
}
} catch (\Throwable) {
// swallow and return what we have
@@ -1730,6 +1821,8 @@ private function actionTranslations(): array
'skip' => 'preskoči',
'implicit' => 'posredno',
'reactivate' => 'reaktiviraj',
'skipped_history' => 'preskoči (zgodovina)',
'implicit_history' => 'posredno (zgodovina)',
];
}
+2 -2
View File
@@ -5,7 +5,6 @@
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"tijsverkoyen/css-to-inline-styles": "^2.2",
"php": "^8.2",
"arielmejiadev/larapex-charts": "^2.1",
"diglactic/laravel-breadcrumbs": "^10.0",
@@ -19,7 +18,8 @@
"maatwebsite/excel": "^3.1",
"meilisearch/meilisearch-php": "^1.11",
"robertboes/inertia-breadcrumbs": "dev-laravel-12",
"tightenco/ziggy": "^2.0"
"tightenco/ziggy": "^2.0",
"tijsverkoyen/css-to-inline-styles": "^2.2"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Generated
+591 -2
View File
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "51fd57123c1b9f51c24f28e04a692ec4",
"content-hash": "d29c47a4d6813ee8e80a7c8112c2f17e",
"packages": [
{
"name": "arielmejiadev/larapex-charts",
@@ -242,6 +242,162 @@
],
"time": "2024-02-09T16:56:22+00:00"
},
{
"name": "composer/pcre",
"version": "3.3.2",
"source": {
"type": "git",
"url": "https://github.com/composer/pcre.git",
"reference": "b2bed4734f0cc156ee1fe9c0da2550420d99a21e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/composer/pcre/zipball/b2bed4734f0cc156ee1fe9c0da2550420d99a21e",
"reference": "b2bed4734f0cc156ee1fe9c0da2550420d99a21e",
"shasum": ""
},
"require": {
"php": "^7.4 || ^8.0"
},
"conflict": {
"phpstan/phpstan": "<1.11.10"
},
"require-dev": {
"phpstan/phpstan": "^1.12 || ^2",
"phpstan/phpstan-strict-rules": "^1 || ^2",
"phpunit/phpunit": "^8 || ^9"
},
"type": "library",
"extra": {
"phpstan": {
"includes": [
"extension.neon"
]
},
"branch-alias": {
"dev-main": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"Composer\\Pcre\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jordi Boggiano",
"email": "j.boggiano@seld.be",
"homepage": "http://seld.be"
}
],
"description": "PCRE wrapping library that offers type-safe preg_* replacements.",
"keywords": [
"PCRE",
"preg",
"regex",
"regular expression"
],
"support": {
"issues": "https://github.com/composer/pcre/issues",
"source": "https://github.com/composer/pcre/tree/3.3.2"
},
"funding": [
{
"url": "https://packagist.com",
"type": "custom"
},
{
"url": "https://github.com/composer",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/composer/composer",
"type": "tidelift"
}
],
"time": "2024-11-12T16:29:46+00:00"
},
{
"name": "composer/semver",
"version": "3.4.4",
"source": {
"type": "git",
"url": "https://github.com/composer/semver.git",
"reference": "198166618906cb2de69b95d7d47e5fa8aa1b2b95"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/composer/semver/zipball/198166618906cb2de69b95d7d47e5fa8aa1b2b95",
"reference": "198166618906cb2de69b95d7d47e5fa8aa1b2b95",
"shasum": ""
},
"require": {
"php": "^5.3.2 || ^7.0 || ^8.0"
},
"require-dev": {
"phpstan/phpstan": "^1.11",
"symfony/phpunit-bridge": "^3 || ^7"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"Composer\\Semver\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nils Adermann",
"email": "naderman@naderman.de",
"homepage": "http://www.naderman.de"
},
{
"name": "Jordi Boggiano",
"email": "j.boggiano@seld.be",
"homepage": "http://seld.be"
},
{
"name": "Rob Bast",
"email": "rob.bast@gmail.com",
"homepage": "http://robbast.nl"
}
],
"description": "Semver library that offers utilities, version constraint parsing and validation.",
"keywords": [
"semantic",
"semver",
"validation",
"versioning"
],
"support": {
"irc": "ircs://irc.libera.chat:6697/composer",
"issues": "https://github.com/composer/semver/issues",
"source": "https://github.com/composer/semver/tree/3.4.4"
},
"funding": [
{
"url": "https://packagist.com",
"type": "custom"
},
{
"url": "https://github.com/composer",
"type": "github"
}
],
"time": "2025-08-20T19:15:30+00:00"
},
{
"name": "dasprid/enum",
"version": "1.0.6",
@@ -737,6 +893,67 @@
],
"time": "2025-03-06T22:45:56+00:00"
},
{
"name": "ezyang/htmlpurifier",
"version": "v4.19.0",
"source": {
"type": "git",
"url": "https://github.com/ezyang/htmlpurifier.git",
"reference": "b287d2a16aceffbf6e0295559b39662612b77fcf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ezyang/htmlpurifier/zipball/b287d2a16aceffbf6e0295559b39662612b77fcf",
"reference": "b287d2a16aceffbf6e0295559b39662612b77fcf",
"shasum": ""
},
"require": {
"php": "~5.6.0 || ~7.0.0 || ~7.1.0 || ~7.2.0 || ~7.3.0 || ~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0"
},
"require-dev": {
"cerdic/css-tidy": "^1.7 || ^2.0",
"simpletest/simpletest": "dev-master"
},
"suggest": {
"cerdic/css-tidy": "If you want to use the filter 'Filter.ExtractStyleBlocks'.",
"ext-bcmath": "Used for unit conversion and imagecrash protection",
"ext-iconv": "Converts text to and from non-UTF-8 encodings",
"ext-tidy": "Used for pretty-printing HTML"
},
"type": "library",
"autoload": {
"files": [
"library/HTMLPurifier.composer.php"
],
"psr-0": {
"HTMLPurifier": "library/"
},
"exclude-from-classmap": [
"/library/HTMLPurifier/Language/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-2.1-or-later"
],
"authors": [
{
"name": "Edward Z. Yang",
"email": "admin@htmlpurifier.org",
"homepage": "http://ezyang.com"
}
],
"description": "Standards compliant HTML filter written in PHP",
"homepage": "http://htmlpurifier.org/",
"keywords": [
"html"
],
"support": {
"issues": "https://github.com/ezyang/htmlpurifier/issues",
"source": "https://github.com/ezyang/htmlpurifier/tree/v4.19.0"
},
"time": "2025-10-17T16:34:55+00:00"
},
{
"name": "facade/ignition-contracts",
"version": "1.0.2",
@@ -2695,6 +2912,272 @@
],
"time": "2024-12-08T08:18:47+00:00"
},
{
"name": "maatwebsite/excel",
"version": "3.1.67",
"source": {
"type": "git",
"url": "https://github.com/SpartnerNL/Laravel-Excel.git",
"reference": "e508e34a502a3acc3329b464dad257378a7edb4d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/SpartnerNL/Laravel-Excel/zipball/e508e34a502a3acc3329b464dad257378a7edb4d",
"reference": "e508e34a502a3acc3329b464dad257378a7edb4d",
"shasum": ""
},
"require": {
"composer/semver": "^3.3",
"ext-json": "*",
"illuminate/support": "5.8.*||^6.0||^7.0||^8.0||^9.0||^10.0||^11.0||^12.0",
"php": "^7.0||^8.0",
"phpoffice/phpspreadsheet": "^1.30.0",
"psr/simple-cache": "^1.0||^2.0||^3.0"
},
"require-dev": {
"laravel/scout": "^7.0||^8.0||^9.0||^10.0",
"orchestra/testbench": "^6.0||^7.0||^8.0||^9.0||^10.0",
"predis/predis": "^1.1"
},
"type": "library",
"extra": {
"laravel": {
"aliases": {
"Excel": "Maatwebsite\\Excel\\Facades\\Excel"
},
"providers": [
"Maatwebsite\\Excel\\ExcelServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Maatwebsite\\Excel\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Patrick Brouwers",
"email": "patrick@spartner.nl"
}
],
"description": "Supercharged Excel exports and imports in Laravel",
"keywords": [
"PHPExcel",
"batch",
"csv",
"excel",
"export",
"import",
"laravel",
"php",
"phpspreadsheet"
],
"support": {
"issues": "https://github.com/SpartnerNL/Laravel-Excel/issues",
"source": "https://github.com/SpartnerNL/Laravel-Excel/tree/3.1.67"
},
"funding": [
{
"url": "https://laravel-excel.com/commercial-support",
"type": "custom"
},
{
"url": "https://github.com/patrickbrouwers",
"type": "github"
}
],
"time": "2025-08-26T09:13:16+00:00"
},
{
"name": "maennchen/zipstream-php",
"version": "3.2.1",
"source": {
"type": "git",
"url": "https://github.com/maennchen/ZipStream-PHP.git",
"reference": "682f1098a8fddbaf43edac2306a691c7ad508ec5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maennchen/ZipStream-PHP/zipball/682f1098a8fddbaf43edac2306a691c7ad508ec5",
"reference": "682f1098a8fddbaf43edac2306a691c7ad508ec5",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"ext-zlib": "*",
"php-64bit": "^8.3"
},
"require-dev": {
"brianium/paratest": "^7.7",
"ext-zip": "*",
"friendsofphp/php-cs-fixer": "^3.86",
"guzzlehttp/guzzle": "^7.5",
"mikey179/vfsstream": "^1.6",
"php-coveralls/php-coveralls": "^2.5",
"phpunit/phpunit": "^12.0",
"vimeo/psalm": "^6.0"
},
"suggest": {
"guzzlehttp/psr7": "^2.4",
"psr/http-message": "^2.0"
},
"type": "library",
"autoload": {
"psr-4": {
"ZipStream\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Paul Duncan",
"email": "pabs@pablotron.org"
},
{
"name": "Jonatan Männchen",
"email": "jonatan@maennchen.ch"
},
{
"name": "Jesse Donat",
"email": "donatj@gmail.com"
},
{
"name": "András Kolesár",
"email": "kolesar@kolesar.hu"
}
],
"description": "ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.",
"keywords": [
"stream",
"zip"
],
"support": {
"issues": "https://github.com/maennchen/ZipStream-PHP/issues",
"source": "https://github.com/maennchen/ZipStream-PHP/tree/3.2.1"
},
"funding": [
{
"url": "https://github.com/maennchen",
"type": "github"
}
],
"time": "2025-12-10T09:58:31+00:00"
},
{
"name": "markbaker/complex",
"version": "3.0.2",
"source": {
"type": "git",
"url": "https://github.com/MarkBaker/PHPComplex.git",
"reference": "95c56caa1cf5c766ad6d65b6344b807c1e8405b9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/MarkBaker/PHPComplex/zipball/95c56caa1cf5c766ad6d65b6344b807c1e8405b9",
"reference": "95c56caa1cf5c766ad6d65b6344b807c1e8405b9",
"shasum": ""
},
"require": {
"php": "^7.2 || ^8.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "dev-master",
"phpcompatibility/php-compatibility": "^9.3",
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
"squizlabs/php_codesniffer": "^3.7"
},
"type": "library",
"autoload": {
"psr-4": {
"Complex\\": "classes/src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mark Baker",
"email": "mark@lange.demon.co.uk"
}
],
"description": "PHP Class for working with complex numbers",
"homepage": "https://github.com/MarkBaker/PHPComplex",
"keywords": [
"complex",
"mathematics"
],
"support": {
"issues": "https://github.com/MarkBaker/PHPComplex/issues",
"source": "https://github.com/MarkBaker/PHPComplex/tree/3.0.2"
},
"time": "2022-12-06T16:21:08+00:00"
},
{
"name": "markbaker/matrix",
"version": "3.0.1",
"source": {
"type": "git",
"url": "https://github.com/MarkBaker/PHPMatrix.git",
"reference": "728434227fe21be27ff6d86621a1b13107a2562c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/MarkBaker/PHPMatrix/zipball/728434227fe21be27ff6d86621a1b13107a2562c",
"reference": "728434227fe21be27ff6d86621a1b13107a2562c",
"shasum": ""
},
"require": {
"php": "^7.1 || ^8.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "dev-master",
"phpcompatibility/php-compatibility": "^9.3",
"phpdocumentor/phpdocumentor": "2.*",
"phploc/phploc": "^4.0",
"phpmd/phpmd": "2.*",
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
"sebastian/phpcpd": "^4.0",
"squizlabs/php_codesniffer": "^3.7"
},
"type": "library",
"autoload": {
"psr-4": {
"Matrix\\": "classes/src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mark Baker",
"email": "mark@demon-angel.eu"
}
],
"description": "PHP Class for working with matrices",
"homepage": "https://github.com/MarkBaker/PHPMatrix",
"keywords": [
"mathematics",
"matrix",
"vector"
],
"support": {
"issues": "https://github.com/MarkBaker/PHPMatrix/issues",
"source": "https://github.com/MarkBaker/PHPMatrix/tree/3.0.1"
},
"time": "2022-12-02T22:17:43+00:00"
},
{
"name": "meilisearch/meilisearch-php",
"version": "v1.13.0",
@@ -3475,6 +3958,112 @@
},
"time": "2024-10-02T11:20:13+00:00"
},
{
"name": "phpoffice/phpspreadsheet",
"version": "1.30.1",
"source": {
"type": "git",
"url": "https://github.com/PHPOffice/PhpSpreadsheet.git",
"reference": "fa8257a579ec623473eabfe49731de5967306c4c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/fa8257a579ec623473eabfe49731de5967306c4c",
"reference": "fa8257a579ec623473eabfe49731de5967306c4c",
"shasum": ""
},
"require": {
"composer/pcre": "^1||^2||^3",
"ext-ctype": "*",
"ext-dom": "*",
"ext-fileinfo": "*",
"ext-gd": "*",
"ext-iconv": "*",
"ext-libxml": "*",
"ext-mbstring": "*",
"ext-simplexml": "*",
"ext-xml": "*",
"ext-xmlreader": "*",
"ext-xmlwriter": "*",
"ext-zip": "*",
"ext-zlib": "*",
"ezyang/htmlpurifier": "^4.15",
"maennchen/zipstream-php": "^2.1 || ^3.0",
"markbaker/complex": "^3.0",
"markbaker/matrix": "^3.0",
"php": ">=7.4.0 <8.5.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "dev-main",
"dompdf/dompdf": "^1.0 || ^2.0 || ^3.0",
"friendsofphp/php-cs-fixer": "^3.2",
"mitoteam/jpgraph": "^10.3",
"mpdf/mpdf": "^8.1.1",
"phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^8.5 || ^9.0",
"squizlabs/php_codesniffer": "^3.7",
"tecnickcom/tcpdf": "^6.5"
},
"suggest": {
"dompdf/dompdf": "Option for rendering PDF with PDF Writer",
"ext-intl": "PHP Internationalization Functions",
"mitoteam/jpgraph": "Option for rendering charts, or including charts with PDF or HTML Writers",
"mpdf/mpdf": "Option for rendering PDF with PDF Writer",
"tecnickcom/tcpdf": "Option for rendering PDF with PDF Writer"
},
"type": "library",
"autoload": {
"psr-4": {
"PhpOffice\\PhpSpreadsheet\\": "src/PhpSpreadsheet"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Maarten Balliauw",
"homepage": "https://blog.maartenballiauw.be"
},
{
"name": "Mark Baker",
"homepage": "https://markbakeruk.net"
},
{
"name": "Franck Lefevre",
"homepage": "https://rootslabs.net"
},
{
"name": "Erik Tilt"
},
{
"name": "Adrien Crivelli"
}
],
"description": "PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine",
"homepage": "https://github.com/PHPOffice/PhpSpreadsheet",
"keywords": [
"OpenXML",
"excel",
"gnumeric",
"ods",
"php",
"spreadsheet",
"xls",
"xlsx"
],
"support": {
"issues": "https://github.com/PHPOffice/PhpSpreadsheet/issues",
"source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/1.30.1"
},
"time": "2025-10-26T16:01:04+00:00"
},
{
"name": "phpoption/phpoption",
"version": "1.9.3",
@@ -10335,6 +10924,6 @@
"platform": {
"php": "^8.2"
},
"platform-dev": {},
"platform-dev": [],
"plugin-api-version": "2.6.0"
}
@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('field_jobs', function (Blueprint $table) {
$table->boolean('added_activity')->default(false);
$table->timestamp('last_activity')->nullable()->default(null);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('field_jobs', function (Blueprint $table) {
$table->dropColumn('last_activity');
$table->dropColumn('added_activity');
});
}
};
@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('person', function (Blueprint $table){
$table->string('employer', 125)->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('person', function (Blueprint $table){
$table->dropColumn('employer');
});
}
};
@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
// Add a generated tsvector column for fulltext search
DB::statement("
ALTER TABLE person_addresses
ADD COLUMN search_vector tsvector
GENERATED ALWAYS AS (
to_tsvector('simple',
coalesce(address, '') || ' ' ||
coalesce(post_code, '') || ' ' ||
coalesce(city, '')
)
) STORED
");
// Create GIN index on the tsvector column for fast fulltext search
DB::statement('CREATE INDEX person_addresses_search_vector_idx ON person_addresses USING GIN(search_vector)');
}
public function down(): void
{
Schema::table('person_addresses', function (Blueprint $table) {
$table->dropIndex('person_addresses_search_vector_idx');
$table->dropColumn('search_vector');
});
}
};
+25 -1
View File
@@ -14,7 +14,7 @@ public function run(): void
'key' => 'person',
'canonical_root' => 'person',
'label' => 'Person',
'fields' => ['first_name', 'last_name', 'full_name', 'gender', 'birthday', 'tax_number', 'social_security_number', 'description'],
'fields' => ['first_name', 'last_name', 'full_name', 'gender', 'birthday', 'tax_number', 'social_security_number', 'description', 'employer'],
'field_aliases' => [
'dob' => 'birthday',
'date_of_birth' => 'birthday',
@@ -30,6 +30,7 @@ public function run(): void
['pattern' => '/^(spol|gender)\b/i', 'field' => 'gender'],
['pattern' => '/^(rojstvo|datum\s*rojstva|dob|birth|birthday|date\s*of\s*birth)\b/i', 'field' => 'birthday'],
['pattern' => '/^(komentar|opis|opomba|comment|description|note)\b/i', 'field' => 'description'],
['pattern' => '/^(delodajalec|služba)\b/i', 'field' => 'employer']
],
'ui' => ['order' => 1],
],
@@ -175,6 +176,29 @@ public function run(): void
],
'ui' => ['order' => 9],
],
[
'key' => 'activities',
'canonical_root' => 'activity',
'label' => 'Activities',
'fields' => ['note', 'due_date', 'amount', 'action_id', 'decision_id', 'contract_id', 'client_case_id', 'user_id'],
'field_aliases' => [
'opis' => 'note',
'datum' => 'due_date',
'rok' => 'due_date',
'znesek' => 'amount',
],
'aliases' => ['activity', 'activities', 'opravilo', 'opravila'],
'rules' => [
['pattern' => '/^(aktivnost|activity|note|opis)\b/i', 'field' => 'note'],
['pattern' => '/^(rok|due|datum|date)\b/i', 'field' => 'due_date'],
['pattern' => '/^(znesek|amount|vrednost|value)\b/i', 'field' => 'amount'],
['pattern' => '/^(akcija|action)\b/i', 'field' => 'action_id'],
['pattern' => '/^(odlocitev|odločitev|decision)\b/i', 'field' => 'decision_id'],
['pattern' => '/^(pogodba|contract)\b/i', 'field' => 'contract_id'],
['pattern' => '/^(primer|case)\b/i', 'field' => 'client_case_id'],
],
'ui' => ['order' => 10],
],
];
foreach ($defs as $d) {
+37
View File
@@ -155,5 +155,42 @@ public function run(): void
'options' => $map['options'] ?? null,
]);
}
// Activities linked to contracts demo
$activities = ImportTemplate::query()->firstOrCreate([
'name' => 'Activities CSV (contract linked)',
], [
'uuid' => (string) Str::uuid(),
'description' => 'Activities import linked to existing contracts via reference.',
'source_type' => 'csv',
'default_record_type' => 'activity',
'sample_headers' => ['contract_reference', 'note', 'due_date', 'amount', 'action', 'decision', 'user_email'],
'is_active' => true,
'meta' => [
'delimiter' => ',',
'enclosure' => '"',
'escape' => '\\',
],
]);
$activityMappings = [
['source_column' => 'contract_reference', 'target_field' => 'contract.reference', 'position' => 1],
['source_column' => 'note', 'target_field' => 'activity.note', 'position' => 2],
['source_column' => 'due_date', 'target_field' => 'activity.due_date', 'position' => 3],
['source_column' => 'amount', 'target_field' => 'activity.amount', 'position' => 4],
['source_column' => 'action', 'target_field' => 'activity.action_id', 'position' => 5],
['source_column' => 'decision', 'target_field' => 'activity.decision_id', 'position' => 6],
];
foreach ($activityMappings as $map) {
ImportTemplateMapping::firstOrCreate([
'import_template_id' => $activities->id,
'source_column' => $map['source_column'],
], [
'target_field' => $map['target_field'],
'position' => $map['position'],
'options' => $map['options'] ?? null,
]);
}
}
}
+66 -10
View File
@@ -580,6 +580,19 @@ const openSmsDialog = (phone) => {
// Load contracts for this case (for contract/account placeholders)
loadContractsForCase();
};
// Format YYYY-MM-DD (or ISO date) to dd.mm.yyyy
function formatDate(value) {
if (!value) return "-";
try {
const iso = String(value).split("T")[0];
const parts = iso.split("-");
if (parts.length !== 3) return value;
const [y, m, d] = parts;
return `${d.padStart(2, "0")}.${m.padStart(2, "0")}.${y}`;
} catch (e) {
return value;
}
}
const loadContractsForCase = async () => {
try {
const url = route("clientCase.contracts.list", { client_case: props.clientCaseUuid });
@@ -640,43 +653,86 @@ const submitSms = () => {
</div>
<div class="grid grid-rows-* grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-2">
<div class="rounded p-2 shadow">
<p class="text-xs leading-5 md:text-sm text-gray-500">Nu.</p>
<p class="text-xs leading-5 md:text-sm text-gray-500">Primer ref.</p>
<p class="text-sm md:text-base leading-7 text-gray-900">{{ person.nu }}</p>
</div>
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Name.</p>
<p class="text-sm leading-5 md:text-sm text-gray-500">Naziv</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ person.full_name }}
</p>
</div>
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Tax NU.</p>
<p class="text-sm leading-5 md:text-sm text-gray-500">Davčna</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ person.tax_number }}
</p>
</div>
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Social security NU.</p>
<p class="text-sm leading-5 md:text-sm text-gray-500">Emšo</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ person.social_security_number }}
</p>
</div>
</div>
<div class="grid grid-rows-* grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2 mt-1">
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Address</p>
<div
v-if="clientCaseUuid"
class="grid grid-rows-* grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2 mt-1"
>
<div class="col-span-full lg:col-span-1 rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Naslov</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ getMainAddress(person.addresses) }}
</p>
</div>
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Phone</p>
<p class="text-sm leading-5 md:text-sm text-gray-500">Telefon</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ getMainPhone(person.phones) }}
</p>
</div>
<div class="md:col-span-full lg:col-span-1 rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Description</p>
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Datum rojstva</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ formatDate(person.birthday) }}
</p>
</div>
</div>
<div v-else class="grid grid-rows-* grid-cols-1 md:grid-cols-2 gap-2 mt-1">
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Naslov</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ getMainAddress(person.addresses) }}
</p>
</div>
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Telefon</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ getMainPhone(person.phones) }}
</p>
</div>
</div>
<div
v-if="clientCaseUuid"
class="grid grid-rows-* grid-cols-1 lg:grid-cols-2 gap-2 mt-1"
>
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Delodajalec</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ person.employer }}
</p>
</div>
<div class="rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Opis</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ person.description }}
</p>
</div>
</div>
<div v-else class="grid grid-rows-* grid-cols-1 md:grid-cols-2 gap-2 mt-1">
<div class="col-span-full rounded p-2 shadow">
<p class="text-sm leading-5 md:text-sm text-gray-500">Opis</p>
<p class="text-sm md:text-base leading-7 text-gray-900">
{{ person.description }}
</p>
@@ -113,6 +113,7 @@ const store = async () => {
form
.transform((data) => ({
...data,
phone_view: props.phoneMode,
due_date: formatDateForSubmit(data.due_date),
attachment_document_ids:
templateAllowsAttachments.value && data.attach_documents
+265 -1
View File
@@ -1,7 +1,8 @@
<script setup>
import AppLayout from "@/Layouts/AppLayout.vue";
import { ref } from "vue";
import { ref, computed } from "vue";
import { Link, router, useForm } from "@inertiajs/vue3";
import axios from "axios";
import DataTableServer from "@/Components/DataTable/DataTableServer.vue";
import PersonInfoGrid from "@/Components/PersonInfoGrid.vue";
import SectionTitle from "@/Components/SectionTitle.vue";
@@ -31,6 +32,30 @@ const showSegmentModal = ref(false);
const targetSegmentId = ref(null);
const segmentForm = useForm({ segment_id: null, contracts: [] });
const exportDialogOpen = ref(false);
const exportScope = ref("current");
const exportColumns = ref(["reference", "customer", "address", "start", "segment", "balance"]);
const exportError = ref("");
const isExporting = ref(false);
const exportableColumns = [
{ key: "reference", label: "Referenca" },
{ key: "customer", label: "Stranka" },
{ key: "address", label: "Naslov" },
{ key: "start", label: "Začetek" },
{ key: "segment", label: "Segment" },
{ key: "balance", label: "Stanje" },
];
const allColumnsSelected = computed(
() => exportColumns.value.length === exportableColumns.length
);
const exportDisabled = computed(
() => exportColumns.value.length === 0 || isExporting.value
);
const currentPageCount = computed(() => props.contracts?.data?.length ?? 0);
const totalContracts = computed(() => props.contracts?.total ?? 0);
function toggleSelectAll() {
if (selectedRows.value.length === props.contracts.data.length) {
selectedRows.value = [];
@@ -149,6 +174,128 @@ function formatDate(value) {
return value;
}
}
function toggleAllColumns(checked) {
exportColumns.value = checked ? exportableColumns.map((col) => col.key) : [];
}
function openExportDialog() {
exportDialogOpen.value = true;
exportError.value = "";
}
function closeExportDialog() {
exportDialogOpen.value = false;
}
async function submitExport() {
if (exportColumns.value.length === 0) {
exportError.value = "Izberi vsaj en stolpec.";
return;
}
try {
exportError.value = "";
isExporting.value = true;
const payload = {
scope: exportScope.value,
columns: [...exportColumns.value],
from: fromDate.value || "",
to: toDate.value || "",
search: search.value || "",
segments:
selectedSegments.value.length > 0
? selectedSegments.value.map((s) => s.id).join(",")
: "",
page: props.contracts.current_page,
per_page: props.contracts.per_page,
};
const response = await axios.post(
route("client.contracts.export", { uuid: props.client.uuid }),
payload,
{ responseType: "blob" }
);
const blob = new Blob([response.data], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
const filename =
extractFilenameFromHeaders(response.headers) || buildDefaultFilename();
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
exportDialogOpen.value = false;
} catch (error) {
console.error("Export error:", error);
console.error("Error response:", error.response);
let errorMessage = "Izvoz je spodletel. Poskusi znova.";
if (error.response?.status === 404) {
errorMessage = "Pot za izvoz ne obstaja. Prosim kontaktiraj administratorja.";
} else if (error.response?.status === 500) {
errorMessage = "Napaka na strežniku. Poskusi znova.";
} else if (error.response?.data) {
try {
const text = await error.response.data.text();
const json = JSON.parse(text);
errorMessage = json.message || errorMessage;
} catch (e) {
console.error("Could not parse error response:", e);
}
}
exportError.value = errorMessage;
} finally {
isExporting.value = false;
}
}
function slugify(value) {
if (!value) {
return "data";
}
const slug = value.replace(/[^a-z0-9]+/gi, "-").replace(/^-+|-+$/g, "");
return slug || "data";
}
function buildDefaultFilename() {
const now = new Date();
const dd = String(now.getDate()).padStart(2, "0");
const mm = String(now.getMonth() + 1).padStart(2, "0");
const yy = String(now.getFullYear()).slice(-2);
const clientName = props.client?.person?.full_name || "stranka";
return `${dd}${mm}${yy}_${slugify(clientName)}-Pogodbe.xlsx`;
}
function extractFilenameFromHeaders(headers) {
if (!headers) {
return null;
}
const disposition =
headers["content-disposition"] || headers["Content-Disposition"] || "";
if (!disposition) {
return null;
}
const utf8Match = disposition.match(/filename\*=UTF-8''([^;]+)/i);
if (utf8Match?.[1]) {
try {
return decodeURIComponent(utf8Match[1]);
} catch (error) {
return utf8Match[1];
}
}
const asciiMatch = disposition.match(/filename="?([^";]+)"?/i);
return asciiMatch?.[1] || null;
}
</script>
<template>
@@ -299,6 +446,7 @@ function formatDate(value) {
{ key: 'select', label: '', sortable: false, width: '50px' },
{ key: 'reference', label: 'Referenca', sortable: false },
{ key: 'customer', label: 'Stranka', sortable: false },
{ key: 'address', label: 'Naslov', sortable: false },
{ key: 'start', label: 'Začetek', sortable: false },
{ key: 'segment', label: 'Segment', sortable: false },
{ key: 'balance', label: 'Stanje', sortable: false, align: 'right' },
@@ -325,6 +473,14 @@ function formatDate(value) {
:only-props="['contracts']"
>
<template #toolbar-extra>
<div class="flex items-center gap-2">
<button
type="button"
class="inline-flex items-center rounded-md border border-indigo-200 bg-white px-3 py-2 text-sm font-medium text-indigo-700 shadow-sm hover:bg-indigo-50"
@click="openExportDialog"
>
Izvozi v Excel
</button>
<div v-if="selectedRows.length" class="flex items-center gap-2">
<div class="text-sm text-gray-700">
Izbrano: <span class="font-medium">{{ selectedRows.length }}</span>
@@ -361,6 +517,7 @@ function formatDate(value) {
</template>
</Dropdown>
</div>
</div>
</template>
<template #header-select>
<input
@@ -390,6 +547,9 @@ function formatDate(value) {
<template #cell-customer="{ row }">
{{ row.client_case?.person?.full_name || "-" }}
</template>
<template #cell-address="{ row }">
{{ row.client_case?.person?.address?.address || "-" }}
</template>
<template #cell-start="{ row }">
{{ formatDate(row.start_date) }}
</template>
@@ -462,6 +622,110 @@ function formatDate(value) {
</button>
</template>
</DialogModal>
<!-- Export Dialog Modal -->
<DialogModal
:show="exportDialogOpen"
max-width="3xl"
@close="closeExportDialog"
>
<template #title>
<div>
<h3 class="text-lg font-semibold">Izvoz v Excel</h3>
<p class="text-sm text-gray-500">
Izberi stolpce in obseg podatkov za izvoz.
</p>
</div>
</template>
<template #content>
<form
id="contract-export-form"
class="space-y-6"
@submit.prevent="submitExport"
>
<div>
<span class="text-sm font-semibold text-gray-700"
>Obseg podatkov</span
>
<div class="mt-2 space-y-2">
<label class="flex items-center gap-2 text-sm text-gray-700">
<input
type="radio"
name="scope"
value="current"
class="text-indigo-600"
v-model="exportScope"
/>
Trenutna stran ({{ currentPageCount }} zapisov)
</label>
<label class="flex items-center gap-2 text-sm text-gray-700">
<input
type="radio"
name="scope"
value="all"
class="text-indigo-600"
v-model="exportScope"
/>
Vse pogodbe ({{ totalContracts }} zapisov)
</label>
</div>
</div>
<div>
<div class="flex items-center justify-between">
<span class="text-sm font-semibold text-gray-700">Stolpci</span>
<label class="flex items-center gap-2 text-xs text-gray-600">
<input
type="checkbox"
:checked="allColumnsSelected"
@change="toggleAllColumns($event.target.checked)"
/>
Označi vse
</label>
</div>
<div class="mt-3 grid grid-cols-1 gap-2 sm:grid-cols-2">
<label
v-for="col in exportableColumns"
:key="col.key"
class="flex items-center gap-2 rounded border border-gray-200 px-3 py-2 text-sm"
>
<input
type="checkbox"
name="columns[]"
:value="col.key"
v-model="exportColumns"
class="text-indigo-600"
/>
{{ col.label }}
</label>
</div>
<p v-if="exportError" class="mt-2 text-sm text-red-600">
{{ exportError }}
</p>
</div>
</form>
</template>
<template #footer>
<div class="flex flex-row gap-2">
<button
type="button"
class="text-sm text-gray-600 hover:text-gray-900"
@click="closeExportDialog"
>
Prekliči
</button>
<button
type="submit"
form="contract-export-form"
class="inline-flex items-center rounded-md bg-indigo-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 disabled:cursor-not-allowed disabled:opacity-60"
:disabled="exportDisabled"
>
<span v-if="!isExporting">Prenesi Excel</span>
<span v-else>Pripravljam ...</span>
</button>
</div>
</template>
</DialogModal>
</div>
<!-- Pagination handled by DataTableServer -->
</div>
+72
View File
@@ -15,6 +15,7 @@ import Modal from "@/Components/Modal.vue"; // still potentially used elsewhere
import CsvPreviewModal from "./Partials/CsvPreviewModal.vue";
import SimulationModal from "./Partials/SimulationModal.vue";
import { useCurrencyFormat } from "./useCurrencyFormat.js";
import DialogModal from "@/Components/DialogModal.vue";
// Reintroduce props definition lost during earlier edits
const props = defineProps({
@@ -185,6 +186,23 @@ function downloadUnresolvedCsv() {
window.location.href = route("imports.missing-keyref-csv", { import: importId.value });
}
// History import: list of contracts that already existed in DB and were matched
const isHistoryImport = computed(() => {
const foundList = props.import?.meta?.history_found_contracts;
const hasFound = Array.isArray(foundList) && foundList.length > 0;
return Boolean(
props.import?.template?.meta?.history_import ??
props.import?.import_template?.meta?.history_import ??
props.import?.meta?.history_import ??
hasFound
);
});
const historyFoundContracts = computed(() => {
const list = props.import?.meta?.history_found_contracts;
return Array.isArray(list) ? list : [];
});
const showFoundContracts = ref(false);
// Determine if all detected columns are mapped with entity+field
function evaluateMappingSaved() {
console.log("here the evaluation happen of mapping save!");
@@ -1145,6 +1163,21 @@ async function fetchSimulation() {
<div class="py-6">
<div class="max-w-5xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white shadow sm:rounded-lg p-6 space-y-6">
<div
v-if="isHistoryImport || historyFoundContracts.length"
class="flex flex-wrap items-center gap-2 text-sm"
>
<button
class="px-3 py-1.5 bg-emerald-700 text-white text-xs rounded"
@click.prevent="showFoundContracts = true"
title="Prikaži pogodbe, ki so bile najdene in že obstajajo v bazi"
>
Najdene pogodbe
</button>
<span v-if="historyFoundContracts.length" class="text-xs text-gray-600">
{{ historyFoundContracts.length }} že obstoječih
</span>
</div>
<div v-if="isCompleted" class="p-3 border rounded bg-gray-50 text-sm">
<div class="flex flex-wrap gap-x-6 gap-y-1">
<div>
@@ -1378,6 +1411,45 @@ async function fetchSimulation() {
</div>
</Modal>
<!-- History import: existing contracts found -->
<DialogModal :show="showFoundContracts" max-width="3xl" @close="showFoundContracts = false">
<template #title>Obstoječe pogodbe najdene v zgodovinskem uvozu</template>
<template #content>
<div v-if="!historyFoundContracts.length" class="text-sm text-gray-600">Ni zadetkov.</div>
<ul v-else class="divide-y divide-gray-200 max-h-[70vh] overflow-auto">
<li
v-for="item in historyFoundContracts"
:key="item.contract_uuid || item.reference"
class="py-3 flex items-center justify-between gap-4"
>
<div class="min-w-0">
<div class="font-mono text-sm text-gray-900">{{ item.reference }}</div>
<div class="text-xs text-gray-600 truncate">
<span>{{ item.full_name || "" }}</span>
</div>
</div>
<div class="flex-shrink-0">
<a
v-if="item.case_uuid"
:href="route('clientCase.show', { client_case: item.case_uuid })"
class="text-blue-600 hover:underline text-xs"
>
Odpri primer
</a>
</div>
</li>
</ul>
</template>
<template #footer>
<button
class="px-3 py-1.5 bg-gray-700 text-white text-xs rounded"
@click.prevent="showFoundContracts = false"
>
Zapri
</button>
</template>
</DialogModal>
<!-- Unresolved keyref rows modal -->
<Modal :show="showUnresolved" max-width="5xl" @close="showUnresolved = false">
<div class="p-4 max-h-[75vh] overflow-auto">
@@ -28,6 +28,8 @@ const form = useForm({
delimiter: "",
// Payments import mode
payments_import: false,
// History import mode
history_import: false,
// For payments mode: how to locate Contract - use single key 'reference'
contract_key_mode: null,
},
@@ -59,6 +61,9 @@ const prevEntities = ref([]);
watch(
() => form.meta.payments_import,
(enabled) => {
if (enabled && form.meta.history_import) {
form.meta.history_import = false;
}
if (enabled) {
// Save current selection and lock to the required chain
prevEntities.value = Array.isArray(form.entities) ? [...form.entities] : [];
@@ -74,6 +79,35 @@ watch(
}
}
);
// History import: restrict entities and auto-add accounts when contracts selected
watch(
() => form.meta.history_import,
(enabled) => {
if (enabled && form.meta.payments_import) {
form.meta.payments_import = false;
form.meta.contract_key_mode = null;
}
const allowed = ["person", "person_addresses", "person_phones", "contracts", "activities", "client_cases"];
if (enabled) {
const current = Array.isArray(form.entities) ? [...form.entities] : [];
let filtered = current.filter((e) => allowed.includes(e));
if (filtered.includes("contracts") && !filtered.includes("accounts")) {
filtered = [...filtered, "accounts"];
}
form.entities = filtered;
}
}
);
watch(
() => form.entities,
(vals) => {
if (form.meta.history_import && Array.isArray(vals) && vals.includes("contracts") && ! vals.includes("accounts")) {
form.entities = [...vals, "accounts"];
}
}
);
</script>
<template>
@@ -112,7 +146,16 @@ watch(
<label class="block text-sm font-medium text-gray-700"
>Entities (tables)</label
>
<label class="inline-flex items-center gap-2 text-sm">
<div class="flex items-center gap-4 text-sm">
<label class="inline-flex items-center gap-2">
<input
type="checkbox"
v-model="form.meta.history_import"
class="rounded"
/>
<span>History import</span>
</label>
<label class="inline-flex items-center gap-2">
<input
type="checkbox"
v-model="form.meta.payments_import"
@@ -121,6 +164,7 @@ watch(
<span>Payments import</span>
</label>
</div>
</div>
<template v-if="!form.meta.payments_import">
<Multiselect
v-model="form.entities"
@@ -128,11 +172,13 @@ watch(
{ value: 'person', label: 'Person' },
{ value: 'person_addresses', label: 'Person Addresses' },
{ value: 'person_phones', label: 'Person Phones' },
{ value: 'client_cases', label: 'Client Cases' },
{ value: 'emails', label: 'Emails' },
{ value: 'accounts', label: 'Accounts' },
{ value: 'contracts', label: 'Contracts' },
{ value: 'case_objects', label: 'Case Objects' },
{ value: 'payments', label: 'Payments' },
{ value: 'activities', label: 'Activities' },
]"
:multiple="true"
track-by="value"
@@ -156,6 +202,9 @@ watch(
Choose which tables this template targets. You can still define per-column
mappings later.
</p>
<div v-if="form.meta.history_import" class="mt-2 text-xs text-gray-600">
History mode allows only person/address/phone/contracts/activities/client cases. Accounts are auto-added when contracts are present and balances stay unchanged.
</div>
<div v-if="form.meta.payments_import" class="mt-2 text-xs text-gray-600">
Payments mode locks entities to:
<span class="font-medium">Contracts Accounts Payments</span> and
+120 -23
View File
@@ -26,6 +26,10 @@ const form = useForm({
// Add meta with default delimiter support
meta: {
...(props.template.meta || {}),
payments_import: props.template.meta?.payments_import ?? false,
history_import: props.template.meta?.history_import ?? false,
activity_action_id: props.template.meta?.activity_action_id ?? props.template.meta?.action_id ?? null,
activity_decision_id: props.template.meta?.activity_decision_id ?? props.template.meta?.decision_id ?? null,
delimiter: (props.template.meta && props.template.meta.delimiter) || "",
},
});
@@ -35,6 +39,21 @@ const decisionsForSelectedAction = vComputed(() => {
return act?.decisions || [];
});
const decisionsForActivitiesAction = vComputed(() => {
const act = (props.actions || []).find((a) => a.id === form.meta.activity_action_id);
return act?.decisions || [];
});
const activityCreatedAtInput = computed({
get() {
if (!form.meta.activity_created_at) return "";
return String(form.meta.activity_created_at).replace(" ", "T");
},
set(v) {
form.meta.activity_created_at = v ? String(v).replace("T", " ") : null;
},
});
vWatch(
() => form.meta.action_id,
() => {
@@ -42,6 +61,13 @@ vWatch(
}
);
vWatch(
() => form.meta.activity_action_id,
() => {
form.meta.activity_decision_id = null;
}
);
const entities = computed(() => props.template.meta?.entities || []);
const hasMappings = computed(() => (props.template.mappings?.length || 0) > 0);
const canChangeClient = computed(() => !hasMappings.value); // guard reassignment when mappings exist (optional rule)
@@ -67,6 +93,15 @@ const unassignedSourceColumns = computed(() => {
}
return Array.from(set).sort((a, b) => a.localeCompare(b));
});
const allSourceColumns = computed(() => {
const set = new Set();
(props.template.sample_headers || []).forEach((h) => set.add(h));
(props.template.mappings || []).forEach((m) => {
if (m.source_column) set.add(m.source_column);
});
return Array.from(set).sort((a, b) => a.localeCompare(b));
});
const unassignedState = ref({});
// Dynamic Import Entity definitions and field options from API
@@ -252,6 +287,11 @@ const save = () => {
// drop client change when blocked
delete payload.client_uuid;
}
const hasActivities = Array.isArray(payload.meta?.entities) && payload.meta.entities.includes('activities');
if (hasActivities && (!payload.meta?.activity_action_id || !payload.meta?.activity_decision_id)) {
alert('Activity imports require selecting an Action and Decision (Activities section).');
return;
}
// Normalize empty delimiter: remove from meta to allow auto-detect
if (
payload.meta &&
@@ -300,11 +340,27 @@ watch(
watch(
() => form.meta.payments_import,
(enabled) => {
if (enabled) {
if (form.meta.history_import) {
form.meta.history_import = false;
}
}
if (enabled && !form.meta.contract_key_mode) {
form.meta.contract_key_mode = "reference";
}
}
);
// History mode is mutually exclusive with payments mode
watch(
() => form.meta.history_import,
(enabled) => {
if (enabled && form.meta.payments_import) {
form.meta.payments_import = false;
form.meta.contract_key_mode = null;
}
}
);
</script>
<template>
@@ -423,7 +479,7 @@ watch(
</div>
<div>
<label class="block text-sm font-medium text-gray-700"
>Privzeto Dejanja (Activity)</label
>Privzeto Dejanja (post-contract activity)</label
>
<select
v-model="form.meta.action_id"
@@ -437,7 +493,7 @@ watch(
</div>
<div>
<label class="block text-sm font-medium text-gray-700"
>Privzeta Odločitev</label
>Privzeta Odločitev (post-contract)</label
>
<select
v-model="form.meta.decision_id"
@@ -484,22 +540,31 @@ watch(
</div>
</div>
<!-- Payments import toggle and settings -->
<!-- History / Payments import toggles and settings -->
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div>
<div class="flex items-center gap-2">
<div class="space-y-2">
<div class="flex items-center flex-wrap gap-4">
<label class="inline-flex items-center gap-2">
<input
id="history_import"
v-model="form.meta.history_import"
type="checkbox"
class="rounded"
/>
<span class="text-sm font-medium text-gray-700">History import</span>
</label>
<label class="inline-flex items-center gap-2">
<input
id="payments_import"
v-model="form.meta.payments_import"
type="checkbox"
class="rounded"
/>
<label for="payments_import" class="text-sm font-medium text-gray-700"
>Payments import</label
>
<span class="text-sm font-medium text-gray-700">Payments import</span>
</label>
</div>
<p class="text-xs text-gray-500 mt-1">
When enabled, entities are locked to Contracts Accounts Payments.
<p class="text-xs text-gray-500">
History allows person/address/phone/contracts/activities/client cases; accounts are auto-added with contracts. Payments locks entities to Contracts Accounts Payments.
</p>
</div>
<div v-if="form.meta.payments_import">
@@ -856,6 +921,41 @@ watch(
<span class="text-xs text-gray-500">Klikni za razširitev</span>
</summary>
<div class="mt-4 space-y-4">
<div v-if="entity === 'activities'" class="grid grid-cols-1 sm:grid-cols-2 gap-4 p-3 bg-gray-50 rounded border">
<div>
<label class="block text-sm font-medium text-gray-700">Activity action *</label>
<select
v-model="form.meta.activity_action_id"
class="mt-1 block w-full border rounded p-2"
required
>
<option :value="null">(Select action)</option>
<option v-for="a in props.actions" :key="a.id" :value="a.id">{{ a.name }}</option>
</select>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Activity decision *</label>
<select
v-model="form.meta.activity_decision_id"
class="mt-1 block w-full border rounded p-2"
:disabled="!form.meta.activity_action_id"
required
>
<option :value="null">(Select decision)</option>
<option v-for="d in decisionsForActivitiesAction" :key="d.id" :value="d.id">{{ d.name }}</option>
</select>
<p v-if="!form.meta.activity_action_id" class="text-xs text-gray-500 mt-1">Choose an action first.</p>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Activity created at (override)</label>
<input
v-model="activityCreatedAtInput"
type="datetime-local"
class="mt-1 block w-full border rounded p-2"
/>
<p class="text-xs text-gray-500 mt-1">Optional: set a fixed timestamp for inserted activities (history imports).</p>
</div>
</div>
<!-- Existing mappings for this entity -->
<div
v-if="props.template.mappings && props.template.mappings.length"
@@ -954,22 +1054,19 @@ watch(
<div class="grid grid-cols-1 sm:grid-cols-6 gap-2 items-end">
<div>
<label class="block text-xs text-gray-600"
>Source column (ne-dodeljene)</label
>Source column (lahko uporabiš večkrat)</label
>
<select
<input
v-model="(newRows[entity] ||= {}).source"
class="mt-1 w-full border rounded p-2"
>
<option value="" disabled>(izberi)</option>
<option v-for="s in unassignedSourceColumns" :key="s" :value="s">
{{ s }}
</option>
</select>
<p
v-if="!unassignedSourceColumns.length"
class="text-xs text-gray-500 mt-1"
>
Ni nedodeljenih virov. Uporabi Bulk ali najprej dodaj vire.
list="src-opts-{{ entity }}"
placeholder="npr.: note, description"
/>
<datalist :id="`src-opts-${entity}`">
<option v-for="s in allSourceColumns" :key="s" :value="s">{{ s }}</option>
</datalist>
<p class="text-xs text-gray-500 mt-1">
Več stolpcev lahko povežeš na isto polje (npr. activity.note).
</p>
</div>
<div>
+98 -7
View File
@@ -1,5 +1,6 @@
<script setup>
import AppPhoneLayout from "@/Layouts/AppPhoneLayout.vue";
import { Separator } from "reka-ui";
import { computed, ref } from "vue";
const props = defineProps({
@@ -11,6 +12,9 @@ const items = computed(() => props.jobs || []);
// Client filter options derived from jobs
const clientFilter = ref("");
const listNonActivity = ref([]);
const listActivity = ref([]);
const clientOptions = computed(() => {
const map = new Map();
for (const job of items.value) {
@@ -28,7 +32,7 @@ const clientOptions = computed(() => {
const search = ref("");
const filteredJobs = computed(() => {
const term = search.value.trim().toLowerCase();
return items.value.filter((job) => {
const filterList = items.value.filter((job) => {
// Filter by selected client (if any)
if (clientFilter.value) {
const juuid = job?.contract?.client_case?.client?.uuid;
@@ -50,6 +54,9 @@ const filteredJobs = computed(() => {
refStr.includes(term) || nameStr.includes(term) || clientNameStr.includes(term)
);
});
listNonActivity.value = filterList.filter((item) => !item.added_activity);
listActivity.value = filterList.filter((item) => !!item.added_activity);
return filterList;
});
function formatDateDMY(d) {
@@ -125,10 +132,12 @@ function getCaseUuid(job) {
Počisti
</button>
</div>
<div class="grid grid-cols-1 gap-3 sm:gap-4 md:grid-cols-2 lg:grid-cols-3">
<template v-if="filteredJobs.length">
<h2 class="py-4">Nove / Ne obdelane</h2>
<div class="grid grid-cols-1 gap-3 sm:gap-4 md:grid-cols-2 lg:grid-cols-3">
<div
v-for="job in filteredJobs"
v-for="job in listNonActivity"
:key="job.id"
class="bg-white rounded-lg shadow border p-3 sm:p-4"
>
@@ -180,9 +189,6 @@ function getCaseUuid(job) {
<p class="text-sm text-gray-600 truncate">
Kontrakt: {{ job.contract?.reference || job.contract?.uuid }}
</p>
<p class="text-sm text-gray-600">
Tip: {{ job.contract?.type?.name || "—" }}
</p>
<p
class="text-sm text-gray-600"
v-if="
@@ -205,6 +211,92 @@ function getCaseUuid(job) {
</p>
</div>
</div>
</div>
<h2 class="py-4">Obdelane pogodbe</h2>
<div class="grid grid-cols-1 gap-3 sm:gap-4 md:grid-cols-2 lg:grid-cols-3">
<div
v-for="job in listActivity"
:key="job.id"
class="bg-white rounded-lg shadow border p-3 sm:p-4"
>
<div class="mb-4 flex gap-2">
<a
v-if="getCaseUuid(job)"
:href="
route('phone.case', {
client_case: getCaseUuid(job),
completed: props.view_mode === 'completed-today' ? 1 : undefined,
})
"
class="inline-flex-1 flex-1 text-center px-3 py-2 rounded-md bg-blue-600 text-white text-sm hover:bg-blue-700"
>
Odpri primer
</a>
<button
v-else
type="button"
disabled
class="inline-flex-1 flex-1 text-center px-3 py-2 rounded-md bg-gray-300 text-gray-600 text-sm cursor-not-allowed"
>
Manjka primer
</button>
</div>
<div class="flex items-center justify-between">
<p class="text-sm text-gray-500">
Dodeljeno:
<span class="font-medium text-gray-700">{{
formatDateDMY(job.assigned_at)
}}</span>
</p>
<span
v-if="job.priority"
class="inline-block text-xs px-2 py-0.5 rounded bg-amber-100 text-amber-700"
>Prioriteta</span
>
</div>
<div class="mt-2">
<p class="text-base sm:text-lg font-semibold text-gray-800">
{{ job.contract?.client_case?.person?.full_name || "—" }}
</p>
<p class="text-sm text-gray-600">
Naročnik:
<span class="font-semibold text-gray-800">
{{ job.contract?.client_case?.client?.person?.full_name || "—" }}
</span>
</p>
<p class="text-sm text-gray-600 truncate">
Kontrakt: {{ job.contract?.reference || job.contract?.uuid }}
</p>
<p
class="text-sm text-gray-600"
v-if="
job.contract?.account &&
job.contract.account.balance_amount !== null &&
job.contract.account.balance_amount !== undefined
"
>
Odprto: {{ formatAmount(job.contract.account.balance_amount) }} €
</p>
</div>
<div class="mt-3 text-sm text-gray-600">
<p>
<span class="font-medium">Naslov:</span>
{{ job.contract?.client_case?.person?.addresses?.[0]?.address || "—" }}
</p>
<p>
<span class="font-medium">Telefon:</span>
{{ job.contract?.client_case?.person?.phones?.[0]?.nu || "—" }}
</p>
</div>
<div class="mt-3 text-sm text-gray-600">
<p>
<span class="font-medium">Zadnja aktivnost:</span>
{{ formatDateDMY(job.last_activity) || "—" }}
</p>
</div>
</div>
</div>
</template>
<div
v-else
@@ -215,6 +307,5 @@ function getCaseUuid(job) {
</div>
</div>
</div>
</div>
</AppPhoneLayout>
</template>
+135 -2
View File
@@ -1,10 +1,11 @@
<script setup>
import AppLayout from "@/Layouts/AppLayout.vue";
import { Link, router } from "@inertiajs/vue3";
import { Link, router, useForm, usePage } from "@inertiajs/vue3";
import { ref, computed, watch } from "vue";
import axios from "axios";
import DataTableServer from "@/Components/DataTable/DataTableServer.vue";
import DialogModal from "@/Components/DialogModal.vue";
import ConfirmDialog from "@/Components/ConfirmDialog.vue";
const props = defineProps({
segment: Object,
@@ -20,8 +21,10 @@ const selectedClient = ref(initialClient);
// Column definitions for the server-driven table
const columns = [
{ key: "select", label: "", sortable: false, width: "50px" },
{ key: "reference", label: "Pogodba", sortable: true },
{ key: "client_case", label: "Primer" },
{ key: "address", label: "Naslov" },
{ key: "client", label: "Stranka" },
{ key: "type", label: "Vrsta" },
{ key: "start_date", label: "Začetek", sortable: true },
@@ -35,6 +38,13 @@ const exportColumns = ref(columns.map((col) => col.key));
const exportError = ref("");
const isExporting = ref(false);
const selectedRows = ref([]);
const showConfirmDialog = ref(false);
const archiveForm = useForm({
contracts: [],
reactivate: false,
});
const contractsCurrentPage = computed(() => props.contracts?.current_page ?? 1);
const contractsPerPage = computed(() => props.contracts?.per_page ?? 15);
const totalContracts = computed(
@@ -43,7 +53,14 @@ const totalContracts = computed(
const currentPageCount = computed(() => props.contracts?.data?.length ?? 0);
const allColumnsSelected = computed(() => exportColumns.value.length === columns.length);
const exportDisabled = computed(() => exportColumns.value.length === 0 || isExporting.value);
const exportDisabled = computed(
() => exportColumns.value.length === 0 || isExporting.value
);
const canManageSettings = computed(() => {
const permissions = usePage().props?.auth?.user?.permissions || [];
return permissions.includes("mass-archive");
});
function toggleAllColumns(checked) {
exportColumns.value = checked ? columns.map((col) => col.key) : [];
@@ -202,6 +219,67 @@ function extractFilenameFromHeaders(headers) {
const asciiMatch = disposition.match(/filename="?([^";]+)"?/i);
return asciiMatch?.[1] || null;
}
function toggleSelectAll() {
if (selectedRows.value.length === props.contracts.data.length) {
selectedRows.value = [];
} else {
selectedRows.value = props.contracts.data.map((row) => row.uuid);
}
}
function toggleRowSelection(uuid) {
const index = selectedRows.value.indexOf(uuid);
if (index > -1) {
selectedRows.value.splice(index, 1);
} else {
selectedRows.value.push(uuid);
}
}
function isRowSelected(uuid) {
return selectedRows.value.includes(uuid);
}
function isAllSelected() {
return (
props.contracts.data.length > 0 &&
selectedRows.value.length === props.contracts.data.length
);
}
function isIndeterminate() {
return (
selectedRows.value.length > 0 &&
selectedRows.value.length < props.contracts.data.length
);
}
function openArchiveModal() {
if (!selectedRows.value.length) return;
showConfirmDialog.value = true;
}
function closeConfirmDialog() {
showConfirmDialog.value = false;
}
function submitArchive() {
if (!selectedRows.value.length) return;
showConfirmDialog.value = false;
archiveForm.contracts = [...selectedRows.value];
archiveForm.reactivate = false;
archiveForm.post(route("contracts.archive-batch"), {
preserveScroll: true,
onSuccess: () => {
selectedRows.value = [];
router.reload({ only: ["contracts"] });
},
});
}
</script>
<template>
@@ -256,6 +334,7 @@ function extractFilenameFromHeaders(headers) {
row-key="uuid"
>
<template #toolbar-extra>
<div class="flex items-center gap-2">
<button
type="button"
class="inline-flex items-center rounded-md border border-indigo-200 bg-white px-3 py-2 text-sm font-medium text-indigo-700 shadow-sm hover:bg-indigo-50"
@@ -263,6 +342,43 @@ function extractFilenameFromHeaders(headers) {
>
Izvozi v Excel
</button>
<div
v-if="canManageSettings && selectedRows.length"
class="flex items-center gap-2"
>
<span class="text-sm text-gray-600"
>{{ selectedRows.length }} izbran{{
selectedRows.length === 1 ? "a" : "ih"
}}</span
>
<button
type="button"
class="inline-flex items-center rounded-md border border-red-200 bg-white px-3 py-2 text-sm font-medium text-red-700 shadow-sm hover:bg-red-50"
@click="openArchiveModal"
>
Arhiviraj izbrane
</button>
</div>
</div>
</template>
<template #header-select>
<input
v-if="canManageSettings"
type="checkbox"
:checked="isAllSelected()"
:indeterminate="isIndeterminate()"
@change="toggleSelectAll"
class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500"
/>
</template>
<template #cell-select="{ row }">
<input
v-if="canManageSettings"
type="checkbox"
:checked="isRowSelected(row.uuid)"
@change="toggleRowSelection(row.uuid)"
class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500"
/>
</template>
<!-- Primer (client_case) cell with link when available -->
<template #cell-client_case="{ row }">
@@ -281,6 +397,10 @@ function extractFilenameFromHeaders(headers) {
<span v-else>{{ row.client_case?.person?.full_name || "-" }}</span>
</template>
<!-- Client case address -->
<template #cell-address="{ row }">
{{ row.client_case?.person?.address?.address || "-" }}
</template>
<!-- Stranka (client) name -->
<template #cell-client="{ row }">
{{ row.client?.person?.full_name || "-" }}
@@ -309,6 +429,19 @@ function extractFilenameFromHeaders(headers) {
</div>
</div>
<ConfirmDialog
:show="showConfirmDialog"
title="Arhiviraj pogodbe"
:message="`Ali ste prepričani, da želite arhivirati ${selectedRows.length} pogodb${
selectedRows.length === 1 ? 'o' : ''
}? Arhivirane pogodbe bodo odstranjene iz aktivnih segmentov.`"
confirm-text="Arhiviraj"
cancel-text="Prekliči"
:danger="true"
@close="closeConfirmDialog"
@confirm="submitArchive"
/>
<DialogModal :show="exportDialogOpen" max-width="3xl" @close="closeExportDialog">
<template #title>
<div>
+2
View File
@@ -308,6 +308,7 @@
Route::get('clients', [ClientController::class, 'index'])->name('client');
Route::get('clients/{client:uuid}', [ClientController::class, 'show'])->name('client.show');
Route::get('clients/{client:uuid}/contracts', [ClientController::class, 'contracts'])->name('client.contracts');
Route::post('clients/{client:uuid}/contracts/export', [ClientController::class, 'exportContracts'])->name('client.contracts.export');
Route::middleware('permission:client-edit')->group(function () {
Route::post('clients', [ClientController::class, 'store'])->name('client.store');
@@ -321,6 +322,7 @@
Route::get('client-cases/{client_case:uuid}', [ClientCaseContoller::class, 'show'])->name('clientCase.show');
Route::post('client-cases/{client_case:uuid}/contracts/{uuid}/segment', [ClientCaseContoller::class, 'updateContractSegment'])->name('clientCase.contract.updateSegment');
Route::post('client-cases/{client_case:uuid}/contracts/{uuid}/archive', [ClientCaseContoller::class, 'archiveContract'])->name('clientCase.contract.archive');
Route::post('contracts/archive-batch', [ClientCaseContoller::class, 'archiveBatch'])->name('contracts.archive-batch')->middleware('permission:mass-archive');
Route::post('client-cases', [ClientCaseContoller::class, 'store'])->name('clientCase.store');
Route::post('client-cases/{client_case:uuid}/emergency-person', [ClientCaseContoller::class, 'emergencyCreatePerson'])->name('clientCase.emergencyPerson');
// client-case / contract