<?php
// Manage Data Kas ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='datakas'){
	echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>List atau Daftar Laporan Kas Keluar Pada Optik<hr></b></div>";
    echo "<input class='linkkk' type=button value='Report Kas' onclick=\"window.location.href='tambah-kas.html';\">
				<span style='float:right; margin-right:0px;'>
					<form style='margin-right:10px' method=POST action='kas-bulanan.html'>
							Periode : <select name='bulan'>
									<option value=0 selected>- Pilih Bulan -</option>";
									$tampil=mysql_query("SELECT substring(tanggal,6,2) as bulan FROM rb_kas GROUP BY substring(tanggal,6,2)");
									while($r=mysql_fetch_array($tampil)){
									$bulan = Bulan($r[bulan]);
									  echo "<option value=$r[bulan]>$bulan</option>";
									}	
							echo "</select>
							<select name='tahun'>
									<option value=0 selected>- Pilih Tahun -</option>";
									$tampil=mysql_query("SELECT substring(tanggal,1,4) as tahun FROM rb_kas GROUP BY substring(tanggal,1,4)");
									while($r=mysql_fetch_array($tampil)){
									  echo "<option value=$r[tahun]>$r[tahun]</option>";
									}
							echo "</select>
						<input type='submit' class='submitt' value='Lihat'>
					</form>
				</span><br><br>

				<div style='float:right;'>
					<form style='margin-top:-5px; margin-right:10px' method=POST action=''>
							Tanggal : <select name='tgl'>
								<option value='".date("d")."' selected>".date("d")."</option>";
                                for($n=1; $n<=31; $n++){
                                	$lebar=strlen($n);
								    switch($lebar){ 
								    	case 1: { $g="0".$n; break; }
									    case 2: { $g=$n; break; }      
								    }
                                    if ($_POST[tgl]==$g){
                                    	echo "<option value='$g' selected>$g</option>";
                                	}else{
                                		echo "<option value='$g'>$g</option>";
                                	}
                                }
							echo "</select>

							<select name='bln'>";
								$bln = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
                                echo "<option value='".date("n")."' selected>".$bln[date("n")]."</option>";
                                for($n=1; $n<=12; $n++){
                                	if ($_POST[bln]==$n){
                                		echo "<option value='$n' selected>$bln[$n]</option>";
                                	}else{
                                    	echo "<option value='$n'>$bln[$n]</option>";
                                    }
                                }
							echo "</select>

							<select name='thn'>
								<option value='".date("Y")."' selected>".date("Y")."</option>";
                                for($n=2013; $n<=date("Y"); $n++){ 
                                    if ($_POST[thn]){
                                		echo "<option value='$n' selected>$n</option>";
                                	}else{
	                                    echo "<option value='$n'>$n</option>";
	                                }
                                } 
							echo "</select>

						<input type='submit' name='submit3' class='submitt' value='Lihat'>
					</form>
				</div><hr>";
				
	echo " <div id='container'>
			<div id='demo'>
