@@ -26,47 +26,114 @@ public function __construct()
2626 */
2727 public function index (Request $ request )
2828 {
29- // redirect user to controls list
30- if (Auth::User ()->role === 5 ) {
29+ // Redirect user to controls list if role is 5
30+ if (Auth::user ()->role === 5 ) {
3131 return redirect ('/bob/index ' );
3232 }
3333
34- // count active domains
35- $ active_domains_count = DB ::table ('controls ' )
36- ->select ('measures.domain_id ' )
34+ // Fetch counts and data using optimized queries
35+ $ activeDomainsCount = $ this ->getActiveDomainsCount ();
36+ $ controlsCount = $ this ->getControlsCount ();
37+ $ activeMeasuresCount = $ this ->getActiveMeasuresCount ();
38+ $ controlsMadeCount = $ this ->getControlsMadeCount ();
39+ $ controlsNeverMade = $ this ->getControlsNeverMade ();
40+ $ planedControlsThisMonthCount = $ this ->getPlanedControlsThisMonthCount ();
41+ $ lateControlsCount = $ this ->getLateControlsCount ();
42+ $ actionPlansCount = $ this ->getActionPlansCount ();
43+
44+ $ activeControls = $ this ->getActiveControls ();
45+ $ controlsTodo = $ this ->getControlsTodo ();
46+ $ expandedControls = $ this ->getExpandedControls ();
47+
48+ // Store counts in session
49+ $ request ->session ()->put ([
50+ 'planed_controls_this_month_count ' => $ planedControlsThisMonthCount ,
51+ 'late_controls_count ' => $ lateControlsCount ,
52+ 'action_plans_count ' => $ actionPlansCount ,
53+ ]);
54+
55+ // Return view with data
56+ return view ('welcome ' , [
57+ 'active_domains_count ' => $ activeDomainsCount ,
58+ 'controls_count ' => $ controlsCount ,
59+ 'active_measures_count ' => $ activeMeasuresCount ,
60+ 'controls_made_count ' => $ controlsMadeCount ,
61+ 'controls_never_made ' => $ controlsNeverMade ,
62+ 'active_controls ' => $ activeControls ,
63+ 'controls_todo ' => $ controlsTodo ,
64+ 'action_plans_count ' => $ actionPlansCount ,
65+ 'late_controls_count ' => $ lateControlsCount ,
66+ 'controls ' => $ expandedControls ,
67+ ]);
68+ }
69+
70+ private function getActiveDomainsCount ()
71+ {
72+ return DB ::table ('controls ' )
3773 ->join ('control_measure ' , 'controls.id ' , '= ' , 'control_id ' )
3874 ->join ('measures ' , 'control_measure.measure_id ' , '= ' , 'measures.id ' )
39- ->whereIn ('status ' , [0 ,1 ])
40- ->distinct ()
41- ->get ()
42- -> count ();
75+ ->whereIn ('status ' , [0 , 1 ])
76+ ->distinct (' measures.domain_id ' )
77+ ->count ( ' measures.domain_id ' );
78+ }
4379
44- // count all measures
45- $ controls_count = DB ::table ('measures ' )
46- ->count ();
80+ private function getControlsCount ()
81+ {
82+ return DB ::table ('measures ' )->count ();
83+ }
4784
48- // count active controls
49- $ active_measures_count = DB :: table ( ' controls ' )
50- //->whereNull('realisation_date ')
51- ->whereIn ('status ' , [0 ,1 ])
85+ private function getActiveMeasuresCount ()
86+ {
87+ return DB :: table ( ' controls ' )
88+ ->whereIn ('status ' , [0 , 1 ])
5289 ->count ();
90+ }
5391
54- // count controls made
55- $ controls_made_count = DB :: table ( ' controls ' )
56- //->whereNotNull('realisation_date ')
92+ private function getControlsMadeCount ()
93+ {
94+ return DB :: table ( ' controls ' )
5795 ->where ('status ' , 2 )
5896 ->count ();
97+ }
5998
60- // count control never made
61- $ controls_never_made = DB ::table ('controls as c1 ' )
99+ private function getControlsNeverMade ()
100+ {
101+ return DB ::table ('controls as c1 ' )
62102 ->leftJoin ('controls as c2 ' , 'c2.next_id ' , '= ' , 'c1.id ' )
63103 ->whereNull ('c1.realisation_date ' )
64104 ->whereNull ('c2.id ' )
65105 ->count ();
106+ }
107+
108+ private function getPlanedControlsThisMonthCount ()
109+ {
110+ return DB ::table ('controls ' )
111+ ->whereNull ('realisation_date ' )
112+ ->whereBetween ('plan_date ' , [
113+ Carbon::now ()->startOfMonth (),
114+ Carbon::now ()->endOfMonth (),
115+ ])
116+ ->count ();
117+ }
118+
119+ private function getLateControlsCount ()
120+ {
121+ return DB ::table ('controls ' )
122+ ->whereNull ('realisation_date ' )
123+ ->where ('plan_date ' , '< ' , Carbon::today ())
124+ ->count ();
125+ }
126+
127+ private function getActionPlansCount ()
128+ {
129+ return DB ::table ('actions ' )
130+ ->where ('status ' , 0 )
131+ ->count ();
132+ }
66133
67- // Last controls made by measures
68- $ active_controls =
69- DB ::table ('controls as c1 ' )
134+ private function getActiveControls ()
135+ {
136+ return DB ::table ('controls as c1 ' )
70137 ->select (['c1.id ' , 'measures.id ' , 'domains.title ' , 'c1.realisation_date ' , 'c1.score ' ])
71138 ->join ('controls as c2 ' , 'c2.id ' , '= ' , 'c1.next_id ' )
72139 ->join ('control_measure ' , 'control_measure.control_id ' , '= ' , 'c1.id ' )
@@ -75,110 +142,68 @@ public function index(Request $request)
75142 ->whereNull ('c2.realisation_date ' )
76143 ->orderBy ('c1.id ' )
77144 ->get ();
145+ }
78146
79- // Get controls todo
80- $ controls_todo =
81- DB ::table ('controls as c1 ' )
82- ->select ([
83- 'c1.id ' ,
84- 'c1.name ' ,
85- 'c1.scope ' ,
86- 'c1.plan_date ' ,
87- 'c1.status ' ,
88- 'c2.id as prev_id ' ,
89- 'c2.realisation_date as prev_date ' ,
90- 'c2.score as score ' ,
91- ])
92- ->leftjoin ('controls as c2 ' , 'c1.id ' , '= ' , 'c2.next_id ' )
93- ->whereIn ('c1.status ' , [0 ,1 ])
94- ->where ('c1.plan_date ' , '< ' , Carbon::today ()->addDays (30 )->format ('Y-m-d ' ))
95- ->orderBy ('c1.plan_date ' )
96- ->get ();
97-
98- // Fetch measures for all controls in one query
99- $ controlMeasures = DB ::table ('control_measure ' )
147+ private function getControlsTodo ()
148+ {
149+ // Fetch the controls todo with necessary joins
150+ $ controlsTodo = DB ::table ('controls as c1 ' )
100151 ->select ([
101- 'control_id ' ,
102- 'measure_id ' ,
103- 'clause ' ,
152+ 'c1.id ' , 'c1.name ' , 'c1.scope ' , 'c1.plan_date ' , 'c1.status ' ,
153+ 'c2.id as prev_id ' , 'c2.realisation_date as prev_date ' , 'c2.score as score ' ,
104154 ])
105- ->leftjoin ('measures ' , 'measures.id ' , '= ' , 'measure_id ' )
106- ->whereIn ('control_id ' , $ controls_todo ->pluck ('id ' ))
107- ->orderBy ('clause ' )
155+ ->leftJoin ('controls as c2 ' , 'c1.id ' , '= ' , 'c2.next_id ' )
156+ ->whereIn ('c1.status ' , [0 , 1 ])
157+ ->where ('c1.plan_date ' , '< ' , Carbon::today ()->addDays (30 ))
158+ ->orderBy ('c1.plan_date ' )
108159 ->get ();
109160
110- // Group measures by control_id
111- $ measuresByControlId = $ controlMeasures ->groupBy ('control_id ' );
161+ // Fetch related control measures in a single query
162+ $ controlMeasures = DB ::table ('control_measure ' )
163+ ->select (['control_id ' , 'measure_id ' , 'clause ' ])
164+ ->leftJoin ('measures ' , 'measures.id ' , '= ' , 'measure_id ' )
165+ ->whereIn ('control_id ' , $ controlsTodo ->pluck ('id ' ))
166+ ->orderBy ('clause ' )
167+ ->get ()
168+ ->groupBy ('control_id ' );
112169
113- // map clauses
114- foreach ($ controls_todo as $ control ) {
115- $ control ->measures = $ measuresByControlId ->get ($ control ->id , collect ())->map (function ($ controlMeasure ) {
170+ // Map over controlsTodo to add measures
171+ $ controlsTodo -> map ( function ($ control ) use ( $ controlMeasures ) {
172+ $ control ->measures = $ controlMeasures ->get ($ control ->id , collect ())->map (function ($ controlMeasure ) {
116173 return [
117174 'id ' => $ controlMeasure ->measure_id ,
118175 'clause ' => $ controlMeasure ->clause ,
119176 ];
120177 });
121- }
122-
123- // planed controls this month
124- $ planed_controls_this_month_count = DB ::table ('controls ' )
125- ->where (
126- [
127- ['realisation_date ' ,'= ' ,null ],
128- ['plan_date ' ,'>= ' , (new Carbon ('first day of this month ' ))->toDateString ()],
129- ['plan_date ' ,'< ' , (new Carbon ('first day of next month ' ))->toDateString ()],
130- ]
131- )
132- ->count ();
133- $ request ->session ()->put ('planed_controls_this_month_count ' , $ planed_controls_this_month_count );
134-
135- // late controls
136- $ late_controls_count = DB ::table ('controls ' )
137- ->where (
138- [
139- ['realisation_date ' ,'= ' ,null ],
140- ['plan_date ' ,'< ' , Carbon::today ()->format ('Y-m-d ' )],
141- ]
142- )
143- ->count ();
144- $ request ->session ()->put ('late_controls_count ' , $ late_controls_count );
145-
146- // Count number of action plans
147- $ action_plans_count =
148- DB ::table ('actions ' )
149- ->where ('status ' , 0 )
150- ->count ();
178+ return $ control ;
179+ });
151180
152- $ request ->session ()->put ('action_plans_count ' , $ action_plans_count );
181+ return $ controlsTodo ;
182+ }
153183
154- // Get all controls
184+ private function getExpandedControls ()
185+ {
155186 $ controls = DB ::table ('controls ' )
156- ->select ([ 'id ' , 'score ' , 'realisation_date ' , 'plan_date ' ] )
187+ ->select ('id ' , 'score ' , 'realisation_date ' , 'plan_date ' , ' periodicity ' )
157188 ->get ();
158189
159- // return
160- return view ('welcome ' )
161- ->with ('active_domains_count ' , $ active_domains_count )
162- ->with ('active_controls ' , $ active_controls )
163- ->with ('controls_count ' , $ controls_count )
164- ->with ('active_measures_count ' , $ active_measures_count )
165- ->with ('controls_made_count ' , $ controls_made_count )
166- ->with ('controls_never_made ' , $ controls_never_made )
167-
168- ->with ('controls_todo ' , $ controls_todo )
169- ->with ('active_controls ' , $ active_controls )
170- ->with ('action_plans_count ' , $ action_plans_count )
171- ->with ('late_controls_count ' , $ late_controls_count )
172-
173- ->with ('controls ' , $ controls )
174- ;
190+ return $ controls ->flatMap (function ($ control ) {
191+ $ expanded = collect ([$ control ]);
192+
193+ if ($ control ->realisation_date === null && $ control ->periodicity > 0 && $ control ->periodicity <= 12 ) {
194+ for ($ i = 1 ; $ i <= 12 / $ control ->periodicity ; $ i ++) {
195+ $ repeatedControl = clone $ control ;
196+ $ repeatedControl ->id = null ;
197+ $ repeatedControl ->score = null ;
198+ $ repeatedControl ->observations = null ;
199+ $ repeatedControl ->realisation_date = null ;
200+ $ repeatedControl ->plan_date = Carbon::parse ($ control ->plan_date )->addMonthsNoOverflow ($ i * $ control ->periodicity );
201+ $ expanded ->push ($ repeatedControl );
202+ }
203+ }
204+
205+ return $ expanded ;
206+ });
175207 }
176208
177- public function test ()
178- {
179- $ domain = DB ::table ('domains ' )->first ();
180-
181- return view ('test ' )
182- ->with ('domain ' , $ domain );
183- }
184209}
0 commit comments