<?php
    include('../loginsession.php');
    error_reporting(E_ALL);
    $filename = basename(__FILE__);
    $save = mysqli_query($conn, "INSERT INTO novia_abc_user_logs (userid,filename) VALUES ('$userini_id','$filename')");
//-------------
    if(isset($_GET['tanggal'])){
        $tgl = $_GET['tanggal'];
    }
    $today = date("Y-m-d");
//-------------
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//ID" "http://www.w3.org/TR/html4/frameset.dtd">
<html lang="id">
<head>
	<title>PENGELUARAN HARIAN</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="../css/style.css">
	<link href="../images/balance.ico" rel="icon" type="image/icon">
	<style>
		img {
            width: 100%;
            max-width: 500px;
            height: auto;
        }
    </style>
</head>

<body>

    <div><img src="../images/page/keuangan_harian.png" alt="Keuangan Harian"></div>

<?php

$no = 0;
$no1 = 0;
$total_stok1= 0;
$sty = 1;

$income_yearly= 0;
$tgl_yearly_new = new DateTime($tgl);
$formated_dateyear = $tgl_yearly_new->format('Y');

$income = "SELECT date_, biaya FROM novia_abc_visit ORDER BY date_ ASC";
$resultan = $conn->query($income);
if ($resultan->num_rows > 0) {
    while($baris = $resultan->fetch_assoc()) {
		$si = $baris["date_"];
        $dti = new DateTime($si);
        $currdate_ = $dti->format('Y-m-d');
		if ( $currdate_ == $tgl ){
			$no1++;
			$total_stok1 += $baris["biaya"];
		}
		
		$syi = $baris["date_"];
        $dtyi = new DateTime($syi);
        $curryear = $dtyi->format('Y');
		if ( $curryear == $formated_dateyear && $currdate_ <= $tgl ){
			$income_yearly += $baris["biaya"];
		}
	}	
} else {}

$dt1 = new DateTime($tgl);
$timeU1 = $dt1->format('U');
$viaMundur = $timeU1 - 86400;
$tgl1 = date("Y-m-d", substr($viaMundur, 0, 10));

$dt2 = new DateTime($tgl);
$timeU2 = $dt2->format('U');
$viaMaju = $timeU2 + 86400;
$tgl2 = date("Y-m-d", substr($viaMaju, 0, 10));

$bulan = new DateTime($tgl);
$formated_date = $bulan->format('d M Y'); // Tuesday
$Hari = $bulan->format('l');
$hari = $HARI[$Hari];

?>
	<table width="100%" cellpadding="5">
		<tr class="dua">
			<td width="50"><a href="../index.php" title="Kembali ke halaman awal"><button style="padding : 10px; width:100%">HOME</button></a></td>
			<td><a href="cash-main.php" title="Lihat Keuangan Hari ini"><button style="padding : 10px; width:100%; background-color:#8baca4;">TAMBAH BELANJA</button></a></td>
			<td width="150"><a href="cash-view-monthly.php?tanggal=<?php echo $tgl;?>" title="Lihat Pasien Bulan ini"><button style="padding : 10px; width:100%">BELANJA BULANAN</button></a></td>
		</tr>
		<tr class="dua">
    		<td colspan="2"><a href="cash-stat.php?tanggal=<?php echo $today;?>" title="Lihat Statistik Keuangan"><button style="padding : 10px; width:100%;">STATISTIK KEUANGAN</button></a></td>
    		<td colspan="1"><a href="../administrasi.php" title="halaman navigasi"><button style="padding : 10px; width:100%">NAVIGASI</button></td>
        </tr>
	</table>
	<table width="100%" cellpadding="5">
		<tr class="dua">
			<td width="5%" style="padding-top : 10 px"; align="center";>
				<a href="cash-view-daily.php?tanggal=<?php echo $tgl1;?> " title="Lihat pasien tanggal sebelumnya" style = "font-family: 'Heebo' sans-serif; font-size:25px;"><<a>

			</td>
			<td width="25%" style="padding-top : 5 px;" align="center";>
				<a style = "font-family: 'Heebo' sans-serif; font-size:25px;"><?php echo $hari." ".$formated_date; ?><a>
			</td>
			<td width="5%" style="padding-top : 10 px"; align="center";>
				<a href="cash-view-daily.php?tanggal=<?php echo $tgl2;?> " title="Lihat pasien tanggal berikutnya" style = "font-family: 'Heebo' sans-serif; font-size:25px;">><a>
			</td>
		</tr>
		<tr class="satu">
			<td colspan=3 style="padding-top : 5 px;"  align="center";><a style = "font-family: 'Heebo' sans-serif; font-size:20px;"><?php echo "Pasien ".$no1." : Income ".$total_stok1; ?></a></td>
		</tr>
		<tr class="satu">
			<td colspan=3  align="center";>
				<form method="get">
					<label>PILIH TANGGAL</label>
					<input type="date" name="tanggal">
					<input type="submit" value="GO">
				</form>
			</td>
		</tr>
	</table>


