<?php
	include '../../inc/inc.koneksi.php';	
	include '../../inc/fungsi_tanggal.php';

	$bulan= $_GET['bulan'];
	$tahun= $_GET['tahun'];
	$laba=0;
	
	if($bulan==1) $nbulan='JANUARI';
	else if($bulan==2) $nbulan='FEBRUARI';
	else if($bulan==3) $nbulan='MARET';
	else if($bulan==4) $nbulan='APRIL';
	else if($bulan==5) $nbulan='MEI';
	else if($bulan==6) $nbulan='JUNI';
	else if($bulan==7) $nbulan='JULI';
	else if($bulan==8) $nbulan='AGUSTUS';
	else if($bulan==9) $nbulan='SEPTEMBER';
	else if($bulan==10) $nbulan='OKTOBER';
	else if($bulan==11) $nbulan='NOVEMBER';
	else $nbulan='DESEMBER';
	
	$sql2= mysql_query("SELECT * FROM company");
	while($r2=mysql_fetch_array($sql2)){
		$kop1 = $r2[nama1];
		$kop2 = $r2[nama2];
		$kop3 = $r2[alamat];
	}
	
	$content .=  "<b><li>$kop1</li></b>
		<li>$kop2</li>
		<li>$kop3</li>
		<br>
		<b><li style='font:16px arial'>LAPORAN LABA RUGI USAHA</b></li>
		<li style='font:16px arial'><b>BULAN $nbulan $tahun</li></b>
		<br>
		<hr>
	
	<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<b><li>Pendapatan Usaha</b></li>
		<li>
	</table>
	<br>";
	
	$sql1= mysql_query("SELECT * FROM akun_bb WHERE nogol='41' AND level='2' ORDER BY noakun");
    $content .= "<table width='100%' align='left' style='font:13px arial'>";
	
	$subtot	= 0;
	while($r1=mysql_fetch_array($sql1)){
		$noakun=$r1[noakun];

		$sql = mysql_query("SELECT tanggal,noakun,debet,kredit FROM rekapjur WHERE noakun='$noakun' AND month(tanggal)='$bulan' AND year(tanggal)='$tahun' AND batal=''");
				
		$rdebet=0;
		$rkredit=0;
		while($r=mysql_fetch_array($sql)){
			$rdebet=$rdebet+$r[debet];
			$rkredit=$rkredit+$r[kredit];
		}			
		$saldo=$rkredit-$rdebet;
		$subtot=$subtot+$saldo;
		
		$content .= "<tr>
			<td width='3%'></td>
			<td width='35%'>$r1[naakun]</td>					
			<td width='2%'>Rp</td>
			<td width='20%' align='right'>".number_format($saldo,2)."</td>
			<td width='40%'></td>
		</tr>";
	}
	$content .=  "</table>
	<br>
	
	<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%' style='border-bottom:double 2px'></td>
			<td width='20%' style='border-bottom:double 2px'></td>
			<td width='2%'></td>
			<td width='20%'></td>
			<td width='18%'></td>
		</tr>
			
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Jumlah</td>
			<td style='border-bottom:double 2px'>Rp</td>
			<td align='right' style='border-bottom:double 2px'>".number_format($subtot,2)."</td>
			<td></td>
		</tr>
	</table>
	<br>";
	$laba=$laba+$subtot;
		
$content .=  "<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td><b>Harga Pokok Penjualan</b></td>
		</tr>
	</table>
	<br>";
				
	$query1 = "SELECT * FROM akun_bb WHERE nogol='51' AND level='2' ORDER BY noakun";		
	$content .= "<table width='100%' align='left' style='font:13px arial'>";
	
	$sql1	= mysql_query($query1);
	$subtot	= 0;
	while($r1=mysql_fetch_array($sql1)){
		$noakun=$r1[noakun];
		
		$sql = mysql_query("SELECT tanggal,noakun,debet,kredit FROM rekapjur WHERE noakun='$noakun' AND month(tanggal)='$bulan' AND year(tanggal)='$tahun' AND batal=''");
				
		$rdebet=0;
		$rkredit=0;
		while($r=mysql_fetch_array($sql)){
			$rdebet=$rdebet+$r[debet];
			$rkredit=$rkredit+$r[kredit];
		}	
		$saldo =$rdebet-$rkredit;
		$subtot=$subtot+$saldo;
			
		$content .=  "<tr>
			<td width='3%'></td>
			<td width='35%'>$r1[naakun]</td>					
			<td width='2%'>Rp</td>
			<td width='20%' align='right'>".number_format($saldo,2)."</td>
			<td width='40%'></td>
		</tr>";
	}
	$content .=  "</table>
	<br>
	
	<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%' style='border-bottom:double 2px'></td>
			<td width='20%' align='right' style='border-bottom:double 2px'></td>
			<td width='2%'></td>
			<td width='20%'></td>
			<td width='18%'></td>
		</tr>
			
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%'></td>
			<td width='20%' align='right'>Jumlah</td>
			<td width='2%' style='border-bottom:double 2px'>Rp</td>
			<td width='20%' align='right' style='border-bottom:double 2px'>".number_format($subtot,2)."</td>
			<td width='18%'></td>
		</tr>
	</table>
	<br>";
	$laba=$laba-$subtot;
		
	$content .=  "<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%'></td>
			<td width='20%' align='right'>Laba Kotor</td>
			<td width='2%' style='border-bottom:double 2px'>Rp</td>
			<td width='20%'align='right' style='border-bottom:double 2px'>".number_format($laba,2)."</td>
			<td width='18%'></td>
		</tr>
	</table>
	<br>";

