Răsfoiți Sursa

Merge pull request #13 from advation/overlapFix

Overlap fix
Adam 9 ani în urmă
părinte
comite
aefcd150f4

+ 12 - 8
application/controllers/timesheetController.php

@@ -51,8 +51,10 @@ class timesheetController extends Staple_Controller
                 if($userDate >= $startDate && $userDate <= $endDate)
                 {
                     //Compare in Times and out Times.
+                    /*
                     if(strtotime($data['inTime']) < strtotime($data['outTime']))
                     {
+                    */
                         //Create a new entry object and set properties
                         $entry = new timeEntryModel();
                         $entry->setDate($data['date']);
@@ -76,6 +78,7 @@ class timesheetController extends Staple_Controller
                             $form->errorMessage = array($message);
                             $this->view->insertTimeForm = $form;
                         }
+                    /*
                     }
                     else
                     {
@@ -83,6 +86,7 @@ class timesheetController extends Staple_Controller
                         $form->errorMessage = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
                         $this->view->insertTimeForm = $form;
                     }
+                    */
                 }
                 else
                 {
@@ -229,8 +233,8 @@ class timesheetController extends Staple_Controller
                     if($userDate >= $startDate && $userDate <= $endDate)
                     {
                         //Compare in Times and out Times.
-                        if(strtotime($data['inTime']) < strtotime($data['outTime']))
-                        {
+                        //if(strtotime($data['inTime']) < strtotime($data['outTime']))
+                        //{
                             //Create a new entry object and set properties
                             $entry = new timeEntryModel();
                             $entry->setId($id);
@@ -254,13 +258,13 @@ class timesheetController extends Staple_Controller
                                 $form->errorMessage = array($message);
                                 $this->view->form = $form;
                             }
-                        }
-                        else
-                        {
+                        //}
+                        //else
+                        //{
                             //Return the same form with error message.
-                            $form->errorMessage = array("<i class='fa fa-warning'></i> <b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
-                            $this->view->form = $form;
-                        }
+                        //    $form->errorMessage = array("<i class='fa fa-warning'></i> <b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
+                        //    $this->view->form = $form;
+                        //}
                     }
                     else
                     {

+ 5 - 0
application/forms/changeYearForm.php

@@ -14,6 +14,11 @@ class changeYearForm extends Staple_Form
             ->addOptionsArray($this->getYears())
             ->addValidator(new Staple_Form_Validate_InArray($this->getYears()));
 
+        if(count($this->getYears()) == 0)
+        {
+            $year->addOption(date('Y'),date('Y'));
+        }
+
         $submit = new Staple_Form_FoundationSubmitElement('submit','Submit');
         $submit->addClass('button expand radius');
 

+ 165 - 62
application/models/timeEntryModel.php

@@ -10,9 +10,11 @@
         private $inTime;
         private $inTimeRaw;
         private $roundedInTime;
+        private $inTimeDate;
         private $outTime;
 		private $outTimeRaw;
         private $roundedOutTime;
+        private $outTimeDate;
 		private $lessTime;
 		private $codeId;
         private $codeName;
@@ -54,33 +56,33 @@
         /**
          * @return mixed
          */
-        public function getInTime()
+        public function getFullDate()
         {
-            return $this->inTime;
+            return $this->fullDate;
         }
 
         /**
-         * @param mixed $inTime
+         * @param mixed $fullDate
          */
-        public function setInTime($inTime)
+        public function setFullDate($fullDate)
         {
-            $this->inTime = $inTime;
+            $this->fullDate = $fullDate;
         }
 
         /**
          * @return mixed
          */
-        public function getOutTime()
+        public function getInTime()
         {
-            return $this->outTime;
+            return $this->inTime;
         }
 
         /**
-         * @param mixed $outTime
+         * @param mixed $inTime
          */
-        public function setOutTime($outTime)
+        public function setInTime($inTime)
         {
-            $this->outTime = $outTime;
+            $this->inTime = $inTime;
         }
 
         /**
@@ -102,145 +104,177 @@
         /**
          * @return mixed
          */
-        public function getOutTimeRaw()
+        public function getRoundedInTime()
         {
-            return $this->outTimeRaw;
+            return $this->roundedInTime;
         }
 
         /**
-         * @param mixed $outTimeRaw
+         * @param mixed $roundedInTime
          */
-        public function setOutTimeRaw($outTimeRaw)
+        public function setRoundedInTime($roundedInTime)
         {
-            $this->outTimeRaw = $outTimeRaw;
+            $this->roundedInTime = $roundedInTime;
         }
 
         /**
          * @return mixed
          */
-        public function getLessTime()
+        public function getInTimeDate()
         {
-            return $this->lessTime;
+            return $this->inTimeDate;
         }
 
         /**
-         * @param mixed $lessTime
+         * @param mixed $inTimeDate
          */
-        public function setLessTime($lessTime)
+        public function setInTimeDate($inTimeDate)
         {
-            $this->lessTime = $lessTime;
+            $this->inTimeDate = $inTimeDate;
         }
 
         /**
          * @return mixed
          */
-        public function getCodeId()
+        public function getOutTime()
         {
-            return $this->codeId;
+            return $this->outTime;
         }
 
         /**
-         * @param mixed $codeId
+         * @param mixed $outTime
          */
-        public function setCodeId($codeId)
+        public function setOutTime($outTime)
         {
-            $this->codeId = $codeId;
+            $this->outTime = $outTime;
         }
 
         /**
          * @return mixed
          */
-        public function getCodeName()
+        public function getOutTimeRaw()
         {
-            return $this->codeName;
+            return $this->outTimeRaw;
         }
 
         /**
-         * @param mixed $codeName
+         * @param mixed $outTimeRaw
          */
-        public function setCodeName($codeName)
+        public function setOutTimeRaw($outTimeRaw)
         {
-            $this->codeName = $codeName;
+            $this->outTimeRaw = $outTimeRaw;
         }
 
         /**
          * @return mixed
          */
-        public function getTimeWorked()
+        public function getRoundedOutTime()
         {
-            return $this->timeWorked;
+            return $this->roundedOutTime;
         }
 
         /**
-         * @param mixed $timeWorked
+         * @param mixed $roundedOutTime
          */
-        public function setTimeWorked($timeWorked)
+        public function setRoundedOutTime($roundedOutTime)
         {
-            $this->timeWorked = $timeWorked;
+            $this->roundedOutTime = $roundedOutTime;
         }
 
         /**
          * @return mixed
          */
-        public function getRoundedInTime()
+        public function getOutTimeDate()
         {
-            return $this->roundedInTime;
+            return $this->outTimeDate;
         }
 
         /**
-         * @param mixed $roundedInTime
+         * @param mixed $outTimeDate
          */
-        public function setRoundedInTime($roundedInTime)
+        public function setOutTimeDate($outTimeDate)
         {
-            $this->roundedInTime = $roundedInTime;
+            $this->outTimeDate = $outTimeDate;
         }
 
         /**
          * @return mixed
          */
-        public function getRoundedOutTime()
+        public function getLessTime()
         {
-            return $this->roundedOutTime;
+            return $this->lessTime;
         }
 
         /**
-         * @param mixed $roundedOutTime
+         * @param mixed $lessTime
          */
-        public function setRoundedOutTime($roundedOutTime)
+        public function setLessTime($lessTime)
         {
-            $this->roundedOutTime = $roundedOutTime;
+            $this->lessTime = $lessTime;
         }
 
         /**
          * @return mixed
          */
-        public function getBatchId()
+        public function getCodeId()
         {
-            return $this->batchId;
+            return $this->codeId;
         }
 
         /**
-         * @param mixed $batchId
+         * @param mixed $codeId
          */
-        public function setBatchId($batchId)
+        public function setCodeId($codeId)
         {
-            $this->batchId = $batchId;
+            $this->codeId = $codeId;
         }
 
         /**
          * @return mixed
          */
-        public function getFullDate()
+        public function getCodeName()
         {
-            return $this->fullDate;
+            return $this->codeName;
         }
 
         /**
-         * @param mixed $fullDate
+         * @param mixed $codeName
          */
-        public function setFullDate($fullDate)
+        public function setCodeName($codeName)
         {
-            $this->fullDate = $fullDate;
+            $this->codeName = $codeName;
+        }
+
+        /**
+         * @return mixed
+         */
+        public function getTimeWorked()
+        {
+            return $this->timeWorked;
+        }
+
+        /**
+         * @param mixed $timeWorked
+         */
+        public function setTimeWorked($timeWorked)
+        {
+            $this->timeWorked = $timeWorked;
+        }
+
+        /**
+         * @return mixed
+         */
+        public function getBatchId()
+        {
+            return $this->batchId;
+        }
+
+        /**
+         * @param mixed $batchId
+         */
+        public function setBatchId($batchId)
+        {
+            $this->batchId = $batchId;
         }
 
 		function __construct($id = null)
@@ -267,6 +301,7 @@
                     $this->setInTime($inTime->format('h:i A'));
                     $this->setInTimeRaw($result['inTime']);
                     $this->setRoundedInTime($this->nearestQuarterHour($result['inTime']));
+                    $this->setInTimeDate(date("Y-m-d", $result['inTime']));
 
                     //Out Time
                     $outTime = new DateTime();
@@ -274,6 +309,7 @@
                     $this->setOutTime($outTime->format('h:i A'));
                     $this->setOutTimeRaw($result['outTime']);
                     $this->setRoundedOutTime($this->nearestQuarterHour($result['outTime']));
+                    $this->setOutTimeDate(date("Y-m-d", $result['outTime']));
 
                     $this->setLessTime($result['lessTime']);
 
@@ -295,7 +331,9 @@
 
                     //Total Worked Time
                     $dateTime1 = new DateTime($this->roundedInTime);
+                    $dateTime1->setDate(date('Y',strtotime($this->inTimeDate)), date('m',strtotime($this->inTimeDate)), date('d',strtotime($this->inTimeDate)));
                     $dateTime2 = new DateTime($this->roundedOutTime);
+                    $dateTime2->setDate(date('Y',strtotime($this->outTimeDate)), date('m',strtotime($this->outTimeDate)), date('d',strtotime($this->outTimeDate)));
                     $interval = $dateTime1->diff($dateTime2);
 
                     $timeWorked = $this->timeToDecimal($interval->h.":".$interval->i)-$lessTime;
@@ -352,8 +390,13 @@
             $inTime = strtotime($this->getDate()." ".$this->getInTime());
             $outTime = strtotime($this->getDate()." ".$this->getOutTime());
 
+            if(strtotime($this->getDate()." ".$this->getInTime()) > strtotime($this->getDate()." ".$this->getOutTime()))
+            {
+                $outTime = strtotime($this->getDate()." 12:00 AM")+86400;
+            }
+
             if($this->id == NULL)
-			{
+            {
                 if($this->_overlap($inTime,$outTime))
                 {
                     //Insert new item
@@ -373,9 +416,9 @@
                         return true;
                     }
                 }
-			}
-			else
-			{
+            }
+            else
+            {
                 if($this->_overlap($inTime,$outTime,$this->getId()))
                 {
                     //Update item
@@ -395,7 +438,7 @@
                         return true;
                     }
                 }
-			}
+            }
 		}
 
         function nearestQuarterHour($time)
