Skip to content

Commit

Permalink
Cleanup order controller
Browse files Browse the repository at this point in the history
Creates a couple query scopes in the Order model so we can strip all
the carbon bits out of the controller
  • Loading branch information
dshoreman committed Nov 8, 2014
1 parent 1cecdee commit 0f5a2d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
17 changes: 6 additions & 11 deletions controllers/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use BackendMenu;
use Backend\Classes\Controller;
use Carbon\Carbon;
use Dshoreman\Shop\Models\Order;

/**
Expand Down Expand Up @@ -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();
}
Expand Down
14 changes: 14 additions & 0 deletions models/Order.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace DShoreman\Shop\Models;

use Carbon\Carbon;
use Model;

/**
Expand Down Expand Up @@ -36,4 +37,17 @@ class Order extends Model
public $attachOne = [];
public $attachMany = [];

public function scopeCreatedThisMonth($query)
{
return $query->where('created_at', '>=', Carbon::now()->startOfMonth());
}

public function scopeCreatedLastMonth($query)
{
return $query->whereBetween('created_at', [
Carbon::now()->subMonth()->startOfMonth(),
Carbon::now()->subMonth()->endOfMonth()
]);
}

}

0 comments on commit 0f5a2d9

Please sign in to comment.