Browse Source

Added printing to user timesheet. Found an error with the totals being reported.

Adam Day 9 years ago
parent
commit
a5887b8b4c

+ 46 - 0
application/controllers/timesheetController.php

@@ -6,6 +6,7 @@ class timesheetController extends Staple_Controller
 
     public function _start()
     {
+        $this->_setLayout('main');
         $auth = Staple_Auth::get();
         $user = new userModel();
         $user->userInfo($auth->getAuthId());
@@ -140,6 +141,51 @@ class timesheetController extends Staple_Controller
         $this->view->changeYearForm = $changeYearForm;
     }
 
+    public function printpreview($id = null, $year = null, $month = null)
+    {
+        $this->_setLayout('print');
+
+        //Set year and month variables if undefined.
+        if($year == null)
+        {
+            $date = new DateTime();
+            $year = $date->format('Y');
+        }
+
+        if($month == null)
+        {
+            $date = new DateTime();
+            if($date->format("j") >= 26)
+            {
+                $month = $date->modify('+1 month')->format('m');
+            }
+            else
+            {
+                $month = $date->format('m');
+            }
+        }
+
+        //Load timesheet for user.
+        $timesheet = new timesheetModel($year,$month);
+
+        $user = new userModel();
+        $user->userInfo($this->userId);
+
+        $this->view->firstName = $user->getFirstName();
+        $this->view->lastName = $user->getLastName();
+        $this->view->batchId = $user->getBatchId();
+
+        //Pass timesheet object to view
+        if($id == $this->userId)
+        {
+            $this->view->timesheet = $timesheet;
+        }
+        else
+        {
+            header("location: ".$this->_link(array('timesheet'))."");
+        }
+    }
+
     public function remove($id = null)
     {
         if($id != null)

+ 64 - 0
application/layouts/print.phtml

@@ -0,0 +1,64 @@
+<?php echo $this->doctype; ?>
+<!--[if IE 9]><html class="lt-ie10" lang="en" > <![endif]-->
+<html class="no-js" lang="en">
+<head>
+    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
+    <meta charset="utf-8">
+
+    <title><?php echo $this->title; ?></title>
+    <?php
+    $this->scripts();
+    $this->styles();
+    ?>
+    <style>
+        @page
+        {
+            size: auto;   /* auto is the initial value */
+
+            /* this affects the margin in the printer settings */
+            margin: 15mm 0mm 15mm 0mm;
+        }
+
+        body
+        {
+            /* this affects the margin on the content before sending to printer */
+            margin: 0px;
+
+        }
+
+        p
+        {
+            font-size:10pt !important;
+        }
+
+    </style>
+</head>
+<body onload="window.print()">
+<nav class="top-bar hide-for-print" data-topbar role="navigation">
+    <ul class="title-area">
+        <li class="name">
+            <h1><a href="#">Print Preview</a></h1>
+        </li>
+    </ul>
+
+    <section class="top-bar-section">
+        <!-- Right Nav Section -->
+        <ul class="right">
+            <li><a href="#" id="print"><i class="fa fa-print"></i> Print</a></li>
+            <li><a href="#" id="close"><i class="fa fa-close"></i> Close</a></li>
+        </ul>
+    </section>
+</nav>
+<?php $this->content();?>
+
+<script>
+    $("#print").click(function() {
+        window.print();
+    });
+
+    $("#close").click(function() {
+        window.close();
+    });
+</script>
+</body>
+</html>

+ 27 - 2
application/models/timesheetModel.php

@@ -20,6 +20,8 @@
 		private $previousMonthText;
 		private $previousYear;
 
+		private $userId;
+
 		private $batch;
 
 		private $entries;
@@ -282,6 +284,24 @@
 			$this->totals = $totals;
 		}
 
+		/**
+		 * @return mixed
+		 */
+		public function getUserId()
+		{
+			return $this->userId;
+		}
+
+		/**
+		 * @param mixed $userId
+		 */
+		public function setUserId($userId)
+		{
+			$this->userId = $userId;
+		}
+
+
+
 		function __construct($year, $month, $user = null)
 		{
 			$this->db = Staple_DB::get();
@@ -307,7 +327,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('+25 day')->format('Y-m-d');
+			$this->endDate = $currentDate->modify('+24 day')->format('Y-m-d');
 			$this->endDateTimeString = strtotime($this->endDate);
 
 			//Previous Dates
@@ -345,6 +365,7 @@
 			$totals['Total Time'] = array_sum($totals);
 
 			$this->setTotals($totals);
+			$this->userId = $user->getId();
 		}
 
 		function validate($batchId)
@@ -409,7 +430,11 @@
 			$user = new userModel();
 			$userId = $user->getId();
 
