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->getCurrentProfileId(), null, true);
     $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) {
         LogManager::getInstance()->info("Error creating time sheet : " . $newTimeSheet->ErrorMsg());
         return new IceResponse(IceResponse::ERROR, "Error creating Timesheet");
     }
     return new IceResponse(IceResponse::SUCCESS, "");
 }