'I need to find an open spot in the calendar (MYSQL) and allocate that time and date to a job/work order

I need to find an open date time in the future and allocate that time in the calendar for that employee based on his/her worktimes Thanks in advance

I have a calendar table, employee(recourse) table ,employee working times and jobs (with Duration)

CREATE TABLE `calendar` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
   `Subject` varchar(200) DEFAULT NULL,
   `DateField` date DEFAULT NULL,
   `EndDate` date DEFAULT NULL,
   `TimeField` time DEFAULT NULL,
   `EndTime` time DEFAULT NULL,
   `job_id` int(11) DEFAULT NULL,
   `recourse_employee_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1902 DEFAULT CHARSET=utf8;

CREATE TABLE `rc_resources_employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `categoryid` int(11) DEFAULT NULL,
  `login_user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

CREATE TABLE `jobs` (
  `Job_ID` int(11) NOT NULL AUTO_INCREMENT,
  `Job_Subject` varchar(150) DEFAULT NULL,
  `Job_Description` mediumtext,
  `Job_Start_Date` date DEFAULT NULL,
  `Job_End_Date` date DEFAULT NULL,
  `Job_Duration` float DEFAULT NULL,
  `Job_Start_Time` time DEFAULT NULL,
  `Job_End_Time` time DEFAULT NULL,
  `Job_employee` int(11) DEFAULT NULL,

  PRIMARY KEY (`Job_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1875 DEFAULT CHARSET=utf8;
CREATE TABLE `work_hours` (
  `wh_id` int(11) NOT NULL AUTO_INCREMENT,
  `recourse_id` int(11) unsigned NOT NULL,
  `wh_day_of_week` int(11) unsigned NOT NULL,
  `wh_start_time` time NOT NULL,
  `wh_end_time` time NOT NULL,
  PRIMARY KEY (`wh_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source