//LEGAL Holiday OT/RD SPL Holiday OT
                 //Exceed the first 8 hours
                 if (strtotime($employeeNightDiffClocking->total_hours_1) > $todayTotalWorkHours) {
                     if (!empty($getHolidayByDate[0]->holiday_type)) {
                         if ('Regular holiday' === $getHolidayByDate[0]->holiday_type) {
                             //echo 'Regular holiday';
                             $employeeNightDiffClocking->total_overtime_1 = getOvertimeHours($clockingIn, $clockingOut, $getWorkShift[0]->start_time, $getWorkShift[0]->end_time);
                             $employeeSummaryNightDiffClocking->rest_day_legal_holiday_overtime = getOvertimeHours($clockingIn, $clockingOut, $getWorkShift[0]->start_time, $getWorkShift[0]->end_time);
                         } elseif ('Special non-working day' === $getHolidayByDate[0]->holiday_type) {
                             //echo 'Special non-working day';
                             $employeeNightDiffClocking->total_overtime_1 = getOvertimeHours($clockingIn, $clockingOut, $getWorkShift[0]->start_time, $getWorkShift[0]->end_time);
                             $employeeSummaryNightDiffClocking->rest_day_special_holiday_overtime = getOvertimeHours($clockingIn, $clockingOut, $getWorkShift[0]->start_time, $getWorkShift[0]->end_time);
                         }
                     } else {
                         $employeeNightDiffClocking->total_overtime_1 = getOvertimeHours($clockingIn, $clockingOut, $getWorkShift[0]->start_time, $getWorkShift[0]->end_time);
                         $employeeSummaryNightDiffClocking->rest_day_overtime = getOvertimeHours($clockingIn, $clockingOut, $getWorkShift[0]->start_time, $getWorkShift[0]->end_time);
                     }
                 }
             }
         } else {
             //Clocking out early/undertime
             //Todo: add a reason form
         }
     } else {
         //Code for the employee with schedule assign
     }
     if ($employeeNightDiffClocking->save()) {
         $employeeSummaryNightDiffClocking->save();
         return Redirect::to('/redraw/timesheet');
     }
 } else {
 public function adminTimesheetSave()
 {
     //value, id, row_id, column
     $data = Input::all();
     //	return dd($data);
     if (Request::ajax()) {
         //General Settings
         $nightDiff['from'] = strtotime('22:00');
         $nightDiff['to'] = strtotime('06:00');
         $hasNightDiff = false;
         //$dayOfTheWeek = date('l');
         $breakTime = date('H:i:s', strtotime('01:00:00'));
         $getTimesheet = Timesheet::where('id', (int) trim($data["row_id"]))->first();
         //in-out 1
         if ((int) $data["column"] === 3) {
             //var_dump($data["value"]);
             if (!empty($data["value"])) {
                 $dataValueArr = explode('-', $data["value"]);
                 /*return dd(getTardinessTime($clockingIn, $shiftStart));
                 		exit;*/
                 if (!empty($dataValueArr)) {
                     //UPDARTED TIME IN AND TIME OUT
                     $clockingIn = trim($dataValueArr[0]);
                     $clockingOut = trim($dataValueArr[1]);
                     $timesheetId = $getTimesheet->id;
                     $employeeId = $getTimesheet->employee_id;
                     $clockingStatus = $getTimesheet->clocking_status;
                     $shiftStart = $getTimesheet->schedule_in;
                     $shiftEnd = $getTimesheet->schedule_out;
                     //$scheduleIn = $getTimesheet->schedule_in;
                     //$scheduleOut = $getTimesheet->schedule_out;
                     $dayDate = $getTimesheet->daydate;
                     $tardiness = $getTimesheet->tardiness_1;
                     //don't forget clocking status
                     //check schedule
                     //General Settings
                     //$nightDiff['from'] = strtotime('22:00:00');
                     //$nightDiff['to'] = strtotime('06:00:00');
                     $timeInDateTime = date('G', strtotime($clockingIn));
                     $timeOutDateTime = date('G', strtotime($clockingOut));
                     //$clockingIn =  date( 'Y-m-d', strtotime($dayDate) ).' '.date( 'H:i:s', strtotime($clockingIn) );
                     //$clockingOut =  date( 'Y-m-d', strtotime($dayDate) ).' '.date( 'H:i:s', strtotime($clockingOut) );
                     //e.g clockingIn result should be: 2015-05-15 15:30:13
                     //$hour >= 21 && $hour <= 4
                     //date('G', strtotime($nightDiff['from']))
                     //$clockingIn =  date( 'Y-m-d', strtotime($dayDate) ).' '.date( 'H:i:s', strtotime($clockingIn) );
                     //$clockingOut =  date( 'Y-m-d', strtotime($dayDate) ).' '.date( 'H:i:s', strtotime($clockingOut) );
                     //CHECK IF NIGHT DIFF TRUE
                     if (date('G', strtotime($shiftStart)) >= date('G', strtotime($nightDiff['from'])) && date('G', strtotime($shiftEnd)) <= date('G', strtotime($nightDiff['to']))) {
                         //echo 'DEBUG.IO';
                         if ($timeInDateTime < 24 && $timeInDateTime < date('G', strtotime($nightDiff['from']))) {
                             $clockingIn = date('Y-m-d', strtotime($dayDate)) . ' ' . date('H:i:s', strtotime($clockingIn));
                             //$clockingIn =  date( 'Y-m-d', strtotime($dayDate) ).' '.date( 'H:i:s', strtotime($clockingIn) );
                         } else {
                             $clockingIn = date('Y-m-d', strtotime($dayDate)) . ' ' . date('H:i:s', strtotime($clockingIn));
                         }
                         if ($timeOutDateTime < 24 && $timeOutDateTime < date('G', strtotime($nightDiff['to']))) {
                             $clockingOut = date('Y-m-d', strtotime($dayDate . '1 day')) . ' ' . date('H:i:s', strtotime($clockingOut));
                         } else {
                             $clockingOut = date('Y-m-d', strtotime($dayDate)) . ' ' . date('H:i:s', strtotime($clockingOut));
                         }
                     } else {
                         $clockingIn = date('Y-m-d', strtotime($dayDate)) . ' ' . date('H:i:s', strtotime($clockingIn));
                         $clockingOut = date('Y-m-d', strtotime($dayDate)) . ' ' . date('H:i:s', strtotime($clockingOut));
                     }
                     $dayOfTheWeek = date('l', strtotime($dayDate));
                     $shift = 1;
                     $schedule = new Schedule();
                     //$getSchedule = $schedule->getSchedule($employeeId, $employeeClocking->daydate);
                     $getSchedule = DB::table('employee_schedule')->where('employee_id', $employeeId)->where('schedule_date', trim($dayDate))->first();
                     $workShift = new Workshift();
                     //$getWorkShiftByDayOfTheWeek = $workShift->getWorkShiftByDayOfTheWeek($employeeId, date('l', strtotime($employeeClocking->daydate)), $overtime->shift);
                     $getWorkShiftByDayOfTheWeek = DB::table('work_shift')->where('employee_id', $employeeId)->where('name_of_day', date('l', strtotime($dayDate)))->where('shift', $shift)->first();
                     if (!empty($getSchedule)) {
                         //$scheduled['start_time'] = $getSchedule[0]->start_time;
                         //$scheduled['end_time'] = $getSchedule[0]->end_time;
                         $scheduled['rest_day'] = $getSchedule->rest_day;
                     } elseif (!empty($getWorkShiftByDayOfTheWeek)) {
                         // From 01:00:00 change to 2015-04-30 09:00:00
                         //$scheduled['start_time'] = date( 'Y-m-d', strtotime($employeeClocking[0]->time_in_1) ).' '.
                         // From 01:00:00 change to 2015-04-30 01:00:00
                         //$scheduled['end_time'] =  date( 'Y-m-d', strtotime($clockingDateTime) ).' '.
                         $scheduled['rest_day'] = $getWorkShiftByDayOfTheWeek->rest_day;
                     }
                     $holiday = DB::table('holiday')->where('date', trim($dayDate))->first();
                     //dd($holiday);
                     $overtime = DB::table('overtime')->where('employee_id', $employeeId)->where('timesheet_id', $timesheetId)->where('seq_no', 1)->where('shift', 1)->where('overtime_status', 1)->get();
                     //dd($overtime);
                     //CHECK IF HAS NIGHTDIFF
                     if (strtotime($clockingIn) >= $nightDiff['from'] || strtotime($clockingOut) <= $nightDiff['to']) {
                         $hasNightDiff = true;
                     }
                     if (strtotime(date('H:i', strtotime($clockingIn))) === strtotime(date('H:i', strtotime($shiftStart))) && strtotime(date('H:i', strtotime($clockingOut))) === strtotime(date('H:i', strtotime($shiftEnd)))) {
                         DB::table('employee_timesheet')->where('id', $timesheetId)->update(array('time_in_1' => trim($clockingIn), 'time_out_1' => trim($clockingOut), 'tardiness_1' => '', 'undertime_1' => '', 'total_overtime_1' => '', 'overtime_status_1' => 'NULL'));
                         //Todo: update overtime table
                         DB::table('overtime')->where('employee_id', $employeeId)->where('timesheet_id', $timesheetId)->where('seq_no', 1)->where('shift', 1)->update(array('overtime_status' => 'NULL'));
                     }
                     //TARDINESS/LATES
                     if (strtotime($clockingIn) > strtotime($shiftStart)) {
                         $tardinessTime = getTardinessTime($clockingIn, $shiftStart);
                         DB::table('employee_timesheet')->where('id', $timesheetId)->update(array('time_in_1' => trim($clockingIn), 'time_out_1' => trim($clockingOut), 'tardiness_1' => $tardinessTime, 'total_overtime_1' => '', 'overtime_status_1' => 'NULL'));
                         //Todo: update overtime table
                         DB::table('overtime')->where('employee_id', $employeeId)->where('timesheet_id', $timesheetId)->where('seq_no', 1)->where('shift', 1)->update(array('overtime_status' => 'NULL'));
                         DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('lates' => $tardinessTime));
                         $getUpdatedTimesheet = Timesheet::where('id', (int) trim($timesheetId))->first();
                     } else {
                         DB::table('employee_timesheet')->where('id', $timesheetId)->update(array('time_in_1' => trim($clockingIn), 'time_out_1' => trim($clockingOut), 'tardiness_1' => ''));
                         DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('lates' => ''));
                     }
                     //dd($getUpdatedTimesheet);
                     if (!empty($getUpdatedTimesheet)) {
                         //LATE/TARDINESS: TRUE
                         if (!empty($getUpdatedTimesheet->tardiness_1)) {
                             //echo "LATE/TARDINESS: TRUE \n";
                             //TODO: check employee setting if has_break is true and break time is set. - function getWorkHours
                             $workHours = getWorkHours($clockingIn, $clockingOut, $shiftEnd);
                             //TODO: Compute total hours with out overtime - getTotalHours
                             $totalHours = getTotalHours($clockingIn, $clockingOut, $shiftEnd);
                             DB::table('employee_timesheet')->where('id', $timesheetId)->update(array('time_in_1' => trim($clockingIn), 'time_out_1' => trim($clockingOut), 'work_hours_1' => $workHours, 'total_hours_1' => $totalHours));
                             //Todo: update overtime table
                             DB::table('overtime')->where('employee_id', $employeeId)->where('timesheet_id', $timesheetId)->where('seq_no', 1)->where('shift', 1)->update(array('overtime_status' => 'NULL'));
                         }
                     } else {
                         //echo "LATE/TARDINESS: FALSE \n";
                         //TODO: check employee setting if has_break is true and break time is set. - function getWorkHours
                         $workHours = getWorkHours($clockingIn, $clockingOut, $shiftEnd);
                         //TODO: Compute total hours with overtime - getTotalHours
                         $totalHours = getTotalHours($clockingIn, $clockingOut, $shiftEnd);
                         DB::table('employee_timesheet')->where('id', $timesheetId)->update(array('time_in_1' => trim($clockingIn), 'time_out_1' => trim($clockingOut), 'work_hours_1' => $workHours, 'total_hours_1' => $totalHours));
                     }
                     //UNDERTIME: TRUE
                     if (strtotime($clockingOut) < strtotime($shiftEnd)) {
                         //echo "UNDERTIME: TRUE \n";
                         $undertime = getUnderTimeHours($clockingIn, $clockingOut, $shiftStart, $shiftEnd);
                         DB::table('employee_timesheet')->where('id', $timesheetId)->update(array('time_in_1' => trim($clockingIn), 'time_out_1' => trim($clockingOut), 'undertime_1' => $undertime, 'total_overtime_1' => '', 'overtime_status_1' => 'NULL'));
                         //Todo: update overtime table
                         DB::table('overtime')->where('employee_id', $employeeId)->where('timesheet_id', $timesheetId)->where('seq_no', 1)->where('shift', 1)->update(array('overtime_status' => 'NULL'));
                         DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('undertime' => $undertime));
                     } else {
                         DB::table('employee_timesheet')->where('id', $timesheetId)->update(array('time_in_1' => trim($clockingIn), 'time_out_1' => trim($clockingOut), 'undertime_1' => ''));
                         DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('undertime' => ''));
                     }
                     //OVERTIME: TRUE
                     $isOvertime = false;
                     /*if ( date('H:i', strtotime($clockingIn)) <= date('H:i', strtotime($shiftStart)) &&
                     	 date('H:i', strtotime($clockingOut)) > date('H:i', strtotime($shiftEnd)) ) {*/
                     if (strtotime($clockingIn) <= strtotime($shiftStart) && strtotime($clockingOut) > strtotime($shiftEnd)) {
                         //echo "OVERTIME: TRUE \n";
                         $isOvertime = true;
                         $totalOvertime = getOvertimeHours($clockingOut, $shiftEnd);
                         DB::table('employee_timesheet')->where('id', $timesheetId)->update(array('total_overtime_1' => $totalOvertime, 'time_in_1' => trim($clockingIn), 'time_out_1' => trim($clockingOut), 'tardiness_1' => '', 'undertime_1' => '', 'overtime_status_1' => 'NULL'));
                         //Todo: update overtime table
                         DB::table('overtime')->where('employee_id', $employeeId)->where('timesheet_id', $timesheetId)->where('seq_no', 1)->where('shift', 1)->update(array('overtime_status' => 'NULL'));
                     }
                     //dd($holiday);
                     //REST DAY: FALSE
                     if ($scheduled['rest_day'] !== 1) {
                         //HOLIDAY: TRUE
                         if (!empty($holiday)) {
                             if ('Regular holiday' === $holiday->holiday_type) {
                                 //Regular holiday
                                 //echo "Regular holiday \n";
                                 $totalHours = getTotalHours($clockingIn, $clockingOut, $shiftEnd);
                                 if (!$isOvertime) {
                                     //ISOVERTIME: FALSE
                                     DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('legal_holiday_night_diff' => $totalHours));
                                 }
                             } elseif ('Special non-working day' === $holiday->holiday_type) {
                                 //Special non-working day
                                 //echo "Special non-working day \n";
                                 $totalHours = getTotalHours($clockingIn, $clockingOut, $shiftEnd);
                                 if (!$isOvertime) {
                                     //ISOVERTIME: FALSE
                                     DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('special_holiday_night_diff' => $totalHours));
                                 }
                             }
                             //HOLIDAY: FALSE
                         } else {
                             //Regular Day
                             //echo "HOLIDAY: FALSE \n";
                             //echo "Regular Day \n";
                             $totalHours = getTotalHours($clockingIn, $clockingOut, $shiftEnd);
                             if (!$isOvertime) {
                                 //ISOVERTIME: FALSE
                                 DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('regular_night_differential' => $totalHours));
                             }
                         }
                         //REST DAY: TRUE
                     } elseif ($scheduled['rest_day'] === 1) {
                         //HOLIDAY: TRUE
                         if (!empty($holiday)) {
                             if ('Regular holiday' === $holiday->holiday_type) {
                                 //Regular holiday
                                 //echo "Regular holiday \n";
                                 $totalHours = getTotalHours($clockingIn, $clockingOut, $shiftEnd);
                                 if (!$isOvertime) {
                                     //ISOVERTIME: FALSE
                                     DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('rest_day_legal_holiday_night_diff' => $totalHours));
                                 }
                             } elseif ('Special non-working day' === $holiday->holiday_type) {
                                 //Special non-working day
                                 //echo "Special non-working day \n";
                                 $totalHours = getTotalHours($clockingIn, $clockingOut, $shiftEnd);
                                 if (!$isOvertime) {
                                     //ISOVERTIME: FALSE
                                     DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('rest_day_special_holiday_night_diff' => $totalHours));
                                 }
                             }
                             //HOLIDAY: FALSE
                         } else {
                             //Regular Day
                             //echo "HOLIDAY: FALSE \n";
                             //echo "Regular Day \n";
                             $totalHours = getTotalHours($clockingIn, $clockingOut, $shiftEnd);
                             if (!$isOvertime) {
                                 //ISOVERTIME: FALSE
                                 DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update(array('rest_day_night_differential' => $totalHours));
                             }
                         }
                     }
                     //OVERTIME TRUE
                     if (!empty($overtime)) {
                         //REST DAY: FALSE
                         if ($scheduled['rest_day'] !== 1) {
                             //HOLIDAY: TRUE
                             if (!empty($holiday)) {
                                 if ('Regular holiday' === $holiday->holiday_type) {
                                     //Regular holiday
                                     if (!$hasNightDiff) {
                                         $update = array('legal_holiday_overtime' => $totalOvertime);
                                     } elseif ($hasNightDiff) {
                                         $update = array('legal_holiday_overtime_night_diff' => $totalOvertime);
                                     }
                                     DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update($update);
                                 } elseif ('Special non-working day' === $holiday->holiday_type) {
                                     //Special non-working day
                                     if (!$hasNightDiff) {
                                         $update = array('legal_holiday_overtime' => $totalOvertime);
                                     } elseif ($hasNightDiff) {
                                         $update = array('legal_holiday_overtime_night_diff' => $totalOvertime);
                                     }
                                     DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update($update);
                                 }
                                 //HOLIDAY: FALSE
                             } else {
                                 //Regular Day
                                 //echo "HOLIDAY: FALSE \n";
                                 if (!$hasNightDiff) {
                                     $update = array('regular_overtime' => $totalOvertime);
                                 } elseif ($hasNightDiff) {
                                     $update = array('regular_overtime_night_diff' => $totalOvertime);
                                 }
                                 DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update($update);
                             }
                             //REST DAY: TRUE
                         } elseif ($scheduled['rest_day'] === 1) {
                             //HOLIDAY: TRUE
                             if (!empty($holiday)) {
                                 if ('Regular holiday' === $holiday->holiday_type) {
                                     //Regular holiday
                                     if (!$hasNightDiff) {
                                         $update = array('rest_day_legal_holiday_overtime' => $totalOvertime);
                                     } elseif ($hasNightDiff) {
                                         $update = array('rest_day_legal_holiday_overtime_night_diff' => $totalOvertime);
                                     }
                                     DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update($update);
                                 } elseif ('Special non-working day' === $holiday->holiday_type) {
                                     //Special non-working day
                                     if (!$hasNightDiff) {
                                         $update = array('rest_day_legal_holiday_overtime' => $totalOvertime);
                                     } elseif ($hasNightDiff) {
                                         $update = array('rest_day_legal_holiday_overtime_night_diff' => $totalOvertime);
                                     }
                                     DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update($update);
                                 }
                                 //HOLIDAY: FALSE
                             } else {
                                 //Regular Day
                                 //echo "HOLIDAY: FALSE \n";
                                 if (!$hasNightDiff) {
                                     $update = array('rest_day_overtime' => $totalOvertime);
                                 } elseif ($hasNightDiff) {
                                     $update = array('rest_day_overtime_night_diff' => $totalOvertime);
                                 }
                                 DB::table('employee_summary')->where('employee_id', $employeeId)->where('daydate', $dayDate)->update($update);
                             }
                         }
                     } else {
                     }
                 } else {
                     return "Not Allowed";
                 }
             } else {
                 return "Not Allowed";
             }
         }
         //in-out 2
         if ((int) $data["column"] === 4) {
             if (!empty($data["value"])) {
                 $dataValueArr = explode('-', $data["value"]);
                 return DB::table('employee_timesheet')->where('id', (int) $data["row_id"])->update(array('time_in_2' => trim($dataValueArr[0]), 'time_out_2' => trim($dataValueArr[1])));
             }
         }
         //in-out 3
         if ((int) $data["column"] === 5) {
             if (!empty($data["value"])) {
                 $dataValueArr = explode('-', $data["value"]);
                 return DB::table('employee_timesheet')->where('id', (int) $data["row_id"])->update(array('time_in_3' => trim($dataValueArr[0]), 'time_out_3' => trim($dataValueArr[1])));
             }
         }
         //return Redirect::to('/redraw/timesheet');
     }
 }
