Skip to content

Commit 8dd387c

Browse files
committed
1 parent 0d842a4 commit 8dd387c

File tree

8 files changed

+272
-141
lines changed

8 files changed

+272
-141
lines changed

app/Http/Controllers/Planning/TaskController.php

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Models\Planning\Status;
1010
use App\Models\Workflow\Orders;
1111
use App\Models\Workflow\Quotes;
12+
use App\Services\TaskKPIService;
1213
use App\Models\Products\Products;
1314
use Illuminate\Support\Facades\DB;
1415
use App\Models\Workflow\OrderLines;
@@ -23,6 +24,14 @@
2324

2425
class TaskController extends Controller
2526
{
27+
28+
protected $taskKPIService;
29+
30+
public function __construct(TaskKPIService $taskKPIService)
31+
{
32+
$this->taskKPIService = $taskKPIService;
33+
}
34+
2635
/**
2736
* @return \Illuminate\Contracts\View\View
2837
*/
@@ -154,64 +163,17 @@ public function sync(Request $request)
154163
*/
155164
public function statu(Request $request)
156165
{
157-
// Number of current OFs
158-
$tasksOpen = Task::whereHas('status', function($query) {
159-
$query->where('title', 'Open');
160-
})->count();
161-
162-
$tasksInProgress = Task::whereHas('status', function($query) {
163-
$query->where('title', 'In Progress');
164-
})->count();
165-
166-
// État des OF
167-
$tasksPending = Task::whereHas('status', function($query) {
168-
$query->where('title', 'Pending');
169-
})->count();
170-
171-
$tasksOngoing = Task::whereHas('status', function($query) {
172-
$query->where('title', 'Supplied');
173-
})->count();
174-
175-
$tasksCompleted = Task::whereHas('status', function($query) {
176-
$query->where('title', 'Finished');
177-
})->count();
178-
179-
// Calculation of the average OF processing time
180-
$averageProcessingTime = 0;
181-
$tasksWithEndDate = Task::whereNotNull('end_date')->get();
182-
if($tasksWithEndDate->count() > 0){
183-
$totalTime = $tasksWithEndDate->sum(function ($task) {
184-
return $task->getTotalLogTime() * 3600; //in second time
185-
});
186-
$averageProcessingTime = $totalTime / $tasksWithEndDate->count();
187-
}
188-
189-
// User productivity
190-
$userProductivity = DB::table('task_activities')
191-
->join('users', 'task_activities.user_id', '=', 'users.id')
192-
->select('users.name', DB::raw('count(task_activities.id) as tasks_count'))
193-
->groupBy('users.name')
194-
->get();
195-
196-
//Ressources Time
197-
$totalResourcesAllocated = TaskResources::count();
198-
$tasks = Task::with('resources')->get();
199-
200-
$resourceHours = [];
201-
202-
foreach ($tasks as $task) {
203-
foreach ($task->resources as $resource) {
204-
$resourceName = $resource->label;
205-
$totalTime = $task->TotalTime();
206-
207-
if (array_key_exists($resourceName, $resourceHours)) {
208-
$resourceHours[$resourceName] += $totalTime;
209-
} else {
210-
$resourceHours[$resourceName] = $totalTime;
211-
}
212-
}
213-
}
214-
166+
$tasksOpen = $this->taskKPIService->getOpenTasksCount();
167+
$tasksInProgress = $this->taskKPIService->getInProgressTasksCount();
168+
$tasksPending = $this->taskKPIService->getPendingTasksCount();
169+
$tasksOngoing = $this->taskKPIService->getSuppliedTasksCount();
170+
$tasksCompleted = $this->taskKPIService->getFinishedTasksCount();
171+
$averageProcessingTime = $this->taskKPIService->getAverageProcessingTime();
172+
$userProductivity = $this->taskKPIService->getUserProductivity();
173+
$totalResourcesAllocated = $this->taskKPIService->getTotalResourcesAllocated();
174+
$resourceHours = $this->taskKPIService->getResourceHours();
175+
$totalProducedHours = $this->taskKPIService->getTotalProducedHoursCurrentMonth();
176+
$averageTRS = $this->taskKPIService->getMonthlyAverageTRS();
215177

216178
return view('workflow/task-statu', compact(
217179
'tasksOpen',
@@ -222,7 +184,9 @@ public function statu(Request $request)
222184
'averageProcessingTime',
223185
'userProductivity',
224186
'totalResourcesAllocated',
225-
'resourceHours'
187+
'resourceHours',
188+
'totalProducedHours',
189+
'averageTRS'
226190
), ['TaskId' => $request->id]);
227191
}
228192
}

