146 lines
4.6 KiB
PHP
146 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\GesconImport\Modules;
|
|
|
|
use App\Models\GesconImportMapping;
|
|
use App\Models\UserSetting;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Throwable;
|
|
|
|
abstract class BaseImportModule
|
|
{
|
|
protected function resolveMdbPath(string $legacy, ?int $anno): string
|
|
{
|
|
if (!$anno) {
|
|
return '';
|
|
}
|
|
|
|
$base = UserSetting::get('gescon.source_path', '/mnt/gescon-archives/gescon');
|
|
$candidate = rtrim($base, DIRECTORY_SEPARATOR)
|
|
. DIRECTORY_SEPARATOR . $legacy
|
|
. DIRECTORY_SEPARATOR . $anno
|
|
. DIRECTORY_SEPARATOR . 'singolo_anno.mdb';
|
|
|
|
return @is_file($candidate) ? $candidate : '';
|
|
}
|
|
|
|
protected function hydrateMappingPreferences(
|
|
ImportContext $context,
|
|
string $legacy,
|
|
?int $targetStabileId,
|
|
?string $targetStabileCode,
|
|
?string $splitMode,
|
|
string $filtersKey,
|
|
?callable $mapCallback = null
|
|
): array {
|
|
$targetAdminId = null;
|
|
try {
|
|
$mapRec = GesconImportMapping::where('user_id', $context->user->id)
|
|
->where('legacy_code', $legacy)
|
|
->first();
|
|
if ($mapRec) {
|
|
$meta = is_array($mapRec->meta) ? $mapRec->meta : [];
|
|
if (!$targetStabileId) {
|
|
$mappedId = data_get($meta, 'target_stabile_id');
|
|
if ($mappedId) {
|
|
$targetStabileId = (int) $mappedId;
|
|
}
|
|
}
|
|
if (!$targetStabileCode) {
|
|
$mappedCode = data_get($meta, 'target_stabile_code');
|
|
if ($mappedCode) {
|
|
$targetStabileCode = (string) $mappedCode;
|
|
}
|
|
}
|
|
if (!$splitMode) {
|
|
$mappedSplit = data_get($meta, $filtersKey . '.split_mode');
|
|
if (is_string($mappedSplit) && $mappedSplit !== '') {
|
|
$splitMode = $mappedSplit;
|
|
}
|
|
}
|
|
$targetAdminId = data_get($meta, 'target_admin_id');
|
|
$targetAdminId = $targetAdminId ? (int) $targetAdminId : null;
|
|
if ($mapCallback) {
|
|
[$targetStabileId, $targetStabileCode, $splitMode, $targetAdminId] = $mapCallback(
|
|
$meta,
|
|
[$targetStabileId, $targetStabileCode, $splitMode, $targetAdminId]
|
|
);
|
|
}
|
|
}
|
|
} catch (Throwable $e) {
|
|
$targetAdminId = null;
|
|
}
|
|
|
|
return [$targetStabileId, $targetStabileCode, $splitMode, $targetAdminId];
|
|
}
|
|
|
|
protected function canTargetAdmin(ImportContext $context, int $targetAdminId): bool
|
|
{
|
|
if ($context->isSuperAdmin) {
|
|
return true;
|
|
}
|
|
|
|
return $context->currentAmministratore && $context->currentAmministratore->id === $targetAdminId;
|
|
}
|
|
|
|
protected function persistMappingMeta(
|
|
ImportContext $context,
|
|
string $legacy,
|
|
?int $targetStabileId,
|
|
?string $targetStabileCode,
|
|
?int $targetAdminId,
|
|
array $filters,
|
|
string $filtersKey
|
|
): void {
|
|
try {
|
|
$mapping = GesconImportMapping::firstOrCreate(
|
|
['user_id' => $context->user->id, 'legacy_code' => $legacy],
|
|
['status' => 'pending']
|
|
);
|
|
$meta = is_array($mapping->meta) ? $mapping->meta : [];
|
|
|
|
if ($targetStabileId !== null) {
|
|
$meta['target_stabile_id'] = $targetStabileId;
|
|
} else {
|
|
unset($meta['target_stabile_id']);
|
|
}
|
|
|
|
if ($targetStabileCode !== null) {
|
|
$meta['target_stabile_code'] = $targetStabileCode;
|
|
} else {
|
|
unset($meta['target_stabile_code']);
|
|
}
|
|
|
|
if ($targetAdminId) {
|
|
$meta['target_admin_id'] = $targetAdminId;
|
|
} else {
|
|
unset($meta['target_admin_id']);
|
|
}
|
|
|
|
$meta[$filtersKey] = $filters;
|
|
$mapping->meta = $meta;
|
|
$mapping->save();
|
|
} catch (Throwable $e) {
|
|
// best-effort
|
|
}
|
|
}
|
|
|
|
protected function buildArtisanParams(array $options): array
|
|
{
|
|
$params = [];
|
|
foreach ($options as $key => $value) {
|
|
if ($value === null || $value === '' || $value === false) {
|
|
continue;
|
|
}
|
|
if ($value === true) {
|
|
$params[$key] = true;
|
|
} else {
|
|
$params[$key] = $value;
|
|
}
|
|
}
|
|
|
|
return $params;
|
|
}
|
|
}
|