5 changed files with 177 additions and 29 deletions
-
4app/Http/Controllers/PagesController.php
-
78app/Queries/AdminDashboardQuery.php
-
53resources/views/pages/home.blade.php
-
2resources/views/reports/payments/yearly.blade.php
-
69tests/Unit/Queries/AdminDashboardQueryTest.php
@ -0,0 +1,78 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Queries; |
||||
|
|
||||
|
use App\Entities\Payments\Payment; |
||||
|
use App\Entities\Projects\Project; |
||||
|
use App\Entities\Subscriptions\Subscription; |
||||
|
use Carbon\Carbon; |
||||
|
|
||||
|
/** |
||||
|
* AdminDashboardQuery |
||||
|
*/ |
||||
|
class AdminDashboardQuery |
||||
|
{ |
||||
|
/** |
||||
|
* Get total money earned on an year |
||||
|
* |
||||
|
* @param string|integer $year |
||||
|
* @return integer Amount of earnings |
||||
|
*/ |
||||
|
public function totalEarnings($year) |
||||
|
{ |
||||
|
$totalEarnings = 0; |
||||
|
$payments = Payment::where('date', 'like', $year.'%')->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; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace Tests\Unit\Queries; |
||||
|
|
||||
|
use App\Entities\Payments\Payment; |
||||
|
use App\Entities\Projects\Project; |
||||
|
use App\Entities\Subscriptions\Subscription; |
||||
|
use App\Queries\AdminDashboardQuery; |
||||
|
use Carbon\Carbon; |
||||
|
use Tests\TestCase; |
||||
|
|
||||
|
class AdminDashboardQueryTest extends TestCase |
||||
|
{ |
||||
|
/** @test */ |
||||
|
public function retrieve_total_earnings_on_the_year() |
||||
|
{ |
||||
|
factory(Payment::class)->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()); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue