-
-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathDashBoardController.php
180 lines (143 loc) · 5.57 KB
/
DashBoardController.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace App\Api\v1\Controllers\DashBoardApi;
use App\Enums\InvoiceStatus;
use App\Enums\OfferStatus;
use App\Http\Controllers\Controller;
use App\Models\Client;
use App\Models\Dashboard\DashboardStats;
use App\Models\Invoice;
use App\Models\InvoiceLine;
use App\Models\Lead;
use App\Models\Offer;
use App\Models\Payment;
use App\Models\Product;
use App\Models\Project;
use App\Models\Task;
use Illuminate\Support\Facades\DB;
class DashBoardController extends Controller
{
public function getStats()
{
$nb_clients = Client::count();
$nb_projects = Project::count();
$nb_leads = Lead::count();
$nb_tasks = Task::count();
$nb_offers = Offer::count();
$nb_offers_conclude = Offer::where('status', 'like', OfferStatus::won()->getStatus())->count();
$nb_offers_decline = Offer::where('status', 'like', OfferStatus::lost()->getStatus())->count();
$nb_offers_progress = Offer::where('status', 'like', OfferStatus::inProgress()->getStatus())->count();
$nb_invoices = Invoice::count();
$nb_payment = Payment::count();
$dashboardData = new DashboardStats(
$nb_clients,
$nb_projects,
$nb_tasks,
$nb_offers,
$nb_offers_conclude,
$nb_offers_decline,
$nb_offers_progress,
$nb_invoices,
$nb_payment,
$nb_leads
);
return response()->json([
'dashboardData' => $dashboardData,
], 200);
}
public function getBestClients()
{
$liste_clients = Client::all();
$client_achat = collect([]);
for ($i = 0; $i < count($liste_clients); $i++) {
$amount_paid = 0;
$liste_invoice = $liste_clients[$i]->invoices()->where('status', 'like', InvoiceStatus::paid()->getStatus())
->orWhere('status', 'like', InvoiceStatus::partialPaid()->getStatus())->where('client_id', $liste_clients[$i]->id)->get();
for ($j = 0; $j < count($liste_invoice); $j++) {
$amount_paid += $liste_invoice[$j]->getAlreadyPaid()->getBigDecimalAmount();
}
$client_achat->put($liste_clients[$i]->company_name, $amount_paid);
}
$top3 = $client_achat->sortDesc()->take(3);
return response()->json([
"top_3_clients" => $top3
], 200);
}
public function getOfferWon()
{
// Récupérer les offres gagnées
$liste_offres = Offer::where('status', 'like', OfferStatus::won()->getStatus())->get();
$gains = 0;
foreach ($liste_offres as $offre) {
// Récupérer les factures associées à cette offre
$invoices = $offre->invoice()->where('offer_id', $offre->id)->get();
if ($invoices->isNotEmpty()) {
foreach ($invoices as $invoice) {
$gains += $invoice->getAlreadyPaid()->getBigDecimalAmount();
}
}
}
return response()->json([
"offre_gains" => $gains
], 200);
}
public function getVentesMensuelles()
{
// Récupérer les factures avec statut payé ou partiellement payé en 2025
$invoices = Invoice::whereYear('due_at', 2025)
->where(function ($query) {
$query->where('status', InvoiceStatus::partialPaid()->getStatus())
->orWhere('status', InvoiceStatus::paid()->getStatus());
})
->get();
// Initialiser un tableau avec tous les mois à 0
$ventesMensuelles = array_fill(1, 12, 0);
// Ajouter les montants des factures aux mois correspondants
foreach ($invoices as $invoice) {
$mois = (int) $invoice->due_at->format('n'); // Mois sous forme d'entier (1-12)
$ventesMensuelles[$mois] += $invoice->getAlreadyPaid()->getBigDecimalAmount();
}
// Reformater les données pour une meilleure lisibilité
$result = [];
foreach ($ventesMensuelles as $mois => $montant) {
$result[] = [
'mois' => $mois,
'montant' => $montant
]
;
}
// Retourner les données en JSON
return response()->json(['ventes' => $result]);
}
public function getBestProducts()
{
$list_products = Product::all();
$averina = collect([]);
foreach ($list_products as $product) {
$total_amount = InvoiceLine::where('product_id', $product->id)
->whereHas('invoice', function ($query) {
$query->whereIn('status', [InvoiceStatus::partialPaid()->getStatus(), InvoiceStatus::paid()->getStatus(), InvoiceStatus::unpaid()->getStatus()]);
})
->sum(DB::raw('price * quantity'));
$averina->put($product->name, $total_amount / 100);
}
$top3 = $averina->sortDesc()->take(3);
return response()->json(['top_produits' => $top3], 200);
}
public function getInvoicesTotal()
{
$paidAmount = 0;
$remainingAmount = 0;
$invoices = Invoice::whereNotNull('offer_id')->get();
foreach ($invoices as $invoice) {
$paidAmount += $invoice->getAlreadyPaid()->getBigDecimalAmount();
$remainingAmount += $invoice->getTotalPriceAttribute()->getBigDecimalAmount()- $invoice->getAlreadyPaid()->getBigDecimalAmount();
}
return response()->json([
'invoice_total'=>[
'Paye' => $paidAmount,
'Non paye' => $remainingAmount
]
]);
}
}
?>