From 910232f4d3efb18cc44c87fd9ea1f7da9fe5f7e5 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 2 Nov 2017 14:00:45 +0800 Subject: [PATCH] Fill out dashboard items with AdminDashboardQuery object --- app/Http/Controllers/PagesController.php | 4 +- app/Queries/AdminDashboardQuery.php | 78 +++++++++++++++++++++++ resources/views/pages/home.blade.php | 53 ++++++++------- resources/views/reports/payments/yearly.blade.php | 2 +- tests/Unit/Queries/AdminDashboardQueryTest.php | 69 ++++++++++++++++++++ 5 files changed, 177 insertions(+), 29 deletions(-) create mode 100644 app/Queries/AdminDashboardQuery.php create mode 100644 tests/Unit/Queries/AdminDashboardQueryTest.php diff --git a/app/Http/Controllers/PagesController.php b/app/Http/Controllers/PagesController.php index 8680906..b2cada3 100644 --- a/app/Http/Controllers/PagesController.php +++ b/app/Http/Controllers/PagesController.php @@ -6,7 +6,9 @@ class PagesController extends Controller { public function home() { - return view('pages.home'); + return view('pages.home', [ + 'queriedYear' => request('year', date('Y')), + ]); } public function about() diff --git a/app/Queries/AdminDashboardQuery.php b/app/Queries/AdminDashboardQuery.php new file mode 100644 index 0000000..9c6c8e9 --- /dev/null +++ b/app/Queries/AdminDashboardQuery.php @@ -0,0 +1,78 @@ +get(); + foreach ($payments as $payment) { + if ($payment->in_out == 1) { + $totalEarnings += $payment->amount; + } else { + $totalEarnings -= $payment->amount; + } + } + return $totalEarnings; + } + + /** + * Get number of projects that has been finished on a year + * + * @param string|integer $year + * @return integer Number of finished projects + */ + public function totalFinishedProjects($year) + { + return Project::where('status_id', 4)->where('start_date', 'like', $year.'%')->count(); + } + + public function currentOutstandingCustomerPayment($year) + { + // On Progress, Done, On Hold + $projects = Project::whereIn('status_id', [2, 3, 6]) + ->where('start_date', 'like', $year.'%') + ->with('payments') + ->get(); + + $filteredProjects = $projects->filter(function ($project) { + return $project->cashInTotal() < $project->project_value; + })->values(); + + $oustandingPaymentTotal = 0; + + foreach ($filteredProjects as $project) { + $oustandingPaymentTotal += $project->project_value - $project->cashInTotal(); + } + + return $oustandingPaymentTotal; + } + + public function upcomingSubscriptionDueDatesList() + { + $subscriptions = Subscription::get(); + + $filteredSubscriptions = $subscriptions->filter(function ($subscription) { + return $subscription->status_id == 1 + && Carbon::parse($subscription->due_date)->diffInDays(Carbon::now()) < 60; + }); + + return $filteredSubscriptions; + } +} diff --git a/resources/views/pages/home.blade.php b/resources/views/pages/home.blade.php index 13aae94..ce0b4ae 100755 --- a/resources/views/pages/home.blade.php +++ b/resources/views/pages/home.blade.php @@ -6,15 +6,14 @@ @section('content-dashboard') + +
-
+
Project Status Stats
@foreach($projectStatuses::all() as $statusId => $status) - @if ($statusId == 4) -
- @endif -
+
@include('view-components.dashboard-panel', [ 'class' => $projectStatuses->getColorById($statusId), 'icon' => $projectStatuses->getIconById($statusId), @@ -23,51 +22,51 @@ 'linkRoute' => route('projects.index', ['status' => $statusId]), ])
- @if ($statusId == 3) -
- @endif @endforeach
-
+
Earnings Stats
    -
  • Earnings (2017) {{ formatRp(1000000) }}
  • -
  • Finished Project (2017) 0 Projects
  • -
  • Receiveable Earnings {{ formatRp(1000000) }}
  • +
  • Yearly Earnings ({{ $queriedYear }}) {{ $totalEarnings }}
  • +
  • Finished Projects ({{ $queriedYear }}) {{ $totalFinishedProjects }} Projects
  • +
  • Receiveable Earnings {{ $currentOutstandingCustomerPayment }}
Upcoming Subscriptions Due Dates -
+
- - - - + + + - @foreach(range(1, 4) as $subscription) + @foreach(AdminDashboardQuery::upcomingSubscriptionDueDatesList() as $subscription) - - - - + + + @endforeach
ProjectItemsTagihanDue Date@lang('customer.customer')@lang('invoice.amount')@lang('subscription.due_date')
Project {{ $subscription }}Hosting & Domain{{ formatRp(rand(1, 3).'000000') }}2017-12-01{{ link_to_route('subscriptions.show', $subscription->domain_name, [$subscription->id]) }}{{ formatRp($subscription->domain_price + $subscription->hosting_price) }} + {{ $subscription->due_date }} + +
diff --git a/resources/views/reports/payments/yearly.blade.php b/resources/views/reports/payments/yearly.blade.php index 7d1ee72..be9a61c 100755 --- a/resources/views/reports/payments/yearly.blade.php +++ b/resources/views/reports/payments/yearly.blade.php @@ -26,7 +26,7 @@

Detail Laporan

-
+
diff --git a/tests/Unit/Queries/AdminDashboardQueryTest.php b/tests/Unit/Queries/AdminDashboardQueryTest.php new file mode 100644 index 0000000..195e0f6 --- /dev/null +++ b/tests/Unit/Queries/AdminDashboardQueryTest.php @@ -0,0 +1,69 @@ +create(['in_out' => 1, 'amount' => 500, 'date' => '2015-01-04']); + factory(Payment::class, 2)->create(['in_out' => 1, 'amount' => 1000, 'date' => '2015-03-04']); + factory(Payment::class, 2)->create(['in_out' => 0, 'amount' => 200, 'date' => '2015-09-04']); + factory(Payment::class)->create(['in_out' => 1, 'amount' => 500, 'date' => '2016-01-04']); + + $this->assertEquals(2100, (new AdminDashboardQuery)->totalEarnings('2015')); + $this->assertEquals(500, (new AdminDashboardQuery)->totalEarnings('2016')); + } + + /** @test */ + public function retrieve_total_finished_projects_on_the_year() + { + factory(Project::class)->create(['status_id' => 4, 'start_date' => '2015-01-04']); + factory(Project::class, 2)->create(['status_id' => 4, 'start_date' => '2015-03-04']); + factory(Project::class, 2)->create(['status_id' => 5, 'start_date' => '2015-09-04']); + factory(Project::class)->create(['status_id' => 4, 'start_date' => '2016-01-04']); + + $this->assertEquals(3, (new AdminDashboardQuery)->totalFinishedProjects('2015')); + $this->assertEquals(1, (new AdminDashboardQuery)->totalFinishedProjects('2016')); + } + + /** @test */ + public function retrieve_current_outstanding_customer_payment() + { + $project = factory(Project::class)->create(['project_value' => 2000, 'status_id' => 2, 'start_date' => '2015-01-04']); + factory(Payment::class)->create(['project_id' => $project->id, 'amount' => 1000]); + + $project = factory(Project::class)->create(['project_value' => 2000, 'status_id' => 3, 'start_date' => '2015-03-04']); + factory(Payment::class)->create(['project_id' => $project->id, 'amount' => 1000]); + + $project = factory(Project::class)->create(['project_value' => 2000, 'status_id' => 1, 'start_date' => '2015-09-04']); + factory(Payment::class)->create(['project_id' => $project->id, 'amount' => 1000]); + + $project = factory(Project::class)->create(['project_value' => 2000, 'status_id' => 3, 'start_date' => '2016-01-04']); + factory(Payment::class)->create(['project_id' => $project->id, 'amount' => 1000]); + + $this->assertEquals(2000, (new AdminDashboardQuery)->currentOutstandingCustomerPayment('2015')); + $this->assertEquals(1000, (new AdminDashboardQuery)->currentOutstandingCustomerPayment('2016')); + } + + /** @test */ + public function retrieve_upcoming_customer_subscription_due_dates_list() + { + $dueDate = Carbon::now()->addMonth()->format('Y-m-d'); + + factory(Subscription::class)->create(['due_date' => $dueDate]); + factory(Subscription::class)->create(['due_date' => $dueDate]); + factory(Subscription::class)->create(['due_date' => $dueDate, 'status_id' => 0]); + factory(Subscription::class)->create(['due_date' => Carbon::now()->addMonths(3)->format('Y-m-d')]); + + $this->assertCount(2, (new AdminDashboardQuery)->upcomingSubscriptionDueDatesList()); + } +}
Bulan