<?php
include "koneksi.php";
if (!isset($_SESSION["UserID"])) {
	header("location:login.php?p=3");
}


?>
<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="style.css">
<html>

<head>
	<title> PT. MULTI HIDRACHROME BALIKPAPAN </title>
	<link rel="icon" type="image/png" href="images/mhb.jpeg">
			<?php
		include "part/menu2.php";
		?>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<?php
	include "part/jss.php";
	?>

</head>

<body>

	<div style="width:1280px; height:220px" class="container">


		<div class="alert alert-info" style='background-color:#F8F8FF' href="#">
			<div style="display: block; margin-left: auto; margin-right: auto; width: 1000px;  height:100px;"><img style="width: 1000px;  height:180px;" src="images/hd.jpg"></div>
			<div>
				<center>
					<h1>PT. MULTI HIDRACHROM BALIKPAPAN</h1>

				</center>
			</div>
			<div>
				<center>
					<h4>PROFESIONAL INFORMATION SYSTEM</h4>
				</center>
			</div>
		</div>
		<div class="alert alert-info" align='center'>
			<h3>Data Keterlambatan Karyawan</h3>
			<?php
			echo date('yy-m-d');
			?>
			<?php
			if (isset($_GET["p"])) {
				$p = $_GET["p"];
				if ($p == "1") {
					?>
					<div class="alert alert-primary">
						Data Karyawan berhasil ditambah!
					</div>
				<?php
					} else if ($p == "2") {
						?>
					<div class="alert alert-primary">
						Data Karyawan berhasil diupdate!
					</div>
				<?php
					} else if ($p == "3") {
						?>
					<div class="alert alert-primary">
						Data Karyawan berhasil dipecat!
					</div>
			<?php
				}
			}
			?>
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>
							NO 1
						</th>
						<th>
							NIK 2
						</th>
						<th>
							NAMA 3
						</th>
						<th>
							DEPARTEMEN 4
						</th>
						<th>
							TANGGAL/JAM 5
						</th>
						<th>
							TOTAL TERLAMBAT/MENIT 6
						</th>
						<th>
							TOTAL HARI TERLAMBAT 7
						</th>
						<th>
							POTONGAN/MENIT 8
						</th>
						<th>
							TOTAL POTONGAN 9
						</th>

					</tr>
				</thead>
				<tbody>
					<?php
					//$tanggalhariini = date('2020-08-28');
					//$q = "SELECT pin, TIME(scan_date) AS jam_masuk, scan_date AS scan FROM att_log WHERE DATE(scan_date) = '$tanggalhariini'";
					$q = "SELECT pin, TIME(scan_date) AS jam_masuk, scan_date AS scan FROM att_log WHERE MONTH(scan_date) = 8 AND YEAR(scan_date) =2020"; //ini buat 1
					$res = mysqli_query($conn2, $q);
					$z = 1;
					$ini = '0';
					
					//////////////buat nyimpan gaji total setelah kurang dengan keterlambatan\\\\\\\\\\\\\\\\\\
					$gaji_didapat = $totalgaji;
					$potongan = $jams;
					
					while ($row = mysqli_fetch_array($res)) {
						?>
						<tr>
							<td>1
								<?php
									echo $z;
									$z = $z + 1;
									?>
							</td>
							<td>2
								<?php
									echo $row["pin"];
									?>
							</td>
							<td>3
								<?php
									$s = "SELECT * FROM data_karyawan WHERE nik=" . $row['pin'];
									//echo "pin karyawan" . $s;
									$resi = mysqli_query($conn, $s);
									$rowi = mysqli_fetch_array($resi);
									echo $rowi["nama_karyawan"];
									$namas = $rowi["nama_karyawan"];
									?>
							</td>
							<td>4
								<?php
									$s = "SELECT * FROM data_karyawan WHERE nik=" . $row['pin'];
									$resi = mysqli_query($conn, $s);
									$rowi = mysqli_fetch_array($resi);
									echo $rowi["departement"];
									$departemens = $rowi["departement"];
									?>

							</td>
							<td>5
								<?php
									//echo $row["jam_masuk"];
									?>
									<?php
									$s = "SELECT * FROM att_log WHERE pin=" .$row['pin'];
									$resi = mysqli_query($conn2,$s);
									$rowi = mysqli_fetch_array($resi);
									
									//$d = "SELECT * FROM "
									//echo $rowi["scan_date"];
									echo "<a href='detailterlambat.php?pin=". $rowi['pin'] ."' class='btn btn-primary' value= " . $rowi['pin'] . " >Detail Terlambat</a>"
									
								?>
							</td>
							<td>6
							<?php
									
							 $jammasuk=$row["jam_masuk"];
							 echo $jammasuk; //BIANG KEROK
							 if($jammasuk>='07:31:00')
							 {
								 ////////////////////////////untuk input data terlambat
								 if (isset($_REQUEST['submit']))
                                 {
                                       $p = "INSERT INTO data_terlambat (nik, nama, departemen, tanggal, total_jam_terlambat)
		                               VALUES 
			                           ('$pins','$namas','$departemens','$tanggals','$ini')";
                                       $insertQuery=mysqli_query($insert);
                                  }
					
								$jamterlambat = "07:31:00";
								$keterlambatan = $jammasuk - $jamterlambat;
								//////////echo $keterlambatan ." jam" ."</br>";
								$pins = $row['pin'];
								$qmasuk= "SELECT TIME(scan_date) as waktu FROM att_log WHERE pin='$pins' AND MONTH(scan_date) = 8 AND YEAR(scan_date) =2020 AND TIME(scan_date) <='16:30:00' AND TIME(scan_date) >='07:31:01'";
								//echo "qjumlah ". $qmasuk;
								$ressjmlh = mysqli_query($conn2, $qmasuk);
								while($rowjml=mysqli_fetch_assoc($ressjmlh))
								{
									//////////echo "jml : ". $rowjml['waktu'] . "</br>";
									$waktu1=strtotime($rowjml['waktu']);
									$waktu2=strtotime("07:31:00");
									//////////echo "waktu1 : " . $rowjml['waktu'] . "</br>";
									/////////echo "waktu2 : " ."07:31:00". "</br>";
									//$total = $rowjml['waktu'] - "07:32:53";
									
									 //menghitung selisih dengan hasil detik
									$diff    =$waktu1 - $waktu2;
									
									//membagi detik menjadi jam
									$jam    =floor($diff / (60 * 60));
									
									//membagi sisa detik setelah dikurangi $jam menjadi menit
									$menit    =$diff - $jam * (60 * 60);

									//menampilkan / print hasil
									//echo 'Hasilnya adalah '.number_format($diff,0,",",".").' detik<br /><br />';
									//////////////////echo 'waktu terlambat : ' . $jam .  ' jam dan ' . floor( $menit / 60 ) . ' menit' . "</br>";
									$jumlah = strtotime($jam. ":" .floor( $menit / 60 ).":00");
									
									$coba = $jam .floor( $menit / 60 );
									//////////////echo "iiiiiiiini jam dan menit jadi satu ".$coba;			
									
									$ini += $coba;
									/////////////echo "/////".$ini;
									
									//////////////////////////////// coba penghitungan gaji\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

									$gaji = "SELECT * FROM data_karyawan WHERE nik =" .$pins;
									$ress=mysqli_query($conn,$gaji);
									while ($rows=mysqli_fetch_array($ress))
						                
									{
										  $gaji_pokok = $rows['gaji'];
										$gaji_tunjangan_jabatan = $rows['tunjangan_jabatan'];
										$gaji_tunjangan_operasional = $rows['tunjangan_operasional'];
										$gaji_tunjangan_komunikatif = $rows['tunjangan_komunikatif'];
										$gaji_tunjangan_makan = $rows['tunjangan_makan'];
										$gaji_tunjangan_transport = $rows['tunjangan_transport'];
										$gaji_tunjangan_total_upah = $rows['tunjangan_total_upah'];
										$bagi = $gaji_tunjangan_total_upah / 10380;
										$totalgaji = $bagi * $potongan;
									}

									$gaji = '5000000';
									$pembaian = $gaji / '10380';
									$hasilgaji = $pembaian * $ini;
									$totalgaji = $gaji - $hasilgaji;
									////////echo "%%%%%%%%%%gaji ku :". $totalgaji;		
											
										
																		
																		
									//echo "sementara  : " . $total . "</br>";
								}
								$jumlah += $jumlah;
								///////echo "jumlah selisih  : " . date('H:i:s',$jumlah);	
								//////echo  "</br>";
								$time1 = strtotime('07:31:00');
								$time2 = strtotime($jammasuk);
								$subTime = $time1 - $time2;
								$h = ($subTime/(60*60))%24;
								$m = ($subTime/60)%60;
								$s = ($subTime/60*60)%60;
								/////echo $m." minit\n";
								/////echo $s." detik\n";
								$jams = $keterlambatan.$m .$s;
								
								$total = $jams;
								echo "<br> total : ".$total;
								
							
								
							 }
							 else
							 {
								
							 	echo "Tidak terlambat";
							 }
									
									?>
							</td>
							<td>7
								<?php
									$pin = $row["pin"];
									//echo "pinnya : " . $row["pin"] . "</br>";
									$d = '08';
									$s = '2020';
									$qtlt = "SELECT pin, TIME(scan_date) AS jam_masuk, scan_date AS scan, COUNT(pin) AS jumlahTerlambat FROM att_log 
									WHERE MONTH(scan_date) =". $d. " AND YEAR(scan_date) =". $s . "  AND pin=$pin AND TIME(scan_date) >'07:31:00' AND TIME(scan_date) <'16:30:00' 
									AND MONTH(scan_date) =08 AND YEAR(scan_date) =2020 ";

									//echo "query lagi: ".$qtlt;
									$restlt = mysqli_query($conn2, $qtlt);
									$rowtlt = mysqli_fetch_array($restlt);
									echo $rowtlt["jumlahTerlambat"];
								?>
							</td>
							<td>
								Gaji : 10.380 * 59
							</td>
							<td>9
								<?php
								    echo $gaji_didapat;
								?>
							</td>
						</tr>
					<?php
					}
					?>
				</tbody>
			</table>
		</div>
		<script>
			$(document).ready(function() {
				$('.table-bordered').DataTable();
			});
		</script>
		<center>
			<h5 class="form-signin">Copyright &copy; <a href="#" data-toggle="modal" data-target="#contact">SatrioPringgondani</a></h5>
		</center>
</body>

</html>