From 3cf70135f3a480cc2da05405908bd50d972a2882 Mon Sep 17 00:00:00 2001 From: Patrick Lucas Date: Wed, 26 May 2021 10:49:08 +0200 Subject: [PATCH] Only show non-draft invoices on dashboard An invoice cannot be considered due if it has not been sent (and thus still a 'DRAFT'). This change adds a `WHERE status != 'DRAFT'` to each invoice query in the dashboard controller. Additionally, this change replaces `->get()->count()` calls to just `->count()`, which should be equivalent and more efficient. --- .../V1/Dashboard/DashboardController.php | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/V1/Dashboard/DashboardController.php b/app/Http/Controllers/V1/Dashboard/DashboardController.php index f59684cb..79f8cb47 100644 --- a/app/Http/Controllers/V1/Dashboard/DashboardController.php +++ b/app/Http/Controllers/V1/Dashboard/DashboardController.php @@ -58,6 +58,7 @@ class DashboardController extends Controller 'invoice_date', [$start->format('Y-m-d'), $end->format('Y-m-d')] ) + ->where('status', '!=', Invoice::STATUS_DRAFT) ->whereCompany($request->header('company')) ->sum('total') ); @@ -94,6 +95,7 @@ class DashboardController extends Controller $start->subMonth()->endOfMonth(); $salesTotal = Invoice::whereCompany($request->header('company')) + ->where('status', '!=', Invoice::STATUS_DRAFT) ->whereBetween( 'invoice_date', [$startDate->format('Y-m-d'), $start->format('Y-m-d')] @@ -121,11 +123,21 @@ class DashboardController extends Controller 'netProfits' => $netProfits, ]; - $customersCount = User::customer()->whereCompany($request->header('company'))->get()->count(); - $invoicesCount = Invoice::whereCompany($request->header('company'))->get()->count(); - $estimatesCount = Estimate::whereCompany($request->header('company'))->get()->count(); - $totalDueAmount = Invoice::whereCompany($request->header('company'))->sum('due_amount'); - $dueInvoices = Invoice::with('user')->whereCompany($request->header('company'))->where('due_amount', '>', 0)->take(5)->latest()->get(); + $customersCount = User::customer()->whereCompany($request->header('company'))->count(); + $invoicesCount = Invoice::whereCompany($request->header('company')) + ->where('status', '!=', Invoice::STATUS_DRAFT) + ->count(); + $estimatesCount = Estimate::whereCompany($request->header('company'))->count(); + $totalDueAmount = Invoice::whereCompany($request->header('company')) + ->where('status', '!=', Invoice::STATUS_DRAFT) + ->sum('due_amount'); + $dueInvoices = Invoice::with('user') + ->whereCompany($request->header('company')) + ->where('status', '!=', Invoice::STATUS_DRAFT) + ->where('due_amount', '>', 0) + ->take(5) + ->latest() + ->get(); $estimates = Estimate::with('user')->whereCompany($request->header('company'))->take(5)->latest()->get(); return response()->json([