Untitled
unknown
plain_text
a year ago
3.2 kB
6
Indexable
protected function getOrderStats(Order $order, $request): array
{
$status = OrderStatus::statusTypes;
$status = array_fill_keys($status, 0);
// dd($status);
$tab = $request->input('filter.tab', $request->input('tab', 'active'));
$builder = QueryBuilder::for(Order::class)
->allowedFilters(['delivery_slot_id', 'currentStatus.status', 'details.product.name', 'delivery_date', 'deliveryLocation.subCluster.cluster.name', 'deliveryLocation.subCluster.name'])
->with(['currentStatus', 'details.product', 'deliveryLocation.subCluster.cluster', 'deliveryLocation.subCluster'])
->select('orders.*');
if ($tab === 'trash') {
$builder->onlyTrashed();
} elseif ($tab === 'active') {
$builder->whereHas('currentStatus', function (Builder $query) {
$query->whereIn('status', [OrderStatus::PLACED, OrderStatus::CONFIRMED, OrderStatus::IN_TRANSIT]);
});
} elseif ($tab === 'completed') {
$builder->whereHas('currentStatus', function (Builder $query) {
$query->where('status', OrderStatus::DELIVERED);
});
}
$product_types = Auth::user()->roles->whereNotNull("product_type")->pluck("product_type")->toArray();
$clusters = Auth::user()->roles->whereNotNull("cluster")->pluck("cluster")->toArray();
$subclusters = Auth::user()->roles->whereNotNull("subcluster")->pluck("subcluster")->toArray();
if(!empty($clusters)){
$builder->whereHas('deliveryLocation.subCluster.cluster', function (Builder $query) use($clusters) {
$query->whereIn('id', $clusters);
});
}
if(!empty($subclusters)){
$builder->whereHas('deliveryLocation.subCluster', function (Builder $query) use($subclusters) {
$query->whereIn('id', $subclusters);
});
}
// dd($product_types);
if(!empty($product_types)){
$prodCat = ProductCategory::whereIn('parent_id', $product_types)->pluck('id')->flatten()->unique()->toArray();
if(count($prodCat) == 0) {
$productIds = Product::whereIn('id', $product_types)->get()->pluck('id')->toArray();
}else{
$productIds = Product::whereIn('product_category_id', $prodCat)->get()->pluck('id')->toArray();
}
if(!empty($productIds)){
$builder->whereHas('details.product', function (Builder $query) use($productIds) {
$query->whereIn('id', $productIds);
});
}
}
$results = $builder->get();
$results = $results->groupBy('currentStatus.status')
->map(function ($group) {
return $group->count();
});
$stats = [];
$status = array_merge($status, $results->toArray());
// dd(array_merge($status, $results->toArray()));
foreach (collect($status) as $key => $val) {
$stats[] = ListItem::make(ucfirst($key), $val);
}
return $stats;
}Editor is loading...
Leave a Comment