@@ -426,15 +469,75 @@
 
         function _overlap($inTime,$outTime,$id = null)
         {
+            //Checks to see if the times entered fit within any other time entry for that user.
             $this->db = Staple_DB::get();
 
             $auth = Staple_Auth::get();
             $user = new userModel($auth->getAuthId());
             $userId = $user->getId();
 
-            $sql = "SELECT id FROM timeEntries WHERE '".$this->db->real_escape_string($inTime)."' >= inTime AND '".$this->db->real_escape_string($outTime)."' <= outTime AND id <> '".$this->db->real_escape_string($id)."' AND userId = '".$this->db->real_escape_string($userId)."'";
+            $dateString = strtotime(date("Y-m-d", $inTime));
+            $nextDateString = $dateString + 86400;
 
-            if($this->db->query($sql)->num_rows > 0)
+            //Find the earliest time for the given date.
+            $sql = "
+                SELECT inTime FROM timeEntries WHERE inTime > '".$this->db->real_escape_string($dateString)."' AND userId = '".$this->db->real_escape_string($userId)."' ORDER BY inTime ASC LIMIT 1
+            ";
+
+            $query = $this->db->query($sql);
+            $result = $query->fetch_assoc();
+            $firstInTime = $result['inTime'];
+
+            //Find the latest time for the given date.
+            $sql = "
+                SELECT outTime FROM timeEntries WHERE outTime > '".$this->db->real_escape_string($dateString)."' AND outTime < '".$this->db->real_escape_string($nextDateString)."' AND userId = '".$this->db->real_escape_string($userId)."' ORDER BY outTime DESC LIMIT 1
+            ";
+
+            $query = $this->db->query($sql);
+            $result = $query->fetch_assoc();
+            $lastOutTime = $result['outTime'];
+
+            if($id == null)
+            {
+                $sql = "SELECT inTime, outTime FROM timeEntries WHERE inTime > $dateString AND outTime < $nextDateString AND userId = $userId";
+            }
+            else
+            {
+                $sql = "SELECT inTime, outTime FROM timeEntries WHERE inTime > $dateString AND outTime < $nextDateString AND userId = $userId AND id <> $id";
+            }
+
+            $query = $this->db->query($sql);
+            $data = array();
+            while($result = $query->fetch_assoc())
+            {
+                $data[] = $result;
+            }
+
+            $overlap = 0;
+            foreach($data as $entry)
+            {
+                if($inTime == $entry['inTime'] && $outTime == $entry['outTime'])
+                {
+                    $overlap++;
+                }
+
+                if($inTime > $entry['inTime'] && $inTime < $entry['outTime'])
+                {
+                    $overlap++;
+                }
+
+                if($outTime > $entry['inTime'] && $outTime < $entry['outTime'])
+                {
+                    $overlap++;
+                }
+
+                if($inTime < $firstInTime && $outTime > $lastOutTime)
+                {
+                    $overlap++;
+                }
+            }
+
+            if($overlap > 0)
             {
                 return false;
             }

+ 5 - 3
application/models/timesheetModel.php

@@ -405,7 +405,7 @@
 			$this->startDate = $currentDate->modify('-1 month +25 day')->format('Y-m-d');
 			$this->startDateTimeString = strtotime($this->startDate);
 			$currentDate->setDate($year, $month, 1);
-			$this->endDate = $currentDate->modify('+24 day')->format('Y-m-d');
+			$this->endDate = $currentDate->modify('+25 day')->format('Y-m-d');
 			$this->endDateTimeString = strtotime($this->endDate);
 
 			//Previous Dates
@@ -429,7 +429,7 @@
 			$this->nextYear = $furtureDate->format('Y');
 
 			//Time Entries
-			$this->entries = $this->entries($this->startDate, $this->endDate);
+			$this->entries = $this->timeEntries($this->startDate, $this->endDate);
 
 			$timeCode = new codeModel();
 
@@ -455,7 +455,7 @@
 			}
 		}
 
