public function init()
 {
     //Add Employee time sheets if it is not already created for current week
     $empId = $this->getCurrentEmployeeId();
     if (date('w', strtotime("now")) == 0) {
         $start = date("Y-m-d", strtotime("now"));
     } else {
         $start = date("Y-m-d", strtotime("last Sunday"));
     }
     if (date('w', strtotime("now")) == 6) {
         $end = date("Y-m-d", strtotime("now"));
     } else {
         $end = date("Y-m-d", strtotime("next Saturday"));
     }
     $timeSheet = new EmployeeTimeSheet();
     $timeSheet->Load("employee = ? and date_start = ? and date_end = ?", array($empId, $start, $end));
     if ($timeSheet->date_start == $start && $timeSheet->employee == $empId) {
     } else {
         if (!empty($empId)) {
             $timeSheet->employee = $empId;
             $timeSheet->date_start = $start;
             $timeSheet->date_end = $end;
             $timeSheet->status = "Pending";
             $ok = $timeSheet->Save();
             if (!$ok) {
                 error_log("Error creating time sheet : " . $timeSheet->ErrorMsg());
             }
         }
     }
     //Generate missing timesheets
 }
 public function createPreviousTimesheet($req)
 {
     $employee = $this->baseService->getElement('Employee', $this->getCurrentEmployeeId());
     $timeSheet = new EmployeeTimeSheet();
     $timeSheet->Load("id = ?", array($req->id));
     if ($timeSheet->id != $req->id) {
         return new IceResponse(IceResponse::ERROR, "Timesheet not found");
     }
     if ($timeSheet->employee != $employee->id) {
         return new IceResponse(IceResponse::ERROR, "You don't have permissions to add this Timesheet");
     }
     $end = date("Y-m-d", strtotime("last Saturday", strtotime($timeSheet->date_start)));
     $start = date("Y-m-d", strtotime("last Sunday", strtotime($end)));
     $tempTimeSheet = new EmployeeTimeSheet();
     $tempTimeSheet->Load("employee = ? and date_start = ?", array($employee->id, $start));
     if ($employee->id == $tempTimeSheet->employee) {
         return new IceResponse(IceResponse::ERROR, "Timesheet already exists");
     }
     $newTimeSheet = new EmployeeTimeSheet();
     $newTimeSheet->employee = $employee->id;
     $newTimeSheet->date_start = $start;
     $newTimeSheet->date_end = $end;
     $newTimeSheet->status = "Pending";
     $ok = $newTimeSheet->Save();
     if (!$ok) {
         error_log("Error creating time sheet : " . $newTimeSheet->ErrorMsg());
         return new IceResponse(IceResponse::ERROR, "Error creating Timesheet");
     }
     return new IceResponse(IceResponse::SUCCESS, "");
 }
 private function getLastTimeSheetHours()
 {
     $timeSheet = new EmployeeTimeSheet();
     $timeSheet->Load("employee = ? order by date_end desc limit 1", array(BaseService::getInstance()->getCurrentProfileId()));
     if (empty($timeSheet->employee)) {
         return new IceResponse(IceResponse::SUCCESS, "0:00");
     }
     $timeSheetEntry = new EmployeeTimeEntry();
     $list = $timeSheetEntry->Find("timesheet = ?", array($timeSheet->id));
     $seconds = 0;
     foreach ($list as $entry) {
         $seconds += strtotime($entry->date_end) - strtotime($entry->date_start);
     }
     $minutes = (int) ($seconds / 60);
     $rem = $minutes % 60;
     $hours = ($minutes - $rem) / 60;
     if ($rem < 10) {
         $rem = "0" . $rem;
     }
     return new IceResponse(IceResponse::SUCCESS, $hours . ":" . $rem);
 }