$content .=  "<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td><b>Biaya Operasional</b></td>
		</tr>
	</table>
	<br>";

	$sql1= mysql_query("SELECT * FROM akun_bb WHERE nogol='52' AND level='2' ORDER BY noakun");
$content .=  "<table width='100%' align='left' style='font:13px arial'>";
	
	$subtot	= 0;
	while($r1=mysql_fetch_array($sql1)){
		$noakun=$r1[noakun];

		$sql = mysql_query("SELECT tanggal,noakun,debet,kredit FROM rekapjur WHERE noakun='$noakun' AND month(tanggal)='$bulan' AND year(tanggal)='$tahun' AND batal='' ");
			
		$rdebet=0;
		$rkredit=0;
		while($r=mysql_fetch_array($sql)){
			$rdebet=$rdebet+$r[debet];
			$rkredit=$rkredit+$r[kredit];
		}			
		$saldo =$rdebet-$rkredit;
		$subtot=$subtot+$saldo;
				
		$content .= "<tr>
			<td width='3%'></td>
			<td width='35%'>$r1[naakun]</td>					
			<td width='2%'>Rp</td>
			<td width='20%' align='right'>".number_format($saldo,2)."</td>
			<td width='40%'></td>
		</tr>";
	}
		
	$content .= "</table>
	<br>
	
	<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%' style='border-bottom:double 2px'></td>
			<td width='20%' align='right' style='border-bottom:double 2px'></td>
			<td width='2%'></td>
			<td width='20%'></td>
			<td width='18%'></td>
		</tr>
			
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Jumlah</td>
			<td style='border-bottom:double 2px'>Rp</td>
			<td align='right' style='border-bottom:double 2px'>".number_format($subtot,2)."</td>
			<td></td>
		</tr>
	</table>
	<br>";
	$laba=$laba-$subtot;
		
$content .=  "<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td><b>Biaya Administrasi dan Umum</b></td>
		</tr>
	</table>
	<br>";

	$sql1	= mysql_query("SELECT * FROM akun_bb WHERE nogol='53' AND level='2' ORDER BY noakun");
	$content .= "<table width='100%' align='left' style='font:13px arial'>";
	
	$subtot	= 0;
	while($r1=mysql_fetch_array($sql1)){
		$noakun=$r1[noakun];

		$sql = mysql_query("SELECT tanggal,noakun,debet,kredit FROM rekapjur WHERE noakun='$noakun' AND month(tanggal)='$bulan' AND year(tanggal)='$tahun' AND batal='' ");

		$rdebet=0;
		$rkredit=0;
		while($r=mysql_fetch_array($sql)){
			$rdebet=$rdebet+$r[debet];
			$rkredit=$rkredit+$r[kredit];
		}
		$saldo =$rdebet-$rkredit;
		$subtot=$subtot+$saldo;
	$content .= "<tr>
			<td width='3%'></td>
			<td width='35%'>$r1[naakun]</td>					
			<td width='2%'>Rp</td>
			<td width='20%' align='right'>".number_format($saldo,2)."</td>
			<td width='40%'></td>
		</tr>";
	}
		
$content .=  "</table>
	<br>
	
	<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%' style='border-bottom:double 2px'></td>
			<td width='20%' align='right' style='border-bottom:double 2px'></td>
			<td width='2%'></td>
			<td width='20%'></td>
			<td width='18%'></td>
		</tr>

		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Jumlah</td>
			<td style='border-bottom:double 2px'>Rp</td>
			<td align='right' style='border-bottom:double 2px'>".number_format($subtot,2)."</td>
			<td></td>
		</tr>
	</table>
	<br>";
	$laba=$laba-$subtot;
			
	$content .=  "<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%'></td>
			<td width='20%' align='right'>Laba Usaha</td>
			<td width='2%' style='border-bottom:double 2px'>Rp</td>
			<td width='20%' align='right' style='border-bottom:double 2px'>".number_format($laba,2)."</td>
			<td width='18%'></td>
		</tr>
	</table>
	<br>";
			
