From e0dd74c06c0a0c57d213bf42dea394bd1a1fcd55 Mon Sep 17 00:00:00 2001 From: imanghafoori Date: Tue, 4 Jun 2019 00:52:37 +0430 Subject: [PATCH] clean up --- .../Http/Controllers/DashboardController.php | 70 +++++++++---------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/packages/Webkul/Admin/src/Http/Controllers/DashboardController.php b/packages/Webkul/Admin/src/Http/Controllers/DashboardController.php index 37848b391..3be5ad0c8 100755 --- a/packages/Webkul/Admin/src/Http/Controllers/DashboardController.php +++ b/packages/Webkul/Admin/src/Http/Controllers/DashboardController.php @@ -131,47 +131,23 @@ class DashboardController extends Controller $statistics = [ 'total_customers' => [ - 'previous' => $previous = $this->customer->scopeQuery(function($query) { - return $query->where('customers.created_at', '>=', $this->lastStartDate) - ->where('customers.created_at', '<=', $this->lastEndDate); - })->count(), - 'current' => $current = $this->customer->scopeQuery(function($query) { - return $query->where('customers.created_at', '>=', $this->startDate) - ->where('customers.created_at', '<=', $this->endDate); - })->count(), + 'previous' => $previous = $this->getCustomersBetweenDates($this->lastStartDate, $this->lastEndDate)->count(), + 'current' => $current = $this->getCustomersBetweenDates($this->startDate, $this->endDate)->count(), 'progress' => $this->getPercentageChange($previous, $current) ], 'total_orders' => [ - 'previous' => $previous = $this->order->scopeQuery(function($query) { - return $query->where('orders.created_at', '>=', $this->lastStartDate) - ->where('orders.created_at', '<=', $this->lastEndDate); - })->count(), - 'current' => $current = $this->order->scopeQuery(function($query) { - return $query->where('orders.created_at', '>=', $this->startDate) - ->where('orders.created_at', '<=', $this->endDate); - })->count(), + 'previous' => $previous = $this->previousOrders()->count(), + 'current' => $current = $this->currentOrders()->count(), 'progress' => $this->getPercentageChange($previous, $current) ], 'total_sales' => [ - 'previous' => $previous = $this->order->scopeQuery(function($query) { - return $query->where('orders.created_at', '>=', $this->lastStartDate) - ->where('orders.created_at', '<=', $this->lastEndDate); - })->sum('base_grand_total'), - 'current' => $current = $this->order->scopeQuery(function($query) { - return $query->where('orders.created_at', '>=', $this->startDate) - ->where('orders.created_at', '<=', $this->endDate); - })->sum('base_grand_total'), + 'previous' => $previous = $this->previousOrders()->sum('base_grand_total'), + 'current' => $current = $this->currentOrders()->sum('base_grand_total'), 'progress' => $this->getPercentageChange($previous, $current) ], 'avg_sales' => [ - 'previous' => $previous = $this->order->scopeQuery(function($query) { - return $query->where('orders.created_at', '>=', $this->lastStartDate) - ->where('orders.created_at', '<=', $this->lastEndDate); - })->avg('base_grand_total'), - 'current' => $current = $this->order->scopeQuery(function($query) { - return $query->where('orders.created_at', '>=', $this->startDate) - ->where('orders.created_at', '<=', $this->endDate); - })->avg('base_grand_total'), + 'previous' => $previous = $this->previousOrders()->avg('base_grand_total'), + 'current' => $current = $this->currentOrders()->avg('base_grand_total'), 'progress' => $this->getPercentageChange($previous, $current) ], 'top_selling_categories' => $this->getTopSellingCategories(), @@ -180,14 +156,10 @@ class DashboardController extends Controller 'stock_threshold' => $this->getStockThreshold(), ]; - foreach (core()->getTimeInterval($this->startDate, $this->endDate) as $interval) { $statistics['sale_graph']['label'][] = $interval['start']->format('d M'); - $total = $this->order->scopeQuery(function($query) use($interval) { - return $query->where('orders.created_at', '>=', $interval['start']) - ->where('orders.created_at', '<=', $interval['end']); - })->sum('base_grand_total'); + $total = $this->getOrdersBetweenDate($interval['start'], $interval['end'])->sum('base_grand_total'); $statistics['sale_graph']['total'][] = $total; $statistics['sale_graph']['formated_total'][] = core()->formatBasePrice($total); @@ -300,4 +272,28 @@ class DashboardController extends Controller $this->lastStartDate->subDays($this->startDate->diffInDays($this->endDate)); // $this->lastEndDate->subDays($this->lastStartDate->diffInDays($this->lastEndDate)); } + + private function previousOrders() + { + return $this->getOrdersBetweenDate($this->lastStartDate, $this->lastEndDate); + } + + private function currentOrders() + { + return $this->getOrdersBetweenDate($this->startDate, $this->endDate); + } + + private function getOrdersBetweenDate($start, $end) + { + return $this->order->scopeQuery(function ($query) use ($start, $end) { + return $query->where('orders.created_at', '>=', $start)->where('orders.created_at', '<=', $end); + }); + } + + private function getCustomersBetweenDates($start, $end) + { + return $this->customer->scopeQuery(function ($query) use ($start, $end) { + return $query->where('customers.created_at', '>=', $start)->where('customers.created_at', '<=', $end); + }); + } } \ No newline at end of file