ImportController.php 4.2 KB

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