-			$sql = "SELECT ROUND((TIME_TO_SEC(SEC_TO_TIME(SUM(outTime - inTime)-SUM(lessTime*60)))/3600)*4)/4 AS 'totalTime' FROM timeEntries WHERE inTime > UNIX_TIMESTAMP('$startDate 00:00:00') AND outTime < UNIX_TIMESTAMP('$endDate 00:00:00') AND userId = $userId AND codeId = $code;";
+			//$sql = "SELECT ROUND((TIME_TO_SEC(SEC_TO_TIME(SUM(outTime - inTime)-SUM(lessTime*60)))/3600)*4)/4 AS 'totalTime' FROM timeEntries WHERE inTime > UNIX_TIMESTAMP('$startDate 00:00:00') AND outTime < UNIX_TIMESTAMP('$endDate 23:59:59') AND userId = $userId AND codeId = $code;";
+			$sql = "SELECT ROUND((SEC_TO_TIME(SUM(outTime - inTime)-SUM(lessTime*60))/3600)*4)/4 AS 'totalTime' FROM timeEntries WHERE inTime > UNIX_TIMESTAMP('$startDate 00:00:00') AND outTime < UNIX_TIMESTAMP('$endDate 23:59:59') AND userId = $userId AND codeId = $code;";
+
+			$sql = "SELECT SEC_TO_TIME(SUM(outTime - inTime)-SUM(lessTime*60)) as 'totalTime' FROM timeEntires WHERE inTime > UNIX_TIMESTAMP('$startDate 00:00:00') AND outTime < UNIX_TIMESTAMP('$endDate 23:59:59') AND userId = $userId AND codeId = $code;";
+			echo $sql."<br><br>";
 
 			if($this->db->query($sql)->num_rows > 0)
 			{

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

@@ -9,7 +9,7 @@
                 <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>
-                <li><a class="button small" href="#"><i class="fa fa-print"></i> Print</a></li>
+                <li><a class="button small" target="_blank" href="<?php echo $this->link(array('timesheet','printpreview',$this->timesheet->userId,$this->timesheet->currentYear,$this->timesheet->currentMonth)) ?>"><i class="fa fa-print"></i> Print</a></li>
                 <?php
                     if($this->needsValidation)
                     {

+ 127 - 0
application/views/timesheet/printpreview.phtml

@@ -0,0 +1,127 @@
+<div class="row">
+    <div class="small-12 columns text-center">
+        <h1><?php echo $this->firstName ?> <?php echo $this->lastName ?> <small><?php echo $this->timesheet->currentMonthText ?> <?php echo $this->timesheet->currentYear ?></small></h1>
+    </div>
+    <div class="small-12 columns text-center">
+        <p><b>Date Range:</b> <?php echo date("l, F jS Y",$this->timesheet->startDateTimeString) ?> to <?php echo date("l, F jS Y",$this->timesheet->endDateTimeString) ?></p>
+        <p><b>Report Generated:</b> <?php echo date('l, F jS Y g:i A') ?></p>
+    </div>
+</div>
+
+<div class="row">
+    <div class="small-12 columns">
+        <h3>Totals <small>Calculated with <i>Adjusted</i> time</small></h3>
+        <table width="100%">
+            <thead>
+            <tr>
+                <?php
+                foreach($this->timesheet->totals as $title => $value)
+                {
+                    if($value > 0)
+                    {
+                        echo "<th>$title</th>";
+                    }
+                }
+                ?>
+            </tr>
+            </thead>
+            <tbody>
+            <?php
+            foreach($this->timesheet->totals as $title => $value)
+            {
+                if($value > 0)
+                {
+                    echo "<td>$value</td>";
+                }
+            }
+            ?>
+            </tbody>
+        </table>
+    </div>
+</div>
+
+<div class="row">
+    <div class="small-12 columns">
+        <h3>Time Sheet</h3>
+        <p><i>Adjusted</i> time calculated to the nearest quarter hour. <br> Example: <b>9:07 AM</b> will be adjusted to <b>9:00 AM</b> and <b>10:08 AM</b> will be adjusted to <b>10:15 AM</b>.</p>
+        <table width="100%">
+            <thead>
+            <tr>
+                <th>Date</th>
+                <th>In Time <small>(Actual)</small></th>
+                <th>Out Time <small>(Actual)</small></th>
+                <th>In Time <small>(Adjusted)</small></th>
+                <th>Out Time <small>(Adjusted)</small></th>
+                <th>Less Time <small>(Minutes)</small></th>
+                <th>Code</th>
+                <th>Total <small>(Hours)</small></th>
+                <th>Validated</th>
+            </tr>
+            </thead>
+            <tbody>
+    </div>
+</div>
+
+        <?php
+
+            $currentBatch = $this->batchId;
+
+            foreach($this->timesheet->entries as $entry)
+            {
+                echo "
+                <tr>
+                    <td>".$entry->date."</td>
+                    <td>".$entry->inTime."</td>
+                    <td>".$entry->outTime."</td>
+                ";
+
+                if($entry->inTime != $entry->roundedInTime)
+                {
+                    echo "<td>".$entry->roundedInTime."</td>";
+                }
+                else
+                {
+                    echo "<td> N/A </td>";
+                }
+
+                if($entry->outTime != $entry->roundedOutTime)
+                {
+                    echo "<td>".$entry->roundedOutTime."</td>";
+                }
+                else
+                {
+                    echo "<td> N/A </td>";
+                }
+
+                echo "
+                    <td>".$entry->lessTime."</td>
+                    <td>".$entry->codeName."</td>
+                    <td>".$entry->timeWorked."</td>
+                ";
+
+                if($currentBatch != $entry->batchId)
+                {
+                    echo "<td>Yes <i class='fa fa-check'></i></td>";
+                }
+                else
+                {
+                    echo "<td>No <i class='fa fa-close'></i></td>";
+                }
+
+                echo "</tr>";
+
+                if(strlen($entry->note) > 0)
+                {
+                    echo "
+                    <tr>
+                        <td class='text-right'><i class='fa fa-edit'></i> Note:</td>
+                        <td colspan='7'>".$entry->note."</td>
+                    </tr>
+                    ";
+                }
+            }
+        ?>
+            </tbody>
+        </table>
+    </div>
+</div>