Parcourir la source

Moved the timesheet into it's own controller. The time submission process is working and saving new time entries to the DB table.

Adam Day il y a 9 ans
Parent
commit
0273d6f888

+ 0 - 30
application/controllers/indexController.php

@@ -11,35 +11,5 @@ class indexController extends Staple_Controller
 		$messages = array("The library will be closed on Monday for whatever reason. Just remember to not come in!");
 		//$this->view->messages = $messages;
 	}
-
-	public function timesheet()
-	{
-		$timesheet = new timesheetModel();
-		$this->view->timesheet = $timesheet->load();
-
-		$insertTimeForm = new insertTimeForm();
-
-		if($insertTimeForm->wasSubmitted())
-		{
-			$insertTimeForm->addData($_POST);
-			if($insertTimeForm->validate())
-			{
-				echo "Valid Form!";
-			}
-			else
-			{
-				$this->view->insertTimeForm = $insertTimeForm;
-			}
-		}
-		else
-		{
-			$this->view->insertTimeForm = $insertTimeForm;
-		}
-	}
-
-	public function insert()
-	{
-
-	}
 }
 ?>

+ 73 - 0
application/controllers/timesheetController.php

@@ -0,0 +1,73 @@
+<?php
+class timesheetController extends Staple_Controller
+{
+    public function _start()
+    {
+
+    }
+
+    public function index()
+    {
+        $timesheet = new timesheetModel();
+        $this->view->timesheet = $timesheet->load();
+
+        $insertTimeForm = new insertTimeForm();
+
+        if($insertTimeForm->wasSubmitted())
+        {
+            $insertTimeForm->addData($_POST);
+            if($insertTimeForm->validate())
+            {
+                $data = $insertTimeForm->exportFormData();
+
+                if($data['inTime'] < $data['outTime'])
+                {
+                    //Set Varibales
+                    $timesheet = new timesheetModel();
+                    $userId = Staple_Auth::get();
+                    $user = new userModel($userId->getAuthId());
+                    $timesheet->setUserId($user->getId());
+                    $timesheet->setDate($data['date']);
+                    $timesheet->setInTime($data['inTime']);
+                    $timesheet->setOutTime($data['outTime']);
+                    $timesheet->setLessTime($data['lessTime']);
+                    $timesheet->setCodeId($data['code']);
+
+                    //Save
+                    if($timesheet->save())
+                    {
+                        header("location:".$this->_link(array('timesheet'))."");
+                    }
+                    else
+                    {
+                        $this->view->message = "Unable to save entry.";
+                    }
+                }
+                else
+                {
+                    $insertTimeForm->message = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
+                    $this->view->insertTimeForm = $insertTimeForm;
+                }
+            }
+            else
+            {
+                $this->view->insertTimeForm = $insertTimeForm;
+            }
+        }
+        else
+        {
+            $this->view->insertTimeForm = $insertTimeForm;
+        }
+    }
+
+    public function timesheet()
+    {
+
+    }
+
+    public function reports()
+    {
+
+    }
+}
+?>

+ 3 - 2
application/forms/insertTimeForm.php

@@ -7,7 +7,7 @@ class insertTimeForm extends Staple_Form
         $this->setLayout('insertFormLayout');
 
         $this->setName('insertTimeForm')
-            ->setAction($this->link(array('index','timesheet')));
+            ->setAction($this->link(array('timesheet')));
 
         $date = new Staple_Form_FoundationDateElement('date','Date');
         $date->setRequired()
@@ -25,9 +25,10 @@ class insertTimeForm extends Staple_Form
         $lessTime->setRequired()
             ->addOptionsArray(array("0"=>"None","60"=>"1 Hour","30"=>"30 Minutes","15"=>"15 Minutes"));
 
+        $timeCodes = new codeModel();
         $code = new Staple_Form_FoundationSelectElement('code','Code');
         $code->setRequired()
