ProductController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\Export\ExportCatalog;
  4. use App\Jobs\Import\ImportCatalog;
  5. use App\Models\Product;
  6. use Illuminate\Http\RedirectResponse;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Support\Str;
  11. class ProductController extends Controller
  12. {
  13. protected array $data = [
  14. 'active' => 'catalog',
  15. 'title' => 'Каталог',
  16. 'id' => 'products',
  17. 'header' => [
  18. 'id' => 'ID',
  19. 'year' => 'Год',
  20. 'nomenclature_number' => 'Номер номенклатуры',
  21. 'article' => 'Артикул',
  22. 'manufacturer' => 'Производитель',
  23. 'name_tz' => 'Наименование ТЗ',
  24. 'type_tz' => 'Тип по ТЗ',
  25. 'type' => 'Тип',
  26. 'manufacturer_name' => 'Наименование производителя',
  27. 'sizes' => 'Размеры',
  28. 'price_status' => 'Статус цены',
  29. 'product_price_txt' => 'Цена товара',
  30. 'installation_price_txt' => 'Цена установки',
  31. 'service_price_txt' => 'Цена обслуживания',
  32. 'total_price_txt' => 'Итоговая цена',
  33. 'note' => 'Примечания',
  34. 'created_at' => 'Дата создания',
  35. ],
  36. 'searchFields' => [
  37. 'nomenclature_number',
  38. 'article',
  39. 'name_tz',
  40. 'manufacturer_name',
  41. 'note',
  42. ],
  43. ];
  44. public function index(Request $request)
  45. {
  46. $model = new Product;
  47. // fill filters
  48. $this->createFilters($model, 'type_tz', 'type');
  49. $this->createRangeFilters($model, 'nomenclature_number', 'product_price', 'installation_price', 'service_price', 'total_price');
  50. $this->createDateFilters($model, 'created_at');
  51. // create request
  52. $q = $model::query();
  53. $this->acceptFilters($q, $request);
  54. $this->acceptSearch($q, $request);
  55. $this->setSortAndOrderBy($model, $request);
  56. $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
  57. $this->data['products'] = $q->paginate()->withQueryString();
  58. return view('catalog.index', $this->data);
  59. }
  60. public function show()
  61. {
  62. }
  63. /**
  64. * @param Request $request
  65. * @return RedirectResponse
  66. */
  67. public function import(Request $request)
  68. {
  69. // validate data
  70. $request->validate([
  71. 'year' => 'required|integer|min:2000|max:' . (int)date('Y', strtotime('next year')),
  72. 'import_file' => 'file',
  73. ]);
  74. // load and save file
  75. $path = Str::random(2) . '/' . Str::uuid() . '.' .$request->file('import_file')->getClientOriginalExtension();
  76. Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
  77. // dispatch job
  78. ImportCatalog::dispatch($path, $request->year, $request->user()->id);
  79. Log::info('ImportCatalog job created!');
  80. return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']);
  81. }
  82. public function export(Request $request)
  83. {
  84. $request->validate([
  85. 'withFilter' => 'nullable',
  86. 'filters' => 'nullable|array',
  87. ]);
  88. // load and save file
  89. $filters = ($request->withFilter) ? $request->filters ?? [] : [];
  90. // dispatch job
  91. ExportCatalog::dispatch($filters , $request->user()->id);
  92. Log::info('ExportCatalog job created!');
  93. return redirect()->route('catalog.index')->with(['success' => 'Задача экспорта успешно создана!']);
  94. }
  95. /**
  96. * @param Request $request
  97. * @return array
  98. */
  99. public function search(Request $request): array
  100. {
  101. $s = $request->get('s');
  102. $searchFields = $this->data['searchFields'];
  103. $ret = [];
  104. if($s) {
  105. $result = Product::query()->where(function ($query) use ($searchFields, $s) {
  106. foreach ($searchFields as $searchField) {
  107. $query->orWhere($searchField, 'LIKE', '%' . $s . '%');
  108. }
  109. });
  110. foreach ($result->get() as $p) {
  111. $ret[$p->id] = $p->common_name;
  112. // $ret[$p->id] = $p->name_tz . ' (арт.' . $p->article . ', №' . $p->nomenclature_number . ', ' . $p->year . 'г., ' . $p->product_price_txt . ')';
  113. }
  114. }
  115. return $ret;
  116. }
  117. }