ImportController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
  43. $this->data['imports'] = $q->paginate(session('per_page', config('pagination.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']) ? 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(Import $import)
  68. {
  69. $this->data['import'] = $import;
  70. return view('import.show', $this->data);
  71. }
  72. /**
  73. * Создание фильтров с переведёнными значениями type и status
  74. */
  75. protected function createImportFilters(Import $model): void
  76. {
  77. // Фильтр по типу
  78. $uniqueTypes = $model::query()->distinct()->pluck('type')->toArray();
  79. $typeValues = [];
  80. foreach ($uniqueTypes as $type) {
  81. $typeValues[$type] = Import::TYPE_LABELS[$type] ?? $type;
  82. }
  83. $this->data['filters']['type'] = [
  84. 'title' => 'Тип',
  85. 'values' => $typeValues,
  86. ];
  87. // Фильтр по статусу
  88. $uniqueStatuses = $model::query()->distinct()->pluck('status')->toArray();
  89. $statusValues = [];
  90. foreach ($uniqueStatuses as $status) {
  91. $statusValues[$status] = Import::STATUS_LABELS[$status] ?? $status;
  92. }
  93. $this->data['filters']['status'] = [
  94. 'title' => 'Статус',
  95. 'values' => $statusValues,
  96. ];
  97. }
  98. }