app/Livewire/TaskStatu.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Illuminate\Support\Facades\Notification;
1717
use App\Models\Products\StockLocationProducts;
1818
use App\Notifications\NonConformityNotification;
19-
use Symfony\Component\Validator\Constraints\NotNull;
2019

2120
class TaskStatu extends Component
2221
{
@@ -42,7 +41,7 @@ class TaskStatu extends Component
4241
private $RecalculateBooleanValue = 0;
4342
public $end_date;
4443

45-
public $tasksOpen, $tasksInProgress, $tasksPending, $tasksOngoing, $tasksCompleted, $averageProcessingTime, $userProductivity, $totalResourcesAllocated, $resourceHours;
44+
public $tasksOpen, $tasksInProgress, $tasksPending, $tasksOngoing, $tasksCompleted, $averageProcessingTime, $userProductivity, $totalResourcesAllocated, $resourceHours, $totalProducedHours, $averageTRS;
4645

4746
public $StockLocationsProducts = null;
4847

app/Services/TaskKPIService.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use Carbon\Carbon;
6+
use App\Models\Planning\Task;
7+
use Illuminate\Support\Facades\DB;
8+
use App\Models\Planning\TaskResources;
9+
10+
class TaskKPIService
11+
{
12+
// Number of tasks with status "Open"
13+
public function getOpenTasksCount()
14+
{
15+
return Task::whereHas('status', function($query) {
16+
$query->where('title', 'Open');
17+
})->count();
18+
}
19+
20+
// Number of tasks with status "In Progress"
21+
public function getInProgressTasksCount()
22+
{
23+
return Task::whereHas('status', function($query) {
24+
$query->where('title', 'In Progress');
25+
})->count();
26+
}
27+
28+
// Number of tasks with status "Pending"
29+
public function getPendingTasksCount()
30+
{
31+
return Task::whereHas('status', function($query) {
32+
$query->where('title', 'Pending');
33+
})->count();
34+
}
35+
36+
// Number of tasks with status "Supplied"
37+
public function getSuppliedTasksCount()
38+
{
39+
return Task::whereHas('status', function($query) {
40+
$query->where('title', 'Supplied');
41+
})->count();
42+
}
43+
44+
// Number of tasks with status "Finished"
45+
public function getFinishedTasksCount()
46+
{
47+
return Task::whereHas('status', function($query) {
48+
$query->where('title', 'Finished');
49+
})->count();
50+
}
51+
52+
// Calculation of average task processing time
53+
public function getAverageProcessingTime()
54+
{
55+
$averageProcessingTime = 0;
56+
$tasksWithEndDate = Task::whereNotNull('end_date')->get();
57+
58+
if ($tasksWithEndDate->count() > 0) {
59+
$totalTime = $tasksWithEndDate->sum(function ($task) {
60+
return $task->getTotalLogTime() * 3600; // en secondes
61+
});
62+
$averageProcessingTime = $totalTime / $tasksWithEndDate->count();
63+
}
64+
65+
return $averageProcessingTime;
66+
}
67+
68+
// Productivity per user
69+
public function getUserProductivity()
70+
{
71+
return DB::table('task_activities')
72+
->join('users', 'task_activities.user_id', '=', 'users.id')
73+
->select('users.name', DB::raw('count(task_activities.id) as tasks_count'))
74+
->groupBy('users.name')
75+
->get();
76+
}
77+
78+
// Total number of resources allocated
79+
public function getTotalResourcesAllocated()
80+
{
81+
return TaskResources::count();
82+
}
83+
84+
// Hours allocated per resource
85+
public function getResourceHours()
86+
{
87+
$tasks = Task::with('resources')->get();
88+
$resourceHours = [];
89+
90+
foreach ($tasks as $task) {
91+
foreach ($task->resources as $resource) {
92+
$resourceName = $resource->label;
93+
$totalTime = $task->TotalTime();
94+
95+
if (array_key_exists($resourceName, $resourceHours)) {
96+
$resourceHours[$resourceName] += $totalTime;
97+
} else {
98+
$resourceHours[$resourceName] = $totalTime;
99+
}
100+
}
101+
}
102+
103+
return $resourceHours;
104+
}
105+
106+
/**
107+
* Calculates the total hours produced in the current month
108+
*
109+
* @return float
110+
*/
111+
public function getTotalProducedHoursCurrentMonth(): float
112+
{
113+
$currentMonthStart = Carbon::now()->startOfMonth();
114+
$currentMonthEnd = Carbon::now()->endOfMonth();
115+
116+
$tasks = Task::whereBetween('start_date', [$currentMonthStart, $currentMonthEnd])
117+
->whereNotNull('end_date')
118+
->get();
119+
120+
$totalHours = $tasks->sum(function ($task) {
121+
return $task->getTotalLogTime();
122+
});
123+
124+
return round($totalHours, 2);
125+
}
126+
127+
/**
128+
* Calculates the TRS in the current month
129+
*
130+
* @return float
131+
*/
132+
public function getMonthlyAverageTRS()
133+
{
134+
$currentMonth = Carbon::now()->month;
135+
$currentYear = Carbon::now()->year;
136+
137+
// Retrieve tasks for the current month
138+
$tasks = Task::whereMonth('start_date', $currentMonth)
139+
->whereYear('start_date', $currentYear)
140+
->get();
141+
142+
if ($tasks->count() === 0) {
143+
return 0; // Returns 0 if no task
144+
}
145+
146+
// Calculate the sum of the TRS
147+
$totalTRS = $tasks->sum(function ($task) {
148+
return $task->getTRSAttribute();
149+
});
150+
151+
// Calculate the average TRS
152+
return $totalTRS / $tasks->count();
153+
}
154+
155+
}

resources/lang/en/general_content.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@
591591
'order_delivered_trans_key' => 'Order delivered',
592592
'order_to_be_invoiced_trans_key' => 'Order to be invoiced',
593593
'order_invoiced_trans_key' => 'Order invoiced',
594+
'order_waiting_trans_key' => 'Wainting orders',
594595
'delivered_month_in_progress_trans_key' => 'Total Delivered for the month',
595596
'remaining_month_trans_key' => 'Total remaining to deliver',
596597
'remaining_invoice_month_trans_key' => 'Total remaining to invoice',
@@ -723,7 +724,9 @@
723724
'user_productivity_trans_key' => 'User Productivity',
724725
'task_count_trans_key' => 'Number of tasks',
725726
'total_hours_per_resource_trans_key' => 'Total hours per resource',
727+
'total_hours_per_month_trans_key' => 'Hours produced over the month',
726728
'trs_trans_key' => 'OEE',
729+
'trs_per_month_trans_key' => 'OEE over the month',
727730

728731
//DELIVERY
729732
'delivery_notes_trans_key' => 'Delivery notes',

resources/lang/fr/general_content.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@
591591
'order_delivered_trans_key' => 'Commandes livrée(s)',
592592
'order_to_be_invoiced_trans_key' => 'Commandes à facturée(s)',
593593
'order_invoiced_trans_key' => 'Commandes facturée(s)',
594+
'order_waiting_trans_key' => 'Commandes en attente(s)',
594595
'delivered_month_in_progress_trans_key' => 'Total livré du mois',
595596
'remaining_month_trans_key' => 'Total restant à livrer',
596597
'remaining_invoice_month_trans_key' => 'Total restant à facturer',
@@ -623,7 +624,7 @@
623624
'new_activity_trans_key' => 'Nouvelle activité',
624625
'new_event_trans_key' => 'Nouveau événement',
625626
'opportunities_list_trans_key' => 'Liste des opportunités',
626-
'opportunities_count_trans_key' => 'Les opportunités comptent',
627+
'opportunities_count_trans_key' => 'Nombre d\'opp',
627628
'total_amount_won_trans_key' => 'Montant total gagné',
628629
'total_amount_lost_trans_key' => 'Montant total perdu',
629630
'opportunities_by_company_trans_key' => 'Opportunités par entreprise',
@@ -723,7 +724,9 @@
723724
'user_productivity_trans_key' => 'Productivité des utilisateurs',
724725
'task_count_trans_key' => 'Nombre de tâches',
725726
'total_hours_per_resource_trans_key' => 'Heures totales par ressource',
727+
'total_hours_per_month_trans_key' => 'Heures produites sur le mois',
726728
'trs_trans_key' => 'TRS',
729+
'trs_per_month_trans_key' => 'TRS sur le mois',
727730

728731
//DELIVERY
729732
'delivery_notes_trans_key' => 'Bons de livraisons',

0 commit comments

Comments
 (0)