<?php
if ($_GET[module]=='pembelian'){
	if (isset($_POST[tgl])){
			$lebar=strlen($_POST[bulan]);
			switch($lebar){
		      case 1:{ $g="0".$_POST[bulan]; break; }
		      case 2:{ $g=$_POST[bulan]; break; }      
		    } 
		    $tgl_pesann = $_POST[tahun].'-'.$g.'-'.$_POST[tanggal];
	}		

$detail=mysql_query("SELECT * FROM rb_kategori_produk where id_kategori='$_GET[id]'");
	$d   = mysql_fetch_array($detail);
	echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>Daftar atau List Laporan Data Pembelian $d[nama_kategori]. <hr></b></div>
		  <input class='linkkk' type=button value='Tambah Lap. Pembelian $d[nama_kategori]' onclick=\"window.location.href='tambah-pembelian-$d[id_kategori].html';\"><hr>";
			  	
			  	echo "<form style='float:right; width:410px' method=POST action='pembelian-$d[id_kategori].html'>
					  Tanggal : <select style='width:23%' name='tanggal'><option value='' selected>- Tanggal -</option>";
													for($n=1; $n<=31; $n++){
															if ($_POST[tanggal]==$n){
																echo "<option value='$n' selected>$n</option>";
															}else{
																echo "<option value='$n'>$n</option>";
															}
														}
				echo "</select>
															-
				<select style='width:25%' name='bulan'> <option value='' selected>- Bulan -</option>";
													$bln = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
														for($n=1; $n<=12; $n++){
															if ($_POST[bulan]==$n){
																echo "<option value='$n' selected>$bln[$n]</option>";
															}else{
																echo "<option value='$n'>$bln[$n]</option>";
															}
														}
				echo "</select>
													  -
				<select style='width:20%' name='tahun'>
									<option value=0 selected>- Tahun -</option>";
									$tampil=mysql_query("SELECT substring(tanggal,7,4) as tahun FROM rb_pembelian where kategori='$_GET[id]' GROUP BY substring(tanggal,7,4)");
									while($r=mysql_fetch_array($tampil)){
									if ($_POST[tahun]==$r[tahun]){
									  echo "<option value=$r[tahun] selected>$r[tahun]</option>";
									}else{
									  echo "<option value=$r[tahun]>$r[tahun]</option>";	
									}

									}
							echo "</select>
					  <input type='submit' class='submitt' name='tgl' value='View'>
				</form>
				<div style='clear:both'></div>";

	echo " <div id='container'>
			<div id='demo'>
<table class='display' id='example'>
	<thead>
		<tr><th width='40px'>No</th>
			<th>Tanggal</th>
			<th>Faktur</th>
			<th>Distributor</th>
			<th>Produk</th>
			<th>Jumlah (Rp)</th>
			<th>Diskon</th>
			<th>Ongkos</th>
			<th>DP</th>
			<th>Sisa Hutang</th>
			<th>Total + Ongkos</th>
			<th style='text-align:center;'>Action</th>
		</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;'>Faktur</th>
			<th style='border: none;'>Distributor</th>
			<th style='border: none;'>Produk</th>
			<th style='border: none;'>Jumlah (Rp)</th>
			<th style='border: none;'>Diskon</th>
			<th style='border: none;'>Ongkos</th>
			<th style='border: none;'>DP</th>
			<th style='border: none;'>Sisa Hutang</th>
			<th style='border: none;'>Total + Ongkos</th>
			<th style='border: none;'></th>
		</tr>
	</tfoot>";
	$no=1;
	if (isset($_POST[tgl])){
		$tampil = mysql_query("SELECT * FROM rb_pembelian left join rb_supplier on rb_pembelian.distributor=rb_supplier.id_supplier where rb_pembelian.kategori='$_GET[id]' AND rb_pembelian.tanggal='$tgl_pesann' ORDER BY rb_pembelian.id_pembelian DESC");
	}else{
	    $tampil = mysql_query("SELECT * FROM rb_pembelian left join rb_supplier on rb_pembelian.distributor=rb_supplier.id_supplier where rb_pembelian.kategori='$_GET[id]' ORDER BY rb_pembelian.id_pembelian DESC");
	}
		echo "<tbody>";
    while($r=mysql_fetch_array($tampil)){
      $tanggal=tgl_indo($r[tgl_masuk]);
	  $total = $r[diskon];
	  $total_bayar =$r[jumlah_rp]-$total;
	  $total_pembelian=$total_bayar+$r[ongkos];
	  $total_rp=format_rupiah($total);
	  $total_bayar_rp=format_rupiah($total_pembelian);
      $jumlah_rp=format_rupiah($r[jumlah_rp]);
	  $dp_rp=format_rupiah($r[dp]);
	  $ongkos_rp=format_rupiah($r[ongkos]);
	  
	  $sisa = $total_pembelian - $r[dp];
	  $sisa_rp = format_rupiah($sisa);
	  
	  if(($no % 2)==0){
			$warna="#ffffff";
		  }
		  else{
			$warna="#E1E1E1";
		  }
	echo "<tr bgcolor='$warna'>
			<td>$no</td>
			<td class='read_only' style='color:blue;'>$r[tanggal]</td>
            <td class='linkSysteM'><a href='#' alt='$r[status]'>$r[faktur]</a></td>
			<td>$r[nama_supplier]</td>
			<td>$r[produk]</td>
			<td>Rp $jumlah_rp</td>
			<td>Rp $r[diskon]</td>
			<td>Rp $ongkos_rp</td>
			<td>Rp $dp_rp</td>
			<td>Rp $sisa_rp</td>
			<td>Rp $total_bayar_rp</td>
			<td width=90px><center><a class='linkkk' href='edit-pembelian-$r[id_pembelian].html'>Edit</a>
						<a class='linkkk' style='color:Red' href='aksi_pembelian.php?act=hapus&id=$r[id_pembelian]'>X</a></center></td>
		</tr>";
		$no++;
		}
		
		if (isset($_POST[tgl])){
		$sql = "SELECT SUM(jumlah_rp) AS jumlah_rp,
						SUM(ongkos) AS ongkos,
						SUM(diskon) AS diskon,
						SUM(dp) AS dp,
						sum((jumlah_rp-dp)+ongkos) as hutangg FROM rb_pembelian where kategori='$_GET[id]' AND tanggal='$tgl_pesann'";
		}else{
		$sql = "SELECT SUM(jumlah_rp) AS jumlah_rp,
						SUM(ongkos) AS ongkos,
						SUM(diskon) AS diskon,
						SUM(dp) AS dp,
						sum((jumlah_rp-dp)+ongkos) as hutang FROM rb_pembelian where kategori='$_GET[id]'";
		}

		$result = mysql_query($sql) or die (mysql_error());
		$t = mysql_fetch_array($result);
		 $jumlah_rp=format_rupiah($t[jumlah_rp]);
		 $ongkos_rp=format_rupiah($t[ongkos]);
		 $dp_rp=format_rupiah($t[dp]);
		 $diskon_rp=format_rupiah($t[diskon]);
		 $hutang_rp=format_rupiah($t[hutang]);
		 $total_semua=$t[jumlah_rp]-$t[diskon];
		 $total_semua_rp=format_rupiah($total_semua);

	echo "
		<tr style='border:1px solid $warna;'>
			<td style='background:$warna; color:$warna'>9999</td>
			<td style='color:red; font-weight:bold;'>T o t a l</td>
			<td></td>
            <td style='color:red'></td>
			<td style='color:red'></td>
			<td style='color:red'>Rp $jumlah_rp</td>
			<td style='color:red'>Rp $diskon_rp</td>
			<td style='color:red'>Rp $ongkos_rp</td>
			<td style='color:red'>Rp $dp_rp</td>
			<td style='color:red'>Rp $hutang_rp</td>
			<td style='color:red'>Rp $total_semua_rp</td>
			<td></td>
		</tr>";
		
echo "	</tbody>
 </table>
</div></div>";  
}