-            ->addOptionsArray(array("normal"=>"Normal","vacation"=>"Vacation","sick"=>"Sick","holiday"=>"Holiday",));
+            ->addOptionsArray($timeCodes->allCodes());
 
         $submit = new Staple_Form_FoundationSubmitElement('submit','Submit');
         $submit->addClass('button radius');

+ 14 - 1
application/forms/layouts/insertFormLayout.phtml

@@ -1,5 +1,18 @@
 <div class="row">
-    <?php echo $this->formstart(); ?>
+    <?php
+        if(count($this->message) > 0)
+        {
+            echo "<div class=\"small-12 columns\">";
+            echo "<div data-alert class=\"alert-box alert\">";
+            foreach($this->message as $message)
+            {
+                echo $message;
+            }
+            echo "<a href=\"#\" class=\"close\">&times;</a></div>";
+            echo "</div>";
+        }
+        echo $this->formstart();
+    ?>
     <div class="small-6 medium-2 columns">
         <?php echo $this->fields['date'] ?>
     </div>

+ 2 - 2
application/layouts/main.phtml

@@ -41,8 +41,8 @@
             <section class="top-bar-section">
                 <!-- Right Nav Section -->
                 <ul class="left">
-                    <li><a href="<?php echo $this->link(array('index','index')) ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
-                    <li><a href="<?php echo $this->link(array('index','timesheet')) ?>"><i class="fa fa-clock-o"></i> Timesheet</a></li>
+                    <li><a href="<?php echo $this->link(array('index')) ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
+                    <li><a href="<?php echo $this->link(array('timesheet')) ?>"><i class="fa fa-clock-o"></i> Timesheet</a></li>
                     <li><a href="<?php echo $this->link(array('index','reports')) ?>"><i class="fa fa-file"></i> Reports</a></li>
                 </ul>
 

+ 15 - 0
application/models/codeModel.php

@@ -92,5 +92,20 @@
 				return true;
 			}
 		}
+
+		function allCodes()
+		{
+			$sql = "SELECT id, name FROM timeCodes WHERE 1";
+			if ($this->db->query($sql)->fetch_row() > 0)
+			{
+				$query = $this->db->query($sql);
+
+				while($result = $query->fetch_assoc())
+                {
+                    $data[$result['id']] = $result['name'];
+                }
+				return $data;
+			}
+		}
 	}
 ?>

+ 174 - 111
application/models/timesheetModel.php

@@ -6,106 +6,123 @@
 
 		private $id;
 		private $userId;
+        private $date;
 		private $inTime;
 		private $outTime;
 		private $lessTime;
 		private $codeId;
 
