/** * * take the post request and process them * @return Json response */ public function inputs() { if (Request::get('task') == "makeAppointment") { $doctor_id = Session::get('requestedDoctor'); $timeVal = Session::get('timeval'); $start = Request::get('start'); $end = Request::get('end'); $check = false; if (is_null($timeVal)) { return response()->json(['message' => 'Data missmatched', 'code' => 'error']); } foreach ($timeVal as $key => $values) { foreach ($values as $value) { if ($value->start == trim($start) && $value->end == trim($end)) { $check = true; break; } //return response()->json(['message' => count($values), 'code' => 'error']); } } if (!is_null($doctor_id) && !is_null($start) && !is_null($end) && !is_null($timeVal) && $check == true) { $doctor = Doctor::where('id', $doctor_id)->first(); if (is_null($doctor)) { return response()->json(['message' => 'Data missmatched', 'code' => 'error']); } $user = Session::get('user'); if ($user->email == $doctor->email) { return response()->json(['message' => 'You cant place appointments to your self', 'code' => 'error']); } if ($doctor->available == 0) { return response()->json(['message' => 'This Doctor is Not available', 'code' => 'error']); } $timeSlot = Timeslots::where('doctor_id', '=', $doctor->id)->first(); if (!is_null($timeSlot)) { $period = explode(".", $timeSlot->period); if (count($period) != 2) { return response()->json(['message' => 'Data missmatched', 'code' => 'error']); } $periodMinutes = $period[1]; $periodHours = $period[0]; $periodMinutes = $periodMinutes + $periodHours * 60; //verify slot is free $startDate = Carbon::createFromFormat('Y-m-d H:i:s', $start); $endDate = Carbon::createFromFormat('Y-m-d H:i:s', $end); $scheduleFree = doctorSchedule::where('did', '=', $doctor_id)->where('schedule_start', '=', $startDate->toDateTimeString())->where('schedule_end', '=', $endDate->toDateTimeString())->where('cancelUser', '=', 0)->where('cancelDoctor', '=', 0)->first(); if (!is_null($scheduleFree)) { return response()->json(['message' => 'Slot is Already reserved please select another or refresh', 'code' => 'error']); } //if( $startDate->diffInMinutes($endDate,false)!=$periodMinutes ){ // return response()->json(['message' => 'Data missmatched', 'code' => 'error']); //} //verify start is above 1 day $dt = Carbon::now('Asia/Colombo'); $dt->hour = 0; $dt->minute = 0; $dt->second = 0; $daysLeft = $dt->diffInDays($startDate); if ($daysLeft < 2 || $daysLeft > 8) { return response()->json(['message' => 'Refresh the page', 'code' => 'error']); } //verify user dont have any schdules with the same doctor within a week // dont check for the cancel ones $dt->addDays(2); $dtEnd = $dt->copy()->addDays(7); $schedules = doctorSchedule::where('did', '=', $doctor_id)->where('uid', '=', Session::get('userid'))->where('schedule_start', '>', $dt->toDateTimeString())->where('schedule_end', '<', $dtEnd->toDateTimeString())->where('cancelUser', '=', 0)->where('cancelDoctor', '=', 0)->first(); if (!is_null($schedules)) { return response()->json(['message' => 'You already have a appointment in this week', 'code' => 'error']); } //save the new schedule and send a email to the user $newSchedule = new doctorSchedule(); $newSchedule->did = $doctor_id; $newSchedule->uid = Session::get('userid'); $newSchedule->schedule_start = $startDate->toDateTimeString(); $newSchedule->schedule_end = $endDate->toDateTimeString(); $newSchedule->code = rand(10000, 99999); $newSchedule->trys = 0; if ($newSchedule->save()) { $dName = $doctor->first_name . " " . $doctor->last_name; $user = Session::get('user'); Mail::send('mailtemplate/appointmentPlaced', ['name' => $user->name, 'dName' => $dName, 'Date' => $startDate->toDateString(), 'Time' => $startDate->format('h:i A'), 'code' => $newSchedule->code, 'doctor' => $doctor], function ($m) use($user) { $m->from('*****@*****.**', 'Native Physician'); $m->to($user->email, $user->name)->subject('Appointment Details'); }); Mail::raw("An Appointment is placed on {$newSchedule->schedule_start}", function ($m) use($doctor) { $m->from('*****@*****.**', 'Native Physician'); $m->to($doctor->email, "{$doctor->first_name} {$doctor->last_name}")->subject('New Appointment Placed'); }); return response()->json(['message' => 'Your appointment placed successfully', 'code' => 'success', 'task' => 'makeAppointment']); } else { return response()->json(['message' => 'Selected slot is already reserved', 'code' => 'error']); } } else { return response()->json(['message' => 'Data missmatched', 'code' => 'error']); } } else { return response()->json(['message' => 'Data missmatched', 'code' => 'error']); } } else { return response()->json(['message' => 'Invalid Option', 'code' => 'error']); } }