I am working on a project management application for my client. The requirement was to create a task & assign to a given employee. I need to calculate start date if end date is given & viceversa. I hope that I have explained my code clearly with the comments.
The issue is that the response I get is incorrect only sometimes. I could not find the scenario in which it gives wrong date. Can you check my code & let me know if you can find the scenario in which it gives wrong date please so that I can fix.
I am given with restricted access to the server as this code is being executed through a despatcher & I do not have access to the log files.
Please help me solve the issue. Your help is much appreciated.
/**
*** PARAMETERS ***
* $givenDate - Carbon object
* $totalDays - no. of days required/assigned for the task
* $direction - calculate the date either forward or backward from the given date
*** VARIABLES ***
* $taskdays
-> Array of dates in which tasks already assigned to the particular employee
* $leaves
-> Array of dates in which tasks the particular employee has applied for leave & approved
* $holidays
-> Array of dates - planned holidays for a particular branch of a company
-> Since branches of the company are located in different countries, applicable holidays are also set based on the country of the branch location.
* $weekends
-> Array of days(eg., $days=array('Saturday', 'Sunday')) - weekends for a particular branch of a company.
-> Since branches of the company are located in different countries, weekends are followed based on the country of the branch location.
-> eg., Qatar(Friday & Saturday), India(Saturday & Sunday), Bangladesh(Sunday Only)
**/
private function calculateStartEndDates($givenDate, $totalDays=1, $direction='forward') {
$taskdays = $this->getTaskAssignedDays($employeeId);
$leaves = $this->getApprovedLeaveDays($employeeId);
$holidays = $this->getHolidays($companyBranchId);
$weekends = $this->getWeekends($companyBranchId);
while($totalDays > 0) {
if($direction == 'backward') {
$givenDate = $givenDate->subDays(1);
} else {
$givenDate = $givenDate->addDays(1);
}
$currentDate = $givenDate->format('Y-m-d');
if(!in_array($currentDate, $taskdays) && !in_array($currentDate, $leaves) && !in_array($currentDate, $holidays) && !in_array($givenDate->format('l'), $weekends)) {
$totalDays--;
}
}
return $givenDate;
}