enabled()) { $this->root(); return true; } return config('inventory_conversion.qa_enabled') === true && app()->environment('testing'); } public function root(): string { if (! $this->enabled() || config('inventory_conversion.qa_enabled') === true) { throw new LogicException('COMPRAS_INVENTORY_CONFIGURATION'); } $runtime = app(StandaloneRuntime::class); if (! $runtime->enabled()) { throw new LogicException('COMPRAS_INVENTORY_INSTALLATION_REQUIRED'); } $runtime->assertInstallation(DB::connection()); $runtime->assertLoadedPackages(); $root = config('inventory_conversion.root'); if (! is_string($root)) { throw new LogicException('COMPRAS_INVENTORY_ROOT'); } self::verifyArtifact($root); foreach ([ 'goods_receipt_items' => ['id', 'tenant_id', 'goods_receipt_id', 'stock_item_id', 'received_milli', 'accepted_milli', 'rejected_milli', 'quality_status', 'notes', 'snapshot'], 'goods_receipts' => ['id', 'tenant_id', 'purchase_order_id', 'received_by', 'warehouse_id', 'delivery_note', 'received_on', 'confirmed_at', 'snapshot'], 'stock_movements' => ['id', 'tenant_id', 'goods_receipt_item_id', 'stock_item_id', 'warehouse_id', 'moved_by', 'quantity_milli', 'movement_type', 'moved_at', 'equipment_id', 'spare_part_id', 'snapshot'], ] as $table => $columns) { if (! DB::getSchemaBuilder()->hasColumns($table, $columns)) { throw new LogicException('COMPRAS_INVENTORY_SCHEMA'); } } return $root; } /** No PHP from the component is executed during verification. */ public static function verifyArtifact(string $root): void { $boundary = realpath($root); $manifest = $root.'/manifest.json'; $bytes = is_file($manifest) ? file_get_contents($manifest) : false; if ($boundary === false || is_link($root) || is_link($manifest) || ! is_file($manifest) || ! is_string($bytes) || ! hash_equals(self::MANIFEST_SHA256, hash('sha256', $bytes))) { throw new LogicException('COMPRAS_INVENTORY_MANIFEST'); } $data = json_decode($bytes, true, 512, JSON_THROW_ON_ERROR); foreach ($data['files'] as $file => $hash) { $path = $root.'/'.$file; $actual = is_file($path) ? hash_file('sha256', $path) : false; if (is_link($path) || ! is_file($path) || ! str_starts_with(realpath($path) ?: '', $boundary.DIRECTORY_SEPARATOR) || ! is_string($actual) || ! hash_equals($hash, $actual)) { throw new LogicException('COMPRAS_INVENTORY_FILE:'.$file); } } } public static function assertArchiveEntries(\ZipArchive $archive): void { $expected = ['manifest.json', 'bootstrap.php', 'src/QuantityConversion.php', 'src/ReceiptMovementWriter.php']; $actual = []; for ($i = 0; $i < $archive->numFiles; $i++) { $actual[] = $archive->getNameIndex($i); } sort($actual, SORT_STRING); sort($expected, SORT_STRING); if ($actual !== $expected) { throw new LogicException('COMPRAS_INVENTORY_ARCHIVE_ENTRIES'); } } /** Disabling the component cannot silently select a writer unable to read frozen conversions. */ public function assertLegacyCompatible(): void { if (DB::table('goods_receipt_items')->whereNotNull('snapshot->quantity_conversion')->exists() || DB::table('supply_records')->whereNotNull('data->conversion_basis')->exists()) { throw new LogicException('COMPRAS_INVENTORY_DOWNGRADE_REFUSED'); } } }