ProductController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Helpers\DateHelper;
  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. use function PHPUnit\Framework\isString;
  12. class ProductController extends Controller
  13. {
  14. protected array $data = [
  15. 'active' => 'catalog',
  16. 'title' => 'Каталог',
  17. ];
  18. public function index(Request $request)
  19. {
  20. $filters = [
  21. 'type_tz' => [
  22. 'title' => 'Тип по ТЗ',
  23. 'values' => Product::getFilters('type_tz')
  24. ],
  25. 'type' => [
  26. 'title' => 'Тип',
  27. 'values' => Product::getFilters('type')
  28. ],
  29. ];
  30. $ranges = [
  31. 'product_price' => [
  32. 'title' => 'Цена товара',
  33. 'min' => Product::query()->min('product_price') / 100,
  34. 'max' => Product::query()->max('product_price') / 100,
  35. ],
  36. 'installation_price' => [
  37. 'title' => 'Цена установки',
  38. 'min' => Product::query()->min('installation_price') / 100,
  39. 'max' => Product::query()->max('installation_price') / 100,
  40. ],
  41. 'service_price' => [
  42. 'title' => 'Цена обслуживания',
  43. 'min' => Product::query()->min('service_price') / 100,
  44. 'max' => Product::query()->max('service_price') / 100,
  45. ],
  46. 'total_price' => [
  47. 'title' => 'Итоговая цена',
  48. 'min' => Product::query()->min('total_price') / 100,
  49. 'max' => Product::query()->max('total_price') / 100,
  50. ],
  51. ];
  52. $dates = [
  53. 'created_at' => [
  54. 'title' => 'Дата создания',
  55. 'min' => DateHelper::getDateForDB(Product::query()->min('created_at')),
  56. 'max' => DateHelper::getDateForDB(Product::query()->max('created_at')),
  57. ]
  58. ];
  59. $this->data['searchFields'] = [
  60. 'nomenclature_number',
  61. 'article',
  62. 'name_tz',
  63. 'manufacturer_name',
  64. 'note',
  65. ];
  66. // fill filters
  67. $this->data['filters'] = $filters;
  68. $this->data['dates'] = $dates;
  69. $this->data['ranges'] = $ranges;
  70. // create request
  71. $q = Product::query();
  72. // accept filters
  73. if(!empty($request->filters) && is_array($request->filters)) {
  74. foreach ($request->filters as $filterName => $filterValue) {
  75. if(!$filterValue) continue;
  76. if(Str::contains($filterName, 'price')) {
  77. $filterValue = $filterValue * 100;
  78. }
  79. if(Str::endsWith($filterName, '_from')) {
  80. if(isString($filterValue) && DateHelper::isDate($filterValue)) {
  81. $filterValue .= ' 00:00:00';
  82. }
  83. $q->where(Str::replace('_from', '', $filterName), '>=', $filterValue);
  84. } elseif(Str::endsWith($filterName, '_to')) {
  85. if(isString($filterValue) && DateHelper::isDate($filterValue)) {
  86. $filterValue .= ' 23:59:59';
  87. }
  88. $q->where(Str::replace('_to', '', $filterName), '<=', $filterValue);
  89. } else {
  90. $q->where($filterName, '=', $filterValue);
  91. }
  92. }
  93. }
  94. // accept search
  95. if(!empty($request->s)) {
  96. $s = $request->s;
  97. $searchFields = $this->data['searchFields'];
  98. $q->where(function ($query) use ($searchFields, $s) {
  99. foreach ($searchFields as $searchField) {
  100. $query->orWhere($searchField, 'LIKE', '%' . $s . '%');
  101. }
  102. });
  103. }
  104. // ------- setup sort and order --------------------------------------------------------------------------------
  105. $this->data['sortBy'] = (!empty($request->sortBy))
  106. ? Str::replace('_txt', '', $request->sortBy) // remove '_txt' fields modifier
  107. : Product::DEFAULT_SORT_BY;
  108. // check for sortBy is valid field
  109. $p = new Product();
  110. if(!in_array($this->data['sortBy'], array_merge(['id', 'created_at'], $p->getFillable()))) {
  111. $this->data['sortBy'] = Product::DEFAULT_SORT_BY;
  112. }
  113. // set order
  114. $this->data['orderBy'] = (!empty($request->order)) ? 'desc' : 'asc';
  115. $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
  116. // $q->dumpRawSql();
  117. $this->data['products'] = $q->paginate()->withQueryString();
  118. return view('catalog.index', $this->data);
  119. }
  120. public function show()
  121. {
  122. }
  123. /**
  124. * @param Request $request
  125. * @return RedirectResponse
  126. */
  127. public function import(Request $request)
  128. {
  129. // validate data
  130. $request->validate([
  131. 'year' => 'required|integer|min:2000|max:' . (int)date('Y', strtotime('next year')),
  132. 'import_file' => 'file',
  133. ]);
  134. // load and save file
  135. $path = Str::random(2) . '/' . Str::uuid() . '.' .$request->file('import_file')->getClientOriginalExtension();
  136. Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
  137. // dispatch job
  138. ImportCatalog::dispatch($path, $request->year, $request->user()->id);
  139. Log::info('ImportCatalog job created!');
  140. return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']);
  141. }
  142. public function export(Request $request)
  143. {
  144. return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']);
  145. }
  146. }