<table class='display' id='example'>
	<thead>
          <tr bgcolor='#2e2e2e'>
          	<th></th>
			<th>No</th>
			<th>Tanggal</th>
			<th>Uraian</th>
			<th>Operasional</th>
			<th>Non Operasional</th>
			<th>Jumlah Total</th>
			<th>Keterangan</th>";
			if ($status == 'admin'){
				echo "<th>Status</th>
					  <th style='width:95px; text-align:center;'>Action</th>";
			}
		echo "</tr>
	</thead>
	<tfoot>
		<tr style='border: 2px solid #e3e3e3;'>
			<th style='border: none;'></th>
			<th style='border: none;'>No</th>
			<th style='border: none;'>Tanggal</th>
			<th style='border: none;'>Uraian</th>
			<th style='border: none;'>Operasional</th>
			<th style='border: none;'>Non Operasional</th>
			<th style='border: none;'>Jumlah Total</th>
			<th style='border: none;'>Keterangan</th>";
			if ($status == 'admin'){
				echo "<th style='border: none;'>Status</th>
					  <th style='border: none;'></th>";
			}
		echo "</tr>
	</tfoot>";
	echo "<tbody>";
	$no=1;
	if (isset($_POST[submit3])){
		$bln=strlen($_POST[bln]);
		switch($bln){ 
			case 1: { $g="0".$_POST[bln]; break; }
			case 2: { $g=$_POST[bln]; break; }      
		}
		$periodee = $_POST[thn].'-'.$g.'-'.$_POST[tgl];
		if ($status == 'admin'){
			$tampil = mysql_query("SELECT * FROM rb_kas where tanggal LIKE '$periodee'");
		}else{
			$tampil = mysql_query("SELECT * FROM rb_kas where status='0' AND tanggal LIKE '$periodee'");
		}
	}else{
		if ($status == 'admin'){
			$tampil = mysql_query("SELECT * FROM rb_kas ORDER BY tanggal ASC");
		}else{
			$tampil = mysql_query("SELECT * FROM rb_kas where status='0' ORDER BY tanggal ASC");
		}
	}
    while($r=mysql_fetch_array($tampil)){
	$tanggal=tgl_indo($r[tanggal]);
	$operasional=format_rupiah($r[operasional]);
	$non_operasional=format_rupiah($r[non_operasional]);
	$gaji_sewa=format_rupiah($r[gaji_sewa]);
	$total=format_rupiah($r[total]);
	  if(($no % 2)==0){
			$warna="#ffffff";
		  }
		  else{
			$warna="#E1E1E1";
		  }

		if ($r[status]=='1'){ $statuss = '<i style="color:red">Sembunyikan</i>'; }else{ $statuss = '<i style="color:green">Tampilkan</i>'; }
        echo "<tr bgcolor=$warna>
        	 <td class='read_only'></td>
             <td>$no</td>
		     <td>$tanggal</td>
		     <td>$r[uraian]</td>
			 <td>Rp $operasional</td>
			 <td>Rp $non_operasional</td>
			 <td>Rp $total</td>
			 <td>$r[keterangan]</td>";
			 if ($status == 'admin'){
		        echo "<td>$statuss</td>
		        	  <td><center><a class='linkkk' href=edit-kas-$r[id_kas].html>Edit</a>
			 					<a class='linkkk' style='color:red' href=aksi_kas.php?act=hapus&id=$r[id_kas]>X</a></center></td>";
			 }
		echo "</tr>";
			 $no++;
		}
		if (isset($_POST[submit3])){
			$bln=strlen($_POST[bln]);
			switch($bln){ 
				case 1: { $g="0".$_POST[bln]; break; }
				case 2: { $g=$_POST[bln]; break; }      
			}
			$periodee = $_POST[thn].'-'.$g.'-'.$_POST[tgl];
			$sql = "SELECT SUM(total) AS total_kas, SUM(operasional) AS total_operasional, SUM(non_operasional) AS total_non_operasional FROM rb_kas where tanggal LIKE '$periodee'";
		}else{
			$sql = "SELECT SUM(total) AS total_kas, SUM(operasional) AS total_operasional, SUM(non_operasional) AS total_non_operasional FROM rb_kas";
		}
		$result = mysql_query($sql) or die (mysql_error());
		$t = mysql_fetch_array($result);
		 $total_kas=format_rupiah($t[total_kas]);
		 $total_operasional=format_rupiah($t[total_operasional]);
		 $total_non_operasional=format_rupiah($t[total_non_operasional]);
		echo "<tr bgcolor='#e3e3e3'>
			<td style='color:red;'></td>
			<td class='read_only' style='background:#e3e3e3; color:#e3e3e3; height:27px;'>X</td>
			<td style='text-align:center; color:red; font-weight:bold;'>T o t a l</td>
            <td style='color:red;'></td>
            <td style='color:red;'>Rp $total_operasional</td>
			<td style='color:red;'>Rp $total_non_operasional</td>
			<td style='color:red;'>Rp $total_kas</td>";
			if ($status == 'admin'){
				echo "<td style='color:red;'></td>
					  <td></td>";
			}
			echo "
				  <td></td>
		</tr>";
		
