ImportController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\Import\ImportJob;
  4. use App\Models\Import;
  5. use App\Services\Access\AccessService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Storage;
  9. use Illuminate\Support\Str;
  10. class ImportController extends Controller
  11. {
  12. protected array $data = [
  13. 'active' => 'import',
  14. 'title' => 'Импорт',
  15. 'id' => 'import',
  16. 'header' => [
  17. 'id' => 'ID',
  18. 'type_label' => 'Тип',
  19. 'status_label' => 'Статус',
  20. 'created_at' => 'Создано',
  21. 'updated_at' => 'Изменено',
  22. ],
  23. 'searchFields' => [
  24. 'id',
  25. ],
  26. 'ranges' => [],
  27. 'filters' => [],
  28. ];
  29. /**
  30. * Display a listing of the resource.
  31. */
  32. public function index(Request $request)
  33. {
  34. session(['gp_import' => $request->all()]);
  35. $nav = $this->startNavigationContext($request);
  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, AccessService $accessService)
  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. if ($request->type === 'catalog') {
  59. $accessService->assertCan($request->user(), 'catalog.import');
  60. } else {
  61. $accessService->assertCan($request->user(), 'import.create');
  62. }
  63. // load and save file
  64. $path = Str::random(2) . '/' . Str::uuid() . '.' .$request->file('import_file')->getClientOriginalExtension();
  65. Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
  66. $import = Import::query()->create([
  67. 'type' => $request->type,
  68. 'year' => in_array($request->type, ['catalog', 'maf_orders', 'mafs']) ? year() : null,
  69. 'status' => 'new',
  70. 'filename' => $path,
  71. ]);
  72. $import->refresh();
  73. ImportJob::dispatch($import, $request->user()->id);
  74. Log::info('Import job ' . $request->type . ' created!');
  75. return redirect()->route('import.index', session('gp_import'))->with(['success' => 'Задача импорта успешно создана!']);
  76. }
  77. public function show(Request $request, Import $import)
  78. {
  79. $this->data['import'] = $import;
  80. $nav = $this->resolveNavToken($request);
  81. $this->rememberNavigation($request, $nav);
  82. $this->data['nav'] = $nav;
  83. $this->data['back_url'] = $this->navigationBackUrl(
  84. $request,
  85. $nav,
  86. route('import.index', session('gp_import'))
  87. );
  88. return view('import.show', $this->data);
  89. }
  90. /**
  91. * Создание фильтров с переведёнными значениями type и status
  92. */
  93. protected function createImportFilters(Import $model): void
  94. {
  95. // Фильтр по типу
  96. $uniqueTypes = $model::query()->distinct()->pluck('type')->toArray();
  97. $typeValues = [];
  98. foreach ($uniqueTypes as $type) {
  99. $typeValues[$type] = Import::TYPE_LABELS[$type] ?? $type;
  100. }
  101. $this->data['filters']['type'] = [
  102. 'title' => 'Тип',
  103. 'values' => $typeValues,
  104. ];
  105. // Фильтр по статусу
  106. $uniqueStatuses = $model::query()->distinct()->pluck('status')->toArray();
  107. $statusValues = [];
  108. foreach ($uniqueStatuses as $status) {
  109. $statusValues[$status] = Import::STATUS_LABELS[$status] ?? $status;
  110. }
  111. $this->data['filters']['status'] = [
  112. 'title' => 'Статус',
  113. 'values' => $statusValues,
  114. ];
  115. }
  116. }