elseif ($_GET[module]=='tambahpembelian'){
$detail=mysql_query("SELECT * FROM rb_kategori_produk where id_kategori='$_GET[id]'");
	$d   = mysql_fetch_array($detail);
    echo "<div class='post_title' style='margin-left:20%;'><b>Tambah laporan Pembelian $d[nama_kategori].</b></div><hr><br/>
          <form method=POST enctype='multipart/form-data' action='aksi_pembelian.php?act=input'>
		  
          <center><table width='60%'>
		  <input type=hidden value='$_GET[id]' name='kategori'>
		  <tr><td class='td' width=140>Tanggal</td><td class='td' width='20px'>:</td><td class='td'> <input id='tgl5' type=text name='tanggal' size=60 style='width:50%'></td></tr>
          <tr><td class='td'>Faktur</td>    <td class='td'>:</td> <td class='td'> <input type=text name='faktur' size=60 style='width:50%'></td></tr>
		  <tr><td class='td'>Distributor</td>  	  <td class='td'>:</td><td class='td'><select name='distributor'>
            <option value=0 selected>- Pilih Nama Distributor -</option>";
            $tampil=mysql_query("SELECT * FROM rb_supplier ORDER BY nama_supplier");
            while($r=mysql_fetch_array($tampil)){
              echo "<option value=$r[id_supplier]>$r[nama_supplier]</option>";
            }
			
	echo "</select></td></tr>
		  <tr><td class='td'>Produk</td>    <td class='td'>:</td> <td class='td'> <input type=text name='produk' style='width:50%'></td></tr>
          <tr><td class='td'>Jumlah Rp</td>    <td class='td'>:</td> <td class='td'> <input type=text name='jumlah_rp' style='width:50%'></td></tr>
		  <tr><td class='td'>Diskon</td>  <td class='td'>:</td> <td class='td'> <input type=text name='diskon' style='width:30%'></td></tr>
		  <tr><td class='td'>Ongkos</td>  <td class='td'>:</td> <td class='td'> <input type=text name='ongkos' style='width:30%'></td></tr>
		  <tr><td class='td'>DP (Rp)</td>  <td class='td'>:</td> <td class='td'> <input type=text name='dp' style='width:30%'></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>
		  <tr><td class='td'>Status</td>  <td class='td'>:</td> <td class='td'> <input type=text name='status' style='width:20%'></td></tr>
		  
          <tr><td class='td' colspan=3><input class='button-reply' type=button value=Batal onclick=self.history.back()>
							<input class='button-reply' type=submit value=Tambah></td></tr>
         </table></center></form>";
}

