diff --git a/app/Http/Controllers/Admin/StatisticsController.php b/app/Http/Controllers/Admin/StatisticsController.php index 267c46b..86219cc 100644 --- a/app/Http/Controllers/Admin/StatisticsController.php +++ b/app/Http/Controllers/Admin/StatisticsController.php @@ -149,6 +149,31 @@ private function getStatPriceChange(Request $request): JsonResponse return \response()->json(['function' => __FUNCTION__, 'parameters' => $this->Service->getStatisticsParameters(), 'ret' => $ret, 'success' => true], 200); } + public function getProductGroupSearch(Request $request): JsonResponse + { + $query = ProductGroup::query() + ->where('name', 'LIKE', '%' . $request->get('term') . '%'); + + if ($request->get('supplierId')) { + $supplierIds = $request->get('supplierId'); + $query->whereHas('products', function ($q) use ($supplierIds) { + $q->whereIn('supplier_id', $supplierIds); + }); + } + + $items = $query->orderBy('name')->get(['id', 'name'])->map(function ($group) { + return [ + 'id' => $group->id, + 'text' => $group->name, + ]; + }); + + return response()->json([ + 'results' => $items, + 'pagination' => ['more' => false], + ]); + } + public function getProductSearch(Request $request): JsonResponse { $page = $request->get('page'); diff --git a/app/Models/ProductGroup.php b/app/Models/ProductGroup.php index 203f1e7..aaf09c7 100644 --- a/app/Models/ProductGroup.php +++ b/app/Models/ProductGroup.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; use Kalnoy\Nestedset\NodeTrait; @@ -32,6 +33,11 @@ class ProductGroup extends Model protected ?array $needsId = null; + public function products(): HasMany + { + return $this->hasMany(Product::class, 'product_group_id'); + } + private function getNodeToArray($node, $needChild, $maxDepth = 999) { if ($this->needsId) { diff --git a/resources/views/admin/statistics/index.blade.php b/resources/views/admin/statistics/index.blade.php index 2e5648f..b438356 100644 --- a/resources/views/admin/statistics/index.blade.php +++ b/resources/views/admin/statistics/index.blade.php @@ -169,6 +169,7 @@ URL: { getDataTableContent: '', getProductSearch:'', + getProductGroupSearch:'', getStat:'', index: '', show: '', @@ -219,7 +220,25 @@ root.container=$(Opts.containerCssSelector); root.container.hide(); root.resultContainer=$(Opts.resultContainerSelector); - root.container.find('select.supplierSelect, select.profitCenterSelect, select.productGroupSelect, select.producerSelect, select.hooreycaNamesSelect, select.specialOfferSelect').select2(); + root.container.find('select.supplierSelect, select.profitCenterSelect, select.producerSelect, select.hooreycaNamesSelect, select.specialOfferSelect').select2(); + root.container.find('select.productGroupSelect').select2({ + placeholder: '...', + allowClear: true, + ajax: { + url: Opts.URL.getProductGroupSearch, + dataType: 'json', + data: function(params) { + return { + term: params.term || '', + supplierId: root.container.find('select.supplierSelect').val(), + }; + }, + cache: false + } + }); + root.container.find('select.supplierSelect').on('change', function() { + root.container.find('select.productGroupSelect').val(null).trigger('change'); + }); debug(root.container.find('select.product')); root.container.find('select.productSelect').select2({ placeholder: '...', @@ -745,6 +764,7 @@ className: 'dt-body-right', URL: { show:'{{route('admin.statistics.show',"%id%")}}', getProductSearch:'{{route('admin.statistics.getProductSearch')}}', + getProductGroupSearch:'{{route('admin.statistics.getProductGroupSearch')}}', getStat:'{{route('admin.statistics.getStat')}}', //getProductGroupHTML:'{{route('admin.priceListProfitCenter.getProductGroupHTML')}}', diff --git a/routes/web.php b/routes/web.php index e5e5bce..064ab70 100644 --- a/routes/web.php +++ b/routes/web.php @@ -163,6 +163,7 @@ // Route::get('getDataTableContent',[SupplierControllerAdmin::class,'getDataTableContent'])->name('getDataTableContent'); // dd('hello'); Route::get('getProductSearch', [StatisticsController::class, 'getProductSearch'])->name('getProductSearch'); + Route::get('getProductGroupSearch', [StatisticsController::class, 'getProductGroupSearch'])->name('getProductGroupSearch'); Route::post('getStat', [StatisticsController::class, 'getStat'])->name('getStat'); Route::resource('/', StatisticsController::class)->only([ 'index', 'show',