@@ -17,132 +17,148 @@ public function index(Request $request)
17
17
$ startDate = $ request ->input ('start_date ' , Carbon::now ()->format ('Y-m-d ' ));
18
18
$ endDate = $ request ->input ('end_date ' , Carbon::now ()->addMonths (1 )->format ('Y-m-d ' )); // Default, 1 month from today
19
19
20
- // Retrieve the state of the display_hours_diff checkbox
20
+ // Retrieve the state of the display_hours_diff checkbox
21
21
$ displayHoursDiff = $ request ->input ('display_hours_diff ' , false );
22
22
23
23
// Check that the start date is not greater than the end date
24
24
if (Carbon::parse ($ startDate )->gt (Carbon::parse ($ endDate ))) {
25
25
return redirect ()->route ('production.load.planning ' )->withErrors (['The start date must be before or equal to the end date. ' ]);
26
26
}
27
27
28
+ // Retrieve tasks and services
29
+ $ taches = $ this ->getTasks ($ startDate , $ endDate );
30
+ $ services = $ this ->getServices ();
28
31
29
- // Dans votre contrôleur ou ailleurs où vous avez besoin de cette information
30
- $ countTaskNullRessource = Task::whereNotNull ('order_lines_id ' )->whereDoesntHave ('resources ' )->count ();
31
-
32
- // Collect Tasks
33
- $ taches = Task::with ('service ' )
34
- ->whereBetween ('end_date ' , [$ startDate , $ endDate ])
35
- ->whereNotNull ('order_lines_id ' )
36
- ->where (function (Builder $ query ) {
37
- return $ query ->where ('tasks.type ' , 1 )
38
- ->orWhere ('tasks.type ' , 7 );
39
- })->get ();
40
-
41
- if ($ taches ->count () < 1 && $ countTaskNullRessource < 1 ){
42
- return redirect ()->route ('production.task ' )->with ('error ' , 'No tosk in planning ' );
32
+ // Check if there are no tasks
33
+ if ($ taches ->isEmpty () && $ this ->countTaskNullRessource () < 1 ) {
34
+ return redirect ()->route ('production.task ' )->with ('error ' , 'No task in planning ' );
43
35
}
44
36
45
- $ countTaskNullDate = Task::whereNull ('end_date ' )
46
- ->whereNotNull ('order_lines_id ' )
47
- ->where (function (Builder $ query ) {
48
- return $ query ->where ('tasks.type ' , 1 )
49
- ->orWhere ('tasks.type ' , 7 );
50
- })
51
- ->get ();
52
- $ countTaskNullDate = $ countTaskNullDate ->count ();
53
- // Collect service
54
- $ services = MethodsServices::where (function (Builder $ query ) {
55
- return $ query ->where ('type ' , 1 )
56
- ->orWhere ('type ' , 7 );
57
- })->get ();
58
-
59
- // Array to store the hours worked by service and by day
37
+ // Calculate hours worked and tasks per service per day
38
+ [$ hoursWorkedPerServiceDay , $ tasksPerServiceDay ] = $ this ->calculateHoursAndTasks ($ taches );
39
+
40
+ // Calculate load rates per service per day
41
+ $ rateChargePerServiceDay = $ this ->calculateLoadRates ($ hoursWorkedPerServiceDay );
42
+
43
+ // Create a data structure for load rates
44
+ $ structureRateLoad = $ this ->createLoadRateStructure ($ rateChargePerServiceDay );
45
+
46
+ // Generate all possible dates between the start and end dates
47
+ $ possibleDates = $ this ->generatePossibleDates ($ startDate , $ endDate );
48
+
49
+ // Count tasks with null end date
50
+ $ countTaskNullDate = $ this ->countTaskNullDate ();
51
+
52
+ // Count tasks with null resources
53
+ $ countTaskNullRessource = $ this ->countTaskNullRessource ();
54
+
55
+ return view ('workflow/planning-index ' , compact ('taches ' , 'countTaskNullRessource ' , 'countTaskNullDate ' , 'tasksPerServiceDay ' , 'structureRateLoad ' , 'services ' , 'possibleDates ' , 'startDate ' , 'endDate ' , 'displayHoursDiff ' ));
56
+ }
57
+
58
+ private function getTasks ($ startDate , $ endDate )
59
+ {
60
+ return Task::with ('service ' )
61
+ ->whereBetween ('end_date ' , [$ startDate , $ endDate ])
62
+ ->whereNotNull ('order_lines_id ' )
63
+ ->where (function (Builder $ query ) {
64
+ return $ query ->where ('tasks.type ' , 1 )
65
+ ->orWhere ('tasks.type ' , 7 );
66
+ })->get ();
67
+ }
68
+
69
+ private function getServices ()
70
+ {
71
+ return MethodsServices::where (function (Builder $ query ) {
72
+ return $ query ->where ('type ' , 1 )
73
+ ->orWhere ('type ' , 7 );
74
+ })->get ();
75
+ }
76
+
77
+ private function countTaskNullRessource ()
78
+ {
79
+ return Task::whereNotNull ('order_lines_id ' )->whereDoesntHave ('resources ' )->count ();
80
+ }
81
+
82
+ private function countTaskNullDate ()
83
+ {
84
+ return Task::whereNull ('end_date ' )
85
+ ->whereNotNull ('order_lines_id ' )
86
+ ->where (function (Builder $ query ) {
87
+ return $ query ->where ('tasks.type ' , 1 )
88
+ ->orWhere ('tasks.type ' , 7 );
89
+ })->count ();
90
+ }
91
+
92
+ private function calculateHoursAndTasks ($ taches )
93
+ {
60
94
$ hoursWorkedPerServiceDay = [];
61
- // Array to store tasks by department and day
62
95
$ tasksPerServiceDay = [];
63
96
64
- // Browse tasks and calculate hours worked for each service and day
65
97
foreach ($ taches as $ tache ) {
66
98
$ serviceId = $ tache ['methods_services_id ' ];
67
99
$ jour = (new Carbon ($ tache ['end_date ' ]))->format ('Y-m-d ' ); // Convert the date to Y-m-d format
68
-
69
- // Check if the service already exists in the array
100
+
101
+ // Calculate hours worked
70
102
if (!isset ($ hoursWorkedPerServiceDay [$ serviceId ])) {
71
103
$ hoursWorkedPerServiceDay [$ serviceId ] = [];
72
104
}
73
-
74
- // Add the hours worked to the existing sum for the service and the day
75
105
if (!isset ($ hoursWorkedPerServiceDay [$ serviceId ][$ jour ])) {
76
106
$ hoursWorkedPerServiceDay [$ serviceId ][$ jour ] = $ tache ->TotalTime ();
77
107
} else {
78
108
$ hoursWorkedPerServiceDay [$ serviceId ][$ jour ] += $ tache ->TotalTime ();
79
109
}
80
110
81
- // Section for add task id in tooltip cell
82
- // Check if the service already exists in the array
111
+ // Collect tasks per service per day
83
112
if (!isset ($ tasksPerServiceDay [$ serviceId ])) {
84
113
$ tasksPerServiceDay [$ serviceId ] = [];
85
114
}
86
-
87
- // Check if the day already exists in the array
88
115
if (!isset ($ tasksPerServiceDay [$ serviceId ][$ jour ])) {
89
116
$ tasksPerServiceDay [$ serviceId ][$ jour ] = [];
90
117
}
91
-
92
- /// Add the task ID to the table corresponding to the service and the day
93
118
$ tasksPerServiceDay [$ serviceId ][$ jour ][] = $ tache ->id ;
94
119
}
95
120
96
- // Array to store load rates per service and per day
121
+ return [$ hoursWorkedPerServiceDay , $ tasksPerServiceDay ];
122
+ }
123
+
124
+ private function calculateLoadRates ($ hoursWorkedPerServiceDay )
125
+ {
97
126
$ rateChargePerServiceDay = [];
127
+ $ capacityHebdoService = 16 ; // Hypothetical weekly capacity of 16 hours
98
128
99
- /// Browse the hours worked by service and by day and calculate the load rates
100
129
foreach ($ hoursWorkedPerServiceDay as $ serviceId => $ hoursPerDay ) {
101
130
foreach ($ hoursPerDay as $ jour => $ HoursWorked ) {
102
- //currently the capacity is fixed, see in the future
103
- $ capacityHebdoService = 16 ; // Hypothetical weekly capacity of 16 hours
104
-
105
- // Calculate the percentage charge rate
106
131
$ chargeRate = ($ HoursWorked / $ capacityHebdoService ) * 100 ;
107
-
108
- // Store the charge rate in the array
109
132
$ rateChargePerServiceDay [$ serviceId ][$ jour ] = $ chargeRate ;
110
133
}
111
134
}
112
135
113
- // Create a data structure for load rates
136
+ return $ rateChargePerServiceDay ;
137
+ }
138
+
139
+ private function createLoadRateStructure ($ rateChargePerServiceDay )
140
+ {
114
141
$ structureRateLoad = [];
142
+
115
143
foreach ($ rateChargePerServiceDay as $ serviceId => $ tauxParJour ) {
116
144
foreach ($ tauxParJour as $ jour => $ taux ) {
117
145
$ structureRateLoad [$ jour ][$ serviceId ] = $ taux ;
118
146
}
119
147
}
120
148
121
- // Extract all unique dates from each array into $rateChargePerServiceDay
122
- $ allDatesUniques = [$ startDate ];
123
- foreach ($ rateChargePerServiceDay as $ ratePerService ) {
124
- $ datesService = array_keys ($ ratePerService );
125
- $ allDatesUniques = array_merge ($ allDatesUniques , $ datesService );
126
- }
127
-
128
- // Remove duplicates and sort unique dates
129
- $ allDatesUniques = array_unique ($ allDatesUniques );
130
- sort ($ allDatesUniques );
131
-
132
- // Get smallest and largest date
133
- $ minDate = min ($ allDatesUniques );
134
- $ maxDate = max ($ allDatesUniques );
149
+ return $ structureRateLoad ;
150
+ }
135
151
136
- // Generate all dates between the smallest and largest date
152
+ private function generatePossibleDates ($ startDate , $ endDate )
153
+ {
137
154
$ possibleDates = [];
138
155
$ currentDate = $ startDate ;
156
+
139
157
while ($ currentDate <= $ endDate ) {
140
158
$ possibleDates [] = $ currentDate ;
141
159
$ currentDate = date ('Y-m-d ' , strtotime ($ currentDate . ' +1 day ' ));
142
160
}
143
161
144
-
145
- return view ('workflow/planning-index ' , compact ('taches ' , 'countTaskNullRessource ' , 'countTaskNullDate ' , 'tasksPerServiceDay ' , 'structureRateLoad ' , 'services ' , 'possibleDates ' , 'startDate ' , 'endDate ' , 'displayHoursDiff ' ));
146
-
162
+ return $ possibleDates ;
147
163
}
148
164
}
0 commit comments