| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace App\Http\Controllers;
- use App\Helpers\DateHelper;
- use App\Jobs\Export\ExportCatalog;
- use App\Jobs\Import\ImportCatalog;
- use App\Models\Product;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- use function PHPUnit\Framework\isString;
- class ProductController extends Controller
- {
- protected array $data = [
- 'active' => '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' => 'Задача экспорта успешно создана!']);
- }
- }
|