123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- class reportModel extends Staple_Model
- {
- private $db;
- private $timesheets;
- /**
- * @return array
- */
- public function getTimesheets()
- {
- return $this->timesheets;
- }
- /**
- * @param array $timesheets
- */
- public function setTimesheets($timesheets)
- {
- $this->timesheets = $timesheets;
- }
- function __construct($year, $month, $inactive = null)
- {
- $this->db = Staple_DB::get();
- if($inactive != null)
- {
- $staffIds = $this->getStaffIds(1);
- }
- else
- {
- $staffIds = $this->getStaffIds();
- }
- $data = array();
- if(count($staffIds) > 0)
- {
- foreach($staffIds as $key => $value)
- {
- $data[$value] = $this->getTimesheet($key, $year, $month);
- }
- }
- $this->timesheets = $data;
- }
- function getStaffIds($inactive = null)
- {
- $auth = Staple_Auth::get();
- $user = new userModel($auth->getAuthId());
- $userId = $user->getId();
- $authLevel = $user->getAuthLevel();
- $data = array();
- if($authLevel >= 900)
- {
- if($inactive == 1)
- {
- $sql = "
- SELECT id, firstName, lastName FROM accounts WHERE status = 0 ORDER BY lastName ASC
- ";
- }
- else
- {
- $sql = "
- SELECT id, firstName, lastName FROM accounts WHERE status = 1 ORDER BY lastName ASC
- ";
- }
- }
- else
- {
- if($inactive == 1)
- {
- $sql = "
- SELECT id, firstName, lastName FROM accounts WHERE status = 0 AND supervisorId = '" . $this->db->real_escape_string($userId) . "' ORDER BY lastName ASC
- ";
- }
- else
- {
- $sql = "
- SELECT id, firstName, lastName FROM accounts WHERE status = 1 AND supervisorId = '" . $this->db->real_escape_string($userId) . "' ORDER BY lastName ASC
- ";
- }
- }
- $query = $this->db->query($sql);
- while($result = $query->fetch_assoc())
- {
- $data[$result['id']] = $result['lastName'].", ".$result['firstName'];
- }
- return $data;
- }
- function getTimesheet($userId, $year, $month)
- {
- $currentDate = new DateTime();
- $currentDate->setDate($year, $month, 1);
- $currentYear = $currentDate->format('Y');
- $currentMonth = $currentDate->format('m');
- $currentMonthText = $currentDate->format('F');
- $startDate = $currentDate->modify('-1 month +25 day')->format('Y-m-d');
- $startDateTimeString = strtotime($startDate);
- $currentDate->setDate($year, $month, 1);
- $endDate = $currentDate->modify('+25 day')->format('Y-m-d');
- $endDateTimeString = strtotime($endDate);
- $sql = "
- SELECT id FROM timeEntries WHERE inTime > $startDateTimeString AND inTime < $endDateTimeString AND userId = $userId ORDER BY inTime ASC;
- ";
- $data = array();
- $query = $this->db->query($sql);
- while($result = $query->fetch_assoc())
- {
- $data[$result['id']] = $this->calculateEntry($result['id']);
- }
- return $data;
- }
- function calculateEntry($id)
- {
- $sql = "
- SELECT * FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."';
- ";
- $query = $this->db->query($sql);
- $result = $query->fetch_assoc();
- //Set inTime
- $inTime = new DateTime();
- $inTime->setTimestamp($result['inTime']);
- $roundedInTime = $this->nearestQuarterHour($result['inTime']);
- $inTimeRaw = $result['inTime'];
- $inTimeDate = date("Y-m-d", $result['inTime']);
- //Out Time
- $outTime = new DateTime();
- $outTime->setTimestamp($result['outTime']);
- $roundedOutTime = $this->nearestQuarterHour($result['outTime']);
- $outTimeRaw = $result['outTime'];
- $roundedOutTime = $this->nearestQuarterHour($result['outTime']);
- $outTimeDate = date("Y-m-d", $result['outTime']);
- $lessTime = $result['lessTime'];
- $timestamp = $result['timestamp'];
- $note = $result['note'];
- //Calculate Time Worked
- switch($result['lessTime'])
- {
- case 60:
- $lessTime = 1;
- break;
- case 30:
- $lessTime = 0.5;
- break;
- case 15:
- $lessTime = 0.25;
- break;
- default:
- $lessTime = 0;
- }
- //Total Worked Time
- $dateTime1 = new DateTime($roundedInTime);
- $dateTime1->setDate(date('Y',strtotime($inTimeDate)), date('m',strtotime($inTimeDate)), date('d',strtotime($inTimeDate)));
- $dateTime2 = new DateTime($roundedOutTime);
- $dateTime2->setDate(date('Y',strtotime($outTimeDate)), date('m',strtotime($outTimeDate)), date('d',strtotime($outTimeDate)));
- $interval = $dateTime1->diff($dateTime2);
- $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
- if($timeWorked !== 0)
- {
- $timeWorked = $timeWorked;
- }
- else
- {
- $timeWorked = 0;
- }
- //Get Code Information
- $code = new codeModel();
- $codeId = $result['codeId'];
- $code->load($result['codeId']);
- $codeName = $code->getName();
- $data['date'] = date('Y-m-d', $inTimeRaw);
- $data['inTime'] = $inTimeRaw;
- $data['outTime'] = $outTimeRaw;
- $data['lessTime'] = $lessTime;
- $data['timeWorked'] = $timeWorked;
- $data['code'] = $codeName;
- $data['timestamp'] = $timestamp;
- $data['note'] = $note;
- //Get the user of the entry.
- $entry = new timeEntryModel($id);
- if($entry->validated($id,$result['userId']))
- {
- $data['validated'] = 0;
- }
- else
- {
- $data['validated'] = 1;
- }
- return $data;
- }
- function nearestQuarterHour($time)
- {
- //$time = strtotime($time);
- $round = 15*60;
- $rounded = round($time/$round)*$round;
- return date("g:i A", $rounded);
- }
- function timeToDecimal($time)
- {
- $timeArr = explode(':', $time);
- $hours = $timeArr[0]*1;
- $minutes = $timeArr[1]/60;
- $dec = $hours + $minutes;
- if($dec > 0)
- {
- return round($dec,2);
- }
- else
- {
- return 0;
- }
- }
- function weekly()
- {
- }
- function payPeriodTotals($year, $month)
- {
- //Get all users
- $users = new userModel();
- $accounts = $users->listAll();
- $data = array();
- $date = new DateTime();
- $date->setTime(0,0,0);
- $date->setDate($year,$month,26);
- $date->modify('-1 month');
- $startDate = $date->format('U');
- $date->modify('+1 month -1 day');
- $date->setTime(23,59,59);
- $endDate = $date->format('U');
- foreach($accounts as $account)
- {
- $userId = $account['id'];
- $userName = $account['lastName'].", ".$account['firstName'];
- $sql = "
- SELECT * FROM timeEntries WHERE inTime >= '".$this->db->real_escape_string($startDate)."' AND inTime <= '".$this->db->real_escape_string($endDate)."' AND userId = '".$this->db->real_escape_string($userId)."' ORDER BY inTime ASC;
- ";
- $query = $this->db->query($sql);
- $data2 = array();
- $setDate = 0;
- $timeWorked = 0;
- while($result = $query->fetch_assoc())
- {
- $day = new DateTime();
- $day->setTimestamp($result['inTime']);
- $date = $day->format('Y-m-d');
- $entry = $this->calculateEntry($result['id']);
- if($date == $setDate)
- {
- $timeWorked = $timeWorked + $entry['timeWorked'];
- }
- else
- {
- $timeWorked = $entry['timeWorked'];
- }
- $data2[$date] = $timeWorked;
- $setDate = $date;
- }
- $data[$userName] = $data2;
- }
- return $data;
- }
- function payroll($year, $month)
- {
- $users = new userModel();
- $accounts = $users->listAll();
- $data = array();
- $date = new DateTime();
- $date->setTime(0,0,0);
- $date->setDate($year,$month,26);
- $date->modify('-1 month');
- $startDate = $date->format('U');
- $date->modify('+1 month -1 day');
- $date->setTime(23,59,59);
- $endDate = $date->format('U');
- foreach($accounts as $account)
- {
- $userId = $account['id'];
- $userName = $account['lastName'] . ", " . $account['firstName'];
- $sql = "
- SELECT * FROM timeEntries WHERE inTime >= '" . $this->db->real_escape_string($startDate) . "' AND inTime <= '" . $this->db->real_escape_string($endDate) . "' AND userId = '" . $this->db->real_escape_string($userId) . "' ORDER BY inTime ASC;
- ";
- $query = $this->db->query($sql);
- $data2 = array();
- $setCode = 0;
- $timeWorked = 0;
- while($result = $query->fetch_assoc())
- {
- $entry = $this->calculateEntry($result['id']);
- $code = $entry['code'];
- if($code == $setCode)
- {
- $timeWorked = $timeWorked + $entry['timeWorked'];
- }
- else
- {
- $timeWorked = $entry['timeWorked'];
- }
- $data2[$code] = $timeWorked;
- $setCode = $code;
- }
- $data[$userName] = $data2;
- }
- return $data;
- }
- }
|