|
12 | 12 |
|
13 | 13 | class HomeController extends Controller
|
14 | 14 | {
|
15 |
| - /** |
16 |
| - * Show the application dashboard. |
17 |
| - * |
18 |
| - * @return \Illuminate\Http\Response |
19 |
| - */ |
20 |
| - public function index() |
| 15 | + protected $occurrences; |
| 16 | + |
| 17 | + public function __construct(Occurrence $occurrences) |
| 18 | + { |
| 19 | + $this->occurrences = $occurrences; |
| 20 | + } |
| 21 | + |
| 22 | + public function getOccurrencesForMonth(int $month) |
21 | 23 | {
|
22 |
| - $currentMonth = date('m'); |
23 |
| - $currentYear = date('Y'); |
24 |
| - $occurrences = Occurrence::orderBy('occurs_at') |
25 |
| - // Where occurs_at month and year is the same as current month and year |
26 |
| - ->where(function($query) use($currentMonth, $currentYear) { |
27 |
| - $query->where('occurs_at', 'like', $currentYear . '-' . $currentMonth . '-%'); |
28 |
| - }) |
29 |
| - // Return only occurrences that belong to services that are active |
30 |
| - ->whereHas('service', function($query) { |
31 |
| - $query->where('active', 1); |
32 |
| - })->with(['service.client'])->get(); |
33 |
| - |
34 |
| - if($currentMonth + 1 <= 12){ |
35 |
| - $upcomingMonth = ++$currentMonth; |
36 |
| - if($upcomingMonth < 10) { |
37 |
| - $upcomingMonth = '0' . $upcomingMonth; |
38 |
| - } |
39 |
| - $upcomingOccurrences = Occurrence::orderBy('occurs_at') |
40 |
| - // Where occurs_at month and year is the same as upcoming month and current year |
41 |
| - ->where(function($query) use($upcomingMonth, $currentYear) { |
42 |
| - $query->where('occurs_at', 'like', $currentYear . '-' . $upcomingMonth . '-%'); |
| 24 | + // Change (int 9) to (string '09') |
| 25 | + if($month < 10) { |
| 26 | + $month = '0' . $month; |
| 27 | + } |
| 28 | + |
| 29 | + $year = Carbon::now()->year; |
| 30 | + |
| 31 | + return $this->occurrences->orderBy('occurs_at') |
| 32 | + // Where occurs_at month and year is |
| 33 | + // the same as current month and year |
| 34 | + ->where(function($query) use($month, $year) { |
| 35 | + // $date = 2016-07-% |
| 36 | + $date = $year . '-' . $month . '-%'; |
| 37 | + |
| 38 | + $query->where('occurs_at', 'like', $date); |
43 | 39 | })
|
44 |
| - // Return only occurrences that belong to services that are active |
| 40 | + // Return only occurrences that belong |
| 41 | + // to services that are active |
45 | 42 | ->whereHas('service', function($query) {
|
46 | 43 | $query->where('active', 1);
|
47 |
| - })->with(['service.client'])->get(); |
48 |
| - } |
49 |
| - else { |
50 |
| - $upcomingOccurrences = []; |
| 44 | + }) |
| 45 | + ->with(['service.client']) |
| 46 | + ->get(); |
| 47 | + } |
| 48 | + public function getOccurrencesForCurrentMonth() |
| 49 | + { |
| 50 | + $month = Carbon::now()->month; |
| 51 | + |
| 52 | + return $this->getOccurrencesForMonth($month); |
| 53 | + } |
| 54 | + |
| 55 | + public function getOccurrencesForNextMonth() |
| 56 | + { |
| 57 | + $month = Carbon::now()->month; |
| 58 | + $occurrences = []; |
| 59 | + |
| 60 | + if(++$month <= 12){ |
| 61 | + $occurrences = $this->getOccurrencesForMonth($month); |
51 | 62 | }
|
52 | 63 |
|
53 |
| - $previousUnpaidOccurrences = Occurrence::orderBy('occurs_at') |
54 |
| - // Where occurs_at month and year is the same as upcoming month and current year |
55 |
| - ->where(function($query) use($currentYear, $currentMonth) { |
56 |
| - $query->where('occurs_at', '<', Carbon::create($currentYear, $currentMonth, 1)); |
| 64 | + return $occurrences; |
| 65 | + } |
| 66 | + |
| 67 | + public function getPreviousUnpaidOccurrences() |
| 68 | + { |
| 69 | + $date = Carbon::now(); |
| 70 | + $date->day = 1; |
| 71 | + $date->hour = 0; |
| 72 | + $date->minute = 0; |
| 73 | + $date->second = 0; |
| 74 | + |
| 75 | + return $this->occurrences->orderBy('occurs_at') |
| 76 | + ->where(function($query) use ($date) { |
| 77 | + $query->where('occurs_at', '<', $date); |
57 | 78 | })
|
58 | 79 | ->where('payment_received', 0)
|
59 |
| - // Return only occurrences that belong to services that are active |
| 80 | + // Return only occurrences that belong |
| 81 | + // to services that are active |
60 | 82 | ->whereHas('service', function($query) {
|
61 | 83 | $query->where('active', 1);
|
62 |
| - })->with(['service.client'])->get(); |
| 84 | + }) |
| 85 | + ->with(['service.client']) |
| 86 | + ->get(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Show the application dashboard. |
| 91 | + * |
| 92 | + * @return \Illuminate\Http\Response |
| 93 | + */ |
| 94 | + public function index() |
| 95 | + { |
| 96 | + $occurrencesThisMonth = $this->getOccurrencesForCurrentMonth(); |
| 97 | + |
| 98 | + $occurrencesNextMonth = $this->getOccurrencesForNextMonth(); |
63 | 99 |
|
| 100 | + $previousUnpaidOccurrences = $this->getPreviousUnpaidOccurrences(); |
64 | 101 |
|
65 |
| - return view('home')->with(compact('occurrences', 'upcomingOccurrences', 'previousUnpaidOccurrences')); |
| 102 | + return view('home')->with(compact('occurrencesThisMonth', 'occurrencesNextMonth', 'previousUnpaidOccurrences')); |
66 | 103 | }
|
67 | 104 |
|
68 | 105 | public function report()
|
|
0 commit comments