public function store()
 {
     if (Request::ajax()) {
         $data = array("agenda_id" => Input::get("agenda_id"), "patient" => Input::get("patient"), "reason" => Input::get("reason"), "fecha" => Input::get("fecha"), "start" => Input::get("start"), "end" => Input::get("end"));
         $rules = array("agenda_id" => 'required', "patient" => 'required|min:2|max:100', "reason" => 'required|min:2|max:200', "fecha" => 'required|min:1|max:100', "start" => 'required|min:1|max:100', "end" => 'required|min:1|max:100');
         $messages = array('required' => 'El campo :attribute es obligatorio.', 'min' => 'El campo :attribute no puede tener menos de :min carácteres.', 'email' => 'El campo :attribute debe ser un email válido.', 'max' => 'El campo :attribute no puede tener más de :max carácteres.', 'numeric' => 'El campo :attribute debe contener solo numeros', 'mimes' => 'El formato de la imagen logo debe ser jpg, git, png');
         $validation = Validator::make(Input::all(), $rules, $messages);
         //si la validación falla redirigimos al formulario de registro con los errores
         if ($validation->fails()) {
             return Response::json(array('success' => false, 'errors' => $validation->getMessageBag()->toArray()));
         } else {
             $agenda = Agenda::find(Input::get('agenda_id'));
             $user = User::where('email', Input::get('patient'))->orWhere('username', Input::get('patient'))->first();
             if ($user) {
                 $patient = Patient::where('user_id', $user->id)->first();
                 if (!$patient) {
                     $patient = new Patient();
                     $patient->user_id = $user->id;
                     $patient->save();
                 }
                 $docPatient = DoctorPatient::where('patient_id', $patient->id)->where('doctor_id', $agenda->doctor_id)->first();
                 if (!$docPatient) {
                     $docPatient = new DoctorPatient();
                     $docPatient->patient_id = $patient->id;
                     $docPatient->doctor_id = $agenda->doctor_id;
                     $docPatient->save();
                 }
                 $apo = new Appointment();
                 $apo->patient_id = $patient->id;
                 $apo->agenda_id = $agenda->id;
                 $apo->day = Input::get("fecha");
                 $apo->start_date = Input::get('start');
                 $apo->end_date = Input::get('end');
                 $apo->reason = Input::get('reason');
                 if ($agenda->appointment_state == 0) {
                     $apo->state = 'confirmed';
                 } else {
                     $apo->state = 'pending';
                 }
                 $apo->save();
                 if ($apo) {
                     if ($agenda->appointment_state == 0) {
                         $mgs = new MgsAppointment();
                         $mgs->appointment_id = $apo->id;
                         $mgs->text = "Su cita fue Confirmada";
                         $mgs->save();
                     }
                     return Response::json(array('success' => true));
                 }
             } else {
                 return Response::json(array('success' => false, 'errors' => 'El usuario no existe'));
             }
         }
     }
 }
 public function postaddPatient()
 {
     if (Request::ajax()) {
         $data = array("agenda_id" => Input::get("agenda_id"), "first_name" => Input::get("first_name"), "last_name" => Input::get("last_name"), "email" => Input::get("email"));
         $rules = array("agenda_id" => 'required', "first_name" => 'required|min:2|max:50', "last_name" => 'required|min:2|max:50', "email" => 'required|email|min:1|max:50');
         $messages = array('required' => 'El campo :attribute es obligatorio.', 'min' => 'El campo :attribute no puede tener menos de :min carácteres.', 'email' => 'El campo :attribute debe ser un email válido.', 'max' => 'El campo :attribute no puede tener más de :max carácteres.', 'numeric' => 'El campo :attribute debe contener solo numeros', 'mimes' => 'El formato de la imagen logo debe ser jpg, git, png');
         $validation = Validator::make(Input::all(), $rules, $messages);
         //si la validación falla redirigimos al formulario de registro con los errores
         if ($validation->fails()) {
             return Response::json(array('success' => false, 'errors' => $validation->getMessageBag()->toArray()));
         } else {
             $password = $this->generaPass();
             $agenda = Agenda::find(Input::get('agenda_id'));
             $user = new User();
             $user->first_name = Input::get("first_name");
             $user->last_name = Input::get("last_name");
             $user->email = Input::get("email");
             $user->password = $password;
             $user->username = Input::get("first_name") . Input::get("last_name");
             $user->activated = 1;
             $user->save();
             if ($user) {
                 $doctorUser = Sentry::getUserProvider()->findByLogin($user->email);
                 $doctorsyGroup = Sentry::getGroupProvider()->findByName('Patients');
                 $doctorUser->addGroup($doctorsyGroup);
                 $profile = new Profile();
                 $profile->user_id = $user->id;
                 $profile->save();
                 $patient = new Patient();
                 $patient->user_id = $user->id;
                 $patient->save();
                 $docPatient = new DoctorPatient();
                 $docPatient->patient_id = $patient->id;
                 $docPatient->doctor_id = $agenda->doctor_id;
                 $docPatient->save();
                 $subject = 'Registro por Clinica | smartdoctorappointments.com';
                 $name = $user->first_name;
                 $email = $user->email;
                 $data = array('name' => $user->first_name . " " . $user->last_name, 'password' => $password, 'email' => $user->email);
                 Mail::send('email.userpatiendadd', $data, function ($message) use($name, $email, $subject) {
                     $message->to($email, $name);
                     $message->subject($subject);
                 });
                 return Response::json(array('success' => true, 'email' => Input::get("email")));
             } else {
                 return Response::json(array('success' => false, 'errors' => 'Error al registrar este usuario.'));
             }
         }
     }
 }