Ver código fonte

Merge pull request #21 from advation/weeklyReport

Weekly report
Adam 9 anos atrás
pai
commit
ce4ba13e2d

+ 32 - 0
application/controllers/reportsController.php

@@ -28,6 +28,38 @@ class reportsController extends Staple_Controller
 
         $report = new reportModel($year, $month);
         $this->view->report = $report->getTimesheets();
+    }
+
+    public function weekly()
+    {
+        //Weekly report form
+        $form = new weeklyReportForm();
+
+        if($form->wasSubmitted())
+        {
+            $form->addData($_POST);
+            if($form->validate())
+            {
+                $data = $form->exportFormData();
+                $report = new weeklyReportModel();
+                $this->view->report = $report->timeWorked($data['account'],$data['year']);
+
+                $account = new userModel();
+                $this->view->account = $account->userInfo($data['account']);
+
+                $this->view->year = $data['year'];
+            }
+            else
+            {
+                $this->view->form = $form;
+            }
+        }
+        else
+        {
+            $this->view->form = $form;
+        }
+
+
 
     }
 }

+ 31 - 47
application/controllers/timesheetController.php

@@ -28,65 +28,42 @@ class timesheetController extends Staple_Controller
                 $data = $form->exportFormData();
 
                 //Check if dates are within the current pay period.
-                $startMonth = date('m',strtotime('last month'));
+                $date = new DateTime();
 
-                if($startMonth == 1)
+                if($date->format('d') > 25)
                 {
-                    $startYear = date('Y',strtotime('last year'));
+                    $date->modify('+1 month');
                 }
-                else
-                {
-                    $startYear = date('Y');
-                }
-
-                $endMonth = date('m');
-                $endYear = date('Y');
-
-                $startDate= strtotime($startMonth.'/26/'.$startYear);
-                $endDate = strtotime($endMonth.'/25/'.$endYear);
-
+                $maxDate = $date->setDate($date->format('Y'),$date->format('m'),25)->setTime(23,59,59)->getTimestamp();
+                $minDate = $date->modify('-1 month +1 day')->setTime(0,0,0)->getTimestamp();
                 $userDate = strtotime($data['date']);
 
                 //Date is within pay period
