Teren-app/app/Services/ImportProcessor.php
2025-09-30 00:06:47 +02:00

1490 lines
64 KiB
PHP

<?php
namespace App\Services;
use App\Models\Account;
use App\Models\AccountType;
use App\Models\Activity;
use App\Models\Client;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\ContractType;
use App\Models\Decision;
use App\Models\Email;
use App\Models\Import;
use App\Models\ImportEntity;
use App\Models\ImportEvent;
use App\Models\ImportRow;
use App\Models\Person\AddressType;
use App\Models\Person\Person;
use App\Models\Person\PersonAddress;
use App\Models\Person\PersonGroup;
use App\Models\Person\PersonPhone;
use App\Models\Person\PersonType;
use App\Models\Person\PhoneType;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class ImportProcessor
{
/**
* Process an import and apply basic dedup checks.
* Returns summary counts.
*/
public function process(Import $import, ?Authenticatable $user = null): array
{
$started = now();
$total = 0;
$skipped = 0;
$imported = 0;
$invalid = 0;
$fh = null;
// Only CSV/TSV supported in this pass
if (! in_array($import->source_type, ['csv', 'txt'])) {
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'event' => 'processing_skipped',
'level' => 'warning',
'message' => 'Only CSV/TXT supported in this pass.',
]);
$import->update(['status' => 'completed', 'finished_at' => now()]);
return ['ok' => true, 'status' => $import->status, 'counts' => compact('total', 'skipped', 'imported', 'invalid')];
}
// Get mappings for this import (with apply_mode)
$mappings = DB::table('import_mappings')
->where('import_id', $import->id)
->get(['source_column', 'target_field', 'transform', 'apply_mode', 'options']);
// Load dynamic entity config
[$rootAliasMap, $fieldAliasMap, $validRoots] = $this->loadImportEntityConfig();
// Normalize aliases (plural/legacy roots, field names) before validation
$mappings = $this->normalizeMappings($mappings, $rootAliasMap, $fieldAliasMap);
// Validate mapping roots early to avoid silent failures due to typos
$this->validateMappingRoots($mappings, $validRoots);
$header = $import->meta['columns'] ?? null;
// Prefer explicitly chosen delimiter, then template meta, else detected
$delimiter = $import->meta['forced_delimiter']
?? optional($import->template)->meta['delimiter']
?? $import->meta['detected_delimiter']
?? ',';
$hasHeader = (bool) ($import->meta['has_header'] ?? true);
$path = Storage::disk($import->disk)->path($import->path);
// Note: Do not auto-detect or infer mappings/fields beyond what the template mapping provides
// Parse file and create import_rows with mapped_data
$fh = @fopen($path, 'r');
if (! $fh) {
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'event' => 'processing_failed',
'level' => 'error',
'message' => 'Unable to open file for reading.',
]);
$import->update(['status' => 'failed', 'failed_at' => now()]);
return ['ok' => false, 'status' => $import->status];
}
try {
DB::beginTransaction();
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'event' => 'processing_started',
'level' => 'info',
'message' => 'Processing started.',
]);
$rowNum = 0;
if ($hasHeader) {
$first = fgetcsv($fh, 0, $delimiter);
$rowNum++;
// Always use the actual header from the file for parsing
$header = array_map(fn ($v) => $this->sanitizeHeaderName((string) $v), $first ?: []);
// Heuristic: if header parsed as a single column but contains common delimiters, warn about mismatch
if (count($header) === 1) {
$rawHeader = $first[0] ?? '';
if (is_string($rawHeader) && (str_contains($rawHeader, ';') || str_contains($rawHeader, "\t"))) {
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'event' => 'delimiter_mismatch_suspected',
'level' => 'warning',
'message' => 'Header parsed as a single column. Suspected delimiter mismatch. Set a forced delimiter in the template or import settings.',
'context' => [
'current_delimiter' => $delimiter,
'raw_header' => $rawHeader,
],
]);
}
}
// Preflight: warn if any mapped source columns are not present in the header (exact match)
$headerSet = [];
foreach ($header as $h) {
$headerSet[$h] = true;
}
$missingSources = [];
foreach ($mappings as $map) {
$src = (string) ($map->source_column ?? '');
if ($src !== '' && ! array_key_exists($src, $headerSet)) {
$missingSources[] = $src;
}
}
if (! empty($missingSources)) {
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'event' => 'source_columns_missing_in_header',
'level' => 'warning',
'message' => 'Some mapped source columns are not present in the file header (exact match required).',
'context' => [
'missing' => $missingSources,
'header' => $header,
],
]);
}
}
// If mapping contains contract.reference, we require each row to successfully resolve/create a contract
$requireContract = $this->mappingIncludes($mappings, 'contract.reference');
while (($row = fgetcsv($fh, 0, $delimiter)) !== false) {
$rowNum++;
$total++;
$rawAssoc = $this->buildRowAssoc($row, $header);
[$recordType, $mapped] = $this->applyMappings($rawAssoc, $mappings);
// Do not auto-derive or fallback values; only use explicitly mapped fields
$importRow = ImportRow::create([
'import_id' => $import->id,
'row_number' => $rowNum,
'record_type' => $recordType,
'raw_data' => $rawAssoc,
'mapped_data' => $mapped,
'status' => 'valid',
]);
// Contracts
$contractResult = null;
if (isset($mapped['contract'])) {
$contractResult = $this->upsertContractChain($import, $mapped, $mappings);
if ($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) {
try {
$this->postContractActions($import, $contractResult['contract']);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'post_contract_actions_applied',
'level' => 'info',
'message' => 'Applied template post-actions on existing contract.',
'context' => ['contract_id' => $contractResult['contract']->id],
]);
} catch (\Throwable $e) {
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'post_contract_action_failed',
'level' => 'warning',
'message' => $e->getMessage(),
]);
}
}
$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'] ?? 'Skipped contract (no changes).',
]);
} elseif (in_array($contractResult['action'], ['inserted', 'updated'])) {
$imported++;
$importRow->update([
'status' => 'imported',
'entity_type' => Contract::class,
'entity_id' => $contractResult['contract']->id,
]);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'row_imported',
'level' => 'info',
'message' => ucfirst($contractResult['action']).' contract',
'context' => ['id' => $contractResult['contract']->id],
]);
// Post-contract actions from template/import meta
try {
$this->postContractActions($import, $contractResult['contract']);
} catch (\Throwable $e) {
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'post_contract_action_failed',
'level' => 'warning',
'message' => $e->getMessage(),
]);
}
} else {
$invalid++;
$importRow->update(['status' => 'invalid', 'errors' => [$contractResult['message'] ?? 'Contract processing failed']]);
}
}
// Enforce hard requirement: if template mapped contract.reference but we didn't resolve/create a contract, mark row invalid and continue
if ($requireContract) {
$contractEnsured = false;
if ($contractResult && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) {
$contractEnsured = true;
}
if (! $contractEnsured) {
$srcCol = $this->findSourceColumnFor($mappings, 'contract.reference');
$rawVal = $srcCol !== null ? ($rawAssoc[$srcCol] ?? null) : null;
$extra = $srcCol !== null ? ' Source column: "'.$srcCol.'" value: '.(is_null($rawVal) || $rawVal === '' ? '(empty)' : (is_scalar($rawVal) ? (string) $rawVal : json_encode($rawVal))) : '';
$msg = 'Row '.$rowNum.': Contract was required (contract.reference mapped) but not created/resolved. '.($contractResult['message'] ?? '').$extra;
// Avoid double-counting invalid if already set by contract processing
if ($importRow->status !== 'invalid') {
$invalid++;
$importRow->update(['status' => 'invalid', 'errors' => [$msg]]);
}
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'row_invalid',
'level' => 'error',
'message' => $msg,
]);
// Skip further processing for this row
continue;
}
}
// Accounts
$accountResult = null;
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);
if ($accountResult['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' => $accountResult['message'] ?? 'Skipped (no changes).',
'context' => $accountResult['context'] ?? null,
]);
} elseif ($accountResult['action'] === 'inserted' || $accountResult['action'] === 'updated') {
$imported++;
$importRow->update([
'status' => 'imported',
'entity_type' => Account::class,
'entity_id' => $accountResult['account']->id,
]);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'row_imported',
'level' => 'info',
'message' => ucfirst($accountResult['action']).' account',
'context' => ['id' => $accountResult['account']->id],
]);
} else {
$invalid++;
$importRow->update(['status' => 'invalid', 'errors' => ['Unhandled result']]);
}
}
// Contacts: resolve person via Contract/Account chain, client_case.client_ref, contacts, or identifiers
$personIdForRow = null;
// Prefer person from contract created/updated above
if ($contractResult && isset($contractResult['contract']) && $contractResult['contract'] instanceof Contract) {
$ccId = $contractResult['contract']->client_case_id;
$personIdForRow = ClientCase::where('id', $ccId)->value('person_id');
}
// If we have a contract reference, resolve existing contract for this client and derive person
if (! $personIdForRow && $import->client_id && ! empty($mapped['contract']['reference'] ?? null)) {
$existingContract = Contract::query()
->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
->where('client_cases.client_id', $import->client_id)
->where('contracts.reference', $mapped['contract']['reference'])
->select('contracts.client_case_id')
->first();
if ($existingContract) {
$personIdForRow = ClientCase::where('id', $existingContract->client_case_id)->value('person_id');
}
}
// If account processing created/resolved a contract, derive person via its client_case
if (! $personIdForRow && $accountResult) {
if (isset($accountResult['contract']) && $accountResult['contract'] instanceof Contract) {
$ccId = $accountResult['contract']->client_case_id;
$personIdForRow = ClientCase::where('id', $ccId)->value('person_id');
} elseif (isset($accountResult['contract_id'])) {
$ccId = Contract::where('id', $accountResult['contract_id'])->value('client_case_id');
if ($ccId) {
$personIdForRow = ClientCase::where('id', $ccId)->value('person_id');
}
}
}
// Resolve by client_case.client_ref for this client (prefer reusing existing person)
if (! $personIdForRow && $import->client_id && ! empty($mapped['client_case']['client_ref'] ?? null)) {
$cc = ClientCase::where('client_id', $import->client_id)
->where('client_ref', $mapped['client_case']['client_ref'])
->first();
if ($cc) {
$personIdForRow = $cc->person_id ?: null;
}
}
// Resolve by contact values next
if (! $personIdForRow) {
$emailVal = trim((string) ($mapped['email']['value'] ?? ''));
$phoneNu = trim((string) ($mapped['phone']['nu'] ?? ''));
$addrLine = trim((string) ($mapped['address']['address'] ?? ''));
// Try to resolve by existing contacts first
if ($emailVal !== '') {
$personIdForRow = Email::where('value', $emailVal)->value('person_id');
}
if (! $personIdForRow && $phoneNu !== '') {
$personIdForRow = PersonPhone::where('nu', $phoneNu)->value('person_id');
}
if (! $personIdForRow && $addrLine !== '') {
$personIdForRow = PersonAddress::where('address', $addrLine)->value('person_id');
}
// If still no person but we have any contact value, auto-create a minimal person
// BUT if we can map to an existing client_case by client_ref, reuse that case and set person there (avoid separate person rows)
if (! $personIdForRow && ($emailVal !== '' || $phoneNu !== '' || $addrLine !== '')) {
if ($import->client_id && ! empty($mapped['client_case']['client_ref'] ?? null)) {
$cc = ClientCase::where('client_id', $import->client_id)
->where('client_ref', $mapped['client_case']['client_ref'])
->first();
if ($cc) {
$pid = $cc->person_id ?: $this->createMinimalPersonId();
if (! $cc->person_id) {
$cc->person_id = $pid;
$cc->save();
}
$personIdForRow = $pid;
}
}
}
if (! $personIdForRow && ($emailVal !== '' || $phoneNu !== '' || $addrLine !== '')) {
$personIdForRow = $this->createMinimalPersonId();
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id ?? null,
'event' => 'person_autocreated_for_contacts',
'level' => 'info',
'message' => 'Created minimal person to attach contact data (email/phone/address).',
'context' => [
'email' => $emailVal ?: null,
'phone' => $phoneNu ?: null,
'address' => $addrLine ?: null,
'person_id' => $personIdForRow,
],
]);
}
}
// Try identifiers from mapped person (no creation yet)
if (! $personIdForRow && ! empty($mapped['person'] ?? [])) {
$personIdForRow = $this->findPersonIdByIdentifiers($mapped['person']);
}
// Finally, if still unknown and person fields provided, create
if (! $personIdForRow && ! empty($mapped['person'] ?? [])) {
$personIdForRow = $this->findOrCreatePersonId($mapped['person']);
}
// At this point, personIdForRow is either resolved or remains null (no contacts/person data)
$contactChanged = false;
if ($personIdForRow) {
if (! empty($mapped['email'] ?? [])) {
$r = $this->upsertEmail($personIdForRow, $mapped['email'], $mappings);
if (in_array($r['action'] ?? 'skipped', ['inserted', 'updated'])) {
$contactChanged = true;
}
}
if (! empty($mapped['address'] ?? [])) {
$r = $this->upsertAddress($personIdForRow, $mapped['address'], $mappings);
if (in_array($r['action'] ?? 'skipped', ['inserted', 'updated'])) {
$contactChanged = true;
}
}
if (! empty($mapped['phone'] ?? [])) {
$r = $this->upsertPhone($personIdForRow, $mapped['phone'], $mappings);
if (in_array($r['action'] ?? 'skipped', ['inserted', 'updated'])) {
$contactChanged = true;
}
}
}
if (! isset($mapped['contract']) && ! isset($mapped['account'])) {
if ($contactChanged) {
$imported++;
$importRow->update([
'status' => 'imported',
'entity_type' => Person::class,
'entity_id' => $personIdForRow,
]);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'import_row_id' => $importRow->id,
'event' => 'row_imported',
'level' => 'info',
'message' => 'Contacts upserted',
'context' => ['person_id' => $personIdForRow],
]);
} else {
$skipped++;
$importRow->update(['status' => 'skipped']);
}
}
}
fclose($fh);
$import->update([
'status' => 'completed',
'finished_at' => now(),
'total_rows' => $total,
'imported_rows' => $imported,
'invalid_rows' => $invalid,
'valid_rows' => $total - $invalid,
]);
DB::commit();
return [
'ok' => true,
'status' => $import->status,
'counts' => compact('total', 'skipped', 'imported', 'invalid'),
];
} catch (\Throwable $e) {
if (is_resource($fh)) {
@fclose($fh);
}
DB::rollBack();
// Mark failed and log after rollback (so no partial writes persist)
$import->refresh();
$import->update(['status' => 'failed', 'failed_at' => now()]);
ImportEvent::create([
'import_id' => $import->id,
'user_id' => $user?->getAuthIdentifier(),
'event' => 'processing_failed',
'level' => 'error',
'message' => $e->getMessage(),
]);
return ['ok' => false, 'status' => 'failed', 'error' => $e->getMessage()];
}
}
private function buildRowAssoc(array $row, ?array $header): array
{
if (! $header) {
// positional mapping: 0..N-1
$assoc = [];
foreach ($row as $i => $v) {
$assoc[(string) $i] = $v;
}
return $assoc;
}
$assoc = [];
foreach ($header as $i => $name) {
$assoc[$name] = $row[$i] ?? null;
}
return $assoc;
}
private function applyMappings(array $raw, $mappings): array
{
$recordType = null;
$mapped = [];
foreach ($mappings as $map) {
$src = $map->source_column;
$target = $map->target_field;
if (! $target) {
continue;
}
$value = $raw[$src] ?? null;
// Transform chain support: e.g. "trim|decimal" or "upper|alnum"
$transform = (string) ($map->transform ?? '');
if ($transform !== '') {
$parts = explode('|', $transform);
foreach ($parts as $t) {
$t = trim($t);
if ($t === 'trim') {
$value = is_string($value) ? trim($value) : $value;
} elseif ($t === 'upper') {
$value = is_string($value) ? strtoupper($value) : $value;
} elseif ($t === 'lower') {
$value = is_string($value) ? strtolower($value) : $value;
} elseif ($t === 'digits' || $t === 'numeric') {
$value = is_string($value) ? preg_replace('/[^0-9]/', '', $value) : $value;
} elseif ($t === 'decimal') {
$value = is_string($value) ? $this->normalizeDecimal($value) : $value;
} elseif ($t === 'alnum') {
$value = is_string($value) ? preg_replace('/[^A-Za-z0-9]/', '', $value) : $value;
} elseif ($t === 'ref') {
// Reference safe: keep letters+digits only, uppercase
$value = is_string($value) ? strtoupper(preg_replace('/[^A-Za-z0-9]/', '', $value)) : $value;
}
}
}
// detect record type from first segment, e.g., "account.balance_amount"
$parts = explode('.', $target);
if (! $recordType && isset($parts[0])) {
$recordType = $parts[0];
}
// build nested array by dot notation
$this->arraySetDot($mapped, $target, $value);
}
return [$recordType, $mapped];
}
private function arraySetDot(array &$arr, string $path, $value): void
{
$keys = explode('.', $path);
$ref = &$arr;
foreach ($keys as $k) {
if (! isset($ref[$k]) || ! is_array($ref[$k])) {
$ref[$k] = [];
}
$ref = &$ref[$k];
}
$ref = $value;
}
private function upsertAccount(Import $import, array $mapped, $mappings): array
{
$clientId = $import->client_id; // may be null, used for contract lookup/creation
$acc = $mapped['account'] ?? [];
$contractId = $acc['contract_id'] ?? null;
$reference = $acc['reference'] ?? null;
// Determine if the template includes any contract mappings; if not, do not create contracts here
$hasContractRoot = $this->mappingsContainRoot($mappings, 'contract');
// Normalize references (remove spaces) for consistent matching
if (! is_null($reference)) {
$reference = preg_replace('/\s+/', '', trim((string) $reference));
$acc['reference'] = $reference;
}
if (! empty($acc['contract_reference'] ?? null)) {
$acc['contract_reference'] = preg_replace('/\s+/', '', trim((string) $acc['contract_reference']));
}
// If contract_id not provided, attempt to resolve by contract reference for the selected client
if (! $contractId) {
$contractRef = $acc['contract_reference'] ?? ($mapped['contract']['reference'] ?? null);
if ($clientId && $contractRef) {
// 1) Search existing contract by reference for that client (across its client cases)
$existingContract = Contract::query()
->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
->where('client_cases.client_id', $clientId)
->where('contracts.reference', $contractRef)
->select('contracts.*')
->first();
if ($existingContract) {
$contractId = $existingContract->id;
} elseif ($hasContractRoot) {
// Only create a new contract if the template explicitly includes contract mappings
// Resolve debtor via identifiers or provided person
$personId = $this->findPersonIdByIdentifiers($mapped['person'] ?? []);
if (! $personId) {
$personId = $this->findOrCreatePersonId($mapped['person'] ?? []);
}
if (! $personId) {
$personId = $this->createMinimalPersonId();
}
$clientCaseId = $this->findOrCreateClientCaseId($clientId, $personId, $mapped['client_case']['client_ref'] ?? null);
$contractFields = $mapped['contract'] ?? [];
$newContractData = [
'client_case_id' => $clientCaseId,
'reference' => $contractRef,
];
foreach (['start_date', 'end_date', 'description', 'type_id'] as $k) {
if (array_key_exists($k, $contractFields) && ! is_null($contractFields[$k])) {
$newContractData[$k] = $contractFields[$k];
}
}
$newContractData['start_date'] = $newContractData['start_date'] ?? now()->toDateString();
$newContractData['type_id'] = $newContractData['type_id'] ?? $this->getDefaultContractTypeId();
$createdContract = Contract::create($newContractData);
$contractId = $createdContract->id;
} else {
// Do not create contracts implicitly when not mapped in the template
$contractId = null;
}
if ($contractId) {
$acc['contract_id'] = $contractId;
$mapped['account'] = $acc;
}
}
}
// Fallback: if account.reference is empty but contract.reference is present, use it
if ((is_null($reference) || $reference === '') && ! empty($mapped['contract']['reference'] ?? null)) {
$reference = preg_replace('/\s+/', '', trim((string) $mapped['contract']['reference']));
if ($reference !== '') {
$acc['reference'] = $reference;
$mapped['account'] = $acc;
}
}
// Do not default or infer account.reference from other fields; rely solely on mapped values
if (! $contractId || ! $reference) {
$issues = [];
if (! $contractId) {
$issues[] = 'contract_id unresolved';
}
if (! $reference) {
$issues[] = 'account.reference empty';
}
$candidateContractRef = $acc['contract_reference'] ?? ($mapped['contract']['reference'] ?? null);
return [
'action' => 'skipped',
'message' => 'Prerequisite missing: '.implode(' & ', $issues),
'context' => [
'has_contract_root_mapped' => $hasContractRoot,
'candidate_contract_reference' => $candidateContractRef,
'account_reference_provided' => $reference,
'account_fields_present' => array_keys(array_filter($acc, fn ($v) => ! is_null($v) && $v !== '')),
],
];
}
$existing = Account::query()
->where('contract_id', $contractId)
->where('reference', $reference)
->first();
// Build applyable data based on apply_mode
$applyInsert = [];
$applyUpdate = [];
$applyModeByField = [];
foreach ($mappings as $map) {
if (! $map->target_field) {
continue;
}
$parts = explode('.', $map->target_field);
if ($parts[0] !== 'account') {
continue;
}
$field = $parts[1] ?? null;
if (! $field) {
continue;
}
$value = $acc[$field] ?? null;
if (in_array($field, ['balance_amount','initial_amount'], true) && is_string($value)) {
$value = $this->normalizeDecimal($value);
}
$mode = $map->apply_mode ?? 'both';
$applyModeByField[$field] = $mode;
if (in_array($mode, ['insert', 'both'])) {
$applyInsert[$field] = $value;
}
if (in_array($mode, ['update', 'both'])) {
$applyUpdate[$field] = $value;
}
}
if ($existing) {
if (empty($applyUpdate)) {
return ['action' => 'skipped', 'message' => 'No fields marked for update'];
}
// Only update fields that are set; skip nulls to avoid wiping unintentionally
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
if (empty($changes)) {
return ['action' => 'skipped', 'message' => 'No non-null changes'];
}
$existing->fill($changes);
$existing->save();
// also include contract hints for downstream contact resolution
return ['action' => 'updated', 'account' => $existing, 'contract_id' => $contractId];
} else {
// On insert: if initial_amount is not provided but balance_amount is, allow defaulting
// Only when the mapping for initial_amount is 'insert' or 'both', or unmapped (null).
$initMode = $applyModeByField['initial_amount'] ?? null;
if ((! array_key_exists('initial_amount', $applyInsert) || is_null($applyInsert['initial_amount'] ?? null))
&& array_key_exists('balance_amount', $applyInsert)
&& ($applyInsert['balance_amount'] !== null && $applyInsert['balance_amount'] !== '')
&& ($initMode === null || in_array($initMode, ['insert','both'], true))) {
$applyInsert['initial_amount'] = $applyInsert['balance_amount'];
}
if (empty($applyInsert)) {
return ['action' => 'skipped', 'message' => 'No fields marked for insert'];
}
$data = array_filter($applyInsert, fn ($v) => ! is_null($v));
$data['contract_id'] = $contractId;
$data['reference'] = $reference;
// ensure required defaults
$data['type_id'] = $data['type_id'] ?? $this->getDefaultAccountTypeId();
if (! array_key_exists('active', $data)) {
$data['active'] = 1;
}
$created = Account::create($data);
return ['action' => 'inserted', 'account' => $created, 'contract_id' => $contractId];
}
}
private function mappingsContainRoot($mappings, string $root): bool
{
foreach ($mappings as $map) {
$target = (string) ($map->target_field ?? '');
if ($target !== '' && str_starts_with($target, $root.'.')) {
return true;
}
}
return false;
}
private function findPersonIdByIdentifiers(array $p): ?int
{
$tax = $p['tax_number'] ?? null;
if ($tax) {
$found = Person::where('tax_number', $tax)->first();
if ($found) {
return $found->id;
}
}
$ssn = $p['social_security_number'] ?? null;
if ($ssn) {
$found = Person::where('social_security_number', $ssn)->first();
if ($found) {
return $found->id;
}
}
return null;
}
private function upsertContractChain(Import $import, array $mapped, $mappings): array
{
$contractData = $mapped['contract'] ?? [];
$reference = $contractData['reference'] ?? null;
if (! is_null($reference)) {
$reference = preg_replace('/\s+/', '', trim((string) $reference));
$contractData['reference'] = $reference;
}
if (! $reference) {
return ['action' => 'invalid', 'message' => 'Missing contract.reference'];
}
// Determine client_case_id: prefer provided, else derive via person/client
$clientCaseId = $contractData['client_case_id'] ?? null;
$clientId = $import->client_id; // may be null
// Try to find existing contract EARLY by (client_id, reference) across all cases to prevent duplicates
$existing = null;
if ($clientId) {
$existing = Contract::query()
->join('client_cases', 'contracts.client_case_id', '=', 'client_cases.id')
->where('client_cases.client_id', $clientId)
->where('contracts.reference', $reference)
->select('contracts.*')
->first();
}
// If not found by client+reference and a specific client_case_id is provided, try that too
if (! $existing && $clientCaseId) {
$existing = Contract::query()
->where('client_case_id', $clientCaseId)
->where('reference', $reference)
->first();
}
// If we still need to insert, we must resolve clientCaseId, but avoid creating new person/case unless necessary
if (! $existing && ! $clientCaseId) {
$clientRef = $mapped['client_case']['client_ref'] ?? null;
// First, if we have a client and client_ref, try to reuse existing case to avoid creating extra persons
if ($clientId && $clientRef) {
$cc = ClientCase::where('client_id', $clientId)->where('client_ref', $clientRef)->first();
if ($cc) {
// Reuse this case
$clientCaseId = $cc->id;
// If case has no person yet and we have mapped person identifiers/data, set it once
if (! $cc->person_id) {
$pid = null;
if (! empty($mapped['person'] ?? [])) {
$pid = $this->findPersonIdByIdentifiers($mapped['person']);
if (! $pid) {
$pid = $this->findOrCreatePersonId($mapped['person']);
}
}
if (! $pid) {
$pid = $this->createMinimalPersonId();
}
$cc->person_id = $pid;
$cc->save();
}
}
}
if (! $clientCaseId) {
// Resolve by identifiers or provided person; do not use Client->person
$personId = null;
if (! empty($mapped['person'] ?? [])) {
$personId = $this->findPersonIdByIdentifiers($mapped['person']);
if (! $personId) {
$personId = $this->findOrCreatePersonId($mapped['person']);
}
}
// As a last resort, create a minimal person for this client
if ($clientId && ! $personId) {
$personId = $this->createMinimalPersonId();
}
if ($clientId && $personId) {
$clientCaseId = $this->findOrCreateClientCaseId($clientId, $personId, $clientRef);
} elseif ($personId) {
// require an import client to attach case/contract
return ['action' => 'invalid', 'message' => 'Import must be linked to a client to create a case'];
} else {
return ['action' => 'invalid', 'message' => 'Unable to resolve client_case (need import client)'];
}
}
}
// Build applyable data based on apply_mode
$applyInsert = [];
$applyUpdate = [];
foreach ($mappings as $map) {
if (! $map->target_field) {
continue;
}
$parts = explode('.', $map->target_field);
if ($parts[0] !== 'contract') {
continue;
}
$field = $parts[1] ?? null;
if (! $field) {
continue;
}
$value = $contractData[$field] ?? null;
if ($field === 'reference' && ! is_null($value)) {
$value = preg_replace('/\s+/', '', trim((string) $value));
}
$mode = $map->apply_mode ?? 'both';
if (in_array($mode, ['insert', 'both'])) {
$applyInsert[$field] = $value;
}
if (in_array($mode, ['update', 'both'])) {
$applyUpdate[$field] = $value;
}
}
if ($existing) {
if (empty($applyUpdate)) {
// Return existing contract reference even when skipped so callers can treat as resolved
return ['action' => 'skipped', 'message' => 'No contract fields marked for update', 'contract' => $existing];
}
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
if (empty($changes)) {
return ['action' => 'skipped', 'message' => 'No non-null contract changes', 'contract' => $existing];
}
$existing->fill($changes);
$existing->save();
return ['action' => 'updated', 'contract' => $existing];
} else {
if (empty($applyInsert)) {
return ['action' => 'skipped', 'message' => 'No contract fields marked for insert'];
}
$data = array_filter($applyInsert, fn ($v) => ! is_null($v));
$data['client_case_id'] = $clientCaseId;
$data['reference'] = $reference;
// ensure required defaults
$data['start_date'] = $data['start_date'] ?? now()->toDateString();
$data['type_id'] = $data['type_id'] ?? $this->getDefaultContractTypeId();
$created = Contract::create($data);
return ['action' => 'inserted', 'contract' => $created];
}
}
private function sanitizeHeaderName(string $v): string
{
// Strip UTF-8 BOM and trim whitespace/control characters
$v = preg_replace('/^\xEF\xBB\xBF/', '', $v) ?? $v;
return trim($v);
}
private function findSourceColumnFor($mappings, string $targetField): ?string
{
foreach ($mappings as $map) {
if ((string) ($map->target_field ?? '') === $targetField) {
$src = (string) ($map->source_column ?? '');
return $src !== '' ? $src : null;
}
}
return null;
}
// Removed auto-detection helpers by request: no pattern scanning or fallback derivation
private function normalizeDecimal(string $raw): string
{
// Keep digits, comma, dot, and minus to detect separators
$s = preg_replace('/[^0-9,\.-]/', '', $raw) ?? '';
$s = trim($s);
if ($s === '') {
return $s;
}
$lastComma = strrpos($s, ',');
$lastDot = strrpos($s, '.');
// Determine decimal separator by last occurrence
$decimalSep = null;
if ($lastComma !== false || $lastDot !== false) {
if ($lastComma === false) {
$decimalSep = '.';
} elseif ($lastDot === false) {
$decimalSep = ',';
} else {
$decimalSep = $lastComma > $lastDot ? ',' : '.';
}
}
// Remove all thousand separators (the other one) and unify decimal to '.'
if ($decimalSep === ',') {
// remove all dots
$s = str_replace('.', '', $s);
// replace last comma with dot
$pos = strrpos($s, ',');
if ($pos !== false) {
$s[$pos] = '.';
}
// remove any remaining commas (unlikely)
$s = str_replace(',', '', $s);
} elseif ($decimalSep === '.') {
// remove all commas
$s = str_replace(',', '', $s);
// dot already decimal
} else {
// no decimal separator: remove commas/dots entirely
$s = str_replace([',', '.'], '', $s);
}
// Collapse multiple minus signs, keep leading only
$s = ltrim($s, '+');
$neg = false;
if (str_starts_with($s, '-')) {
$neg = true;
$s = ltrim($s, '-');
}
// Remove any stray minus signs
$s = str_replace('-', '', $s);
if ($neg) {
$s = '-'.$s;
}
return $s;
}
/**
* Ensure mapping roots are recognized; fail fast if unknown roots found.
*/
private function validateMappingRoots($mappings, array $validRoots): void
{
foreach ($mappings as $map) {
$target = (string) ($map->target_field ?? '');
if ($target === '') {
continue;
}
$root = explode('.', $target)[0];
if (! in_array($root, $validRoots, true)) {
// Common typos guidance
$hint = '';
if (str_starts_with($root, 'contract')) {
$hint = ' Did you mean "contract"?';
}
throw new \InvalidArgumentException('Unknown mapping root "'.$root.'" in target_field "'.$target.'".'.$hint);
}
}
}
private function mappingIncludes($mappings, string $targetField): bool
{
foreach ($mappings as $map) {
if ((string) ($map->target_field ?? '') === $targetField) {
return true;
}
}
return false;
}
/**
* Normalize mapping target_field to canonical forms.
* Examples:
* - contracts.reference => contract.reference
* - accounts.balance_amount => account.balance_amount
* - person_phones.nu => phone.nu
* - person_addresses.address => address.address
* - emails.email|emails.value => email.value
*/
private function normalizeMappings($mappings, array $rootAliasMap, array $fieldAliasMap)
{
$normalized = [];
foreach ($mappings as $map) {
$clone = clone $map;
$clone->target_field = $this->normalizeTargetField((string) ($map->target_field ?? ''), $rootAliasMap, $fieldAliasMap);
$normalized[] = $clone;
}
return collect($normalized);
}
private function normalizeTargetField(string $target, array $rootAliasMap, array $fieldAliasMap): string
{
if ($target === '') {
return $target;
}
$parts = explode('.', $target);
$root = $parts[0] ?? '';
$field = $parts[1] ?? null;
// Root aliases (plural to canonical) from DB
$root = $rootAliasMap[$root] ?? $root;
// Field aliases per root from DB
$aliases = $fieldAliasMap[$root] ?? [];
if ($field === null && isset($aliases['__default'])) {
$field = $aliases['__default'];
} elseif (isset($aliases[$field])) {
$field = $aliases[$field];
}
// Rebuild
if ($field !== null) {
return $root.'.'.$field;
}
return $root;
}
private function loadImportEntityConfig(): array
{
$entities = ImportEntity::all();
$rootAliasMap = [];
$fieldAliasMap = [];
$validRoots = [];
foreach ($entities as $ent) {
$canonical = $ent->canonical_root;
$validRoots[] = $canonical;
foreach ((array) ($ent->aliases ?? []) as $alias) {
$rootAliasMap[$alias] = $canonical;
}
// Also ensure canonical maps to itself
$rootAliasMap[$canonical] = $canonical;
$aliases = (array) ($ent->field_aliases ?? []);
// Allow default field per entity via '__default'
if (is_array($ent->fields) && count($ent->fields)) {
$aliases['__default'] = $aliases['__default'] ?? null;
}
$fieldAliasMap[$canonical] = $aliases;
}
// sensible defaults when DB empty
if (empty($validRoots)) {
$validRoots = ['person', 'contract', 'account', 'address', 'phone', 'email', 'client_case'];
}
return [$rootAliasMap, $fieldAliasMap, $validRoots];
}
private function findOrCreatePersonId(array $p): ?int
{
// Basic dedup: by tax_number, ssn, else full_name
$query = Person::query();
if (! empty($p['tax_number'] ?? null)) {
$found = $query->where('tax_number', $p['tax_number'])->first();
if ($found) {
return $found->id;
}
}
if (! empty($p['social_security_number'] ?? null)) {
$found = Person::where('social_security_number', $p['social_security_number'])->first();
if ($found) {
return $found->id;
}
}
// Do NOT use full_name as an identifier
// 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) {
if (array_key_exists($k, $p)) {
$data[$k] = $p[$k];
}
}
// derive full_name if missing
if (empty($data['full_name'])) {
$fn = trim((string) ($data['first_name'] ?? ''));
$ln = trim((string) ($data['last_name'] ?? ''));
if ($fn || $ln) {
$data['full_name'] = trim($fn.' '.$ln);
}
}
// ensure required group/type ids
$data['group_id'] = $data['group_id'] ?? $this->getDefaultPersonGroupId();
$data['type_id'] = $data['type_id'] ?? $this->getDefaultPersonTypeId();
$created = Person::create($data);
return $created->id;
}
return null;
}
private function createMinimalPersonId(): int
{
return Person::create([
'group_id' => $this->getDefaultPersonGroupId(),
'type_id' => $this->getDefaultPersonTypeId(),
// names and identifiers can be null; 'nu' will be auto-generated (unique 6-char)
])->id;
}
private function getDefaultPersonGroupId(): int
{
return (int) (PersonGroup::min('id') ?? 1);
}
private function getDefaultPersonTypeId(): int
{
return (int) (PersonType::min('id') ?? 1);
}
private function getDefaultContractTypeId(): int
{
return (int) (ContractType::min('id') ?? 1);
}
private function getDefaultAccountTypeId(): int
{
return (int) (AccountType::min('id') ?? 1);
}
private function getDefaultAddressTypeId(): int
{
return (int) (AddressType::min('id') ?? 1);
}
private function getDefaultPhoneTypeId(): int
{
return (int) (PhoneType::min('id') ?? 1);
}
// Removed getExistingPersonIdForClient: resolution should come from Contract -> ClientCase -> Person or identifiers
private function findOrCreateClientId(int $personId): int
{
$client = Client::where('person_id', $personId)->first();
if ($client) {
return $client->id;
}
return Client::create(['person_id' => $personId])->id;
}
private function findOrCreateClientCaseId(int $clientId, int $personId, ?string $clientRef = null): int
{
// Prefer existing by client_ref if provided
if ($clientRef) {
$cc = ClientCase::where('client_id', $clientId)
->where('client_ref', $clientRef)
->first();
if ($cc) {
// Ensure person_id is set (if missing) when matching by client_ref
if (! $cc->person_id) {
$cc->person_id = $personId;
$cc->save();
}
return $cc->id;
}
}
// Fallback: by (client_id, person_id)
$cc = ClientCase::where('client_id', $clientId)->where('person_id', $personId)->first();
if ($cc) {
return $cc->id;
}
return ClientCase::create(['client_id' => $clientId, 'person_id' => $personId, 'client_ref' => $clientRef])->id;
}
private function upsertEmail(int $personId, array $emailData, $mappings): array
{
$value = trim((string) ($emailData['value'] ?? ''));
if ($value === '') {
return ['action' => 'skipped', 'message' => 'No email value'];
}
$existing = Email::where('person_id', $personId)->where('value', $value)->first();
$applyInsert = [];
$applyUpdate = [];
foreach ($mappings as $map) {
if (! $map->target_field) {
continue;
}
$parts = explode('.', $map->target_field);
if ($parts[0] !== 'email') {
continue;
}
$field = $parts[1] ?? null;
if (! $field) {
continue;
}
$val = $emailData[$field] ?? null;
$mode = $map->apply_mode ?? 'both';
if (in_array($mode, ['insert', 'both'])) {
$applyInsert[$field] = $val;
}
if (in_array($mode, ['update', 'both'])) {
$applyUpdate[$field] = $val;
}
}
if ($existing) {
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
if (empty($changes)) {
return ['action' => 'skipped', 'message' => 'No email updates'];
}
$existing->fill($changes);
$existing->save();
return ['action' => 'updated', 'email' => $existing];
} else {
if (empty($applyInsert)) {
return ['action' => 'skipped', 'message' => 'No email fields for insert'];
}
$data = array_filter($applyInsert, fn ($v) => ! is_null($v));
$data['person_id'] = $personId;
if (! array_key_exists('is_active', $data)) {
$data['is_active'] = true;
}
$created = Email::create($data);
return ['action' => 'inserted', 'email' => $created];
}
}
private function upsertAddress(int $personId, array $addrData, $mappings): array
{
$addressLine = trim((string) ($addrData['address'] ?? ''));
if ($addressLine === '') {
return ['action' => 'skipped', 'message' => 'No address value'];
}
// 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();
$applyInsert = [];
$applyUpdate = [];
foreach ($mappings as $map) {
if (! $map->target_field) {
continue;
}
$parts = explode('.', $map->target_field);
if ($parts[0] !== 'address') {
continue;
}
$field = $parts[1] ?? null;
if (! $field) {
continue;
}
$val = $addrData[$field] ?? null;
$mode = $map->apply_mode ?? 'both';
if (in_array($mode, ['insert', 'both'])) {
$applyInsert[$field] = $val;
}
if (in_array($mode, ['update', 'both'])) {
$applyUpdate[$field] = $val;
}
}
if ($existing) {
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
if (empty($changes)) {
return ['action' => 'skipped', 'message' => 'No address updates'];
}
$existing->fill($changes);
$existing->save();
return ['action' => 'updated', 'address' => $existing];
} else {
if (empty($applyInsert)) {
return ['action' => 'skipped', 'message' => 'No address fields for insert'];
}
$data = array_filter($applyInsert, fn ($v) => ! is_null($v));
$data['person_id'] = $personId;
$data['country'] = $data['country'] ?? 'SLO';
$data['type_id'] = $data['type_id'] ?? $this->getDefaultAddressTypeId();
$created = PersonAddress::create($data);
return ['action' => 'inserted', 'address' => $created];
}
}
private function upsertPhone(int $personId, array $phoneData, $mappings): array
{
$nu = trim((string) ($phoneData['nu'] ?? ''));
if ($nu === '') {
return ['action' => 'skipped', 'message' => 'No phone value'];
}
$existing = PersonPhone::where('person_id', $personId)->where('nu', $nu)->first();
$applyInsert = [];
$applyUpdate = [];
foreach ($mappings as $map) {
if (! $map->target_field) {
continue;
}
$parts = explode('.', $map->target_field);
if ($parts[0] !== 'phone') {
continue;
}
$field = $parts[1] ?? null;
if (! $field) {
continue;
}
$val = $phoneData[$field] ?? null;
$mode = $map->apply_mode ?? 'both';
if (in_array($mode, ['insert', 'both'])) {
$applyInsert[$field] = $val;
}
if (in_array($mode, ['update', 'both'])) {
$applyUpdate[$field] = $val;
}
}
if ($existing) {
$changes = array_filter($applyUpdate, fn ($v) => ! is_null($v));
if (empty($changes)) {
return ['action' => 'skipped', 'message' => 'No phone updates'];
}
$existing->fill($changes);
$existing->save();
return ['action' => 'updated', 'phone' => $existing];
} else {
if (empty($applyInsert)) {
return ['action' => 'skipped', 'message' => 'No phone fields for insert'];
}
$data = array_filter($applyInsert, fn ($v) => ! is_null($v));
$data['person_id'] = $personId;
$data['type_id'] = $data['type_id'] ?? $this->getDefaultPhoneTypeId();
$created = PersonPhone::create($data);
return ['action' => 'inserted', 'phone' => $created];
}
}
/**
* After a contract is inserted/updated, attach default segment and create an activity
* using decision_id from import/template meta. Activity note includes template name.
*/
private function postContractActions(Import $import, Contract $contract): void
{
$meta = $import->meta ?? [];
$segmentId = (int) ($meta['segment_id'] ?? 0);
$decisionId = (int) ($meta['decision_id'] ?? 0);
$templateName = (string) ($meta['template_name'] ?? optional($import->template)->name ?? '');
$actionId = (int) ($meta['action_id'] ?? 0);
// Attach segment to contract as the main (active) segment if provided
if ($segmentId > 0) {
// Ensure the segment exists on the client case and is active
$ccSeg = \DB::table('client_case_segment')
->where('client_case_id', $contract->client_case_id)
->where('segment_id', $segmentId)
->first();
if (! $ccSeg) {
\DB::table('client_case_segment')->insert([
'client_case_id' => $contract->client_case_id,
'segment_id' => $segmentId,
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
} elseif (! $ccSeg->active) {
\DB::table('client_case_segment')
->where('id', $ccSeg->id)
->update(['active' => true, 'updated_at' => now()]);
}
// Deactivate all other segments for this contract to make this the main one
\DB::table('contract_segment')
->where('contract_id', $contract->id)
->where('segment_id', '!=', $segmentId)
->update(['active' => false, 'updated_at' => now()]);
// Upsert the selected segment as active for this contract
$pivot = \DB::table('contract_segment')
->where('contract_id', $contract->id)
->where('segment_id', $segmentId)
->first();
if ($pivot) {
if (! $pivot->active) {
\DB::table('contract_segment')
->where('id', $pivot->id)
->update(['active' => true, 'updated_at' => now()]);
}
} else {
\DB::table('contract_segment')->insert([
'contract_id' => $contract->id,
'segment_id' => $segmentId,
'active' => true,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
// Create activity if decision provided
if ($decisionId > 0) {
Activity::create([
'decision_id' => $decisionId,
'action_id' => $actionId > 0 ? $actionId : null,
'contract_id' => $contract->id,
'client_case_id' => $contract->client_case_id,
'note' => trim('Imported via template'.($templateName ? ': '.$templateName : '')),
]);
}
}
}