Skip to content

Commit 37db9d4

Browse files
committed
1 parent 36eef10 commit 37db9d4

File tree

10 files changed

+390
-129
lines changed

10 files changed

+390
-129
lines changed

app/Http/Controllers/HomeController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Carbon\Carbon;
66
use App\Models\User;
7-
use Illuminate\Http\Request;
87
use App\Models\Workflow\Orders;
98
use App\Models\Workflow\Quotes;
109
use App\Models\Products\Products;
@@ -169,9 +168,9 @@ public function index()
169168
$LastProducts = Products::orderBy('id', 'desc')->take(6)->get();
170169

171170
//total price
172-
$data['delivered_month_in_progress'] = $this->deliveryKPIService->getDeliveryMonthlyProgress($CurentMonth, $CurentYear);
171+
$deliveredMonthInProgress = $this->deliveryKPIService->getDeliveryMonthlyProgress($CurentMonth, $CurentYear);
173172

174-
$data['remaining_order'] = $this->orderKPIService->getOrderMonthlyRemaining($CurentMonth, $CurentYear);
173+
$remainingDeliveryOrder = $this->orderKPIService->getOrderMonthlyRemainingToDelivery($CurentMonth, $CurentYear);
175174

176175
return view('dashboard', [
177176
'userRoleCount' => $userRoleCount,
@@ -189,6 +188,8 @@ public function index()
189188
'ServiceGoals' => $ServiceGoals,
190189
'Tasks' => $Tasks,
191190
'EstimatedBudgets' => $EstimatedBudgets,
191+
'deliveredMonthInProgress' => $deliveredMonthInProgress,
192+
'remainingDeliveryOrder' => $remainingDeliveryOrder
192193
])->with('data',$data);
193194
}
194195

