瀏覽代碼

Creating a way for supervisor and administrative level users to view time sheets for their direct staff.

Adam Day 9 年之前
父節點
當前提交
01f0ff6f5a

+ 14 - 3
application/controllers/reportsController.php

@@ -8,14 +8,25 @@ class reportsController extends Staple_Controller
     {
         $auth = Staple_Auth::get();
         $this->authLevel = $auth->getAuthLevel();
-        if($this->authLevel < 900)
+        if($this->authLevel < 500)
         {
             header("location:".$this->_link(array('index','index'))."");
         }
     }
 
-    public function index()
+    public function index($year = null, $month = null)
     {
-        echo "Reports";
+        if($year == null)
+        {
+            $year = date('Y');
+        }
+
+        if($month == null)
+        {
+            $month = date('m');
+        }
+
+        $report = new reportModel($year,$month);
+
     }
 }

+ 188 - 0
application/models/reportModel.php

@@ -0,0 +1,188 @@
+<?php
+class reportModel extends Staple_Model
+{
+    private $db;
+    private $timesheets;
+
+    function __construct($year, $month)
+    {
+        $this->db = Staple_DB::get();
+        $staffIds = $this->getStaffIds();
+
+        $data = array();
+
+        foreach($staffIds as $key => $value)
+        {
+            $data[$value] = $this->getTimesheet($key, $year, $month);
+        }
+
+        echo "<pre>";
+        print_r($data);
+        echo "</pre>";
+
+        $this->timesheets = $data;
+    }
+
+    function getStaffIds()
+    {
+        $auth = Staple_Auth::get();
+        $user = new userModel($auth->getAuthId());
+        $userId = $user->getId();
+        $authLevel = $user->getAuthLevel();
+
+        if($authLevel >= 900)
+        {
+            $sql = "
+            SELECT id, firstName, lastName FROM accounts WHERE 1 ORDER BY lastName ASC
+            ";
+        }
+        else
+        {
+            $sql = "
+            SELECT id, firstName, lastName FROM accounts WHERE 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[] = $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();
+
+        print_r($result);
+
+        //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'];
+
+        //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;
+
+        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;
+        }
+    }
+}

+ 11 - 4
application/models/timesheetModel.php

@@ -282,13 +282,20 @@
 			$this->totals = $totals;
 		}
 
-		function __construct($year, $month)
+		function __construct($year, $month, $user = null)
 		{
 			$this->db = Staple_DB::get();
 
-			//Get batchID
-			$user = new userModel();
-			$this->batch = $user->getBatchId();
+			if($user == null)
+			{
+				//Get batchID
+				$user = new userModel();
+				$this->batch = $user->getBatchId();
+			}
+			else
+			{
+				$user = new userModel($user);
+			}
 
 			//Current Dates
 			$currentDate = new DateTime();

+ 12 - 4
application/models/userModel.php

@@ -158,14 +158,21 @@
 			$this->pin = $pin;
 		}
 
-		function __construct()
+		function __construct($user = null)
 		{
 			$this->db = Staple_DB::get();
 
-			$auth = Staple_Auth::get();
-			$username = $auth->getAuthId();
+			if($user == null)
+			{
+				$auth = Staple_Auth::get();
+				$username = $auth->getAuthId();
+			}
+			else
+			{
+				$username = $user;
+			}
 
-			$sql = "SELECT id, username, firstName, lastName, authLevel, batchId FROM accounts WHERE username = '".$this->db->real_escape_string($username)."'";
+			$sql = "SELECT id, username, firstName, lastName, authLevel, batchId, supervisorId FROM accounts WHERE username = '".$this->db->real_escape_string($username)."'";
 			if($this->db->query($sql)->fetch_row() > 0)
 			{
 				$query = $this->db->query($sql);
@@ -177,6 +184,7 @@
 				$this->setLastName($result['lastName']);
 				$this->setAuthLevel($result['authLevel']);
 				$this->setBatchId($result['batchId']);
+				$this->setSupervisorId($result['supervisorId']);
 			}
 			else
 			{

+ 10 - 0
application/views/reports/index.phtml

@@ -0,0 +1,10 @@
+<div class="section">
+    <div class="row">
+        <div class="small-12 columns text-center">
+            <h1><i class="fa fa-file"></i> Reports</h1>
+        </div>
+    </div>
+    <div class="row">
+
+    </div>
+</div>