echo "	</tbody>
 </table><hr><br>";
 	if (isset($_POST[submit3])){
 		$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 '$periodee%'"));
 		$dd = mysql_fetch_array(mysql_query("SELECT sum(sisa) as sisa FROM `rb_orders` where sisa > 0 AND tanggal_pesan='$periodee'"));
 		$gg = mysql_fetch_array(mysql_query("SELECT sum(sisa) as sisa FROM `rb_orders_grosir` where sisa > 0 AND tanggal_pesan='$periodee'"));

 		$tke = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah_bayar) as jumlah_bayar FROM `rb_orders` a where a.tanggal_pesan='$periodee'"));
 		$tkg = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah_bayar) as total FROM `rb_orders_grosir` a where a.tanggal_pesan='$periodee'"));
 		$ss = mysql_fetch_array(mysql_query("SELECT sum(bayar_sisa) as bayar_sisa FROM rb_sisa where waktu='$periodee'"));
 		$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='$periodee'"));
        $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='$periodee'"));
        $modal = $ec[total]+$eg[total];
 		if ($status == 'admin'){
		 	echo "<div style='float:right;' class='post_title' style='margin-bottom:10px; position:relative;'>
		 		Penghasilan Bersih = <span style='color:blue;'>Rp ".format_rupiah(($tke[jumlah_bayar]-$dd[sisa])+($tkg[total]-$gg[sisa])+$ss[bayar_sisa])." - Rp ".format_rupiah($tk[total_kas])." </span> 
		 		= <span style='color:red;'>Rp ".format_rupiah((($tke[jumlah_bayar]-$dd[sisa])+($tkg[total]-$gg[sisa])+$ss[bayar_sisa])-$tk[total_kas])."</span></div>";
		}
	}
echo "</div></div>";
}

