ImportController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\Import\ImportJob;
  4. use App\Models\Import;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Str;
  9. class ImportController extends Controller
  10. {
  11. protected array $data = [
  12. 'active' => 'import',
  13. 'title' => 'Импорт',
  14. 'id' => 'import',
  15. 'header' => [
  16. 'id' => 'ID',
  17. 'type_label' => 'Тип',
  18. 'status_label' => 'Статус',
  19. 'created_at' => 'Создано',
  20. 'updated_at' => 'Изменено',
  21. ],
  22. 'searchFields' => [
  23. 'id',
  24. ],
  25. 'ranges' => [],
  26. 'filters' => [],
  27. ];
  28. /**
  29. * Display a listing of the resource.
  30. */
  31. public function index(Request $request)
  32. {
  33. session(['gp_import' => $request->all()]);
  34. $model = new Import;
  35. // fill filters
  36. $this->data['ranges'] = [];
  37. $this->createImportFilters($model);
  38. $this->createDateFilters($model, 'created_at', 'updated_at');
  39. $q = $model::query();
  40. $q->withoutGlobalScope(\App\Models\Scopes\YearScope::class);
  41. $this->acceptFilters($q, $request);
  42. $this->acceptSearch($q, $request);
  43. $this->setSortAndOrderBy($model, $request);
  44. $this->applyStableSorting($q);
  45. $this->data['imports'] = $q->paginate($this->data['per_page'])->withQueryString();
  46. return view('import.index', $this->data);
  47. }
  48. public function store(Request $request)
  49. {
  50. // validate data
  51. $request->validate([
  52. 'type' => 'required|in:orders,reclamations,mafs,catalog,spare_part_orders,maf_orders',
  53. 'import_file' => 'required|file',
  54. ]);
  55. // load and save file
  56. $path = Str::random(2) . '/' . Str::uuid() . '.' .$request->file('import_file')->getClientOriginalExtension();
  57. Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
  58. $import = Import::query()->create([
  59. 'type' => $request->type,
  60. 'year' => in_array($request->type, ['catalog', 'maf_orders', 'mafs']) ? year() : null,
  61. 'status' => 'new',
  62. 'filename' => $path,
  63. ]);
  64. $import->refresh();
  65. ImportJob::dispatch($import, $request->user()->id);
  66. Log::info('Import job ' . $request->type . ' created!');
  67. return redirect()->route('import.index', session('gp_import'))->with(['success' => 'Задача импорта успешно создана!']);
  68. }
  69. public function show(Request $request, Import $import)
  70. {
  71. $this->data['import'] = $import;
  72. $this->data['previous_url'] = $this->resolvePreviousUrl(
  73. $request,
  74. 'previous_url_import',
  75. route('import.index', session('gp_import'))
  76. );
  77. return view('import.show', $this->data);
  78. }
  79. /**
  80. * Создание фильтров с переведёнными значениями type и status
  81. */
  82. protected function createImportFilters(Import $model): void
  83. {
  84. // Фильтр по типу
  85. $uniqueTypes = $model::query()->distinct()->pluck('type')->toArray();
  86. $typeValues = [];
  87. foreach ($uniqueTypes as $type) {
  88. $typeValues[$type] = Import::TYPE_LABELS[$type] ?? $type;
  89. }
  90. $this->data['filters']['type'] = [
  91. 'title' => 'Тип',
  92. 'values' => $typeValues,
  93. ];
  94. // Фильтр по статусу
  95. $uniqueStatuses = $model::query()->distinct()->pluck('status')->toArray();
  96. $statusValues = [];
  97. foreach ($uniqueStatuses as $status) {
  98. $statusValues[$status] = Import::STATUS_LABELS[$status] ?? $status;
  99. }
  100. $this->data['filters']['status'] = [
  101. 'title' => 'Статус',
  102. 'values' => $statusValues,
  103. ];
  104. }
  105. }