clearData(); DB::commit(); Log::info("Clear year data job done for year {$this->year}"); event(new SendWebSocketMessageEvent( "Данные за {$this->year} год успешно удалены!", $this->userId, ['success' => true, 'year' => $this->year] )); } catch (Exception $e) { DB::rollBack(); Log::error("Clear year data job failed for year {$this->year}: " . $e->getMessage()); event(new SendWebSocketMessageEvent( "Ошибка удаления данных за {$this->year} год: " . $e->getMessage(), $this->userId, ['error' => $e->getMessage(), 'year' => $this->year] )); } } private function clearData(): void { $orderIds = Order::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->pluck('id'); $productIds = Product::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->pluck('id'); $productSkuIds = ProductSKU::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->pluck('id'); $reclamationIds = Reclamation::whereIn('order_id', $orderIds)->pluck('id'); $fileIds = $this->collectFileIds($orderIds, $productIds, $productSkuIds); // Рекламации и связанные данные DB::table('reclamation_details')->whereIn('reclamation_id', $reclamationIds)->delete(); DB::table('reclamation_product_sku')->whereIn('reclamation_id', $reclamationIds)->delete(); DB::table('reclamation_photo_before')->whereIn('reclamation_id', $reclamationIds)->delete(); DB::table('reclamation_photo_after')->whereIn('reclamation_id', $reclamationIds)->delete(); DB::table('reclamation_document')->whereIn('reclamation_id', $reclamationIds)->delete(); DB::table('reclamation_act')->whereIn('reclamation_id', $reclamationIds)->delete(); Reclamation::whereIn('id', $reclamationIds)->delete(); // Связи заказов с файлами DB::table('order_photo')->whereIn('order_id', $orderIds)->delete(); DB::table('order_document')->whereIn('order_id', $orderIds)->delete(); DB::table('order_statement')->whereIn('order_id', $orderIds)->delete(); // Расписания Schedule::whereIn('order_id', $orderIds)->delete(); // SKU продуктов ProductSKU::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->forceDelete(); // Заказы Order::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->forceDelete(); // Заказы МАФ MafOrder::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->forceDelete(); // Продукты Product::withoutGlobalScopes()->withTrashed() ->whereIn('id', $productIds) ->update(['certificate_id' => null]); Product::withoutGlobalScopes()->withTrashed()->where('year', $this->year)->forceDelete(); // ТТН Ttn::where('year', $this->year)->update(['file_id' => null]); Ttn::where('year', $this->year)->delete(); // Контракты Contract::where('year', $this->year)->delete(); // Файлы $this->deleteFiles($fileIds); } private function collectFileIds($orderIds, $productIds, $productSkuIds): \Illuminate\Support\Collection { $fileIds = collect(); $fileIds = $fileIds->merge( DB::table('order_photo')->whereIn('order_id', $orderIds)->pluck('file_id') ); $fileIds = $fileIds->merge( DB::table('order_document')->whereIn('order_id', $orderIds)->pluck('file_id') ); $fileIds = $fileIds->merge( DB::table('order_statement')->whereIn('order_id', $orderIds)->pluck('file_id') ); $reclamationIds = Reclamation::whereIn('order_id', $orderIds)->pluck('id'); $fileIds = $fileIds->merge( DB::table('reclamation_photo_before')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id') ); $fileIds = $fileIds->merge( DB::table('reclamation_photo_after')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id') ); $fileIds = $fileIds->merge( DB::table('reclamation_document')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id') ); $fileIds = $fileIds->merge( DB::table('reclamation_act')->whereIn('reclamation_id', $reclamationIds)->pluck('file_id') ); $fileIds = $fileIds->merge( Product::withoutGlobalScopes()->withTrashed() ->whereIn('id', $productIds) ->whereNotNull('certificate_id') ->pluck('certificate_id') ); $fileIds = $fileIds->merge( ProductSKU::withoutGlobalScopes()->withTrashed() ->whereIn('id', $productSkuIds) ->whereNotNull('passport_id') ->pluck('passport_id') ); $fileIds = $fileIds->merge( Ttn::where('year', $this->year)->whereNotNull('file_id')->pluck('file_id') ); return $fileIds->unique(); } private function deleteFiles($fileIds): void { $files = File::whereIn('id', $fileIds)->get(); foreach ($files as $file) { if ($file->path && Storage::disk('public')->exists($file->path)) { Storage::disk('public')->delete($file->path); } } File::whereIn('id', $fileIds)->delete(); } }