ProductController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // fill filters
  60. $this->data['filters'] = $filters;
  61. $this->data['dates'] = $dates;
  62. $this->data['ranges'] = $ranges;
  63. // create request
  64. $q = Product::query();
  65. // accept filters
  66. if(!empty($request->filters) && is_array($request->filters)) {
  67. foreach ($request->filters as $filterName => $filterValue) {
  68. if(!$filterValue) continue;
  69. if(Str::contains($filterName, 'price')) {
  70. $filterValue = $filterValue * 100;
  71. }
  72. if(Str::endsWith($filterName, '_from')) {
  73. if(isString($filterValue) && DateHelper::isDate($filterValue)) {
  74. $filterValue .= ' 00:00:00';
  75. }
  76. $q->where(Str::replace('_from', '', $filterName), '>=', $filterValue);
  77. } elseif(Str::endsWith($filterName, '_to')) {
  78. if(isString($filterValue) && DateHelper::isDate($filterValue)) {
  79. $filterValue .= ' 23:59:59';
  80. }
  81. $q->where(Str::replace('_to', '', $filterName), '<=', $filterValue);
  82. } else {
  83. $q->where($filterName, '=', $filterValue);
  84. }
  85. }
  86. }
  87. // ------- setup sort and order --------------------------------------------------------------------------------
  88. $this->data['sortBy'] = (!empty($request->sortBy))
  89. ? Str::replace('_txt', '', $request->sortBy) // remove '_txt' fields modifier
  90. : Product::DEFAULT_SORT_BY;
  91. // check for sortBy is valid field
  92. $p = new Product();
  93. if(!in_array($this->data['sortBy'], array_merge(['id', 'created_at'], $p->getFillable()))) {
  94. $this->data['sortBy'] = Product::DEFAULT_SORT_BY;
  95. }
  96. // set order
  97. $this->data['orderBy'] = (!empty($request->order)) ? 'desc' : 'asc';
  98. $q->orderBy($this->data['sortBy'], $this->data['orderBy']);
  99. // $q->dumpRawSql();
  100. $this->data['products'] = $q->paginate()->withQueryString();
  101. return view('catalog.index', $this->data);
  102. }
  103. public function show()
  104. {
  105. }
  106. /**
  107. * @param Request $request
  108. * @return RedirectResponse
  109. */
  110. public function import(Request $request)
  111. {
  112. // validate data
  113. $request->validate([
  114. 'year' => 'required|integer|min:2000|max:' . (int)date('Y', strtotime('next year')),
  115. 'import_file' => 'file',
  116. ]);
  117. // load and save file
  118. $path = Str::random(2) . '/' . Str::uuid() . '.' .$request->file('import_file')->getClientOriginalExtension();
  119. Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
  120. // dispatch job
  121. ImportCatalog::dispatch($path, $request->year, $request->user()->id);
  122. Log::info('ImportCatalog job created!');
  123. return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']);
  124. }
  125. public function export(Request $request)
  126. {
  127. return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']);
  128. }
  129. }