Refactoring

This commit is contained in:
gamonoid
2017-09-03 20:39:22 +02:00
parent af40881847
commit a7274d3cfd
5075 changed files with 238202 additions and 16291 deletions

View File

@@ -0,0 +1,133 @@
<?php
/*
This file is part of iCE Hrm.
iCE Hrm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
iCE Hrm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with iCE Hrm. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------
Original work Copyright (c) 2012 [Gamonoid Media Pvt. Ltd]
Developer: Thilina Hasantha (thilina.hasantha[at]gmail.com / facebook.com/thilinah)
*/
namespace Attendance\Admin\Api;
use Attendance\Common\Model\Attendance;
use Classes\BaseService;
use Classes\IceResponse;
use Classes\LanguageManager;
use Classes\SubActionManager;
use Utils\LogManager;
class AttendanceActionManager extends SubActionManager
{
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,
LanguageManager::tran('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"
);
} elseif (!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");
} elseif (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");
} elseif (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) {
LogManager::getInstance()->info($attendance->ErrorMsg());
return new IceResponse(IceResponse::ERROR, "Error occurred while saving attendance");
}
return new IceResponse(IceResponse::SUCCESS, $attendance);
}
public function getImages($req)
{
$attendance = BaseService::getInstance()->getElement(
'Attendance',
$req->id,
'{"employee":["Employee","id","first_name+last_name"]}'
);
return new IceResponse(IceResponse::SUCCESS, $attendance);
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace Attendance\Admin\Api;
use Attendance\Common\Model\Attendance;
use Classes\AbstractModuleManager;
use Classes\UIManager;
class AttendanceAdminManager extends AbstractModuleManager
{
public function initializeUserClasses()
{
}
public function initializeFieldMappings()
{
}
public function initializeDatabaseErrorMappings()
{
}
public function setupModuleClassDefinitions()
{
$this->addModelClass('Attendance');
$this->addModelClass('AttendanceStatus');
}
public function getDashboardItemData()
{
$data = array();
$attendance = new Attendance();
$data['numberOfAttendanceLastWeek']
= $attendance->Count("in_time > '".date("Y-m-d H:i:s", strtotime("-1 week"))."'");
if (empty($data['numberOfAttendanceLastWeek'])) {
$data['numberOfAttendanceLastWeek'] = 0;
}
return $data;
}
public function initQuickAccessMenu()
{
UIManager::getInstance()->addQuickAccessMenuItem(
"Clocked In Employees",
"fa-clock-o",
CLIENT_BASE_URL."?g=admin&n=attendance&m=admin_Employees#tabAttendanceStatus",
array("Admin","Manager")
);
}
public function initCalculationHooks()
{
$this->addCalculationHook(
'AttendanceUtil_getTimeWorkedHours',
'Total Hours from Attendance',
'AttendanceUtil',
'getTimeWorkedHours'
);
$this->addCalculationHook(
'AttendanceUtil_getRegularWorkedHours',
'Total Regular Hours from Attendance',
'AttendanceUtil',
'getRegularWorkedHours'
);
$this->addCalculationHook(
'AttendanceUtil_getOverTimeWorkedHours',
'Total Overtime Hours from Attendance',
'AttendanceUtil',
'getOverTimeWorkedHours'
);
$this->addCalculationHook(
'AttendanceUtil_getWeeklyRegularWorkedHours',
'Total Weekly Regular Hours from Attendance',
'AttendanceUtil',
'getWeeklyBasedRegularHours'
);
$this->addCalculationHook(
'AttendanceUtil_getWeeklyOverTimeWorkedHours',
'Total Weekly Overtime Hours from Attendance',
'AttendanceUtil',
'getWeeklyBasedOvertimeHours'
);
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Attendance\Admin\Api;
use Classes\AbstractModuleManager;
class AttendanceDashboardManager extends AbstractModuleManager
{
public function initializeUserClasses()
{
}
public function initializeFieldMappings()
{
}
public function initializeDatabaseErrorMappings()
{
}
public function setupModuleClassDefinitions()
{
$this->addModelClass('Attendance');
}
}

View File

@@ -0,0 +1,132 @@
<?php
/**
* Created by PhpStorm.
* User: Thilina
* Date: 8/13/17
* Time: 8:07 AM
*/
namespace Attendance\Admin\Api;
use Attendance\Common\Model\Attendance;
use Classes\SettingsManager;
class AttendanceUtil
{
public function getAttendanceSummary($employeeId, $startDate, $endDate)
{
$startTime = $startDate." 00:00:00";
$endTime = $endDate." 23:59:59";
$attendance = new Attendance();
$atts = $attendance->Find(
"employee = ? and in_time >= ? and out_time <= ?",
array($employeeId, $startTime, $endTime)
);
$atCalClassName = SettingsManager::getInstance()->getSetting('Attendance: Overtime Calculation Class');
$atCalClassName = '\\Attendance\\Common\\\Calculations\\'.$atCalClassName;
$atCal = new $atCalClassName();
$atSum = $atCal->getDataSeconds($atts, $startDate, true);
return $atSum;
}
public function getTimeWorkedHours($employeeId, $startDate, $endDate)
{
$atSum = $this->getAttendanceSummary($employeeId, $startDate, $endDate);
return round(($atSum['t']/60)/60, 2);
}
public function getRegularWorkedHours($employeeId, $startDate, $endDate)
{
$atSum = $this->getAttendanceSummary($employeeId, $startDate, $endDate);
return round(($atSum['r']/60)/60, 2);
}
public function getOverTimeWorkedHours($employeeId, $startDate, $endDate)
{
$atSum = $this->getAttendanceSummary($employeeId, $startDate, $endDate);
return round(($atSum['o']/60)/60, 2);
}
public function getWeeklyBasedRegularHours($employeeId, $startDate, $endDate)
{
$atSum = $this->getWeeklyBasedOvertimeSummary($employeeId, $startDate, $endDate);
return round(($atSum['r']/60)/60, 2);
}
public function getWeeklyBasedOvertimeHours($employeeId, $startDate, $endDate)
{
$atSum = $this->getWeeklyBasedOvertimeSummary($employeeId, $startDate, $endDate);
return round(($atSum['o']/60)/60, 2);
}
public function getWeeklyBasedOvertimeSummary($employeeId, $startDate, $endDate)
{
$attendance = new Attendance();
$atTimeByWeek = array();
//Find weeks starting from sunday and ending from saturday in day period
$weeks = $this->getWeeklyDays($startDate, $endDate);
foreach ($weeks as $k => $week) {
$startTime = $week[0]." 00:00:00";
$endTime = $week[count($week) - 1]." 23:59:59";
$atts = $attendance->Find(
"employee = ? and in_time >= ? and out_time <= ?",
array($employeeId, $startTime, $endTime)
);
foreach ($atts as $atEntry) {
if ($atEntry->out_time == "0000-00-00 00:00:00" || empty($atEntry->out_time)) {
continue;
}
if (!isset($atTimeByWeek[$k])) {
$atTimeByWeek[$k] = 0;
}
$diff = strtotime($atEntry->out_time) - strtotime($atEntry->in_time);
if ($diff < 0) {
$diff = 0;
}
$atTimeByWeek[$k] += $diff;
}
}
$overtimeStarts = SettingsManager::getInstance()->getSetting('Attendance: Overtime Start Hour');
$overtimeStarts = (is_numeric($overtimeStarts))?floatval($overtimeStarts) * 60 * 60 * 5 : 0;
$regTime = 0;
$overTime = 0;
foreach ($atTimeByWeek as $value) {
if ($value > $overtimeStarts) {
$regTime += $overtimeStarts;
$overTime = $value - $overtimeStarts;
} else {
$regTime += $value;
}
}
return array('r'=>$regTime,'o'=>$overTime);
}
private function getWeeklyDays($startDate, $endDate)
{
$start = new \DateTime($startDate);
$end = new \DateTime($endDate.' 23:59');
$interval = new \DateInterval('P1D');
$dateRange = new \DatePeriod($start, $interval, $end);
$weekNumber = 1;
$weeks = array();
/* @var \DateTime $date */
foreach ($dateRange as $date) {
$weeks[$weekNumber][] = $date->format('Y-m-d');
if ($date->format('w') == 6) {
$weekNumber++;
}
}
return $weeks;
}
}