EV3-386 Verzióváltott teszt - Statisztikák

This commit is contained in:
E98Developer 2026-03-20 18:53:11 +01:00
parent 6a0048df11
commit 9a7610f14f

View File

@ -2,26 +2,30 @@
namespace App\Services; namespace App\Services;
use App\Enums\OrderFlag; use App\Enums\OrderFlag;
use App\Enums\OrderType; use App\Enums\OrderType;
use App\Models\OrderArchive; use App\Models\OrderArchive;
use App\Models\OrderArchivesItem; use App\Models\OrderArchivesItem;
use App\Models\PriceList;
use App\Models\ProductGroup; use App\Models\ProductGroup;
use App\Models\ProfitCenter; use App\Models\ProfitCenter;
use App\Models\Supplier;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class StatisticsService extends ServiceBase class StatisticsService extends ServiceBase
{ {
private array $statisticsParameters=[ private array $statisticsParameters=[
'suppliers' => [], "suppliers"=>[],
'profitCenters' => [], "profitCenters"=>[],
'productGroups' => [], "productGroups"=>[],
'products' => [], "products"=>[],
'producers' => [], "producers"=>[],
'hooreycaNames' => [], "hooreycaNames"=>[],
'dateFrom' => null, "dateFrom"=>null,
'dateTo' => null, "dateTo"=>null,
]; ];
private array $availableFilters=[ private array $availableFilters=[
@ -30,14 +34,13 @@ class StatisticsService extends ServiceBase
'ProductAmount'=>['suppliers','profitCenters','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[$type];
} }
return $this->availableFilters; return $this->availableFilters;
} }
@ -46,7 +49,6 @@ public function getStatisticsParameters(?string $pointer = null): array
if($pointer){ if($pointer){
return $this->statisticsParameters[$pointer]; return $this->statisticsParameters[$pointer];
} }
return $this->statisticsParameters; return $this->statisticsParameters;
} }
@ -54,11 +56,9 @@ public function setStatisticsParameters(?string $pointer, $value): StatisticsSer
{ {
if($pointer){ if($pointer){
$this->statisticsParameters[$pointer]=$value; $this->statisticsParameters[$pointer]=$value;
return $this; return $this;
} }
$this->statisticsParameters = $value; $this->statisticsParameters = $value;
return $this; return $this;
} }
@ -67,7 +67,6 @@ public function addStatisticsParameter(string $pointer, array $value): Statistic
if(is_array($this->statisticsParameters[$pointer])){ if(is_array($this->statisticsParameters[$pointer])){
$this->statisticsParameters[$pointer]=array_unique (array_merge ($this->statisticsParameters[$pointer],$value)); $this->statisticsParameters[$pointer]=array_unique (array_merge ($this->statisticsParameters[$pointer],$value));
} }
return $this; return $this;
} }
@ -79,7 +78,7 @@ public function __construct()
private function getProductGroupsWithParentName($productGroupIds):array private function getProductGroupsWithParentName($productGroupIds):array
{ {
$ProductGroup = new \App\Models\ProductGroup; $ProductGroup=new \App\Models\ProductGroup();
$productGroupArray=$ProductGroup->allWithPath(3); $productGroupArray=$ProductGroup->allWithPath(3);
$productGroupData=[]; $productGroupData=[];
foreach ($productGroupArray as $productGroup){ foreach ($productGroupArray as $productGroup){
@ -88,7 +87,6 @@ private function getProductGroupsWithParentName($productGroupIds): array
} }
} }
return $productGroupData; return $productGroupData;
} }
@ -96,7 +94,7 @@ private function parseProductGroupFilter(): void
{ {
$tmp=$this->statisticsParameters['productGroups']; $tmp=$this->statisticsParameters['productGroups'];
if(count($this->statisticsParameters['productGroups'])>0){ if(count($this->statisticsParameters['productGroups'])>0){
$ProductGroup = new ProductGroup; $ProductGroup=new ProductGroup();
$newGroups=[]; $newGroups=[];
foreach ($this->statisticsParameters['productGroups'] as $productGroupId) { foreach ($this->statisticsParameters['productGroups'] as $productGroupId) {
$newGroups=array_unique(array_merge($newGroups, $newGroups=array_unique(array_merge($newGroups,
@ -107,17 +105,145 @@ private function parseProductGroupFilter(): void
} }
} }
public function getStatisticsPriceChange() :array public function getStatisticsPriceChange() :array
{ {
$data=[]; $data=[];
$Model = OrderArchivesItem::where('status', 'active'); /* $ret=[];
$Model->limit(1); $ret['data']=[];
$data = $Model->get(); $ret['data']['changeDate']=['2024-01-01','2024-01-03','2024-04-11'];
return $ret;
;*/
// dd($Model); /* $Model=OrderArchivesItem::where('status','active');
return ['SQL' => getSql($Model), 'data' => $data->toArray()]; $Model->limit(1);
$data=$Model->get();*/
$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=[]; $data=[];
@ -141,7 +267,8 @@ public function getStatisticsProductAmount(): array
} }
//->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(); $this->parseProductGroupFilter();
@ -164,7 +291,7 @@ public function getStatisticsProductAmount(): array
$Model->whereIn('producer_id',$this->statisticsParameters['producers']); $Model->whereIn('producer_id',$this->statisticsParameters['producers']);
} }
$Model->limit(2000); $Model->limit(12000);
/* /*
$data=$Model->get()->toArray(); $data=$Model->get()->toArray();
dd($data); dd($data);
@ -184,16 +311,17 @@ public function getStatisticsProductAmount(): array
//$data[0]->xxx='xxx'; //$data[0]->xxx='xxx';
//dd($data[0]->toArray()); //dd($data[0]->toArray());
//->toArray(); //->toArray();
return ['SQL'=>getSql($Model),'data'=>$data->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) { ->where(function ($query) {
$query->wherein('orderFlag',[OrderFlag::modifier]) $query->wherein('orderFlag',[OrderFlag::modifier])
->orWhere('orderFlag', '=', null); ->orWhere('orderFlag', '=', null);
@ -213,7 +341,8 @@ public function getStatisticsOrderFrequency(): array
$Model->where('deliveryDate','<=',$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']); //->with(['profitCenter','supplier']);
$Model->groupBy('profit_center_name','supplier_name'); $Model->groupBy('profit_center_name','supplier_name');
@ -221,15 +350,10 @@ public function getStatisticsOrderFrequency(): array
$data=[]; $data=[];
$data=$Model->get()->toArray(); $data=$Model->get()->toArray();
return ['SQL'=>getSql($Model),'data'=>$data]; return ['SQL'=>getSql($Model),'data'=>$data];
return $Model->toSql(); return $Model->toSql();
return $Model->get()->toArray(); return $Model->get()->toArray();
return $ProfitCenter->get()->toArray(); return $ProfitCenter->get()->toArray();
return $this->getStatisticsParameters(null); return $this->getStatisticsParameters(null);
} }