@@ -710,39 +710,49 @@ public function destroy(int $id)
710710
711711 public function history ()
712712 {
713- // Not API and auditee
714- abort_if (
715- Auth::User ()->role === 4 || Auth::User ()->role === 5 ,
716- Response::HTTP_FORBIDDEN ,
717- '403 Forbidden '
718- );
713+ // Abort if user role is 4 or 5
714+ abort_if (in_array (Auth::user ()->role , [4 , 5 ]), Response::HTTP_FORBIDDEN , '403 Forbidden ' );
719715
720- // Get all controls
721- $ controls = DB ::table ('controls ' )
722- ->select ('id ' , 'score ' , 'observations ' , 'realisation_date ' , 'plan_date ' )
723- ->get ();
724-
725- // Fetch measures for all controls in one query
726- $ controlMeasures = DB ::table ('control_measure ' )
727- ->select (['control_id ' , 'clause ' ])
728- ->leftjoin ('measures ' , 'measures.id ' , '= ' , 'measure_id ' )
729- ->whereIn ('control_id ' , $ controls ->pluck ('id ' ))
716+ // Fetch controls and their measures in a single query
717+ $ controlsData = DB ::table ('controls ' )
718+ ->select (
719+ 'controls.id ' ,
720+ 'controls.score ' ,
721+ 'controls.observations ' ,
722+ 'controls.realisation_date ' ,
723+ 'controls.plan_date ' ,
724+ 'controls.periodicity ' ,
725+ 'measures.clause ' )
726+ ->leftJoin ('control_measure ' , 'controls.id ' , '= ' , 'control_measure.control_id ' )
727+ ->join ('measures ' , 'control_measure.measure_id ' , '= ' , 'measures.id ' )
730728 ->get ();
731729
732730 // Group measures by control_id
733- $ measuresByControlId = $ controlMeasures ->groupBy ('control_id ' );
734-
735- // map clauses
731+ $ controls = $ controlsData ->groupBy ('id ' )->map (function ($ controlGroup ) {
732+ $ control = $ controlGroup ->first ();
733+ $ control ->measures = $ controlGroup ->pluck ('clause ' )->filter ()->values ();
734+ return $ control ;
735+ })->values ();
736+
737+ // Repeat controls based on periodicity
738+ $ expandedControls = collect ();
736739 foreach ($ controls as $ control ) {
737- $ control ->measures = $ measuresByControlId
738- ->get ($ control ->id , collect ())
739- ->map (function ($ controlMeasure ) {
740- return $ controlMeasure ->clause ;
741- });
740+ $ expandedControls ->push ($ control );
741+
742+ if (($ control ->realisation_date ==null )&&($ control ->periodicity >0 ))
743+ for ($ i = 1 ; $ i <= 12 / $ control ->periodicity ; $ i ++) {
744+ $ repeatedControl = clone $ control ;
745+ $ repeatedControl ->id = null ;
746+ $ repeatedControl ->score = null ;
747+ $ repeatedControl ->observations = null ;
748+ $ repeatedControl ->realisation_date = null ;
749+ $ repeatedControl ->plan_date = Carbon::parse ($ control ->plan_date )->addMonthsNoOverflow ($ i * $ control ->periodicity );
750+ $ expandedControls ->push ($ repeatedControl );
751+ }
742752 }
743-
744- // return
745- return view ( ' controls.history ' ) ->with ('controls ' , $ controls );
753+ // Return view with controls
754+ return view ( ' controls.history ' )
755+ ->with ('controls ' , $ expandedControls );
746756 }
747757
748758 /*
@@ -1265,7 +1275,7 @@ public function make(Request $request)
12651275 } else {
12661276 $ next_date =
12671277 $ control ->next_date === null
1268- ? \ Carbon \ Carbon::createFromFormat ('Y-m-d ' , $ control ->plan_date )
1278+ ? Carbon::createFromFormat ('Y-m-d ' , $ control ->plan_date )
12691279 ->addMonthsNoOverflow ($ control ->periodicity )
12701280 ->format ('Y-m-d ' )
12711281 : $ control ->next_date ->format ('Y-m-d ' );
0 commit comments