-                if($userDate >= $startDate && $userDate <= $endDate)
+                if($userDate >= $minDate && $userDate <= $maxDate)
                 {
-                    //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']);
+                    $entry->setInTime($data['inTime']);
+                    $entry->setOutTime($data['outTime']);
+                    $entry->setLessTime($data['lessTime']);
+                    $entry->setCodeId($data['code']);
+
+                    //Save entry data to table.
+                    if($entry->save())
                     {
-                    */
-                        //Create a new entry object and set properties
-                        $entry = new timeEntryModel();
-                        $entry->setDate($data['date']);
-                        $entry->setInTime($data['inTime']);
-                        $entry->setOutTime($data['outTime']);
-                        $entry->setLessTime($data['lessTime']);
-                        $entry->setCodeId($data['code']);
-
-                        //Save entry data to table.
-                        if($entry->save())
-                        {
-                            //Return a new time form with success message
-                            $form = new insertTimeForm();
-                            $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
-                            $this->view->insertTimeForm = $form;
-                        }
-                        else
-                        {
-                            //Return the same form with a warning message
-                            $message = "<i class=\"fa fa-warning\"></i> Cannot insert overlapping time entries. Please add a new entry or edit an already existing one.";
-                            $form->errorMessage = array($message);
-                            $this->view->insertTimeForm = $form;
-                        }
-                    /*
+                        //Return a new time form with success message
+                        $form = new insertTimeForm();
+                        $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
+                        $this->view->insertTimeForm = $form;
                     }
                     else
                     {
-                        //Return the same form with error message.
-                        $form->errorMessage = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
+                        //Return the same form with a warning message
+                        $message = "<i class=\"fa fa-warning\"></i> Cannot insert overlapping time entries. Please add a new entry or edit an already existing one.";
+                        $form->errorMessage = array($message);
                         $this->view->insertTimeForm = $form;
                     }
-                    */
                 }
                 else
                 {
@@ -117,7 +94,14 @@ class timesheetController extends Staple_Controller
         if($month == null)
         {
             $date = new DateTime();
-            $month = $date->format('m');
+            if($date->format("j") >= 26)
+            {
+                $month = $date->modify('+1 month')->format('m');
+            }
+            else
+            {
+                $month = $date->format('m');
+            }
         }
 
         //Load timesheet for user.

+ 13 - 2
application/forms/layouts/insertFormLayout.phtml

@@ -69,11 +69,22 @@
 </div>
 
 <script>
-    <?php $timesheet = new timesheetModel(date('Y'),date('m')) ?>
+    <?php
+    $date = new DateTime();
+
+    if($date->format('d') > 25)
+    {
+        $date->modify('+1 month');
+    }
+    $maxDate = $date->setDate($date->format('Y'),$date->format('m'),25)->format('m/d/Y');
+    $minDate = $date->modify('-1 month +1 day');
+    $minDate = $date->format('m/d/Y');
+    ?>
+
     $(document).ready(function() {
 
         $(function() {
-            $( "#date" ).datepicker({numberOfMonths:2, minDate: "<?php echo date('m',strtotime('last month'))."/26/".date('Y') ?>", maxDate: "<?php echo date('m',strtotime('m'))."/25/".date('Y') ?>" });
+            $( "#date" ).datepicker({numberOfMonths:2, minDate: "<?php echo $minDate ?>", maxDate: "<?php echo $maxDate ?>" });
         });
 
         $('#entryToggle').click(function()

+ 13 - 0
application/forms/layouts/weeklyReportFormLayout.phtml

@@ -0,0 +1,13 @@
+<?php echo $this->formstart(); ?>
+    <div class="row">
+        <div class="small-6 columns">
+            <?php echo $this->fields['account'] ?>
+        </div>
+        <div class="small-6 columns">
+            <?php echo $this->fields['year'] ?>
+        </div>
+        <div class="small-12 columns">
+            <?php echo $this->fields['submit'] ?>
+        </div>
+    </div>
+<?php echo $this->formend(); ?>

+ 65 - 0
application/forms/weeklyReportForm.php

@@ -0,0 +1,65 @@
+<?php
+
+class weeklyReportForm extends Staple_Form
+{
+    public function _start()
+    {
+        $this->setLayout('weeklyReportFormLayout');
+
+        $this->setName('weeklyReportForm')
+            ->setAction($this->link(array('reports','weekly')));
+
+        $account = new Staple_Form_FoundationSelectElement('account','Account');
+        $account->setRequired()
+            ->addOption('','Select an account')
+            ->addOptionsArray($this->accounts())
+            ->addValidator(new Staple_Form_Validate_InArray($this->accounts(1)));
+
+        $year = new Staple_Form_FoundationTextElement('year','Year');
+        $year->setRequired()
+            ->setValue(date('Y'))
+            ->addValidator(new Staple_Form_Validate_Length(4,4))
+            ->addValidator(new Staple_Form_Validate_Numeric());
+
+        $submit = new Staple_Form_FoundationSubmitElement('submit','Submit');
+        $submit->addClass('button expand radius');
+
+        $this->addField($account, $year, $submit);
+    }
+
+    public function accounts($ids = null)
+    {
+        $user = new userModel();
+        $id = $user->getId();
+        $authLevel = $user->getAuthLevel();
+
+        $accounts = new userModel();
+        $users = $accounts->listAll();
+        $data = array();
+        if($ids == null)
+        {
+            foreach($users as $user)
+            {
+                if($user['supervisorId'] == $id)
+                {
+                    $data[$user['id']] = $user['lastName'].", ".$user['firstName'];
+                }
+                elseif($authLevel >= 900)
+                {
+                    $data[$user['id']] = $user['lastName'].", ".$user['firstName'];
+                }
+            }
+        }
+        else
+        {
+            foreach($users as $user)
+            {
+                $data[] = $user['id'];
+            }
+        }
+
+        return $data;
+    }
+}
+
+?>

+ 52 - 0
application/models/calculateModel.php

@@ -0,0 +1,52 @@
+<?php
+class calculateModel extends Staple_Model
+{
+    private $db;
+
+    function __construct()
+    {
+        $this->db = Staple_DB::get();
+    }
+
+    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 calculatedTotals($uid,$startDate,$endDate)
+    {
+        $sql = "SELECT ROUND((TIME_TO_SEC(SEC_TO_TIME(SUM(outTime - inTime)-SUM(lessTime*60)))/3600)*4)/4 AS 'totalTime' FROM timeEntries WHERE inTime >= $startDate AND outTime <= $endDate AND userId = $uid";
+
+        if($this->db->query($sql)->num_rows > 0)
+        {
+            $query = $this->db->query($sql);
+            $result = $query->fetch_assoc();
+            return round($result['totalTime'],2);
+        }
+        else
+        {
+            return 0;
+        }
+    }
+}

+ 5 - 0
application/models/reportModel.php

@@ -207,4 +207,9 @@ class reportModel extends Staple_Model
             return 0;
         }
     }
+
+    function weekly()
+    {
+
+    }
 }

+ 65 - 0
application/models/weeklyReportModel.php

@@ -0,0 +1,65 @@
+<?php
+class weeklyReportModel extends Staple_Model
+{
+    private $db;
+
+    function __construct()
+    {
+        $this->db = Staple_DB::get();
+    }
+
+    function timeWorked($uid,$year)
+    {
+        //Get an array of weeks
+        $weeks = array();
+        for($i=1;$i<53;$i++)
+        {
+            $weeks[$i] = $this->getStartAndEndDate($i, $year);
+            $sql = "
+              SELECT ROUND((TIME_TO_SEC(SEC_TO_TIME(SUM(outTime - inTime)-SUM(lessTime*60)))/3600)*4)/4 AS 'totalTime' FROM timeEntries WHERE inTime >= ".$weeks[$i]['start']['unix']." AND outTime <= ".$weeks[$i]['end']['unix']." AND userId = $uid;
+            ";
+
+            $total = 0;
+            if($this->db->query($sql)->num_rows > 0)
+            {
+                $query = $this->db->query($sql);
+                $result = $query->fetch_assoc();
+                $total = $result['totalTime'];
+            }
+
+            $weeks[$i]['hoursWorked'] = $total;
+        }
+
+        return $weeks;
+    }
+
+    function getStartAndEndDate($week, $year)
+    {
+        $dto = new DateTime();
+        $dto->setISODate($year, $week,0);
+
+        $ret = array();
+        $ret['week'] = $week;
+        $ret['year'] = $year;
+
+        //Week Start
+        $dto->setTime(0,0,0);
+        $ret['start']['unix'] = $dto->format('U');
+        $ret['start']['formatted'] = $dto->format('Y-m-d h:i:s');
+        $ret['start']['dayName'] = $dto->format('l');
+        $ret['start']['day'] = $dto->format('jS');
+        $ret['start']['month'] = $dto->format('F');
+        $ret['start']['year'] = $dto->format('Y');
+
+        //Week End
+        $dto->modify('+5 days')->setTime(24,0,0);
+        $ret['end']['unix'] = $dto->format('U');
+        $ret['end']['formatted'] = $dto->format('Y-m-d h:i:s');
+        $ret['end']['dayName'] = $dto->format('l');
+        $ret['end']['day'] = $dto->format('jS');
+        $ret['end']['month'] = $dto->format('F');
+        $ret['end']['year'] = $dto->format('Y');
+
+        return $ret;
+    }
+}

+ 18 - 17
application/views/reports/index.phtml

@@ -1,12 +1,17 @@
 <div class="section">
     <div class="row">
-        <div class="small-12 columns text-center">
+        <div class="small-12 columns">
             <h1><i class="fa fa-file"></i> Reports</h1>
         </div>
     </div>
     <div class="row">
-        <div class="small-12 columns">
-            <ul class="button-group round right">
+        <div class="small-6 columns">
+            <ul class="button-group radius left">
+                <li><a class="button small" href="<?php echo $this->link(array('reports','weekly')) ?>">Week Report</a></li>
+            </ul>
+        </div>
+        <div class="small-6 columns">
+            <ul class="button-group radius right">
                 <li><a id="showAll" class="button small secondary" href="#"><i class="fa fa-eye"></i> Show All</a></li>
                 <li><a id="hideAll" class="button small secondary" href="#"><i class="fa fa-eye-slash"></i> Hide All</a></li>
             </ul>
@@ -19,12 +24,9 @@
             $i = 0;
             foreach($this->report as $user=>$timesheet)
             {
+                echo "<h3 id='user'.$i.'' class='timeTitle'>$user <i class='fa fa-chevron-down right'></i></h3>";
                 echo "
-                    <h3 id='user'.$i.'' class='timeTitle'>$user <i class='fa fa-chevron-up right'></i></h3>
-                ";
-
-                echo "
-        <div class=\"wrapper\">";
+        <div class=\"wrapper hide\">";
 
                 if(count($timesheet) > 0)
                 {
@@ -33,11 +35,11 @@
                         <thead>
                         <tr>
                             <th>Date</th>
-                            <th>In Time</th>
-                            <th>Out Time</th>
-                            <th>Less Worked</th>
-                            <th>Total Worked</th>
-                            <th>Time Code</th>
+                            <th>In</th>
+                            <th>Out</th>
+                            <th>Less Time</th>
+                            <th>Hours</th>
+                            <th>Code</th>
                             <th>Validated</th>
                         </tr>
                         </thead>
@@ -95,10 +97,9 @@
 
                 }
 
-                if(count($timesheet) > 0) {
-                    echo "</table>";
-
-
+                if(count($timesheet) > 0)
+                {
+                echo "</table>";
                 echo "<div class=\"row\">";
                 echo "<div class=\"small-6 medium-4 large-3 columns\">";
                                         echo "<div class=\"card successBg\">

+ 72 - 0
application/views/reports/weekly.phtml

@@ -0,0 +1,72 @@
+<div class="section">
+    <div class="row">
+        <div class="small-12 columns">
+            <h2><i class="fa fa-file"></i> Week Report</h2>
+        </div>
+    </div>
+    <div class="row">
+        <div class="small-6 push-3 columns">
+            <?php echo $this->form; ?>
+        </div>
+        <div class="small-12 columns">
+            <?php
+                if(count($this->report) > 0)
+                {
+                    echo "
+                        <div class='row'>
+                            <div class='small-6 columns'>
+                                <h3>".$this->account['firstName']." ".$this->account['lastName']."</h3>
+                            </div>
+                            <div class='small-6 columns'>
+                                <a class='button secondary radius right' href='".$this->link(array('reports','weekly'))."'>Back</a>
+                            </div>
+                        </div>
+
+                    <table width=\"100%\">
+                        <thead>
+                        <tr>
+                            <th width='50%'>Week</th>
+                            <th width='50%'>Hours Worked</th>
+                        </tr>
+                        </thead>
+                        <tbody>
+                    ";
+                    $i = 0;
+                    foreach($this->report as $entry)
+                    {
+                        if($entry['hoursWorked'] !== null)
+                        {
+                            echo "
+                            <tr>
+                                <td>
+                                    <b>Week:</b> ".$entry['week']." <br>
+                                    <b>Start:</b> ".$entry['start']['dayName'].", ".$entry['start']['month']." ".$entry['start']['day']." ".$entry['start']['year']." <br>
+                                    <b>End:</b> ".$entry['end']['dayName'].", ".$entry['end']['month']." ".$entry['end']['day']." ".$entry['end']['year']."
+                                </td>
+                                <td>".$entry['hoursWorked']."</td>
+                            </tr>
+                            ";
+                            $i++;
+                        }
+                    }
+
+                    if($i == 0)
+                    {
+                        echo "
+                            <tr>
+                                <td colspan='2'>
+                                    <div class='text-center'>User hasn't submitted any time entries for ".$this->year.".</div>
+                                </td>
+                            </tr>
+                        ";
+                    }
+
+                    echo "
+                        </tbody>
+                    </table>
+                    ";
+                }
+            ?>
+        </div>
+    </div>
+</div>