public function savePunch($req) { $employee = $this->baseService->getElement('Employee', $req->employee, null, true); $inDateTime = $req->in_time; $inDateArr = explode(" ", $inDateTime); $inDate = $inDateArr[0]; $outDateTime = $req->out_time; $outDate = ""; if (!empty($outDateTime)) { $outDateArr = explode(" ", $outDateTime); $outDate = $outDateArr[0]; } $note = $req->note; //check if dates are differnet if (!empty($outDate) && $inDate != $outDate) { return new IceResponse(IceResponse::ERROR, "Attendance entry should be within a single day"); } //compare dates if (!empty($outDateTime) && strtotime($outDateTime) <= strtotime($inDateTime)) { return new IceResponse(IceResponse::ERROR, "Punch-in time should be lesser than Punch-out time"); } //Find all punches for the day $attendance = new Attendance(); $attendanceList = $attendance->Find("employee = ? and DATE_FORMAT( in_time, '%Y-%m-%d' ) = ?", array($employee->id, $inDate)); foreach ($attendanceList as $attendance) { if (!empty($req->id) && $req->id == $attendance->id) { continue; } if (empty($attendance->out_time) || $attendance->out_time == "0000-00-00 00:00:00") { return new IceResponse(IceResponse::ERROR, "There is a non closed attendance entry for today. Please mark punch-out time of the open entry before adding a new one"); } else { if (!empty($outDateTime)) { if (strtotime($attendance->out_time) >= strtotime($outDateTime) && strtotime($attendance->in_time) <= strtotime($outDateTime)) { //-1---0---1---0 || ---0--1---1---0 return new IceResponse(IceResponse::ERROR, "Time entry is overlapping with an existing one"); } else { if (strtotime($attendance->out_time) >= strtotime($inDateTime) && strtotime($attendance->in_time) <= strtotime($inDateTime)) { //---0---1---0---1 || ---0--1---1---0 return new IceResponse(IceResponse::ERROR, "Time entry is overlapping with an existing one"); } else { if (strtotime($attendance->out_time) <= strtotime($outDateTime) && strtotime($attendance->in_time) >= strtotime($inDateTime)) { //--1--0---0--1-- return new IceResponse(IceResponse::ERROR, "Time entry is overlapping with an existing one"); } } } } else { if (strtotime($attendance->out_time) >= strtotime($inDateTime) && strtotime($attendance->in_time) <= strtotime($inDateTime)) { //---0---1---0 return new IceResponse(IceResponse::ERROR, "Time entry is overlapping with an existing one"); } } } } $attendance = new Attendance(); if (!empty($req->id)) { $attendance->Load("id = ?", array($req->id)); } $attendance->in_time = $inDateTime; if (empty($outDateTime)) { $attendance->out_time = "0000-00-00 00:00:00"; } else { $attendance->out_time = $outDateTime; } $attendance->employee = $req->employee; $attendance->note = $note; $ok = $attendance->Save(); if (!$ok) { error_log($attendance->ErrorMsg()); return new IceResponse(IceResponse::ERROR, "Error occured while saving attendance"); } return new IceResponse(IceResponse::SUCCESS, $attendance); }
public function savePunch($req){ $req->date = $req->time; /* if(strtotime($req->date) > strtotime($req->cdate)){ return new IceResponse(IceResponse::ERROR,"You are not allowed to punch a future time"); } */ //check if there is an open punch $openPunch = $this->getPunch($req)->getData(); if(empty($openPunch)){ $openPunch = new Attendance(); } $dateTime = $req->date; $arr = explode(" ",$dateTime); $date = $arr[0]; $currentDateTime = $req->cdate; $arr = explode(" ",$currentDateTime); $currentDate = $arr[0]; if($currentDate != $date){ return new IceResponse(IceResponse::ERROR,"You are not allowed to punch time for a previous date"); } $employee = $this->baseService->getElement('Employee',$this->getCurrentProfileId(),null,true); //check if dates are differnet $arr = explode(" ",$openPunch->in_time); $inDate = $arr[0]; if(!empty($openPunch->in_time) && $inDate != $date){ return new IceResponse(IceResponse::ERROR,"Attendance entry should be within a single day"); } //compare dates if(!empty($openPunch->in_time) && strtotime($dateTime) <= strtotime($openPunch->in_time)){ return new IceResponse(IceResponse::ERROR,"Punch-in time should be lesser than Punch-out time"); } //Find all punches for the day $attendance = new Attendance(); $attendanceList = $attendance->Find("employee = ? and DATE_FORMAT( in_time, '%Y-%m-%d' ) = ?",array($employee->id,$date)); foreach($attendanceList as $attendance){ if(!empty($openPunch->in_time)){ if($openPunch->id == $attendance->id){ continue; } if(strtotime($attendance->out_time) >= strtotime($dateTime) && strtotime($attendance->in_time) <= strtotime($dateTime)){ //-1---0---1---0 || ---0--1---1---0 return new IceResponse(IceResponse::ERROR,"Time entry is overlapping with an existing one 1"); }else if(strtotime($attendance->out_time) >= strtotime($openPunch->in_time) && strtotime($attendance->in_time) <= strtotime($openPunch->in_time)){ //---0---1---0---1 || ---0--1---1---0 return new IceResponse(IceResponse::ERROR,"Time entry is overlapping with an existing one 2"); }else if(strtotime($attendance->out_time) <= strtotime($dateTime) && strtotime($attendance->in_time) >= strtotime($openPunch->in_time)){ //--1--0---0--1-- return new IceResponse(IceResponse::ERROR,"Time entry is overlapping with an existing one 3 ".$attendance->id); } }else{ if(strtotime($attendance->out_time) >= strtotime($dateTime) && strtotime($attendance->in_time) <= strtotime($dateTime)){ //---0---1---0 return new IceResponse(IceResponse::ERROR,"Time entry is overlapping with an existing one 4"); } } } if(!empty($openPunch->in_time)){ $openPunch->out_time = $dateTime; $openPunch->note = $req->note; $this->baseService->audit(IceConstants::AUDIT_ACTION, "Punch Out \ time:".$openPunch->out_time); }else{ $openPunch->in_time = $dateTime; $openPunch->out_time = '0000-00-00 00:00:00'; $openPunch->note = $req->note; $openPunch->employee = $employee->id; $this->baseService->audit(IceConstants::AUDIT_ACTION, "Punch In \ time:".$openPunch->in_time); } $ok = $openPunch->Save(); if(!$ok){ LogManager::getInstance()->info($openPunch->ErrorMsg()); return new IceResponse(IceResponse::ERROR,"Error occured while saving attendance"); } return new IceResponse(IceResponse::SUCCESS,$openPunch); }
public function savePunch($req) { $useServerTime = SettingsManager::getInstance()->getSetting('Attendance: Use Department Time Zone'); $currentEmployeeTimeZone = BaseService::getInstance()->getCurrentEmployeeTimeZone(); if ($useServerTime == '1' && !empty($currentEmployeeTimeZone)) { date_default_timezone_set('Asia/Colombo'); $date = new DateTime("now", new DateTimeZone('Asia/Colombo')); $date->setTimezone(new DateTimeZone($currentEmployeeTimeZone)); $req->time = $date->format('Y-m-d H:i:s'); } $req->date = $req->time; //check if there is an open punch $openPunch = $this->getPunch($req)->getData(); if (empty($openPunch)) { $openPunch = new Attendance(); } $dateTime = $req->date; $arr = explode(" ", $dateTime); $date = $arr[0]; $employee = $this->baseService->getElement('Employee', $this->getCurrentProfileId(), null, true); //check if dates are differnet $arr = explode(" ", $openPunch->in_time); $inDate = $arr[0]; if (!empty($openPunch->in_time) && $inDate != $date) { return new IceResponse(IceResponse::ERROR, "Attendance entry should be within a single day"); } //compare dates if (!empty($openPunch->in_time) && strtotime($dateTime) <= strtotime($openPunch->in_time)) { return new IceResponse(IceResponse::ERROR, "Punch-in time should be lesser than Punch-out time"); } //Find all punches for the day $attendance = new Attendance(); $attendanceList = $attendance->Find("employee = ? and DATE_FORMAT( in_time, '%Y-%m-%d' ) = ?", array($employee->id, $date)); foreach ($attendanceList as $attendance) { if (!empty($openPunch->in_time)) { if ($openPunch->id == $attendance->id) { continue; } if (strtotime($attendance->out_time) >= strtotime($dateTime) && strtotime($attendance->in_time) <= strtotime($dateTime)) { //-1---0---1---0 || ---0--1---1---0 return new IceResponse(IceResponse::ERROR, "Time entry is overlapping with an existing one"); } else { if (strtotime($attendance->out_time) >= strtotime($openPunch->in_time) && strtotime($attendance->in_time) <= strtotime($openPunch->in_time)) { //---0---1---0---1 || ---0--1---1---0 return new IceResponse(IceResponse::ERROR, "Time entry is overlapping with an existing one"); } else { if (strtotime($attendance->out_time) <= strtotime($dateTime) && strtotime($attendance->in_time) >= strtotime($openPunch->in_time)) { //--1--0---0--1-- return new IceResponse(IceResponse::ERROR, "Time entry is overlapping with an existing one " . $attendance->id); } } } } else { if (strtotime($attendance->out_time) >= strtotime($dateTime) && strtotime($attendance->in_time) <= strtotime($dateTime)) { //---0---1---0 return new IceResponse(IceResponse::ERROR, "Time entry is overlapping with an existing one"); } } } if (!empty($openPunch->in_time)) { $openPunch->out_time = $dateTime; $openPunch->note = $req->note; $this->baseService->audit(IceConstants::AUDIT_ACTION, "Punch Out \\ time:" . $openPunch->out_time); } else { $openPunch->in_time = $dateTime; $openPunch->out_time = '0000-00-00 00:00:00'; $openPunch->note = $req->note; $openPunch->employee = $employee->id; $this->baseService->audit(IceConstants::AUDIT_ACTION, "Punch In \\ time:" . $openPunch->in_time); } $ok = $openPunch->Save(); if (!$ok) { LogManager::getInstance()->info($openPunch->ErrorMsg()); return new IceResponse(IceResponse::ERROR, "Error occured while saving attendance"); } return new IceResponse(IceResponse::SUCCESS, $openPunch); }