$content .=  "<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td><b>Pendapatan dan Biaya Non Usaha</b></td>
		</tr>
		<tr>
			<td><b>Pendapatan Non Usaha</b></td>
		</tr>
	</table>
	<br>";
	$labanon=0;

	$sql1	= mysql_query("SELECT * FROM akun_bb WHERE nogol='61' AND level='2' ORDER BY noakun");
	$content .= "<table width='100%' align='left' style='font:13px arial'>";
	
	$subtot	= 0;
	while($r1=mysql_fetch_array($sql1)){
		$noakun=$r1[noakun];

		$sql = mysql_query("SELECT tanggal,noakun,debet,kredit FROM rekapjur WHERE noakun='$noakun' AND month(tanggal)='$bulan' AND year(tanggal)='$tahun' AND batal='' ");
				
		$rdebet=0;
		$rkredit=0;
		while($r=mysql_fetch_array($sql)){
			$rdebet=$rdebet+$r[debet];
			$rkredit=$rkredit+$r[kredit];
		}			
		$saldo =$rkredit-$rdebet;
		$subtot=$subtot+$saldo;
				
		$content .=  "<tr>
			<td width='3%'></td>
			<td width='35%'>$r1[naakun]</td>					
			<td width='2%'>Rp</td>
			<td width='20%' align='right'>".number_format($saldo,2)."</td>
			<td width='40%'></td>
		</tr>";
	}
		
	$content .=  "</table>
	<br>
	<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%' style='border-bottom:double 2px'></td>
			<td width='20%' align='right' style='border-bottom:double 2px'></td>
			<td width='2%'></td>
			<td width='20%'></td>
			<td width='18%'></td>
		</tr>
			
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Jumlah</td>
			<td style='border-bottom:double 2px'>Rp</td>
			<td align='right' style='border-bottom:double 2px'>".number_format($subtot,2)."</td>
			<td></td>
		</tr>
	</table>
	<br>";
	$labanon=$labanon+$subtot;
	$laba=$laba+$subtot;
			
$content .=  "<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td><b>Biaya Non Usaha</b></td>
		</tr>
	</table>
	<br>";

	$sql1= mysql_query("SELECT * FROM akun_bb WHERE nogol='62' AND level='2' ORDER BY noakun");
	$content .=  "<table width='100%' align='left' style='font:13px arial'>";
	
	$subtot	= 0;
	while($r1=mysql_fetch_array($sql1)){
		$noakun=$r1[noakun];

		$sql = mysql_query("SELECT tanggal,noakun,debet,kredit FROM rekapjur WHERE noakun='$noakun' AND month(tanggal)='$bulan' AND year(tanggal)='$tahun' AND batal='' ");
				
		$rdebet=0;
		$rkredit=0;
		while($r=mysql_fetch_array($sql)){
			$rdebet=$rdebet+$r[debet];
			$rkredit=$rkredit+$r[kredit];
		}			
		$saldo =$rdebet-$rkredit;
		$subtot=$subtot+$saldo;
				
		$content .=  "<tr>
			<td width='3%'></td>
			<td width='35%'>$r1[naakun]</td>					
			<td width='2%'>Rp</td>
			<td width='20%' align='right'>".number_format($saldo,2)."</td>
			<td width='40%'></td>
		</tr>";
	}
		
	$content .=  "</table>
	<br>
	
	<table width='100%' align='left' style='font:13px arial'>
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%' style='border-bottom:double 2px'></td>
			<td width='20%' align='right' style='border-bottom:double 2px'></td>
			<td width='2%'></td>
			<td width='20%'></td>
			<td width='18%'></td>
		</tr>
		
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Jumlah</td>
			<td style='border-bottom:double 2px'>Rp</td>
			<td align='right' style='border-bottom:double 2px'>".number_format($subtot,2)."</td>
			<td></td>
		</tr>";
		$labanon=$labanon-$subtot;
		$laba=$laba-$subtot;
		
	$content .=  "</table>
	<br>
	<table align='left' style='font:13px arial'>
		<tr>
			<td width='3%'></td>
			<td width='35%'></td>
			<td width='2%'></td>
			<td width='20%' align='right'>Laba Non Usaha</td>
			<td width='2%' style='border-bottom:double 2px'>Rp</td>
			<td width='20%' align='right' style='border-bottom:double 2px'>".number_format($labanon,2)."</td>
			<td width='18%'></td>
		</tr>
		
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Laba Bersih</td>
			<td style='border-bottom:double 2px'>Rp</td>
			<td align='right' style='border-bottom:double 2px'>".number_format($laba,2)."</td>
			<td></td>
		</tr>
	</table>
	<hr>";
	
	    header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=labarugi_bln$bulan$tahun.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $content;
?>