Bladeren bron

Weekly report not breaks down a users time worked for the numbered calendar weeks in a year.

Adam Day 9 jaren geleden
bovenliggende
commit
d7545072c9

+ 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;
+        }
+
+
 
     }
 }

+ 53 - 0
application/forms/weeklyReportForm.php

@@ -0,0 +1,53 @@
+<?php
+
+class weeklyReportForm extends Staple_Form
+{
+    public function _start()
+    {
+        //$this->setLayout('insertFormLayout');
+
+        $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()
+            ->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)
+    {
+        $accounts = new userModel();
+        $users = $accounts->listAll();
+        $data = array();
+        if($ids == null)
+        {
+            foreach($users as $user)
+            {
+                $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('+6 days')->setTime(23,59,59);
+        $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;
+    }
+}

+ 6 - 9
application/views/reports/index.phtml

@@ -1,12 +1,13 @@
 <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">
+                <li><a class="button small" href="<?php echo $this->link(array('reports','weekly')) ?>">Weekly Breakdown</a></li>
                 <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,10 +20,7 @@
             $i = 0;
             foreach($this->report as $user=>$timesheet)
             {
-                echo "
-                    <h3 id='user'.$i.'' class='timeTitle'>$user <i class='fa fa-chevron-up right'></i></h3>
-                ";
-
+                echo "<h3 id='user'.$i.'' class='timeTitle'>$user <i class='fa fa-chevron-up right'></i></h3>";
                 echo "
         <div class=\"wrapper\">";
 
@@ -95,10 +93,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\">

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

@@ -0,0 +1,65 @@
+<div class="section">
+    <div class="row">
+        <div class="small-12 columns">
+            <h2><i class="fa fa-file"></i> Weekly Breakdown Report</h2>
+        </div>
+    </div>
+    <div class="row">
+        <div class="small-12 columns">
+            <?php
+
+                echo $this->form;
+
+                echo "<h3>".$this->account['firstName']." ".$this->account['lastName']."</h3>";
+
+                if(count($this->report) > 0)
+                {
+                    echo "
+                    <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>