Ejemplo n.º 1
0
 public function store(Request $request)
 {
     $rules = ['period_id' => 'required|exists:periods,id', 'start' => 'required|date', 'end' => 'required|date'];
     $messages = ['start.required' => 'Es necesario definir la fecha de inicio.', 'end.required' => 'Es necesario definir la fecha de fin.'];
     $v = Validator::make($request->all(), $rules, $messages);
     if ($v->fails()) {
         return back()->withErrors($v)->withInput();
     }
     // Custom validations
     if ($request->get('start') > $request->get('end')) {
         $errors['range'] = 'Las fechas son inconsistentes.';
     }
     $period = Period::find($request->get('period_id'));
     // Exist an automatic cast from string to carbon
     // But the comparison for "2016-01-15" < $period->start
     // is evaluated as TRUE when they have the same date
     $start = Carbon::createFromFormat("Y-m-d", $request->get('start'));
     $end = Carbon::createFromFormat("Y-m-d", $request->get('end'));
     if ($start < $period->start) {
         $errors['start'] = 'La fecha de inicio debe ser posterior al inicio del periodo.';
     }
     if ($end > $period->end) {
         $errors['end'] = 'La fecha de fin debe ser anterior al fin del periodo.';
     }
     if (isset($errors)) {
         return back()->withErrors($errors)->withInput();
     }
     Unit::create($request->all());
     return back()->with('success', 'Unidad registrada exitosamente.');
 }
 public function destroy($id)
 {
     $period = Period::find($id);
     $units = $period->units;
     if ($units->count() > 0) {
         return back()->with('error', 'No es posible eliminar un periodo asociado a unidades.');
     }
     $period->delete();
     return back();
 }