<?php
// Manage Data Kas ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='kastahunan'){
	if (isset($_POST[submit])){ $tahun = $_POST[tahun]; }else{ $tahun = $thn_sekarang; }
	echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>List atau Daftar Laporan Keuangan Tahun <b style='color:red'>$tahun</b> Pada Optik<hr></b></div>";
    			echo "<span style='float:right; margin-right:0px;'>
					<form method=POST action='kas-tahunan.html'>
							Periode : 
							<select name='tahun'>
									<option value='' selected>- Pilih Tahun -</option>";

									for ($x = 2012; $x <= $thn_sekarang; $x++) {
										if ($_POST[tahun]==$x){
										  echo "<option value=$x selected>$x</option>";
										}else{
										  echo "<option value=$x>$x</option>";
										}
									}
							echo "</select>
						<input type='submit' name='submit' class='submitt' value='Lihat'>
					</form>
				</span><div style='clear:both'></div><hr>";
				
echo "<table style='border: 2px solid #e3e3e3;'width=100%>
		<tr bgcolor='#2e2e2e'>
          <tr bgcolor='#2e2e2e'>
			<th width=40px>No</th>
			<th>Bulan</th>
			<th>Total Jual</th>
			<th>Total Modal</th>
			<th>Kas Keluar</th>
			<th>Laba</th>
		  </tr>";
    for ($x = 1; $x <= 12; $x++) {
	  if(($x % 2)==0){ $warna="#ffffff"; }else{ $warna="#E1E1E1"; }
	  	$lebar=strlen($x);
		switch($lebar){
		    case 1:{ $g="0".$x; break; }
		    case 2:{ $g=$x; break; }      
		} 

		if (isset($_POST[submit])){
			$periode = $_POST[tahun].'-'.$g;
			$tk = mysql_fetch_array(mysql_query("SELECT SUM(total) AS total_kas, SUM(operasional) AS total_operasional, SUM(non_operasional) AS total_non_operasional FROM rb_kas where tanggal LIKE '$periode%'"));
	 		$dd = mysql_fetch_array(mysql_query("SELECT sum(sisa) as sisa FROM `rb_orders` where sisa > 0 AND tanggal_pesan LIKE '$periode%'"));
	 		$gg = mysql_fetch_array(mysql_query("SELECT sum(sisa) as sisa FROM `rb_orders_grosir` where sisa > 0 AND tanggal_pesan LIKE '$periode%'"));

	 		$tke = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah_bayar) as jumlah_bayar FROM `rb_orders` a where a.tanggal_pesan LIKE '$periode%'"));
	 		$tkg = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah_bayar) as total FROM `rb_orders_grosir` a where a.tanggal_pesan LIKE '$periode%'"));
	 		$ss = mysql_fetch_array(mysql_query("SELECT sum(bayar_sisa) as bayar_sisa FROM rb_sisa where waktu LIKE '$periode%'"));
		
		}else{
			$periode = date("Y").'-'.$g;
			$tk = mysql_fetch_array(mysql_query("SELECT SUM(total) AS total_kas, SUM(operasional) AS total_operasional, SUM(non_operasional) AS total_non_operasional FROM rb_kas where tanggal LIKE '$periode%'"));
	 		$dd = mysql_fetch_array(mysql_query("SELECT sum(sisa) as sisa FROM `rb_orders` where sisa > 0 AND tanggal_pesan LIKE '$periode%'"));
	 		$gg = mysql_fetch_array(mysql_query("SELECT sum(sisa) as sisa FROM `rb_orders_grosir` where sisa > 0 AND tanggal_pesan LIKE '$periode%'"));

	 		$tke = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah_bayar) as jumlah_bayar FROM `rb_orders` a where a.tanggal_pesan LIKE '$periode%'"));
	 		$tkg = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah_bayar) as total FROM `rb_orders_grosir` a where a.tanggal_pesan LIKE '$periode%'"));
	 		$ss = mysql_fetch_array(mysql_query("SELECT sum(bayar_sisa) as bayar_sisa FROM rb_sisa where waktu LIKE '$periode%'"));

	 		$ec = mysql_fetch_array(mysql_query("SELECT sum(b.jumlah*c.harga_pokok) as total FROM `rb_orders` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders JOIN rb_produk c ON b.id_produk=c.id_produk where a.tanggal_pesan LIKE '$periode%'"));
        	$eg = mysql_fetch_array(mysql_query("SELECT sum(b.jumlah*c.harga_pokok) as total FROM `rb_orders_grosir` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders JOIN rb_produk c ON b.id_produk=c.id_produk where a.tanggal_pesan LIKE '$periode%'"));
        }

        $untung = (($tke[jumlah_bayar]-$dd[sisa])+($tkg[total]-$gg[sisa])+$ss[bayar_sisa]) - ($ec[total]+$eg[total]) - $tk[total_kas];
        echo "<tr bgcolor=$warna>
	             <td class='read_only'>$x</td>
			     <td>".$nama_bln[$x]."</td>
			     <td>Rp ".format_rupiah((($tke[jumlah_bayar]-$dd[sisa])+($tkg[total]-$gg[sisa])+$ss[bayar_sisa]))."</td>
				 <td>Rp ".format_rupiah($ec[total]+$eg[total])."</td>
				 <td>Rp ".format_rupiah($tk[total_kas])."</td>
				 <td>Rp ".format_rupiah($untung)."</td>
			 </tr>";
	}

		if (isset($_POST[submit])){
			$periode = $_POST[tahun];
			$tot = mysql_fetch_array(mysql_query("SELECT sum(c.harga-a.diskon) as total_harga_jual, sum(b.jumlah) as jumlah, sum((b.jumlah*c.harga)-b.diskon) as total_sub_total, 
														 sum(b.jumlah*c.harga_pokok) as total_modal, sum((b.jumlah*c.harga)-(b.jumlah*c.harga_pokok)) as total_keuntungan  
															FROM `rb_orders` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders JOIN rb_produk c ON b.id_produk=c.id_produk 
																 where a.tanggal_pesan LIKE '$periode%' ORDER BY a.id_orders"));
			$totg=mysql_fetch_array(mysql_query("SELECT sum(b.jumlah) as total_jumlah, sum(c.harga_grosir) as total_harga_grosir, sum(b.diskon/100*(b.jumlah*c.harga_grosir)) as total_diskon, sum((b.jumlah*c.harga_grosir)-(b.diskon/100*(b.jumlah*c.harga_grosir))) as total_sub, sum(b.jumlah*c.harga_pokok) as total_modal, sum(((b.jumlah*c.harga_grosir)-(b.diskon/100*(b.jumlah*c.harga_grosir)))-(b.jumlah*c.harga_pokok)) as keuntungan  
	    							 	FROM `rb_orders_grosir` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders 
	    							 		JOIN rb_produk c ON b.id_produk=c.id_produk where a.tanggal_pesan LIKE '$periode%' ORDER BY a.id_orders"));
			$totkk=mysql_fetch_array(mysql_query("SELECT sum(total) as total_kas FROM `rb_kas` 
													where tanggal LIKE '$periode%'"));
		}else{
			$periode = date("Y");
			$tk = mysql_fetch_array(mysql_query("SELECT SUM(total) AS total_kas, SUM(operasional) AS total_operasional, SUM(non_operasional) AS total_non_operasional FROM rb_kas where tanggal LIKE '$periode%'"));
	 		$dd = mysql_fetch_array(mysql_query("SELECT sum(sisa) as sisa FROM `rb_orders` where sisa > 0 AND tanggal_pesan LIKE '$periode%'"));
	 		$gg = mysql_fetch_array(mysql_query("SELECT sum(sisa) as sisa FROM `rb_orders_grosir` where sisa > 0 AND tanggal_pesan LIKE '$periode%'"));

	 		$tke = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah_bayar) as jumlah_bayar FROM `rb_orders` a where a.tanggal_pesan LIKE '$periode%'"));
	 		$tkg = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah_bayar) as total FROM `rb_orders_grosir` a where a.tanggal_pesan LIKE '$periode%'"));
	 		$ss = mysql_fetch_array(mysql_query("SELECT sum(bayar_sisa) as bayar_sisa FROM rb_sisa where waktu LIKE '$periode%'"));

	 		$ec = mysql_fetch_array(mysql_query("SELECT sum(b.jumlah*c.harga_pokok) as total FROM `rb_orders` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders JOIN rb_produk c ON b.id_produk=c.id_produk where a.tanggal_pesan LIKE '$periode%'"));
        	$eg = mysql_fetch_array(mysql_query("SELECT sum(b.jumlah*c.harga_pokok) as total FROM `rb_orders_grosir` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders JOIN rb_produk c ON b.id_produk=c.id_produk where a.tanggal_pesan LIKE '$periode%'"));
        }
        	$untung = (($tke[jumlah_bayar]-$dd[sisa])+($tkg[total]-$gg[sisa])+$ss[bayar_sisa]) - ($ec[total]+$eg[total]) - $tk[total_kas];
		echo "	<tr style='border:1px solid $warna;'>
				<td style='background:$warna; color:$warna'>X</td>
				<td style='color:red; font-weight:bold;'>Total</td>
				<td style='color:red; font-weight:bold;'>Rp ".format_rupiah(($tke[jumlah_bayar]-$dd[sisa])+($tkg[total]-$gg[sisa])+$ss[bayar_sisa])."</td>
				<td style='color:red; font-weight:bold;'>Rp ".format_rupiah($ec[total]+$eg[total])."</td>
				<td style='color:red; font-weight:bold;'>Rp ".format_rupiah($tk[total_kas])."</td>
				<td style='color:red; font-weight:bold;'>Rp ".format_rupiah($untung)."</td>
			</tr>";
echo "</table>";
}