<?php
include "../../include/config.php";
include "../../function/inc_function.php";
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="export_laba_rugi_'.get_datetime().'.xls"');
header('Cache-Control: max-age=0');

$fromdate   = $_REQUEST['fromdate'];
$todate     = $_REQUEST['todate'];

$sqlShow 	= mysql_query("SELECT prof_name FROM profil");
$dataShow 	= mysql_fetch_array($sqlShow);

?>
<table width="100%" border="0">
    <tr>
  		<td colspan="5">LAPORAN LABA RUGI</td>
    </tr>
    <tr>
  		<td colspan="5">KAWASAN BERIKAT <?=strtoupper($dataShow['prof_name']);?></td>
    </tr>
    <tr>
  		<td colspan="5">PERIODE : <?=$fromdate;?>S/D<?=$todate;?></td>
    </tr>
    <tr>
  		<td colspan="5">&nbsp;</td>
    </tr>
</table>	
<table width="100%" border="1">	
  <tr height="30px;">
    <th>No</th>
    <th>No COA/GL</th>
	<th>Keterangan</th>
	<th>Bulan Berjalan</th>
	<th>Tahun Berjalan</th>
	<!--<th>%</th>	
	<th>%</th>-->
  </tr>
  <!--1-->
  <?php
	$no1=1; $t_lastmonth1=0; $t_curmonth1=0; $t_curyear1=0;
	$qd1 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=5 ORDER BY a.j_acctno ASC");
	while($dd1 = mysql_fetch_array($qd1)){		
		$curmonth1    = get_nr_curmonth($fromdate,$todate,$dd1['j_acctno']);		
		$t_curmonth1  = $t_curmonth1+$curmonth1;
		$lastmonth1   = get_nr_lastmonth($fromdate,$todate,$dd1['j_acctno']);
		$t_lastmonth1 = $t_lastmonth1+$lastmonth1;
		$curyear1     = $lastmonth1+$curmonth1;
		$t_curyear1   = $t_curyear1+$curyear1;		
  ?>
	<tr>
		<td><?=$no1;?>.</td>
		<td><?=$dd1['j_acctno'];?></td>
		<td><?=$dd1['j_name1'];?></td>		
		<td align="right"><?=number_format($curmonth1,2,'.','');?></td>
		<td align="right"><?=number_format($curyear1,2,'.','');?></td>
		<!--<td>&nbsp;</td>		
		<td>&nbsp;</td>-->
	</tr>
  <?php $no1++; } ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>PENJUALAN</b></td>
	<td align="right"><b><?=number_format($t_curmonth1,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t_curyear1,2,'.','');?></b></td>
  </tr>
  <!--2-->
  <?php
	$no2=1; $t_lastmonth2=0; $t_curmonth2=0; $t_curyear2=0;
	$qd2 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=6 ORDER BY a.j_acctno ASC");
	while($dd2 = mysql_fetch_array($qd2)){
		$curmonth2   = get_nr_curmonth($fromdate,$todate,$dd2['j_acctno']);
		$t_curmonth2 = $t_curmonth2+$curmonth2;
		$lastmonth2   = get_nr_lastmonth($fromdate,$todate,$dd2['j_acctno']);
		$t_lastmonth2 = $t_lastmonth2+$lastmonth2;
		$curyear2     = $lastmonth2+$curmonth2;
		$t_curyear2   = $t_curyear2+$curyear2;
  ?>
	<tr>
		<td><?=$no2;?>.</td>
		<td><?=$dd2['j_acctno'];?></td>
		<td><?=$dd2['j_name1'];?></td>		
		<td align="right"><?=number_format($curmonth2,2,'.','');?></td>
		<td align="right"><?=number_format($curyear2,2,'.','');?></td>
		<!--<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>-->
	</tr>
  <?php $no2++; } ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>PERSEDIAAN AWAL BARANG JADI</b></td>
	<td align="right"><b><?=number_format($t_curmonth2,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t_curyear2,2,'.','');?></b></td>
  </tr>
  <?php $thpp  =get_hpp($fromdate,$todate); 
		$thppy =get_hppy($fromdate,$todate); 
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>JUMLAH HARGA POKOK PRODUKSI</b></td>
	<td align="right"><b><?=number_format($thpp,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($thppy,2,'.','');?></b></td>
  </tr>
  <?php $t1  = $thpp + $t_curmonth2; 
		$t1y = $thppy + $t_curyear2; 
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>BARANG TERSEDIA UNTUK DIJUAL</b></td>
	<td align="right"><b><?=number_format($t1,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t1y,2,'.','');?></b></td>
  </tr>
  <!--3-->
  <?php
	$no3=1; $t_lastmonth3=0; $t_curmonth3=0; $t_curyear3=0;
	$qd3 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=7 ORDER BY a.j_acctno ASC");
	while($dd3 = mysql_fetch_array($qd3)){
		$curmonth3    = get_nr_curmonth($fromdate,$todate,$dd3['j_acctno']);
		$t_curmonth3  = $t_curmonth3+$curmonth3;
		$lastmonth3   = get_nr_lastmonth($fromdate,$todate,$dd3['j_acctno']);
		$t_lastmonth3 = $t_lastmonth3+$lastmonth3;
		$curyear3     = $lastmonth3+$curmonth3;
		$t_curyear3   = $t_curyear3+$curyear3;
		/*if($curmonth3<0){
			$curmonth3=$curmonth3*-1;
			$curyear3 =$curyear3*-1;
		}else{
			$curmonth3=$curmonth3;
			$curyear3 =$curyear3;
		}*/	
		$curmonth3=$curmonth3;
		$curyear3 =$curyear3;
  ?>
	<tr>
		<td><?=$no3;?>.</td>
		<td><?=$dd3['j_acctno'];?></td>
		<td><?=$dd3['j_name1'];?></td>		
		<td align="right"><?=number_format($curmonth3,2,'.','');?></td>
		<td align="right"><?=number_format($curyear3,2,'.','');?></td>
		<!--<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>-->
	</tr>
  <?php $no3++; } 
	/*if($t_curmonth3<0){
		$t_curmonth3=$t_curmonth3*-1;
		$t_curyear3 =$t_curyear3*-1;
	}else{
		$t_curmonth3=$t_curmonth3;
		$t_curyear3 =$t_curyear3;
	}*/
	$t_curmonth3=$t_curmonth3;
	$t_curyear3 =$t_curyear3;
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>PERSEDIAAN AKHIR BARANG JADI</b></td>
	<td align="right"><b><?=number_format($t_curmonth3,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t_curyear3,2,'.','');?></b></td>
  </tr>
  <?php $t2  = $t1 - $t_curmonth3; 
		$t2y = $t1y - $t_curyear3; 
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>JUMLAH HARGA POKOK</b></td>
	<td align="right"><b><?=number_format($t2,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t2y,2,'.','');?></b></td>
  </tr>
  <?php $t3  = $t_curmonth1 - $t2; 
		$t3y = $t_curyear1 - $t2y; 
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>LABA KOTOR</b></td>
	<td align="right"><b><?=number_format($t3,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t3y,2,'.','');?></b></td>
  </tr>
  <!--4-->
  <?php
	$no4=1; $t_lastmonth4=0; $t_curmonth4=0; $t_curyear4=0;
	$qd4 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=8 ORDER BY a.j_acctno ASC");
	while($dd4 = mysql_fetch_array($qd4)){
		$curmonth4   = get_nr_curmonth($fromdate,$todate,$dd4['j_acctno']);
		$t_curmonth4 = $t_curmonth4+$curmonth4;
		$lastmonth4   = get_nr_lastmonth($fromdate,$todate,$dd4['j_acctno']);
		$t_lastmonth4 = $t_lastmonth4+$lastmonth4;
		$curyear4     = $lastmonth4+$curmonth4;
		$t_curyear4   = $t_curyear4+$curyear4;
		/*if($curmonth4<0){
			$curmonth4=$curmonth4*-1;
			$curyear4 = $curyear4*-1;
		}else{
			$curmonth4=$curmonth4;
			$curyear4 =$curyear4;
		}*/
		$curmonth4=$curmonth4;
		$curyear4 =$curyear4;	
  ?>
	<tr>
		<td><?=$no4;?>.</td>
		<td><?=$dd4['j_acctno'];?></td>
		<td><?=$dd4['j_name1'];?></td>		
		<td align="right"><?=number_format($curmonth4,2,'.','');?></td>
		<td align="right"><?=number_format($curyear4,2,'.','');?></td>
		<!--<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>-->
	</tr>
  <?php $no4++; } 
	/*if($t_curmonth4<0){
		$t_curmonth4=$t_curmonth4*-1;
		$t_curyear4 =$t_curyear4*-1;
	}else{
		$t_curmonth4=$t_curmonth4;
		$t_curyear4 =$t_curyear4;
	}*/
	$t_curmonth4=$t_curmonth4;
	$t_curyear4 =$t_curyear4;
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>BIAYA ADMINISTRASI DAN UMUM</b></td>
	<td align="right"><b><?=number_format($t_curmonth4,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t_curyear4,2,'.','');?></b></td>
  </tr>
  <?php $t4 = $t3 - $t_curmonth4; 
		$t4y = $t3y - $t_curyear4; 
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>LABA OPERASI</b></td>
	<td align="right"><b><?=number_format($t4,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t4y,2,'.','');?></b></td>
  </tr>
  <!--5-->
  <?php
	$no5=1; $t_lastmonth5=0; $t_curmonth5=0; $t_curyear5=0;
	$qd5 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=9 ORDER BY a.j_acctno ASC");
	while($dd5 = mysql_fetch_array($qd5)){
		$curmonth5   = get_nr_curmonth($fromdate,$todate,$dd5['j_acctno']);
		$t_curmonth5 = $t_curmonth5+$curmonth5;
		$lastmonth5   = get_nr_lastmonth($fromdate,$todate,$dd5['j_acctno']);
		$t_lastmonth5 = $t_lastmonth5+$lastmonth5;
		$curyear5     = $lastmonth5+$curmonth5;
		$t_curyear5   = $t_curyear5+$curyear5;
		/*if($curmonth5<0){
			$curmonth5=$curmonth5*-1;
			$curyear5 =$curyear5*-1;
		}else{
			$curmonth5=$curmonth5;
			$curyear5 =$curyear5;
		}*/		
		$curmonth5=$curmonth5;
		$curyear5 =$curyear5;
  ?>
	<tr>
		<td><?=$no5;?>.</td>
		<td><?=$dd5['j_acctno'];?></td>
		<td><?=$dd5['j_name1'];?></td>		
		<td align="right"><?=number_format($curmonth5,2,'.','');?></td>
		<td align="right"><?=number_format($curyear5,2,'.','');?></td>
		<!--<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>-->
	</tr>
  <?php $no5++; } 
    /*if($t_curmonth5<0){
		$t_curmonth5=$t_curmonth5*-1;
		$t_curyear5 =$t_curyear5*-1;
	}else{
		$t_curmonth5=$t_curmonth5;
		$t_curyear5 =$t_curyear5;
	}*/
	$t_curmonth5=$t_curmonth5;
	$t_curyear5 =$t_curyear5;
	?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>PENGHASILAN DILUAR USAHA</b></td>
	<td align="right"><b><?=number_format($t_curmonth5,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t_curyear5,2,'.','');?></b></td>
  </tr>
  <!--6-->
  <?php
	$no6=1; $t_lastmonth6=0; $t_curmonth6=0; $t_curyear6=0;
	$qd6 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=10 ORDER BY a.j_acctno ASC");
	while($dd6 = mysql_fetch_array($qd6)){
		$curmonth6    = get_nr_curmonth($fromdate,$todate,$dd6['j_acctno']);
		$curmonth6    = $curmonth6*-1;
		$t_curmonth6  = $t_curmonth6+$curmonth6;
		$lastmonth6   = get_nr_lastmonth($fromdate,$todate,$dd6['j_acctno']);
		$lastmonth6   = $lastmonth6*-1;
		$t_lastmonth6 = $t_lastmonth6+$lastmonth6;
		$curyear6     = $lastmonth6+$curmonth6;
		$t_curyear6   = $t_curyear6+$curyear6;
		/*if($curmonth6<0){
			$curmonth6=$curmonth6*-1;
			$curyear6 =$curyear6*-1;
		}else{
			$curmonth6=$curmonth6;
			$curyear6 =$curyear6;
		}
		if($t_curmonth6<0){
			$t_curmonth6=$t_curmonth6;
			$t_curyear6 =$t_curyear6;
		}else{
			$t_curmonth6=$t_curmonth6;
			$t_curyear6 =$t_curyear6;
		}*/
  ?>
	<tr>
		<td><?=$no6;?>.</td>
		<td><?=$dd6['j_acctno'];?></td>
		<td><?=$dd6['j_name1'];?></td>		
		<td align="right"><?=number_format($curmonth6,2,'.','');?></td>
		<td align="right"><?=number_format($curyear6,2,'.','');?></td>
		<!--<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>-->
	</tr>
  <?php $no6++; } ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>PENGELUARAN DILUAR USAHA</b></td>
	<td align="right"><b><?=number_format($t_curmonth6,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t_curyear6,2,'.','');?></b></td>
  </tr>
  <?php 
  		#if($t_curmonth6<0){$t5 = $t_curmonth5 - (-$t_curmonth6);}else{$t5 = $t_curmonth5 - $t_curmonth6;} 
		#if($t_curyear6<0){$t5y = $t_curyear5 - (-$t_curyear6);}else{$t5y = $t_curyear5 - $t_curyear6;} 
		$t5 = $t_curmonth5 + $t_curmonth6;
		$t5y = $t_curyear5 + $t_curyear6;
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>JUMLAH BIAYA/PENDAPATAN LAIN</b></td>
	<td align="right"><b><?=number_format($t5,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t5y,2,'.','');?></b></td>
  </tr>
  <?php $t6 = $t4 + $t5; 
		$t6y = $t4y + $t5y;
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>LABA SEBELUM PAJAK</b></td>
	<td align="right"><b><?=number_format($t6,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t6y,2,'.','');?></b></td>
  </tr>
  
  <!--7--NEW-->
  <?php
	$no19=1; $t_lastmonth19=0; $t_curmonth19=0; $t_curyear19=0;
	$qd19 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=19 ORDER BY a.j_acctno ASC");
	while($dd19 = mysql_fetch_array($qd19)){
		$curmonth19    = get_nr_curmonth($fromdate,$todate,$dd19['j_acctno']);		
		$t_curmonth19  = $t_curmonth19+$curmonth19;
		$lastmonth19   = get_nr_lastmonth($fromdate,$todate,$dd19['j_acctno']);		
		$t_lastmonth19 = $t_lastmonth19+$lastmonth19;
		$curyear19     = $lastmonth19+$curmonth19;
		$t_curyear19   = $t_curyear19+$curyear19;		
  ?>
	<tr>
		<td><?=$no19;?>.</td>
		<td><?=$dd19['j_acctno'];?></td>
		<td><?=$dd19['j_name1'];?></td>		
		<td align="right"><?=number_format($curmonth19,2,'.','');?></td>
		<td align="right"><?=number_format($curyear19,2,'.','');?></td>
		<!--<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>-->
	</tr>
  <?php $no19++; } ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>PAJAK PENGHASILAN BADAN</b></td>
	<td align="right"><b><?=number_format($t_curmonth19,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t_curyear19,2,'.','');?></b></td>
  </tr>  
  <?php $t8 =$t6 - $t_curmonth19; 
		$t8y =$t6y - $t_curyear19; 
  ?>
  <tr bgcolor="#CCFF00">
	<td colspan="3"><b>LABA SETELAH PAJAK</b></td>
	<td align="right"><b><?=number_format($t8,2,'.','');?></b></td>
	<td align="right"><b><?=number_format($t8y,2,'.','');?></b></td>
  </tr>
</table>