diff --git a/controllers/Orders.php b/controllers/Orders.php index fb91609..ae213e4 100644 --- a/controllers/Orders.php +++ b/controllers/Orders.php @@ -2,7 +2,6 @@ use BackendMenu; use Backend\Classes\Controller; -use Carbon\Carbon; use Dshoreman\Shop\Models\Order; /** @@ -33,17 +32,13 @@ public function index() $this->vars['ordersPaid'] = Order::where('is_paid', 1)->count(); $this->vars['ordersPending'] = $this->vars['ordersTotal'] - $this->vars['ordersPaid']; - $this_month_start = Carbon::now()->startOfMonth(); - $last_month_start = Carbon::now()->subMonth()->startOfMonth(); - $last_month_end = Carbon::now()->subMonth()->endOfMonth(); + $c = $this->vars['ordersCount'] = Order::createdThisMonth()->count(); + $cl = $this->vars['ordersCountLast'] = Order::createdLastMonth()->count(); + $this->vars['ordersCountClass'] = $this->scoreboardClass($cl, $c); - $this->vars['ordersCount'] = Order::where('created_at', '>=', $this_month_start)->count(); - $this->vars['ordersCountLast'] = Order::whereBetween('created_at', [$last_month_start, $last_month_end])->count(); - $this->vars['ordersCountClass'] = $this->scoreboardClass($this->vars['ordersCountLast'], $this->vars['ordersCount']); - - $this->vars['ordersValue'] = Order::where('created_at', '>=', $this_month_start)->sum('total'); - $this->vars['ordersValueLast'] = Order::whereBetween('created_at', [$last_month_start, $last_month_end])->sum('total'); - $this->vars['ordersValueClass'] = $this->scoreboardClass($this->vars['ordersValueLast'], $this->vars['ordersValue']); + $v = $this->vars['ordersValue'] = Order::createdThisMonth()->sum('total'); + $vl = $this->vars['ordersValueLast'] = Order::createdLastMonth()->sum('total'); + $this->vars['ordersValueClass'] = $this->scoreboardClass($vl, $v); return $this->asExtension('ListController')->index(); } diff --git a/models/Order.php b/models/Order.php index ceebc10..8fe0ed6 100644 --- a/models/Order.php +++ b/models/Order.php @@ -1,5 +1,6 @@ where('created_at', '>=', Carbon::now()->startOfMonth()); + } + + public function scopeCreatedLastMonth($query) + { + return $query->whereBetween('created_at', [ + Carbon::now()->subMonth()->startOfMonth(), + Carbon::now()->subMonth()->endOfMonth() + ]); + } + }