From 9a7610f14f104bb8055eac40ff1c50fefeafdfc4 Mon Sep 17 00:00:00 2001 From: E98Developer Date: Fri, 20 Mar 2026 18:53:11 +0100 Subject: [PATCH] =?UTF-8?q?EV3-386=20Verzi=C3=B3v=C3=A1ltott=20teszt=20-?= =?UTF-8?q?=20Statisztik=C3=A1k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/StatisticsService.php | 356 +++++++++++++++++++---------- 1 file changed, 240 insertions(+), 116 deletions(-) diff --git a/app/Services/StatisticsService.php b/app/Services/StatisticsService.php index 64c4e24..f04aa1a 100644 --- a/app/Services/StatisticsService.php +++ b/app/Services/StatisticsService.php @@ -2,72 +2,71 @@ namespace App\Services; + use App\Enums\OrderFlag; use App\Enums\OrderType; use App\Models\OrderArchive; use App\Models\OrderArchivesItem; +use App\Models\PriceList; use App\Models\ProductGroup; use App\Models\ProfitCenter; +use App\Models\Supplier; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; class StatisticsService extends ServiceBase { - private array $statisticsParameters = [ - 'suppliers' => [], - 'profitCenters' => [], - 'productGroups' => [], - 'products' => [], - 'producers' => [], - 'hooreycaNames' => [], - 'dateFrom' => null, - 'dateTo' => null, + + private array $statisticsParameters=[ + "suppliers"=>[], + "profitCenters"=>[], + "productGroups"=>[], + "products"=>[], + "producers"=>[], + "hooreycaNames"=>[], + "dateFrom"=>null, + "dateTo"=>null, ]; - private array $availableFilters = [ - 'OrderFrequency' => ['suppliers', 'profitCenters', 'date', 'dateFrom', 'dateTo'], - 'PriceChange' => ['suppliers', 'date', 'dateFrom', 'dateTo', 'productGroups', 'products', 'hooreycaNames', 'producers'], - 'ProductAmount' => ['suppliers', 'profitCenters', 'date', 'dateFrom', 'dateTo', 'productGroups', 'products', 'hooreycaNames', 'producers'], + private array $availableFilters=[ + 'OrderFrequency'=>['suppliers','profitCenters','date','dateFrom','dateTo'], + 'PriceChange'=>['suppliers','date','dateFrom','dateTo','productGroups','products','hooreycaNames','producers'], + 'ProductAmount'=>['suppliers','profitCenters','date','dateFrom','dateTo','productGroups','products','hooreycaNames','producers'], ]; - private Collection $S; + Private Collection $S; - public function getAvailableFilters($type = null): array + public function getAvailableFilters($type=null):array { - if (isset($this->availableFilters[$type])) { + if(isset($this->availableFilters[$type])){ return $this->availableFilters[$type]; } - return $this->availableFilters; } - public function getStatisticsParameters(?string $pointer = null): array + public function getStatisticsParameters(?string $pointer=null): array { - if ($pointer) { + if($pointer){ return $this->statisticsParameters[$pointer]; } - return $this->statisticsParameters; } public function setStatisticsParameters(?string $pointer, $value): StatisticsService { - if ($pointer) { - $this->statisticsParameters[$pointer] = $value; - + if($pointer){ + $this->statisticsParameters[$pointer]=$value; return $this; } $this->statisticsParameters = $value; - return $this; } public function addStatisticsParameter(string $pointer, array $value): StatisticsService { - if (is_array($this->statisticsParameters[$pointer])) { - $this->statisticsParameters[$pointer] = array_unique(array_merge($this->statisticsParameters[$pointer], $value)); + if(is_array($this->statisticsParameters[$pointer])){ + $this->statisticsParameters[$pointer]=array_unique (array_merge ($this->statisticsParameters[$pointer],$value)); } - return $this; } @@ -77,159 +76,284 @@ public function __construct() } - private function getProductGroupsWithParentName($productGroupIds): array + private function getProductGroupsWithParentName($productGroupIds):array { - $ProductGroup = new \App\Models\ProductGroup; - $productGroupArray = $ProductGroup->allWithPath(3); - $productGroupData = []; - foreach ($productGroupArray as $productGroup) { - if (in_array($productGroup['id'], $productGroupIds)) { - $productGroupData[$productGroup['id']] = explode('/', $productGroup['fullPath']); + $ProductGroup=new \App\Models\ProductGroup(); + $productGroupArray=$ProductGroup->allWithPath(3); + $productGroupData=[]; + foreach ($productGroupArray as $productGroup){ + if(in_array($productGroup['id'],$productGroupIds)){ + $productGroupData[$productGroup['id']]=explode('/',$productGroup['fullPath']); } } - return $productGroupData; - } +} - private function parseProductGroupFilter(): void + private function parseProductGroupFilter():void { - $tmp = $this->statisticsParameters['productGroups']; - if (count($this->statisticsParameters['productGroups']) > 0) { - $ProductGroup = new ProductGroup; - $newGroups = []; + $tmp=$this->statisticsParameters['productGroups']; + if(count($this->statisticsParameters['productGroups'])>0){ + $ProductGroup=new ProductGroup(); + $newGroups=[]; foreach ($this->statisticsParameters['productGroups'] as $productGroupId) { - $newGroups = array_unique(array_merge($newGroups, - ProductGroup::find($productGroupId)->descendants()->get()->pluck('id')->toArray() - )); + $newGroups=array_unique(array_merge($newGroups, + ProductGroup::find($productGroupId)->descendants()->get()->pluck('id')->toArray() + )); } - $this->statisticsParameters['productGroups'] = array_unique(array_merge($this->statisticsParameters['productGroups'], $newGroups)); + $this->statisticsParameters['productGroups']=array_unique(array_merge($this->statisticsParameters['productGroups'],$newGroups)); } } - public function getStatisticsPriceChange(): array + + public function getStatisticsPriceChange() :array { - $data = []; - $Model = OrderArchivesItem::where('status', 'active'); + $data=[]; +/* $ret=[]; + $ret['data']=[]; + $ret['data']['changeDate']=['2024-01-01','2024-01-03','2024-04-11']; + return $ret; + ;*/ + +/* $Model=OrderArchivesItem::where('status','active'); $Model->limit(1); - $data = $Model->get(); + $data=$Model->get();*/ - // dd($Model); - return ['SQL' => getSql($Model), 'data' => $data->toArray()]; + $Model=PriceList::where('status','active'); + if($this->statisticsParameters['dateFrom']){ + $Model->where('available','>=',$this->statisticsParameters['dateFrom']); + } + if($this->statisticsParameters['dateTo']){ + $Model->where('available','<=',$this->statisticsParameters['dateTo']); + } + if(count($this->statisticsParameters['suppliers'])>0){ + $Model->whereIn('supplier_id',$this->statisticsParameters['suppliers']); + } + $Model->orderBy('available','asc' ); + $Model->with('productsWithRelations',function ($query){ + //return $query->where('product_group_id',37); + + //csoport szúrő, figyelembe venni ha 1, 2 színtű lekérdezni a gyerekeket + $this->parseProductGroupFilter(); + if(count($this->statisticsParameters['productGroups'])>0) { + $query->whereIn('product_group_id',$this->statisticsParameters['productGroups']); + } + + //termÉk szűrő + if(count($this->statisticsParameters['products'])>0){ + $query->whereIn('name',$this->statisticsParameters['products']); + } + + //hooreyca name szürő + if(count($this->statisticsParameters['hooreycaNames'])>0){ + $query->whereIn('buyerProductName',$this->statisticsParameters['hooreycaNames']); + } + + //gyártó szürő + if(count($this->statisticsParameters['producers'])>0){ + $query->whereIn('producer_id',$this->statisticsParameters['producers']); + } + return $query; + }); + + /* + dd($Model->getProductRelations()); + dd($Model->with('productsWithRelations')->get()->toArray()); +// $priceLists=$Model->with('products')->get(); + */ + //dd($Model->with('productsWithRelations')->get()->toArray()); + //$Model->with('productsWithRelations')->where() + $priceLists=$Model->get(); + $products=[]; + $changedProducts=[]; + $changeDate=[]; + $needProductGroupIds=[]; + $supplierIds=[]; + if(count($priceLists)>0){ + foreach ($priceLists as $priceList){ + if(!in_array($priceList->supplier_id,$supplierIds)){ + $supplierIds[]=$priceList->supplier_id; + } + foreach ($priceList->productsWithRelations as $product){ + //dd($product->priceListPrice); + if(!isset($products[$product->id])){ + $producer_name='N/A'; + if($product->producer){ + $producer_name=$product->producer->name; + } + $products[$product->id]=[ + 'id'=>$product->id, + 'price'=>$product->priceListPrice->price, + 'name'=>$product->name, + //'producer_name'=>$product->producer->name, + 'producer_name'=>$producer_name, + 'supplier_id'=>$priceList->supplier_id, + 'producer_id'=>$product->producer_id, + 'productGroup_id'=>$product->product_group_id + ]; + }else{ + if ($products[$product->id]['price']!=$product->priceListPrice->price){ + $datePointer=preg_replace('~\D~','',[$priceList['available']])[0]; + $changedProducts[$product->id]['changes'][$datePointer]=$product->priceListPrice->price; + $products[$product->id]['price']=$product->priceListPrice->price; + if(!in_array($priceList['available'],$changeDate)){ + $changeDate[]=$priceList['available']; + } + } + if(!in_array($product->product_group_id,$needProductGroupIds)){ + $needProductGroupIds[]=$product->product_group_id; + } + } + } + } + + $suppliers=Supplier::select(['id','name'])->whereIn('id',$supplierIds )->get()->pluck('name','id'); + + $productGroupData=$this->getProductGroupsWithParentName($needProductGroupIds); + +/* foreach ($data as $k=>$item) { + $data[$k]['productGroupData']=$productGroupData[$item->product_group_id]; + $data[$k]['sum_quantity']=$item['quantity']*$item['unitMultiplier']; + }*/ + + $data['changedProducts']=[]; + + foreach ($changedProducts as $k=>$changedProduct){ +/* $changedProducts[$k]=array_merge($products[$k],$changedProduct); + $changedProducts[$k]['supplier_name']=$suppliers[$changedProducts[$k]['supplier_id']]; + $changedProducts[$k]['productGroupData']=$productGroupData[$changedProducts[$k]['productGroup_id']]; +*/ + + $tmpChangedProducts=array_merge($products[$k],$changedProduct); + $tmpChangedProducts['supplier_name']=$suppliers[$tmpChangedProducts['supplier_id']]; + $tmpChangedProducts['productGroupData']=$productGroupData[$tmpChangedProducts['productGroup_id']]; + $data['changedProducts'][]=$tmpChangedProducts; + } + //dd($data['changedProducts']); + + //$data['changedProducts']=$changedProducts; + + $data['priceLists']=$priceLists; + $data['changeDate']=$changeDate; + } + + + + + //dd($Model); + return ['SQL'=>getSql($Model),'data'=>$data]; + return ['SQL'=>getSql($Model),'data'=>$data->toArray()]; } - - public function getStatisticsProductAmount(): array + public function getStatisticsProductAmount() :array { - $data = []; - $Model = OrderArchivesItem::whereHas('orderArchive', function ($query) { + $data=[]; + $Model=OrderArchivesItem::whereHas('orderArchive',function($query) { $query->where('orderType', OrderType::daily) ->where(function ($query) { - $query->wherein('orderFlag', [OrderFlag::modifier]) + $query->wherein('orderFlag',[OrderFlag::modifier]) ->orWhere('orderFlag', '=', null); }); if (count($this->statisticsParameters['suppliers']) > 0) { $query->whereIn('supplier_id', $this->statisticsParameters['suppliers']); } - if (count($this->statisticsParameters['profitCenters']) > 0) { - $query->whereIn('profit_center_id', $this->statisticsParameters['profitCenters']); + if(count($this->statisticsParameters['profitCenters'])>0){ + $query->whereIn('profit_center_id',$this->statisticsParameters['profitCenters']); } - if ($this->statisticsParameters['dateFrom']) { - $query->where('deliveryDate', '>=', $this->statisticsParameters['dateFrom']); + if($this->statisticsParameters['dateFrom']){ + $query->where('deliveryDate','>=',$this->statisticsParameters['dateFrom']); } - if ($this->statisticsParameters['dateTo']) { - $query->where('deliveryDate', '<=', $this->statisticsParameters['dateTo']); + if($this->statisticsParameters['dateTo']){ + $query->where('deliveryDate','<=',$this->statisticsParameters['dateTo']); } - // ->where('orderArchive.orderType',OrderType::daily) + //->where('orderArchive.orderType',OrderType::daily) }) - ->with('orderArchive:id,profit_center_name'); + ->with('orderArchive:id,profit_center_name') + ; - // csoport szúrő, figyelembe venni ha 1, 2 színtű lekérdezni a gyerekeket + //csoport szúrő, figyelembe venni ha 1, 2 színtű lekérdezni a gyerekeket $this->parseProductGroupFilter(); - if (count($this->statisticsParameters['productGroups']) > 0) { - $Model->whereIn('product_group_id', $this->statisticsParameters['productGroups']); + if(count($this->statisticsParameters['productGroups'])>0) { + $Model->whereIn('product_group_id',$this->statisticsParameters['productGroups']); } - // termÉk szűrő - if (count($this->statisticsParameters['products']) > 0) { - $Model->whereIn('name', $this->statisticsParameters['products']); + //termÉk szűrő + if(count($this->statisticsParameters['products'])>0){ + $Model->whereIn('name',$this->statisticsParameters['products']); } - // hooreyca name szürő - if (count($this->statisticsParameters['hooreycaNames']) > 0) { - $Model->whereIn('buyerProductName', $this->statisticsParameters['hooreycaNames']); + //hooreyca name szürő + if(count($this->statisticsParameters['hooreycaNames'])>0){ + $Model->whereIn('buyerProductName',$this->statisticsParameters['hooreycaNames']); } - // gyártó szürő - if (count($this->statisticsParameters['producers']) > 0) { - $Model->whereIn('producer_id', $this->statisticsParameters['producers']); + //gyártó szürő + if(count($this->statisticsParameters['producers'])>0){ + $Model->whereIn('producer_id',$this->statisticsParameters['producers']); } - $Model->limit(2000); - /* - $data=$Model->get()->toArray(); - dd($data); - */ + $Model->limit(12000); +/* + $data=$Model->get()->toArray(); + dd($data); + */ /* product_group_id */ - $data = $Model->get(); - $productGroupIds = $data->unique('product_group_id')->pluck('product_group_id')->toArray(); - $productGroupData = $this->getProductGroupsWithParentName($productGroupIds); - foreach ($data as $k => $item) { - $data[$k]['productGroupData'] = $productGroupData[$item->product_group_id]; - $data[$k]['sum_quantity'] = $item['quantity'] * $item['unitMultiplier']; + $data=$Model->get(); + $productGroupIds=$data->unique('product_group_id')->pluck('product_group_id')->toArray(); + $productGroupData=$this->getProductGroupsWithParentName($productGroupIds); + foreach ($data as $k=>$item) { + $data[$k]['productGroupData']=$productGroupData[$item->product_group_id]; + $data[$k]['sum_quantity']=$item['quantity']*$item['unitMultiplier']; } - // $data[0]->xxx='xxx'; - // dd($data[0]->toArray()); + //$data[0]->xxx='xxx'; +//dd($data[0]->toArray()); - // ->toArray(); - return ['SQL' => getSql($Model), 'data' => $data->toArray()]; + //->toArray(); + + return ['SQL'=>getSql($Model),'data'=>$data->toArray()]; } - - public function getStatisticsOrderFrequency(): array + public function getStatisticsOrderFrequency() :array { - // return \response()->json([__FILE__,__LINE__,$this->getStatisticsParameters()], 400); + //return \response()->json([__FILE__,__LINE__,$this->getStatisticsParameters()], 400); - $Model = OrderArchive::where('orderType', OrderType::daily) + $Model=OrderArchive:: + where('orderType',OrderType::daily) ->where(function ($query) { - $query->wherein('orderFlag', [OrderFlag::modifier]) + $query->wherein('orderFlag',[OrderFlag::modifier]) ->orWhere('orderFlag', '=', null); }); - // wherein('orderFlag',[OrderFlag::modifier])->orWhere(); - if (count($this->statisticsParameters['profitCenters']) > 0) { - $Model->whereIn('profit_center_id', $this->statisticsParameters['profitCenters']); + //wherein('orderFlag',[OrderFlag::modifier])->orWhere(); + if(count($this->statisticsParameters['profitCenters'])>0){ + $Model->whereIn('profit_center_id',$this->statisticsParameters['profitCenters']); } - if (count($this->statisticsParameters['suppliers']) > 0) { - $Model->whereIn('supplier_id', $this->statisticsParameters['suppliers']); + if(count($this->statisticsParameters['suppliers'])>0){ + $Model->whereIn('supplier_id',$this->statisticsParameters['suppliers']); } - if ($this->statisticsParameters['dateFrom']) { - $Model->where('deliveryDate', '>=', $this->statisticsParameters['dateFrom']); + if($this->statisticsParameters['dateFrom']){ + $Model->where('deliveryDate','>=',$this->statisticsParameters['dateFrom']); } - if ($this->statisticsParameters['dateTo']) { - $Model->where('deliveryDate', '<=', $this->statisticsParameters['dateTo']); + if($this->statisticsParameters['dateTo']){ + $Model->where('deliveryDate','<=',$this->statisticsParameters['dateTo']); } - $Model->select(['profit_center_name', 'supplier_name', DB::raw('MAX(sumPrice) AS max_price, MIN(sumPrice) AS min_price, AVG(sumPrice) as avg_price, SUM(sumPrice) as sum_price, count(DISTINCT(deliveryDate)) as count_transaction'), + $Model->select(['profit_center_name','supplier_name' + ,DB::raw("MAX(sumPrice) AS max_price, MIN(sumPrice) AS min_price, AVG(sumPrice) as avg_price, SUM(sumPrice) as sum_price, count(DISTINCT(deliveryDate)) as count_transaction") ]); - // ->with(['profitCenter','supplier']); - $Model->groupBy('profit_center_name', 'supplier_name'); + //->with(['profitCenter','supplier']); + $Model->groupBy('profit_center_name','supplier_name'); $Model->orderBy('profit_center_name')->orderBy('supplier_name'); - $data = []; - $data = $Model->get()->toArray(); - - return ['SQL' => getSql($Model), 'data' => $data]; - + $data=[]; + $data=$Model->get()->toArray(); + return ['SQL'=>getSql($Model),'data'=>$data]; return $Model->toSql(); - return $Model->get()->toArray(); - return $ProfitCenter->get()->toArray(); - return $this->getStatisticsParameters(null); }