'catalog', 'title' => 'Каталог', 'id' => 'products', 'header' => [ 'id' => 'ID', 'year' => 'Год', 'nomenclature_number' => 'Номер номенклатуры', 'article' => 'Артикул', 'manufacturer' => 'Производитель', 'name_tz' => 'Наименование ТЗ', 'type_tz' => 'Тип по ТЗ', 'type' => 'Тип', 'manufacturer_name' => 'Наименование производителя', 'sizes' => 'Размеры', 'price_status' => 'Статус цены', 'product_price_txt' => 'Цена товара', 'installation_price_txt' => 'Цена установки', 'service_price_txt' => 'Цена обслуживания', 'total_price_txt' => 'Итоговая цена', 'note' => 'Примечания', 'created_at' => 'Дата создания', ] ]; public function index(Request $request) { $filters = [ 'type_tz' => [ 'title' => 'Тип по ТЗ', 'values' => Product::getFilters('type_tz') ], 'type' => [ 'title' => 'Тип', 'values' => Product::getFilters('type') ], ]; $ranges = [ 'product_price' => [ 'title' => 'Цена товара', 'min' => Product::query()->min('product_price') / 100, 'max' => Product::query()->max('product_price') / 100, ], 'installation_price' => [ 'title' => 'Цена установки', 'min' => Product::query()->min('installation_price') / 100, 'max' => Product::query()->max('installation_price') / 100, ], 'service_price' => [ 'title' => 'Цена обслуживания', 'min' => Product::query()->min('service_price') / 100, 'max' => Product::query()->max('service_price') / 100, ], 'total_price' => [ 'title' => 'Итоговая цена', 'min' => Product::query()->min('total_price') / 100, 'max' => Product::query()->max('total_price') / 100, ], ]; $dates = [ 'created_at' => [ 'title' => 'Дата создания', 'min' => DateHelper::getDateForDB(Product::query()->min('created_at') ?? ''), 'max' => DateHelper::getDateForDB(Product::query()->max('created_at') ?? ''), ] ]; $this->data['searchFields'] = [ 'nomenclature_number', 'article', 'name_tz', 'manufacturer_name', 'note', ]; // fill filters $this->data['filters'] = $filters; $this->data['dates'] = $dates; $this->data['ranges'] = $ranges; // create request $q = Product::query(); // accept filters if(!empty($request->filters) && is_array($request->filters)) { foreach ($request->filters as $filterName => $filterValue) { if(!$filterValue) continue; if(Str::contains($filterName, 'price')) { $filterValue = $filterValue * 100; } if(Str::endsWith($filterName, '_from')) { if(isString($filterValue) && DateHelper::isDate($filterValue)) { $filterValue .= ' 00:00:00'; } $q->where(Str::replace('_from', '', $filterName), '>=', $filterValue); } elseif(Str::endsWith($filterName, '_to')) { if(isString($filterValue) && DateHelper::isDate($filterValue)) { $filterValue .= ' 23:59:59'; } $q->where(Str::replace('_to', '', $filterName), '<=', $filterValue); } else { $q->where($filterName, '=', $filterValue); } } } // accept search if(!empty($request->s)) { $s = $request->s; $searchFields = $this->data['searchFields']; $q->where(function ($query) use ($searchFields, $s) { foreach ($searchFields as $searchField) { $query->orWhere($searchField, 'LIKE', '%' . $s . '%'); } }); } // ------- setup sort and order -------------------------------------------------------------------------------- $this->data['sortBy'] = (!empty($request->sortBy)) ? Str::replace('_txt', '', $request->sortBy) // remove '_txt' fields modifier : Product::DEFAULT_SORT_BY; // check for sortBy is valid field $p = new Product(); if(!in_array($this->data['sortBy'], array_merge(['id', 'created_at'], $p->getFillable()))) { $this->data['sortBy'] = Product::DEFAULT_SORT_BY; } // set order $this->data['orderBy'] = (!empty($request->order)) ? 'desc' : 'asc'; $q->orderBy($this->data['sortBy'], $this->data['orderBy']); // $q->dumpRawSql(); $this->data['products'] = $q->paginate()->withQueryString(); return view('catalog.index', $this->data); } public function show() { } /** * @param Request $request * @return RedirectResponse */ public function import(Request $request) { // validate data $request->validate([ 'year' => 'required|integer|min:2000|max:' . (int)date('Y', strtotime('next year')), 'import_file' => 'file', ]); // load and save file $path = Str::random(2) . '/' . Str::uuid() . '.' .$request->file('import_file')->getClientOriginalExtension(); Storage::disk('upload')->put($path, $request->file('import_file')->getContent()); // dispatch job ImportCatalog::dispatch($path, $request->year, $request->user()->id); Log::info('ImportCatalog job created!'); return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']); } public function export(Request $request) { $request->validate([ 'withFilter' => 'nullable', ]); // load and save file // dispatch job ExportCatalog::dispatch([], $request->user()->id); Log::info('ImportCatalog job created!'); return redirect()->route('catalog.index')->with(['success' => 'Задача экспорта успешно создана!']); } }