function getTotalHoursYesterday($clockingIn, $clockingOut, $shiftEnd)
{
    $yesterDayDate = date("Y-m-d", strtotime('yesterday'));
    $schedule = new Schedule();
    $getSchedule = $schedule->getSchedule($employeeId, $yesterDayDate);
    //$getSchedule = $schedule->getSchedule($employeeId, date('Y-m-d'));
    $workShift = new Workshift();
    $getWorkShiftByEmployeeId = $workShift->getWorkShiftByEmployeeId($employeeId);
    $setting = new Setting();
    $employeeSetting = $setting->getEmployeeSettingByEmployeeId($employeeId);
    if (!empty($getSchedule)) {
        $schedule['start_time'] = $getSchedule[0]->start_time;
        $schedule['end_time'] = $getSchedule[0]->end_time;
        $schedule['hours_per_day'] = $getSchedule[0]->hours_per_day;
        $halfOfhoursPerDay = $schedule['hours_per_day'] / 2;
        // e.g 8.00 without break
    } elseif (!empty($getWorkShiftByEmployeeId)) {
        /*$workShift['start_time'] = $getWorkShiftByEmployeeId[0]->start_time;
        		$workShift['end_time'] = $getWorkShiftByEmployeeId[0]->end_time;		
        		$workShift['hours_per_day'] = $getWorkShiftByEmployeeId[0]->hours_per_day;*/
        $workShift['start_time'] = date('Y-m-d', strtotime($clockingIn)) . ' ' . $getWorkShiftByEmployeeId[0]->start_time;
        $workShift['end_time'] = date('Y-m-d', strtotime('-1 day')) . ' ' . $getWorkShiftByEmployeeId[0]->end_time;
        //$workShift['rest_day'] = explode(', ', $getWorkShiftByEmployeeId[0]->rest_day);
        $workShift['hours_per_day'] = $getWorkShiftByEmployeeId[0]->hours_per_day;
        $halfOfhoursPerDay = $workShift['hours_per_day'] / 2;
        // e.g 8.00 without break
    }
    if (!empty($employeeSetting->hours_per_day)) {
        $halfOfhoursPerDay = $employeeSetting->hours_per_day / 2;
        // e.g 8.00 without break
        $setting['break_time'] = $employeeSetting->break_time;
    }
    $interval = getDateTimeDiffInterval($clockingIn, $clockingOut);
    $days = $interval->format('%a');
    $days = (int) $days;
    if ($days !== 0) {
        $hhToDays = $days * 24;
        $hh = (int) $hhToDays;
    } else {
        $hh = (int) $interval->format('%h');
    }
    $mm = (int) $interval->format('%i');
    $ss = (int) $interval->format('%s');
    $totalHours = getTimeToDecimalHours($hh, $mm, $ss);
    //number_format($hours, 2);
    $overtimeHours = getOvertimeHours($clockingOut, $shiftEnd);
    if ($employeeSetting->has_break === 1) {
        list($breakTimeHh, $breakTimeMm, $breakTimeSs) = explode(':', $setting['break_time']);
        $breakTime = getTimeToDecimalHours($breakTimeHh, $breakTimeMm, $breakTimeSs);
    }
    //$clockingOut = date('Y-m-d', strtotime($shiftEnd)).' '.$clockingOut;
    if (strtotime(date('H:i', strtotime($clockingOut))) > strtotime(date('H:i', strtotime($shiftEnd)))) {
        if ($employeeSetting->has_break === 1) {
            if ($totalHours > $halfOfhoursPerDay) {
                return number_format($totalHours, 2) + number_format($overtimeHours, 2) - $breakTime;
            } else {
                return number_format($totalHours, 2) + number_format($overtimeHours, 2);
            }
        } else {
            return number_format($totalHours, 2) + number_format($overtimeHours, 2);
        }
    } else {
        if ($employeeSetting->has_break === 1) {
            if ($totalHours > $halfOfhoursPerDay) {
                return number_format($totalHours, 2) - $breakTime;
            } else {
                return number_format($totalHours, 2);
            }
        } else {
            return number_format($totalHours, 2);
        }
    }
}
 public function updateTimesheetTimeOut3($data = '', $column = NULL)
 {
     //Settings
     $nightDiff['from'] = strtotime('22:00:00');
     $nightDiff['to'] = strtotime('06:00:00');
     $breakTime = date('H:i:s', strtotime('01:00:00'));
     $shift = 2;
     $hasNightDiff = false;
     $getTimesheet = Timesheet::where('id', (int) trim($data["row_id"]))->first();
     $clockingIn = $getTimesheet->time_in_3;
     $clockingOut = $getTimesheet->time_out_3;
     $timeInHour = date('G', strtotime($clockingIn));
     //24-hour format of an hour without leading zeros
     $timeOutHour = date('G', strtotime($clockingOut));
     //24-hour format of an hour without leading zeros
     $timesheetId = $getTimesheet->id;
     $employeeId = $getTimesheet->employee_id;
     $clockingStatus = $getTimesheet->clocking_status;
     //$shiftStart = $getTimesheet->schedule_in;
     //$shiftEnd = $getTimesheet->schedule_out;
     $dayDate = $getTimesheet->daydate;
     $tardiness = $getTimesheet->tardiness_3;
     $dayOfTheWeek = date('l', strtotime($dayDate));
     $getSummary = Summary::where('employee_id', $employeeId)->where('daydate', trim($dayDate))->first();
     $hourNeedle = date('G', strtotime(trim($data['value'])));
     $hourHaystack = array(0, 1, 2, 3, 4, 5, 6);
     if (in_array($hourNeedle, $hourHaystack)) {
         $nightdiff = true;
     } else {
         $nightdiff = false;
     }
     $dayDateModify = new DateTime($dayDate);
     //Todo Add a condition here
     if (!empty($data['value']) && !empty($getTimesheet->time_in_3)) {
         if (!$nightdiff) {
             $clockingDateTime = date('Y-m-d', strtotime($dayDate)) . ' ' . date('H:i:s', strtotime($data['value']));
         } else {
             //$dayDateModify = new DateTime($dayDate);
             $dayDateModify->modify('+1 day');
             //$dayDateModify->format('Y-m-d');
             $clockingDateTime = date('Y-m-d', strtotime($dayDateModify->format('Y-m-d'))) . ' ' . date('H:i:s', strtotime($data['value']));
         }
         $getTimesheet->time_out_3 = $clockingDateTime;
         $getTimesheet->clocking_status = 'clock_out_3';
     } elseif (!empty($data['value']) && empty($getTimesheet->time_in_3)) {
         if (!$nightdiff) {
             $clockingDateTime = date('Y-m-d', strtotime($dayDate)) . ' ' . date('H:i:s', strtotime($data['value']));
         } else {
             //$dayDateModify = new DateTime($dayDate);
             $dayDateModify->modify('+1 day');
             //$dayDateModify->format('Y-m-d');
             $clockingDateTime = date('Y-m-d', strtotime($dayDateModify->format('Y-m-d'))) . ' ' . date('H:i:s', strtotime($data['value']));
         }
         $getTimesheet->time_out_3 = $clockingDateTime;
         $getTimesheet->clocking_status = 'clock_out_3';
         //close
     } elseif (empty($data['value']) && !empty($getTimesheet->time_in_3)) {
         $clockingDateTime = '';
         $getTimesheet->time_out_3 = $clockingDateTime;
         $getTimesheet->clocking_status = 'clock_in_3';
     } elseif (empty($data['value']) && empty($getTimesheet->time_in_3)) {
         $clockingDateTime = date('Y-m-d', strtotime($dayDate)) . ' ' . date('H:i:s', strtotime($data['value']));
         if (!$nightdiff) {
             $clockingDateTime = date('Y-m-d', strtotime($dayDate)) . ' ' . date('H:i:s', strtotime($data['value']));
         } else {
             //$dayDateModify = new DateTime($dayDate);
             $dayDateModify->modify('+1 day');
             //$dayDateModify->format('Y-m-d');
             $clockingDateTime = date('Y-m-d', strtotime($dayDateModify->format('Y-m-d'))) . ' ' . date('H:i:s', strtotime($data['value']));
         }
         $getTimesheet->time_out_3 = '';
         $getTimesheet->clocking_status = 'close';
     }
     $schedule = new Schedule();
     $getSchedule = DB::table('employee_schedule')->where('employee_id', $employeeId)->where('schedule_date', trim($dayDate))->first();
     $workShift = new Workshift();
     $getWorkShiftByDayOfTheWeek = DB::table('work_shift')->where('employee_id', $employeeId)->where('name_of_day', date('l', strtotime($dayDate)))->where('shift', $shift)->first();
     $holiday = new Holiday();
     $getHolidayByDate = DB::table('holiday')->where('date', trim($dayDate))->first();
     if (!empty($getSchedule)) {
         $scheduled['start_time'] = $getSchedule->start_time;
         $scheduled['end_time'] = $getSchedule->end_time;
         $scheduled['rest_day'] = $getSchedule->rest_day;
         $startTime = date('H:i:s', strtotime($scheduled['start_time']));
     } elseif (!empty($getWorkShiftByDayOfTheWeek)) {
         //$scheduled['start_time'] = $getWorkShiftByDayOfTheWeek->start_time;
         //$scheduled['end_time'] = $getWorkShiftByDayOfTheWeek->end_time;
         // From 01:00:00 change to 2015-04-30 09:00:00
         $scheduled['start_time'] = date('Y-m-d', strtotime($getTimesheet->time_out_3)) . ' ' . $getWorkShiftByDayOfTheWeek->start_time;
         // From 01:00:00 change to 2015-04-30 01:00:00
         $scheduled['end_time'] = date('Y-m-d', strtotime($clockingDateTime)) . ' ' . $getWorkShiftByDayOfTheWeek->end_time;
         $scheduled['rest_day'] = $getWorkShiftByDayOfTheWeek->rest_day;
         $startTime = $scheduled['start_time'];
     }
     //return dd($clockingDateTime);
     //SCHEDULED : TRUE
     if ((!empty($scheduled['start_time']) || $scheduled['start_time'] !== '00:00:00' || $scheduled['start_time'] !== '') && (!empty($scheduled['end_time']) || $scheduled['end_time'] !== '00:00:00' || $scheduled['end_time'] !== '')) {
         //REST DAY: FALSE
         if ($scheduled['rest_day'] !== 1) {
             $datetime1 = $getTimesheet->time_in_3;
             $datetime2 = $clockingDateTime;
             //$interval = getDateTimeDiffInterval($getTimesheet->time_in_3, $getTimesheet->time_out_3) { //Used
             $datetime1 = new DateTime($datetime1);
             $datetime2 = new DateTime($datetime2);
             $interval = $datetime1->diff($datetime2);
             $hh = $interval->format('%H');
             $mm = $interval->format('%I');
             $ss = $interval->format('%S');
             $overtime = getTimeToDecimalHours($hh, $mm, $ss);
             //number_format($hours, 2);*/
             //OVERTIME: TRUE
             $isOvertime = true;
             $getTimesheet->total_overtime_3 = $overtime;
             $getTimesheet->total_hours_3 = $overtime;
             /*
             //echo "LATE/TARDINESS: TRUE \n";
             //TODO: check employee setting if has_break is true and break time is set. - function getWorkHours			
             
             $workHours = $getTimesheet->total_overtime_3;											
             
             if ( $workHours >= 8 || $workHours >= 8.00 || $workHours >= 8.0 ) {
             
             	$getTimesheet->work_hours_3 = 8.00;					
             
             } else {
             
             	$getTimesheet->work_hours_3 = $workHours;
             
             }
             */
             //TODO: Compute total hours with out overtime - getTotalHours
             $getTimesheet->total_hours_3 = $getTimesheet->total_overtime_3;
             //GET NIGHTDIFF
             $timeInArr = array();
             $timeOutArr = array();
             $timeInArr = explode(' ', $getTimesheet->time_in_3);
             $timeOutArr = explode(' ', $clockingDateTime);
             $nightDiff['from'] = strtotime(date('Y-m-d H:i', strtotime($timeInArr[0] . ' ' . '22:00:00')));
             $nightDiff['to'] = strtotime(date('Y-m-d H:i', strtotime($timeOutArr[0] . ' ' . '06:00:00')));
             $timesheet['timeIn'] = strtotime(date('Y-m-d H:i', strtotime($getTimesheet->time_in_3)));
             $timesheet['timeOut'] = strtotime(date('Y-m-d H:i', strtotime($clockingDateTime)));
             if ($timesheet['timeIn'] >= $nightDiff['from'] && $timesheet['timeIn'] <= $nightDiff['to']) {
                 if ($timesheet['timeOut'] >= $nightDiff['to']) {
                     // SET IT TO 8.00
                     //$getTimesheet->night_differential_3 = 8.00;
                     $getTimesheet->night_differential_3 = ($nightDiff['to'] - $timesheet['timeIn']) / 3600;
                 } else {
                     //$getTimesheet->night_differential_3 = $overtime;
                     $getTimesheet->night_differential_3 = ($timesheet['timeOut'] - $timesheet['timeIn']) / 3600;
                 }
             } elseif ($timesheet['timeOut'] >= $nightDiff['from'] && $timesheet['timeOut'] <= $nightDiff['to']) {
                 if ($timesheet['timeIn'] <= $nightDiff['from']) {
                     //$getTimesheet->night_differential_3 = 8.00;
                     $getTimesheet->night_differential_3 = ($timesheet['timeOut'] - $nightDiff['from']) / 3600;
                 } else {
                     //$getTimesheet->night_differential_3 = $overtime;
                     $getTimesheet->night_differential_3 = ($timesheet['timeOut'] - $timesheet['timeIn']) / 3600;
                 }
             }
             /*else {
             
             		        if($timesheet['timeIn'] < $nightDiff['from'] && $timesheet['timeOut'] > $nightDiff['to']) {
             		            
             					//$getTimesheet->night_differential_3 = 8.00;
             					$getTimesheet->night_differential_3 = ($nightDiff['from'] - $nightDiff['to']) / 3600;
             
             		        }
             	        
             	    	}*/
             //HOLIDAY: TRUE
             if (hasHoliday($dayDate)) {
                 ////echo "HOLIDAY: TRUE \n";
                 if ('Regular holiday' === $getHolidayByDate->holiday_type) {
                     //Regular holiday
                     //echo "Regular holiday \n";
                     if (!$isOvertime) {
                         //ISOVERTIME: FALSE
                         $getSummary->legal_holiday = getTotalHours($getTimesheet->time_in_3, $clockingDateTime, $scheduled['end_time']);
                         $getSummary->legal_holiday_overtime = '';
                     } else {
                         //ISOVERTIME: TRUE
                         $getSummary->legal_holiday_overtime = getOvertimeHours($clockingDateTime, $scheduled['end_time']);
                         $getSummary->legal_holiday = '';
                     }
                 } elseif ('Special non-working day' === $getHolidayByDate->holiday_type) {
                     //Special non-working day
                     //echo "Special non-working day \n";
                     if (!$isOvertime) {
                         //ISOVERTIME: FALSE
                         $getSummary->special_holiday = getTotalHours($getTimesheet->time_in_3, $clockingDateTime, $scheduled['end_time']);
                         $getSummary->special_holiday_overtime = '';
                     } else {
                         //ISOVERTIME: TRUE
                         $getSummary->special_holiday_overtime = getOvertimeHours($clockingDateTime, $scheduled['end_time']);
                         $getSummary->special_holiday = '';
                     }
                 }
                 //HOLIDAY: FALSE
             } else {
                 //Regular Day
                 //echo "HOLIDAY: FALSE \n";
                 //echo "Regular Day \n";
                 if (!$isOvertime) {
                     //ISOVERTIME: FALSE
                     $getSummary->regular = getTotalHours($getTimesheet->time_in_3, $clockingDateTime, $scheduled['end_time']);
                     $getSummary->regular_overtime = '';
                     $getSummary->legal_holiday = '';
                     $getSummary->special_holiday = '';
                 } else {
                     //ISOVERTIME: TRUE
                     $getSummary->regular_overtime = getOvertimeHours($clockingDateTime, $scheduled['end_time']);
                     $getSummary->regular = '';
                     $getSummary->legal_holiday_overtime = '';
                     $getSummary->special_holiday_overtime = '';
                 }
             }
             //REST DAY: TRUE
         } elseif ($scheduled['rest_day'] === 1) {
             $datetime1 = $getTimesheet->time_in_3;
             $datetime2 = $clockingDateTime;
             //$interval = getDateTimeDiffInterval($getTimesheet->time_in_3, $getTimesheet->time_out_3) { //Used
             $datetime1 = new DateTime($datetime1);
             $datetime2 = new DateTime($datetime2);
             $interval = $datetime1->diff($datetime2);
             $hh = $interval->format('%H');
             $mm = $interval->format('%I');
             $ss = $interval->format('%S');
             $overtime = getTimeToDecimalHours($hh, $mm, $ss);
             //number_format($hours, 2);*/
             //OVERTIME: TRUE
             $isOvertime = true;
             $getTimesheet->total_overtime_3 = $overtime;
             $getTimesheet->total_hours_3 = $overtime;
             /*
             //echo "LATE/TARDINESS: TRUE \n";
             //TODO: check employee setting if has_break is true and break time is set. - function getWorkHours			
             
             $workHours = $getTimesheet->total_overtime_3;											
             
             if ( $workHours >= 8 || $workHours >= 8.00 || $workHours >= 8.0 ) {
             
             	$getTimesheet->work_hours_3 = 8.00;					
             
             } else {
             
             	$getTimesheet->work_hours_3 = $workHours;					
             
             }
             */
             //TODO: Compute total hours with out overtime - getTotalHours
             $getTimesheet->total_hours_3 = $getTimesheet->total_overtime_3;
             //GET NIGHTDIFF
             $timeInArr = array();
             $timeOutArr = array();
             $timeInArr = explode(' ', $getTimesheet->time_in_3);
             $timeOutArr = explode(' ', $clockingDateTime);
             $nightDiff['from'] = strtotime(date('Y-m-d H:i', strtotime($timeInArr[0] . ' ' . '22:00:00')));
             $nightDiff['to'] = strtotime(date('Y-m-d H:i', strtotime($timeOutArr[0] . ' ' . '06:00:00')));
             $timesheet['timeIn'] = strtotime(date('Y-m-d H:i', strtotime($getTimesheet->time_in_3)));
             $timesheet['timeOut'] = strtotime(date('Y-m-d H:i', strtotime($clockingDateTime)));
             if ($timesheet['timeIn'] >= $nightDiff['from'] && $timesheet['timeIn'] <= $nightDiff['to']) {
                 if ($timesheet['timeOut'] >= $nightDiff['to']) {
                     // SET IT TO 8.00
                     $getTimesheet->night_differential_3 = 8.0;
                 } else {
                     $getTimesheet->night_differential_3 = $overtime;
                 }
             } elseif ($timesheet['timeOut'] >= $nightDiff['from'] && $timesheet['timeOut'] <= $nightDiff['to']) {
                 if ($timesheet['timeIn'] <= $nightDiff['from']) {
                     $getTimesheet->night_differential_3 = 8.0;
                 } else {
                     $getTimesheet->night_differential_3 = $overtime;
                 }
             }
             /*else {
             
             		        if($timesheet['timeIn'] < $nightDiff['from'] && $timesheet['timeOut'] > $nightDiff['to']) {
             		            
             					$getTimesheet->night_differential_3 = 8.00;
             
             		        }
             	        
             	    	}*/
             //HOLIDAY: TRUE
             if (hasHoliday($dayDate)) {
                 ////echo "HOLIDAY: TRUE \n";
                 if ('Regular holiday' === $getHolidayByDate->holiday_type) {
                     //Regular holiday
                     //echo "Regular holiday \n";
                     if (!$isOvertime) {
                         //ISOVERTIME: FALSE
                         $getSummary->rest_day_legal_holiday = getTotalHours($getTimesheet->time_in_3, $clockingDateTime, $scheduled['end_time']);
                         $getSummary->rest_day_legal_holiday_overtime = '';
                     } else {
                         //ISOVERTIME: TRUE
                         $getSummary->rest_day_legal_holiday_overtime = getOvertimeHours($clockingDateTime, $scheduled['end_time']);
                         $getSummary->rest_day_legal_holiday = '';
                     }
                 } elseif ('Special non-working day' === $getHolidayByDate->holiday_type) {
                     //Special non-working day
                     //echo "Special non-working day \n";
                     if (!$isOvertime) {
                         //ISOVERTIME: FALSE
                         $getSummary->rest_day_special_holiday = getTotalHours($getTimesheet->time_in_3, $clockingDateTime, $scheduled['end_time']);
                         $getSummary->rest_day_special_holiday_overtime = '';
                     } else {
                         //ISOVERTIME: TRUE
                         $getSummary->rest_day_special_holiday_overtime = getOvertimeHours($clockingDateTime, $scheduled['end_time']);
                         $getSummary->rest_day_special_holiday = '';
                     }
                 }
                 //HOLIDAY: FALSE
             } else {
                 //Regular Day
                 //echo "HOLIDAY: FALSE \n";
                 //echo "Regular Day \n";
                 if (!$isOvertime) {
                     //ISOVERTIME: FALSE
                     $getSummary->rest_day = getTotalHours($getTimesheet->time_in_3, $clockingDateTime, $scheduled['end_time']);
                     $getSummary->rest_day_overtime = '';
                     $getSummary->rest_day_legal_holiday = '';
                     $getSummary->rest_day_special_holiday = '';
                 } else {
                     //ISOVERTIME: TRUE
                     $getSummary->rest_day_overtime = getOvertimeHours($clockingDateTime, $scheduled['end_time']);
                     $getSummary->rest_day = '';
                     $getSummary->rest_day_legal_holiday_overtime = '';
                     $getSummary->rest_day_special_holiday_overtime = '';
                 }
             }
         }
     }
     if ($getTimesheet->save()) {
         $getSummary->save();
         return Redirect::to('/redraw/timesheet');
     }
     //}
 }