elseif ($_GET[module]=='editpembelian'){
	$edit = mysql_query("SELECT * FROM rb_pembelian WHERE id_pembelian='$_GET[id]'");
    $r    = mysql_fetch_array($edit);
	
    echo "<div class='post_title' style='margin-left:20%;'><b>Edit Laporan Pembelian </b></div><hr><br/>
          <form method=POST enctype='multipart/form-data' action='aksi_pembelian.php?act=update'>
          <input type=hidden name=id value=$r[id_pembelian]>
          <center><table width='60%'>
		  <input type=hidden value='$r[kategori]' name='kategori'>
		  <tr><td class='td' width=140>Tanggal</td><td class='td' width='20px'>:</td><td class='td'> <input id='tgl5' type=text name='tanggal' size=60 style='width:50%' value='$r[tanggal]'></td></tr>
          <tr><td class='td'>Faktur</td>    <td class='td'>:</td> <td class='td'> <input type=text name='faktur' size=60 style='width:50%' value='$r[faktur]'></td></tr>
		  <tr><td class='td'>Distributor</td>  <td class='td'>:</td> <td class='td'> <select name='distributor'>";
 
          $tampil=mysql_query("SELECT * FROM rb_supplier ORDER BY nama_supplier");
          if ($r[distributor]==0){
            echo "<option value=0 selected>- Pilih Nama Distributor -</option>";
          }   

          while($w=mysql_fetch_array($tampil)){
            if ($r[distributor]==$w[id_supplier]){
              echo "<option value=$w[id_supplier] selected>$w[nama_supplier]</option>";
            }
            else{
              echo "<option value=$w[id_supplier]>$w[nama_supplier]</option>";
            }
          }
    echo "</select></td></tr>
		  <tr><td class='td'>Produk</td>    <td class='td'>:</td> <td class='td'> <input type=text name='produk' value='$r[produk]' style='width:50%'></td></tr>
		  <tr><td class='td'>Jumlah Rp</td>    <td class='td'>:</td> <td class='td'> <input type=text name='jumlah_rp' value='$r[jumlah_rp]' style='width:50%'></td></tr>
		  <tr><td class='td'>Diskon</td>  <td class='td'>:</td> <td class='td'> <input type=text name='diskon' value='$r[diskon]' style='width:30%'></td></tr>
		  <tr><td class='td'>Ongkos</td>  <td class='td'>:</td> <td class='td'> <input type=text name='ongkos' value='$r[ongkos]' style='width:30%'></td></tr>
		  <tr><td class='td'>DP (Rp)</td>  <td class='td'>:</td> <td class='td'> <input type=text name='dp' value='$r[dp]' style='width:30%'></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>
		  <tr><td class='td'>Status</td>  <td class='td'>:</td> <td class='td'> <input type=text name='status' value='$r[status]' style='width:20%'></td></tr>
		  
          <tr><td class='td' colspan=3><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>";
}