<?php
    include('../loginsession.php');
    error_reporting(E_ALL);
    $filename = __FILE__;  //---Nama file saat ini
    if (strlen(__FILE__) > 48) {$filename = substr(__FILE__, 48);}
    $save = mysqli_query($conn, "INSERT INTO novia_abc_user_logs (userid,filename) VALUES ('$userini_id','$filename')");
//-------------
    $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 BULANAN</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_bulanan.png" alt="Keuangan Harian"></div>

<?php

if(isset($_GET['tanggal'])){
$no = 0;
$total_stok= 0;
$no1 = 0;
$total_stok1= 0;
$sty = 1;
$tgl = $_GET['tanggal'];

$bulan = new DateTime($tgl);
$formated_dateyear = $bulan->format('Y-m');
$formated_date = $bulan->format('M Y'); // Tuesday

$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');
		if ( $currdate_ == $formated_dateyear){
			$no1++;
			$total_stok1 += $baris["biaya"];
		}
	}	
} else {}

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

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

?>
	<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-daily.php?tanggal=<?php echo $tgl;?>" title="Lihat Pasien Bulan ini"><button style="padding : 10px; width:100%">BELANJA HARIAN</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-monthly.php?tanggal=<?php echo $tgl1;?>" title="Lihat pasien bulan 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 "Pasien ".$formated_date; ?><a>
			</td>
			<td width="5%" style="padding-top : 10 px"; align="center";>
				<a href="cash-view-monthly.php?tanggal=<?php echo $tgl2;?>" title="Lihat pasien bulan 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 BULAN</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>No</th>
			<th>Tanggal</th>
			<th>Nama Pengeluaran</th>
			<th align=right>Harga</th>
			<th>Keterangan</th>
			
		</tr>
	
	

<?php 
$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');
        $jam = $dt->format('Y-m-d');
		
		if ( $currdate == $formated_dateyear){
			$no++;
			$y = 2;
			$result1 = fmod($no,$y);
			if ( $result1 == 0 ) $sty = "dua";
			if ( $result1 == 1 ) $sty = "satu";
			$total_stok += $row["harga"];
			//--------
			
				$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><?php echo $no; ?>.</td>
					<td align="left">
                        <a href="<?php 
                            echo 'cash-belanja-edit.php?id=' . $row['id'] . 
                             '&file=cash-view-monthly.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-monthly.php?tanggal=' . $tgl; 
                            ?>" 
                            title="Edit Belanja">
                            <?php echo $subname; ?>
                        </a>					    
				    </td>
					<td align="right"><a><?php echo $row["harga"]; ?></a></td>
					<td align="left"><a><?php echo $row["keterangan"]; ?></a></td>
					
					
				</tr>
				<?php
				}
			}
		} else {
			//echo "0 results";
	}
}
?>
		<tr class="head" align="left">
			<th colspan="4"></th>
			<th colspan="2"><?php echo $total_stok; ?></th>
		</tr>
		
		<tr class="satu">
			<td colspan=3 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:20px;"><?php echo "Total Pendapatan"; ?></a></td>
			<td colspan=3 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:20px;"><?php echo $total_stok1; ?></a></td>
		</tr>
		<tr class="satu">
			<td colspan=3 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:20px;"><?php echo "Total Pengeluaran"; ?></a></td>
			<td colspan=3 style="padding-top : 5 px;"  align="left";><a style = "font-family: 'Heebo' sans-serif; font-size:20px;"><?php echo $total_stok; ?></a></td>
		</tr>
		<?php $Saldo = $total_stok1 - $total_stok; ?>
		<tr class="head" align="left">
			<th colspan="3"><a style = "font-family: 'Heebo' sans-serif; font-size:20px;">SALDO</a></th>
			<th colspan="3"><a style = "font-family: 'Heebo' sans-serif; font-size:20px;"><?php echo $Saldo; ?></a></th>
		</tr>
		
		
</table>

</body>
</html>
