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

	$tgl_awal = $_GET['tgl_awal'];
	$tgl_akhir= $_GET['tgl_akhir'];
	$kodesl   = $_GET['kodesl'];
	$namasl   = $_GET['namasl'];
	
	if($kodesl=='')	$namasl='KESELURUHAN';

	$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>LAPORAN LABA RUGI PENJUALAN</li>
		<li>TANGGAL : ".jin_date_str($tgl_awal)." S/D ".jin_date_str($tgl_akhir)."</li></b>
		<li>SALESMAN : $namasl</li>
		<br>
	
		<table width='390' border='1' style='border-collapse:collapse'>
		<tr>
			<th>No.</th>
			<th>Kode Barang</th>
			<th>Nama Barang</th>
			<th>Satuan</th>
			<th>Jumlah Unit</th>
			<th>Harga Netto</th>
			<th>Harga Pokok</th>
			<th>Laba Kotor</th>
			<th>Margin</th>
		</tr>";
				
	if (!empty($kodesl))
		$query= "SELECT kode_st,nama_st,satuan,sum(jml_unit) as tunit,sum(jumlah) as tjumlah,sum(hpp) as thpp FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' AND kode_sl='$kodesl' GROUP BY kode_st";
	else
		$query= "SELECT kode_st,nama_st,satuan,sum(jml_unit) as tunit,sum(jumlah) as tjumlah,sum(hpp) as thpp FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' GROUP BY kode_st";
	
	$data= mysql_query($query);
	$no=1;
	$gtotal=0;
	$gthpp=0;
	$gtlaba=0;
	
	while($r_data=mysql_fetch_array($data)){
		$laba=$r_data[tjumlah]-$r_data[thpp];
		$hpp=$r_data[thpp];
		$kodest=$r_data[kode_st];
		$junit=$r_data[junit];
		$netto=$r_data[tjumlah]-$r_data[tpot];
		
		if (!empty($kodesl))
			$sql= mysql_query("SELECT kode_st,sum(jml_retur) as jretur,sum(jumlah) as nretur,sum(hpp) as jhpp FROM retur_pj WHERE kode_st='$kodest' AND tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_sl='$kodesl' AND batal='' GROUP BY kode_st");
		else
			$sql= mysql_query("SELECT kode_st,sum(jml_retur) as jretur,sum(jumlah) as nretur,sum(hpp) as jhpp FROM retur_pj WHERE kode_st='$kodest' AND tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' GROUP BY kode_st ");
		
		$row=mysql_num_rows($sql);
		if($row>0){
			$r=mysql_fetch_assoc($sql);
			$junit=$junit-$r[jretur];
			$netjual=$netto-$r[nretur];
			$hpp=$hpp-$r[jhpp];
			$laba=$netjual-$hpp;
		}else{
			$netjual=$netto;
		}
		if($hpp==0)
			$margin=100;
		else
			$margin=($laba/$hpp)*100;
		
		$content .= "<tr>	
			<td align='center'>$no</td>
			<td align='center'>$r_data[kode_st]</td>
			<td>$r_data[nama_st]</td>
			<td align='center'>$r_data[satuan]</td>
			<td align='right'>$r_data[tunit]</td>
			<td align='right'>$netjual</td>
			<td align='right'>$hpp</td>
			<td align='right'></td>
			<td align='right'>".number_format($margin,2)." %</td>
		</tr>";
		$gtotal=$gtotal+$netjual;
		$gthpp=$gthpp+$hpp;
		$gtlaba=$gtlaba+$laba;
		$no++;
	}
	$margin=($gtlaba/$gthpp)*100;
		
	$content .= "<tr>
		<td></td>
		<td></td>
		<td></td>			
		<td></td>
		<td align='right'>Total</td>
		<td align='right'>$gtotal</td>
		<td align='right'>$gthpp</td>
		<td align='right'>$gtlaba</td>
		<td align='right'>".number_format($margin,2)." %</td>
	</tr>";
		
	$content .= "</table>";
			
	header("Content-type: application/x-msdownload");
	header("Content-Disposition: attachment; filename=laba_pjl.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	echo $content;
?>