<table width="100%" cellpadding="5">
	<tr class="head" align="left">
		<th width="10">No</th>
		<th width="10">Jam</th>
		<th>Pengeluaran</th>
		<th width="10">$</th>
		<th>Keterangan</th>
		<th width="10">By</th>
	</tr>
	
<?php

$daily_outcome= 0;
$yearly_outcome= 0;
$sql = "SELECT * FROM novia_abc_cash_shopping ORDER BY date_ ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
		
		$s = $row["date_"];
        $dt = new DateTime($s);
        $currdate = $dt->format('Y-m-d');
        $jam = $dt->format('H:i');
		
		if ( $currdate == $tgl){
			$no++;
			$y = 2;
			$result1 = fmod($no,$y);
			if ( $result1 == 0 ) $sty = "dua";
			if ( $result1 == 1 ) $sty = "satu";
			$daily_outcome += $row["harga"];
			
			$username = $row["userid"];
			if(isset($USERNAME[$username])){
    			$Usernamer = $USERNAME[$username];
			}
			//--------
			
				$subid = $row["subcategory_id"];
				$sql_2 = mysqli_fetch_array(mysqli_query($conn, "SELECT id, subcategory_item FROM novia_abc_cash_category_sub where id='$subid'"));
				$subname = $sql_2['subcategory_item'];
			
?>
				
				<tr class=<?php echo $sty; ?>>
					<td width="10" align="right"><?php echo $no; ?>.</td>
					<td width="10" align="left">
                        <a href="<?php 
                            echo 'cash-belanja-edit.php?id=' . $row['id'] . 
                            '&file=cash-view-daily.php?tanggal=' . $tgl; 
                            ?>" 
                            title="Edit Belanja">
                            <?php echo $jam; ?>
                        </a>
				    </td>
					<td align="left">
                        <a href="<?php 
                            echo 'cash-belanja-edit.php?id=' . $row['id'] . 
                            '&file=cash-view-daily.php?tanggal=' . $tgl; 
                            ?>" 
                            title="Edit Belanja">
                            <?php echo $subname; ?>
                        </a>
				    </td>
					<td width="10" align="right"><a><?php echo $row["harga"]; ?></a></td>
					<td align="left"><a><?php echo $row["keterangan"]; ?></a></td>
					<td width="10" align="left"><a><?php echo $Usernamer; ?></a></td>
				</tr>
<?php
			}
			$syi = $row["date_"];
			$dtyi = new DateTime($syi);
			$curryear = $dtyi->format('Y');
			if ( $curryear == $formated_dateyear && $currdate <= $tgl ){
				$yearly_outcome += $row["harga"];
			}
				
				
			}
		} if ($no == 0) { 
?>
			<tr class="satu">
				<td colspan="6" align="center"><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo "Belum ada transaksi pada hari ini."; ?></a></td>
			</tr>
<?php
		}
?>
</table>
<table width="100%" cellpadding="5">
		<tr class="head" align="left">
			<th colspan="7">NERACA HARIAN</th>
			<th colspan="7">NERACA TAHUNAN</th>
		</tr>
		<tr class="satu">
			<td colspan=5 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo "Pengeluaran"; ?></a></td>
			<td colspan=2 style="padding-top : 5 px;"  align="right";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo $daily_outcome; ?></a></td>
			<td colspan=5 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo "Pengeluaran Tahunan"; ?></a></td>
			<td colspan=2 style="padding-top : 5 px;"  align="right";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo $yearly_outcome; ?></a></td>
		</tr>
		<tr class="satu">
			<td colspan=5 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo "Pendapatan"; ?></a></td>
			<td colspan=2 style="padding-top : 5 px;"  align="right";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo $total_stok1; ?></a></td>
			<td colspan=5 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo "Pendapatan Tahunan"; ?></a></td>
			<td colspan=2 style="padding-top : 5 px;"  align="right";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo $income_yearly; ?></a></td>
		</tr>
		<?php $yearly_saldo = $income_yearly - $yearly_outcome; ?>
		<?php $Saldo_harian = $total_stok1 - $daily_outcome; ?>
		<?php $Saldo_kemaren = $yearly_saldo - $Saldo_harian; ?>		
		<tr class="satu">
			<td colspan=5 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo "Saldo"; ?></a></td>
			<td colspan=2 style="padding-top : 5 px;"  align="right";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo $Saldo_harian; ?></a></td>
			<td colspan=5 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo "Saldo Kemaren"; ?></a></td>
			<td colspan=2 style="padding-top : 5 px;"  align="right";><a style = "font-family: 'Heebo' sans-serif; font-size:15px;"><?php echo $Saldo_kemaren; ?></a></td>
		</tr>
		
		<?php $saldo_bersih = $yearly_saldo + $Saldo_harian ?>
		<tr class="head" align="left">
			<th colspan="5"><a style = "font-family: 'Heebo' sans-serif; font-size:18px;">Saldo Bersih</a></th>
			<th colspan="11" align="right"><a style = "font-family: 'Heebo' sans-serif; font-size:18px;"><?php echo $yearly_saldo; ?></a></th>
		</tr>
</table>
</body>
</html>