elseif ($_GET[module]=='filterdatakas'){
	$filter = $_POST[tahun].'-'.$_POST[bulan];
	$bulan = Bulan($_POST[bulan]);
	echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>List atau Daftar Laporan Kas pada : <span style='color:red'>$bulan - $_POST[tahun]</span><hr></b></div>";
    echo "<input class='linkkk' type=button value='Report Kas' onclick=\"window.location.href='tambah-kas.html';\">";
		
				echo "<span style='float:right; margin-right:0px;'>
					<form method=POST action='kas-bulanan.html'>
							Periode : <select name='bulan'>
									<option value='$_POST[bulan]' selected>$bulan</option>";
									$tampil=mysql_query("SELECT substring(tanggal,6,2) as bulan FROM rb_kas GROUP BY substring(tanggal,6,2)");
									while($r=mysql_fetch_array($tampil)){
									$bulan = Bulan($r[bulan]);
									  echo "<option value=$r[bulan]>$bulan</option>";
									}	
							echo "</select>
							<select name='tahun'>
									<option value='$_POST[tahun]' selected>$_POST[tahun]</option>";
									$tampil=mysql_query("SELECT substring(tanggal,1,4) as tahun FROM rb_orders GROUP BY substring(tanggal,1,4)");
									while($r=mysql_fetch_array($tampil)){
									  echo "<option value=$r[tahun]>$r[tahun]</option>";
									}
							echo "</select>
						<input type='submit' class='submitt' value='View'>
						<a style='font-size:12px; text-decoration:none;' class='submitt' href='kas.html'>All Periode</a>
					</form>
				</span><hr>";

	echo " <div id='container'>
			<div id='demo'>
<table class='display' id='example'>
	<thead>
          <tr bgcolor='#2e2e2e'>
			<th>No</th>
			<th>Tanggal</th>
			<th>Uraian</th>
			<th>Operasional</th>
			<th>Non Operasional</th>
			<th>Jumlah Total</th>
			<th>Keterangan</th>";
			if ($status == 'admin'){
				echo "<th style='width:95px; text-align:center;'>Action</th>";
			}
		echo "</tr>
	</thead>
	<tfoot>
		<tr style='border: 2px solid #e3e3e3;'>
			<th style='border: none;'>No</th>
			<th style='border: none;'>Tanggal</th>
			<th style='border: none;'>Uraian</th>
			<th style='border: none;'>Operasional</th>
			<th style='border: none;'>Non Operasional</th>
			<th style='border: none;'>Jumlah Total</th>
			<th style='border: none;'>Keterangan</th>";
			if ($status == 'admin'){
				echo "<th style='border: none;'></th>";
			}
		echo "</tr>
	</tfoot>";
	echo "<tbody>";
	$no=1;
	$tampil = mysql_query("SELECT * FROM rb_kas WHERE tanggal like '$filter%'");
    while($r=mysql_fetch_array($tampil)){
	$tanggal=tgl_indo($r[tanggal]);
	$operasional=format_rupiah($r[operasional]);
	$non_operasional=format_rupiah($r[non_operasional]);
	$total=format_rupiah($r[total]);
	  if(($no % 2)==0){
			$warna="#ffffff";
		  }
		  else{
			$warna="#E1E1E1";
		  }
       echo "<tr bgcolor=$warna>
             <td class='read_only'>$no</td>
		     <td>$tanggal</td>
		     <td>$r[uraian]</td>
			 <td>Rp $operasional</td>
			 <td>Rp $non_operasional</td>
			 <td>Rp $total</td>
			 <td>$r[keterangan]</td>";
			 if ($status == 'admin'){
		        echo "<td width='130px'><center><a class='linkkk' href=edit-kas-$r[id_kas].html>Edit</a>
					 <a class='linkkk' style='color:red' href=aksi_kas.php?act=hapus&id=$r[id_kas]>X</a></center></td>";
			 }
		echo "</tr>";
			 $no++;
		}
		
		$sql = "SELECT SUM(total) AS total_kas, SUM(operasional) AS total_operasional, 
				SUM(non_operasional) AS total_non_operasional FROM rb_kas WHERE tanggal like '$filter%'";
		$result = mysql_query($sql) or die (mysql_error());
		$t = mysql_fetch_array($result);
		 $total_kas=format_rupiah($t[total_kas]);
		 $total_operasional=format_rupiah($t[total_operasional]);
		 $total_non_operasional=format_rupiah($t[total_non_operasional]);
echo "<tr bgcolor='#e3e3e3'>
			<td class='read_only' style='background:#e3e3e3; color:#e3e3e3; height:27px;'>X</td>
			<td style='text-align:center; color:red; font-weight:bold;'>T o t a l</td>
            <td style='color:red;'></td>
            <td style='color:red;'>Rp $total_operasional</td>
			<td style='color:red;'>Rp $total_non_operasional</td>
			<td style='color:red;'>Rp $total_kas</td>";
			if ($status == 'admin'){
				echo "<td style='color:red;'></td>";
			}
			echo "<td></td>
		</tr>";
		
echo "	</tbody>
 </table><br/></br/>";
 // Hitung Total Penjualan (eceran) Bersih Perbulan,..
			$tot = mysql_fetch_array(mysql_query("SELECT sum(c.harga) 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 '$filter%' 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 '$filter%' ORDER BY a.id_orders"));
			
			$totk=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 '$filter%'"));
		
		if ($status == 'admin'){
		 	echo "<div style='float:right;' class='post_title' style='margin-bottom:10px; position:relative;'>
		 		Penghasilan Bersih = <span style='color:blue;'>Rp ".format_rupiah(($tot[total_harga_jual]+$totg[total_sub])-($tot[total_modal]+$totg[total_modal]))." - Rp ".format_rupiah($totk[total_kas])."</span> 
		 		= <span style='color:red;'>Rp ".format_rupiah((($tot[total_harga_jual]+$totg[total_sub])-($tot[total_modal]+$totg[total_modal]))-$totk[total_kas])."</span></div>";
		}
echo "</div></div>";
}

