@@ -25,14 +25,15 @@ final class ImportCostDocumentHealth
|
||||
'documents_checked' => 0, 'files_checked' => 0, 'missing' => 0, 'altered' => 0,
|
||||
'invalid_reference' => 0, 'unreferenced' => 0, 'reasons' => []];
|
||||
$disk = Storage::disk('local');
|
||||
$records = DB::table('supply_records')->where('kind', 'import_cost_doc')->orderBy('id')->limit($limit + 1)->get();
|
||||
$records = DB::table('supply_records')->whereIn('kind', ['import_cost_doc', 'import_document'])->orderBy('id')->limit($limit + 1)->get();
|
||||
$limited = $records->count() > $limit;
|
||||
$root = realpath($disk->path(''));
|
||||
foreach ($records->take($limit) as $record) {
|
||||
$result['documents_checked']++;
|
||||
$snapshot = json_decode($record->snapshot, true, 64, JSON_THROW_ON_ERROR);
|
||||
$evidence = $snapshot['evidence'] ?? [];
|
||||
$prefix = 'import-costs/'.$record->tenant_id.'/'.$record->purchase_order_id.'/';
|
||||
$directory = $record->kind === 'import_cost_doc' ? 'import-costs' : 'import-dossier';
|
||||
$prefix = $directory.'/'.$record->tenant_id.'/'.$record->purchase_order_id.'/';
|
||||
$path = $evidence['path'] ?? null;
|
||||
if (($evidence['disk'] ?? null) !== 'local' || ! is_string($path)
|
||||
|| ! preg_match('/^[a-f0-9-]{36}$/D', $record->record_key)
|
||||
@@ -51,7 +52,7 @@ final class ImportCostDocumentHealth
|
||||
continue;
|
||||
}
|
||||
$absolute = str_replace('\\', '/', $resolved);
|
||||
$boundary = $root === false ? null : str_replace('\\', '/', $root).'/import-costs/';
|
||||
$boundary = $root === false ? null : str_replace('\\', '/', $root).'/'.$directory.'/';
|
||||
if ($boundary === null || ! (PHP_OS_FAMILY === 'Windows'
|
||||
? str_starts_with(strtolower($absolute), strtolower($boundary)) : str_starts_with($absolute, $boundary))) {
|
||||
$result['invalid_reference']++;
|
||||
@@ -68,17 +69,19 @@ final class ImportCostDocumentHealth
|
||||
$result['altered']++;
|
||||
}
|
||||
}
|
||||
foreach ($disk->getDriver()->listContents('import-costs', true) as $entry) {
|
||||
if (! $entry->isFile()) {
|
||||
continue;
|
||||
}
|
||||
if ($result['files_checked'] >= $limit) {
|
||||
$limited = true;
|
||||
break;
|
||||
}
|
||||
$result['files_checked']++;
|
||||
if (! DB::table('supply_records')->where('kind', 'import_cost_doc')->where('snapshot->evidence->path', $entry->path())->exists()) {
|
||||
$result['unreferenced']++;
|
||||
foreach (['import-costs' => 'import_cost_doc', 'import-dossier' => 'import_document'] as $directory => $kind) {
|
||||
foreach ($disk->getDriver()->listContents($directory, true) as $entry) {
|
||||
if (! $entry->isFile()) {
|
||||
continue;
|
||||
}
|
||||
if ($result['files_checked'] >= $limit) {
|
||||
$limited = true;
|
||||
break;
|
||||
}
|
||||
$result['files_checked']++;
|
||||
if (! DB::table('supply_records')->where('kind', $kind)->where('snapshot->evidence->path', $entry->path())->exists()) {
|
||||
$result['unreferenced']++;
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (['missing', 'altered', 'invalid_reference', 'unreferenced'] as $reason) {
|
||||
|
||||
Reference in New Issue
Block a user