-
-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathApiDashboardController.php
59 lines (49 loc) · 1.93 KB
/
ApiDashboardController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace App\Api\v1\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Routing\Controller;
use Carbon\CarbonPeriod;
use Illuminate\Http\JsonResponse;
use App\Models\{Client, Offer, Payment, Task, Project, Invoice};
class ApiDashboardController extends Controller
{
public function dashboardData(): JsonResponse
{
$startDate = now()->subDays(14)->startOfDay();
$endDate = now()->endOfDay();
$dates = [];
foreach (CarbonPeriod::create($startDate, $endDate) as $date) {
$dates[] = $date->format('Y-m-d');
}
$graphTemplate = array_fill_keys($dates, 0);
$graphs = [
'tasks' => $this->getDailyCounts(Task::class, $startDate, $graphTemplate),
'projects' => $this->getDailyCounts(Project::class, $startDate, $graphTemplate),
'invoices' => $this->getDailyCounts(Invoice::class, $startDate, $graphTemplate),
];
return response()->json([
'totals' => [
'clients' => Client::count(),
'offers' => Offer::count(),
'invoices' => Invoice::count(),
'payments' => doubleval(Payment::sum('amount')/100),
'factures' => $this->totalInvoice()
],
'graphs' => $graphs
]);
}
private function getDailyCounts(string $modelClass, $startDate, array $template): array
{
$counts = $modelClass::whereBetween('created_at', [$startDate, now()])
->selectRaw('DATE_FORMAT(created_at, "%Y-%m-%d") as date, COUNT(*) as count')
->groupBy('date')
->get()
->pluck('count', 'date');
return array_replace($template, $counts->toArray());
}
private function totalInvoice(){
$total = DB::select("select sum(price*quantity) as total from invoice_lines where offer_id is null")[0]->total;
$total = $total/100;
return $total;
}
}