elseif ($_GET[module]=='tambahdatakas'){
echo "<div class='post_title' style='margin-left:20%;'><b>Tambahkan Laporan Data Kas Masuk atau Keluar.</b></div><hr><br/>
          <form method=POST action='aksi_kas.php?act=input'>
          <center><table width='60%'>
          <tr><td width='160px' class='td'><br>Tanggal</td> <td class='td'><br/>:</td><td class='td'> <br><input id='tgl3' type=text name='tanggal' style='width:50%'></td></tr>  
          <tr><td class='td'>Uraian</td>   <td class='td'>:</td><td class='td'> <textarea name='uraian' style='width:100%; height:60px;'></textarea></td></tr>
		   <tr><td class='td'>Operasional</td>   <td class='td'>:</td><td class='td'> <input type=text name='operasional' style='width:50%'></td></tr>
		  <tr><td class='td'>Non Operasional</td>   <td class='td'>:</td><td class='td'> <input type=text name='non_operasional' style='width:50%'></td></tr>
		  <tr><td class='td'>Keterangan</td>   <td class='td'>:</td><td class='td'> <textarea name='keterangan' style='width:100%; height:60px;'></textarea></td></tr>";
		  if ($status == 'admin'){
			  echo "<tr><td class='td'>Status</td>   <td class='td'>:</td><td class='td'> <input type='radio' value='0' name='a'> Tampilkan 
			  																		<input type='radio' value='1' name='a' checked> Sembunyikan </td></tr>";
		  }	  																		
          echo "<tr><td class='td' colspan=3><br/><input class='button-reply' type=button value=Batal onclick=self.history.back()>
							<input class='button-reply' type=submit value=Simpan></td></tr>
          </table></center></form>";
}

elseif ($_GET[module]=='editdatakas'){
$edit=mysql_query("SELECT * FROM rb_kas WHERE id_kas='$_GET[id]'");
    $r=mysql_fetch_array($edit);
	$tanggal = tgl_indo($r[tanggal]);
    echo "<div class='post_title' style='margin-left:20%;'><b>Edit Data kas keluar atau masuk.</b></div><hr style='color:#e3e3e3;'><br/>
		 <div class='h_line'></div>
          <form method=POST action='aksi_kas.php?act=update'>
          <input type=hidden name=id value='$r[id_kas]'>
          <center><table width='60%'>
          <tr><td width='160px' class='td'><br>Tanggal</td> <td class='td'><br/>:</td><td class='td'><br> <input id='tgl3' type=text name='tanggal' size=30  value='$tanggal' style='width:50%'></td></tr>
          <tr><td class='td'>Uraian</td>   <td class='td'>:</td><td class='td'> <textarea name='uraian' style='width:100%; height:60px;'>$r[uraian]</textarea></td></tr>
		   <tr><td class='td'>Operasional</td>   <td class='td'>:</td><td class='td'> <input type=text name='operasional' value='$r[operasional]' style='width:50%'></td></tr>
		  <tr><td class='td'>Non Operasional</td>   <td class='td'>:</td><td class='td'> <input type=text name='non_operasional' value='$r[non_operasional]' style='width:50%'></td></tr>
		  <tr><td class='td'>Keterangan</td>   <td class='td'>:</td><td class='td'> <textarea name='keterangan' style='width:100%; height:60px;'>$r[keterangan]</textarea></td></tr>";
		  if ($status == 'admin'){
		  echo "<tr><td class='td'>Status</td>   <td class='td'>:</td><td class='td'> ";
		  														if ($r[status]=='0'){
		  															echo "<input type='radio' value='0' name='a' checked> Tampilkan 
		  															<input type='radio' value='1' name='a'> Sembunyikan ";
		  														}else{
		  															echo "<input type='radio' value='0' name='a'> Tampilkan 
		  															<input type='radio' value='1' name='a' checked> Sembunyikan ";
		  														}
		  																		echo "</td></tr>";
		 } 																		
    echo "<tr><td class='td' colspan=3><br/><input class='button-reply' type=button value=Batal onclick=self.history.back()>
									   <input class='button-reply' type=submit value=Update></td></tr>
          </table></center></form>";     
}