app/Http/Controllers/Workflow/OrdersController.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,28 @@ public function __construct(
4343
*/
4444
public function index()
4545
{
46-
$currentYear = Carbon::now()->format('Y');
47-
$ordersDataRate = $this->orderKPIService->getOrdersDataRate();
48-
$orderMonthlyRecap = $this->orderKPIService->getOrderMonthlyRecap($currentYear);
46+
$CurentYear = now()->year;
4947

50-
// Prepare data array
51-
$data = [
52-
'ordersDataRate' => $ordersDataRate,
53-
'orderMonthlyRecap' => $orderMonthlyRecap,
54-
];
55-
56-
return view('workflow/orders-index')->with('data',$data);
48+
// Récupérer les KPI
49+
$deliveredOrdersPercentage = $this->orderKPIService->getDeliveredOrdersPercentage();
50+
$invoicedOrdersPercentage = $this->orderKPIService->getInvoicedOrdersPercentage();
51+
$pendingDeliveries = $this->orderKPIService->getPendingDeliveries();
52+
$lateOrdersCount = $this->orderKPIService->getLateOrdersCount();
53+
$remainingDeliveryOrder = $this->orderKPIService->getOrderMonthlyRemainingToDelivery(now()->month, $CurentYear);
54+
$remainingInvoiceOrder = $this->orderKPIService->getOrderMonthlyRemainingToInvoice();
55+
$data['ordersDataRate']= $this->orderKPIService->getOrdersDataRate();
56+
$data['orderMonthlyRecap'] = $this->orderKPIService->getOrderMonthlyRecap($CurentYear);
57+
$data['orderMonthlyRecapPreviousYear'] = $this->orderKPIService->getOrderMonthlyRecapPreviousYear($CurentYear);
58+
59+
return view('workflow/orders-index', compact(
60+
'deliveredOrdersPercentage',
61+
'invoicedOrdersPercentage',
62+
'pendingDeliveries',
63+
'lateOrdersCount',
64+
'remainingDeliveryOrder',
65+
'remainingInvoiceOrder',
66+
'data',
67+
));
5768
}
5869

5970
/**

app/Services/OpportunitiesKPIService.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ public function getOpportunitiesCount()
5252
public function getQuotesSummary()
5353
{
5454
$quotesWon = Quotes::where('statu', 3)->whereNotNull('opportunities_id')->get();
55-
$totalQuotesWon = $quotesWon->sum(function ($quote) {
55+
$totalQuotesWon = number_format($quotesWon->sum(function ($quote) {
5656
return $quote->getTotalPriceAttribute();
57-
});
57+
}),2);
5858

5959
$quotesLost = Quotes::where('statu', 4)->whereNotNull('opportunities_id')->get();
60-
$totalQuotesLost = $quotesLost->sum(function ($quote) {
60+
$totalQuotesLost = number_format($quotesLost->sum(function ($quote) {
6161
return $quote->getTotalPriceAttribute();
62-
});
62+
}),2);
63+
6364

6465
return compact('totalQuotesWon', 'totalQuotesLost');
6566
}

app/Services/OrderKPIService.php

Lines changed: 200 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,91 @@
22

33
namespace App\Services;
44

5+
use Carbon\Carbon;
6+
use App\Models\Workflow\Orders;
7+
use App\Models\Workflow\Deliverys;
58
use Illuminate\Support\Facades\DB;
69
use App\Models\Workflow\OrderLines;
7-
use Carbon\Carbon;
810

911
class OrderKPIService
1012
{
13+
14+
/**
15+
* Calculates the percentage of orders fully delivered for the current year.
16+
*
17+
* An order is considered fully delivered if the delivered quantity (`delivered_qty`)
18+
* is greater than or equal to the ordered quantity (`qty`) for all associated order lines.
19+
*
20+
* @return float The percentage of orders delivered for the current year.
21+
*/
22+
public function getDeliveredOrdersPercentage()
23+
{
24+
$totalOrders = OrderLines::whereYear('created_at', now()->year)->count();
25+
26+
if ($totalOrders === 0) {
27+
return 0;
28+
}
29+
30+
$deliveredOrders = OrderLines::whereYear('created_at', now()->year)
31+
->where('delivery_status', '=', 3)
32+
->count();
33+
34+
return round(($deliveredOrders / $totalOrders) * 100,2);
35+
}
36+
37+
/**
38+
* Calculates the percentage of orders that are fully invoiced for the current year.
39+
*
40+
* An order is considered fully invoiced if all of its order lines
41+
* have an `invoice_status` of 5, meaning that the line has been fully invoiced.
42+
*
43+
* @return float The percentage of orders that are invoiced for the current year.
44+
*/
45+
public function getInvoicedOrdersPercentage()
46+
{
47+
$totalOrders = OrderLines::whereYear('created_at', now()->year)->count();
48+
49+
if ($totalOrders === 0) {
50+
return 0;
51+
}
52+
53+
$invoicedOrders = OrderLines::whereYear('created_at', now()->year)
54+
->where('invoice_status', 3)
55+
->count();
56+
57+
return round(($invoicedOrders / $totalOrders) * 100,2);
58+
}
59+
60+
/**
61+
* Get the number of backorders for the current year.
62+
*
63+
* An order is considered backordered if the expected delivery date
64+
* has passed and it has not yet been fully delivered (delivered quantity < ordered quantity).
65+
*
66+
* @return int The number of backorders for the current year.
67+
*/
68+
public function getLateOrdersCount()
69+
{
70+
return Orders::whereYear('created_at', now()->year)
71+
->whereHas('orderLines', function($query) {
72+
$query->where('delivery_status', 1) // delivered quantity < ordered quantity
73+
->where('delivery_date', '<', now()); // expected delivery date has passed
74+
})->count();
75+
}
76+
77+
/**
78+
* Retrieve all orders that have quantities still to be delivered for the current year.
79+
*
80+
* An order is considered to have a pending delivery if statu != 3
81+
*
82+
* @return int of orders with pending deliveries for the current year.
83+
*/
84+
public function getPendingDeliveries()
85+
{
86+
return Orders::where('statu', '!=', '3')->count();
87+
}
88+
89+
1190
/**
1291
* Retrieves the monthly summary of order for the current year.
1392
*
@@ -29,12 +108,34 @@ public function getOrderMonthlyRecap($year)
29108
->get();
30109
}
31110

111+
/**
112+
* Retrieves the monthly summary of order for the last year.
113+
*
114+
* @return \Illuminate\Support\Collection
115+
*/
116+
public function getOrderMonthlyRecapPreviousYear($year)
117+
{
118+
$lastyear = $year-1;
119+
return DB::table('order_lines')
120+
->selectRaw('
121+
MONTH(delivery_date) AS month,
122+
SUM((selling_price * qty)-(selling_price * qty)*(discount/100)) AS orderSum
123+
')
124+
->leftJoin('orders', function($join) {
125+
$join->on('order_lines.orders_id', '=', 'orders.id')
126+
->where('orders.type', '=', 1);
127+
})
128+
->whereYear('order_lines.created_at', $lastyear)
129+
->groupByRaw('MONTH(order_lines.delivery_date)')
130+
->get();
131+
}
132+
32133
/**
33134
* Retrieves the monthly summary of order for the current month.
34135
*
35136
* @return \Illuminate\Support\Collection
36137
*/
37-
public function getOrderMonthlyRemaining($month ,$year)
138+
public function getOrderMonthlyRemainingToDelivery($month ,$year)
38139
{
39140
return DB::table('order_lines')
40141
->selectRaw('
@@ -46,6 +147,21 @@ public function getOrderMonthlyRemaining($month ,$year)
46147
->get();
47148
}
48149

150+
/**
151+
* Retrieves the monthly summary of order for the current month.
152+
*
153+
* @return \Illuminate\Support\Collection
154+
*/
155+
public function getOrderMonthlyRemainingToInvoice()
156+
{
157+
return DB::table('order_lines')
158+
->selectRaw('
159+
FLOOR(SUM((selling_price * qty)-(selling_price * qty)*(discount/100))) AS orderSum
160+
')
161+
->where('invoice_status', 1)
162+
->get();
163+
}
164+
49165
/**
50166
* Retrieves the total amount summary of order for the comming current year.
51167
*
@@ -79,4 +195,86 @@ public function getOrdersDataRate()
79195
->groupBy('statu')
80196
->get();
81197
}
198+
199+
/**
200+
* Calculate the order completion rate for the current year.
201+
*
202+
* An order is considered completed if all of its lines are fully delivered.
203+
*
204+
* @return float The order completion rate in percentage.
205+
*/
206+
public function getOrderCompletionRate()
207+
{
208+
$totalOrders = Orders::whereYear('created_at', now()->year)->count();
209+
210+
if ($totalOrders === 0) {
211+
return 0;
212+
}
213+
214+
$completedOrders = Orders::whereYear('created_at', now()->year)
215+
->where('statu', 3)
216+
->count();
217+
218+
return ($completedOrders / $totalOrders) * 100;
219+
}
220+
221+
/**
222+
* Calculate the average processing time of an order for the current year.
223+
*
224+
* The processing time is the difference between the order creation date
225+
* and the date of the last associated delivery.
226+
*
227+
* @return float The average processing time in days.
228+
*/
229+
public function getAverageOrderProcessingTime()
230+
{
231+
$orders = Orders::whereYear('created_at', now()->year)
232+
->whereHas('orderLines', function($query) {
233+
$query->whereColumn('delivered_qty', '>=', 'qty');
234+
})->get();
235+
236+
if ($orders->isEmpty()) {
237+
return 0;
238+
}
239+
240+
$totalDays = $orders->map(function($order) {
241+
$lastDeliveryDate = Deliverys::where('order_id', $order->id)
242+
->latest('created_at')
243+
->value('created_at');
244+
245+
return $lastDeliveryDate ? $lastDeliveryDate->diffInDays($order->created_at) : 0;
246+
})->sum();
247+
248+
return $totalDays / $orders->count();
249+
}
250+
251+
/**
252+
* Retrieve customers sorted by order volume for the current year.
253+
*
254+
* @param int $limit The number of customers to retrieve (default 5).
255+
* @return \Illuminate\Database\Eloquent\Collection Collection of customers sorted by order volume.
256+
*/
257+
public function getTopCustomersByOrderVolume($limit = 5)
258+
{
259+
return Orders::select('companies_id', DB::raw('COUNT(*) as order_count'))
260+
->whereYear('created_at', now()->year)
261+
->groupBy('companies_id')
262+
->orderBy('order_count', 'desc')
263+
->take($limit)
264+
->with('companie') // Assuming a relationship with companie model
265+
->get();
266+
}
267+
268+
/**
269+
* Get the number of pending orders for the current year.
270+
*
271+
* An order is pending if it is not fully delivered and the remaining quantity to be delivered is > 0.
272+
*
273+
* @return int The number of pending orders.
274+
*/
275+
public function getPendingOrdersCount()
276+
{
277+
return Orders::whereYear('created_at', now()->year)->where('statu', '!=', 3)->count();
278+
}
279+
82280
}

resources/lang/en/general_content.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,10 @@
588588
'no_product_trans_key' => 'No product, go product page for add item',
589589
'announcement_trans_key' => 'Announcements',
590590
'order_to_be_delivered_trans_key' => 'Order to be delivered',
591+
'order_to_be_invoiced_trans_key' => 'Order to be invoiced',
591592
'delivered_month_in_progress_trans_key' => 'Total Delivered for the month',
592593
'remaining_month_trans_key' => 'Total remaining to deliver',
594+
'remaining_invoice_month_trans_key' => 'Total remaining to invoice',
593595
'niko_niko_team_trans_key' => 'Niko Niko - Team Moods Today',
594596
'no_niko_niko_team_trans_key' => 'No team mood today',
595597

resources/lang/fr/general_content.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,10 @@
588588
'no_product_trans_key' => 'Aucun produit, accédez à la page du produit pour ajouter un article',
589589
'announcement_trans_key' => 'Annonces',
590590
'order_to_be_delivered_trans_key' => 'Commandes à livrée(s)',
591+
'order_to_be_invoiced_trans_key' => 'Commandes à facturée(s)',
591592
'delivered_month_in_progress_trans_key' => 'Total livré du mois',
592593
'remaining_month_trans_key' => 'Total restant à livrer',
594+
'remaining_invoice_month_trans_key' => 'Total restant à facturer',
593595
'niko_niko_team_trans_key' => 'Niko Niko - Humeur de l\'équipe aujourd\'hui',
594596
'no_niko_niko_team_trans_key' => 'Aucune humeur aujourd\'hui',
595597

resources/views/dashboard.blade.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,16 @@
281281

282282
<!-- TABLE: DELIVERED -->
283283
<div class="col-lg-6 col-md-12">
284-
<x-adminlte-small-box title="{{ __('general_content.delivered_month_in_progress_trans_key') }}"
285-
text="{{ $data['delivered_month_in_progress'][0]->orderSum ?? 0}} {{ $Factory->curency }}"
284+
<x-adminlte-small-box title="{{ number_format($deliveredMonthInProgress[0]->orderSum,2) ?? 0}} {{ $Factory->curency }}"
285+
text="{{ __('general_content.delivered_month_in_progress_trans_key') }}"
286286
icon="icon fas fa-info"
287287
theme="yellow"
288288
url="{{ route('orders') }}"
289289
url-text="{{ __('general_content.view_details_trans_key') }}"/>
290290
</div>
291291
<div class="col-lg-6 col-md-12">
292-
<x-adminlte-small-box title="{{ __('general_content.remaining_month_trans_key') }}"
293-
text="{{ $data['remaining_order'][0]->orderSum ?? 0 -$data['delivered_month_in_progress'][0]->orderSum ?? 0}} {{ $Factory->curency }}"
292+
<x-adminlte-small-box title="{{ number_format($remainingDeliveryOrder[0]->orderSum ?? 0 -$remainingDeliveryOrder[0]->orderSum ?? 0 ,2)}} {{ $Factory->curency }}"
293+
text="{{ __('general_content.remaining_month_trans_key') }}"
294294
icon="icon fas fa-info"
295295
theme="danger"
296296
url="{{ route('orders') }}"
@@ -676,7 +676,7 @@
676676
data: lineChartData,
677677
options: lineChartOptions
678678
})
679-
</script>
679+
</script>
680680

681681
<script>
682682

0 commit comments

Comments
 (0)