-		/**
-		 * @return mixed
-		 */
-		public function getId()
-		{
-			return $this->id;
-		}
-
-		/**
-		 * @param mixed $id
-		 */
-		public function setId($id)
-		{
-			$this->id = $id;
-		}
-
-		/**
-		 * @return mixed
-		 */
-		public function getUserId()
-		{
-			return $this->userId;
-		}
-
-		/**
-		 * @param mixed $userId
-		 */
-		public function setUserId($userId)
-		{
-			$this->userId = $userId;
-		}
-
-		/**
-		 * @return mixed
-		 */
-		public function getInTime()
-		{
-			return $this->inTime;
-		}
-
-		/**
-		 * @param mixed $inTime
-		 */
-		public function setInTime($inTime)
-		{
-			$this->inTime = $inTime;
-		}
-
-		/**
-		 * @return mixed
-		 */
-		public function getOutTime()
-		{
-			return $this->outTime;
-		}
-
-		/**
-		 * @param mixed $outTime
-		 */
-		public function setOutTime($outTime)
-		{
-			$this->outTime = $outTime;
-		}
-
-		/**
-		 * @return mixed
-		 */
-		public function getLessTime()
-		{
-			return $this->lessTime;
-		}
-
-		/**
-		 * @param mixed $lessTime
-		 */
-		public function setLessTime($lessTime)
-		{
-			$this->lessTime = $lessTime;
-		}
-
-		/**
-		 * @return mixed
-		 */
-		public function getCodeId()
-		{
-			return $this->codeId;
-		}
-
-		/**
-		 * @param mixed $codeId
-		 */
-		public function setCodeId($codeId)
-		{
-			$this->codeId = $codeId;
-		}
+        /**
+         * @return mixed
+         */
+        public function getId()
+        {
+            return $this->id;
+        }
+
+        /**
+         * @param mixed $id
+         */
+        public function setId($id)
+        {
+            $this->id = $id;
+        }
+
+        /**
+         * @return mixed
+         */
+        public function getUserId()
+        {
+            return $this->userId;
+        }
+
+        /**
+         * @param mixed $userId
+         */
+        public function setUserId($userId)
+        {
+            $this->userId = $userId;
+        }
+
+        /**
+         * @return mixed
+         */
+        public function getDate()
+        {
+            return $this->date;
+        }
+
+        /**
+         * @param mixed $date
+         */
+        public function setDate($date)
+        {
+            $this->date = $date;
+        }
+
+        /**
+         * @return mixed
+         */
+        public function getInTime()
+        {
+            return $this->inTime;
+        }
+
+        /**
+         * @param mixed $inTime
+         */
+        public function setInTime($inTime)
+        {
+            $this->inTime = $inTime;
+        }
+
+        /**
+         * @return mixed
+         */
+        public function getOutTime()
+        {
+            return $this->outTime;
+        }
+
+        /**
+         * @param mixed $outTime
+         */
+        public function setOutTime($outTime)
+        {
+            $this->outTime = $outTime;
+        }
+
+        /**
+         * @return mixed
+         */
+        public function getLessTime()
+        {
+            return $this->lessTime;
+        }
+
+        /**
+         * @param mixed $lessTime
+         */
+        public function setLessTime($lessTime)
+        {
+            $this->lessTime = $lessTime;
+        }
+
+        /**
+         * @return mixed
+         */
+        public function getCodeId()
+        {
+            return $this->codeId;
+        }
+
+        /**
+         * @param mixed $codeId
+         */
+        public function setCodeId($codeId)
+        {
+            $this->codeId = $codeId;
+        }
 
 		function __construct()
 		{
@@ -169,6 +186,12 @@
 							//MonthYear
 							$data2['date']['my'] = $date->format("m/y");
 
+                            //MonthDay
+                            $data2['date']['md'] = $date->format("m/d");
+
+                            //DateMonthYear
+                            $data2['date']['mdy'] = $date->format("m/d/y");
+
 							//In Time
 							$data2['rawInTime'] = $date->format("g:i A");
 							$data2['roundedInTime'] = $this->nearestQuarterHour($date->format("g:i A"));
@@ -181,13 +204,38 @@
 							//Less Time
 							$data2['lessTime'] = $entry['lessTime'];
 
+                            switch($entry['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($data2['roundedInTime']);
 							$dateTime2 = new DateTime($data2['roundedOutTime']);
 							$interval = $dateTime1->diff($dateTime2);
 
 							$data2['timeWorked'] = $interval->h.":".$interval->i;
-							$data2['timeWorkedDec'] = $this->time_to_decimal($interval->h.":".$interval->i);
+
+                            $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
+
+                            if($timeWorked > 0)
+                            {
+                                $data2['timeWorkedDec'] = $timeWorked;
+                            }
+                            else
+                            {
+                                $data2['timeWorkedDec'] = 0;
+                            }
 
 							$data2['code'] = $codeName;
 
@@ -204,46 +252,61 @@
             }
 		}
 
-		function nearestQuarterHour($time)
+		private function nearestQuarterHour($time)
 		{
 			$time = strtotime($time);
 			$round = 15*60;
 			$rounded = round($time/$round)*$round;
+
 			return date("g:i A", $rounded);
 		}
 
-		function time_to_decimal($time)
+		private function timeToDecimal($time)
 		{
 			$timeArr = explode(':', $time);
 			$hours = $timeArr[0]*1;
 			$minutes = $timeArr[1]/60;
 			$dec = $hours + $minutes;
-			return round($dec,2);
+
+            if($dec > 0)
+            {
+                return round($dec,2);
+            }
+            else
+            {
+                return 0;
+            }
 		}
 
 		function save()
 		{
 			$id = $this->getId();
-			
-			if($id == NULL)
+
+            $inTime = strtotime($this->getDate()." ".$this->getInTime());
+            $outTime = strtotime($this->getDate()." ".$this->getOutTime());
+
+            if($id == NULL)
 			{
 				//Insert new item
-				$sql = "INSERT INTO posts (title, content, expires, post) 
+				$sql = "INSERT INTO timeEntries (userId, inTime, outTime, lessTime, codeId)
 					VALUES (
-						'".$this->db->real_escape_string($title)."',
-						'".$this->db->real_escape_string($content)."',
-						'".$this->db->real_escape_string($expire)."',
-						'".$this->db->real_escape_string($post)."'
+						'".$this->db->real_escape_string($this->getUserId())."',
+						'".$this->db->real_escape_string($inTime)."',
+						'".$this->db->real_escape_string($outTime)."',
+						'".$this->db->real_escape_string($this->getLessTime())."',
+						'".$this->db->real_escape_string($this->getCodeId())."'
 						)";
 			}
 			else
 			{
 				//Update item
-				$sql = "UPDATE posts SET
-					title='".$this->db->real_escape_string($title)."',
-					content='".$this->db->real_escape_string($content)."',
-					expires='".$this->db->real_escape_string($expire)."',
-					post='".$this->db->real_escape_string($post)."' WHERE id='".$this->db->real_escape_string($id)."'
+				$sql = "UPDATE timeEntries SET
+					userId='".$this->db->real_escape_string($this->getUserId())."',
+					inTime='".$this->db->real_escape_string($inTime)."',
+					outTime='".$this->db->real_escape_string($outTime)."',
+					lessTime='".$this->db->real_escape_string($this->getLessTime())."',
+                    codeId='".$this->db->real_escape_string($this->getCodeId())."'
+					WHERE id='".$this->db->real_escape_string($id)."'
 				";
 			}
 			

+ 16 - 2
application/views/index/timesheet.phtml → application/views/timesheet/index.phtml

@@ -1,5 +1,13 @@
 <div class="section">
     <div class="row">
+        <?php
+            if(count($this->message) > 0)
+            {
+                echo "<div class=\"small-12 columns\"><div data-alert class=\"alert-box warning\">";
+                echo $this->message;
+                echo "<a href=\"#\" class=\"close\">&times;</a></div></div>";
+            }
+        ?>
         <div class="small-12 columns">
             <?php echo $this->insertTimeForm ?>
         </div>
@@ -54,7 +62,7 @@
             echo "
             <div class=\"row\">
                 <div class=\"small-2 columns\">
-                    <a href=\"\" class=\"button success radius expand\">".$entry['date']['dayShort']." ".$entry['date']['my']."</a>
+                    <a href=\"\" class=\"button success radius expand\">".$entry['date']['dayShort']." ".$entry['date']['mdy']."</a>
                 </div>
                 <div class=\"small-2 columns\">
                     <span data-tooltip aria-haspopup=\"true\" class=\"has-tip\" title=\"Entered as: ".$entry['rawInTime']."\">".$entry['roundedInTime']."</span>
@@ -79,7 +87,13 @@
     }
     else
     {
-        echo "No time submitted.";
+        echo "
+            <div class=\"row\">
+                <div class=\"small-12 columns text-center\">
+                    No time submitted.
+                </div>
+            </div>
+        ";
     }
 
 ?>