|
|
@@ -10,6 +10,7 @@ use App\Models\SparePart;
|
|
|
use App\Models\SparePartsView;
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
@@ -63,13 +64,24 @@ class SparePartController extends Controller
|
|
|
|
|
|
// Фильтры
|
|
|
$this->createFilters($model, 'used_in_maf');
|
|
|
-
|
|
|
- // Для range фильтров нужно использовать реальные поля БД (без _txt)
|
|
|
- // но заголовки брать из header с _txt
|
|
|
-// $this->createRangeFiltersForPrices($model, 'customer_price', 'expertise_price', 'min_stock');
|
|
|
-// if (hasRole('admin')) {
|
|
|
-// $this->createRangeFiltersForPrices($model, 'purchase_price');
|
|
|
-// }
|
|
|
+ $this->data['filters']['customer_price_txt'] = [
|
|
|
+ 'title' => $this->data['header']['customer_price_txt'],
|
|
|
+ 'values' => [],
|
|
|
+ ];
|
|
|
+ $this->data['filters']['expertise_price_txt'] = [
|
|
|
+ 'title' => $this->data['header']['expertise_price_txt'],
|
|
|
+ 'values' => [],
|
|
|
+ ];
|
|
|
+ $this->data['filters']['pricing_codes_list'] = [
|
|
|
+ 'title' => $this->data['header']['pricing_codes_list'],
|
|
|
+ 'values' => [],
|
|
|
+ ];
|
|
|
+ if (hasRole('admin')) {
|
|
|
+ $this->data['filters']['purchase_price_txt'] = [
|
|
|
+ 'title' => $this->data['header']['purchase_price_txt'],
|
|
|
+ 'values' => [],
|
|
|
+ ];
|
|
|
+ }
|
|
|
|
|
|
// Запрос
|
|
|
$q = $model::query()->with('pricingCodes');
|
|
|
@@ -78,7 +90,18 @@ class SparePartController extends Controller
|
|
|
$this->acceptSearch($q, $request);
|
|
|
|
|
|
$this->setSortAndOrderBy($model, $request);
|
|
|
- $this->applyStableSorting($q);
|
|
|
+ if ($request->get('sortBy') === 'pricing_codes_list') {
|
|
|
+ $this->data['sortBy'] = 'pricing_codes_list';
|
|
|
+ $q->orderBy(
|
|
|
+ DB::table('spare_part_pricing_code as sppc')
|
|
|
+ ->join('pricing_codes as pc', 'pc.id', '=', 'sppc.pricing_code_id')
|
|
|
+ ->selectRaw('MIN(pc.code)')
|
|
|
+ ->whereColumn('sppc.spare_part_id', 'spare_parts_view.id'),
|
|
|
+ $this->data['orderBy'] ?? 'asc'
|
|
|
+ )->orderBy('id', $this->data['orderBy'] ?? 'asc');
|
|
|
+ } else {
|
|
|
+ $this->applyStableSorting($q);
|
|
|
+ }
|
|
|
|
|
|
$this->data['spare_parts'] = $q->paginate($this->data['per_page'])->withQueryString();
|
|
|
$this->data['strings'] = $this->data['spare_parts'];
|
|
|
@@ -303,37 +326,4 @@ class SparePartController extends Controller
|
|
|
return response()->json($spareParts);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Создание range фильтров для полей с ценами
|
|
|
- * Использует правильные заголовки из header (_txt версии)
|
|
|
- */
|
|
|
- protected function createRangeFiltersForPrices(SparePart $model, string ...$columnNames): void
|
|
|
- {
|
|
|
- foreach ($columnNames as $columnName) {
|
|
|
- // Определяем ключ заголовка
|
|
|
- $headerKey = str_ends_with($columnName, '_price') ? $columnName . '_txt' : $columnName;
|
|
|
-
|
|
|
- // Проверяем, есть ли заголовок
|
|
|
- if (!isset($this->data['header'][$headerKey])) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (str_ends_with($columnName, '_price')) {
|
|
|
- $min = $model::query()->min($columnName);
|
|
|
- $max = $model::query()->max($columnName);
|
|
|
-
|
|
|
- $this->data['ranges'][$columnName] = [
|
|
|
- 'title' => $this->data['header'][$headerKey],
|
|
|
- 'min' => $min ? $min / 100 : 0,
|
|
|
- 'max' => $max ? $max / 100 : 0,
|
|
|
- ];
|
|
|
- } else {
|
|
|
- $this->data['ranges'][$columnName] = [
|
|
|
- 'title' => $this->data['header'][$headerKey],
|
|
|
- 'min' => $model::query()->min($columnName) ?? 0,
|
|
|
- 'max' => $model::query()->max($columnName) ?? 0,
|
|
|
- ];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|