How to get end date from start date and leave days based on condition using jQuery datapicker

How to get end date from start date and leave days based on condition using jQuery datapicker

I am using JQuery-ui datepicker in Leave Management Project.

This is what I have:



Controller

  1.         public function findNationalHoliday(Request $request)
  2.         {
  3.            $nationalholidays               = HrNationalHoliday::select('holiday_date')->whereYear('created_at', '=', date('Y'))->get();
  4.          return response()->json([
  5.             'nationalholidays' => $nationalholidays,
  6.          ]);        
  7.        }

holiday_days comes like this in the console:

0:
holiday_date: "2020-08-25 00:00:00"
__proto__: Object
1:
holiday_date: "2020-08-26 00:00:00"
__proto__: Object
2: {holiday_date: "2020-09-25 00:00:00"}
3: {holiday_date: "2020-11-30 00:00:00"}

__proto__: Array(0)

Javascript

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. $(document).on('change', '#leave_type', function() {
  4. var air_id = $(this).val();
  5. var a = $(this).parent();
  6. var op = "";

  7. $.ajax({
  8. type: 'get',
  9. url: '{{ route('get.nationalholidays.all') }}',
  10. data: { 'id': air_id },
  11. dataType: 'json', //return data will be json
  12. success: function(data) {
  13. $('#nationalholidays').val(data.nationalholidays);
  14. console.log(data.nationalholidays);
  15. },
  16. error:function(){
  17. }
  18. });
  19. });
  20. });
  21. </script>
  22. <script type="text/javascript">
  23. $("#leave_days").on('keyup blur', function (e) {
  24. var periodval=parseInt($("#leave_days").val());
  25. var startDate = $('.start_date');
  26. var endDate = $('.end_date');
  27. var dte = startDate.datepicker("getDate");
  28. dte.setDate(dte.getDate()+periodval);
  29. endDate.datepicker("setDate", dte);
  30. });
  31. $( '.start_date' ).datepicker({
  32. dateFormat: 'dd-mm-yy',
  33. changeMonth: true,
  34. changeYear: true,
  35. showAnim: 'slideDown',
  36. duration: 'fast',
  37. minDate: +1,
  38. setDate: new Date(),
  39. beforeShowDay: $.datepicker.noWeekends,
  40. yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
  41. }).datepicker('setDate', '1');
  42. $( '.end_date' ).datepicker({
  43. dateFormat: 'dd-mm-yy',
  44. changeMonth: true,
  45. changeYear: true,
  46. showAnim: 'slideDown',
  47. duration: 'fast',
  48. minDate: +1,
  49. beforeShowDay: $.datepicker.noWeekends,
  50. yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
  51. enableOnReadonly: true,
  52. beforeShow: function(i) { if ($(i).attr('readonly')) { return false; } }
  53. });
  54. });
  55. </script>

Basically, I have a table for NationalHoliday which stores all the national_holidays.

What I want to achieve is that, the user selects the start_date, and as he enters leave_days onchange, it should add national_holidays (if any) automatically display end_date. Note that the end_date is disabled.

So far, when the users enters leave_days automatically displays end_date, but not removing NationalHolidays.

Initially the user selects start_date and end_date from the view, then everything is calculated from the controller.

But now, the requirement has changed: national_holidays are sent as json from controller to the view. user selects

start_date + leave_days + national_holidays + weekend = end_date

start_date, enters leave_days, then the end_date is automatically calculated from the view blade

What I want is that the user selects start_date (end_date is locked and should not be selected), and as he types leave_days, end_date should automatically be generated adding holidays and weekend. How do I consume the nationalholidays.

Thanks