Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. <div class="card">
  2. <div class="card-header bg-info">
  3. Set Appointment Details
  4. </div>
  5. <div class="card-body" style="overflow: scroll;height: 605px;">
  6. @if($bookingRequest->appointment)
  7. {!! Form::model($bookingRequest->appointment, ['url' => route('therapist.book.appointment', $bookingRequest)]) !!}
  8. @else
  9. {!! Form::open(['url' => route('therapist.book.appointment', $bookingRequest)]) !!}
  10. @endif
  11. <table class="table dyanmic" id="dynamic">
  12. <thead>
  13. <th>
  14. Session
  15. </th>
  16. </thead>
  17. <tbody>
  18. <tr>
  19. <td>
  20. <div class="form-row data">
  21. <div class="col-6">
  22. {!! Form::inputGroup('date', 'Starting', 'start_date[]') !!}
  23. </div>
  24. <div class="col-6">
  25. {!! Form::inputGroup('date', 'Until', 'end_date[]') !!}
  26. </div>
  27. </div>
  28. </td>
  29.  
  30. </tr>
  31. <tr>
  32. <td>
  33. <div class="form-row data">
  34. <div class="col-6">
  35. {!! Form::inputGroup('time', '&nbsp;', 'start_date_time[]') !!}
  36. </div>
  37. <div class="col-6">
  38. {!! Form::inputGroup('time', '&nbsp;', 'end_date_time[]') !!}
  39. </div>
  40. </div>
  41. </td>
  42. </tr>
  43. <tr>
  44. <td>
  45. {!! Form::inputGroup('text', 'Other Services Applied', 'other_services[]') !!}
  46. {!! Form::inputGroup('number', 'Fee', 'other_services_fee[]') !!}
  47. </td>
  48. </tr>
  49. </tbody>
  50. <tfoot>
  51. <td>
  52. <div class="form-row form-group data">
  53. <div class="col-8">
  54. <button class="btn btn-sm btn-danger remove-line" id="remove-line"><i class="fa fa-times"></i></button>
  55. <button type="button" class="btn btn-sm btn-outline-primary add-line" id="add-line">
  56. <i class="fa fa-plus"></i> Add new session</button>
  57. </div>
  58. </div>
  59. </td>
  60. </tfoot>
  61. </table>
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. <button type="submit" class="btn btn-success" name="submit" id="submit">Submit</button>
  69. {!! Form::close() !!}
  70. </div>
  71. </div>
  72. @endif
  73. </div>
  74.  
  75. <script type="text/javascript">
  76. $(document).ready(function(){
  77.  
  78. var postURL = "<?php echo url('/therapist-calendar')?>";
  79. var table = $(this).closest('table.dynamic');
  80. var i = 1;
  81.  
  82. $('#add-line').click(function (){
  83. i++;
  84.  
  85. $('#dynamic').append(
  86. '<tr id="row'+i+'" class="dynamic-added">'+
  87. '<th>Add another session</th>'+
  88. '</tr>'+
  89. ''+
  90. '<tr id="row'+i+'" class="dynamic-added">'+
  91. '<td><div class="form-row data"><div class="col-6">{!! Form::inputGroup("date", "Starting", "start_date[]") !!}</div>' +
  92. '<div class="col-6">{!! Form::inputGroup("date", "Until", "end_date[]") !!}</div></div></td>'+
  93. '</tr>' +
  94. ''+
  95. '<tr id="row'+i+'" class="dynamic-added">'+
  96. '<td><div class="form-row data"><div class="col-6">{!! Form::inputGroup("time", "Start Time", "start_date_time[]") !!}</div>' +
  97. '<div class="col-6">{!! Form::inputGroup("time", "End Time", "end_date_time[]") !!}</div></div></td>'+
  98. '</tr>' +
  99. ''+
  100. '<tr id="row'+i+'" class="dynamic-added">'+
  101. '<td>{!! Form::inputGroup("text", "Other Services Applied", "other_services[]") !!}' +
  102. '{!! Form::inputGroup("number", "Fee", "other_services_fee[]") !!}</td>'+
  103. '</tr>'
  104.  
  105. );
  106. });
  107.  
  108. $(document).on('click', '.remove-line', function (){
  109.  
  110. var button_id = $(this).attr("id");
  111.  
  112. $('#row'+button_id+'').remove();
  113. });
  114.  
  115. $.ajaxSetup({
  116. headers: {
  117. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  118. }
  119. });
  120.  
  121. $('submit').click(function(){
  122. $.ajax({
  123. URL: postURL,
  124. method: "POST",
  125. data: $('#add_name').serialize(),
  126. type: 'json',
  127. });
  128. });
  129. });
  130. </script>
  131.  
  132. public function saveAppointment(Request $request, BookingRequest $bookingRequest)
  133. {
  134. $validator = Validator::make($request->all(), [
  135. 'start_date' => 'required|date|after:yesterday',
  136. 'end_date' => 'required|date|after_or_equal:start_date',
  137. 'start_date_time' => 'required|date_format:H:i',
  138. 'end_date_time' => 'required|date_format:H:i',
  139. 'other_services' => 'sometimes|nullable|string',
  140. 'other_services_fee' => 'sometimes|nullable|numeric',
  141. ]);
  142.  
  143. $bookingRequest->load('appointment');
  144. Auth::user()->load([
  145. 'therapist.appointments[]'
  146. ]);
  147.  
  148. $validator->after(function ($v) use ($request, $bookingRequest) {
  149. if ($v->errors()) {
  150. $startDateTime[] = Carbon::parse("{$request->start_date} {$request->start_date_time}");
  151. $endDateTime[] = Carbon::parse("{$request->end_date} {$request->end_date_time}");
  152.  
  153. /** @NOTE: lte => Less than or equal */
  154. if ($endDateTime->lte($startDateTime)) {
  155. $v->errors()->add('end_date_time', 'This should be after start date and time');
  156.  
  157. return;
  158. }
  159.  
  160. $conflicts = data_get(Auth::user(), 'therapist.approvedAppointments')
  161. ->filter(function ($existingAppointment) use ($startDateTime, $bookingRequest) {
  162. // ignore the same appointments
  163. if(optional($bookingRequest->appointment)->id === $existingAppointment->id){
  164. return false;
  165. }
  166.  
  167. return $startDateTime->between($existingAppointment->start_timestamp, $existingAppointment->end_timestamp);
  168. });
  169.  
  170. if($conflicts->isNotEmpty()){
  171. $v->errors()->add('start_date', 'Conflict');
  172. $v->errors()->add('end_date', 'Conflict');
  173. $v->errors()->add('end_date_time', 'Conflict');
  174. $v->errors()->add('start_date_time', 'Conflict');
  175. }
  176. }
  177. });
  178.  
  179. $input = $validator->validate();
  180.  
  181. $appointment = array_merge(
  182. $input,
  183. $bookingRequest->only(['client_id', 'therapist_id']),
  184. ['name' => '-', 'address' => '-']
  185. );
  186.  
  187. DB::transaction(function () use ($bookingRequest, $appointment) {
  188. if ($bookingRequest->appointment()->exists()) {
  189. $bookingRequest->appointment()->update($appointment);
  190. } else {
  191. $bookingRequest->approve();
  192. $bookingRequest->appointment()->create($appointment);
  193. }
  194. });
  195.  
  196. return redirect()->back()->with('message', 'Booking successful');
  197. }
  198.  
  199. $request = $request->all();
  200. $input = array();
  201.  
  202. foreach($request['start_date'] as $key => $startDate){
  203. foreach($request['start_date_time'] as $key => $startDateTime){
  204.  
  205. $input[$key]['start_date'] = $startDate;
  206. $input[$key]['start_date_time'] = $startDateTime;
  207.  
  208. $dateTimeDuration = Carbon::parse("{$startDate} {$startDateTime}");
  209. }
  210. }
  211.  
  212. $validator = Validator::make($request->all(), [
  213. 'start_date' => 'required|array|date|after:yesterday', //OR
  214. "start_date.*" => => 'required|date|after:yesterday'
  215. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement