View difference between Paste ID: E8jiDv2u and eYetL7HK
SHOW: | | - or go back to the newest paste.
1
<?php 
2
//error_reporting(E_ALL); ini_set('display_errors', '1');
3
    if(! $logged_in ) {
4
5
        require_once('form-login.php');
6
    } else { ?>
7
8
        <div class="col-md-10 col-md-offset-2  col-sm-9 main">
9
          <h1 class="page-header">Timesheets</h1>
10
          
11
          <form action="https://portal.designloud.com/index.php?do=timesheets" method="POST" class="form-horizontal" role="form">
12
				<div class="form-group">
13
					<legend>View Timesheet</legend>
14
				</div>
15
				<div class="form-group">
16
					<?php if($level === '5') { ?>
17
					<label for="employee" class="col-sm-2 col-lg-1 control-label">Employee</label>
18
					<div class="col-sm-10 col-lg-11">
19
						<select name="employee" id="employee" class="form-control">
20
							<option value="" disabled selected>Select an Employee</option>
21
							<?php 
22
							$connect = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
23
							if (mysqli_connect_errno()) {
24
								echo "Failed to connect to Database: " . mysqli_connect_error();
25
							}
26
27
							$check = mysqli_query($connect, "SELECT id, firstname, lastname
28
								FROM members") or die('Error: ' . mysqli_error($connect));
29
30
							if(mysqli_num_rows($check)>0){
31
								while ($row = mysqli_fetch_array($check, MYSQL_ASSOC)) {
32
									$firstname = $row['firstname'];
33
									$lastname = $row['lastname'];
34
									$userID = $row['id'];
35
36
									echo '<option value="' . $userID . '">' . $firstname . ' ' . $lastname . '</option>';
37
38
								}
39
							} ?>
40
						</select>
41
					</div>
42
					<?php } 
43
					else {
44
						$user_id = $_SESSION['user_id'];
45
						echo '<input type="hidden" name="employee" id="employee" value="' . $user_id . '" />';
46
					} ?>
47
				</div>
48
				
49
				<div class="form-group col-sm-12 col-md-6 col-lg-6">
50
					<label for="from_date" class="col-sm-6 col-md-3 col-lg-3 control-label">From Date</label>
51
					<div class="col-sm-6 col-md-9 col-lg-9">
52
						<input type="text" name="from_date" id="from_date" class="form-control">
53
					</div>
54
				</div>
55
				<div class="form-group col-sm-12 col-md-6 col-lg-6">
56
					<label for="to_date" class="col-sm-6 col-md-3 col-lg-3 control-label">To Date</label>
57
					<div class="col-sm-6 col-md-9 col-lg-9">
58
						<input type="text" name="to_date" id="to_date" class="form-control">
59
					</div>
60
				</div>
61
62
				<div class="form-group col-sm-12 col-md-12 col-lg-12">
63
					<div class="col-sm-offset-10 col-sm-2 col-lg-offset-11 col-lg-1">
64
						<button type="submit" class="btn btn-primary">Submit</button>
65
					</div>
66
				</div>
67
		</form>
68
69
		<?php
70
		if (isset($_POST['employee']) && isset($_POST['to_date']) && isset($_POST['from_date']) ) {
71
72
			include_once 'includes/db_connect.php';
73
			include_once 'includes/psl-config.php';
74
75
				$employee = $_POST['employee'];
76
				$fromDate = $_POST['from_date'];
77
				$toDate = $_POST['to_date'];
78
79
80
				$connect = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
81
				if (mysqli_connect_errno()) {
82
					echo "Failed to connect to Database: " . mysqli_connect_error();
83
				}
84
85
				/**************************************************
86
					General Timesheet
87
				**************************************************/
88
				$timesheet = mysqli_query($connect, "SELECT DATE_FORMAT(punch, '%Y/%m/%d') AS punch_date, DATE_FORMAT(punch, '%H:%i:%s') AS punch_time, comment
89
					FROM timesheet WHERE user_id = '$employee' AND punch BETWEEN '$fromDate' AND '$toDate'") or die('Error: ' . mysqli_error($connect));
90
91-
						$iterations = mysqli_num_rows($timesheet) % 2 == 0 ? mysqli_num_rows($timesheet) : mysqli_num_rows($timesheet) - 1;
91+
92
93
					$output = '<table class="table table-striped">
94
							      <thead>
95
							        <tr>
96
							          <th>Date</th>
97
							          <th>Time</th>
98
							          <th>Status</th>
99
							        </tr>
100
							      </thead>
101
							      <tbody>';
102
103
					    $iterations = mysqli_num_rows($timesheet) % 2 == 0 ? mysqli_num_rows($timesheet) : mysqli_num_rows($timesheet) - 1;
104
    
105
    					$total = 0;
106
107
					while ($row = mysqli_fetch_array($timesheet, MYSQL_ASSOC)) {
108
109
						$date = $row['punch_date'];
110
						$time = $row['punch_time'];
111
						$status = $row['comment'];
112
113
						$output .= '<tr>
114
							          <td>' . $date . '</td>
115
							          <td ';
116
117
						$parts = explode(':', $row['punch_time']);
118
119
						$seconds = (int) $parts[0] * 3600 + (int) $parts[1] * 60 + (int) $parts[2];
120
121
						if($status === 'In'){ 
122
123
						   $output .= 'class="in-time"'; 
124
125
						   $total -= $iterations ? $seconds : 0; // Subtract from total when in and there is out coming
126
127
						} else if($status === 'Out') {
128
129
						   $output .= 'class="out-time"';
130
131
						   $total += $seconds; // Add  to total when out
132
						}
133
134
						$output .= '>' . $time . '</td>
135
							          <td>' . $status . '</td>
136
							        </tr>';
137
						$iterations--;
138
139
					} //end while
140
141
					$output .= '</tbody>
142
							    </table>';
143
144
					
145
					echo $output;
146
147
					$hours = floor($total / 3600);
148
149
					$minutes = floor(($total / 60) % 60);
150
151
					$seconds = $total % 60;
152
153
					echo "<strong>Total: </strong> $hours:$minutes:$seconds";
154
155
				} //end if mysqli_num_rows
156
157
			} // end if isset
158
			?>
159
			
160
        </div> <!-- /.main -->
161
    <?php    
162
        } 
163
?>