-		function entries($startDate,$endDate)
+		function timeEntries($startDate,$endDate)
 		{
 			//Get user ID from Auth
 			$user = new userModel();
@@ -479,6 +479,7 @@
 			}
 		}
 
+		/* TODO depricate
 		function payPeriodCalculatedTotals($startDate, $endDate)
 		{
 			//Get user ID from Auth
@@ -498,6 +499,7 @@
 				return 0;
 			}
 		}
+		*/
 
 		function calculatedTotals($code,$startDate,$endDate)
 		{

+ 2 - 1
application/views/timesheet/index.phtml

@@ -5,7 +5,7 @@
             <h3><i class="fa fa-calendar"></i> <?php echo $this->timesheet->currentMonthText ?> <a href="#" data-reveal-id="yearForm"><?php echo $this->timesheet->currentYear ?></a></h3>
         </div>
         <div class="small-12 medium-8 text-right columns">
-            <ul class="button-group radius even-5 stack-for-small">
+            <ul class="button-group round even-5 stack-for-small">
                 <li><a class="button small secondary" href="<?php echo $this->link(array('timesheet',$this->timesheet->currentYear,$this->timesheet->previousMonth)) ?>"><i class="fa fa-caret-left"></i> Prev.</a></li>
                 <li><a class="button small secondary" href="<?php echo $this->link(array('timesheet',$this->timesheet->currentYear,$this->timesheet->nextMonth)) ?>">Next <i class="fa fa-caret-right"></i></a></li>
                 <li><a class="button small toggleTotals" href="#"><i class="fa fa-calculator"></i> Totals</a></li>
@@ -186,6 +186,7 @@
         $(".timeTitle").click(function() {
             $(this).parent().nextUntil(".marker").slideToggle("slow");
             $(this).find("i").toggleClass("fa-chevron-up fa-chevron-down")
+            return false;
         });
 
         $("#hideAll").click(function() {