ImportController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. ];
  26. /**
  27. * Display a listing of the resource.
  28. */
  29. public function index(Request $request)
  30. {
  31. session(['gp_import' => $request->all()]);
  32. $model = new Import;
  33. // fill filters
  34. $this->data['ranges'] = [];
  35. $this->createImportFilters($model);
  36. $this->createDateFilters($model, 'created_at', 'updated_at');
  37. $q = $model::query();
  38. $q->withoutGlobalScopes();
  39. $this->acceptFilters($q, $request);
  40. $this->acceptSearch($q, $request);
  41. $this->setSortAndOrderBy($model, $request);
  42. $this->applyStableSorting($q);
  43. $this->data['imports'] = $q->paginate($this->data['per_page'])->withQueryString();
  44. return view('import.index', $this->data);
  45. }
  46. public function store(Request $request)
  47. {
  48. // validate data
  49. $request->validate([
  50. 'type' => 'required|in:orders,reclamations,mafs,catalog,spare_part_orders,maf_orders',
  51. 'import_file' => 'required|file',
  52. ]);
  53. // load and save file
  54. $path = Str::random(2) . '/' . Str::uuid() . '.' .$request->file('import_file')->getClientOriginalExtension();
  55. Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
  56. $import = Import::query()->create([
  57. 'type' => $request->type,
  58. 'year' => in_array($request->type, ['catalog', 'maf_orders', 'mafs']) ? year() : null,
  59. 'status' => 'new',
  60. 'filename' => $path,
  61. ]);
  62. $import->refresh();
  63. ImportJob::dispatch($import, $request->user()->id);
  64. Log::info('Import job ' . $request->type . ' created!');
  65. return redirect()->route('import.index', session('gp_import'))->with(['success' => 'Задача импорта успешно создана!']);
  66. }
  67. public function show(Request $request, Import $import)
  68. {
  69. $this->data['import'] = $import;
  70. $this->data['previous_url'] = $this->resolvePreviousUrl(
  71. $request,
  72. 'previous_url_import',
  73. route('import.index', session('gp_import'))
  74. );
  75. return view('import.show', $this->data);
  76. }
  77. /**
  78. * Создание фильтров с переведёнными значениями type и status
  79. */
  80. protected function createImportFilters(Import $model): void
  81. {
  82. // Фильтр по типу
  83. $uniqueTypes = $model::query()->distinct()->pluck('type')->toArray();
  84. $typeValues = [];
  85. foreach ($uniqueTypes as $type) {
  86. $typeValues[$type] = Import::TYPE_LABELS[$type] ?? $type;
  87. }
  88. $this->data['filters']['type'] = [
  89. 'title' => 'Тип',
  90. 'values' => $typeValues,
  91. ];
  92. // Фильтр по статусу
  93. $uniqueStatuses = $model::query()->distinct()->pluck('status')->toArray();
  94. $statusValues = [];
  95. foreach ($uniqueStatuses as $status) {
  96. $statusValues[$status] = Import::STATUS_LABELS[$status] ?? $status;
  97. }
  98. $this->data['filters']['status'] = [
  99. 'title' => 'Статус',
  100. 'values' => $statusValues,
  101. ];
  102. }
  103. }