Reputation: 971
I planned a training date on 10/09/2019
My problem is that when I planned a revision for example on 09/09/2019 to 11/09/2019. I can add one recording , I would like to make one blocking... Is it possible?
Here is one idea of the code for the Controller Training. I don't understand, how can I compare precisely dates between Revision and Training?
Controller Training
public function store(Request $request)
{
$request->validate([
'date_seance' => 'required',
'hour_start' => 'required',
'hour_end' => 'required',
'fk_motorbike' => 'required',
'fk_former' => 'required',
'fk_student' => 'required'
]);
$date_start = Carbon::parse($request->get('date_seance'))->format('Y-m-d');
//$date_start = $request->get('date_seance');
/*
$hour_start = $request->get('hour_start');
$hour_end = $request->get('hour_end'); */
$fk_motorbike = $request->get('fk_motorbike');
$fk_former = $request->get('fk_former');
$conflictTraining = Training::where('fk_motorbike', $fk_motorbike)
->whereDate('date_seance', "=" , $date_start) /*
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))*/
->where('fk_former', $request->get('fk_former'))
->first();
$conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
->whereDate('date_revision_start', "<=" , $date_start)
->whereDate('date_revision_end', ">=", $date_start)/*
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))*/
->first();
$conflictFormer = Training::where('fk_former', $fk_former)
->whereDate('date_seance', "=" , $date_start) /*
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))*/
->first();
$conflictMotorbike = Training::where('fk_motorbike', $fk_motorbike)
->whereDate('date_seance', "=" , $date_start) /*
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))*/
->first();
if(isset($conflictTraining)){
return redirect()->route('trainings.index')
->with('error', 'Duplicate ! ');
}
if(isset($conflictRevision)){
return redirect()->route('trainings.index')
->with('error', 'The motorbike is in revision! ');
}
if(isset($conflictRevision2)){
return redirect()->route('trainings.index')
->with('error', 'The motorbike is in revision! ');
}
if(isset($conflictFormer)){
return redirect()->route('trainings.index')
->with('error', 'Duplicate former ! ');
}
if(isset($conflictMotorbike)){
return redirect()->route('trainings.index')
->with('error', 'Duplicate motorbik ! ');
}
else{
Training::create($request->all());
return redirect()->route('trainings.index')
->with('success', 'Add');
}
Revision Controller
public function store(Request $request)
{
/*
$date_revision_start = $request->get('date_revision_start');
$date_revision_end = $request->get('date_revision_end');*/
$date_revision_start = Carbon::parse($request->get('date_revision_start'))->format('Y-m-d');
$date_revision_end = Carbon::parse($request->get('date_revision_end'))->format('Y-m-d');/*
$hour_start = $request->get('hour_start');
$hour_end = $request->get('hour_end');*/
$garage = $request->get('garage');
$fk_motorbike = $request->get('fk_motorbike');
$conflictTraining = Training::where('fk_motorbike', $request->get('fk_motorbike'))
->whereDate('date_seance', "=" , $date_revision_start)/*
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))*/
->first();
$conflictTraining2 = Training::where('fk_motorbike', $request->get('fk_motorbike'))
->whereDate('date_seance', "=" , $date_revision_end)/*
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))*/
->first();
$conflict = Revision::where('fk_motorbike', $request->get('fk_motorbike'))
->whereDate('date_revision_start', "<=" , $date_revision_start)
->whereDate('date_revision_end', ">=", $date_revision_start)
->first();
$conflict2 = Revision::where('fk_motorbike', $request->get('fk_motorbike'))->whereDate('date_revision_start', "<=" , $date_revision_end)->whereDate('date_revision_end', ">=", $date_revision_end)->first();
if(isset($conflictTraining)){
return redirect()->route('revisions.index')
->with('error', 'Le véhicule est en cours pour la date de début de la révision! ');
}
if(isset($conflictTraining2)){
return redirect()->route('revisions.index')
->with('error', 'Le véhicule est en cours pour la date de fin de la révision! ');
}
if(isset($conflict2) || isset($conflict)){
return redirect()->route('revisions.index')
->with('error', 'duplicate');
}
else{
Revision::create($request->all());
return redirect()->route('revisions.index')
->with('success', 'new data created successfully');
}
Thank you for your help and explanation
Upvotes: 0
Views: 65
Reputation: 8178
Couple of things. First, your $conflictRevision
and $conflictRevision2
queries are exactly the same. Suggest you remove the $conflictRevision2
as it doesn't seem to be used again in the method anyway.
As to blocking the new Training, your code on the Training Controller looks correct to block a conflict against a revision. This code should stop the training from being added (create a conflict object):
$conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
->whereDate('date_revision_start', "<=" , $date_start)
->whereDate('date_revision_end', ">=", $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->first();
A few things may be allowing the training to be recorded.
A potentially bigger issue, however, is that while your code to block seems correct, the logic of this architecture for the reverse (don't block if the time is outside the revision) seems like it needs attention. Because the revision may span several days, I not sure it makes sense to have the time on the revision unless it is ONLY down for revision during those times, and then available for the rest of the day. Not sure what the point of tracking the hours of revision is, if it is a multiple day outage.
As an example, if the bike is down 10/9 - 15/9 10:00-11:00, and I want a training on 12/9 at 14:00, the query above will NOT show a conflict and WILL allow the training. Because the bike shows it is only in revision for 1 hour every day that week. Is that what you want? If not, it may make more sense to remove the hours from the revision, since it is spanning multiple days.
Upvotes: 1